From 96871fc236a19a4c3c74dbab81c63838e7de0c32 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 21 May 2006 20:20:24 +0000 Subject: Modify libpq's string-escaping routines to be aware of encoding considerations and standard_conforming_strings. The encoding changes are needed for proper escaping in multibyte encodings, as per the SQL-injection vulnerabilities noted in CVE-2006-2313 and CVE-2006-2314. Concurrent fixes are being applied to the server to ensure that it rejects queries that may have been corrupted by attempted SQL injection, but this merely guarantees that unpatched clients will fail rather than allow injection. An actual fix requires changing the client-side code. While at it we have also fixed these routines to understand about standard_conforming_strings, so that the upcoming changeover to SQL-spec string syntax can be somewhat transparent to client code. Since the existing API of PQescapeString and PQescapeBytea provides no way to inform them which settings are in use, these functions are now deprecated in favor of new functions PQescapeStringConn and PQescapeByteaConn. The new functions take the PGconn to which the string will be sent as an additional parameter, and look inside the connection structure to determine what to do. So as to provide some functionality for clients using the old functions, libpq stores the latest encoding and standard_conforming_strings values received from the backend in static variables, and the old functions consult these variables. This will work reliably in clients using only one Postgres connection at a time, or even multiple connections if they all use the same encoding and string syntax settings; which should cover many practical scenarios. Clients that use homebrew escaping methods, such as PHP's addslashes() function or even hardwired regexp substitution, will require extra effort to fix :-(. It is strongly recommended that such code be replaced by use of PQescapeStringConn/PQescapeByteaConn if at all feasible. --- doc/src/sgml/libpq.sgml | 165 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 123 insertions(+), 42 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 3e874fbb3ed..a26caa8d1c4 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -1,5 +1,5 @@ @@ -1957,15 +1957,16 @@ It is not thread-safe. Escaping Strings for Inclusion in SQL Commands + PQescapeStringConn PQescapeString escaping strings -PQescapeString escapes a string for use within an SQL +PQescapeStringConn escapes a string for use within an SQL command. This is useful when inserting data values as literal constants in SQL commands. Certain characters (such as quotes and backslashes) must be escaped to prevent them from being interpreted specially by the SQL parser. -PQescapeString performs this operation. +PQescapeStringConn performs this operation. @@ -1983,36 +1984,68 @@ value is passed as a separate parameter in PQexecParams or its sibling routines. -size_t PQescapeString (char *to, const char *from, size_t length); +size_t PQescapeStringConn (PGconn *conn, + char *to, const char *from, size_t length, + int *error); +PQescapeStringConn writes an escaped +version of the from string to the to +buffer, escaping special characters so that they cannot cause any +harm, and adding a terminating zero byte. The single quotes that +must surround PostgreSQL string literals are not +included in the result string; they should be provided in the SQL +command that the result is inserted into. The parameter from points to the first character of the string that is to be escaped, and the length parameter gives the -number of characters in this string. A terminating zero byte is not +number of bytes in this string. A terminating zero byte is not required, and should not be counted in length. (If a terminating zero byte is found before length bytes are -processed, PQescapeString stops at the zero; the behavior +processed, PQescapeStringConn stops at the zero; the behavior is thus rather like strncpy.) to shall point to a -buffer that is able to hold at least one more character than twice +buffer that is able to hold at least one more byte than twice the value of length, otherwise the behavior is -undefined. A call to PQescapeString writes an escaped -version of the from string to the to -buffer, replacing special characters so that they cannot cause any -harm, and adding a terminating zero byte. The single quotes that -must surround PostgreSQL string literals are not -included in the result string; they should be provided in the SQL -command that the result is inserted into. +undefined. +Behavior is likewise undefined if the to and from +strings overlap. + + +If the error parameter is not NULL, then *error +is set to zero on success, nonzero on error. Presently the only possible +error conditions involve invalid multibyte encoding in the source string. +The output string is still generated on error, but it can be expected that +the server will reject it as malformed. On error, a suitable message is +stored in the conn object, whether or not error +is NULL. -PQescapeString returns the number of characters written +PQescapeStringConn returns the number of bytes written to to, not including the terminating zero byte. + -Behavior is undefined if the to and from -strings overlap. + +size_t PQescapeString (char *to, const char *from, size_t length); + + + + +PQescapeString is an older, deprecated version of +PQescapeStringConn; the difference is that it does not +take conn or error parameters. Because of this, +it cannot adjust its behavior depending on the connection properties (such as +character encoding) and therefore it may give the wrong results. +Also, it has no way to report error conditions. + + +PQescapeString can be used safely in single-threaded client +programs that work with only one PostgreSQL connection at +a time (in this case it can find out what it needs to know behind the +scenes). In other contexts it is a security hazard and should be avoided +in favor of PQescapeStringConn. @@ -2027,29 +2060,30 @@ strings overlap. - PQescapeByteaPQescapeBytea + PQescapeByteaConnPQescapeByteaConn Escapes binary data for use within an SQL command with the type - bytea. As with PQescapeString, + bytea. As with PQescapeStringConn, this is only used when inserting data directly into an SQL command string. -unsigned char *PQescapeBytea(const unsigned char *from, - size_t from_length, - size_t *to_length); +unsigned char *PQescapeByteaConn(PGconn *conn, + const unsigned char *from, + size_t from_length, + size_t *to_length); Certain byte values must be escaped (but all - byte values may be escaped) when used as part + byte values can be escaped) when used as part of a bytea literal in an SQL statement. In general, to escape a byte, it is converted into the three digit octal number equal to the octet value, and preceded by - two backslashes. The single quote (') and backslash + one or two backslashes. The single quote (') and backslash (\) characters have special alternative escape sequences. See for more - information. PQescapeBytea performs this + information. PQescapeByteaConn performs this operation, escaping only the minimally required bytes. @@ -2060,22 +2094,60 @@ unsigned char *PQescapeBytea(const unsigned char *from, bytes in this binary string. (A terminating zero byte is neither necessary nor counted.) The to_length parameter points to a variable that will hold the resultant - escaped string length. The result string length includes the terminating + escaped string length. This result string length includes the terminating zero byte of the result. - PQescapeBytea returns an escaped version of the + PQescapeByteaConn returns an escaped version of the from parameter binary string in memory - allocated with malloc(). This memory must be freed - using PQfreemem when the result is no longer needed. - The return string has all special characters replaced so that they - can be properly processed by the - PostgreSQL string literal parser, and - the bytea input function. A terminating zero byte is - also added. The single quotes that must surround - PostgreSQL string literals are not part - of the result string. + allocated with malloc(). This memory must be freed using + PQfreemem() when the result is no longer needed. The + return string has all special characters replaced so that they can + be properly processed by the PostgreSQL + string literal parser, and the bytea input function. A + terminating zero byte is also added. The single quotes that must + surround PostgreSQL string literals are + not part of the result string. + + + + On error, a NULL pointer is returned, and a suitable error message + is stored in the conn object. Currently, the only + possible error is insufficient memory for the result string. + + + + + + PQescapeByteaPQescapeBytea + + + PQescapeBytea is an older, deprecated version of + PQescapeByteaConn. + +unsigned char *PQescapeBytea(const unsigned char *from, + size_t from_length, + size_t *to_length); + + + + + The only difference from PQescapeByteaConn is that + PQescapeBytea does not + take a PGconn parameter. Because of this, it cannot adjust + its behavior depending on the connection properties (in particular, + whether standard-conforming strings are enabled) + and therefore it may give the wrong results. Also, it + has no way to return an error message on failure. + + + + PQescapeBytea can be used safely in single-threaded client + programs that work with only one PostgreSQL connection at + a time (in this case it can find out what it needs to know behind the + scenes). In other contexts it is a security hazard and should be + avoided in favor of PQescapeByteaConn. @@ -2084,7 +2156,7 @@ unsigned char *PQescapeBytea(const unsigned char *from, PQunescapeByteaPQunescapeBytea - Converts an escaped string representation of binary data into binary + Converts a string representation of binary data into binary data --- the reverse of PQescapeBytea. This is needed when retrieving bytea data in text format, but not when retrieving it in binary format. @@ -2094,16 +2166,24 @@ unsigned char *PQunescapeBytea(const unsigned char *from, size_t *to_length); - - The from parameter points to an escaped string - such as might be returned by PQgetvalue when applied to a - bytea column. PQunescapeBytea converts - this string representation into its binary representation. + + The from parameter points to a string + such as might be returned by PQgetvalue when applied + to a bytea column. PQunescapeBytea + converts this string representation into its binary representation. It returns a pointer to a buffer allocated with malloc(), or null on error, and puts the size of the buffer in to_length. The result must be freed using PQfreemem when it is no longer needed. + + + This conversion is not exactly the inverse of + PQescapeBytea, because the string is not expected + to be escaped when received from PQgetvalue. + In particular this means there is no need for string quoting considerations, + and so no need for a PGconn parameter. + @@ -2119,6 +2199,7 @@ void PQfreemem(void *ptr); Frees memory allocated by libpq, particularly + PQescapeByteaConn, PQescapeBytea, PQunescapeBytea, and PQnotifies. -- cgit v1.2.3