Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hmuEA-00038v-4S for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Jul 2019 06:12:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hmuE7-0003dn-Ot for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Jul 2019 06:12:43 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hmuE7-0003df-8U for pgsql-hackers@lists.postgresql.org; Mon, 15 Jul 2019 06:12:43 +0000 Received: from cyclops.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1hmuE3-0007lN-OH for pgsql-hackers@postgresql.org; Mon, 15 Jul 2019 06:12:41 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 7ACCB21C02B5 for ; Mon, 15 Jul 2019 09:12:37 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.129.217] (unknown [94.25.177.92]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 1CE6C21C0225 for ; Mon, 15 Jul 2019 09:12:36 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1563171157; bh=TDjWHb1IwgAv5PqjMcLgMpSeOxLr4SA8Is0yZWEVweE=; h=From:Subject:To:Date; b=KoFK1HLeMZ799L1BO8YMP82dJ00L1lYCM0w2UKmG9bUhQmD4u+TjSicVM00oq8DY6 icx5Vli6v5KbvGHuhHh1a1d8l1gmB9Y4gcjg5gAcPU3Eo70R+Cof2Lz76M9cH1ksHo VYFgCofg/+aStPNj9w8aWn249JGT0W6aCGKEtnko= From: Andrey Lepikhov Subject: Insecure initialization of required_relids field Organization: Postgres Professional To: PostgreSQL-Dev Message-ID: <5c21029d-81a2-c999-6744-6a898fcc9a19@postgrespro.ru> Date: Mon, 15 Jul 2019 11:12:35 +0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------733A58098BF09C1863AF9B05" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------733A58098BF09C1863AF9B05 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi, commit a31ad27fc5d introduced required_relids field. By default, it links to the clause_relids. It works good while we do not modify clause_relids or required_relids. But in the case of modification such initialization demands us to remember, that this field is shared. And we need to do bms_copy() before making any changes (see [1] for example). Also, we make some changes of the RestrictInfo fields (see patch [2]) during removing of unneeded self joins. I propose to do more secure initialization way of required_relids (see patch in attachment). [1] commit 4e97631e6a9, analyzejoins.c, line 434,435: rinfo->required_relids = bms_copy(rinfo->required_relids); rinfo->required_relids = bms_del_member(rinfo->required_relids, relid); [2] https://commitfest.postgresql.org/23/1712/ -- Andrey Lepikhov Postgres Professional https://postgrespro.com The Russian Postgres Company --------------733A58098BF09C1863AF9B05 Content-Type: text/x-patch; name="0001-More-secure-initialization-of-required_relids-field.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-More-secure-initialization-of-required_relids-field.pat"; filename*1="ch" From 1dc021c0c9827ecd530ee3bb0020bad9bc5d14b6 Mon Sep 17 00:00:00 2001 From: "Andrey V. Lepikhov" Date: Mon, 15 Jul 2019 11:07:20 +0500 Subject: [PATCH] More secure initialization of required_relids field --- src/backend/optimizer/util/restrictinfo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/optimizer/util/restrictinfo.c b/src/backend/optimizer/util/restrictinfo.c index 3b50fd29ad..450558e61b 100644 --- a/src/backend/optimizer/util/restrictinfo.c +++ b/src/backend/optimizer/util/restrictinfo.c @@ -172,7 +172,7 @@ make_restrictinfo_internal(Expr *clause, if (required_relids != NULL) restrictinfo->required_relids = required_relids; else - restrictinfo->required_relids = restrictinfo->clause_relids; + restrictinfo->required_relids = bms_copy(restrictinfo->clause_relids); /* * Fill in all the cacheable fields with "not yet set" markers. None of -- 2.17.1 --------------733A58098BF09C1863AF9B05--