From: Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>
To: bianpan2016@163.com
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #14927: Unchecked SearchSysCache1() return value
Date: Mon, 27 Nov 2017 19:20:51 +0900
Message-ID: <ca5461f9-1d9b-dd89-2c58-cd6202cfdb5d@lab.ntt.co.jp> (raw)
In-Reply-To: <20171127090105.1463.3962@wrigleys.postgresql.org>
References: <20171127090105.1463.3962@wrigleys.postgresql.org>
On 2017/11/27 18:01, bianpan2016@163.com wrote:
> The following bug has been logged on the website:
>
> Bug reference: 14927
> Logged by: Pan Bian
> Email address: bianpan2016@163.com
> PostgreSQL version: 10.1
> Operating system: Linux
> Description:
>
> File: postgresql-10.1/src/backend/catalog/heap.c
> Function: heap_drop_with_catalog
> Line: 1771
>
> Function SearchSysCache1() may return a NULL pointer, but in
> heap_drop_with_catalog(), its return value is not validated before it is
> dereferenced. To avoid NULL dereference, it is better to check the return
> value of SearchSysCache1() against NULL.
>
> For your convenience, I paste related codes as follows:
>
> 1771 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
> 1772 if (((Form_pg_class) GETSTRUCT(tuple))->relispartition)
> 1773 {
> 1774 parentOid = get_partition_parent(relid);
> 1775 LockRelationOid(parentOid, AccessExclusiveLock);
> 1776 }
> 1777
> 1778 ReleaseSysCache(tuple);
Thanks for the report. Attached a patch that adds a check that tuple is
valid before trying to dereference it.
Thanks,
Amit
Attachments:syscache-check-tuple-heap.patch (text/plain, 1.2K) downloaddiff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.cindex 9e14880b99..f6c9283086 100644--- a/src/backend/catalog/heap.c+++ b/src/backend/catalog/heap.c@@ -1772,6 +1772,8 @@ heap_drop_with_catalog(Oid relid)
* shared-cache-inval notice that will make them update their index lists.
*/
tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
+ if (!HeapTupleIsValid(tuple))+ elog(ERROR, "cache lookup failed for relation %u", relid);
if (((Form_pg_class) GETSTRUCT(tuple))->relispartition)
{
parentOid = get_partition_parent(relid);
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.cindex d19846d005..8cd6c65111 100644--- a/src/backend/commands/tablecmds.c+++ b/src/backend/commands/tablecmds.c@@ -14111,6 +14111,9 @@ ATExecDetachPartition(Relation rel, RangeVar *name)
classRel = heap_open(RelationRelationId, RowExclusiveLock);
tuple = SearchSysCacheCopy1(RELOID,
ObjectIdGetDatum(RelationGetRelid(partRel)));
+ if (!HeapTupleIsValid(tuple))+ elog(ERROR, "cache lookup failed for relation %u",+ RelationGetRelid(partRel));
Assert(((Form_pg_class) GETSTRUCT(tuple))->relispartition);
(void) SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relpartbound,
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: Langote_Amit_f8@lab.ntt.co.jp, bianpan2016@163.com
Subject: Re: BUG #14927: Unchecked SearchSysCache1() return value
In-Reply-To: <ca5461f9-1d9b-dd89-2c58-cd6202cfdb5d@lab.ntt.co.jp>
References: <20171127090105.1463.3962@wrigleys.postgresql.org> <ca5461f9-1d9b-dd89-2c58-cd6202cfdb5d@lab.ntt.co.jp>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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