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=1796381060; b=yEuNmgQ33zdYELInwK1MHXNpRZBmaLKZ7nmZcUtZCitpwrOWeHsQvES9T7Q7a36x64vf HFuhPDxjGyUy/l8OA8S0edZbElq3nsmGaBARDCfdkal0yqqq9Pch5sc0+xWd1WDlYgp2f +PyGQTbc0WS1u8t3eN6Quyl01iP6CQgYO+XhV1G+8pxMU1huISbcEm9sad5w7rR3OmdAD AIPWiKcwy69Lkr2bXHZYTwme1s3sxM6DojdM7dvhHivS8dPFtno8g/9dw+7/7HAhpopuu 5XxDXiysN8EXihsmxECzO/+/GP/f9+KAaA57zB65V6M5DUdFnDCk036KMBRoYnfkHcA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381060; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=8a1qj3jV0U64h8ocGpZIX/BI2MESLZWfzOmg2IX1kF8=; b=ZneogaGNDBwHYfxLgfaMq8D2//R+ieqWvP5ortgFKsn4QyyknKxmJBmcibD8BXfaH2C0 pgw7QOfmdPiHUbPaTfwUt9C5emvWjPQxq10xX5a7Px72ifV7okqpPGV6c0a5iyVlIfCeq 07u8N3mgaE0h0j71tRbaZ2cFfBH/VGIhqYzRoSyHZc7R3Tp5F2qV2odaxofVwHraLPv+A RLijlTihWq8PRiDMx3TH3guRw5uOdzuGa7Xj8lUfVf8Yo+XMH0iRFRx3R0sGHpJlv02r/ QP0JwKJ3KRjOwY+ikl1NmMh/a5+KfGO2yF8tnBFsf0vV1TJJeuwSNZGdidA9pX/ksRQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=dva2YtfO; 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 92A86606A337; Fri, 4 Dec 2026 10:44:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=dva2YtfO Received: by smtp.kernel.org (Postfix) with ESMTPSA id 094F31F00897; Fri, 4 Dec 2026 10:44:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381060; bh=8a1qj3jV0U64h8ocGpZIX/BI2MESLZWfzOmg2IX1kF8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=dva2YtfObAKY+1bBw7fVH6pUUmRJn6Nu3AFJXroZzNnHh6/NgENarPMH5HT9LC1RB kmVVPcBFMcUQfuIArJeee5k/NR8T+A7dpbCMmgDOkUe8LzQTdOoDv7mRz0ut1udCqS FvdA87pLjAvbTIB+KpCBRr9svAODmC7xCf9CB5VQ= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Chris Mason , "Lorenzo Stoakes (ARM)" , Lance Yang , "David Hildenbrand (Arm)" Subject: [PATCH 7.3 542/742] mm/memfd: handle GUP pins more reliably when write-sealing Date: Fri, 4 Dec 2026 19:06:46 +0900 Message-ID: <20261204100649.564505162@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Hildenbrand (Arm) commit d369ba5a46c65c54b94c94713ca1fb4ad6a87ebf upstream. memfd_wait_for_pins() detects GUP references by comparing a folio's actual refcount with its expected refcount. As spelled out by folio_expected_ref_count(), obtaining the expected refcount when there can be concurrent folio (un)mapping is racy. So it is possible that a changing folio mapcount can race with our check for GUP references, and F_SEAL_WRITE might succeed even if there are some, allowing file contents to be modified after sealing. For folios with a reliable pincount, detect FOLL_PIN references using folio_maybe_dma_pinned() before falling back to the refcount comparison. For small folios without that, the 1024 pinning BIAS makes it very hard (although not impossible) to misguide the refcount comparison. This improves FOLL_PIN detection for folios with a pincount. Handling FOLL_GET and small folios requires a more fundamental change. Fortunately, most interfaces that allow for GUP'ing such pages use FOLL_PIN, in particular interfaces using FOLL_LONGTERM. However, we still have some FOLL_GET leftovers ... Fixes: 05f65b5c7090 ("shm: wait for pins to be released when sealing") Cc: stable@vger.kernel.org Cc: Chris Mason Reviewed-by: Lorenzo Stoakes (ARM) Link: https://patch.msgid.link/20261103-fixes_core_2-v1-1-f332954f1df4@kernel.org Reviewed-by: Lance Yang Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Greg Kroah-Hartman --- mm/memfd.c | 7 +++++++ 1 file changed, 7 insertions(+) --- a/mm/memfd.c +++ b/mm/memfd.c @@ -32,6 +32,13 @@ static bool memfd_folio_has_extra_refs(struct folio *folio) { + /* + * Use the precise pincount where available to avoid racy refcount + * comparisons. + */ + if (folio_has_pincount(folio) && folio_maybe_dma_pinned(folio)) + return true; + return folio_ref_count(folio) != folio_expected_ref_count(folio); }