]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
memfd: deny writeable mappings when implying SEAL_WRITE
authorPratyush Yadav (Google) <pratyush@kernel.org>
Tue, 5 May 2026 13:39:20 +0000 (15:39 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 22 May 2026 02:06:11 +0000 (19:06 -0700)
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.

Link: https://lore.kernel.org/20260505133922.797635-1-pratyush@kernel.org
Fixes: c4f75bc8bd6b ("mm/memfd: add write seals when apply SEAL_EXEC to executable memfd")
Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Acked-by: Jeff Xu <jeffxu@google.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Brendan Jackman <jackmanb@google.com>
Cc: Greg Thelen <gthelen@google.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Kees Cook <kees@kernel.org>
Cc: "David Hildenbrand (Arm)" <david@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/memfd.c

index fb425f4e315f457575d609eb70bceb0e7ecaadfa..abe13b291ddc8b4441aba83def30f10411b51f5c 100644 (file)
@@ -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;