diff options
author | Michael Paquier <michael@paquier.xyz> | 2022-01-11 09:55:16 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2022-01-11 09:55:16 +0900 |
commit | b69aba74578adf58f48de0f21dceddc2d9f1c4b1 (patch) | |
tree | 14d04301193cb381c9f6deda914714be6f24d0a8 /src/backend/libpq/crypt.c | |
parent | 9ef2c655558bb1ec20c4f87850d9322420e35acb (diff) | |
download | postgresql-b69aba74578adf58f48de0f21dceddc2d9f1c4b1.tar.gz postgresql-b69aba74578adf58f48de0f21dceddc2d9f1c4b1.zip |
Improve error handling of cryptohash computations
The existing cryptohash facility was causing problems in some code paths
related to MD5 (frontend and backend) that relied on the fact that the
only type of error that could happen would be an OOM, as the MD5
implementation used in PostgreSQL ~13 (the in-core implementation is
used when compiling with or without OpenSSL in those older versions),
could fail only under this circumstance.
The new cryptohash facilities can fail for reasons other than OOMs, like
attempting MD5 when FIPS is enabled (upstream OpenSSL allows that up to
1.0.2, Fedora and Photon patch OpenSSL 1.1.1 to allow that), so this
would cause incorrect reports to show up.
This commit extends the cryptohash APIs so as callers of those routines
can fetch more context when an error happens, by using a new routine
called pg_cryptohash_error(). The error states are stored within each
implementation's internal context data, so as it is possible to extend
the logic depending on what's suited for an implementation. The default
implementation requires few error states, but OpenSSL could report
various issues depending on its internal state so more is needed in
cryptohash_openssl.c, and the code is shaped so as we are always able to
grab the necessary information.
The core code is changed to adapt to the new error routine, painting
more "const" across the call stack where the static errors are stored,
particularly in authentication code paths on variables that provide
log details. This way, any future changes would warn if attempting to
free these strings. The MD5 authentication code was also a bit blurry
about the handling of "logdetail" (LOG sent to the postmaster), so
improve the comments related that, while on it.
The origin of the problem is 87ae969, that introduced the centralized
cryptohash facility. Extra changes are done for pgcrypto in v14 for the
non-OpenSSL code path to cope with the improvements done by this
commit.
Reported-by: Michael Mühlbeyer
Author: Michael Paquier
Reviewed-by: Tom Lane
Discussion: https://postgr.es/m/89B7F072-5BBE-4C92-903E-D83E865D9367@trivadis.com
Backpatch-through: 14
Diffstat (limited to 'src/backend/libpq/crypt.c')
-rw-r--r-- | src/backend/libpq/crypt.c | 38 |
1 files changed, 17 insertions, 21 deletions
diff --git a/src/backend/libpq/crypt.c b/src/backend/libpq/crypt.c index 7ebcdd71236..1ff8b0507d4 100644 --- a/src/backend/libpq/crypt.c +++ b/src/backend/libpq/crypt.c @@ -34,7 +34,7 @@ * sent to the client, to avoid giving away user information! */ char * -get_role_password(const char *role, char **logdetail) +get_role_password(const char *role, const char **logdetail) { TimestampTz vuntil = 0; HeapTuple roleTup; @@ -116,6 +116,7 @@ encrypt_password(PasswordType target_type, const char *role, { PasswordType guessed_type = get_password_type(password); char *encrypted_password; + const char *errstr = NULL; if (guessed_type != PASSWORD_TYPE_PLAINTEXT) { @@ -132,8 +133,8 @@ encrypt_password(PasswordType target_type, const char *role, encrypted_password = palloc(MD5_PASSWD_LEN + 1); if (!pg_md5_encrypt(password, role, strlen(role), - encrypted_password)) - elog(ERROR, "password encryption failed"); + encrypted_password, &errstr)) + elog(ERROR, "password encryption failed: %s", errstr); return encrypted_password; case PASSWORD_TYPE_SCRAM_SHA_256: @@ -159,17 +160,18 @@ encrypt_password(PasswordType target_type, const char *role, * 'client_pass' is the response given by the remote user to the MD5 challenge. * 'md5_salt' is the salt used in the MD5 authentication challenge. * - * In the error case, optionally store a palloc'd string at *logdetail - * that will be sent to the postmaster log (but not the client). + * In the error case, save a string at *logdetail that will be sent to the + * postmaster log (but not the client). */ int md5_crypt_verify(const char *role, const char *shadow_pass, const char *client_pass, const char *md5_salt, int md5_salt_len, - char **logdetail) + const char **logdetail) { int retval; char crypt_pwd[MD5_PASSWD_LEN + 1]; + const char *errstr = NULL; Assert(md5_salt_len > 0); @@ -183,16 +185,13 @@ md5_crypt_verify(const char *role, const char *shadow_pass, /* * Compute the correct answer for the MD5 challenge. - * - * We do not bother setting logdetail for any pg_md5_encrypt failure - * below: the only possible error is out-of-memory, which is unlikely, and - * if it did happen adding a psprintf call would only make things worse. */ /* stored password already encrypted, only do salt */ if (!pg_md5_encrypt(shadow_pass + strlen("md5"), md5_salt, md5_salt_len, - crypt_pwd)) + crypt_pwd, &errstr)) { + *logdetail = errstr; return STATUS_ERROR; } @@ -215,15 +214,16 @@ md5_crypt_verify(const char *role, const char *shadow_pass, * pg_authid.rolpassword. * 'client_pass' is the password given by the remote user. * - * In the error case, optionally store a palloc'd string at *logdetail - * that will be sent to the postmaster log (but not the client). + * In the error case, store a string at *logdetail that will be sent to the + * postmaster log (but not the client). */ int plain_crypt_verify(const char *role, const char *shadow_pass, const char *client_pass, - char **logdetail) + const char **logdetail) { char crypt_client_pass[MD5_PASSWD_LEN + 1]; + const char *errstr = NULL; /* * Client sent password in plaintext. If we have an MD5 hash stored, hash @@ -251,14 +251,10 @@ plain_crypt_verify(const char *role, const char *shadow_pass, if (!pg_md5_encrypt(client_pass, role, strlen(role), - crypt_client_pass)) + crypt_client_pass, + &errstr)) { - /* - * We do not bother setting logdetail for pg_md5_encrypt - * failure: the only possible error is out-of-memory, which is - * unlikely, and if it did happen adding a psprintf call would - * only make things worse. - */ + *logdetail = errstr; return STATUS_ERROR; } if (strcmp(crypt_client_pass, shadow_pass) == 0) |