diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2006-05-21 20:19:23 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2006-05-21 20:19:23 +0000 |
commit | 515112f9d4874aaedd0c093f41c0ba3e0bf7f660 (patch) | |
tree | a1870e1e1d7aa7c0b70d110c344f009992c23eda /doc/src | |
parent | b3eb4ea5d8c35ba9ec890b0f51d04c0b62d53a03 (diff) | |
download | postgresql-515112f9d4874aaedd0c093f41c0ba3e0bf7f660.tar.gz postgresql-515112f9d4874aaedd0c093f41c0ba3e0bf7f660.zip |
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.
Diffstat (limited to 'doc/src')
-rw-r--r-- | doc/src/sgml/libpq.sgml | 157 |
1 files changed, 119 insertions, 38 deletions
diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index aef1129844c..0c24f2b773d 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -1,4 +1,4 @@ -<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.209 2006/05/17 21:50:54 momjian Exp $ --> +<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.210 2006/05/21 20:19:23 tgl Exp $ --> <chapter id="libpq"> <title><application>libpq</application> - C Library</title> @@ -2187,15 +2187,16 @@ It is not thread-safe. <sect2 id="libpq-exec-escape-string"> <title>Escaping Strings for Inclusion in SQL Commands</title> + <indexterm zone="libpq-exec-escape-string"><primary>PQescapeStringConn</></> <indexterm zone="libpq-exec-escape-string"><primary>PQescapeString</></> <indexterm zone="libpq-exec-escape-string"><primary>escaping strings</></> <para> -<function>PQescapeString</function> escapes a string for use within an SQL +<function>PQescapeStringConn</function> 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. -<function>PQescapeString</> performs this operation. +<function>PQescapeStringConn</> performs this operation. </para> <tip> @@ -2213,36 +2214,68 @@ value is passed as a separate parameter in <function>PQexecParams</> or its sibling routines. <synopsis> -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); </synopsis> </para> <para> +<function>PQescapeStringConn</> writes an escaped +version of the <parameter>from</> string to the <parameter>to</> +buffer, escaping special characters so that they cannot cause any +harm, and adding a terminating zero byte. The single quotes that +must surround <productname>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 <parameter>from</> points to the first character of the string that is to be escaped, and the <parameter>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 <parameter>length</>. (If a terminating zero byte is found before <parameter>length</> bytes are -processed, <function>PQescapeString</> stops at the zero; the behavior +processed, <function>PQescapeStringConn</> stops at the zero; the behavior is thus rather like <function>strncpy</>.) <parameter>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 <parameter>length</>, otherwise the behavior is -undefined. A call to <function>PQescapeString</> writes an escaped -version of the <parameter>from</> string to the <parameter>to</> -buffer, replacing special characters so that they cannot cause any -harm, and adding a terminating zero byte. The single quotes that -must surround <productname>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 <parameter>to</> and <parameter>from</> +strings overlap. +</para> +<para> +If the <parameter>error</> parameter is not NULL, then <literal>*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 <parameter>conn</> object, whether or not <parameter>error</> +is NULL. </para> <para> -<function>PQescapeString</> returns the number of characters written +<function>PQescapeStringConn</> returns the number of bytes written to <parameter>to</>, not including the terminating zero byte. </para> + <para> -Behavior is undefined if the <parameter>to</> and <parameter>from</> -strings overlap. +<synopsis> +size_t PQescapeString (char *to, const char *from, size_t length); +</synopsis> +</para> + +<para> +<function>PQescapeString</> is an older, deprecated version of +<function>PQescapeStringConn</>; the difference is that it does not +take <parameter>conn</> or <parameter>error</> parameters. Because of this, +it cannot adjust its behavior depending on the connection properties (such as +character encoding) and therefore <emphasis>it may give the wrong results</>. +Also, it has no way to report error conditions. +</para> +<para> +<function>PQescapeString</> can be used safely in single-threaded client +programs that work with only one <productname>PostgreSQL</> connection at +a time (in this case it can find out what it needs to know <quote>behind the +scenes</>). In other contexts it is a security hazard and should be avoided +in favor of <function>PQescapeStringConn</>. </para> </sect2> @@ -2257,16 +2290,17 @@ strings overlap. <variablelist> <varlistentry> - <term><function>PQescapeBytea</function><indexterm><primary>PQescapeBytea</></></term> + <term><function>PQescapeByteaConn</function><indexterm><primary>PQescapeByteaConn</></></term> <listitem> <para> Escapes binary data for use within an SQL command with the type - <type>bytea</type>. As with <function>PQescapeString</function>, + <type>bytea</type>. As with <function>PQescapeStringConn</function>, this is only used when inserting data directly into an SQL command string. <synopsis> -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); </synopsis> </para> @@ -2276,10 +2310,10 @@ unsigned char *PQescapeBytea(const unsigned char *from, of a <type>bytea</type> literal in an <acronym>SQL</acronym> 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 (<literal>'</>) and backslash + one or two backslashes. The single quote (<literal>'</>) and backslash (<literal>\</>) characters have special alternative escape sequences. See <xref linkend="datatype-binary"> for more - information. <function>PQescapeBytea</function> performs this + information. <function>PQescapeByteaConn</function> performs this operation, escaping only the minimally required bytes. </para> @@ -2290,16 +2324,15 @@ unsigned char *PQescapeBytea(const unsigned char *from, bytes in this binary string. (A terminating zero byte is neither necessary nor counted.) The <parameter>to_length</parameter> 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. </para> <para> - <function>PQescapeBytea</> returns an escaped version of the + <function>PQescapeByteaConn</> returns an escaped version of the <parameter>from</parameter> parameter binary string in memory - allocated with <function>malloc()</> (a null pointer is returned if - memory could not be allocated). This memory must be freed using - <function>PQfreemem</> when the result is no longer needed. The + allocated with <function>malloc()</>. This memory must be freed using + <function>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 <productname>PostgreSQL</productname> string literal parser, and the <type>bytea</type> input function. A @@ -2307,6 +2340,45 @@ unsigned char *PQescapeBytea(const unsigned char *from, surround <productname>PostgreSQL</productname> string literals are not part of the result string. </para> + + <para> + On error, a NULL pointer is returned, and a suitable error message + is stored in the <parameter>conn</> object. Currently, the only + possible error is insufficient memory for the result string. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><function>PQescapeBytea</function><indexterm><primary>PQescapeBytea</></></term> + <listitem> + <para> + <function>PQescapeBytea</> is an older, deprecated version of + <function>PQescapeByteaConn</>. +<synopsis> +unsigned char *PQescapeBytea(const unsigned char *from, + size_t from_length, + size_t *to_length); +</synopsis> +</para> + +<para> + The only difference from <function>PQescapeByteaConn</> is that + <function>PQescapeBytea</> does not + take a <structname>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 <emphasis>it may give the wrong results</>. Also, it + has no way to return an error message on failure. + </para> + + <para> + <function>PQescapeBytea</> can be used safely in single-threaded client + programs that work with only one <productname>PostgreSQL</> connection at + a time (in this case it can find out what it needs to know <quote>behind the + scenes</>). In other contexts it is a security hazard and should be + avoided in favor of <function>PQescapeByteaConn</>. + </para> </listitem> </varlistentry> @@ -2314,7 +2386,7 @@ unsigned char *PQescapeBytea(const unsigned char *from, <term><function>PQunescapeBytea</function><indexterm><primary>PQunescapeBytea</></></term> <listitem> <para> - Converts an escaped string representation of binary data into binary + Converts a string representation of binary data into binary data — the reverse of <function>PQescapeBytea</function>. This is needed when retrieving <type>bytea</type> data in text format, but not when retrieving it in binary format. @@ -2324,16 +2396,24 @@ unsigned char *PQunescapeBytea(const unsigned char *from, size_t *to_length); </synopsis> </para> -<para> - The <parameter>from</parameter> parameter points to an escaped string - such as might be returned by <function>PQgetvalue</function> when applied to a - <type>bytea</type> column. <function>PQunescapeBytea</function> converts - this string representation into its binary representation. + <para> + The <parameter>from</parameter> parameter points to a string + such as might be returned by <function>PQgetvalue</function> when applied + to a <type>bytea</type> column. <function>PQunescapeBytea</function> + converts this string representation into its binary representation. It returns a pointer to a buffer allocated with <function>malloc()</function>, or null on error, and puts the size of the buffer in <parameter>to_length</parameter>. The result must be freed using <function>PQfreemem</> when it is no longer needed. </para> + + <para> + This conversion is not exactly the inverse of + <function>PQescapeBytea</function>, because the string is not expected + to be <quote>escaped</> when received from <function>PQgetvalue</function>. + In particular this means there is no need for string quoting considerations, + and so no need for a <structname>PGconn</> parameter. + </para> </listitem> </varlistentry> @@ -2349,6 +2429,7 @@ void PQfreemem(void *ptr); <para> Frees memory allocated by <application>libpq</>, particularly + <function>PQescapeByteaConn</function>, <function>PQescapeBytea</function>, <function>PQunescapeBytea</function>, and <function>PQnotifies</function>. @@ -4000,9 +4081,9 @@ current connection parameters will be used. (Therefore, put more-specific entries first when you are using wildcards.) If an entry needs to contain <literal>:</literal> or <literal>\</literal>, escape this character with <literal>\</literal>. -A hostname of <literal>localhost</> matches both TCP <literal>host</> (hostname <literal>localhost</>) -and Unix domain socket <literal>local</> (<literal>pghost</> empty or the default socket directory) -connections coming from the local machine. +A hostname of <literal>localhost</> matches both TCP (hostname +<literal>localhost</>) and Unix domain socket (<literal>pghost</> empty or the +default socket directory) connections coming from the local machine. </para> <para> |