Skip to content

Commit 310ef84

Browse files
Andrey Rachitskiyhackorum
authored andcommitted
pgcrypto: Reject crypt() results invalid in database encoding
crypt() copies a fixed number of caller-supplied salt bytes into its text result. When a multibyte character straddles that cut, the result contains a truncated sequence that is not valid in the database encoding, yet can still be stored. Validate the result with pg_verifymbstr(), as already done for pgp_*_decrypt_text. Verifying a password with crypt() now raises an error if the stored hash is not valid in the database encoding, for the correct password as well as a wrong one. Such a hash could not be restored from a dump. Bug: #19600 Author: Andrey Rachitskiy <pl0h0yp1@gmail.com> Reported-by: Michael Malis <malis@pgrust.com> Reviewed-by: Ishjaap Singh <ishjaap.singh07@gmail.com> Discussion: https://postgr.es/m/19600-e5fb479f50f5022d@postgresql.org
1 parent 7340144 commit 310ef84

6 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎contrib/pgcrypto/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ PGFILEDESC = "pgcrypto - cryptographic functions"
4242

4343
REGRESS = init md5 sha1 hmac-md5 hmac-sha1 blowfish rijndael \
4444
sha2 des 3des cast5 \
45-
crypt-des crypt-md5 crypt-blowfish crypt-xdes \
45+
crypt-des crypt-md5 crypt-blowfish crypt-xdes crypt-utf8 \
4646
pgp-armor pgp-decrypt pgp-encrypt pgp-encrypt-md5 $(CF_PGP_TESTS) \
4747
pgp-pubkey-decrypt pgp-pubkey-encrypt pgp-pubkey-session \
4848
pgp-info crypt-shacrypt
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Needs UTF8; skip otherwise (truncated multibyte salt copy). */
2+
SELECT getdatabaseencoding() <> 'UTF8' AS skip_test \gset
3+
\if :skip_test
4+
\quit
5+
\endif
6+
-- Salt bytes e282ac41 (euro then A). DES copies only e282, which is invalid UTF8.
7+
SELECT crypt('password', convert_from(decode('e282ac41', 'hex'), 'utf8'));
8+
ERROR: invalid byte sequence for encoding "UTF8": 0xe2 0x82 0x55
9+
-- Salt bytes 5f4a392e2e6162e282ac (_J9..ab then euro). xDES copies 9 bytes and drops ac.
10+
SELECT crypt('password', convert_from(decode('5f4a392e2e6162e282ac', 'hex'), 'utf8'));
11+
ERROR: invalid byte sequence for encoding "UTF8": 0xe2 0x82 0x48
12+
-- Salt bytes 24312431323334353637e282ac ($1$1234567 then euro). MD5 copies 8 salt bytes and keeps a trailing e2.
13+
SELECT crypt('password', convert_from(decode('24312431323334353637e282ac', 'hex'), 'utf8'));
14+
ERROR: invalid byte sequence for encoding "UTF8": 0xe2 0x24 0x69
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Needs UTF8; skip otherwise (truncated multibyte salt copy). */
2+
SELECT getdatabaseencoding() <> 'UTF8' AS skip_test \gset
3+
\if :skip_test
4+
\quit

‎contrib/pgcrypto/meson.build‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pgcrypto_regress = [
4646
'crypt-md5',
4747
'crypt-blowfish',
4848
'crypt-xdes',
49+
'crypt-utf8',
4950
'pgp-armor',
5051
'pgp-decrypt',
5152
'pgp-encrypt',

‎contrib/pgcrypto/pgcrypto.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include <ctype.h>
3535

36+
#include "mb/pg_wchar.h"
3637
#include "parser/scansup.h"
3738
#include "pgcrypto.h"
3839
#include "px-crypt.h"
@@ -239,6 +240,9 @@ pg_crypt(PG_FUNCTION_ARGS)
239240

240241
pfree(resbuf);
241242

243+
/* Ensure text result is valid in the database encoding. */
244+
pg_verifymbstr(VARDATA_ANY(res), VARSIZE_ANY_EXHDR(res), false);
245+
242246
PG_FREE_IF_COPY(arg0, 0);
243247
PG_FREE_IF_COPY(arg1, 1);
244248

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* Needs UTF8; skip otherwise (truncated multibyte salt copy). */
2+
SELECT getdatabaseencoding() <> 'UTF8' AS skip_test \gset
3+
\if :skip_test
4+
\quit
5+
\endif
6+
-- Salt bytes e282ac41 (euro then A). DES copies only e282, which is invalid UTF8.
7+
SELECT crypt('password', convert_from(decode('e282ac41', 'hex'), 'utf8'));
8+
-- Salt bytes 5f4a392e2e6162e282ac (_J9..ab then euro). xDES copies 9 bytes and drops ac.
9+
SELECT crypt('password', convert_from(decode('5f4a392e2e6162e282ac', 'hex'), 'utf8'));
10+
-- Salt bytes 24312431323334353637e282ac ($1$1234567 then euro). MD5 copies 8 salt bytes and keeps a trailing e2.
11+
SELECT crypt('password', convert_from(decode('24312431323334353637e282ac', 'hex'), 'utf8'));

0 commit comments

Comments
 (0)