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 1nu6Oe-0001ow-Ta for pgsql-bugs@arkaria.postgresql.org; Thu, 26 May 2022 05:50:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nu6Od-00008G-Q1 for pgsql-bugs@arkaria.postgresql.org; Thu, 26 May 2022 05:50:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nu6Od-000087-AK for pgsql-bugs@lists.postgresql.org; Thu, 26 May 2022 05:50:55 +0000 Received: from mail-qv1-xf2a.google.com ([2607:f8b0:4864:20::f2a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nu6Oa-0007V6-0W for pgsql-bugs@lists.postgresql.org; Thu, 26 May 2022 05:50:54 +0000 Received: by mail-qv1-xf2a.google.com with SMTP id v5so953625qvs.10 for ; Wed, 25 May 2022 22:50:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=AizwQWdhuGI7nL8QIPHA2Dhbn+wa8qk7+g2MhYEBM1o=; b=T3foIPkETgVxqE/913vIWRi387i28MAtnKBvPQaWxl+IYoeRAxvdyQEtamAGDjITpU 9cwuNy79j+SMgh1yhI0gaEs5ZzkPtMj0gChHaTqRazkvQ1F7CqSROYrTi3PwAGDKmcSl FgKbJQhLuLh23NDfKcVnQ1QE+eEJ0Jq79e72E= 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:user-agent; bh=AizwQWdhuGI7nL8QIPHA2Dhbn+wa8qk7+g2MhYEBM1o=; b=Gmwj0IrADbm6KFkQcqMy2rPZBU7ABGwZHU5lXqeyin1txd1b40uotuxSnst1zT9jtn bnAJLgAOVGImgRncmpFFCHlnQxk7hwSRyWB2V9UyHQU94loqr742x30CXvZT1L0wqjVM MAILmb3KVoTVmGyepzIiQKwO/rJZgvWMbLx4+dZNqOzz7+zCOEdG9bKYkPbnoRitM4az cG8psClkyQxKMkvm0ZN6rDYgzdJioJ8fcz+MnEWiHG8N5hSNzBnzwPNv2Zk0v5EYRvBr SE/WQEgQf/ITHlifR5yVENZH6g2HnFd/1ZKN0sF5RCueqSCNpEFR2wuRLUIOR5oZQvf6 1dpg== X-Gm-Message-State: AOAM531N90MqeuBGlJ2B3pYz3QyujQDr1TMW3f7bM6u7VXz448bjT1ML Bp/d1Yp+RJ+jsA5DaPARJeOthjbYo9oI7Q== X-Google-Smtp-Source: ABdhPJxjiq0sQJPGMRzeLm01SrNCxtpw/sedqXKrULQS26CRDDZt7VcYEEckvOv8LOOdjLlAC7+QDw== X-Received: by 2002:a0c:fe0c:0:b0:461:d8e1:41d5 with SMTP id x12-20020a0cfe0c000000b00461d8e141d5mr28420764qvr.125.1653544251072; Wed, 25 May 2022 22:50:51 -0700 (PDT) Received: from rfd.leadboat.com ([2600:1702:a20:5750::2e]) by smtp.gmail.com with ESMTPSA id c13-20020a05622a024d00b002f942e6bd88sm516793qtx.48.2022.05.25.22.50.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 25 May 2022 22:50:50 -0700 (PDT) Date: Wed, 25 May 2022 22:50:47 -0700 From: Noah Misch To: Tom Lane Cc: =?iso-8859-1?Q?F=E4rber=2C_Franz-Josef_=28StMUK=29?= , pgsql-bugs@lists.postgresql.org Subject: Re: Extension pg_trgm, permissions and pg_dump order Message-ID: <20220526055047.GA3153526@rfd.leadboat.com> References: <1961551.1653498155@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1961551.1653498155@sss.pgh.pa.us> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thanks for the report. On Wed, May 25, 2022 at 01:02:35PM -0400, Tom Lane wrote: > =?iso-8859-1?Q?F=E4rber=2C_Franz-Josef_=28StMUK=29?= writes: > > My minimal example goes like this: On the fresh container, execute > > > ```sql > > CREATE ROLE limitedrole; > > CREATE SCHEMA ext_trgm; > > CREATE EXTENSION pg_trgm SCHEMA ext_trgm; > > GRANT USAGE ON SCHEMA ext_trgm TO limitedrole; > > > SET ROLE limitedrole; > > CREATE TABLE x(y text); > > CREATE INDEX ON x USING gist(y ext_trgm.gist_trgm_ops); > > ``` > > > Dump the database with `pg_dump > /tmp/x`, then do > > ```sql > > DROP SCHEMA ext_trgm CASCADE; DROP TABLE x; > > ``` > > (or alternatively create a fresh database and do a ` CREATE ROLE limitedrole;`) > > > Then try to restore the dump with `cat /tmp/x | psql`. More-minimally, one can run this without involving pg_dump: 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; > > On version 14.2, this succeeds. > > On version 14.3, this fails with "ERROR: permission denied for schema ext_trgm". > > I think what is happening here is that parse analysis of the index > expression is now being done as the owner of the table (already assigned > as limitedrole), as a consequence of the patch for CVE-2022-1552. Yep. > So basically, that patch has completely broken pg_dump's scheme for > when it can issue GRANTs. I'm not sure there is a simple fix :-(. 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. > We could issue the GRANTs earlier but that is going to break some > other use-cases, if memory serves. It does. Early GRANT is still the right thing, but there's more to build before one can do that without breaking things that manage to work today.