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=1796318130; b=xRYZFyp6u2v2QOiYu6nRB8ydsE/2nfpVFRgn2r/Iq2RONeV16j2QcS0gSoCAz5gIuW66 GA1pn1CnfIUCPoySyfn3sPOrEBAvMeTBnadJavquqGjFOAcdEREmmKSq72qd3q0xc1DpL HR1ISflXLA41VzzCY03xC/Hm/guzmPSkKkqxV9F2awhwbWnBz5g1DpFjYPp5Zt41XLKjK rZsK2u0RCq05AzeJv5BqcYbfG2ECT1fv4Mv/UDe5brTCm49GEDksqW+JyY1AkeUaei2Py Mn1u00AQFBajmjedkPK1GI+JS5FBHKobV9r0rr0SasoV9Jv0+4QNZLsEjU6f6ddi6vQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796318130; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer; bh=M92Dcq3rQIhCrRQbkiPTNprexy2yL4GhqPxg6eYxiM4=; b=fulYuGiemQeTAll4hYiczThYOJ8i59Bnk2jtq6meNk2tQAJA3ZlSvYFy9pCm1MCuu1Yn Pl4VczD+YKSEIyx6SZGljEXDP5dPP0/eIH3GOLVrkYPeWmeLydEtR6Lzj6fw0QyixpIcM EWKus0+5blWv9H5rr3ZeteXYAOi3rbbOH33nxe+D7wMuJ3zz8cmT2w85cdz9UFjAUKOZF iM8NcgROeyxYhTH5+i7/Vx5YJZAzhmwV2BjdPqV6Ao6tQl+re7Dzbq+mV8c7Tdf1yADIt C0b9n3jXkILe87otKzowsFx7vDQrX9iR5qrAh1/3vVlARvPjS9jTOSuQTbjtfKHnpBA== 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=V21LobJE; 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 7DE2E606A339; Thu, 3 Dec 2026 17:15:30 +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=V21LobJE Received: by smtp.kernel.org (Postfix) with ESMTPSA id 953891F00919; Thu, 3 Dec 2026 17:15:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796318129; bh=M92Dcq3rQIhCrRQbkiPTNprexy2yL4GhqPxg6eYxiM4=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=V21LobJEeRJ3Dy5FO5+zQz8ezEh5VGOqyBfyX7OZiaOTAZv80WSZWu3vvfgje54w8 r552bg5KuWozJvTpDR8q8i1BL6jAICoKhzCRmJ190QXAVoM5kjgikErv6D5QUIoR2L FrEmBipgD6iVpdEaUWZ43hLpaF1jDXvBgSIXa7ywleIO3bg/gbKzOSqU/vHD8n9IKF MfRZJhX9vjv0jA0rCN65XKvxUD5JFaElurIjT9oB6JtESbykDcxe3XraV3vSRvOhwA 532altlSdiDwTXoDo2SRNnQfkLJ93M6iU8o47GWq6dFx4PJRJURGOd1/WPRWPZhmMA teZ0B2I+pD7AA== From: "Mike Rapoport (Microsoft)" Date: Thu, 03 Dec 2026 19:15:21 +0200 Subject: [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state 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-fixes-uffd-sashiko-v2-1-308ce557a7b6@kernel.org> References: <20261203-fixes-uffd-sashiko-v2-0-308ce557a7b6@kernel.org> In-Reply-To: <20261203-fixes-uffd-sashiko-v2-0-308ce557a7b6@kernel.org> To: Andrew Morton , David Hildenbrand Cc: Mike Rapoport , Peter Xu , Suren Baghdasaryan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Sashiko X-Mailer: b4 0.16.1-dev UFFDIO_COPY and other uffd operations run under the per-VMA lock. They check that the VMA is registered with a userfaultfd, but not that it is registered with the userfaultfd that issued the operation. When a userfaultfd is closed, userfaultfd_release_all() clears the uffd state of its VMAs without write-locking them. Another userfaultfd of the same mm can then install a write-protected PTE in a part of the VMA that userfaultfd_clear_ptes() has already walked. The PTE keeps its uffd bit after the VMA is unregistered, and a later write fault makes it writable with the uffd bit still set. close(uffd1) UFFDIO_COPY(uffd2, MODE_WP) userfaultfd_release_all() mmap_write_lock() userfaultfd_clear_vma() userfaultfd_clear_ptes() change_protection() // walks past addr lock_vma_under_rcu() validate_dst_vma() // installs PTE with the uffd bit // at addr vma_end_read() userfaultfd_reset_ctx() vma_start_write() mmap_write_unlock() write fault at addr do_wp_page() // the PTE becomes writable with the uffd bit set UFFDIO_UNREGISTER is not affected since commit 35f7ecd55ce7 ("mm/userfaultfd: write-lock the VMA before waking waiters when unregistering"). Write-lock the VMA in userfaultfd_clear_ptes() before walking it. Reported-by: Sashiko Closes: https://sashiko.dev/bug/linux-5cddfd13-146a-47fc-97f8-feca4b2339b4 Fixes: c88033efe9a3 ("mm/userfaultfd: reset ptes when close() for wr-protected ones") Reviewed-by: Suren Baghdasaryan Signed-off-by: Mike Rapoport (Microsoft) --- mm/userfaultfd.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 65f7399e79b65..772adff8883d3 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -2216,6 +2216,9 @@ static void userfaultfd_clear_ptes(struct vm_area_struct *vma, if (!userfaultfd_protected(vma)) return; + /* keep uffd operations under the per-VMA lock away from the walk */ + vma_start_write(vma); + if (userfaultfd_wp(vma)) mm_cp_flags |= MM_CP_UFFD_WP_RESOLVE; if (userfaultfd_rwp(vma)) -- 2.53.0