1
0
mirror of https://github.com/postgres/postgres.git synced 2025-05-09 18:21:05 +03:00

Restrict password hash length.

Commit 6aa44060a3 removed pg_authid's TOAST table because the only
varlena column is rolpassword, which cannot be de-TOASTed during
authentication because we haven't selected a database yet and
cannot read pg_class.  Since that change, attempts to set password
hashes that require out-of-line storage will fail with a "row is
too big" error.  This error message might be confusing to users.

This commit places a limit on the length of password hashes so that
attempts to set long password hashes will fail with a more
user-friendly error.  The chosen limit of 512 bytes should be
sufficient to avoid "row is too big" errors independent of BLCKSZ,
but it should also be lenient enough for all reasonable use-cases
(or at least all the use-cases we could imagine).

Reviewed-by: Tom Lane, Jonathan Katz, Michael Paquier, Jacob Champion
Discussion: https://postgr.es/m/89e8649c-eb74-db25-7945-6d6b23992394%40gmail.com
This commit is contained in:
Nathan Bossart 2024-10-07 10:56:16 -05:00
parent 022564f60c
commit 8275325a06
4 changed files with 63 additions and 18 deletions

View File

@ -116,7 +116,7 @@ encrypt_password(PasswordType target_type, const char *role,
const char *password) const char *password)
{ {
PasswordType guessed_type = get_password_type(password); PasswordType guessed_type = get_password_type(password);
char *encrypted_password; char *encrypted_password = NULL;
const char *errstr = NULL; const char *errstr = NULL;
if (guessed_type != PASSWORD_TYPE_PLAINTEXT) if (guessed_type != PASSWORD_TYPE_PLAINTEXT)
@ -125,9 +125,10 @@ encrypt_password(PasswordType target_type, const char *role,
* Cannot convert an already-encrypted password from one format to * Cannot convert an already-encrypted password from one format to
* another, so return it as it is. * another, so return it as it is.
*/ */
return pstrdup(password); encrypted_password = pstrdup(password);
} }
else
{
switch (target_type) switch (target_type)
{ {
case PASSWORD_TYPE_MD5: case PASSWORD_TYPE_MD5:
@ -136,21 +137,44 @@ encrypt_password(PasswordType target_type, const char *role,
if (!pg_md5_encrypt(password, role, strlen(role), if (!pg_md5_encrypt(password, role, strlen(role),
encrypted_password, &errstr)) encrypted_password, &errstr))
elog(ERROR, "password encryption failed: %s", errstr); elog(ERROR, "password encryption failed: %s", errstr);
return encrypted_password; break;
case PASSWORD_TYPE_SCRAM_SHA_256: case PASSWORD_TYPE_SCRAM_SHA_256:
return pg_be_scram_build_secret(password); encrypted_password = pg_be_scram_build_secret(password);
break;
case PASSWORD_TYPE_PLAINTEXT: case PASSWORD_TYPE_PLAINTEXT:
elog(ERROR, "cannot encrypt password with 'plaintext'"); elog(ERROR, "cannot encrypt password with 'plaintext'");
break;
}
} }
Assert(encrypted_password);
/* /*
* This shouldn't happen, because the above switch statements should * Valid password hashes may be very long, but we don't want to store
* handle every combination of source and target password types. * anything that might need out-of-line storage, since de-TOASTing won't
* work during authentication because we haven't selected a database yet
* and cannot read pg_class. 512 bytes should be more than enough for all
* practical use, so fail for anything longer.
*/ */
elog(ERROR, "cannot encrypt password to requested type"); if (encrypted_password && /* keep compiler quiet */
return NULL; /* keep compiler quiet */ strlen(encrypted_password) > MAX_ENCRYPTED_PASSWORD_LEN)
{
/*
* We don't expect any of our own hashing routines to produce hashes
* that are too long.
*/
Assert(guessed_type != PASSWORD_TYPE_PLAINTEXT);
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("encrypted password is too long"),
errdetail("Encrypted passwords must be no longer than %d bytes.",
MAX_ENCRYPTED_PASSWORD_LEN)));
}
return encrypted_password;
} }
/* /*

View File

@ -15,6 +15,16 @@
#include "datatype/timestamp.h" #include "datatype/timestamp.h"
/*
* Valid password hashes may be very long, but we don't want to store anything
* that might need out-of-line storage, since de-TOASTing won't work during
* authentication because we haven't selected a database yet and cannot read
* pg_class. 512 bytes should be more than enough for all practical use, and
* our own password encryption routines should never produce hashes longer than
* this.
*/
#define MAX_ENCRYPTED_PASSWORD_LEN (512)
/* /*
* Types of password hashes or secrets. * Types of password hashes or secrets.
* *

View File

@ -127,6 +127,13 @@ SELECT rolname, rolpassword not like '%A6xHKoH/494E941doaPOYg==%' as is_rolpassw
regress_passwd_sha_len2 | t regress_passwd_sha_len2 | t
(3 rows) (3 rows)
-- Test that valid hashes that are too long are rejected
CREATE ROLE regress_passwd10 PASSWORD 'SCRAM-SHA-256$000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004096:wNFxNSk1hAXBkgub8py3bg==$65zC6E+R0U7tiYTC9+Wtq4Thw6gUDj3eDCINij8TflU=:rC1I7tcVugrHEY2DT0iPjGyjM4aJxkMM9n8WBxtUtHU=';
ERROR: encrypted password is too long
DETAIL: Encrypted passwords must be no longer than 512 bytes.
ALTER ROLE regress_passwd9 PASSWORD 'SCRAM-SHA-256$000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004096:wNFxNSk1hAXBkgub8py3bg==$65zC6E+R0U7tiYTC9+Wtq4Thw6gUDj3eDCINij8TflU=:rC1I7tcVugrHEY2DT0iPjGyjM4aJxkMM9n8WBxtUtHU=';
ERROR: encrypted password is too long
DETAIL: Encrypted passwords must be no longer than 512 bytes.
DROP ROLE regress_passwd1; DROP ROLE regress_passwd1;
DROP ROLE regress_passwd2; DROP ROLE regress_passwd2;
DROP ROLE regress_passwd3; DROP ROLE regress_passwd3;

View File

@ -95,6 +95,10 @@ SELECT rolname, rolpassword not like '%A6xHKoH/494E941doaPOYg==%' as is_rolpassw
WHERE rolname LIKE 'regress_passwd_sha_len%' WHERE rolname LIKE 'regress_passwd_sha_len%'
ORDER BY rolname; ORDER BY rolname;
-- Test that valid hashes that are too long are rejected
CREATE ROLE regress_passwd10 PASSWORD 'SCRAM-SHA-256$000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004096:wNFxNSk1hAXBkgub8py3bg==$65zC6E+R0U7tiYTC9+Wtq4Thw6gUDj3eDCINij8TflU=:rC1I7tcVugrHEY2DT0iPjGyjM4aJxkMM9n8WBxtUtHU=';
ALTER ROLE regress_passwd9 PASSWORD 'SCRAM-SHA-256$000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004096:wNFxNSk1hAXBkgub8py3bg==$65zC6E+R0U7tiYTC9+Wtq4Thw6gUDj3eDCINij8TflU=:rC1I7tcVugrHEY2DT0iPjGyjM4aJxkMM9n8WBxtUtHU=';
DROP ROLE regress_passwd1; DROP ROLE regress_passwd1;
DROP ROLE regress_passwd2; DROP ROLE regress_passwd2;
DROP ROLE regress_passwd3; DROP ROLE regress_passwd3;