Skip to content

Commit 0b0a808

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. Closes phpGH-21985
1 parent 8d0777e commit 0b0a808

4 files changed

Lines changed: 162 additions & 10 deletions

File tree

ext/pgsql/pgsql.c

Lines changed: 49 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,34 @@ 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_append(&querystr, table_name);
3381+
} else if (build_tablename(&querystr, pgsql, table_name) == FAILURE) {
3382+
smart_str_free(&querystr);
3383+
RETURN_FALSE;
3384+
}
33763385

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);
3386+
char *escaped_delimiter = PQescapeLiteral(pgsql, ZSTR_VAL(pg_delimiter), 1);
3387+
char *escaped_null_as = PQescapeLiteral(pgsql, pg_null_as, pg_null_as_len);
3388+
if (!escaped_delimiter || !escaped_null_as) {
3389+
php_error_docref(NULL, E_WARNING, "Failed to escape COPY parameters");
3390+
if (escaped_delimiter) PQfreemem(escaped_delimiter);
3391+
if (escaped_null_as) PQfreemem(escaped_null_as);
3392+
smart_str_free(&querystr);
3393+
RETURN_FALSE;
3394+
}
3395+
smart_str_append_printf(&querystr, " TO STDOUT DELIMITER %s NULL AS %s", escaped_delimiter, escaped_null_as);
3396+
smart_str_0(&querystr);
3397+
PQfreemem(escaped_delimiter);
3398+
PQfreemem(escaped_null_as);
33783399

33793400
while ((pgsql_result = PQgetResult(pgsql))) {
33803401
PQclear(pgsql_result);
33813402
}
3382-
pgsql_result = PQexec(pgsql, query);
3383-
efree(query);
3403+
pgsql_result = PQexec(pgsql, ZSTR_VAL(querystr.s));
3404+
smart_str_free(&querystr);
33843405

33853406
if (pgsql_result) {
33863407
status = PQresultStatus(pgsql_result);
@@ -3463,8 +3484,7 @@ PHP_FUNCTION(pg_copy_from)
34633484
zend_string *table_name;
34643485
zend_string *pg_delimiter = NULL;
34653486
char *pg_null_as = "\\\\N";
3466-
size_t pg_null_as_len;
3467-
char *query;
3487+
size_t pg_null_as_len = sizeof("\\\\N") - 1;
34683488
PGconn *pgsql;
34693489
PGresult *pgsql_result;
34703490
ExecStatusType status;
@@ -3488,14 +3508,33 @@ PHP_FUNCTION(pg_copy_from)
34883508
zend_argument_value_error(4, "must be one character");
34893509
RETURN_THROWS();
34903510
}
3511+
smart_str querystr = {0};
3512+
smart_str_appends(&querystr, "COPY ");
3513+
if (build_tablename(&querystr, pgsql, table_name) == FAILURE) {
3514+
smart_str_free(&querystr);
3515+
RETURN_FALSE;
3516+
}
3517+
3518+
char *escaped_delimiter = PQescapeLiteral(pgsql, ZSTR_VAL(pg_delimiter), 1);
3519+
char *escaped_null_as = PQescapeLiteral(pgsql, pg_null_as, pg_null_as_len);
3520+
if (!escaped_delimiter || !escaped_null_as) {
3521+
php_error_docref(NULL, E_WARNING, "Failed to escape COPY parameters");
3522+
if (escaped_delimiter) PQfreemem(escaped_delimiter);
3523+
if (escaped_null_as) PQfreemem(escaped_null_as);
3524+
smart_str_free(&querystr);
3525+
RETURN_FALSE;
3526+
}
3527+
smart_str_append_printf(&querystr, " FROM STDIN DELIMITER %s NULL AS %s", escaped_delimiter, escaped_null_as);
3528+
smart_str_0(&querystr);
3529+
PQfreemem(escaped_delimiter);
3530+
PQfreemem(escaped_null_as);
34913531

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);
34933532
while ((pgsql_result = PQgetResult(pgsql))) {
34943533
PQclear(pgsql_result);
34953534
}
3496-
pgsql_result = PQexec(pgsql, query);
3535+
pgsql_result = PQexec(pgsql, ZSTR_VAL(querystr.s));
34973536

3498-
efree(query);
3537+
smart_str_free(&querystr);
34993538

35003539
if (pgsql_result) {
35013540
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: 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)