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 1whVqh-000Gsr-01 for pgsql-bugs@arkaria.postgresql.org; Wed, 08 Jul 2026 17:14:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1whVqf-008M6X-0h for pgsql-bugs@arkaria.postgresql.org; Wed, 08 Jul 2026 17:14:13 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1whVqe-008M6P-37 for pgsql-bugs@lists.postgresql.org; Wed, 08 Jul 2026 17:14:13 +0000 Received: from fhigh-b2-smtp.messagingengine.com ([202.12.124.153]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1whVqd-00000000Bc1-2UOM for pgsql-bugs@lists.postgresql.org; Wed, 08 Jul 2026 17:14:12 +0000 Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfhigh.stl.internal (Postfix) with ESMTP id A949A7A00DF; Wed, 8 Jul 2026 13:14:10 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Wed, 08 Jul 2026 13:14:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eisentraut.org; h=cc:cc:content-transfer-encoding:content-type:content-type :date:date:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:subject:subject:to:to; s=fm1; t=1783530850; x=1783617250; bh=8CeeN84V1aPKxJXssuayRNcTLDK3RPry rsbCykiyPlA=; b=tO++jkJue4h23beb17RzyV4X/fuM2EYRbp+5Le47s//aaIUH 9zXqYcxmHuTqm+NOQrU5FtxI73auzTYklh/vhY1vbX+Wv3wCBKCMqUHJiDlQmEKl 9jfrGIoY+RFrnZ0T+SQu1oKM/pJivY3vBiSPF6YwkiYaIXcCCTrk0pK80IuPNzfw dca4Cr8Kc8SV59ANshVpYhVACGkgHL5gVVgXvGiXRNTrl9RE+NZVMtFQ2NS5LTQG Cd2rvpvhl/vqHqAqs/pwWrwxWs+Ozq72lZVue/zqQvjduZU5ry9mgudMMitGAWVd IWZ2j6VRT7c4TUpLPCRPCpZ0ub8ar96M1J/V2Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1783530850; x= 1783617250; bh=8CeeN84V1aPKxJXssuayRNcTLDK3RPryrsbCykiyPlA=; b=n doELTWDLr259kgKs8Ueg++G1QqIP2EmbFCDHNXs64CADowMZCjPWSyItk9ngjUuq ze0lt9TqhjfmAnyaK2NId/zYGjzZP7LRnXW/aTJKpdFbofVudcB9tRITBqr+PF+7 6kklDpwQ8JmUbcZ55jRneRXtmymr7rqIP4NISApGpUbm6mmu2XAe5+0JBUizbCQd V2S8H1o3kOex2CTSH4ZabXZvFERQK7Zz5eChVx15oe2GH45a039Oopmt2agGrq0b loDwc3Ixohx3y/xoexuvrIJScgnHPps7KB2b5t7SxO02TOXyfxgPAK7iaodOfT20 UkYk03ZIIjRm9t2V8e+xQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTFcY5pMO+yzdbJ/GDbB+i+adfn8GI1StFJXdHm/kXlPFvE4gmuHzKEqjRrGKLLL0B PmmaHb8IFqmm7WcRYub0CVmnvv86PHDxuJzNJLBBTvChQ8oDwucxJeKSuf5f6oa7Cl3zkB rjV1+OCs/YnNCOrkMjQDyXfywc71IjpOmUywaiAt/qy5rhEyza0/TifHqAjUVzT8PlWk3j +w1qAmTZXp4//B7RRLhkHlydH/T2kjL0zLxogL8Cs1TjQ208B33deC3WljWazdTkHqy83y SO9/Dr8Hms8liPDnXrERrixmtjjx+dIfGXUr2l6jYXj0LDdWZT+Az7w7X7EfIRAJyHDXs9 74tkUg98Ojy7J2vlbQKu3tXpT39TP5kPGsNjaP+RKtDILb9ldnpP2Trg2SOcCzvsRDKxX+ n85hr7x6h8dqLUbSsTZoZJcoUfG/q01JOnGHNTBv8K5KLQc1c3Qx4Vnn4e+Nea/KI5yeAu 80fbduzfdSm3lLmUOmDQiKN+dVLBqMm0aIyzpSaJraRLmVQN7/vRqOJdzvhxWOe5kRSDo1 S2bRJfqOQlZoRVwepKYS1O9ORzz/yCwAkXzYQRB33+oZ8EKWbGA/yQnkbnvMA9X1M6LhsL pvs7jbi+4L63UCdV5MAkgTl1YZ/3+xo2t7HBkfgBzYrbTqHpmCIC43wV6IRw X-ME-Proxy: Feedback-ID: ie0a040ee:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 8 Jul 2026 13:14:09 -0400 (EDT) Message-ID: <53f1faeb-72a4-4180-b2cd-88d524003b9d@eisentraut.org> Date: Wed, 8 Jul 2026 19:14:08 +0200 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 , Heikki Linnakangas Cc: 1217816127@qq.com, pgsql-bugs@lists.postgresql.org References: <19545-0f25b7e47351e8fc@postgresql.org> Content-Language: en-US From: Peter Eisentraut In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 08.07.26 13:34, Ewan Young wrote: > Hi Heikki, > > Thanks a lot for taking a look, and for the good questions! > > On Wed, Jul 8, 2026 at 3:52 PM Heikki Linnakangas wrote: >> >> 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. > > Good point, agreed. v2 (attached) uses int for keylen everywhere: in > GinTuple (was uint16) and in GinBuffer (was Size); the local in > _gin_build_tuple() was already int. int matches the tuplen and nitems > fields of GinTuple and is plenty wide (a key can't exceed the 1GB varlena > limit), so it seemed like the natural choice. Size (or size_t) is the correct type for sizes of objects in memory. Note that the return type of VARSIZE_ANY() is already Size, so by using int you are still doing a type truncation, and by using a signed type you are introducing unnecessary potential for confusion.