summaryrefslogtreecommitdiff
path: root/contrib/pgcrypto
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2021-02-15 10:18:34 +0900
committerMichael Paquier <michael@paquier.xyz>2021-02-15 10:18:34 +0900
commitb83dcf792869fb4a9270d17c961eab75f51c44e4 (patch)
treedd697da2a88c0e07f8b8304351ec9e5e398baa01 /contrib/pgcrypto
parent2dd6733108f2bea07b0a3469e768bd900c0808b3 (diff)
downloadpostgresql-b83dcf792869fb4a9270d17c961eab75f51c44e4.tar.gz
Add result size as argument of pg_cryptohash_final() for overflow checks
With its current design, a careless use of pg_cryptohash_final() could would result in an out-of-bound write in memory as the size of the destination buffer to store the result digest is not known to the cryptohash internals, without the caller knowing about that. This commit adds a new argument to pg_cryptohash_final() to allow such sanity checks, and implements such defenses. The internals of SCRAM for HMAC could be tightened a bit more, but as everything is based on SCRAM_KEY_LEN with uses particular to this code there is no need to complicate its interface more than necessary, and this comes back to the refactoring of HMAC in core. Except that, this minimizes the uses of the existing DIGEST_LENGTH variables, relying instead on sizeof() for the result sizes. In ossp-uuid, this also makes the code more defensive, as it already relied on dce_uuid_t being at least the size of a MD5 digest. This is in philosophy similar to cfc40d3 for base64.c and aef8948 for hex.c. Reported-by: Ranier Vilela Author: Michael Paquier, Ranier Vilela Reviewed-by: Kyotaro Horiguchi Discussion: https://postgr.es/m/CAEudQAoqEGmcff3J4sTSV-R_16Monuz-UpJFbf_dnVH=APr02Q@mail.gmail.com
Diffstat (limited to 'contrib/pgcrypto')
-rw-r--r--contrib/pgcrypto/internal-sha2.c2
-rw-r--r--contrib/pgcrypto/internal.c4
2 files changed, 3 insertions, 3 deletions
diff --git a/contrib/pgcrypto/internal-sha2.c b/contrib/pgcrypto/internal-sha2.c
index 0fe53e15af..ecf3004e95 100644
--- a/contrib/pgcrypto/internal-sha2.c
+++ b/contrib/pgcrypto/internal-sha2.c
@@ -118,7 +118,7 @@ int_sha2_finish(PX_MD *h, uint8 *dst)
{
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
- if (pg_cryptohash_final(ctx, dst) < 0)
+ if (pg_cryptohash_final(ctx, dst, h->result_size(h)) < 0)
elog(ERROR, "could not finalize %s context", "SHA2");
}
diff --git a/contrib/pgcrypto/internal.c b/contrib/pgcrypto/internal.c
index ef6ce2fb1e..dd45fee7ed 100644
--- a/contrib/pgcrypto/internal.c
+++ b/contrib/pgcrypto/internal.c
@@ -106,7 +106,7 @@ int_md5_finish(PX_MD *h, uint8 *dst)
{
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
- if (pg_cryptohash_final(ctx, dst) < 0)
+ if (pg_cryptohash_final(ctx, dst, h->result_size(h)) < 0)
elog(ERROR, "could not finalize %s context", "MD5");
}
@@ -156,7 +156,7 @@ int_sha1_finish(PX_MD *h, uint8 *dst)
{
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
- if (pg_cryptohash_final(ctx, dst) < 0)
+ if (pg_cryptohash_final(ctx, dst, h->result_size(h)) < 0)
elog(ERROR, "could not finalize %s context", "SHA1");
}