8000 Be more paranoid about null return values from libpq status functions. · SudhirLonkar/postgres@6430a11 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6430a11

Browse files
committed
Be more paranoid about null return values from libpq status functions.
PQhost() can return NULL in non-error situations, namely when a Unix-socket connection has been selected by default. That behavior is a tad debatable perhaps, but for the moment we should make sure that psql copes with it. Unfortunately, do_connect() failed to: it could pass a NULL pointer to strcmp(), resulting in crashes on most platforms. This was reported as a security issue by ChenQin of Topsec Security Team, but the consensus of the security list is that it's just a garden-variety bug with no security implications. For paranoia's sake, I made the keep_password test not trust PQuser or PQport either, even though I believe those will never return NULL given a valid PGconn. Back-patch to all supported branches.
1 parent c36064e commit 6430a11

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/bin/psql/command.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,15 +1483,18 @@ do_connect(char *dbname, char *user, char *host, char *port)
14831483

14841484
/*
14851485
* Any change in the parameters read above makes us discard the password.
1486-
* We also discard it if we're to use a conninfo rather than the positional
1487-
* syntax.
1486+
* We also discard it if we're to use a conninfo rather than the
1487+
* positional syntax. Note that currently, PQhost() can return NULL for a
1488+
* default Unix-socket connection, so we have to allow NULL for host.
14881489
*/
1489-
keep_password =
1490-
(o_conn &&
1491-
(strcmp(user, PQuser(o_conn)) == 0) &&
1492-
(!host || strcmp(host, PQhost(o_conn)) == 0) &&
1493-
(strcmp(port, PQport(o_conn)) == 0) &&
1494-
!has_connection_string);
1490+
if (has_connection_string)
1491+
keep_password = false;
1492+
else
1493+
keep_password =
1494+
(user && PQuser(o_conn) && strcmp(user, PQuser(o_conn)) == 0) &&
1495+
((host && PQhost(o_conn) && strcmp(host, PQhost(o_conn)) == 0) ||
1496+
(host == NULL && PQhost(o_conn) == NULL)) &&
1497+
(port && PQport(o_conn) && strcmp(port, PQport(o_conn)) == 0);
14951498

14961499
/*
14971500
* Grab dbname from old connection unless supplied by caller. No password
@@ -1503,8 +1506,8 @@ do_connect(char *dbname, char *user, char *host, char *port)
15031506
/*
15041507
* If the user asked to be prompted for a password, ask for one now. If
15051508
* not, use the password from the old connection, provided the username
1506-
* has not changed. Otherwise, try to connect without a password first,
1507-
* and then ask for a password if needed.
1509+
* etc have not changed. Otherwise, try to connect without a password
1510+
* first, and then ask for a password if needed.
15081511
*
15091512
* XXX: this behavior leads to spurious connection attempts recorded in
15101513
* the postmaster's log. But libpq offers no API that would let us obtain

0 commit comments

Comments
 (0)
0