Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 342A42D5A1A for ; Wed, 9 Jul 2025 10:44:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752057879; cv=none; b=rGmC0mAyuyVWgMQ66cdogasxaMMYyaIt+UC160tVKG8OSgTi1+p7bqyoqWS7qrIxqMmea1Gf3VfMPwg5rqFfVranzjkFlU9zzxxFSwjEYETZIegwZJ8MU+cUCd0GqGl27NkqMxXmRM2zA8N7kTskNxrULO2JZgdXUdQ6Waadngc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752057879; c=relaxed/simple; bh=5alkfSoqz/VOzhUm7yzGDlWm73EZxYHVi8B/hpsce+0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=hrxmNw3OIY205uq8aPFpVV+3TJYWhdn7nyYHAv9aNn6iLUPLz4XR0ziYX0hKmg4+N2C5DY+Vv2LJkyAndAE0pSg/wKLnbm9PNjAfdbTJOT5fzfFButms3waboZrrVZMrojvtKRosmKZ24KbN3zo+6Fft9T7RFglT5XS3noxGj0I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=p/Q5TfTQ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="p/Q5TfTQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6EE12C4CEEF; Wed, 9 Jul 2025 10:44:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1752057878; bh=5alkfSoqz/VOzhUm7yzGDlWm73EZxYHVi8B/hpsce+0=; h=From:To:Cc:Subject:Date:Reply-to:From; b=p/Q5TfTQX6Z7IyiKtUeOVPq7K3YEgMVvg1YMCBKd8HrDbKrXr4ll+W/+oPrDuFPPJ GSPBZaZhzFgEVZhTFEDXOmNPyq5F2O/A6cWzdt5cBZgS469PxuuvtcedTiUcyzxp4v 90L7bkS8tpsNMpz+8oHXx8wRFfJBV4pgSUcr/d8I= From: Greg Kroah-Hartman To: linux-cve-announce@vger.kernel.org Cc: Greg Kroah-Hartman Subject: CVE-2025-38242: mm: userfaultfd: fix race of userfaultfd_move and swap cache Date: Wed, 9 Jul 2025 12:42:34 +0200 Message-ID: <2025070933-CVE-2025-38242-8f85@gregkh> X-Mailer: git-send-email 2.50.0 Precedence: bulk X-Mailing-List: linux-cve-announce@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Reply-to: , X-Developer-Signature: v=1; a=openpgp-sha256; l=6639; i=gregkh@linuxfoundation.org; h=from:subject:message-id; bh=NhCz0eFxADrhDMS9k543MNqaS117PKVY+mjroGWlziE=; b=owGbwMvMwCRo6H6F97bub03G02pJDBl57jP7Y3qubY9buG7J711X21Wrt72ZeyvWj7t5a9tJI QmhA1d/dMSyMAgyMciKKbJ82cZzdH/FIUUvQ9vTMHNYmUCGMHBxCsBEwj4yzA9d2bK1/ruh7H7H Fhn1t55n3s2aJsiwYEp6T6RKU8USIbO6+ekPDFj3n3UWBAA= X-Developer-Key: i=gregkh@linuxfoundation.org; a=openpgp; fpr=F4B60CC5BF78C2214A313DCB3147D40DDB2DFB29 Content-Transfer-Encoding: 8bit From: Greg Kroah-Hartman Description =========== In the Linux kernel, the following vulnerability has been resolved: mm: userfaultfd: fix race of userfaultfd_move and swap cache This commit fixes two kinds of races, they may have different results: Barry reported a BUG_ON in commit c50f8e6053b0, we may see the same BUG_ON if the filemap lookup returned NULL and folio is added to swap cache after that. If another kind of race is triggered (folio changed after lookup) we may see RSS counter is corrupted: [ 406.893936] BUG: Bad rss-counter state mm:ffff0000c5a9ddc0 type:MM_ANONPAGES val:-1 [ 406.894071] BUG: Bad rss-counter state mm:ffff0000c5a9ddc0 type:MM_SHMEMPAGES val:1 Because the folio is being accounted to the wrong VMA. I'm not sure if there will be any data corruption though, seems no. The issues above are critical already. On seeing a swap entry PTE, userfaultfd_move does a lockless swap cache lookup, and tries to move the found folio to the faulting vma. Currently, it relies on checking the PTE value to ensure that the moved folio still belongs to the src swap entry and that no new folio has been added to the swap cache, which turns out to be unreliable. While working and reviewing the swap table series with Barry, following existing races are observed and reproduced [1]: In the example below, move_pages_pte is moving src_pte to dst_pte, where src_pte is a swap entry PTE holding swap entry S1, and S1 is not in the swap cache: CPU1 CPU2 userfaultfd_move move_pages_pte() entry = pte_to_swp_entry(orig_src_pte); // Here it got entry = S1 ... < interrupted> ... // folio A is a new allocated folio // and get installed into src_pte // src_pte now points to folio A, S1 // has swap count == 0, it can be freed // by folio_swap_swap or swap // allocator's reclaim. // folio B is a folio in another VMA. // S1 is freed, folio B can use it // for swap out with no problem. ... folio = filemap_get_folio(S1) // Got folio B here !!! ... < interrupted again> ... // Now S1 is free to be used again. // Now src_pte is a swap entry PTE // holding S1 again. folio_trylock(folio) move_swap_pte double_pt_lock is_pte_pages_stable // Check passed because src_pte == S1 folio_move_anon_rmap(...) // Moved invalid folio B here !!! The race window is very short and requires multiple collisions of multiple rare events, so it's very unlikely to happen, but with a deliberately constructed reproducer and increased time window, it can be reproduced easily. This can be fixed by checking if the folio returned by filemap is the valid swap cache folio after acquiring the folio lock. Another similar race is possible: filemap_get_folio may return NULL, but folio (A) could be swapped in and then swapped out again using the same swap entry after the lookup. In such a case, folio (A) may remain in the swap cache, so it must be moved too: CPU1 CPU2 userfaultfd_move move_pages_pte() entry = pte_to_swp_entry(orig_src_pte); // Here it got entry = S1, and S1 is not in swap cache folio = filemap_get_folio(S1) // Got NULL ... < interrupted again> ... move_swap_pte double_pt_lock is_pte_pages_stable // Check passed because src_pte == S1 folio_move_anon_rmap(...) // folio A is ignored !!! Fix this by checking the swap cache again after acquiring the src_pte lock. And to avoid the filemap overhead, we check swap_map directly [2]. The SWP_SYNCHRONOUS_IO path does make the problem more complex, but so far we don't need to worry about that, since folios can only be exposed to the swap cache in the swap out path, and this is covered in this patch by checking the swap cache again after acquiring the src_pte lock. Testing with a simple C program that allocates and moves several GB of memory did not show any observable performance change. The Linux kernel CVE team has assigned CVE-2025-38242 to this issue. Affected and fixed versions =========================== Issue introduced in 6.8 with commit adef440691bab824e39c1b17382322d195e1fab0 and fixed in 6.15.5 with commit db2ca8074955ca64187a4fb596dd290b9c446cd3 Issue introduced in 6.8 with commit adef440691bab824e39c1b17382322d195e1fab0 and fixed in 6.16-rc4 with commit 0ea148a799198518d8ebab63ddd0bb6114a103bc Please see https://www.kernel.org for a full list of currently supported kernel versions by the kernel community. Unaffected versions might change over time as fixes are backported to older supported kernel versions. The official CVE entry at https://cve.org/CVERecord/?id=CVE-2025-38242 will be updated if fixes are backported, please check that for the most up to date information about this issue. Affected files ============== The file(s) affected by this issue are: mm/userfaultfd.c Mitigation ========== The Linux kernel CVE team recommends that you update to the latest stable kernel version for this, and many other bugfixes. Individual changes are never tested alone, but rather are part of a larger kernel release. Cherry-picking individual commits is not recommended or supported by the Linux kernel community at all. If however, updating to the latest release is impossible, the individual changes to resolve this issue can be found at these commits: https://git.kernel.org/stable/c/db2ca8074955ca64187a4fb596dd290b9c446cd3 https://git.kernel.org/stable/c/0ea148a799198518d8ebab63ddd0bb6114a103bc