ECPG: NULL pointer dereference in EXECUTE with a NULL parameter
ECPG: NULL pointer dereference in EXECUTE with a NULL parameter
Hello,
While reviewing a static analysis warning (DEREF_OF_NULL.EX) in PostgreSQL 17.0, I found a path in src/interfaces/ecpg/ecpglib/execute.c where a NULL parameter is passed to strlen().
In ecpg_store_input(), a negative indicator sets *tobeinserted_p to NULL (line 531 for a short indicator). The function returns true, since this is a valid SQL NULL value.
The caller, ecpg_build_params(), checks only the return value at line 1395. For ECPGst_exec_with_exprlist with a nonbinary parameter, execution then reaches the call to insert_tobeinserted() at line 1489 with tobeinserted == NULL. That function calls strlen(tobeinserted) at line 1132 without checking the pointer.
This appears to allow a valid NULL input parameter to crash the ECPG client while constructing the command.
I have checked the execution path in the 17.0 sources, but have not reproduced the failure at runtime or checked newer releases. The test case and build commands below are untested.
The following program is intended to exercise this path. It connects to the postgres database and requires permission to create a temporary table. Save it as ecpg_null_exec.pgc:
#include <stdio.h>
#include <stdlib.h>
EXEC SQL INCLUDE sqlca;
int
main(void)
{ EXEC SQL BEGIN DECLARE SECTION; int value = 0; short ind = -1; int null_count = 0; EXEC SQL END DECLARE SECTION;
EXEC SQL WHENEVER SQLERROR STOP;
EXEC SQL CONNECT TO postgres;
EXEC SQL CREATE TEMP TABLE ecpg_null_exec_test (v integer);
EXEC SQL PREPARE null_exec_stmt(integer) AS INSERT INTO ecpg_null_exec_test (v) VALUES ($1);
EXEC SQL EXECUTE null_exec_stmt(:value :ind);
EXEC SQL SELECT count(*) INTO :null_count FROM ecpg_null_exec_test WHERE v IS NULL;
printf("null_count=%d\n", null_count);
EXEC SQL DISCONNECT ALL;
return null_count == 1 ? EXIT_SUCCESS : EXIT_FAILURE;
}Example commands for Linux, with ecpg and pg_config from the same PostgreSQL installation:
ecpg -o ecpg_null_exec.c ecpg_null_exec.pgc
cc -g -O0 \ -I"$(pg_config --includedir)" \ ecpg_null_exec.c \ -L"$(pg_config --libdir)" \ -Wl,-rpath,"$(pg_config --libdir)" \ -lecpg \ -o ecpg_null_exec
./ecpg_null_execConnection settings and authentication need to be configured for the local environment.
The expected result is one row containing SQL NULL, followed by null_count=1 and a successful exit. Based on the source, the EXECUTE statement instead reaches strlen(NULL) before being sent to the server. I do not have a runtime backtrace.
The expression-list substitution code should handle SQL NULL explicitly, for example by inserting the unquoted keyword NULL. Any replacement string would need to be allocated, since insert_tobeinserted() frees the buffer passed to it. The example above could also serve as the basis for a regression test.
Thank you.
Re: ECPG: NULL pointer dereference in EXECUTE with a NULL parameter
Hi Nikita, Thanks for the report. I ran your program and it does crash, as you expected: on master it dies with a segfault in strlen(), called from insert_tobeinserted(), before the EXECUTE is sent to the server. With the indicator set to 0 instead of -1 it runs fine. The same code handles any EXECUTE name(:var, ...), including after PREPARE name FROM, not just PREPARE ... AS. A NULL bytea value can crash too, even earlier: reading the code, convert_bytea_to_string() reads from the null pointer. I saw that crash on master with a separate program. The attached patch does what you suggested. In the ECPGst_exec_with_exprlist branch of ecpg_build_params(), a NULL value is replaced with the keyword NULL (allocated, since insert_tobeinserted() frees its argument), before the bytea conversion. It also adds a NULL case to the existing prepareas test. Tested on master, REL_19, REL_17 and REL_15: your program now stores the NULL, the ECPG suite passes, and the new test fails without the fix. The back-branch file also applies cleanly to REL_16 and REL_18; I didn't build those. There are two files because ecpg_strdup() takes a third argument from 19 on: - v1-0001-ecpg-Fix-crash-on-NULL-in-EXECUTE-master.patch: master and REL_19 - v1-0001-ecpg-Fix-crash-on-NULL-in-EXECUTE-REL_15-18.patch.txt: REL_15 to REL_18 (named .txt so cfbot only tests the master one) Regards, Ishjaap Singh