]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
binfmt_elf: mseal address zero
authorJeff Xu <jeffxu@chromium.org>
Tue, 6 Aug 2024 21:49:27 +0000 (21:49 +0000)
committerKees Cook <kees@kernel.org>
Wed, 14 Aug 2024 16:56:48 +0000 (09:56 -0700)
In load_elf_binary as part of the execve(), when the current
task’s personality has MMAP_PAGE_ZERO set, the kernel allocates
one page at address 0. According to the comment:

/* Why this, you ask???  Well SVr4 maps page 0 as read-only,
    and some applications "depend" upon this behavior.
    Since we do not have the power to recompile these, we
     emulate the SVr4 behavior. Sigh. */

At one point, Linus suggested removing this [1].

Code search in debian didn't see much use of MMAP_PAGE_ZERO [2],
it exists in util and test (rr).

Sealing this is probably safe, the comment doesn't say
the app ever wanting to change the mapping to rwx. Sealing
also ensures that never happens.

If there is a complaint, we can make this configurable.

Link: https://lore.kernel.org/lkml/CAHk-=whVa=nm_GW=NVfPHqcxDbWt4JjjK1YWb0cLjO4ZSGyiDA@mail.gmail.com/
Link: https://codesearch.debian.net/search?q=MMAP_PAGE_ZERO&literal=1&perpkg=1&page=1
Signed-off-by: Jeff Xu <jeffxu@chromium.org>
Link: https://lore.kernel.org/r/20240806214931.2198172-2-jeffxu@google.com
Signed-off-by: Kees Cook <kees@kernel.org>
fs/binfmt_elf.c
include/linux/mm.h
mm/mseal.c

index bf9bfd1a0007ffebf3a61daa968710200f9f74bd..c3aa700aeb3c9a819a3be6c0c9950b4ccd920f94 100644 (file)
@@ -1314,6 +1314,11 @@ out_free_interp:
                   emulate the SVr4 behavior. Sigh. */
                error = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
                                MAP_FIXED | MAP_PRIVATE, 0);
+
+               retval = do_mseal(0, PAGE_SIZE, 0);
+               if (retval)
+                       pr_warn_ratelimited("pid=%d, couldn't seal address 0, ret=%d.\n",
+                                           task_pid_nr(current), retval);
        }
 
        regs = current_pt_regs();
index c4b238a20b76e906c4a29fe58cb76cb5e95a56a3..a178c15812ebda2f384785a329589adb420eb521 100644 (file)
@@ -4201,4 +4201,14 @@ void vma_pgtable_walk_end(struct vm_area_struct *vma);
 
 int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t *size);
 
+#ifdef CONFIG_64BIT
+int do_mseal(unsigned long start, size_t len_in, unsigned long flags);
+#else
+static inline int do_mseal(unsigned long start, size_t len_in, unsigned long flags)
+{
+       /* noop on 32 bit */
+       return 0;
+}
+#endif
+
 #endif /* _LINUX_MM_H */
index bf783bba8ed0b92ba80fac5b3459d9bfe0ceb4b4..7a40a84569c8f584349caf86a34110cc362e7562 100644 (file)
@@ -248,7 +248,7 @@ static int apply_mm_seal(unsigned long start, unsigned long end)
  *
  *  unseal() is not supported.
  */
-static int do_mseal(unsigned long start, size_t len_in, unsigned long flags)
+int do_mseal(unsigned long start, size_t len_in, unsigned long flags)
 {
        size_t len;
        int ret = 0;