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 1nv9ea-0003ZJ-7A for pgsql-bugs@arkaria.postgresql.org; Sun, 29 May 2022 03:31:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nv9dZ-0003sq-68 for pgsql-bugs@arkaria.postgresql.org; Sun, 29 May 2022 03:30:41 +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 1nv9dY-0003sP-QW for pgsql-bugs@lists.postgresql.org; Sun, 29 May 2022 03:30:40 +0000 Received: from mail-qk1-x735.google.com ([2607:f8b0:4864:20::735]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nv9dW-0005xP-4i for pgsql-bugs@lists.postgresql.org; Sun, 29 May 2022 03:30:40 +0000 Received: by mail-qk1-x735.google.com with SMTP id v11so8573922qkf.1 for ; Sat, 28 May 2022 20:30:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=d4rehG75zrrrj3Xl+A9DQfF1vlbbapMH3ZJsiRlDOCc=; b=n9fBIF070MKcqR4I4WwfgnfrrzwREWVWGiIGqtPmNvqFEMNIdLXY3XWfCWfjxxdGSs QlCswrKw1FTKhwiOp5LWfXwqJBkg++E7XYDnYDdjTSYRnvpj5GroJSvNFD9+cTuoJo3Q yPpJqulN9ZHr+spj8cP7CVWx2XmlTmKo0+scxDOogl74GIjkz0BAlzdvLDNzpMTZ5KMS 0A/32g3Omos3CsAkS+9I519a6wZ5FT4yfYKdIsNyq6JaFoH36wl6hSGo9fYXLPR46tPC rIy16HFZ1PDNmGQWtK9sQu5w0DHMftrc7WKhG/h3VR4Tq2EY4A56guzqIQV5ppAwREbQ k27A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=d4rehG75zrrrj3Xl+A9DQfF1vlbbapMH3ZJsiRlDOCc=; b=Q+/j7yzcSU+vgcfsf+4ZH4iYJ6nnZhGLv0bOYlpBc6OpRstMG4+jbOEi+tgBbsRxLc BAOHn07fKTdpxSFwH+eR7S6Lqx/DSs55obcpe+BWkMywCium9xaZOM7G/KnYkxR8KQzJ g+cobpTR6oBpM4arSDhdn98dsVSA5wGUD7irA9pWd7YY4BkrSOEOWAXcOgYFrjrwYT9y 6MDdB1lk6DwOIgV8ufrzGxPZGY7xgCnpOfWydmeTLoDvGQvGRmXNqV8q2/5Bv20nwFv/ XiogveFY6cckTfbDx4SgMYXnIQU/ihXPU9x15lYp2Ebh0l9zcmOU2fyHhMY/+upPCw6d 6yJw== X-Gm-Message-State: AOAM5326pWnYazIIbAnXqhwCiDk0OeeDUbAF7pnzSSuajyyy8JtLmiht iTH2Ccr6CdFePqgx04lDugE= X-Google-Smtp-Source: ABdhPJz8eft1UOMVSW57efh8/qo/GrmfbTAg6SIvJpMrqc70MdqfGrplzbMPLMNpgar0V/1DY+WMdg== X-Received: by 2002:a05:620a:25d1:b0:47e:b90:bf6c with SMTP id y17-20020a05620a25d100b0047e0b90bf6cmr32711760qko.538.1653795036090; Sat, 28 May 2022 20:30:36 -0700 (PDT) Received: from nathanxps13 ([2600:1700:48f8:340f:67a8:79e9:dd3f:6466]) by smtp.gmail.com with ESMTPSA id u126-20020ae9d884000000b006a35ac236c4sm5381882qkf.113.2022.05.28.20.30.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 May 2022 20:30:35 -0700 (PDT) Date: Sat, 28 May 2022 20:30:33 -0700 From: Nathan Bossart To: Noah Misch Cc: pgsql-bugs@lists.postgresql.org, =?iso-8859-1?Q?F=E4rber=2C_Franz-Josef_=28StMUK=29?= , Tom Lane Subject: Re: Extension pg_trgm, permissions and pg_dump order Message-ID: <20220529033033.GA1973706@nathanxps13> References: <1961551.1653498155@sss.pgh.pa.us> <20220526055047.GA3153526@rfd.leadboat.com> <20220528045122.GA3228881@rfd.leadboat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="X1bOJ3K7DJ5YkBrT" Content-Disposition: inline In-Reply-To: <20220528045122.GA3228881@rfd.leadboat.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --X1bOJ3K7DJ5YkBrT Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, May 27, 2022 at 09:51:22PM -0700, Noah Misch wrote: > On Wed, May 25, 2022 at 10:50:47PM -0700, Noah Misch wrote: >> BEGIN; >> CREATE ROLE limitedrole; >> CREATE SCHEMA ext_trgm; >> CREATE EXTENSION pg_trgm SCHEMA ext_trgm; >> CREATE TABLE x(y text); >> ALTER TABLE x OWNER TO limitedrole; >> CREATE INDEX ON x USING gist(y ext_trgm.gist_trgm_ops); >> ROLLBACK; > >> Not too simple, no. The parts of DefineIndex() that execute user code >> (e.g. DefineIndex->ComputeIndexAttrs->CheckMutability) are interspersed with >> the parts that do permissions checks, like the one yielding $SUBJECT at >> DefineIndex->ComputeIndexAttrs->ResolveOpClass->LookupExplicitNamespace. My >> first candidate is to undo the userid switch before the ResolveOpClass() call >> and restore it after. My second candidate is to pass down the userid we want >> used for this sort of permissions check. Depending on the full list of call >> stacks reaching permissions checks, this could get hairy. > > While I'd value the opportunity to work on this, there only a 50% chance I > could get it done by 2022-08-01. I've set aside four hours on 2022-06-12 to > see how far I get. For a 95% chance, the date would be 2023-02-01. (I've > already canceled a 2022-07 vacation for the thing taking my time instead; > there's nothing clearly left to cut.) If anyone would like it done faster > than that, I welcome that person taking over. I'd like to help. I started looking at the problem and hacked together a proof-of-concept based on your first candidate that seems to fix the reported issue. However, from the upthread discussion, it is not clear whether there is agreement on the approach. IIUC there are still many other code paths that would require a similar treatment, so perhaps identifying all of those would be a good next step. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --X1bOJ3K7DJ5YkBrT Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="fix_pg_dump_proof_of_concept.patch" diff --git a/contrib/pg_trgm/expected/pg_trgm.out b/contrib/pg_trgm/expected/pg_trgm.out index 20141ce7f3..060fd8e23d 100644 --- a/contrib/pg_trgm/expected/pg_trgm.out +++ b/contrib/pg_trgm/expected/pg_trgm.out @@ -5402,3 +5402,18 @@ SELECT DISTINCT city, similarity(city, 'Warsaw'), show_limit() Warsaw | 1 | 0.5 (1 row) +DROP EXTENSION pg_trgm CASCADE; +NOTICE: drop cascades to 4 other objects +DETAIL: drop cascades to index test2_idx_gist +drop cascades to index restaurants_city_idx +drop cascades to index trgm_idx +drop cascades to index t_trgm_idx +BEGIN; +CREATE ROLE limitedrole; +CREATE SCHEMA ext_trgm; +CREATE EXTENSION pg_trgm SCHEMA ext_trgm; +CREATE TABLE x(y text); +ALTER TABLE x OWNER TO limitedrole; +CREATE INDEX ON x USING gist(y ext_trgm.gist_trgm_ops); +ROLLBACK; +CREATE EXTENSION pg_trgm; diff --git a/contrib/pg_trgm/sql/pg_trgm.sql b/contrib/pg_trgm/sql/pg_trgm.sql index 6a9da24d5a..00917f8085 100644 --- a/contrib/pg_trgm/sql/pg_trgm.sql +++ b/contrib/pg_trgm/sql/pg_trgm.sql @@ -236,3 +236,14 @@ SELECT DISTINCT city, similarity(city, 'Warsaw'), show_limit() SELECT set_limit(0.5); SELECT DISTINCT city, similarity(city, 'Warsaw'), show_limit() FROM restaurants WHERE city % 'Warsaw'; + +DROP EXTENSION pg_trgm CASCADE; +BEGIN; +CREATE ROLE limitedrole; +CREATE SCHEMA ext_trgm; +CREATE EXTENSION pg_trgm SCHEMA ext_trgm; +CREATE TABLE x(y text); +ALTER TABLE x OWNER TO limitedrole; +CREATE INDEX ON x USING gist(y ext_trgm.gist_trgm_ops); +ROLLBACK; +CREATE EXTENSION pg_trgm; diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index eac13ac0b7..d4c718b696 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -80,7 +80,9 @@ static void ComputeIndexAttrs(IndexInfo *indexInfo, Oid relId, const char *accessMethodName, Oid accessMethodId, bool amcanorder, - bool isconstraint); + bool isconstraint, + Oid root_save_userid, + int root_save_sec_context); static char *ChooseIndexName(const char *tabname, Oid namespaceId, List *colnames, List *exclusionOpNames, bool primary, bool isconstraint); @@ -236,7 +238,7 @@ CheckIndexCompatible(Oid oldId, coloptions, attributeList, exclusionOpNames, relationId, accessMethodName, accessMethodId, - amcanorder, isconstraint); + amcanorder, isconstraint, 0, 0); /* Get the soon-obsolete pg_index tuple. */ @@ -890,7 +892,8 @@ DefineIndex(Oid relationId, coloptions, allIndexParams, stmt->excludeOpNames, relationId, accessMethodName, accessMethodId, - amcanorder, stmt->isconstraint); + amcanorder, stmt->isconstraint, root_save_userid, + root_save_sec_context); /* * Extra checks when creating a PRIMARY KEY index. @@ -1743,12 +1746,16 @@ ComputeIndexAttrs(IndexInfo *indexInfo, const char *accessMethodName, Oid accessMethodId, bool amcanorder, - bool isconstraint) + bool isconstraint, + Oid root_save_userid, + int root_save_sec_context) { ListCell *nextExclOp; ListCell *lc; int attn; int nkeycols = indexInfo->ii_NumIndexKeyAttrs; + Oid saved_userid; + int saved_sec_ctx; /* Allocate space for exclusion operator info, if needed */ if (exclusionOpNames) @@ -1922,6 +1929,12 @@ ComputeIndexAttrs(IndexInfo *indexInfo, collationOidP[attn] = attcollation; + if (OidIsValid(root_save_userid)) + { + GetUserIdAndSecContext(&saved_userid, &saved_sec_ctx); + SetUserIdAndSecContext(root_save_userid, root_save_sec_context); + } + /* * Identify the opclass to use. */ @@ -1930,6 +1943,9 @@ ComputeIndexAttrs(IndexInfo *indexInfo, accessMethodName, accessMethodId); + if (OidIsValid(root_save_userid)) + SetUserIdAndSecContext(saved_userid, saved_sec_ctx); + /* * Identify the exclusion operator, if any. */ --X1bOJ3K7DJ5YkBrT--