Skip to content

Commit c61a2f5

Browse files
committed
Change the backend to reject strings containing invalidly-encoded multibyte
characters in all cases. Formerly we mostly just threw warnings for invalid input, and failed to detect it at all if no encoding conversion was required. The tighter check is needed to defend against SQL-injection attacks as per CVE-2006-2313 (further details will be published after release). Embedded zero (null) bytes will be rejected as well. The checks are applied during input to the backend (receipt from client or COPY IN), so it no longer seems necessary to check in textin() and related routines; any string arriving at those functions will already have been validated. Conversion failure reporting (for characters with no equivalent in the destination encoding) has been cleaned up and made consistent while at it. Also, fix a few longstanding errors in little-used encoding conversion routines: win1251_to_iso, win866_to_iso, euc_tw_to_big5, euc_tw_to_mic, mic_to_euc_tw were all broken to varying extents. Patches by Tatsuo Ishii and Tom Lane. Thanks to Akio Ishida and Yasuo Ohgaki for identifying the security issues.
1 parent 1f219cf commit c61a2f5

File tree

31 files changed

+1527
-932
lines changed

31 files changed

+1527
-932
lines changed

src/backend/commands/copy.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.263 2006/04/05 22:11:54 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.264 2006/05/21 20:05:19 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1023,9 +1023,15 @@ DoCopy(const CopyStmt *stmt)
10231023
cstate->raw_buf_index = cstate->raw_buf_len = 0;
10241024
cstate->processed = 0;
10251025

1026-
/* Set up encoding conversion info */
1026+
/*
1027+
* Set up encoding conversion info. Even if the client and server
1028+
* encodings are the same, we must apply pg_client_to_server() to
1029+
* validate data in multibyte encodings.
1030+
*/
10271031
cstate->client_encoding = pg_get_client_encoding();
1028-
cstate->need_transcoding = (cstate->client_encoding != GetDatabaseEncoding());
1032+
cstate->need_transcoding =
1033+
(cstate->client_encoding != GetDatabaseEncoding() ||
1034+
pg_database_encoding_max_length() > 1);
10291035
/* See Multibyte encoding comment above */
10301036
cstate->encoding_embeds_ascii = PG_ENCODING_IS_CLIENT_ONLY(cstate->client_encoding);
10311037

src/backend/utils/adt/name.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/utils/adt/name.c,v 1.57 2006/03/05 15:58:43 momjian Exp $
17+
* $PostgreSQL: pgsql/src/backend/utils/adt/name.c,v 1.58 2006/05/21 20:05:19 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -49,10 +49,7 @@ namein(PG_FUNCTION_ARGS)
4949
NameData *result;
5050
int len;
5151

52-
/* verify encoding */
5352
len = strlen(s);
54-
pg_verifymbstr(s, len, false);
55-
5653
len = pg_mbcliplen(s, len, NAMEDATALEN - 1);
5754

5855
result = (NameData *) palloc0(NAMEDATALEN);

src/backend/utils/adt/varchar.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/varchar.c,v 1.115 2006/03/05 15:58:44 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/varchar.c,v 1.116 2006/05/21 20:05:19 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -73,9 +73,6 @@ bpchar_input(const char *s, size_t len, int32 atttypmod)
7373
char *r;
7474
size_t maxlen;
7575

76-
/* verify encoding */
77-
pg_verifymbstr(s, len, false);
78-
7976
/* If typmod is -1 (or invalid), use the actual string length */
8077
if (atttypmod < (int32) VARHDRSZ)
8178
maxlen = len;
@@ -393,9 +390,6 @@ varchar_input(const char *s, size_t len, int32 atttypmod)
393390
VarChar *result;
394391
size_t maxlen;
395392

396-
/* verify encoding */
397-
pg_verifymbstr(s, len, false);
398-
399393
maxlen = atttypmod - VARHDRSZ;
400394

401395
if (atttypmod >= (int32) VARHDRSZ && len > maxlen)

src/backend/utils/adt/varlena.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.146 2006/04/04 19:35:36 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.147 2006/05/21 20:05:19 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -256,10 +256,7 @@ textin(PG_FUNCTION_ARGS)
256256
text *result;
257257
int len;
258258

259-
/* verify encoding */
260259
len = strlen(inputText);
261-
pg_verifymbstr(inputText, len, false);
262-
263260
result = (text *) palloc(len + VARHDRSZ);
264261
VARATT_SIZEP(result) = len + VARHDRSZ;
265262

@@ -299,9 +296,6 @@ textrecv(PG_FUNCTION_ARGS)
299296

300297
str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
301298

302-
/* verify encoding */
303-
pg_verifymbstr(str, nbytes, false);
304-
305299
result = (text *) palloc(nbytes + VARHDRSZ);
306300
VARATT_SIZEP(result) = nbytes + VARHDRSZ;
307301
memcpy(VARDATA(result), str, nbytes);

0 commit comments

Comments
 (0)