Skip to content

Commit bc6f630

Browse files
committed
ext/pgsql: escape table name, delimiter, null marker in pg_copy_from/to
The COPY query embedded the table_name argument with a raw "%s" and the delimiter and null marker inside literal E'..' wrappers, so caller-supplied strings could break out and run side queries. Route bare table names through build_tablename (the same helper pg_insert/update/select/delete have used since bug #62978), and pass the delimiter and null marker through PQescapeLiteral. pg_copy_to keeps the parenthesised (query) source form documented in bug 73498, but wraps it in an extra paren pair so a string like (SELECT 1); DROP TABLE x; -- becomes a syntax error inside the outer parens instead of escaping out into a second statement. Closes phpGH-21985
1 parent 8d0777e commit bc6f630

5 files changed

Lines changed: 214 additions & 10 deletions

File tree

ext/pgsql/pgsql.c

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ static void pgsql_lob_free_obj(zend_object *obj)
273273

274274
/* Compatibility definitions */
275275

276+
static inline zend_result build_tablename(smart_str *querystr, PGconn *pg_link, const zend_string *table);
277+
276278
static zend_string *_php_pgsql_trim_message(const char *message)
277279
{
278280
size_t i = strlen(message);
@@ -3348,8 +3350,7 @@ PHP_FUNCTION(pg_copy_to)
33483350
zend_string *table_name;
33493351
zend_string *pg_delimiter = NULL;
33503352
char *pg_null_as = "\\\\N";
3351-
size_t pg_null_as_len = 0;
3352-
char *query;
3353+
size_t pg_null_as_len = sizeof("\\\\N") - 1;
33533354
PGconn *pgsql;
33543355
PGresult *pgsql_result;
33553356
ExecStatusType status;
@@ -3373,14 +3374,36 @@ PHP_FUNCTION(pg_copy_to)
33733374
zend_argument_value_error(3, "must be one character");
33743375
RETURN_THROWS();
33753376
}
3377+
smart_str querystr = {0};
3378+
smart_str_appends(&querystr, "COPY ");
3379+
if (ZSTR_LEN(table_name) > 0 && ZSTR_VAL(table_name)[0] == '(') {
3380+
smart_str_appendc(&querystr, '(');
3381+
smart_str_append(&querystr, table_name);
3382+
smart_str_appendc(&querystr, ')');
3383+
} else if (build_tablename(&querystr, pgsql, table_name) == FAILURE) {
3384+
smart_str_free(&querystr);
3385+
RETURN_FALSE;
3386+
}
33763387

3377-
spprintf(&query, 0, "COPY %s TO STDOUT DELIMITER E'%c' NULL AS E'%s'", ZSTR_VAL(table_name), *ZSTR_VAL(pg_delimiter), pg_null_as);
3388+
char *escaped_delimiter = PQescapeLiteral(pgsql, ZSTR_VAL(pg_delimiter), 1);
3389+
char *escaped_null_as = PQescapeLiteral(pgsql, pg_null_as, pg_null_as_len);
3390+
if (!escaped_delimiter || !escaped_null_as) {
3391+
php_error_docref(NULL, E_WARNING, "Failed to escape COPY parameters");
3392+
if (escaped_delimiter) PQfreemem(escaped_delimiter);
3393+
if (escaped_null_as) PQfreemem(escaped_null_as);
3394+
smart_str_free(&querystr);
3395+
RETURN_FALSE;
3396+
}
3397+
smart_str_append_printf(&querystr, " TO STDOUT DELIMITER %s NULL AS %s", escaped_delimiter, escaped_null_as);
3398+
smart_str_0(&querystr);
3399+
PQfreemem(escaped_delimiter);
3400+
PQfreemem(escaped_null_as);
33783401

33793402
while ((pgsql_result = PQgetResult(pgsql))) {
33803403
PQclear(pgsql_result);
33813404
}
3382-
pgsql_result = PQexec(pgsql, query);
3383-
efree(query);
3405+
pgsql_result = PQexec(pgsql, ZSTR_VAL(querystr.s));
3406+
smart_str_free(&querystr);
33843407

33853408
if (pgsql_result) {
33863409
status = PQresultStatus(pgsql_result);
@@ -3463,8 +3486,7 @@ PHP_FUNCTION(pg_copy_from)
34633486
zend_string *table_name;
34643487
zend_string *pg_delimiter = NULL;
34653488
char *pg_null_as = "\\\\N";
3466-
size_t pg_null_as_len;
3467-
char *query;
3489+
size_t pg_null_as_len = sizeof("\\\\N") - 1;
34683490
PGconn *pgsql;
34693491
PGresult *pgsql_result;
34703492
ExecStatusType status;
@@ -3488,14 +3510,33 @@ PHP_FUNCTION(pg_copy_from)
34883510
zend_argument_value_error(4, "must be one character");
34893511
RETURN_THROWS();
34903512
}
3513+
smart_str querystr = {0};
3514+
smart_str_appends(&querystr, "COPY ");
3515+
if (build_tablename(&querystr, pgsql, table_name) == FAILURE) {
3516+
smart_str_free(&querystr);
3517+
RETURN_FALSE;
3518+
}
3519+
3520+
char *escaped_delimiter = PQescapeLiteral(pgsql, ZSTR_VAL(pg_delimiter), 1);
3521+
char *escaped_null_as = PQescapeLiteral(pgsql, pg_null_as, pg_null_as_len);
3522+
if (!escaped_delimiter || !escaped_null_as) {
3523+
php_error_docref(NULL, E_WARNING, "Failed to escape COPY parameters");
3524+
if (escaped_delimiter) PQfreemem(escaped_delimiter);
3525+
if (escaped_null_as) PQfreemem(escaped_null_as);
3526+
smart_str_free(&querystr);
3527+
RETURN_FALSE;
3528+
}
3529+
smart_str_append_printf(&querystr, " FROM STDIN DELIMITER %s NULL AS %s", escaped_delimiter, escaped_null_as);
3530+
smart_str_0(&querystr);
3531+
PQfreemem(escaped_delimiter);
3532+
PQfreemem(escaped_null_as);
34913533

3492-
spprintf(&query, 0, "COPY %s FROM STDIN DELIMITER E'%c' NULL AS E'%s'", ZSTR_VAL(table_name), *ZSTR_VAL(pg_delimiter), pg_null_as);
34933534
while ((pgsql_result = PQgetResult(pgsql))) {
34943535
PQclear(pgsql_result);
34953536
}
3496-
pgsql_result = PQexec(pgsql, query);
3537+
pgsql_result = PQexec(pgsql, ZSTR_VAL(querystr.s));
34973538

3498-
efree(query);
3539+
smart_str_free(&querystr);
34993540

35003541
if (pgsql_result) {
35013542
status = PQresultStatus(pgsql_result);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
pg_copy_from() escapes the null_as argument
3+
--EXTENSIONS--
4+
pgsql
5+
--SKIPIF--
6+
<?php include("inc/skipif.inc"); ?>
7+
--FILE--
8+
<?php
9+
10+
include('inc/config.inc');
11+
12+
$db = pg_connect($conn_str);
13+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_null_as_target');
14+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_null_as_injected');
15+
pg_query($db, 'CREATE TABLE pg_copy_null_as_target (v text)');
16+
17+
$evil = "X'; CREATE TABLE pg_copy_null_as_injected (v text); --";
18+
var_dump(pg_copy_from($db, 'pg_copy_null_as_target', ["row\n"], "\t", $evil));
19+
20+
$r = pg_query($db, "SELECT 1 FROM pg_tables WHERE tablename = 'pg_copy_null_as_injected'");
21+
var_dump(pg_num_rows($r));
22+
23+
$r = pg_query($db, 'SELECT v FROM pg_copy_null_as_target ORDER BY v');
24+
var_dump(pg_fetch_all($r));
25+
26+
?>
27+
--CLEAN--
28+
<?php
29+
include('inc/config.inc');
30+
$db = pg_connect($conn_str);
31+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_null_as_target');
32+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_null_as_injected');
33+
?>
34+
--EXPECT--
35+
bool(true)
36+
int(0)
37+
array(1) {
38+
[0]=>
39+
array(1) {
40+
["v"]=>
41+
string(3) "row"
42+
}
43+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
pg_copy_from() escapes the table name argument
3+
--EXTENSIONS--
4+
pgsql
5+
--SKIPIF--
6+
<?php include("inc/skipif.inc"); ?>
7+
--FILE--
8+
<?php
9+
10+
include('inc/config.inc');
11+
12+
$db = pg_connect($conn_str);
13+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_from_target');
14+
pg_query($db, 'CREATE TABLE pg_copy_from_target (v text)');
15+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_from_other');
16+
pg_query($db, 'CREATE TABLE pg_copy_from_other (v text)');
17+
18+
$evil = "pg_copy_from_other FROM STDIN --";
19+
var_dump(pg_copy_from($db, $evil, ["redirected\n"]));
20+
21+
$rows = pg_fetch_all(pg_query($db, 'SELECT v FROM pg_copy_from_other')) ?: [];
22+
var_dump($rows);
23+
24+
?>
25+
--CLEAN--
26+
<?php
27+
include('inc/config.inc');
28+
$db = pg_connect($conn_str);
29+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_from_target');
30+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_from_other');
31+
?>
32+
--EXPECTF--
33+
Warning: pg_copy_from(): %s in %s on line %d
34+
bool(false)
35+
array(0) {
36+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
pg_copy_to() rejects statement injection through the (query) source form
3+
--EXTENSIONS--
4+
pgsql
5+
--SKIPIF--
6+
<?php include("inc/skipif.inc"); ?>
7+
--FILE--
8+
<?php
9+
10+
include('inc/config.inc');
11+
12+
$db = pg_connect($conn_str);
13+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_to_qsource');
14+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_to_qinjected');
15+
pg_query($db, 'CREATE TABLE pg_copy_to_qsource (v text)');
16+
pg_query($db, "INSERT INTO pg_copy_to_qsource VALUES ('a'), ('b')");
17+
18+
var_dump(pg_copy_to($db, '(SELECT v FROM pg_copy_to_qsource ORDER BY v)'));
19+
20+
$evil = '(SELECT 1); DROP TABLE pg_copy_to_qsource; --';
21+
var_dump(pg_copy_to($db, $evil));
22+
23+
$r = pg_query($db, "SELECT 1 FROM pg_tables WHERE tablename = 'pg_copy_to_qsource'");
24+
var_dump(pg_num_rows($r));
25+
26+
$r = pg_query($db, "SELECT 1 FROM pg_tables WHERE tablename = 'pg_copy_to_qinjected'");
27+
var_dump(pg_num_rows($r));
28+
29+
?>
30+
--CLEAN--
31+
<?php
32+
include('inc/config.inc');
33+
$db = pg_connect($conn_str);
34+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_to_qsource');
35+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_to_qinjected');
36+
?>
37+
--EXPECTF--
38+
array(2) {
39+
[0]=>
40+
string(2) "a
41+
"
42+
[1]=>
43+
string(2) "b
44+
"
45+
}
46+
47+
Warning: pg_copy_to(): %ain %s on line %d
48+
bool(false)
49+
int(1)
50+
int(0)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
pg_copy_to() escapes the table name argument
3+
--EXTENSIONS--
4+
pgsql
5+
--SKIPIF--
6+
<?php include("inc/skipif.inc"); ?>
7+
--FILE--
8+
<?php
9+
10+
include('inc/config.inc');
11+
12+
$db = pg_connect($conn_str);
13+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_to_source');
14+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_to_injected');
15+
pg_query($db, 'CREATE TABLE pg_copy_to_source (v text)');
16+
17+
$evil = "pg_copy_to_source; CREATE TABLE pg_copy_to_injected (v text); --";
18+
var_dump(pg_copy_to($db, $evil));
19+
20+
$r = pg_query($db, "SELECT 1 FROM pg_tables WHERE tablename = 'pg_copy_to_injected'");
21+
var_dump(pg_num_rows($r));
22+
23+
?>
24+
--CLEAN--
25+
<?php
26+
include('inc/config.inc');
27+
$db = pg_connect($conn_str);
28+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_to_source');
29+
pg_query($db, 'DROP TABLE IF EXISTS pg_copy_to_injected');
30+
?>
31+
--EXPECTF--
32+
Warning: pg_copy_to(): %s in %s on line %d
33+
bool(false)
34+
int(0)

0 commit comments

Comments
 (0)