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.98.2) (envelope-from ) id 1xHO6M-00000002yYG-2nh2 for pgsql-bugs@arkaria.postgresql.org; Thu, 15 Oct 2026 16:14:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xHO6L-00000009s2V-2mjN for pgsql-bugs@arkaria.postgresql.org; Thu, 15 Oct 2026 16:14:41 +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.98.2) (envelope-from ) id 1xHO6L-00000009s2N-1Cn0 for pgsql-bugs@lists.postgresql.org; Thu, 15 Oct 2026 16:14:41 +0000 Received: from relay1-d.mail.gandi.net ([2001:4b98:dc4:8::221]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xHO6I-00000002Guk-3yP6 for pgsql-bugs@lists.postgresql.org; Thu, 15 Oct 2026 16:14:40 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id DDDAB3EE2E; Thu, 15 Oct 2026 16:14:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vondra.me; s=gm1; t=1792080872; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GGPNgUtzkarE7uhV0Q/ewKTMT9shTKw2rKTyN2umqAU=; b=aNNGx+TKqdkQM0CR8MDdipJa1t0HmuZbAxg7SpF0WpG8xbzjZS0cuw4/R9/nEwW7I43Iyz pIwPU7k2tPJgMGvuE4DxavBcTgsIV8uTRHsBV6JdhrVccMVqAr4q+6IohTApFjKcPZyPNK 2RO6ASukwQeePUYioFc0CCS55QHSZgCLP1tyt4eV+s8aMTNM0zIE0twl+Z/WjwVIK5iwmk fDgFtWhvX7FAUZPzHl0DNvOrXDMP/dmcQr8F+zgu2/0OMBFniZzD7vcA73Pk+gOkIP2HI1 JFF1AOZ9e2RNFdEMmJ61r37uS0dDkDYIWxGv47yIroU7LdP1Ekrv/13igARR7Q== Message-ID: <2b40afe4-3379-4ec0-815d-954e648fed1b@vondra.me> Date: Thu, 15 Oct 2026 18:14:29 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: BUG #19762: ManualBug1 tcn: NULL pointer dereference in triggered_change_notification() via strcpy_quoted To: 1217816127@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form References: <19762-04e5ab8e5459b958@postgresql.org> Content-Language: en-US From: Tomas Vondra In-Reply-To: <19762-04e5ab8e5459b958@postgresql.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-GND-Sasl: tomas@vondra.me X-GND-State: clean X-GND-Score: 0 X-GND-Cause: dmFkZTG1vhd+ZnyCsKbjVdwAQIKfHVE0L/r+3GsvJNg9VMwfMKjau4tFQ6sFfvyvybUwifjLhqwV4tsaHPLEPBTJiaLLywVoFurI19GyDpltIlCbjQTBXT/5H0EwXiad02n6d/kBF5tMGY33NCpf/v24hJ9nxNaIZI2fDIJphpzL2xVO1WBvNH/HG6tYKyAp0iAJYL6fdPGlyEGJWOhXAi4ff9n3xyjkvXZXPPDCOLWHZFWYblpYvWwDbPnvNwf6SeVf9uCy3DFeowCokcBlzR8pDb4lX1ZFBa8xm78OuKw6OKoqlGk/NYFmu1n9zBm1/mU+KBJwYccNy5WS3XZgh2If2O6/D198xItCyMd9XpV29id81iZu0pwAE0oFYzlfD5TZABxNGlZnD1zL7kxx7CStG/Rr8zHOYtSguaymUTXbenBgyo8SPjN5Q9/EaclWFxjqyimP9sPnxAFs/axXcW4q4PV+etvPNRdvAsD5BdGsd+NkEFfGjTLnS6Qx8s4YOvjtniUYMYMikWpkcXPr0hJV/FodrGRkxSDFpgC8Ei9b1rPZY5gGyw7wQ1qxthdrMYRlLKGhwElwmlcv1Gx+AToZ4cchrb+/jysh5a4Lhkkdl4i4H/+YUtDBnh9ZhuvGWSZYlMjQSAA9W5ZIaefxrDBzcD45b0TldTEpAPCJWztpABD/7g List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 10/15/26 17:02, PG Bug reporting form wrote: > The following bug has been logged on the website: > > Bug reference: 19762 > Logged by: Yuelin Wang > Email address: 1217816127@qq.com > PostgreSQL version: 19rc1 > Operating system: Linux (Ubuntu 24.04, x86_64) > Description: > > ### What happens? > > In `contrib/tcn/tcn.c` at line 165, the function > `triggered_change_notification()` calls `SPI_getvalue(trigtuple, tupdesc, > colno)` and passes the return value directly to `strcpy_quoted()` without > checking whether it is NULL: > > ```c > /* contrib/tcn/tcn.c:165 */ > strcpy_quoted(&payload, SPI_getvalue(trigtuple, tupdesc, colno), '\''); > ``` > > `SPI_getvalue()` returns NULL when the requested column value is SQL NULL > (it sets `isnull = true` and returns NULL from `heap_getattr`). > The function `strcpy_quoted()` defined at line 36 immediately dereferences > the pointer at line 39: > > ```c > static void > strcpy_quoted(StringInfo r, const char *s, const char q) > { > appendStringInfoCharMacro(r, q); > while (*s) /* line 39: unconditional dereference */ > ... > } > ``` > > The violated invariant is: any caller of `strcpy_quoted` must guarantee the > string pointer `s` is non-NULL before passing it. > The Entry61 fix in `refint.c` showed the identical invariant being violated > by `quote_literal_cstr(nv)` where `nv` could be NULL. > Here the same class of bug appears: a nullable value returned by > `SPI_getvalue` is passed to a string-consuming function that dereferences > the pointer unconditionally. > The column `colno` comes from `index->indkey.values[i]` for the primary key > index. > Under normal DDL, primary key columns are NOT NULL, which prevents > `SPI_getvalue` from returning NULL. > However, the code contains no guard against NULL and will crash if > `SPI_getvalue` returns NULL for any reason (for example when a superuser > bypasses the NOT NULL constraint by directly updating > `pg_index.indisprimary` on an index whose key columns lack a NOT NULL > constraint, or if a future code path makes the invariant reachable through > other means). > The correct fix mirrors Entry61: replace the bare `SPI_getvalue(...)` > argument with a ternary expression that substitutes the unquoted SQL keyword > `NULL` when the value is NULL, exactly as `dblink.c` already does at lines > 2197-2203. > > ### To Reproduce > > Start a fresh PostgreSQL ASAN instance and run: > > ```sql > CREATE EXTENSION tcn; > > CREATE TABLE tcn_test (id int, data text); > > CREATE UNIQUE INDEX tcn_test_uniq ON tcn_test (id); > > SET allow_system_table_mods = on; > > UPDATE pg_index SET indisprimary = true > WHERE indexrelid = 'tcn_test_uniq'::regclass; > I don't think this counts as a bug. It requires superuser privileges to update catalogs to break a primary key - effectively introducing data corruption. Pretty sure there are countless other places relying on PK columns being NOT NULL. A superuser can introduce all kinds of corrupted data and cause far worse stuff. If you could trigger this without requiring superuser privileges, that'd be a different story, ofc. regards -- Tomas Vondra