From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796298155; b=rM/nzqR/76hqc6oOdxYKla7A1Qj6Op7q/YYGjkuaFpEhMUt6njjUtfOmj04U5oV4//KI S0HEB3oH2naQK0E4bQ7S4TGZL3JildWAh5u4PrQ5XtK0TjQ/cv1KZLpyLsNPWhSDgo1cN 0PWuFWnomDN5BnTlYx2cON4Lyfd9dbvZxzQRruoJlsEl1n2hAM5O10PAjSnN2W+3YvSRv ovlR/1vmIvdWZozUd/uUMnkZdsYdDl8sbNEVehzSqMO7uJqkkPYMojVNkH8B9/riAlY9g Jzc3g3b/UBx+v9BG+UoqXRgoJnoppXSP+DU7/8pIvhxMbuxhSJsM2tMZwHfhJVwqQDA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796298155; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=vHwjfvsYxO6LrG+p1d+nc0iDGYiqFBQEcu685eufZ4E=; b=FBt3c6ekEIc72ZidmrG9NbeLspuBLSn91nzUcI00CZ1aukpj0znDd7E51CW/mjjPO5Xh xy3UO3Ynui9ZlYyk6HWV67eHwmmAoNcQlYNWWzyABqvfkJ69q8wi38BH+PEKOL//gRV93 zMZ6WSF76gsM+vrEEwya0QKYfqZwV42rB9ATfz8Icm0dD+VZjOqs5HjK5t/XkE6RmuoMH 09bsc/LS/V85Q3bA+GVQ8uArrj/Su8T6N1y44QIs1sjNAtPO5vnm8veFDSAkAl0KEtGfx KntFzlDB1WxMz45JrO4N5x5C54U5VF4rVQHBpuJD8omV1lYSDUdubT7vZLJ8v77MiEw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TBPsd+Vv; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DFEEA606A322; Thu, 3 Dec 2026 11:42:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TBPsd+Vv Received: by smtp.kernel.org (Postfix) with ESMTPSA id DDE6E1F00896; Thu, 3 Dec 2026 11:42:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796298154; bh=vHwjfvsYxO6LrG+p1d+nc0iDGYiqFBQEcu685eufZ4E=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=TBPsd+Vva24jZg31rMcos/gjAFidNbQy4hgfAbzqG1Is4j9N7FBw17bC8rtgmyL/G Jm5EXumU8w6ZJBK1ankA3Nx/vkpbFxBF3zClvBjgIn2IUqkRFSi6Qz8BSiNtsst63n +MkWKz5V0NeJFORKAuBTZrzkCGvWR33RzCcljSLAhaUr1VpO5eLf6acRQlTe4VOfLT 955iy7g8qK+Me1DPOsMpm0ZLT0xuKe6WEmTZdIi/BfhmmxTvYsJL4HCfs2p72SnUpb iVb8ZxJv4sXsx3KR4sqSZpdjobikI16jDwVhqFEl/Ai2769GTywR5BOSg2u4aAsu2+ c6TK3ph8rozOw== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:42 +0100 Subject: [PATCH v2 19/35] hugetlbfs: recheck the vma range after the lock wait X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-hugetlb_fixes_2-v2-19-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> In-Reply-To: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> To: Muchun Song , Oscar Salvador , Andrew Morton , Hongfu Li , Jinmeng Zhou , Mina Almasry , Davidlohr Bueso , Naoya Horiguchi , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Jonathan Corbet , Shuah Khan , Randy Dunlap , Vivek Kasireddy , Mel Gorman , Hugh Dickins , Baolin Wang , Dave Airlie , Jason Gunthorpe , Andy Whitcroft , Roman Gushchin , Barry Song , Andrea Arcangeli , Lorenzo Stoakes , Suren Baghdasaryan , Shakeel Butt , Pedro Falcato , Jann Horn , Guillaume Morin , Yang Shi , James Houghton , Rik van Riel , Harry Yoo , Lance Yang , Axel Rasmussen , Peter Xu , Sasha Levin , Kiryl Shutsemau , Christian Brauner , "Steven Rostedt (Google)" , Hongbo Li , Gregory Price , Zi Yan , Joshua Hahn , Rakie Kim , Ying Huang , Matthew Brost , Byungchul Park , Alistair Popple , Andi Kleen , David Rientjes , Joern Engel , "Aneesh Kumar K.V" Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, Gerd Hoffmann , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Johannes Weiner , Chris Mason , Jiaqi Yan , stable@vger.kernel.org, "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3507; i=david@kernel.org; h=from:subject:message-id; bh=fsby/Jlwf4Bn8yO3DsIbp7+ZSMAF0X2gp8pZgwcjTEY=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQ6plYz9osC35mPHrfINufzpzaVeq0CQlKztuntuNO ZGcnakdpSwMYhwMsmKKLEHB9w1evlxXW+3Gxwwzh5UJZAgDF6cATERwCcMvZhahM3VNV71FDyh+ 93qXIqS1csnLE9L7ROV5JkZ+YK7bwMhw6tPifeG2fqUi7xe5vfDbwzgl7Lv37AdBe/LN4ye12rs zAQA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A From: Chris Mason A hole punch or a truncation of a hugetlb file that races with a split of a shared vma can lead to a use-after-free read of a page table, and to an MMU notifier range with end <= start, which trips a WARN_ON_ONCE() in KVM. hugetlb_unmap_file_folio() computes the address of the folio in a vma and walks the page tables to unmap the folio. As free_pgtables() unlinks a vma from the rmap tree before it frees its page tables, i_mmap_rwsem does not protect page tables outside of the vma, so walking them can result in a use-after-free read. The rmap tree walk guarantees that a found vma spans our folio. However, when we have to fall back to locking the vma lock first and drop the i_mmap_rwsem, we don't check whether that is still the case after we acquired the vma lock and the i_mmap_rwsem. The following is then possible: hugetlb_unmap_file_folio() __split_vma(), new_below == 0 ========================== ============================= hugetlb_vma_trylock_write() fails i_mmap_unlock_write() down_write(&vma_lock->rw_sema) vma_prepare() i_mmap_lock_write() vma->vm_end = addr vma_complete() i_mmap_unlock_write() i_mmap_lock_write() vma_lock->vma is not NULL vma_offset_start() >= vma->vm_end vma_offset_end() == vma->vm_end hugetlb_vma_maps_pfn(vma, v_start) unmap_hugepage_range(vma, v_start, v_end) Similarly, a split with new_below == 1, where the vma keeps the upper part and vm_start and vm_pgoff move up, or shrinking the vma with mremap() can leave the vma not covering our folio. Let's fix it by checking whether the possibly split vma still spans our folio. Add a VM_WARN_ON_ONCE() to highlight the problem that was previously missed. This has medium urgency, as this is triggerable by unprivileged users. Fixes: 40549ba8f8e0 ("hugetlb: use new vma_lock for pmd sharing synchronization") Cc: Signed-off-by: Chris Mason [ david: adjust comment and description ] Signed-off-by: David Hildenbrand (Arm) --- fs/hugetlbfs/inode.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 9b2c61339f830..db2af960ee7dc 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -439,10 +439,11 @@ static void hugetlb_unmap_file_folio(struct hstate *h, i_mmap_lock_write(mapping); vma = vma_lock->vma; - if (!vma) { + if (!vma || vma_start_pgoff(vma) >= end || vma_end_pgoff(vma) <= start) { /* - * If lock is no longer attached to vma, then just - * unlock, drop our reference and retry looking for + * If the lock is no longer attached to this vma, or if + * the vma changed and no longer covers the folio, then + * just unlock, drop our reference and retry looking for * other vmas. */ up_write(&vma_lock->rw_sema); @@ -456,6 +457,7 @@ static void hugetlb_unmap_file_folio(struct hstate *h, */ v_start = vma_offset_start(vma, start); v_end = vma_offset_end(vma, end); + VM_WARN_ON_ONCE(v_start >= v_end); if (hugetlb_vma_maps_pfn(vma, v_start, pfn)) unmap_hugepage_range(vma, v_start, v_end, NULL, ZAP_FLAG_DROP_MARKER); -- 2.56.1