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=1796235614; b=VWr/4z6ruvOzjHfFSqU39BYtzfWCYjbr281TTo9pZaD8nbLRLLJf0uE2P+dX3flqIbDy 8xQ9SuOU4pELqlsQfSIIhRU2Np0gk+y9x6wNmCM7rM93QMacL3PIOMWGDGZVLkMoU6lY3 x9NEZ5HBlYjYahN5ShgCOa4L5O+ZFZJfgAWtoDVz+iEj1o3aGuIQFzv9PF7MeAsIpqtCV h3OL0WTkm7RxktD9WG3759Pgd6JvS3kiA1kgK5pVcNcvWoOYe08S4Olvanw/lnb6OOhRb K8SjfHf9yn+y7bvMVZc0go2r5qqoNWzqbE/79rl3npLr/b74wGmAnmi5USWbeuR+8Rw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796235614; 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=o7wayXSirfkL508dsv7GMuRiFHt3QNhkq1Cd1Dm9go0=; b=FZLOjQQUJ0hEYcamyQJpF6nP/oHP4XDIGudLfThF6NEdv6DNjoYJsdd9ghHZHUYQTlg+ Pi/rUrFVtQVlFamU+fZ5q9Q5TkKv0KOwRmeG0WBNB2hO8ChHHl20fc6W4kr+2iJxRO7uQ xZjgnc1ulg7vH1tF9ZZxES3I6ws+ZnhZop8IRFyMPCodx4gQ88ZhiH3bSZjUJ3vGL4IlF rS8wRGJpl4qCrohgfFga7msg3lm4ARfKkR8SI61bPgBxMIno60evzCc97uH5TDOzQ4yrW vtkegUzue2dkGqYFvnO7qkh0wliDlHQSer+I+Jk4SgLqCX8A+SW8RtulifFR/zvJbFw== 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=YSYoeX6H; 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 F213B606A33E; Wed, 2 Dec 2026 18:20:13 +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=YSYoeX6H Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8C241F0089A; Wed, 2 Dec 2026 18:20:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796235613; bh=o7wayXSirfkL508dsv7GMuRiFHt3QNhkq1Cd1Dm9go0=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=YSYoeX6HyoSIMg25XTAdiJRFaVaD9A+8YBp7d/hHYtKVT1Aq/WWl1c42hfkjRdF33 f0ZRUurFWsgZB3XU8lxBlbA5CHb3KwrOEpF9u9DRERjmuPcTgWJuBJOwL+n/xOWGju qF50SPjydoxqCttCnQWUX3hHyN6pIcryaHTJWEtt3cepFRT1e4+gw8wMUUzZMDrZFA XarBOgJA78rXkibj9KoyMQtgH4PEyylmU6syWawZe1sTdh+RNlnBZi/7tc8aCYZYHw Nygx0BXXoEQwJMcBReoKVchqdVEC5UDbbOwsBgmzYWua/Ni+kV5k9vC/nRS45B2J2H RxO7QO0/H+mYQ== From: "Mike Rapoport (Microsoft)" Date: Wed, 02 Dec 2026 20:20:06 +0200 Subject: [PATCH 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: <20261202-fixes-uffd-sashiko-v1-1-a6f5a874334a@kernel.org> References: <20261202-fixes-uffd-sashiko-v1-0-a6f5a874334a@kernel.org> In-Reply-To: <20261202-fixes-uffd-sashiko-v1-0-a6f5a874334a@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") 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