pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Use the right memory context for partkey's FmgrInfo
2+ messages / 1 participants
[nested] [flat]

* pgsql: Use the right memory context for partkey's FmgrInfo
@ 2018-04-12 18:09  Alvaro Herrera <alvherre@alvh.no-ip.org>
  0 siblings, 0 replies; 2+ messages in thread

From: Alvaro Herrera @ 2018-04-12 18:09 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Use the right memory context for partkey's FmgrInfo

We were using CurrentMemoryContext to put the partsupfunc fmgr_info
into, which isn't right, because we want the PartitionKey as a whole to
be in the isolated Relation->rd_partkeycxt context.  This can cause a
crash with user-defined support functions in the operator classes used
by partitioning keys.  (Maybe this can cause problems with core-supplied
opclasses too, not sure.)

This is demonstrably broken in Postgres 10, too, but the initial
proposed fix runs afoul of a problem discussed back when 8a0596cb656e
("Get rid of copy_partition_key") reorganized that code: namely that it
is possible to jump out of RelationBuildPartitionKey because of some
error and leave a dangling memory context child of CacheMemoryContext.
Also, while reviewing this I noticed that the removed-in-pg11
copy_partition_key was doing something wrong, unfixed in pg10, namely
doing memcpy() on the FmgrInfo, which is bogus (should be doing
fmgr_info_copy).  Therefore, in branch pg10, the sane fix seems to be to
backpatch both the aforementioned 8a0596cb656e and its followup
be2343221fb7 ("Protect against hypothetical memory leaks in
RelationGetPartitionKey"), so do that, then apply the fmgr_info memcxt
bugfix on top.

Add a test case exercising btree-based custom operator classes, which
causes a crash prior to this fix.  This is not a security problem,
because in order to create an operator class you need superuser
privileges anyway.

Authors: Álvaro Herrera and Amit Langote
Reported and diagnosed by: Amit Langote
Discussion: https://postgr.es/m/3041e853-b1dd-a0c6-ff21-7cc5633bffd0@lab.ntt.co.jp

Branch
------
REL_10_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/5f11c6ec61a579d60347a5d13af7e42b17fadc56

Modified Files
--------------
src/backend/utils/cache/relcache.c         | 109 ++++++++---------------------
src/include/access/hash.h                  |   2 +-
src/test/regress/expected/create_table.out |  14 ++++
src/test/regress/sql/create_table.sql      |  15 ++++
4 files changed, 60 insertions(+), 80 deletions(-)



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* pgsql: Use the right memory context for partkey's FmgrInfo
@ 2018-04-12 18:09  Alvaro Herrera <alvherre@alvh.no-ip.org>
  0 siblings, 0 replies; 2+ messages in thread

From: Alvaro Herrera @ 2018-04-12 18:09 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Use the right memory context for partkey's FmgrInfo

We were using CurrentMemoryContext to put the partsupfunc fmgr_info
into, which isn't right, because we want the PartitionKey as a whole to
be in the isolated Relation->rd_partkeycxt context.  This can cause a
crash with user-defined support functions in the operator classes used
by partitioning keys.  (Maybe this can cause problems with core-supplied
opclasses too, not sure.)

This is demonstrably broken in Postgres 10, too, but the initial
proposed fix runs afoul of a problem discussed back when 8a0596cb656e
("Get rid of copy_partition_key") reorganized that code: namely that it
is possible to jump out of RelationBuildPartitionKey because of some
error and leave a dangling memory context child of CacheMemoryContext.
Also, while reviewing this I noticed that the removed-in-pg11
copy_partition_key was doing something wrong, unfixed in pg10, namely
doing memcpy() on the FmgrInfo, which is bogus (should be doing
fmgr_info_copy).  Therefore, in branch pg10, the sane fix seems to be to
backpatch both the aforementioned 8a0596cb656e and its followup
be2343221fb7 ("Protect against hypothetical memory leaks in
RelationGetPartitionKey"), so do that, then apply the fmgr_info memcxt
bugfix on top.

Add a test case exercising btree-based custom operator classes, which
causes a crash prior to this fix.  This is not a security problem,
because in order to create an operator class you need superuser
privileges anyway.

Authors: Álvaro Herrera and Amit Langote
Reported and diagnosed by: Amit Langote
Discussion: https://postgr.es/m/3041e853-b1dd-a0c6-ff21-7cc5633bffd0@lab.ntt.co.jp

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/a4d56f583e7cff052c2699e62d867ae1c8fda4f3

Modified Files
--------------
src/backend/utils/cache/relcache.c         |  2 +-
src/test/regress/expected/create_table.out | 16 ++++++++++++++--
src/test/regress/sql/create_table.sql      | 17 +++++++++++++++--
3 files changed, 30 insertions(+), 5 deletions(-)



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2018-04-12 18:09 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-04-12 18:09 pgsql: Use the right memory context for partkey's FmgrInfo Alvaro Herrera <alvherre@alvh.no-ip.org>
2018-04-12 18:09 pgsql: Use the right memory context for partkey's FmgrInfo Alvaro Herrera <alvherre@alvh.no-ip.org>

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