Skip to content

Commit b2f833e

Browse files
committed
Don't allow logging in with empty password.
Some authentication methods allowed it, others did not. In the client-side, libpq does not even try to authenticate with an empty password, which makes using empty passwords hazardous: an administrator might think that an account with an empty password cannot be used to log in, because psql doesn't allow it, and not realize that a different client would in fact allow it. To clear that confusion and to be be consistent, disallow empty passwords in all authentication methods. All the authentication methods that used plaintext authentication over the wire, except for BSD authentication, already checked that the password received from the user was not empty. To avoid forgetting it in the future again, move the check to the recv_password_packet function. That only forbids using an empty password with plaintext authentication, however. MD5 and SCRAM need a different fix: * In stable branches, check that the MD5 hash stored for the user does not not correspond to an empty string. This adds some overhead to MD5 authentication, because the server needs to compute an extra MD5 hash, but it is not noticeable in practice. * In HEAD, modify CREATE and ALTER ROLE to clear the password if an empty string, or a password hash that corresponds to an empty string, is specified. The user-visible behavior is the same as in the stable branches, the user cannot log in, but it seems better to stop the empty password from entering the system in the first place. Secondly, it is fairly expensive to check that a SCRAM hash doesn't correspond to an empty string, because computing a SCRAM hash is much more expensive than an MD5 hash by design, so better avoid doing that on every authentication. We could clear the password on CREATE/ALTER ROLE also in stable branches, but we would still need to check at authentication time, because even if we prevent empty passwords from being stored in pg_authid, there might be existing ones there already. Reported by Jeroen van der Ham, Ben de Graaff and Jelte Fennema. Security: CVE-2017-7546
1 parent 73c2b36 commit b2f833e

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

src/backend/libpq/auth.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,20 @@ recv_password_packet(Port *port)
707707
(errcode(ERRCODE_PROTOCOL_VIOLATION),
708708
errmsg("invalid password packet size")));
709709

710+
/*
711+
* Don't allow an empty password. Libpq treats an empty password the same
712+
* as no password at all, and won't even try to authenticate. But other
713+
* clients might, so allowing it would be confusing.
714+
*
715+
* Note that this only catches an empty password sent by the client in
716+
* plaintext. There's another check in md5_crypt_verify to prevent an
717+
* empty password from being used with MD5 authentication.
718+
*/
719+
if (buf.data[0] == '\0')
720+
ereport(ERROR,
721+
(errcode(ERRCODE_INVALID_PASSWORD),
722+
errmsg("empty password returned by client")));
723+
710724
/* Do not echo password to logs, for security. */
711725
ereport(DEBUG5,
712726
(errmsg("received password packet")));
@@ -1896,12 +1910,6 @@ pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg,
18961910
*/
18971911
goto fail;
18981912
}
1899-
if (strlen(passwd) == 0)
1900-
{
1901-
ereport(LOG,
1902-
(errmsg("empty password returned by client")));
1903-
goto fail;
1904-
}
19051913
}
19061914
if ((reply[i].resp = strdup(passwd)) == NULL)
19071915
goto fail;
@@ -2167,16 +2175,11 @@ CheckLDAPAuth(Port *port)
21672175
if (passwd == NULL)
21682176
return STATUS_EOF; /* client wouldn't send password */
21692177

2170-
if (strlen(passwd) == 0)
2171-
{
2172-
ereport(LOG,
2173-
(errmsg("empty password returned by client")));
2174-
return STATUS_ERROR;
2175-
}
2176-
21772178
if (InitializeLDAPConnection(port, &ldap) == STATUS_ERROR)
2179+
{
21782180
/* Error message already sent */
21792181
return STATUS_ERROR;
2182+
}
21802183

21812184
if (port->hba->ldapbasedn)
21822185
{
@@ -2535,13 +2538,6 @@ CheckRADIUSAuth(Port *port)
25352538
if (passwd == NULL)
25362539
return STATUS_EOF; /* client wouldn't send password */
25372540

2538-
if (strlen(passwd) == 0)
2539-
{
2540-
ereport(LOG,
2541-
(errmsg("empty password returned by client")));
2542-
return STATUS_ERROR;
2543-
}
2544-
25452541
if (strlen(passwd) > RADIUS_VECTOR_LENGTH)
25462542
{
25472543
ereport(LOG,

src/backend/libpq/crypt.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,35 @@ md5_crypt_verify(const Port *port, const char *role, char *client_pass)
6969

7070
ReleaseSysCache(roleTup);
7171

72-
if (*shadow_pass == '\0')
73-
return STATUS_ERROR; /* empty password */
74-
7572
/* Re-enable immediate response to SIGTERM/SIGINT/timeout interrupts */
7673
ImmediateInterruptOK = true;
7774
/* And don't forget to detect one that already arrived */
7875
CHECK_FOR_INTERRUPTS();
7976

77+
/*
78+
* Don't allow an empty password. Libpq treats an empty password the same
79+
* as no password at all, and won't even try to authenticate. But other
80+
* clients might, so allowing it would be confusing.
81+
*
82+
* For a plaintext password, we can simply check that it's not an empty
83+
* string. For an encrypted password, check that it does not match the MD5
84+
* hash of an empty string.
85+
*/
86+
if (*shadow_pass == '\0')
87+
return STATUS_ERROR; /* empty password */
88+
if (isMD5(shadow_pass))
89+
{
90+
char crypt_empty[MD5_PASSWD_LEN + 1];
91+
92+
if (!pg_md5_encrypt("",
93+
port->user_name,
94+
strlen(port->user_name),
95+
crypt_empty))
96+
return STATUS_ERROR;
97+
if (strcmp(shadow_pass, crypt_empty) == 0)
98+
return STATUS_ERROR; /* empty password */
99+
}
100+
80101
/*
81102
* Compare with the encrypted or plain password depending on the
82103
* authentication method being used for this connection.

0 commit comments

Comments
 (0)