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 1vxvih-00HGcD-1X for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Mar 2026 23:33:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vxvie-00EvB1-23 for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Mar 2026 23:33:33 +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 1vxvie-00EvAs-0r for pgsql-hackers@lists.postgresql.org; Wed, 04 Mar 2026 23:33:32 +0000 Received: from p-west3-cluster3-host2-snip4-2.eps.apple.com ([57.103.72.163] helo=outbound.ms.icloud.com) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vxvib-00000000h8U-3Nhy for pgsql-hackers@postgresql.org; Wed, 04 Mar 2026 23:33:32 +0000 Received: from outbound.ms.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-west-3a-60-percent-4 (Postfix) with ESMTPS id 2D4761800102 for ; Wed, 4 Mar 2026 23:33:27 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1772667207; x=1775259207; bh=micYw+0KtVCT77bORC5TuhTyHBJjuotX7fSj9Tjsy7M=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:x-icloud-hme; b=tnOUKU9jwG2hX/CDMAyaPkkkc6Hbd50IKKf2SfsRAUglSgb6QZYp9DtHRVDiOht5kBDY2TkaGpJUQbRW/gnSekGlbF4CQ9K30RSqLdXM/+FQXVFBz5K4IQT4P0iO3fYY60sgOwQBNXjHFVJk0yg7sjrIUsolWXP9gSb71NH9DZm+pvZZBgqY2QWQOLthV5WeMPFG3qi2gMiITIy70EMuKT7SItcTdjbn9CovHkX+Ok0mpd+kkr3dXIAPjUSmSgxUtWdziU+zMLC1+2syko1fV8OvhfLBTJ2UGR37GkOdJQ6TYc4H5xOeSojLLxitNvKhtpkpnW3gjHm6EGiR8TEwJw== Received: from PAUL (unknown [17.57.154.37]) by p00-icloudmta-asmtp-us-west-3a-60-percent-4 (Postfix) with ESMTPSA id A1A0B1800104 for ; Wed, 4 Mar 2026 23:33:26 +0000 (UTC) From: To: References: <044401dcabac$fe432490$fac96db0$@icloud.com> In-Reply-To: <044401dcabac$fe432490$fac96db0$@icloud.com> Subject: RE: [BUG + PATCH] DSA pagemap out-of-bounds in make_new_segment odd-sized path Date: Wed, 4 Mar 2026 15:33:24 -0800 Message-ID: <00f901dcac2f$4c8cf9a0$e5a6ece0$@icloud.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_00FA_01DCABEC.3E6A7CF0" X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQG6QsibSPUizeMIuu+P0Wl8N/HzObXj12tA Content-Language: en-us X-Authority-Info-Out: v=2.4 cv=cJftc1eN c=1 sm=1 tr=0 ts=69a8c147 cx=c_apl:c_pps:t_out a=qkKslKyYc0ctBTeLUVfTFg==:117 a=Yq5XynenixoA:10 a=x7bEGLp0ZPQA:10 a=1_K1eIhTUloA:10 a=VkNPw1HP01LnGYTKEx00:22 a=DAwyPP_o2Byb1YXLmDAA:9 a=v3ZZPjhaAAAA:8 a=epTmVMiNAAAA:8 a=ePYpYu1eSlWsgg9QHLMA:9 a=CjuIK1q_8ugA:10 a=yMhMjlubAAAA:8 a=SSmOFEACAAAA:8 a=mXnn9AnQOTG12yQfAGMA:9 a=pcgcqsGdUEoyX44X:21 a=gKO2Hq4RSVkA:10 a=UiCQ7L4-1S4A:10 a=hTZeC7Yk6K0A:10 a=frz4AuCg-hUA:10 a=DNmq7HOU84q2RHPteTMA:9 a=B2y7HmGcmWMA:10 X-Proofpoint-ORIG-GUID: OPF4T2l5ZUBC7R-x9ZJVzdgjpq-csArO X-Proofpoint-GUID: OPF4T2l5ZUBC7R-x9ZJVzdgjpq-csArO X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMzA0MDE5NCBTYWx0ZWRfX7BT++vEwFhSc UVK3yfUJAz6yBy3weLg6jimhIKMp8k9VvIfvD8ulm1F7gS0of47xQbTpzrtfK/MQ7x80NhelT5L aQhK5Z1B0R87Hhyp/PmT7y9N6rkvxvY43Py7w/koyry774IfiJ6hV3LVUuViuwgbGIAROHBUBn3 /vzLYPC5kHvixONsbxySSlpJvoK1JJIPwl3yp/cEECagZlEtSHtF32mq9KBwrmWp5L0nBkAOmn+ 3PLxu0J64OiEUp9jwKEDE20x3LpUFwJzo34hWmaw7TIoFsYx6kzb4XKcISMmw+ElSaxwL5+4gnU eU2ykxTRdrLJUl8I7zp8XsDXtXkQd7LiHvCLpHkm/Kd1Q1BJNNoWDxlFJISzDY= X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-03-04_08,2026-03-04_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 clxscore=1015 suspectscore=0 spamscore=0 lowpriorityscore=0 adultscore=0 mlxlogscore=999 phishscore=0 bulkscore=0 malwarescore=0 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2603040194 X-JNJ: AAAAAAABPvL+cwBGvvJ11CAk6xJLWr0zuox7KiEIptsY3IrN3KcDEM/8ycXVPcFerFrz+HgSZxo9q10xkMgi9kGfkuaQMH5BtKCyvwpy6CTqHYjLkir5bFImIsw46x0wqaQ2jSfoChX9d1+lpDgvOGHNYw6+Er47skhSB4k/ScyeGXEnxUAWzjDYNetKQDEuM6D15v51rt7VGL+gaHlFp5TxdTzqtT4GOg5q7wM6VKS2JYjJbT5/E4UjAekDFQh+TOpIrLPwzbxmbG4l7bOLiGQ4mGjwDAMfN9VgNckwnRxBa7n6VVJELR+W50es/6uYFwEfcmiPZMsO7zB6hAsEZo7hb1s8AH/AKY6aTIukMMdzgVBJDhjcud1hwm166j18z3ksVMnX6ukVQrLaiqGZoMYmGltNxuS9jeckn2F1ty1B1T4dAWRClHCj4Xo36Bx1/1V45ubkRIpZDfYHCJ9DSkKhQEBSSX9j4eFws4DF7/8M/Jiqns/CHGKexESCgGN+3fPLnLEtBq2xffCn0CLGsouJ9BMAxsIgWIAMWqzXPbZr+20Urlw/rIsqY19kVWHg4fWFx0jjU0apxHQUb3m6q+m3L/5ItMGslC1yezAwGa2wXTk49Htt6iijbjJry/5zhpV0kCOQX11DEUYbrhfuGzlmAVEk9C7o3/hlol2kVkX3 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multipart message in MIME format. ------=_NextPart_000_00FA_01DCABEC.3E6A7CF0 Content-Type: multipart/alternative; boundary="----=_NextPart_001_00FB_01DCABEC.3E6A7CF0" ------=_NextPart_001_00FB_01DCABEC.3E6A7CF0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Attached is a new patch to demonstrate the bug in make_new_segment. It's not meant as a permanent new test (it's using some hard-coded assumptions about the x64 platform) -- just useful in validating the bug and fix. From: paul.bunn@icloud.com Sent: Wednesday, March 4, 2026 12:01 AM To: pgsql-hackers@postgresql.org Subject: [BUG + PATCH] DSA pagemap out-of-bounds in make_new_segment odd-sized path Hi hackers, Sorry for the previously poorly-formatted/threaded email. We've identified a bug in the DSA (Dynamic Shared Memory Area) allocator that causes memory corruption and crashes during parallel hash joins with large data sets. The bug has been present since the DSA implementation was introduced and affects all supported branches. Attached is a minimal fix (5 lines added, 0 changed). == Bug Summary == In make_new_segment() (src/backend/utils/mmgr/dsa.c), there are two paths for computing segment layout: Path 1 (geometric): knows total_size upfront, computes total_pages = total_size / FPM_PAGE_SIZE pagemap entries = total_pages <-- correct Path 2 (odd-sized, when requested > geometric): works forward from usable_pages = requested_pages pagemap entries = usable_pages <-- BUG The pagemap is indexed by absolute page number. The FreePageManager hands out pages with indices from metadata_pages to (metadata_pages + usable_pages - 1). Since metadata_pages >= 1, page indices at the high end of the range exceed usable_pages, making the pagemap accesses out-of-bounds. == How It Was Found on Postgres 15 == Multiple parallel worker backends crashed simultaneously with SIGSEGV in dsa_get_address(), called from dsa_free() in ExecHashTableDetachBatch() during parallel hash join batch cleanup. Stack trace: #0 dsa_get_address (area, dp) at dsa.c:955 #1 dsa_free (area, dp) at dsa.c:839 #2 ExecHashTableDetachBatch (hashtable) at nodeHash.c:3189 #3 ExecParallelHashJoinNewBatch (hjstate) at nodeHashjoin.c:1157 All crashing workers computed the same pageno (196993), which was the last valid FPM page but beyond the pagemap array (usable_pages = 196609). == Root Cause (from core dump analysis) == The crashing segment had: usable_pages = 196,609 (pagemap has this many entries) metadata_pages = 385 total_pages = 196,994 (= metadata_pages + usable_pages) last FPM page = 196,993 (= metadata_pages + usable_pages - 1) pagemap valid indices: 0 .. 196,608 FPM page indices: 385 .. 196,993 Pages 196,609 through 196,993 (385 pages) have no valid pagemap entry. The pagemap array ends 3,072 bytes before the data area starts (padding zone). Most out-of-bounds entries fall in this padding and cause silent corruption. The last 17 entries fall in the data area itself, causing bidirectional corruption: pagemap writes destroy allocated object data, and subsequent pagemap reads return garbage, crashing dsa_free(). == The Fix == After computing metadata_bytes with usable_pages pagemap entries, add entries for the metadata pages themselves: metadata_bytes += ((metadata_bytes / (FPM_PAGE_SIZE - sizeof(dsa_pointer))) + 1) * sizeof(dsa_pointer); The divisor (FPM_PAGE_SIZE - sizeof(dsa_pointer)) = 4088 accounts for the circular dependency: each metadata page costs one pagemap entry (8 bytes), so only 4088 of each 4096-byte metadata page is net-free for other pagemap entries. The +1 absorbs the ceiling. == Repro == It's tricky to create a test that causes a failure, or standalone SQL that will cause the bug to manifest. This is a (long) dormant bug that only causes a crash in rare circumstances. -- Thanks, Paul ------=_NextPart_001_00FB_01DCABEC.3E6A7CF0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Attached is a new = patch to demonstrate the bug in = make_new_segment.

