From mboxrd@z Thu Jan 1 00:00:00 1970 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 CB0533DE430; Tue, 5 May 2026 13:39:34 +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=1777988374; cv=none; b=iO05VMga1tOsHjqwErtTPJibSwTWkWFoU3zetIcrx1ihhtg4cG4fKmupl5/WjTV+QOI17StyJ+3KEMvSfx+SosndxaLOsq30zrGoI9Ii4gleRj5ztFq8B4HXUjGb6CL+VKc1Zo5Cy4wUp3aMPLHGmr40C8wHNreWbOmONl6KBNA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777988374; c=relaxed/simple; bh=yP93zuXO35JzbFR2uaIJaF+ZEOQ/IPJfbTeZRtoTuh8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=PvXBjL+xL9YvYG8lwDnO52Qo7JVHR6iPhsJir1qOH0T7gOB/o77nGt6b8XltZo6q/8JLAIK0DGE1pHhhqV7jzj/EveljcZufnLy8cIpBGguuZxqcS/NfpDOyOuVEcpbb8a2Gr0/QCvJZxKL1LkGnW0UyH919gz/iBSkkjLWbYeA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=u4ju/N3J; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="u4ju/N3J" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82566C2BCB4; Tue, 5 May 2026 13:39:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777988374; bh=yP93zuXO35JzbFR2uaIJaF+ZEOQ/IPJfbTeZRtoTuh8=; h=From:To:Cc:Subject:Date:From; b=u4ju/N3Je4WfXZbx4x7zuV2kiqhpzgJ2pdsonvo3LwA4my/a98rxYGyA/i9J3HtOQ el2hHy7jyflpq3tf+c5/Bu6F54DSRThQL5yXeaUuIVIy/oPfgzJ+0jdgXtzeell1zT H7mJKg2IJni55kxpBoZ32XMSqtbNcvWauE7dviTNodsUEQ0VrxujP1lE59Psy3qYHa yUXEh+izXbfU8pkeRkvG8Z5L3RRNypobg/YdsPZTu9iWgUeKk9v4bB3ESGRt9wAOaX t+td1/ZECCYP47S68qfO1dZx0iCEEGuTQKcuHtEaDNOmk4GcLE3msdOCkVRRFxXF0i fPi8754ZNHbAA== From: Pratyush Yadav To: Hugh Dickins , Baolin Wang , Andrew Morton , Jeff Xu , Kees Cook Cc: "Pratyush Yadav (Google)" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Pasha Tatashin , Brendan Jackman , Greg Thelen , stable@vger.kernel.org Subject: [PATCH] memfd: deny writeable mappings when implying SEAL_WRITE Date: Tue, 5 May 2026 15:39:20 +0200 Message-ID: <20260505133922.797635-1-pratyush@kernel.org> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Pratyush Yadav (Google)" When SEAL_EXEC is added, SEAL_WRITE is implied to make W^X. But the implied seal is set after the check that makes sure the memfd can not have any writable mappings. This means one can use SEAL_EXEC to apply SEAL_WRITE while having writeable mappings. This breaks the contract that SEAL_WRITE provides and can be used by an attacker to pass a memfd that appears to be write sealed but can still be modified arbitrarily. Fix this by adding the implied seals before the call for mapping_deny_writable() is done. Fixes: c4f75bc8bd6b ("mm/memfd: add write seals when apply SEAL_EXEC to executable memfd") Cc: stable@vger.kernel.org Signed-off-by: Pratyush Yadav (Google) --- mm/memfd.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mm/memfd.c b/mm/memfd.c index fb425f4e315f..abe13b291ddc 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -283,6 +283,12 @@ int memfd_add_seals(struct file *file, unsigned int seals) goto unlock; } + /* + * SEAL_EXEC implies SEAL_WRITE, making W^X from the start. + */ + if (seals & F_SEAL_EXEC && inode->i_mode & 0111) + seals |= F_SEAL_SHRINK|F_SEAL_GROW|F_SEAL_WRITE|F_SEAL_FUTURE_WRITE; + if ((seals & F_SEAL_WRITE) && !(*file_seals & F_SEAL_WRITE)) { error = mapping_deny_writable(file->f_mapping); if (error) @@ -295,12 +301,6 @@ int memfd_add_seals(struct file *file, unsigned int seals) } } - /* - * SEAL_EXEC implies SEAL_WRITE, making W^X from the start. - */ - if (seals & F_SEAL_EXEC && inode->i_mode & 0111) - seals |= F_SEAL_SHRINK|F_SEAL_GROW|F_SEAL_WRITE|F_SEAL_FUTURE_WRITE; - *file_seals |= seals; error = 0; -- 2.54.0.545.g6539524ca2-goog