Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sSPQn-006y8m-Ay for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Jul 2024 23:12:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sSPQk-00CbcK-52 for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Jul 2024 23:11:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sSPQj-00CbcA-Jg for pgsql-hackers@lists.postgresql.org; Fri, 12 Jul 2024 23:11:57 +0000 Received: from mail-yw1-x1132.google.com ([2607:f8b0:4864:20::1132]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sSPQg-001qOL-Tl for pgsql-hackers@postgresql.org; Fri, 12 Jul 2024 23:11:56 +0000 Received: by mail-yw1-x1132.google.com with SMTP id 00721157ae682-65fa21206b5so599947b3.3 for ; Fri, 12 Jul 2024 16:11:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1720825913; x=1721430713; darn=postgresql.org; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=IXJc7Vvhk2TTl52sd455X9t3M0C4Ey4FBGyCrWtz92Y=; b=Gg8lF0ekivj26qZ8mEhXcZrhNNB6/xpWKSpgeNIZ2o/+UwbSICDQ7HuWQOGBWYJNn5 RwoiP0lctSPSjcQ8lPwO5vElvg2dKEJpOdl6dPyYaDL7gGAScfV+BPzs5yMFBbzK/AUj Avn2ZMpft64dZRWnpbZD2aveYyBcf0ZMsPTuU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720825913; x=1721430713; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=IXJc7Vvhk2TTl52sd455X9t3M0C4Ey4FBGyCrWtz92Y=; b=wCPxC9BiLDVcn/+ilyr7yznGnsV2QqCQ5L+hbzrHbK1oo9sZhMPVpIz72kLXbkCpjF JUJOl8+14zzkSujKB28ZR+R0kApecavTGCxMWgddzcXJGBgd8io0UdQuqvU4Q+01WrX8 LgkkbcfppbK4GEFa5vC+durtqcSrNEoAbOf3dGUKJ6QExLrnBfjbuHP/E3E3Oz7jGXi9 gycbSLJrvC3pUNJNyZjiwHZwvs2IKSJQZYhI4vcw5VBxsAGKCcJlFWGKA67rZNzirJ1X 5yG/NqI7pPA/fQMt8if1kllmUlsPqXKQupLJiaO7OF61JMXC/HjqUhfaSrkXfE+RFP4Z m7Ag== X-Forwarded-Encrypted: i=1; AJvYcCX6Kfug+1yQHPgbfCiloolndHbrimX8TG2qA7ZDmpD5DCuqrmy4eEkus1xSPJ4dxelbsNIVc2oZC696IuC0keIxyAI0LfFiTaZsSa7B X-Gm-Message-State: AOJu0Yz7Ysd9rpwnWvwrQF7jXn0C+e9saKr8vVvPmbMr8Bt6oIpRspzm Oyda9zOV/6NSkkBlQsmOT1+X/Bsg6ECXXJ8iZLylTf6SSsZDD4+kOHMqBL+mMw== X-Google-Smtp-Source: AGHT+IGWyzL0J8tgLw+IS2O+OJOzAvrrypoDFNGwlNkmgIYYniPsXAzxTBE3WzUHUDQcLbcOv1YKJg== X-Received: by 2002:a0d:e343:0:b0:62f:b282:9f02 with SMTP id 00721157ae682-658eee58250mr135931387b3.21.1720825912901; Fri, 12 Jul 2024 16:11:52 -0700 (PDT) Received: from google.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id 00721157ae682-658e4d2ab4bsm16777027b3.33.2024.07.12.16.11.51 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jul 2024 16:11:52 -0700 (PDT) Date: Fri, 12 Jul 2024 16:11:49 -0700 From: Noah Misch To: Jeff Davis Cc: Michael Paquier , Nathan Bossart , pgsql-hackers@postgresql.org, Joe Conway , Robert Haas Subject: Re: MAINTAIN privilege -- what do we need to un-revert it? Message-ID: <20240712231149.0d.nmisch@google.com> References: <20240223213055.GA1805646@nathanxps13> <196d1c86d43a147e777bb74159774f255dbe73e3.camel@j-davis.com> <20240228165523.GA2435539@nathanxps13> <31ae1bf73e8a1dae3ee7ef2dd43a9bbda1e1ab86.camel@j-davis.com> <20240630222344.db.nmisch@google.com> <23fac3b2a722373a52b92a12ee0f500821450f97.camel@j-davis.com> <20240711125207.98.nmisch@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Jul 12, 2024 at 02:50:52PM -0700, Jeff Davis wrote: > On Thu, 2024-07-11 at 05:52 -0700, Noah Misch wrote: > > > I could try to refactor it into two statements and execute them > > > separately, or I could try to rewrite the statement to use a fully- > > > qualified destination table before execution. Thoughts? > > > > Those sound fine.  Also fine: just adding a comment on why creation > > namespace > > considerations led to not doing it there. > > Attached. 0002 separates the CREATE MATERIALIZED VIEW ... WITH DATA > into (effectively): > > CREATE MATERIALIZED VIEW ... WITH NO DATA; > REFRESH MATERIALIZED VIEW ...; > > Using refresh also achieves the stated goal more directly: to (mostly) > ensure that a subsequent REFRESH will succeed. > > Note: the creation itself no longer executes in a security-restricted > context, but I don't think that's a problem. The only reason it's using > the security restricted context is so the following REFRESH will > succeed, right? Right, that's the only reason. > @@ -346,13 +339,21 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt, > PopActiveSnapshot(); > } > > - if (is_matview) > + /* > + * For materialized views, use REFRESH, which locks down > + * security-restricted operations and restricts the search_path. > + * Otherwise, one could create a materialized view not possible to > + * refresh. > + */ > + if (do_refresh) > { > - /* Roll back any GUC changes */ > - AtEOXact_GUC(false, save_nestlevel); > + RefreshMatViewStmt *refresh = makeNode(RefreshMatViewStmt); > > - /* Restore userid and security context */ > - SetUserIdAndSecContext(save_userid, save_sec_context); > + refresh->relation = into->rel; > + ExecRefreshMatView(refresh, pstate->p_sourcetext, NULL, qc); > + > + if (qc) > + qc->commandTag = CMDTAG_SELECT; > } Since refresh->relation is a RangeVar, this departs from the standard against repeated name lookups, from CVE-2014-0062 (commit 5f17304).