From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 EDE91402BA0 for ; Wed, 8 Jul 2026 08:38:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783499918; cv=none; b=gA3YpY2Hr+8QYOMWl3Vgsc0S3O92fpB+uMsBsTLoeLSzCcFtmA99h9r3+fAkolKGx9r44duzYamwOPtpeEoMJS+3PRooxgQi9FpPs6vuCSF/VdOjOT64frwTuuJ7U4JY9FRh55DZXr3IKvQOrmKL+gJuvSGeFPE5iSUXK0SiCnk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783499918; c=relaxed/simple; bh=GpmS8rjrOo1X2v1AuEtdDRy59qpIOtc/mxRGbTRGgbA=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=haCxOWHHDsR66n1t4/hpXDHxskhAy+KaXb8Kp6WIT4Tmzu1+r1kcWM1Cg0t9RRPdyuGGqgnL+3ngdRtoZasPSRUwXa0Qbsdd5JsqzjVSKnUm+d0TKkdCAI6jUsnd86cEbhUMpWuPO3RXngtQNbpa5D92kah4FTMaFqTV+d5gJgs= 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=pIjvROxj; arc=none smtp.client-ip=209.85.210.180 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="pIjvROxj" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-847aa193d98so329249b3a.3 for ; Wed, 08 Jul 2026 01:38:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783499912; x=1784104712; 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=YpkkoaSl0O2AaMEcTuxDfNe4rT/5zh+FKCaNy9UkAKw=; b=pIjvROxjCbknj8MtGtrQKMS+jRWt39O5MTFJsU8D6iH2qDksVI6BuaylfqxAELvJ5O CBjKvsq5cJoMogkTgx+rKEye/YX0UvghszI5YBWgh/oPKPskk3/Ws+kmg+J8+1mYrEFu 4J3fDpw+KecDjunNeZVuj+S3N6xy1Dl/5xPlQdw4nJRpSFaVQe9X2nIm7Cw8UPyv805D i8LJw44tu+xQv+ONlwJyRTbDMnNjLTnwKfNW91Js4Pj3JLi+D9Nblaqx1Gg9A8lPsR1p 2bK0loC6bWSMvM01dUFtpoFsr6CCsADNhkK1R4AHcUswJCH5d7gAWV9nx2cPFgZlymsD Ic6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783499912; x=1784104712; 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=YpkkoaSl0O2AaMEcTuxDfNe4rT/5zh+FKCaNy9UkAKw=; b=RG54zFzFl8kPkR5GZWSCAeAl5Drvyb/PJbmclgup6UIR7bv05QT/oFhHKKhOWfINqG VRq4F8nQBfbCPVAcCblplMKt1LRqdHYoZRwsY+Ql3vstvYTjViro/becQmd5oMAYRH40 Gad5fK4BUTykHSPUMHNpETzGGpZNRFN53h+KECNsYfeGdU2U9bZ84uV01tEUfA6vQUCp wLAc/OfYVVQegby/PLIcgVSvSa1uXLpmGgkbu8dfX3Tw4PUtEcVD4N9PYmJM5rnpdENq GhomKg2Gv4hW7CXa1hLT0Sv+6U89x6fMqLoHlip+OPsjsQzPS8VR5P//U1rl+3FjcmpE hSow== X-Gm-Message-State: AOJu0Yz2AuiEsViyu38L2Ro9YO6zrsIdm59h2FzSkvE415sWfwagnpB/ eVKWCqY0FEGz7vNpxv0J7hanf1V4kheyVhOCHy8VRjopNunBUWMTPAEZTKmBS3MG X-Gm-Gg: AfdE7cl13RptbgOeuZ8qxgMa0zee20Hro6v3g0daom/El00FQrlt1BXkf6DISMBmcMK 30MirRMvbsDwssgZd4oA2Bnt6tIAXOgqV254QUSwQttu1PF/EP086iClRNk8PSyLkiOYrGV9q4J MMcSotEyKr07jDKvvi4ptijnmFQusuPMeaPa5QGtSll2uBMmNXxtmqxhy4qKEMQ60+crwneWqm4 qXmyxeDBrXzr0dMbIeMSTM0ieiEbEtMeMhLrhSZGiMurAzWsh7nVk5ei+UjCWB1qYvKLlUT7WGo qnEFvaJH7o8SKhUDBTP08PjeOneHc42U9QdhV1L+CKvvTjNklQ5IHaJgm+QEX8V1tz9qNvdji9D FTeKJrdeIQ1C1z+7thkukukO3V+F8bKO0z8yRloy+FDMFPoG5jDjqLSzWOKzZEi/kpl3RIGFDbd hRE7AhDsTzEHu0jxYmtFFvdcv4E5cRT2dt7ScPN8TanwtqOQ6hSoDxsRGzTj1GzRPZDWiP3QTsk aPWnorXBdqposwP3cDsdnAvyrxn X-Received: by 2002:a05:6a00:1396:b0:845:39dd:f773 with SMTP id d2e1a72fcca58-84842fdf963mr1745490b3a.7.1783499912247; Wed, 08 Jul 2026 01:38:32 -0700 (PDT) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-847f6d4dbdesm6672431b3a.31.2026.07.08.01.38.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jul 2026 01:38:31 -0700 (PDT) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id E30A714914F8; Wed, 8 Jul 2026 17:38:30 +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 v3] mm: nommu: fix the error path when vma_iter_prealloc() fails Date: Wed, 8 Jul 2026 17:38:29 +0900 Message-ID: <20260708083829.576036-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 as a error path of this function, but there are several possible issues. 1) It jumps to error_just_free without updating ret to -ENOMEM, meaning do_mmap() will return 0 on failure. 2) The 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. 3) If do_mmap() finds an existing overlapping shared region, it increments its usage, sets region to this existing pregion, and jumps to share: region = pregion; result = start; goto share; When vma_iter_prealloc() fails and jumps to error_just_free, the error path unconditionally frees the region: error: ... if (region->vm_file) fput(region->vm_file); kmem_cache_free(vm_region_jar, region); This potentially leaves a dangling pointer in nommu_region_tree and causes RB-tree corruption. 4) When establishing a new private mapping, do_mmap_private() allocates physical pages and assigns them to region->vm_start: base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL); ... region->vm_start = (unsigned long) base; If we later fail at vma_iter_prealloc() and jump to error_just_free, the region struct is freed, but the backing physical memory isn't freed via free_page_series(). 5) In the error label of do_mmap(), the vm_area_struct allocated is freed by vm_area_free(vma) but not called after vma_close(), leaving potential memory leak which should be handled by a custom .close handler of vm_ops. This commit fixes those issues by introducing new jump label, error_vma_iter_prealloc, to correctly handle the error case of vma_iter_prealloc(), updating ret value (1), and move the region updates after the place that the allocation is finished (2). Additionally, the commit removes the existing goto label, error, and consolidates to error_just_free as existing `goto error;` code blocks always release nommu_region_sem. Moreover, it only frees region allocated in this request to avoid freeing the shared, existing region shared by other processes (3), and free physical memory when do_mmap_private() allocates (4). It also add vma_close() before vm_area_free() to fix the potential leak (5). Those issues are discovered by Sashiko, linked below. Link: https://sashiko.dev/#/patchset/20260702012830.667205-1-thehajime%40gmail.com Link: https://sashiko.dev/#/patchset/c8513ee5aa8444ec9bf6c276043c9f833016a2fa.1783304131.git.thehajime%40gmail.com Link: https://sashiko.dev/#/patchset/20260707235137.498738-1-thehajime%40gmail.com Fixes: b5df09226450 ("mm: set up vma iterator for vma_iter_prealloc() calls") Signed-off-by: Hajime Tazaki -- v3: address another sashiko review. https://sashiko.dev/#/patchset/20260708052820.556085-1-thehajime%40gmail.com v2: consolidate commits into a single patch, address additional Sashiko review (sorry for the noise: i have no working local environment of sashiko/gemini) https://lore.kernel.org/linux-mm/20260708052820.556085-1-thehajime@gmail.com/ v1: https://lore.kernel.org/linux-mm/20260707235137.498738-1-thehajime@gmail.com/ --- mm/nommu.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/mm/nommu.c b/mm/nommu.c index 815ebefddfae..852ec9bd0505 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++; @@ -1218,22 +1219,41 @@ unsigned long do_mmap(struct file *file, return result; error_just_free: + vma_close(vma); + /* if the error was from shared mapping/existing region, don't free the region. + * this has to be before releasing semaphore. + */ + if (region->vm_usage == 1) { + if (region->vm_file) + fput(region->vm_file); + kmem_cache_free(vm_region_jar, region); + + } else + region->vm_usage--; + up_write(&nommu_region_sem); -error: vma_iter_free(&vmi); - if (region->vm_file) - fput(region->vm_file); - kmem_cache_free(vm_region_jar, region); + if (vma->vm_file) fput(vma->vm_file); vm_area_free(vma); return ret; sharing_violation: - up_write(&nommu_region_sem); pr_warn("Attempt to share mismatched mappings\n"); ret = -EINVAL; - goto error; + goto error_just_free; + +error_vma_iter_prealloc: + pr_warn("Allocation of vma iterator for process %d failed\n", current->pid); + show_mem(); + ret = -ENOMEM; + + /* in case that the region is allocated via do_mmap_private() */ + if ((region->vm_usage == 1) && (region->vm_flags & VM_MAPPED_COPY)) + free_page_series(region->vm_start, region->vm_top); + + goto error_just_free; error_getting_vma: kmem_cache_free(vm_region_jar, region); -- 2.43.0