Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPguH-0003SY-Px for pgsql-hackers@arkaria.postgresql.org; Wed, 08 Feb 2023 09:38:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pPguG-0005Qy-D5 for pgsql-hackers@arkaria.postgresql.org; Wed, 08 Feb 2023 09:38:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPfhW-0001y1-Ie for pgsql-hackers@lists.postgresql.org; Wed, 08 Feb 2023 08:21:10 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pPfhR-0004Tr-6h for pgsql-hackers@postgresql.org; Wed, 08 Feb 2023 08:21:09 +0000 Received: from mail.postgrespro.ru (webmail.mstn.postgrespro.ru [192.168.2.26]) (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 A034A21CB737; Wed, 8 Feb 2023 11:21:03 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1675844463; bh=uf6IRRVZD0/kV9QEt1Ob+AXNtwrx9AE8UBDVBpOjcY4=; h=Date:From:To:Cc:Subject; b=QvNAzIjrR+MVO9sx683manGhwCMU4ImJFdyclVpJAIMaS20j7zWid91BT0n7F9aHg 22thoU6vtuO4Jg3JeRXAQBhm5MAk0qy3tn4u7A+Qnry2hdX+m+z5tcRKNi6m8prgin peR3yrUrNE4+1r/nTbi5QQJDueZfzcRipLlxjf/E= MIME-Version: 1.0 Date: Wed, 08 Feb 2023 15:21:03 +0700 From: o.tselebrovskiy@postgrespro.ru To: pgsql-hackers@postgresql.org Cc: s.shinderuk@postgrespro.ru Subject: A bug with ExecCheckPermissions User-Agent: Roundcube Webmail/1.4.11 Message-ID: <3722b7a2cbe27a1796ee40824bd86dd1@postgrespro.ru> X-Sender: o.tselebrovskiy@postgrespro.ru Content-Type: multipart/mixed; boundary="=_936f18d333a13564f07186ac4963598b" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=_936f18d333a13564f07186ac4963598b Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed Greetings, everyone! While working on an extension my colleague and I have found an interesting case; When you try to execute next SQL statements on master branch of PostgreSQL: CREATE TABLE parted_fk_naming ( id bigint NOT NULL default 1, id_abc bigint, CONSTRAINT dummy_constr FOREIGN KEY (id_abc) REFERENCES parted_fk_naming (id), PRIMARY KEY (id) ) PARTITION BY LIST (id); CREATE TABLE parted_fk_naming_1 ( id bigint NOT NULL default 1, id_abc bigint, PRIMARY KEY (id), CONSTRAINT dummy_constr CHECK (true) ); ALTER TABLE parted_fk_naming ATTACH PARTITION parted_fk_naming_1 FOR VALUES IN ('1'); seemingly nothing suspicious happens. But if you debug function ExecCheckPermissions and look into what is passed to function (contents of rangeTable and rteperminfos to be exact), you'll see some strange behaviour: ( {RANGETBLENTRY :alias <> :eref <> :rtekind 0 :relid 16395 :relkind r :rellockmode 1 :tablesample <> :perminfoindex 0 :lateral false :inh false :inFromCl false :securityQuals <> } {RANGETBLENTRY :alias <> :eref <> :rtekind 0 :relid 16384 :relkind p :rellockmode 1 :tablesample <> :perminfoindex 0 :lateral false :inh false :inFromCl false :securityQuals <> } ) ( {RTEPERMISSIONINFO :relid 16395 :inh false :requiredPerms 2 :checkAsUser 0 :selectedCols (b 9) :insertedCols (b) :updatedCols (b) } {RTEPERMISSIONINFO :relid 16384 :inh false :requiredPerms 2 :checkAsUser 0 :selectedCols (b 8) :insertedCols (b) :updatedCols (b) } ) Both of RangeTableEntries have a perminfoindex of 0 and simultaneously have a RTEPERMISSIONINFO entry for them! Right now this behaviour isn't affecting anything, but in future should someone want to use ExecutorCheckPerms_hook from /src/backend/executor/execMain.c, its input parameters won't correspond to each other since members of rangeTable will have incorrect perminfoindex. To fix this, we're setting fk's index to 1 and pk's index to 2 in /src/backend/utils/adt/ri_triggers.c so that list being passed to ExecCheckPermissions and its hook has indexes for corresponding rteperminfos entries. 1 and 2 are chosen because perminfoindex is 1-based and fk is passed to list_make2 first; We are eager to hear some thoughts from the community! Regards, Oleg Tselebrovskii --=_936f18d333a13564f07186ac4963598b Content-Transfer-Encoding: base64 Content-Type: text/x-diff; name=missing-perminfoindex.diff Content-Disposition: attachment; filename=missing-perminfoindex.diff; size=893 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL3V0aWxzL2FkdC9yaV90cmlnZ2Vycy5jIGIvc3JjL2Jh Y2tlbmQvdXRpbHMvYWR0L3JpX3RyaWdnZXJzLmMKaW5kZXggMjlmMjlkNjY0YjYuLjc2NjY4ODY3 NDJhIDEwMDY0NAotLS0gYS9zcmMvYmFja2VuZC91dGlscy9hZHQvcmlfdHJpZ2dlcnMuYworKysg Yi9zcmMvYmFja2VuZC91dGlscy9hZHQvcmlfdHJpZ2dlcnMuYwpAQCAtMTM5OSw2ICsxMzk5LDcg QEAgUklfSW5pdGlhbF9DaGVjayhUcmlnZ2VyICp0cmlnZ2VyLCBSZWxhdGlvbiBma19yZWwsIFJl bGF0aW9uIHBrX3JlbCkKIAlwa3J0ZS0+cmVsaWQgPSBSZWxhdGlvbkdldFJlbGlkKHBrX3JlbCk7 CiAJcGtydGUtPnJlbGtpbmQgPSBwa19yZWwtPnJkX3JlbC0+cmVsa2luZDsKIAlwa3J0ZS0+cmVs bG9ja21vZGUgPSBBY2Nlc3NTaGFyZUxvY2s7CisJcGtydGUtPnBlcm1pbmZvaW5kZXggPSAyOwog CiAJcGtfcGVybWluZm8gPSBtYWt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