It’s not meant as a permanent new test = (it’s using some hard-coded assumptions about the x64 platform) -- = just useful in validating the bug and fix.

 

 

From: paul.bunn@icloud.com = <paul.bunn@icloud.com>
Sent: Wednesday, March 4, 2026 = 12:01 AM
To: pgsql-hackers@postgresql.org
Subject: = [BUG + PATCH] DSA pagemap out-of-bounds in make_new_segment odd-sized = path

 

Hi hackers,

 

Sorry for the = previously poorly-formatted/threaded email.

 

We've identified a = bug in the DSA (Dynamic Shared Memory Area) = allocator

that causes memory corruption and crashes = during parallel hash joins with

large data = sets.  The bug has been present since the DSA = implementation

was introduced and affects all supported = branches.

 

Attached is a minimal = fix (5 lines added, 0 changed).

 

=3D=3D Bug Summary = =3D=3D

 

In make_new_segment() = (src/backend/utils/mmgr/dsa.c), there are two = paths

for computing segment = layout:

 

  Path 1 = (geometric): knows total_size upfront, computes

    = total_pages =3D total_size / FPM_PAGE_SIZE

    = pagemap entries =3D = total_pages          &n= bsp;       <-- = correct

 

  Path 2 = (odd-sized, when requested > geometric): works forward = from

    usable_pages =3D = requested_pages

    pagemap entries =3D = usable_pages          &= nbsp;      <-- BUG

 

