pgsql: Improve error handling of cryptohash computations

Started by Michael Paquierover 4 years ago5 messagescomitters
Jump to latest
#1Michael Paquier
michael@paquier.xyz

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: /messages/by-id/89B7F072-5BBE-4C92-903E-D83E865D9367@trivadis.com
Backpatch-through: 14

Branch
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/3a0cced86d3b1bbbf0aaa0fb34e2b15edd643979

Modified Files
--------------
contrib/passwordcheck/passwordcheck.c | 2 +-
contrib/pgcrypto/internal-sha2.c | 9 ++-
contrib/pgcrypto/internal.c | 18 ++++--
contrib/uuid-ossp/uuid-ossp.c | 18 ++++--
src/backend/commands/user.c | 4 +-
src/backend/libpq/auth-scram.c | 2 +-
src/backend/libpq/auth.c | 36 +++++++-----
src/backend/libpq/crypt.c | 38 ++++++-------
src/backend/replication/backup_manifest.c | 9 ++-
src/backend/utils/adt/cryptohashfuncs.c | 25 ++++++---
src/common/cryptohash.c | 57 ++++++++++++++++++-
src/common/cryptohash_openssl.c | 93 +++++++++++++++++++++++++++++++
src/common/md5_common.c | 20 +++++--
src/include/common/cryptohash.h | 1 +
src/include/common/md5.h | 9 ++-
src/include/libpq/crypt.h | 7 ++-
src/include/libpq/scram.h | 3 +-
src/interfaces/libpq/fe-auth.c | 22 ++++++--
src/tools/pgindent/typedefs.list | 1 +
19 files changed, 290 insertions(+), 84 deletions(-)

#2Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Michael Paquier (#1)
Re: pgsql: Improve error handling of cryptohash computations

On Tue, 2022-01-11 at 00:56 +0000, Michael Paquier wrote:

Improve error handling of cryptohash computations

This commit changes the signature of the exported "pg_md5_hash" function,
which breaks my extension that uses this function.

Is there no way to avoid this API break in the back branches?

Yours,
Laurenz Albe

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Laurenz Albe (#2)
Re: pgsql: Improve error handling of cryptohash computations

Laurenz Albe <laurenz.albe@cybertec.at> writes:

On Tue, 2022-01-11 at 00:56 +0000, Michael Paquier wrote:

Improve error handling of cryptohash computations

This commit changes the signature of the exported "pg_md5_hash" function,
which breaks my extension that uses this function.
Is there no way to avoid this API break in the back branches?

Probably none of this should have been back-patched --- the issues
are not so large that they require a risk of breaking things in
the back branches.

regards, tom lane

#4Michael Paquier
michael@paquier.xyz
In reply to: Tom Lane (#3)
Re: pgsql: Improve error handling of cryptohash computations

On Thu, Jan 13, 2022 at 02:52:12PM -0500, Tom Lane wrote:

Probably none of this should have been back-patched --- the issues
are not so large that they require a risk of breaking things in
the back branches.

Agreed, that's not worth breaking things in v14, so I'll go and revert
this stuff in it.
--
Michael

#5Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Michael Paquier (#4)
Re: pgsql: Improve error handling of cryptohash computations

On Fri, 2022-01-14 at 10:49 +0900, Michael Paquier wrote:

On Thu, Jan 13, 2022 at 02:52:12PM -0500, Tom Lane wrote:

Probably none of this should have been back-patched --- the issues
are not so large that they require a risk of breaking things in
the back branches.

Agreed, that's not worth breaking things in v14, so I'll go and revert
this stuff in it.

Thanks!

Yours,
Laurenz Albe