From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ua1-f43.google.com (mail-ua1-f43.google.com [209.85.222.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7ABC034E75A for ; Fri, 13 Mar 2026 16:11:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773418284; cv=none; b=LURj4x/afCs5mV2fwFstpdDkAVQYqIQXNzf3CgQCJ0pO7wqC8ohW/cXe+inHOsYv3xwTje5oBUdTJTRlBw8BJggFX/RDZy6IGbbwthl/+GPrdREgmhPM0vAnFrzfUS3WmUPUOOLm9kCM/kM+r9r/74ZvCHbzTB0+bfkkmUyrzcE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773418284; c=relaxed/simple; bh=rq0laD/NPKt2Ap7DhnuKjx19WrRLlI7nxlxtq6oaCtg=; h=Date:From:To:Cc:Message-ID:In-Reply-To:References:Subject: MIME-Version:Content-Type; b=upLMCAS08EO8S6IDxqCdANuYdEDZdp/ev//AteGwE0Ugw2GqvAtUFGjsT9eKj89l65Vtlh4yFyvZU0U1uiSLNmVHw29mfuPJpg3UJkYB5jEIggKcDTNd+RZbgE0VSUDKi8A43djTnYdOv5XX1f2G7KjCXG/tbixf2/E3k/0IWDg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FwdiuSWL; arc=none smtp.client-ip=209.85.222.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FwdiuSWL" Received: by mail-ua1-f43.google.com with SMTP id a1e0cc1a2514c-94dd0f3c4b7so1391739241.1 for ; Fri, 13 Mar 2026 09:11:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773418282; x=1774023082; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:subject:references :in-reply-to:message-id:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=rq0laD/NPKt2Ap7DhnuKjx19WrRLlI7nxlxtq6oaCtg=; b=FwdiuSWL6JTOA3ymvh2bQ9nzxQvmQAuI5XerWie+5ya4UVq+KWpW8ZMLeSfnUDmI6+ sPhhChDVTLFTIqs58ed8R3wvOVY/yLU2tEQD0G2H0x8G2IBD1/RRi9g06NHV6JsyL/Uq VEih3rtrRj51t3r6q9tdvs4yTlTJyKM6EIfNKL9hK4fBV3CMrk6EL6i+QmJ+pNiRaCuq BaOGNct1ND3CNpzLs0YRSgRCEV4F4/F5MZI4QR/bpF2iciuk9a/107oAVqp+jMxmlwqj udY6mX42lKZm7LWvo1/zcDILJmDZeoEZiFsl1n+9uTt6Q9VIJsctICrBh5jyx9NGCPv2 Kzwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773418282; x=1774023082; h=content-transfer-encoding:mime-version:subject:references :in-reply-to:message-id:cc:to:from:date:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=rq0laD/NPKt2Ap7DhnuKjx19WrRLlI7nxlxtq6oaCtg=; b=mn+r8R810X4LufKrighMK7ceW+rYd35Hw1mRhY4yiYFym4O6fMWSBsZ5HAnpiZ7pAT 59C9sd6zR7/rdcTqLdzkRBvwt/eVLAX9FEE5+9CqoTbE2KJVYUUHviSo14KdfnGwD1OJ boUob/IOW4MHRNPqvxZqx8dGvNNdOems2LDiDBFSR96YJD+5jkSXqZfMwt4vVcBNcfPd XVP0jNF/WcOkMxwqeBmatJkczH7i11Opb2VkBM3pTuHd8Q6HJ7tg1aHVHbqtEJETGdew aWUPAw1/XJAjKBCfd7GGj5FoAGLa3g+vDRgLnuHW/tzpRn+yUvxbSoiElEslZhbMw6DC IVSQ== X-Forwarded-Encrypted: i=1; AJvYcCUTm/XLSqwXpDS9LBc4m1A4w3kGU5LNvwa7lRKNXgm+pabKY3/ZrWfFPxN+ftAx6HI4SIgGv/uPCgkDgDQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzcyV5FUjC1PDPlkgWnvyiC5dm4j33xTdueE110VvucIBzb2CBs NoDQUGoiLqW+8xoEIgoiWTPEkfo+JVYUL8kv/SAjxEwYfMBJ4o5LEBbd X-Gm-Gg: ATEYQzy7JLMmLAxIl3JXc11Pz8DMM1gSJNs7NFCEiCOuQ+jdoIUWAXwdxJBqVmWXszj 4oMRi17qFlQOXV+gw5Lir6ydoOZw1tL98o5WEMcm5Rlat4zL8qMqF9cml/Z36+RktADFd1Ere/J fo4udLYqDFRljglSP9A53GUx97G8k4a9QLPk7Ff4T+jBltz3sYUXw3fmQaJMXmkcJustvgdd9qC 4LPPlWTmc2lONo4Qyh6D218xEgVWaXhUsbmJZqZtgO0knJOyYfketGBZvRI0lAYadGuK62WEoRQ vbutb2IQbCW+ZrzxFUveRp+7ex+PXP69pBhGWXXTZN0YKvt94VbmnvukcdLHT/oE7+AE/FY02pA x5iFCepB9/tDqwb/h4ciW4SOrUNBmL7A737WI4I8qcpJEqSc83vJ6qnqiyjPalu56aaclY9V6XU sQsU7cG+zaAEv4EWxY X-Received: by 2002:a05:6102:c0a:b0:5ff:ae5c:c669 with SMTP id ada2fe7eead31-6020e653695mr1859129137.37.1773418282237; Fri, 13 Mar 2026 09:11:22 -0700 (PDT) Received: from [127.0.0.1] ([86.1.69.5]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-94ecfedb472sm2756412241.11.2026.03.13.09.11.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 13 Mar 2026 09:11:21 -0700 (PDT) Date: Fri, 13 Mar 2026 16:11:14 +0000 From: Josh Law To: Pedro Falcato Cc: Andrew Morton , "Liam R . Howlett" , Alice Ryhl , Andrew Ballance , Josh Law , maple-tree@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Message-ID: In-Reply-To: References: <20260312184054.23481-1-objecting@objecting.org> <20260312134531.49c1f9171b4b0bc8352e678d@linux-foundation.org> Subject: Re: [PATCH 1/3] lib/maple_tree: fix potential NULL dereference in mas_pop_node() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Correlation-ID: 13 Mar 2026 09:05:37 Pedro Falcato : > On Fri, Mar 13, 2026 at 07:17:17AM +0000, Josh Law wrote: >> 12 Mar 2026 23:22:48 Pedro Falcato : >> >>> On Thu, Mar 12, 2026 at 01:45:31PM -0700, Andrew Morton wrote: >>>> On Thu, 12 Mar 2026 18:40:53 +0000 Josh Law wr= ote: >>>> >>>>> If kmem_cache_alloc_from_sheaf() returns NULL (possible under >>>>> GFP_NOWAIT pressure), mas_pop_node() falls through to the out label >>>>> and dereferences the NULL pointer in memset(ret, 0, sizeof(*ret)). >>>> >>>> This is such a glaring bug that I wonder if we're missing something. >>> >>> According to my local copy of lib/maple_tree.c: >>> >>> mas_pop_node() - Get a previously allocated maple node from the maple s= tate. >>> >>> Note the "previously" :) kmem_cache_alloc_from_sheaf() can only fail if= you >>> run out of objects in the sheaf. >>> >>> So yeah, this "bug" looks bogus. >>> >>> -- >>> Pedro >> >> Hi Pedro, >> >> I see the comment regarding 'previously allocated' nodes. However, >> mas_pop_node() explicitly calls kmem_cache_alloc_from_sheaf() with >> GFP_NOWAIT. If there is any path=E2=80=94even an unexpected one=E2=80=94= where the >> sheaf is exhausted or the allocator fails, the code immediately >> performs a memset on the NULL pointer. > > And? This does not happen, simply. If it does, your maple tree is hosed > and, really, you're not recovering from it. > >> >> Even if this is a 'should never happen' scenario, returning NULL is >> safer than a kernel panic. As Andrew noted, the current structure >> allows a fall-through directly into a dereference. My patch ensures >> we handle that edge case safely. > > ... and now because none of the mas_pop_node() callers ever checks for NU= LL > (why would they, they preallocated those same nodes before), you safely > dereference NULL away from mas_pop_node!. > > -- > Pedro Hi Pedro, I see your point regarding the invariants of the sheaf. If the pre-allocation logic is guaranteed, then a NULL return here implies a fundamental corruption of the maple state. My concern was primarily the 'fall-through' structure which makes the dereference look accidental rather than intentional. However, if the callers aren't prepared to handle a NULL return anyway, simply returning NULL doesn't solve the underlying panic. I'll take this as a lesson in understanding function invariants before jumping to defensive checks. Thanks for the technical explanation. V/R Josh law