8000 libpq: Fix minor TOCTOU violation · macdice/postgres@e882bca · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit e882bca

Browse files
committed
libpq: Fix minor TOCTOU violation
libpq checks the permissions of the password file before opening it. The way this is done in two separate operations, a static analyzer would flag as a time-of-check-time-of-use violation. In practice, you can't do anything with that, but it still seems better style to fix it. To fix it, open the file first and then check the permissions on the opened file handle. Reviewed-by: Aleksander Alekseev <aleksander@timescale.com> Reviewed-by: Andreas Karlsson <andreas@proxel.se> Discussion: https://www.postgresql.org/message-id/flat/a3356054-14ae-4e7a-acc6-249d19dac20b%40eisentraut.org
1 parent e3ec9dc commit e882bca

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7452,7 +7452,9 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname,
74527452
const char *username, const char *pgpassfile)
74537453
{
74547454
FILE *fp;
7455+
#ifndef WIN32
74557456
struct stat stat_buf;
7457+
#endif
74567458
PQExpBufferData buf;
74577459

74587460
if (dbname == NULL || dbname[0] == '\0')
@@ -7477,10 +7479,14 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname,
74777479
port = DEF_PGPORT_STR;
74787480

74797481
/* If password file cannot be opened, ignore it. */
7480-
if (stat(pgpassfile, &stat_buf) != 0)
7482+
fp = fopen(pgpassfile, "r");
7483+
if (fp == NULL)
74817484
return NULL;
74827485

74837486
#ifndef WIN32
7487+
if (fstat(fileno(fp), &stat_buf) != 0)
7488+
return NULL;
7489+
74847490
if (!S_ISREG(stat_buf.st_mode))
74857491
{
74867492
fprintf(stderr,
@@ -7505,10 +7511,6 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname,
75057511
*/
75067512
#endif
75077513

7508-
fp = fopen(pgpassfile, "r");
7509-
if (fp == NULL)
7510-
return NULL;
7511-
75127514
/* Use an expansible buffer to accommodate any reasonable line length */
75137515
initPQExpBuffer(&buf);
75147516

0 commit comments

Comments
0