]> granicus.if.org Git - postgresql/commit
Prevent dangling-pointer access when update trigger returns old tuple.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 27 Feb 2018 18:27:38 +0000 (13:27 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 27 Feb 2018 18:28:02 +0000 (13:28 -0500)
commit25b692568f429436f89ff203c1413e9670d0ad67
tree88676f048c38ea5231b1bf36d4bfc8a39bbd5c87
parent5e6a63c0d1028b9950c9cbcd7aaf9f2a67880a8d
Prevent dangling-pointer access when update trigger returns old tuple.

A before-update row trigger may choose to return the "new" or "old" tuple
unmodified.  ExecBRUpdateTriggers failed to consider the second
possibility, and would proceed to free the "old" tuple even if it was the
one returned, leading to subsequent access to already-deallocated memory.
In debug builds this reliably leads to an "invalid memory alloc request
size" failure; in production builds it might accidentally work, but data
corruption is also possible.

This is a very old bug.  There are probably a couple of reasons it hasn't
been noticed up to now.  It would be more usual to return NULL if one
wanted to suppress the update action; returning "old" is significantly less
efficient since the update will occur anyway.  Also, none of the standard
PLs would ever cause this because they all returned freshly-manufactured
tuples even if they were just copying "old".  But commit 4b93f5799 changed
that for plpgsql, making it possible to see the bug with a plpgsql trigger.
Still, this is certainly legal behavior for a trigger function, so it's
ExecBRUpdateTriggers's fault not plpgsql's.

It seems worth creating a test case that exercises returning "old" directly
with a C-language trigger; testing this through plpgsql seems unreliable
because its behavior might change again.

Report and fix by Rushabh Lathia; regression test case by me.
Back-patch to all supported branches.

Discussion: https://postgr.es/m/CAGPqQf1P4pjiNPrMof=P_16E-DFjt457j+nH2ex3=nBTew7tXw@mail.gmail.com
src/backend/commands/trigger.c
src/test/regress/expected/triggers.out
src/test/regress/input/create_function_1.source
src/test/regress/output/create_function_1.source
src/test/regress/regress.c
src/test/regress/sql/triggers.sql