From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 CD994368974 for ; Tue, 7 Jul 2026 23:51:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783468306; cv=none; b=KlI0bnmMS4pvN+d+1hOip6e8qPWu4c+1IwWqfnrptUP0DXzwoWtlzSfcyY7zaDzaR2bjyIkGc6sF9IXyWhLtNVXQOMoSA9N7rZq2sgOB7/UBHIhiqFeHwbF8Jcoqrn/muaILOrGFo7vnO+zS6R0pNOEQ8L+nGnlSZTZJxeogVyU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783468306; c=relaxed/simple; bh=3XeJNhyNCCZ8W/lj62dDXOTY0g/DW/CeBx26KLVYXaw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=HCDwLXfnS3sc5rHiUdhtA2mFjX022XL/wWKc+OUVEHnAhHtK758h1HdgMtqghJ/FzljxNvBEUTnvDKzkNIoq2FSpxiJdP+PNidj7YZGe9VkEuUT9/pGqXsrVnuyJV1phD8xmO1zCyUXcIpFoCTLDZ7aVt+46R6fHD6FGdKTAJS8= 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=QDYL6wVw; arc=none smtp.client-ip=209.85.214.179 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="QDYL6wVw" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2ca70925c25so1703585ad.1 for ; Tue, 07 Jul 2026 16:51:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783468304; x=1784073104; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=xg1plK6yIX0LTs5KrUD1h414rIIh4WSWs4+c6LLldXs=; b=QDYL6wVwzIYZv6Q13lp+m0930AakBJskohizvcdi+d8s0paZTMLyoU7xpJmNB8WNo/ TgoZHe5fllerCugLWjpjZlPJXnE1Ptn42rPJz3xmcQVYT/5cfzwZNmOQwpTDPPdp4SYY jImNRriFSIaYzQYY9nMroSEWcIsR3YwUHYmuWwMkGijPcUo0pUc6vnyuRcplfNIM+pRt ijiTEdaEtwZLDTsv7UY4KbZoNUltDC4ZtCjU+oOfB7BeEdu0X6IPw1YnWpjzpyCiLGwv n7f+aa/6px6eVBLp69Opif8CXMw5liYrSl9ozPT8xQyd2jYJXFjLiZ6EPCk6R3VKuryh 0OFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783468304; x=1784073104; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=xg1plK6yIX0LTs5KrUD1h414rIIh4WSWs4+c6LLldXs=; b=MRVYbbojZZkIIYmrJhU99xNNjt9SEEsluonB2X7hBWFDy4fhGD9/PFQ7ohp6QeUUo5 iVR/kANAp7HvJFhL/yetxH1iClUGpdamwcwm0h9xOZH3Pcve/7DZiR7QY5TP95KBFSK9 o8EJ24mryx49RG3YoI4oYm0eRdCBC//5Kot2f5ZlF7Pfkvcw6T2pv1xVL/m99Xy85jss UTLOKihAKb3zHnoZqqnBsbF2t2bb5hDdv8I6taOMhW1Z2damjch9CNgC5bp1utJNMj37 jZhEA9VjlG0+2PIt1OTVurJV8iCWWoUMao9wIJhjud3dc1ACjUk9mRHCKp4AvxCOAbcZ CMGw== X-Gm-Message-State: AOJu0YwS0UuAOO2TX0PJcWJmCFi8I73vjqd/j5LlIEoCnTnnv8+3OThE dpBzYH0DIlRbmLO6fJ4FuZqrm51QF6J6ChUa4TdOaXu/G4DvtrotQrsf X-Gm-Gg: AfdE7cm+tD0RyS9KVoGQpBUjWP9+0Jc/sGDd+zODsOKQM1R/waKApceOYI7C1FrcJ6i VHIOhwaQUTbgT1b06IuRSKExgLdzuxlfawO0soC6nY9KGYaEc6BShxkRtH2/iF5mh/cCJoChXCV VUwGW1wfMfYEq5wo02/aqQzhT3pevIZFTes56X2br/MTbAsW7ZFL2vs3cqmepYceOjifec0kcFC 6SQXoHKlSvXQtN2uWDPrHmBN1r8+dsRqXSigmlswAlbvl3xDO8aapyxSFdWbzOXIJEL6cw51nWX RM51XxmS55jUEC0BbiQPQUwFXMxx026G8ZCR5PNKg6XrL/y5qB2Nr8JDO7A6E5w+hXS4jfJtP4A UhmQt5YwgsVT/qx7qehETqiMGnBh/GG4ZFk6NZ87tZmamP8PNcUDNtcPf1s+fLHkyAL/izhW6Kk qtZIkSrBqHTdX6jiVjIyxjtUU2gcxqis606ZuwMrw31RhlE/CzLvdIlq1A1Pczop0QQmXUAdtco qfYgymNVQ6TDgmZNw== X-Received: by 2002:a17:902:e84c:b0:2cb:3f63:7c3e with SMTP id d9443c01a7336-2ccea36aec5mr1391525ad.35.1783468304300; Tue, 07 Jul 2026 16:51:44 -0700 (PDT) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ccc9bdb9a3sm18882265ad.13.2026.07.07.16.51.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jul 2026 16:51:43 -0700 (PDT) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id A319E148F31C; Wed, 8 Jul 2026 08:51:42 +0900 (JST) From: Hajime Tazaki To: akpm@linux-foundation.org, liam@infradead.org, ljs@kernel.org, vbabka@kernel.org, jannh@google.com, pfalcato@suse.de, linux-mm@kvack.org, geert@linux-m68k.org Cc: linux-kernel@vger.kernel.org, Hajime Tazaki Subject: [PATCH 1/2] mm: nommu: fix the error path when vma_iter_prealloc() fails Date: Wed, 8 Jul 2026 08:51:36 +0900 Message-ID: <20260707235137.498738-1-thehajime@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit When vma_iter_prealloc() fails in do_mmap(), it jumps to error_just_free without updating ret to -ENOMEM, meaning do_mmap() will return 0 on failure. Additionally, this error path unconditionally frees the region struct. Since the region was already added to the global nommu_region_tree via add_nommu_region(), leaving it makes a potential dangling pointer in the tree and may cause a use-after-free on the next tree walk. This commit fixes those issues by introducing new jump label, error_vma_iter_prealloc, to correctly handle the error case of vma_iter_prealloc(), and move the region updates after the place that the allocation is finished. Those issues are discovered by Sashiko, linked below. Link: https://sashiko.dev/#/patchset/20260702012830.667205-1-thehajime%40gmail.com Signed-off-by: Hajime Tazaki --- mm/nommu.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mm/nommu.c b/mm/nommu.c index 815ebefddfae..ad619611e83f 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -1181,7 +1181,6 @@ unsigned long do_mmap(struct file *file, ret = do_mmap_private(vma, region, len, capabilities); if (ret < 0) goto error_just_free; - add_nommu_region(region); /* clear anonymous mappings that don't ask for uninitialized data */ if (!vma->vm_file && @@ -1199,7 +1198,9 @@ unsigned long do_mmap(struct file *file, BUG_ON(!vma->vm_region); vma_iter_config(&vmi, vma->vm_start, vma->vm_end); if (vma_iter_prealloc(&vmi, vma)) - goto error_just_free; + goto error_vma_iter_prealloc; + + add_nommu_region(region); setup_vma_to_mm(vma, current->mm); current->mm->map_count++; @@ -1235,6 +1236,12 @@ unsigned long do_mmap(struct file *file, ret = -EINVAL; goto error; +error_vma_iter_prealloc: + up_write(&nommu_region_sem); + pr_warn("Failed vma_iter_prealloc()\n"); + ret = -ENOMEM; + goto error; + error_getting_vma: kmem_cache_free(vm_region_jar, region); pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n", -- 2.43.0