Skip to content

Commit 969ab9d

Browse files
committed
Follow-up fixes for SHA-2 patch (commit 749a9e2).
This changes the check for valid characters in the salt string to only allow plain ASCII letters and digits. The previous coding was locale-dependent which doesn't really seem like a great idea here; moreover it could not work correctly in multibyte encodings. This fixes a careless pointer-use-after-pfree, too. Reported-by: Tom Lane <tgl@sss.pgh.pa.us> Reported-by: Andres Freund <andres@anarazel.de> Author: Bernd Helmle <mailings@oopsware.de> Discussion: https://postgr.es/m/6fab35422df6b6b9727fdcc243c5fa1c667dd3b5.camel@oopsware.de
1 parent b73e6d7 commit 969ab9d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

contrib/pgcrypto/crypt-sha.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "postgres.h"
4747

4848
#include "common/string.h"
49+
#include "mb/pg_wchar.h"
4950
#include "miscadmin.h"
5051

5152
#include "px-crypt.h"
@@ -58,7 +59,7 @@ typedef enum
5859
PGCRYPTO_SHA_UNKOWN
5960
} PGCRYPTO_SHA_t;
6061

61-
static unsigned char _crypt_itoa64[64 + 1] =
62+
static const char _crypt_itoa64[64 + 1] =
6263
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
6364

6465
/*
@@ -321,10 +322,13 @@ px_crypt_shacrypt(const char *pw, const char *salt, char *passwd, unsigned dstle
321322

322323
if (*ep != '$')
323324
{
324-
if (isalpha(*ep) || isdigit(*ep) || (*ep == '.') || (*ep == '/'))
325+
if (strchr(_crypt_itoa64, *ep) != NULL)
325326
appendStringInfoCharMacro(decoded_salt, *ep);
326327
else
327-
elog(ERROR, "invalid character in salt string: \"%c\"", *ep);
328+
ereport(ERROR,
329+
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
330+
errmsg("invalid character in salt string: \"%.*s\"",
331+
pg_mblen(ep), ep));
328332
}
329333
else
330334
{
@@ -602,8 +606,6 @@ px_crypt_shacrypt(const char *pw, const char *salt, char *passwd, unsigned dstle
602606
elog(ERROR, "unsupported digest length");
603607
}
604608

605-
*cp = '\0';
606-
607609
/*
608610
* Copy over result to specified buffer.
609611
*

0 commit comments

Comments
 (0)