The pagemap is = indexed by absolute page number.  The FreePageManager = hands

out pages with indices from metadata_pages to = (metadata_pages +

usable_pages - 1).  Since metadata_pages = >=3D 1, page indices at the high

end of the range = exceed usable_pages, making the pagemap accesses

out-of-bounds.

 

=3D=3D How It Was = Found on Postgres 15 =3D=3D

 

Multiple parallel = worker backends crashed simultaneously with SIGSEGV = in

dsa_get_address(), called from dsa_free() in = ExecHashTableDetachBatch()

during parallel hash = join batch cleanup.

 

Stack = trace:

  #0 dsa_get_address (area, dp) at = dsa.c:955

  #1 dsa_free (area, dp) at = dsa.c:839

  #2 ExecHashTableDetachBatch = (hashtable) at nodeHash.c:3189

  #3 = ExecParallelHashJoinNewBatch (hjstate) at = nodeHashjoin.c:1157

 

All crashing workers = computed the same pageno (196993), which was the = last

valid FPM page but beyond the pagemap array = (usable_pages =3D 196609).

 

=3D=3D Root Cause = (from core dump analysis) =3D=3D

 

The crashing segment = had:

  usable_pages   =3D = 196,609   (pagemap has this many = entries)

  metadata_pages =3D = 385

  total_pages    =3D = 196,994   (=3D metadata_pages + = usable_pages)

  last FPM page  =3D = 196,993   (=3D metadata_pages + usable_pages - = 1)

 

  pagemap valid = indices: 0 .. 196,608

  FPM page = indices:      385 .. = 196,993

 

