pg.ddx.io pgsql-committers@postgresql.org mailing list archive
help / color / mirror / Atom feedpgsql: PL/Python: Fix potential NULL pointer dereference
8+ messages / 3 participants
[nested] [flat]
* pgsql: PL/Python: Fix potential NULL pointer dereference
@ 2017-11-28 16:31 Peter Eisentraut <peter_e@gmx.net>
0 siblings, 1 reply; 8+ messages in thread
From: Peter Eisentraut @ 2017-11-28 16:31 UTC (permalink / raw)
To: pgsql-committers
PL/Python: Fix potential NULL pointer dereference
After d0aa965c0a0ac2ff7906ae1b1dad50a7952efa56, one error path in
PLy_spi_execute_fetch_result() could result in the variable "result"
being dereferenced after being set to NULL. To fix that, just clear the
resources right there and return early.
Also add another SPI_freetuptable() call so that that is cleared in all
error paths.
discovered by John Naylor <jcnaylor@gmail.com> via scan-build
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/e42e2f38907681c48c43f49c5ec9f9f41a9aa9a5
Modified Files
--------------
src/pl/plpython/plpy_spi.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: pgsql: PL/Python: Fix potential NULL pointer dereference
@ 2017-11-28 16:35 Tom Lane <tgl@sss.pgh.pa.us>
parent: Peter Eisentraut <peter_e@gmx.net>
0 siblings, 1 reply; 8+ messages in thread
From: Tom Lane @ 2017-11-28 16:35 UTC (permalink / raw)
To: Peter Eisentraut <peter_e@gmx.net>; +Cc: pgsql-committers
Peter Eisentraut <peter_e@gmx.net> writes:
> PL/Python: Fix potential NULL pointer dereference
I do not think it's correct to just "return" out of the middle
of a PG_TRY block --- doesn't that result in failing to pop the
PG_exception_stack and error_context_stack?
regards, tom lane
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: pgsql: PL/Python: Fix potential NULL pointer dereference
@ 2017-11-28 18:35 Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 8+ messages in thread
From: Peter Eisentraut @ 2017-11-28 18:35 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-committers
On 11/28/17 11:35, Tom Lane wrote:
> Peter Eisentraut <peter_e@gmx.net> writes:
>> PL/Python: Fix potential NULL pointer dereference
>
> I do not think it's correct to just "return" out of the middle
> of a PG_TRY block --- doesn't that result in failing to pop the
> PG_exception_stack and error_context_stack?
OK, I'll revert and rethink.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: pgsql: PL/Python: Fix potential NULL pointer dereference
@ 2017-12-05 19:16 Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
parent: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
0 siblings, 1 reply; 8+ messages in thread
From: Peter Eisentraut @ 2017-12-05 19:16 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-committers
On 11/28/17 13:35, Peter Eisentraut wrote:
> On 11/28/17 11:35, Tom Lane wrote:
>> Peter Eisentraut <peter_e@gmx.net> writes:
>>> PL/Python: Fix potential NULL pointer dereference
>>
>> I do not think it's correct to just "return" out of the middle
>> of a PG_TRY block --- doesn't that result in failing to pop the
>> PG_exception_stack and error_context_stack?
>
> OK, I'll revert and rethink.
How about this instead?
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Attachments:
0001-PL-Python-Fix-potential-NULL-pointer-dereference.patch (text/plain, 1.7K) download
From a665798c57207e35eee9b106b0da3dab4e1964a3 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Tue, 5 Dec 2017 14:14:55 -0500
Subject: [PATCH] PL/Python: Fix potential NULL pointer dereference
After d0aa965c0a0ac2ff7906ae1b1dad50a7952efa56, one error path in
PLy_spi_execute_fetch_result() could result in the variable "result"
being dereferenced after being set to NULL. Put a conditional around
that to fix that.
Also add another SPI_freetuptable() call so that that is cleared in all
error paths.
discovered by John Naylor <jcnaylor@gmail.com> via scan-build
---
src/pl/plpython/plpy_spi.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/src/pl/plpython/plpy_spi.c b/src/pl/plpython/plpy_spi.c
index ade27f3924..39d1c92fc4 100644
--- a/src/pl/plpython/plpy_spi.c
+++ b/src/pl/plpython/plpy_spi.c
@@ -361,7 +361,10 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
result = (PLyResultObject *) PLy_result_new();
if (!result)
+ {
+ SPI_freetuptable(tuptable);
return NULL;
+ }
Py_DECREF(result->status);
result->status = PyInt_FromLong(status);
@@ -440,9 +443,12 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
* possible, to minimize the number of ways the tupdesc could get
* leaked due to errors.)
*/
- oldcontext2 = MemoryContextSwitchTo(TopMemoryContext);
- result->tupdesc = CreateTupleDescCopy(tuptable->tupdesc);
- MemoryContextSwitchTo(oldcontext2);
+ if (result)
+ {
+ oldcontext2 = MemoryContextSwitchTo(TopMemoryContext);
+ result->tupdesc = CreateTupleDescCopy(tuptable->tupdesc);
+ MemoryContextSwitchTo(oldcontext2);
+ }
}
PG_CATCH();
{
--
2.15.0
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: pgsql: PL/Python: Fix potential NULL pointer dereference
@ 2017-12-05 19:45 Tom Lane <tgl@sss.pgh.pa.us>
parent: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
0 siblings, 1 reply; 8+ messages in thread
From: Tom Lane @ 2017-12-05 19:45 UTC (permalink / raw)
To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; +Cc: pgsql-committers
Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:
> On 11/28/17 13:35, Peter Eisentraut wrote:
>> OK, I'll revert and rethink.
> How about this instead?
This looks better, but I think there are a couple more things:
* Seems like you ought to s/Py_DECREF(result)/Py_XDECREF(result)/ in the
PG_CATCH block (line 451 in HEAD). As this is coded, although result
is set to NULL within the PG_TRY, it doesn't seem possible for control
to reach PG_CATCH after that ... but that's a pretty fragile assumption.
* You'd also need to declare "result" as
PLyResultObject *volatile result;
if you intend to change it within the PG_TRY block.
On the whole it seems like it might be better to dodge this whole business
of changing "result" inside the TRY. You could do that if you did
something like
result->rows = PyList_New(rows);
- if (!result->rows)
- {
- Py_DECREF(result);
- result = NULL;
- }
- else
+ if (result->rows)
{
PLy_input_setup_tuple(&ininfo, tuptable->tupdesc,
and then add after the PG_END_TRY
if (!result->rows)
{
Py_DECREF(result);
result = NULL;
}
The repeated test is a bit annoying but maybe it's better than the
alternative. You'd probably need a comment explaining why it's
like that, though.
regards, tom lane
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: pgsql: PL/Python: Fix potential NULL pointer dereference
@ 2017-12-12 17:10 Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 8+ messages in thread
From: Peter Eisentraut @ 2017-12-12 17:10 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-committers
On 12/5/17 14:45, Tom Lane wrote:
> On the whole it seems like it might be better to dodge this whole business
> of changing "result" inside the TRY. You could do that if you did
> something like
>
> result->rows = PyList_New(rows);
> - if (!result->rows)
> - {
> - Py_DECREF(result);
> - result = NULL;
> - }
> - else
> + if (result->rows)
> {
> PLy_input_setup_tuple(&ininfo, tuptable->tupdesc,
>
> and then add after the PG_END_TRY
>
> if (!result->rows)
> {
> Py_DECREF(result);
> result = NULL;
> }
Yeah, that looks much better. Next try attached.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Attachments:
v2-0001-PL-Python-Fix-potential-NULL-pointer-dereference.patch (text/plain, 1.9K) download
From aa2f7ca289b3475f1680b181b08be84bda5d74bc Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Tue, 5 Dec 2017 14:14:55 -0500
Subject: [PATCH v2] PL/Python: Fix potential NULL pointer dereference
After d0aa965c0a0ac2ff7906ae1b1dad50a7952efa56, one error path in
PLy_spi_execute_fetch_result() could result in the variable "result"
being dereferenced after being set to NULL. Put a conditional around
that to fix that.
Also add another SPI_freetuptable() call so that that is cleared in all
error paths.
discovered by John Naylor <jcnaylor@gmail.com> via scan-build
---
src/pl/plpython/plpy_spi.c | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/src/pl/plpython/plpy_spi.c b/src/pl/plpython/plpy_spi.c
index ade27f3924..0c623a9458 100644
--- a/src/pl/plpython/plpy_spi.c
+++ b/src/pl/plpython/plpy_spi.c
@@ -361,7 +361,10 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
result = (PLyResultObject *) PLy_result_new();
if (!result)
+ {
+ SPI_freetuptable(tuptable);
return NULL;
+ }
Py_DECREF(result->status);
result->status = PyInt_FromLong(status);
@@ -411,12 +414,7 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
Py_DECREF(result->rows);
result->rows = PyList_New(rows);
- if (!result->rows)
- {
- Py_DECREF(result);
- result = NULL;
- }
- else
+ if (result->rows)
{
PLy_input_setup_tuple(&ininfo, tuptable->tupdesc,
exec_ctx->curr_proc);
@@ -455,6 +453,13 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
MemoryContextDelete(cxt);
SPI_freetuptable(tuptable);
+
+ /* in case PyList_New() failed above */
+ if (!result->rows)
+ {
+ Py_DECREF(result);
+ result = NULL;
+ }
}
return (PyObject *) result;
base-commit: d329dc2ea4bfac84ec60ba14b96561a7508bb37b
--
2.15.1
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: pgsql: PL/Python: Fix potential NULL pointer dereference
@ 2017-12-12 21:42 Tom Lane <tgl@sss.pgh.pa.us>
parent: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
0 siblings, 0 replies; 8+ messages in thread
From: Tom Lane @ 2017-12-12 21:42 UTC (permalink / raw)
To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; +Cc: pgsql-committers
Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:
> On 12/5/17 14:45, Tom Lane wrote:
>> On the whole it seems like it might be better to dodge this whole business
>> of changing "result" inside the TRY. You could do that if you did
>> something like ...
> Yeah, that looks much better. Next try attached.
Looks sane to me.
regards, tom lane
^ permalink raw reply [nested|flat] 8+ messages in thread
* pgsql: PL/Python: Fix potential NULL pointer dereference
@ 2017-12-13 02:03 Peter Eisentraut <peter_e@gmx.net>
0 siblings, 0 replies; 8+ messages in thread
From: Peter Eisentraut @ 2017-12-13 02:03 UTC (permalink / raw)
To: pgsql-committers
PL/Python: Fix potential NULL pointer dereference
After d0aa965c0a0ac2ff7906ae1b1dad50a7952efa56, one error path in
PLy_spi_execute_fetch_result() could result in the variable "result"
being dereferenced after being set to NULL. Rearrange the code a bit to
fix that.
Also add another SPI_freetuptable() call so that that is cleared in all
error paths.
discovered by John Naylor <jcnaylor@gmail.com> via scan-build
ideas and review by Tom Lane
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/4c6744ed705df6f388371d044b87d1b4a60e9f80
Modified Files
--------------
src/pl/plpython/plpy_spi.c | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
^ permalink raw reply [nested|flat] 8+ messages in thread
end of thread, other threads:[~2017-12-13 02:03 UTC | newest]
Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-11-28 16:31 pgsql: PL/Python: Fix potential NULL pointer dereference Peter Eisentraut <peter_e@gmx.net>
2017-11-28 16:35 ` Tom Lane <tgl@sss.pgh.pa.us>
2017-11-28 18:35 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-05 19:16 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-05 19:45 ` Tom Lane <tgl@sss.pgh.pa.us>
2017-12-12 17:10 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-12 21:42 ` Tom Lane <tgl@sss.pgh.pa.us>
2017-12-13 02:03 pgsql: PL/Python: Fix potential NULL pointer dereference Peter Eisentraut <peter_e@gmx.net>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox