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.96) (envelope-from ) id 1whN5a-000BJG-0u for pgsql-bugs@arkaria.postgresql.org; Wed, 08 Jul 2026 07:53:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1whN5Y-0063ET-2Y for pgsql-bugs@arkaria.postgresql.org; Wed, 08 Jul 2026 07:53:01 +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.96) (envelope-from ) id 1whN5Y-0063EL-1n for pgsql-bugs@lists.postgresql.org; Wed, 08 Jul 2026 07:53:01 +0000 Received: from lahtoruutu.iki.fi ([2a0b:5c81:1c1::37]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1whN5W-00000000CSU-2n5J for pgsql-bugs@lists.postgresql.org; Wed, 08 Jul 2026 07:53:01 +0000 Received: from [10.0.2.15] (unknown [130.41.208.2]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4gw9LW4rlSz49PxK; Wed, 08 Jul 2026 10:52:51 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1783497172; 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=qI9m5IPfhQxH+2pR93UOqF8T1Y9hz+Krkh6/Hk3eiKM=; b=Yct1ZRqH65D2M0cTOK82WdOlU+M9ht/Vwl+voYrGr/qEr/KybwLIBA6eSLCeQKHhpEXfs2 A/hXwyt5jVL97KLGe4giMN6aad/w7HZVtwyb6lBETetpJiCemEfWDii0DRMNONc04R5lha F3YTPzEJEKANbSVx8m2cQXewUqkh+gG8GMSR96+VN6l2QKNsK4IATv7CMnEJpGweLot0bx xJUu7ZellGnLootdMc3L4h+VGP7fV5X3l8CZWAbqWrHOxTylKwHY++zE/nw4hpEyD/OIA7 Rmj4IetCLQG7j54RI7r/4b8Xm5LOUZQHf1vantkqoeDPsTftglzknXSGjhdgdA== ARC-Seal: i=1; a=rsa-sha256; d=iki.fi; s=lahtoruutu; cv=none; t=1783497172; b=J4zY3iYFQ3NucbHJJQG661wyxVe1KGTGevJZpSPNs8wnV3OP4Gi0s6qf6J1xLKSx1nMnIl Pyk1ZIpXFNC+seWQquI+8kgTsMoUNVbVvbW4rIs/WAvH8ENOJo1b6Dvr+/TDLJ50TWuOC7 1sDH8jn+7/swj71ykjpKo/nKjzx5XHWzHQOxXFYw3sXNnT5L2IrQ99k93o2gF3iNJ3tWSk p/40glqDlOY1nuHz9uCGl4TGrFb7I+cEzZCczMP1zPIHRs4bbZrY/KE2VtiUprKWberBjP hWvXJa77BX0ytqxHtT06tUw8Rcr8SH2CD6glGJXhmuSMLS78zzqxZ+1dyZ1a7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1783497172; 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=qI9m5IPfhQxH+2pR93UOqF8T1Y9hz+Krkh6/Hk3eiKM=; b=WsxUR0PBWiCZZPY9B/780Iavmngp8Hpqs6i6VvdEMRummN6lp4AVZKm6Dq4XE0LOeLoqEA 0FtHJYbTSPgLHAlUoSkPs06z/tQ+O6bU0nARkbBibf7/aXgtAtRnnYqgdGPNHe68wJSn5H ToEdskHed+E4TNIOzpOxf/vd51oogyteVHdZYcwg0Bf8Zy9Bsw+Emk90VSxoGIjeM943kW CjsiuTSKoPk0Tu7QIoUq+m5mfcgwE6gcMakBgNsoHNkByfkanGGXc/URRh2j3x996OEviA uVjAmVgNBGRJfhZx03jk3XhJFEDArM2huWaEPQDG67V7hsHC0OJyb88S8TIUUQ== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi Message-ID: Date: Wed, 8 Jul 2026 10:52:50 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: BUG #19545: Integer truncation of `GinTuple.keylen` causes out-of-bounds read in parallel GIN index build To: Ewan Young , 1217816127@qq.com, pgsql-bugs@lists.postgresql.org References: <19545-0f25b7e47351e8fc@postgresql.org> Content-Language: en-US From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 08/07/2026 09:27, Ewan Young wrote: > Hi Yuelin, > > Thanks for the very precise report -- I reproduced it on master and your > analysis is exactly right. _gin_build_tuple() builds the whole GinTuple > (palloc size, key memcpy, TID-list offset) from the int keylen, but the > stored GinTuple.keylen is uint16, so a key wider than 65535 bytes has its > stored length truncated. On read-back GinTupleGetFirst() and > _gin_parse_tuple_items() recompute the posting-list offset from the > truncated value, and ginPostingListDecodeAllSegments() then walks the key > bytes, aborting (or reading past the allocation on non-assert builds) > exactly as you saw. It's parallel-only because only the parallel path > serializes a GinTuple. > > I went with your fix A -- widening keylen to uint32 (attached). It's the > minimal root-cause fix: the stored length now matches the length the rest > of the function already uses. Ugh, the datatypes used for keylen are all over the place. In GinTuple struct it was 'uint16', in GinBuffer it's Size, and in the _gin_build_tuple() function's local variable it's 'int'. Would be good to make them consistent. > I preferred it over an explicit ereport at > UINT16_MAX, since 65535 isn't a meaningful GIN limit -- the entry-tree item > limit is much smaller and is applied to the (compressed) tuple by > GinFormTuple() -- so rejecting there would be an arbitrary cutoff. Hmm, we don't compress the key data though, so a tuple with a key larger than 65535 will inevitably fail in GinFormTuple(), right? I agree it would be a little arbitrary to cut off at 65535, but then again, it seems a little silly to continue when we know it's just going to fail later on. I think it would make sense to check if keylen > GinMaxItemSize. It might still fail later in GinFormTuple(), because the index tuple headers take some space, but still. - Heikki