Pages 196,609 through = 196,993 (385 pages) have no valid pagemap entry.

 

The pagemap array = ends 3,072 bytes before the data area starts = (padding

zone).  Most out-of-bounds entries fall = in this padding and cause silent

corruption.  The = last 17 entries fall in the data area itself, = causing

bidirectional corruption: pagemap writes = destroy allocated object data,

and subsequent = pagemap reads return garbage, crashing = dsa_free().

 

=3D=3D The Fix = =3D=3D

 

After computing = metadata_bytes with usable_pages pagemap entries, = add

entries for the metadata pages = themselves:

 

  metadata_bytes = +=3D

      = ((metadata_bytes / (FPM_PAGE_SIZE - sizeof(dsa_pointer))) + 1) = *

      = sizeof(dsa_pointer);

 

The divisor = (FPM_PAGE_SIZE - sizeof(dsa_pointer)) =3D 4088 accounts = for

the circular dependency: each metadata page = costs one pagemap entry

(8 bytes), so only 4088 of each 4096-byte = metadata page is net-free for

other pagemap = entries.  The +1 absorbs the ceiling.

 

 

=3D=3D Repro = =3D=3D

It's tricky to create a test that causes a = failure, or standalone SQL that will cause

the bug to manifest. = This is a (long) dormant bug that only causes a crash in = rare

circumstances.

 

--

Thanks,

Paul

------=_NextPart_001_00FB_01DCABEC.3E6A7CF0-- ------=_NextPart_000_00FA_01DCABEC.3E6A7CF0 Content-Type: application/octet-stream; name="test_dsa_pagemap_overflow.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="test_dsa_pagemap_overflow.patch"