Skip to content

Commit 502e5f9

Browse files
committed
Replace a bunch more uses of strncpy() with safer coding.
strncpy() has a well-deserved reputation for being unsafe, so make an effort to get rid of nearly all occurrences in HEAD. A large fraction of the remaining uses were passing length less than or equal to the known strlen() of the source, in which case no null-padding can occur and the behavior is equivalent to memcpy(), though doubtless slower and certainly harder to reason about. So just use memcpy() in these cases. In other cases, use either StrNCpy() or strlcpy() as appropriate (depending on whether padding to the full length of the destination buffer seems useful). I left a few strncpy() calls alone in the src/timezone/ code, to keep it in sync with upstream (the IANA tzcode distribution). There are also a few such calls in ecpg that could possibly do with more analysis. AFAICT, none of these changes are more than cosmetic, except for the four occurrences in fe-secure-openssl.c, which are in fact buggy: an overlength source leads to a non-null-terminated destination buffer and ensuing misbehavior. These don't seem like security issues, first because no stack clobber is possible and second because if your values of sslcert etc are coming from untrusted sources then you've got problems way worse than this. Still, it's undesirable to have unpredictable behavior for overlength inputs, so back-patch those four changes to all active branches.
1 parent bdde191 commit 502e5f9

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/interfaces/libpq/fe-secure.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ initialize_SSL(PGconn *conn)
10751075

10761076
/* Read the client certificate file */
10771077
if (conn->sslcert && strlen(conn->sslcert) > 0)
1078-
strncpy(fnbuf, conn->sslcert, sizeof(fnbuf));
1078+
strlcpy(fnbuf, conn->sslcert, sizeof(fnbuf));
10791079
else if (have_homedir)
10801080
snprintf(fnbuf, sizeof(fnbuf), "%s/%s", homedir, USER_CERT_FILE);
10811081
else
@@ -1266,7 +1266,7 @@ initialize_SSL(PGconn *conn)
12661266
#endif /* USE_SSL_ENGINE */
12671267
{
12681268
/* PGSSLKEY is not an engine, treat it as a filename */
1269-
strncpy(fnbuf, conn->sslkey, sizeof(fnbuf));
1269+
strlcpy(fnbuf, conn->sslkey, sizeof(fnbuf));
12701270
}
12711271
}
12721272
else if (have_homedir)
@@ -1329,7 +1329,7 @@ initialize_SSL(PGconn *conn)
13291329
* verification after the connection has been completed.
13301330
*/
13311331
if (conn->sslrootcert && strlen(conn->sslrootcert) > 0)
1332-
strncpy(fnbuf, conn->sslrootcert, sizeof(fnbuf));
1332+
strlcpy(fnbuf, conn->sslrootcert, sizeof(fnbuf));
13331333
else if (have_homedir)
13341334
snprintf(fnbuf, sizeof(fnbuf), "%s/%s", homedir, ROOT_CERT_FILE);
13351335
else
@@ -1367,7 +1367,7 @@ initialize_SSL(PGconn *conn)
13671367
if ((cvstore = SSL_CTX_get_cert_store(SSL_context)) != NULL)
13681368
{
13691369
if (conn->sslcrl && strlen(conn->sslcrl) > 0)
1370-
strncpy(fnbuf, conn->sslcrl, sizeof(fnbuf));
1370+
strlcpy(fnbuf, conn->sslcrl, sizeof(fnbuf));
13711371
else if (have_homedir)
13721372
snprintf(fnbuf, sizeof(fnbuf), "%s/%s", homedir, ROOT_CRL_FILE);
13731373
else

0 commit comments

Comments
 (0)