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 1qfXc0-00BrwH-8p for pgsql-bugs@arkaria.postgresql.org; Mon, 11 Sep 2023 03:29:20 +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 1qfXby-00CxlR-AT for pgsql-bugs@arkaria.postgresql.org; Mon, 11 Sep 2023 03:29:18 +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 1qfXbx-00CxlI-WF for pgsql-bugs@lists.postgresql.org; Mon, 11 Sep 2023 03:29:17 +0000 Received: from mail-oa1-x36.google.com ([2001:4860:4864:20::36]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qfXbt-004NAb-K7 for pgsql-bugs@postgresql.org; Mon, 11 Sep 2023 03:29:17 +0000 Received: by mail-oa1-x36.google.com with SMTP id 586e51a60fabf-1cc61f514baso2362823fac.1 for ; Sun, 10 Sep 2023 20:29:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694402951; x=1695007751; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=n3J3DZvFsAyId2YzfRPGsSWyCBjKzrvNPRq5S25ZPUw=; b=Yj2IpV2QN6CPa+pz4+cTWoKZ7bLr/Yn51EswgpHNxE+L/yi5LIkbnskUx5hklbX2qR FxaiXJ4fYArF/gFuoCcnP751XnT6nKJUJFCxzljMlJy6AUXqeowao8+IV+bylaYDW6II lCfPNew7wF/izXG/9v5mo3auw16mDAHtHqYFW1FdjX1rlNGS2FL2YErYsI75kMsHHv6a svY2UhvIAcwBRGmIcca0abTZ30iydlIl07w4gqNKD4UsI1kuABxH0s2xg0hQXHUIIl3E jG6Vg/3ikW091xB9ZWFr0RXxoOVjDpvBRQkzRLgSBxnr+a2n7k7dvBPGJI4ZqsZFALd+ d77A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694402951; x=1695007751; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=n3J3DZvFsAyId2YzfRPGsSWyCBjKzrvNPRq5S25ZPUw=; b=BHdivWBcEQLWZVDnBuz3qRxo0aezM/pZlLS2VCW8Qqcb7qGQaYyKButwnuLji1FtO5 2v82y0OfvgEoED0L8Pvuoto1o5rXpqMUy+39WqOdu8GxJE9dYFEMgV/x0oiC5pNQzDPZ RYDoGcC22WHvulAHX5UNoHQGevgfNQ07/boRLYOjrIbGxorKkGYOtgbVVMAWTPvnB90b tn1qSOtjVPRWvLtFIOG/FK04iTHRQpmN+pURpP5BsyQUbcy+EqAkK+57npOhLvkLPxvo 5upenonMJ8ewDPiCPtQNEu0mnVWTPfEgOqoXcgxE3zbS7BEw6jKEgcFTG6Emjp2JBq9t RAaQ== X-Gm-Message-State: AOJu0Yxr09ri0T4hIyOMxwiR7tyjN8hgxY9/n3J+SpaBCPU/eoUvNysf A9RoyHfLCPYlTZegNg6WDt0xTk5pov+x1JCAtzs= X-Google-Smtp-Source: AGHT+IFrvqEpTGVncV/eXcCecoHibMPisOoy8PCUW/BFOuRKMvR2pWKC5aNC+KmYkxAj4aIGuZ9ja8D8/WxWroCrQ/Q= X-Received: by 2002:a05:6871:8fc3:b0:1d5:a6ac:ac8d with SMTP id ab3-20020a0568718fc300b001d5a6acac8dmr2436366oac.47.1694402951286; Sun, 10 Sep 2023 20:29:11 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Amit Kapila Date: Mon, 11 Sep 2023 08:59:00 +0530 Message-ID: Subject: Re: [16+] subscription can end up in inconsistent state To: Jeff Davis Cc: pgsql-bugs@postgresql.org, Robert Haas Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, Sep 10, 2023 at 9:15=E2=80=AFPM Jeff Davis wrot= e: > > [ Moved from -security, where it was initially posted as a precaution. > It was determined that the normal bug process is the right way to fix > this. ] > > If I understand correctly, the security requirements for checking a > non-superuser's connection string are that (a) the connection string > itself specifies a password, thereby ensuring it won't come from a > PGPASSFILE; and (b) checking PQconnectionUsedPassword() after the > connection to ensure that the password was actually used for > authentication, rather than some other method. > > If subpasswordrequired=3Dtrue, that causes (b) to be consistently checked > for all connections. > > The checking of (a) is more complicated -- it's checked at CREATE/ALTER > time, but only if the user is not a superuser. If the superuser gives > the subscription to a non-superuser, or if the subowner loses their > superuser status, then the subscription is in a weird state: > subpasswordrequired may be true even if subconninfo doesn't have a > password. That means that (a) is not being checked, and (b) is being > checked. > > Repro (16+): > > Publisher: > CREATE USER repl REPLICATION PASSWORD 'secret'; > CREATE TABLE t(i INT); > INSERT INTO t VALUES(1); > GRANT SELECT ON t TO repl; > CREATE PUBLICATION p1 FOR TABLE t; > > Subscriber (has a PGPASSFILE for user "repl"): > CREATE USER u1; > CREATE TABLE t(i INT); > ALTER TABLE t OWNER TO u1; > -- no password specified; relies on passfile > CREATE SUBSCRIPTION s1 > CONNECTION 'dbname=3Dpostgres user=3Drepl' > PUBLICATION p1 WITH (enabled=3Dfalse); > ALTER SUBSCRIPTION s1 OWNER TO u1; > SELECT COUNT(*) FROM t; -- 0 > \c - u1 > -- still using passfile > ALTER SUBSCRIPTION s1 ENABLE; > SELECT pg_sleep(2); > SELECT COUNT(*) FROM t; -- 1 > ALTER SUBSCRIPTION s1 REFRESH PUBLICATION; > > It's also possible to get into this state by the superuser modifying a > non-superuser's subscription. > > The purpose of the "password_required" option is to handle the case > where a subscription was formerly owned by a superuser, or has been > modified by a superuser. The docs say: > > "[password_required] ... This setting is ignored when the > subscription is owned by a superuser... Only superusers can set this > value to false." > https://www.postgresql.org/docs/16/sql-createsubscription.html > > So a superuser changing the owner or modifying another user's > subscription is a supported operation, and not just a superuser doing > the wrong thing. (Perhaps the docs should be more clear on this point?) > > We could address this either by: > > * maintaining the invariant that if subpasswordrequired=3Dtrue, then > subconninfo specifies a password; or > * calling walrcv_check_conninfo() before each connection > Can we think of calling walrcv_check_conninfo() when the following check is true (MySubscription->passwordrequired && !superuser_arg(MySubscription->owner))? IIUC this has to be done for both apply_worker and tablesync_worker. --=20 With Regards, Amit Kapila.