Skip to contentSkip to conversation

seal system mappings

RFC PATCH Superseded [email protected] 5 participants 20 messages Oct 14, 2024 – Nov 13, 2024

Subsystems (10)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover seal system mappings

From: Jeff Xu <[email protected]>

Seal vdso, vvar, sigpage, uprobes and vsyscall.

Those mappings are readonly or executable only, sealing can protect them from ever changing during the life time of the process. For complete descriptions of memory sealing, please see mseal.rst [1].

System mappings such as vdso, vvar, and sigpage (for arm) are generated by the kernel during program initialization. These mappings are designated as non-writable, and sealing them will prevent them from ever becoming writeable.

Unlike the aforementioned mappings, the uprobe mapping is not established during program startup. However, its lifetime is the same as the process's lifetime [2], thus sealable.

The vdso, vvar, sigpage, and uprobe mappings all invoke the _install_special_mapping() function. As no other mappings utilize this function, it is logical to incorporate sealing logic within _install_special_mapping(). This approach avoids the necessity of modifying code across various architecture-specific implementations.

The vsyscall mapping, which has its own initialization function, is sealed in the XONLY case, it seems to be the most common and secure case of using vsyscall.

It is important to note that the CHECKPOINT_RESTORE feature (CRIU) may alter the mapping of vdso, vvar, and sigpage during restore operations. Consequently, this feature cannot be universally enabled across all systems. To address this, a kernel configuration option has been introduced to enable or disable this functionality. Note, uprobe is always sealed and not controlled by this kernel configuration.

I tested CONFIG_SEAL_SYSTEM_MAPPINGS_ALWAYS with ChromeOS, which doesn’t use CHECKPOINT_RESTORE.

[1] Documentation/userspace-api/mseal.rst
[2] https://lore.kernel.org/all/CABi2SkU9BRUnqf70-nksuMCQ+yyiWjo3fM4XkRkL-NrCZxYAyg@mail.gmail.com/

History:
V2:
Seal uprobe always (Oleg Nesterov)
Update comments and description (Randy Dunlap, Liam R.Howlett, Oleg Nesterov) Rebase to linux_main

V1:
https://lore.kernel.org/all/[email protected]/

Jeff Xu (1):
exec: seal system mappings

 .../admin-guide/kernel-parameters.txt         | 10 ++++
 arch/x86/entry/vsyscall/vsyscall_64.c         |  9 +++-
 fs/exec.c                                     | 53 +++++++++++++++++++
 include/linux/fs.h                            |  1 +
 kernel/events/uprobes.c                       |  2 +-
 mm/mmap.c                                     |  1 +
 security/Kconfig                              | 26 +++++++++
 7 files changed, 99 insertions(+), 3 deletions(-)
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
#1 lore raw

RFC PATCH v2 1/1 exec: seal system mappings

From: Jeff Xu <[email protected]>

Seal vdso, vvar, sigpage, uprobes and vsyscall.

Those mappings are readonly or executable only, sealing can protect them from ever changing during the life time of the process. For complete descriptions of memory sealing, please see mseal.rst [1].

System mappings such as vdso, vvar, and sigpage (for arm) are generated by the kernel during program initialization. These mappings are designated as non-writable, and sealing them will prevent them from ever becoming writeable.

Unlike the aforementioned mappings, the uprobe mapping is not established during program startup. However, its lifetime is the same as the process's lifetime [2], thus sealable.

The vdso, vvar, sigpage, and uprobe mappings all invoke the _install_special_mapping() function. As no other mappings utilize this function, it is logical to incorporate sealing logic within _install_special_mapping(). This approach avoids the necessity of modifying code across various architecture-specific implementations.

The vsyscall mapping, which has its own initialization function, is sealed in the XONLY case, it seems to be the most common and secure case of using vsyscall.

It is important to note that the CHECKPOINT_RESTORE feature (CRIU) may alter the mapping of vdso, vvar, and sigpage during restore operations. Consequently, this feature cannot be universally enabled across all systems. To address this, a kernel configuration option has been introduced to enable or disable this functionality. Note, uprobe is always sealed and not controlled by this kernel configuration.

[1] Documentation/userspace-api/mseal.rst
[2] https://lore.kernel.org/all/CABi2SkU9BRUnqf70-nksuMCQ+yyiWjo3fM4XkRkL-NrCZxYAyg@mail.gmail.com/

---
 .../admin-guide/kernel-parameters.txt         | 10 ++++
 arch/x86/entry/vsyscall/vsyscall_64.c         |  9 +++-
 fs/exec.c                                     | 53 +++++++++++++++++++
 include/linux/fs.h                            |  1 +
 kernel/events/uprobes.c                       |  2 +-
 mm/mmap.c                                     |  1 +
 security/Kconfig                              | 26 +++++++++
 7 files changed, 99 insertions(+), 3 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +10 -0
index e7bfe1bde49e..02e5eb23d76f 100644
@@ -1538,6 +1538,16 @@
 			Permit 'security.evm' to be updated regardless of
 			current integrity status.
 
+	exec.seal_system_mappings = [KNL]
+			Format: { never | always }
+			Seal system mappings: vdso, vvar, sigpage, uprobes,
+			vsyscall.
+			This overwrites KCONFIG CONFIG_SEAL_SYSTEM_MAPPINGS_*
+			- 'never':  never seal system mappings.
+			- 'always': always seal system mappings.
+			If not specified or invalid, default is the KCONFIG value.
+			This option has no effect if CONFIG_64BIT=n
+
 	early_page_ext [KNL,EARLY] Enforces page_ext initialization to earlier
 			stages so cover more early boot allocations.
 			Please note that as side effect some optimizations
arch/x86/entry/vsyscall/vsyscall_64.c +7 -2
index 2fb7d53cf333..20a3000550d2 100644
@@ -32,6 +32,7 @@
 #include <linux/mm_types.h>
 #include <linux/syscalls.h>
 #include <linux/ratelimit.h>
+#include <linux/fs.h>
 
 #include <asm/vsyscall.h>
 #include <asm/unistd.h>
@@ -366,8 +367,12 @@ void __init map_vsyscall(void)
 		set_vsyscall_pgtable_user_bits(swapper_pg_dir);
 	}
 
-	if (vsyscall_mode == XONLY)
-		vm_flags_init(&gate_vma, VM_EXEC);
+	if (vsyscall_mode == XONLY) {
+		unsigned long vm_flags = VM_EXEC;
+
+		update_seal_exec_system_mappings(&vm_flags);
+		vm_flags_init(&gate_vma, vm_flags);
+	}
 
 	BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE) !=
 		     (unsigned long)VSYSCALL_ADDR);
fs/exec.c +53 -0

62 lines not on this page · raw message

include/linux/fs.h +1 -0
index 42444ec95c9b..6e44aca4b24b 100644
@@ -3079,6 +3079,7 @@ ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos);
 extern ssize_t kernel_write(struct file *, const void *, size_t, loff_t *);
 extern ssize_t __kernel_write(struct file *, const void *, size_t, loff_t *);
 extern struct file * open_exec(const char *);
+extern void update_seal_exec_system_mappings(unsigned long *vm_flags);
  
 /* fs/dcache.c -- generic fs support functions */
 extern bool is_subdir(struct dentry *, struct dentry *);
kernel/events/uprobes.c +1 -1
index c47a0bf25e58..e9876fae8887 100644
@@ -1506,7 +1506,7 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
 	}
 
 	vma = _install_special_mapping(mm, area->vaddr, PAGE_SIZE,
-				VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO,
+				VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO|VM_SEALED,
 				&xol_mapping);
 	if (IS_ERR(vma)) {
 		ret = PTR_ERR(vma);
mm/mmap.c +1 -0
index 57fd5ab2abe7..d4717e34a60d 100644
@@ -2133,6 +2133,7 @@ struct vm_area_struct *_install_special_mapping(
 	unsigned long addr, unsigned long len,
 	unsigned long vm_flags, const struct vm_special_mapping *spec)
 {
+	update_seal_exec_system_mappings(&vm_flags);
 	return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
 					&special_mapping_vmops);
 }
security/Kconfig +26 -0
index 28e685f53bd1..4ec8045339c3 100644
@@ -51,6 +51,32 @@ config PROC_MEM_NO_FORCE
 
 endchoice
 
+choice
+	prompt "Seal system mappings"
+	default SEAL_SYSTEM_MAPPINGS_NEVER
+	help
+	  Seal system mappings such as vdso, vvar, sigpage, uprobes and
+	  vsyscall.
+	  Note: kernel command line exec.seal_system_mappings overwrites this.
+
+config SEAL_SYSTEM_MAPPINGS_NEVER
+	bool "Traditional behavior - not sealed"
+	help
+	  Do not seal system mappings.
+	  This is default.
+
+config SEAL_SYSTEM_MAPPINGS_ALWAYS
+	bool "Always seal system mappings"
+	depends on 64BIT
+	depends on !CHECKPOINT_RESTORE
+	help
+	  Seal system mappings such as vdso, vvar, sigpage, uprobes and
+	  vsyscall.
+	  Note: CHECKPOINT_RESTORE might relocate vdso mapping during restore,
+	  and remap will fail if the mapping is sealed, therefore
+	  !CHECKPOINT_RESTORE is added as dependency.
+endchoice
+
 config SECURITY
 	bool "Enable different security models"
 	depends on SYSFS
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
maintainer
#2 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

(I don't think this needs "RFC" any more)

Quoted 56 lines · On Mon, Oct 14, 2024 at 09:50:20PM +0000, [email protected] wrote:

From: Jeff Xu <[email protected]>

Seal vdso, vvar, sigpage, uprobes and vsyscall.

Those mappings are readonly or executable only, sealing can protect them from ever changing during the life time of the process. For complete descriptions of memory sealing, please see mseal.rst [1].

System mappings such as vdso, vvar, and sigpage (for arm) are generated by the kernel during program initialization. These mappings are designated as non-writable, and sealing them will prevent them from ever becoming writeable.

Unlike the aforementioned mappings, the uprobe mapping is not established during program startup. However, its lifetime is the same as the process's lifetime [2], thus sealable.

The vdso, vvar, sigpage, and uprobe mappings all invoke the _install_special_mapping() function. As no other mappings utilize this function, it is logical to incorporate sealing logic within _install_special_mapping(). This approach avoids the necessity of modifying code across various architecture-specific implementations.

The vsyscall mapping, which has its own initialization function, is sealed in the XONLY case, it seems to be the most common and secure case of using vsyscall.

It is important to note that the CHECKPOINT_RESTORE feature (CRIU) may alter the mapping of vdso, vvar, and sigpage during restore operations. Consequently, this feature cannot be universally enabled across all systems. To address this, a kernel configuration option has been introduced to enable or disable this functionality. Note, uprobe is always sealed and not controlled by this kernel configuration.

[1] Documentation/userspace-api/mseal.rst
[2] https://lore.kernel.org/all/CABi2SkU9BRUnqf70-nksuMCQ+yyiWjo3fM4XkRkL-NrCZxYAyg@mail.gmail.com/

---
 .../admin-guide/kernel-parameters.txt         | 10 ++++
 arch/x86/entry/vsyscall/vsyscall_64.c         |  9 +++-
 fs/exec.c                                     | 53 +++++++++++++++++++
 include/linux/fs.h                            |  1 +
 kernel/events/uprobes.c                       |  2 +-
 mm/mmap.c                                     |  1 +
 security/Kconfig                              | 26 +++++++++
 7 files changed, 99 insertions(+), 3 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +0 -0
index e7bfe1bde49e..02e5eb23d76f 100644
@@ -1538,6 +1538,16 @@
 			Permit 'security.evm' to be updated regardless of
 			current integrity status.
 

Documentation/admin-guide/kernel-parameters.txt line 1550

Quoted 10 lines
Documentation/admin-guide/kernel-parameters.txt +10 -0
+	exec.seal_system_mappings = [KNL]
+			Format: { never | always }
+			Seal system mappings: vdso, vvar, sigpage, uprobes,
+			vsyscall.
+			This overwrites KCONFIG CONFIG_SEAL_SYSTEM_MAPPINGS_*
+			- 'never':  never seal system mappings.
+			- 'always': always seal system mappings.
+			If not specified or invalid, default is the KCONFIG value.
+			This option has no effect if CONFIG_64BIT=n
+

Any reason for "always"/"never" instead of the more traditional y/n enabled/disabled, etc?

Otherwise, this all makes sense to me.

Signature · 1 line
Kees Cook
author
#3 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

Quoted 60 lines · Kees Cook

(I don't think this needs "RFC" any more)

Quoted 56 lines · On Mon, Oct 14, 2024 at 09:50:20PM +0000, [email protected] wrote:

From: Jeff Xu <[email protected]>

Seal vdso, vvar, sigpage, uprobes and vsyscall.

Those mappings are readonly or executable only, sealing can protect them from ever changing during the life time of the process. For complete descriptions of memory sealing, please see mseal.rst [1].

System mappings such as vdso, vvar, and sigpage (for arm) are generated by the kernel during program initialization. These mappings are designated as non-writable, and sealing them will prevent them from ever becoming writeable.

Unlike the aforementioned mappings, the uprobe mapping is not established during program startup. However, its lifetime is the same as the process's lifetime [2], thus sealable.

The vdso, vvar, sigpage, and uprobe mappings all invoke the _install_special_mapping() function. As no other mappings utilize this function, it is logical to incorporate sealing logic within _install_special_mapping(). This approach avoids the necessity of modifying code across various architecture-specific implementations.

The vsyscall mapping, which has its own initialization function, is sealed in the XONLY case, it seems to be the most common and secure case of using vsyscall.

It is important to note that the CHECKPOINT_RESTORE feature (CRIU) may alter the mapping of vdso, vvar, and sigpage during restore operations. Consequently, this feature cannot be universally enabled across all systems. To address this, a kernel configuration option has been introduced to enable or disable this functionality. Note, uprobe is always sealed and not controlled by this kernel configuration.

[1] Documentation/userspace-api/mseal.rst
[2] https://lore.kernel.org/all/CABi2SkU9BRUnqf70-nksuMCQ+yyiWjo3fM4XkRkL-NrCZxYAyg@mail.gmail.com/

---
 .../admin-guide/kernel-parameters.txt         | 10 ++++
 arch/x86/entry/vsyscall/vsyscall_64.c         |  9 +++-
 fs/exec.c                                     | 53 +++++++++++++++++++
 include/linux/fs.h                            |  1 +
 kernel/events/uprobes.c                       |  2 +-
 mm/mmap.c                                     |  1 +
 security/Kconfig                              | 26 +++++++++
 7 files changed, 99 insertions(+), 3 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +0 -0
index e7bfe1bde49e..02e5eb23d76f 100644
@@ -1538,6 +1538,16 @@
                      Permit 'security.evm' to be updated regardless of
                      current integrity status.
 
Quoted 14 lines

Documentation/admin-guide/kernel-parameters.txt line 1550

Quoted 10 lines
Documentation/admin-guide/kernel-parameters.txt +10 -0
+     exec.seal_system_mappings = [KNL]
+                     Format: { never | always }
+                     Seal system mappings: vdso, vvar, sigpage, uprobes,
+                     vsyscall.
+                     This overwrites KCONFIG CONFIG_SEAL_SYSTEM_MAPPINGS_*
+                     - 'never':  never seal system mappings.
+                     - 'always': always seal system mappings.
+                     If not specified or invalid, default is the KCONFIG value.
+                     This option has no effect if CONFIG_64BIT=n
+

Any reason for "always"/"never" instead of the more traditional y/n enabled/disabled, etc?

Yes. I like to leave room for future extension, in case someone ever needs a prctl for pre-process opt-in, e.g. Format:{never|prctl|always}

Quoted 4 lines

Otherwise, this all makes sense to me.

--
Kees Cook

author
#4 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

Quoted 76 lines · Jeff Xu
Quoted 60 lines · Kees Cook

(I don't think this needs "RFC" any more)

Quoted 56 lines · On Mon, Oct 14, 2024 at 09:50:20PM +0000, [email protected] wrote:

From: Jeff Xu <[email protected]>

Seal vdso, vvar, sigpage, uprobes and vsyscall.

Those mappings are readonly or executable only, sealing can protect them from ever changing during the life time of the process. For complete descriptions of memory sealing, please see mseal.rst [1].

System mappings such as vdso, vvar, and sigpage (for arm) are generated by the kernel during program initialization. These mappings are designated as non-writable, and sealing them will prevent them from ever becoming writeable.

Unlike the aforementioned mappings, the uprobe mapping is not established during program startup. However, its lifetime is the same as the process's lifetime [2], thus sealable.

The vdso, vvar, sigpage, and uprobe mappings all invoke the _install_special_mapping() function. As no other mappings utilize this function, it is logical to incorporate sealing logic within _install_special_mapping(). This approach avoids the necessity of modifying code across various architecture-specific implementations.

The vsyscall mapping, which has its own initialization function, is sealed in the XONLY case, it seems to be the most common and secure case of using vsyscall.

It is important to note that the CHECKPOINT_RESTORE feature (CRIU) may alter the mapping of vdso, vvar, and sigpage during restore operations. Consequently, this feature cannot be universally enabled across all systems. To address this, a kernel configuration option has been introduced to enable or disable this functionality. Note, uprobe is always sealed and not controlled by this kernel configuration.

[1] Documentation/userspace-api/mseal.rst
[2] https://lore.kernel.org/all/CABi2SkU9BRUnqf70-nksuMCQ+yyiWjo3fM4XkRkL-NrCZxYAyg@mail.gmail.com/

---
 .../admin-guide/kernel-parameters.txt         | 10 ++++
 arch/x86/entry/vsyscall/vsyscall_64.c         |  9 +++-
 fs/exec.c                                     | 53 +++++++++++++++++++
 include/linux/fs.h                            |  1 +
 kernel/events/uprobes.c                       |  2 +-
 mm/mmap.c                                     |  1 +
 security/Kconfig                              | 26 +++++++++
 7 files changed, 99 insertions(+), 3 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +0 -0
index e7bfe1bde49e..02e5eb23d76f 100644
@@ -1538,6 +1538,16 @@
                      Permit 'security.evm' to be updated regardless of
                      current integrity status.
 
Quoted 14 lines

Documentation/admin-guide/kernel-parameters.txt line 1550

Quoted 10 lines
Documentation/admin-guide/kernel-parameters.txt +10 -0
+     exec.seal_system_mappings = [KNL]
+                     Format: { never | always }
+                     Seal system mappings: vdso, vvar, sigpage, uprobes,
+                     vsyscall.
+                     This overwrites KCONFIG CONFIG_SEAL_SYSTEM_MAPPINGS_*
+                     - 'never':  never seal system mappings.
+                     - 'always': always seal system mappings.
+                     If not specified or invalid, default is the KCONFIG value.
+                     This option has no effect if CONFIG_64BIT=n
+

Any reason for "always"/"never" instead of the more traditional y/n enabled/disabled, etc?

Quoted 4 lines

Yes. I like to leave room for future extension, in case someone ever needs a prctl for pre-process opt-in, e.g. Format:{never|prctl|always}

I copied the pattern from:

        proc_mem.force_override= [KNL]
                        Format: {always | ptrace | never}
Quoted 4 lines
Quoted 4 lines

Otherwise, this all makes sense to me.

--
Kees Cook

#5 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

In reply to [email protected]
Quoted 27 lines · * [email protected] <[email protected]> [241014 17:50]:

From: Jeff Xu <[email protected]>

Seal vdso, vvar, sigpage, uprobes and vsyscall.

Those mappings are readonly or executable only, sealing can protect them from ever changing during the life time of the process. For complete descriptions of memory sealing, please see mseal.rst [1].

System mappings such as vdso, vvar, and sigpage (for arm) are generated by the kernel during program initialization. These mappings are designated as non-writable, and sealing them will prevent them from ever becoming writeable.

Unlike the aforementioned mappings, the uprobe mapping is not established during program startup. However, its lifetime is the same as the process's lifetime [2], thus sealable.

The vdso, vvar, sigpage, and uprobe mappings all invoke the _install_special_mapping() function. As no other mappings utilize this function, it is logical to incorporate sealing logic within _install_special_mapping(). This approach avoids the necessity of modifying code across various architecture-specific implementations.

The vsyscall mapping, which has its own initialization function, is sealed in the XONLY case, it seems to be the most common and secure case of using vsyscall.

Quoted 6 lines

It is important to note that the CHECKPOINT_RESTORE feature (CRIU) may alter the mapping of vdso, vvar, and sigpage during restore operations. Consequently, this feature cannot be universally enabled across all systems. To address this, a kernel configuration option has been introduced to enable or disable this functionality. Note, uprobe is always sealed and not controlled by this kernel configuration.

Considering that uprobes are always sealed regardless of boot or kernel config, the descriptions below all suffer from inaccurate descriptions.

It is also very easy to overlook that you are changing the default vm flags of uprobe, especially considering the text implies that they are not altered if you select "never".

Quoted 20 lines

[1] Documentation/userspace-api/mseal.rst
[2] https://lore.kernel.org/all/CABi2SkU9BRUnqf70-nksuMCQ+yyiWjo3fM4XkRkL-NrCZxYAyg@mail.gmail.com/

---
 .../admin-guide/kernel-parameters.txt         | 10 ++++
 arch/x86/entry/vsyscall/vsyscall_64.c         |  9 +++-
 fs/exec.c                                     | 53 +++++++++++++++++++
 include/linux/fs.h                            |  1 +
 kernel/events/uprobes.c                       |  2 +-
 mm/mmap.c                                     |  1 +
 security/Kconfig                              | 26 +++++++++
 7 files changed, 99 insertions(+), 3 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +0 -0
index e7bfe1bde49e..02e5eb23d76f 100644

Documentation/admin-guide/kernel-parameters.txt line 1546

Quoted 9 lines
Documentation/admin-guide/kernel-parameters.txt +6 -0
@@ -1538,6 +1538,16 @@
 			Permit 'security.evm' to be updated regardless of
 			current integrity status.
 
+	exec.seal_system_mappings = [KNL]
+			Format: { never | always }
+			Seal system mappings: vdso, vvar, sigpage, uprobes,
+			vsyscall.
+			This overwrites KCONFIG CONFIG_SEAL_SYSTEM_MAPPINGS_*
+			- 'never':  never seal system mappings.

Not true, uprobes are sealed when 'never' is selected.

fs/exec.c

Quoted 38 lines
Documentation/admin-guide/kernel-parameters.txt +4 -0
+			- 'always': always seal system mappings.
+			If not specified or invalid, default is the KCONFIG value.
+			This option has no effect if CONFIG_64BIT=n
+
 	early_page_ext [KNL,EARLY] Enforces page_ext initialization to earlier
 			stages so cover more early boot allocations.
 			Please note that as side effect some optimizations
arch/x86/entry/vsyscall/vsyscall_64.c +7 -2
index 2fb7d53cf333..20a3000550d2 100644
@@ -32,6 +32,7 @@
 #include <linux/mm_types.h>
 #include <linux/syscalls.h>
 #include <linux/ratelimit.h>
+#include <linux/fs.h>
 
 #include <asm/vsyscall.h>
 #include <asm/unistd.h>
@@ -366,8 +367,12 @@ void __init map_vsyscall(void)
 		set_vsyscall_pgtable_user_bits(swapper_pg_dir);
 	}
 
-	if (vsyscall_mode == XONLY)
-		vm_flags_init(&gate_vma, VM_EXEC);
+	if (vsyscall_mode == XONLY) {
+		unsigned long vm_flags = VM_EXEC;
+
+		update_seal_exec_system_mappings(&vm_flags);
+		vm_flags_init(&gate_vma, vm_flags);
+	}
 
 	BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE) !=
 		     (unsigned long)VSYSCALL_ADDR);
fs/exec.c +0 -0
index 77364806b48d..5030879cda47 100644

Does it make sense for this to live in exec? Couldn't you put it in the mm/mseal.c file? It's vma flags for mappings and you've put it in fs/exec?

Quoted 42 lines
fs/exec.c +31 -0
@@ -68,6 +68,7 @@
 #include <linux/user_events.h>
 #include <linux/rseq.h>
 #include <linux/ksm.h>
+#include <linux/fs_parser.h>
 
 #include <linux/uaccess.h>
 #include <asm/mmu_context.h>
@@ -2159,3 +2160,55 @@ fs_initcall(init_fs_exec_sysctls);
 #ifdef CONFIG_EXEC_KUNIT_TEST
 #include "tests/exec_kunit.c"
 #endif
+
+#ifdef CONFIG_64BIT
+/*
+ * Kernel cmdline overwrite for CONFIG_SEAL_SYSTEM_MAPPINGS_X
+ */
+enum seal_system_mappings_type {
+	SEAL_SYSTEM_MAPPINGS_NEVER,
+	SEAL_SYSTEM_MAPPINGS_ALWAYS
+};
+
+static enum seal_system_mappings_type seal_system_mappings __ro_after_init =
+	IS_ENABLED(CONFIG_SEAL_SYSTEM_MAPPINGS_ALWAYS) ? SEAL_SYSTEM_MAPPINGS_ALWAYS :
+	SEAL_SYSTEM_MAPPINGS_NEVER;
+
+static const struct constant_table value_table_sys_mapping[] __initconst = {
+	{ "never", SEAL_SYSTEM_MAPPINGS_NEVER},
+	{ "always", SEAL_SYSTEM_MAPPINGS_ALWAYS},
+	{ }
+};
+
+static int __init early_seal_system_mappings_override(char *buf)
+{
+	if (!buf)
+		return -EINVAL;
+
+	seal_system_mappings = lookup_constant(value_table_sys_mapping,
+			buf, seal_system_mappings);
+
+	return 0;
+}

fs/exec.c fs_initcall(init_fs_exec_sysctls); · line 2202

Quoted 10 lines
fs/exec.c +10 -0
+
+early_param("exec.seal_system_mappings", early_seal_system_mappings_override);
+
+static bool seal_system_mappings_enabled(void)
+{
+	if (seal_system_mappings == SEAL_SYSTEM_MAPPINGS_ALWAYS)
+		return true;
+
+	return false;
+}

This function seems unnecessary, it is called from another 3-4 line function only.

fs/exec.c fs_initcall(init_fs_exec_sysctls); · line 2206

Quoted 4 lines
fs/exec.c +4 -0
+
+void update_seal_exec_system_mappings(unsigned long *vm_flags)
+{
+	if (!(*vm_flags & VM_SEALED) && seal_system_mappings_enabled())

Why !(*vm_flags & VM_SEALED) here?

fs/exec.c fs_initcall(init_fs_exec_sysctls); · line 2209

Quoted 3 lines
fs/exec.c +3 -0
+		*vm_flags |= VM_SEALED;
+
+}

Instead of passing a pointer around and checking enabled, why don't you have a function that just returns the VM_SEALED or 0 and just or it into the flags? This seems very heavy for what it does, why did you do it this way?

The name is also very long and a bit odd, it could be used for other reasons, but you have _system_mappings on the end, and you use seal but it's mseal (or vm_seal)? Would mseal_flag() work?

include/linux/fs.h

Quoted 9 lines
fs/exec.c +5 -0
+#else
+void update_seal_exec_system_mappings(unsigned long *vm_flags)
+{
+}
+#endif /* CONFIG_64BIT */
include/linux/fs.h +0 -0
index 42444ec95c9b..6e44aca4b24b 100644

Again, I don't understand why fs.h is the place for mseal definitions?

include/linux/fs.h ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos); · line 3082

Quoted 5 lines
include/linux/fs.h +1 -0
@@ -3079,6 +3079,7 @@ ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos);
 extern ssize_t kernel_write(struct file *, const void *, size_t, loff_t *);
 extern ssize_t __kernel_write(struct file *, const void *, size_t, loff_t *);
 extern struct file * open_exec(const char *);
+extern void update_seal_exec_system_mappings(unsigned long *vm_flags);

We are dropping extern where possible now.

Quoted 8 lines
include/linux/fs.h +0 -0
 /* fs/dcache.c -- generic fs support functions */
 extern bool is_subdir(struct dentry *, struct dentry *);
kernel/events/uprobes.c +0 -0
index c47a0bf25e58..e9876fae8887 100644

kernel/events/uprobes.c static int xol_add_vma(struct mm_struct *mm, struct xol_area *area) · line 1510

Quoted 6 lines
kernel/events/uprobes.c +1 -1
@@ -1506,7 +1506,7 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
 	}
 
 	vma = _install_special_mapping(mm, area->vaddr, PAGE_SIZE,
-				VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO,
+				VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO|VM_SEALED,
 				&xol_mapping);

Changing all uprobes seems like something that should probably be mentioned more than just the note at the end of the change log, even if you think it won't have any impact. The note is even hidden at the end of a paragraph.

I would go as far as splitting this patch out as its own so that the subject line specifies that all uprobes will be VM_SEALED now.

Maybe it's fine but maybe it isn't and you've buried it so that it will be missed by virtually everyone.

mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 2138

Quoted 13 lines
kernel/events/uprobes.c +0 -0
 	if (IS_ERR(vma)) {
 		ret = PTR_ERR(vma);
mm/mmap.c +1 -0
index 57fd5ab2abe7..d4717e34a60d 100644
@@ -2133,6 +2133,7 @@ struct vm_area_struct *_install_special_mapping(
 	unsigned long addr, unsigned long len,
 	unsigned long vm_flags, const struct vm_special_mapping *spec)
 {
+	update_seal_exec_system_mappings(&vm_flags);
 	return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
 					&special_mapping_vmops);

If you were to return a flag, you could change the vm_flags argument to vm_flags | mseal_flag()

Quoted 6 lines
mm/mmap.c +0 -0
 }
security/Kconfig +0 -0
index 28e685f53bd1..4ec8045339c3 100644

security/Kconfig config PROC_MEM_NO_FORCE · line 60

Quoted 10 lines
security/Kconfig +7 -0
@@ -51,6 +51,32 @@ config PROC_MEM_NO_FORCE
 
 endchoice
 
+choice
+	prompt "Seal system mappings"
+	default SEAL_SYSTEM_MAPPINGS_NEVER
+	help
+	  Seal system mappings such as vdso, vvar, sigpage, uprobes and
+	  vsyscall.
+	  Note: kernel command line exec.seal_system_mappings overwrites this.

Not true, uprobes are always sealed.

security/Kconfig config PROC_MEM_NO_FORCE · line 63

Quoted 3 lines
security/Kconfig +3 -0
+
+config SEAL_SYSTEM_MAPPINGS_NEVER
+	bool "Traditional behavior - not sealed"

Not true, uprobes are sealed.

Quoted 22 lines
security/Kconfig +16 -0
+	help
+	  Do not seal system mappings.
+	  This is default.
+
+config SEAL_SYSTEM_MAPPINGS_ALWAYS
+	bool "Always seal system mappings"
+	depends on 64BIT
+	depends on !CHECKPOINT_RESTORE
+	help
+	  Seal system mappings such as vdso, vvar, sigpage, uprobes and
+	  vsyscall.
+	  Note: CHECKPOINT_RESTORE might relocate vdso mapping during restore,
+	  and remap will fail if the mapping is sealed, therefore
+	  !CHECKPOINT_RESTORE is added as dependency.
+endchoice
+
 config SECURITY
 	bool "Enable different security models"
 	depends on SYSFS
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
author
#6 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

Quoted 10 lines · Liam R. Howlett

Documentation/admin-guide/kernel-parameters.txt line 1546

Quoted 7 lines
Documentation/admin-guide/kernel-parameters.txt +6 -0
+     exec.seal_system_mappings = [KNL]
+                     Format: { never | always }
+                     Seal system mappings: vdso, vvar, sigpage, uprobes,
+                     vsyscall.
+                     This overwrites KCONFIG CONFIG_SEAL_SYSTEM_MAPPINGS_*
+                     - 'never':  never seal system mappings.

Not true, uprobes are sealed when 'never' is selected.

Thanks. I forgot to uprobes from the description in Kconfig and kernel-parameters.txt, will update.

Quoted 32 lines
Quoted 32 lines
Documentation/admin-guide/kernel-parameters.txt +4 -0
+                     - 'always': always seal system mappings.
+                     If not specified or invalid, default is the KCONFIG value.
+                     This option has no effect if CONFIG_64BIT=n
+
      early_page_ext [KNL,EARLY] Enforces page_ext initialization to earlier
                      stages so cover more early boot allocations.
                      Please note that as side effect some optimizations
arch/x86/entry/vsyscall/vsyscall_64.c +7 -2
index 2fb7d53cf333..20a3000550d2 100644
@@ -32,6 +32,7 @@
 #include <linux/mm_types.h>
 #include <linux/syscalls.h>
 #include <linux/ratelimit.h>
+#include <linux/fs.h>
 
 #include <asm/vsyscall.h>
 #include <asm/unistd.h>
@@ -366,8 +367,12 @@ void __init map_vsyscall(void)
              set_vsyscall_pgtable_user_bits(swapper_pg_dir);
      }
 
-     if (vsyscall_mode == XONLY)
-             vm_flags_init(&gate_vma, VM_EXEC);
+     if (vsyscall_mode == XONLY) {
+             unsigned long vm_flags = VM_EXEC;
+
+             update_seal_exec_system_mappings(&vm_flags);
+             vm_flags_init(&gate_vma, vm_flags);
+     }
 
Quoted 11 lines

fs/exec.c

Quoted 6 lines
arch/x86/entry/vsyscall/vsyscall_64.c +0 -0
      BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE) !=
                   (unsigned long)VSYSCALL_ADDR);
fs/exec.c +0 -0
index 77364806b48d..5030879cda47 100644

Does it make sense for this to live in exec? Couldn't you put it in the mm/mseal.c file? It's vma flags for mappings and you've put it in fs/exec?

If you are referring to utilities related to kernel cmdline, they should be in this file.

Quoted 47 lines
Quoted 47 lines
fs/exec.c +36 -0
@@ -68,6 +68,7 @@
 #include <linux/user_events.h>
 #include <linux/rseq.h>
 #include <linux/ksm.h>
+#include <linux/fs_parser.h>
 
 #include <linux/uaccess.h>
 #include <asm/mmu_context.h>
@@ -2159,3 +2160,55 @@ fs_initcall(init_fs_exec_sysctls);
 #ifdef CONFIG_EXEC_KUNIT_TEST
 #include "tests/exec_kunit.c"
 #endif
+
+#ifdef CONFIG_64BIT
+/*
+ * Kernel cmdline overwrite for CONFIG_SEAL_SYSTEM_MAPPINGS_X
+ */
+enum seal_system_mappings_type {
+     SEAL_SYSTEM_MAPPINGS_NEVER,
+     SEAL_SYSTEM_MAPPINGS_ALWAYS
+};
+
+static enum seal_system_mappings_type seal_system_mappings __ro_after_init =
+     IS_ENABLED(CONFIG_SEAL_SYSTEM_MAPPINGS_ALWAYS) ? SEAL_SYSTEM_MAPPINGS_ALWAYS :
+     SEAL_SYSTEM_MAPPINGS_NEVER;
+
+static const struct constant_table value_table_sys_mapping[] __initconst = {
+     { "never", SEAL_SYSTEM_MAPPINGS_NEVER},
+     { "always", SEAL_SYSTEM_MAPPINGS_ALWAYS},
+     { }
+};
+
+static int __init early_seal_system_mappings_override(char *buf)
+{
+     if (!buf)
+             return -EINVAL;
+
+     seal_system_mappings = lookup_constant(value_table_sys_mapping,
+                     buf, seal_system_mappings);
+
+     return 0;
+}
+
+early_param("exec.seal_system_mappings", early_seal_system_mappings_override);
+
+static bool seal_system_mappings_enabled(void)
+{
Quoted 9 lines

fs/exec.c fs_initcall(init_fs_exec_sysctls); · line 2202

Quoted 5 lines
fs/exec.c +5 -0
+     if (seal_system_mappings == SEAL_SYSTEM_MAPPINGS_ALWAYS)
+             return true;
+
+     return false;
+}

This function seems unnecessary, it is called from another 3-4 line function only.

It is more readable this way.

Quoted 7 lines

fs/exec.c fs_initcall(init_fs_exec_sysctls); · line 2206

Quoted 4 lines
fs/exec.c +4 -0
+
+void update_seal_exec_system_mappings(unsigned long *vm_flags)
+{
+     if (!(*vm_flags & VM_SEALED) && seal_system_mappings_enabled())

Why !(*vm_flags & VM_SEALED) here?

If vm_flags is already sealed, then there is no need to check seal_system_mappings_enabled.

Quoted 9 lines

fs/exec.c fs_initcall(init_fs_exec_sysctls); · line 2209

Quoted 3 lines
fs/exec.c +3 -0
+             *vm_flags |= VM_SEALED;
+
+}

Instead of passing a pointer around and checking enabled, why don't you have a function that just returns the VM_SEALED or 0 and just or it into the flags? This seems very heavy for what it does, why did you do it this way?

Why is that heavy ? passing a pointer for updating variables is natural.

Quoted 4 lines

The name is also very long and a bit odd, it could be used for other reasons, but you have _system_mappings on the end, and you use seal but it's mseal (or vm_seal)? Would mseal_flag() work?

It could be longer :-)
it means update_sealing_flag_for_executable_system_mappings.
mseal_flag is too short and not descriptive.

Quoted 12 lines

include/linux/fs.h

Quoted 9 lines
fs/exec.c +5 -0
+#else
+void update_seal_exec_system_mappings(unsigned long *vm_flags)
+{
+}
+#endif /* CONFIG_64BIT */
include/linux/fs.h +0 -0
index 42444ec95c9b..6e44aca4b24b 100644

Again, I don't understand why fs.h is the place for mseal definitions?

include/linux/fs.h contains other exec related function signatures. So it is better to keep them at the same header.

Quoted 8 lines

include/linux/fs.h ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos); · line 3082

Quoted 5 lines
include/linux/fs.h +1 -0
@@ -3079,6 +3079,7 @@ ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos);
 extern ssize_t kernel_write(struct file *, const void *, size_t, loff_t *);
 extern ssize_t __kernel_write(struct file *, const void *, size_t, loff_t *);
 extern struct file * open_exec(const char *);
+extern void update_seal_exec_system_mappings(unsigned long *vm_flags);

We are dropping extern where possible now.

extern can be dropped, it appears not causing link error.

Quoted 20 lines
Quoted 8 lines
include/linux/fs.h +0 -0
 /* fs/dcache.c -- generic fs support functions */
 extern bool is_subdir(struct dentry *, struct dentry *);
kernel/events/uprobes.c +0 -0
index c47a0bf25e58..e9876fae8887 100644

kernel/events/uprobes.c static int xol_add_vma(struct mm_struct *mm, struct xol_area *area) · line 1510

Quoted 6 lines
kernel/events/uprobes.c +1 -1
@@ -1506,7 +1506,7 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
      }
 
      vma = _install_special_mapping(mm, area->vaddr, PAGE_SIZE,
-                             VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO,
+                             VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO|VM_SEALED,
                              &xol_mapping);

Changing all uprobes seems like something that should probably be mentioned more than just the note at the end of the change log, even if you think it won't have any impact. The note is even hidden at the end of a paragraph.

Quoted 6 lines

I would go as far as splitting this patch out as its own so that the subject line specifies that all uprobes will be VM_SEALED now.

Maybe it's fine but maybe it isn't and you've buried it so that it will be missed by virtually everyone.

I will add "It is sealed from creation." in the above "uprobe" section.

Quoted 18 lines
Quoted 8 lines
kernel/events/uprobes.c +0 -0
      if (IS_ERR(vma)) {
              ret = PTR_ERR(vma);
mm/mmap.c +0 -0
index 57fd5ab2abe7..d4717e34a60d 100644

mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 2138

Quoted 6 lines
mm/mmap.c +1 -0
@@ -2133,6 +2133,7 @@ struct vm_area_struct *_install_special_mapping(
      unsigned long addr, unsigned long len,
      unsigned long vm_flags, const struct vm_special_mapping *spec)
 {
+     update_seal_exec_system_mappings(&vm_flags);
      return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
                                      &special_mapping_vmops);

If you were to return a flag, you could change the vm_flags argument to vm_flags | mseal_flag()

passing pointer seems to be the most efficient way.

Thanks
-Jeff

maintainer
#7 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

Quoted 10 lines · Jeff Xu
Quoted 8 lines · Liam R. Howlett
Quoted 7 lines
Documentation/admin-guide/kernel-parameters.txt +6 -0
+     exec.seal_system_mappings = [KNL]
+                     Format: { never | always }
+                     Seal system mappings: vdso, vvar, sigpage, uprobes,
+                     vsyscall.
+                     This overwrites KCONFIG CONFIG_SEAL_SYSTEM_MAPPINGS_*
+                     - 'never':  never seal system mappings.
Quoted 4 lines
Quoted 2 lines

Not true, uprobes are sealed when 'never' is selected.

Thanks. I forgot to uprobes from the description in Kconfig and kernel-parameters.txt, will update.

Jeff, I am sorry for confusion.

No need to make uprobes "special" and complicate the logic/documentation.

I just meant that, unlike vdso, it is always safe/good to mseal the "[uprobes]" vma, regardless of config/boot options.

Please do what you think is right, I am fine either way.

Oleg.

author
#8 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

Quoted 17 lines · Oleg Nesterov
Quoted 10 lines · Jeff Xu
Quoted 8 lines · Liam R. Howlett
Quoted 7 lines
Documentation/admin-guide/kernel-parameters.txt +6 -0
+     exec.seal_system_mappings = [KNL]
+                     Format: { never | always }
+                     Seal system mappings: vdso, vvar, sigpage, uprobes,
+                     vsyscall.
+                     This overwrites KCONFIG CONFIG_SEAL_SYSTEM_MAPPINGS_*
+                     - 'never':  never seal system mappings.
Quoted 4 lines
Quoted 2 lines

Not true, uprobes are sealed when 'never' is selected.

Thanks. I forgot to uprobes from the description in Kconfig and kernel-parameters.txt, will update.

Quoted 9 lines

Jeff, I am sorry for confusion.

No need to make uprobes "special" and complicate the logic/documentation.

I just meant that, unlike vdso, it is always safe/good to mseal the "[uprobes]" vma, regardless of config/boot options.

Please do what you think is right, I am fine either way.

OK, in that case, V1 is a better approach, using the same config to control all system mappings.
I will send V1 to revert that.

Thanks for clarifying.
-Jeff

Quoted 2 lines

Oleg.

#9 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

In reply to Jeff Xu
Quoted 1 line · Liam R. Howlett

...

Quoted 36 lines
Quoted 25 lines
Quoted 25 lines
arch/x86/entry/vsyscall/vsyscall_64.c +7 -2
index 2fb7d53cf333..20a3000550d2 100644
@@ -32,6 +32,7 @@
 #include <linux/mm_types.h>
 #include <linux/syscalls.h>
 #include <linux/ratelimit.h>
+#include <linux/fs.h>
 
 #include <asm/vsyscall.h>
 #include <asm/unistd.h>
@@ -366,8 +367,12 @@ void __init map_vsyscall(void)
              set_vsyscall_pgtable_user_bits(swapper_pg_dir);
      }
 
-     if (vsyscall_mode == XONLY)
-             vm_flags_init(&gate_vma, VM_EXEC);
+     if (vsyscall_mode == XONLY) {
+             unsigned long vm_flags = VM_EXEC;
+
+             update_seal_exec_system_mappings(&vm_flags);
+             vm_flags_init(&gate_vma, vm_flags);
+     }
 
Quoted 11 lines

fs/exec.c

Quoted 6 lines
arch/x86/entry/vsyscall/vsyscall_64.c +0 -0
      BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE) !=
                   (unsigned long)VSYSCALL_ADDR);
fs/exec.c +0 -0
index 77364806b48d..5030879cda47 100644

Does it make sense for this to live in exec? Couldn't you put it in the mm/mseal.c file? It's vma flags for mappings and you've put it in fs/exec?

Quoted 2 lines

If you are referring to utilities related to kernel cmdline, they should be in this file.

You created a wrapper for the command line, but then included the user in this file as well.

hugetlbfs reads the command line as well, in cmdline_parse_hugetlb_cma. That parser lives with the rest of the hugetlb code in hugetlb.c

I think this has to do with your view as this is an exec thing, where I think it's an mm thing. My arguments are that you are directly adding flags to vmas and it's dealing with mseal which has memory in the name with the file living in the mm/ directory. If I wanted to know what's using mseal, I'd start there and totally miss what you are adding here.

Besides applying a vma flag to exec mappings, why do you feel like it belongs in fs/ ?

Quoted 57 lines
Quoted 48 lines
Quoted 48 lines
fs/exec.c +36 -0
@@ -68,6 +68,7 @@
 #include <linux/user_events.h>
 #include <linux/rseq.h>
 #include <linux/ksm.h>
+#include <linux/fs_parser.h>
 
 #include <linux/uaccess.h>
 #include <asm/mmu_context.h>
@@ -2159,3 +2160,55 @@ fs_initcall(init_fs_exec_sysctls);
 #ifdef CONFIG_EXEC_KUNIT_TEST
 #include "tests/exec_kunit.c"
 #endif
+
+#ifdef CONFIG_64BIT
+/*
+ * Kernel cmdline overwrite for CONFIG_SEAL_SYSTEM_MAPPINGS_X
+ */
+enum seal_system_mappings_type {
+     SEAL_SYSTEM_MAPPINGS_NEVER,
+     SEAL_SYSTEM_MAPPINGS_ALWAYS
+};
+
+static enum seal_system_mappings_type seal_system_mappings __ro_after_init =
+     IS_ENABLED(CONFIG_SEAL_SYSTEM_MAPPINGS_ALWAYS) ? SEAL_SYSTEM_MAPPINGS_ALWAYS :
+     SEAL_SYSTEM_MAPPINGS_NEVER;
+
+static const struct constant_table value_table_sys_mapping[] __initconst = {
+     { "never", SEAL_SYSTEM_MAPPINGS_NEVER},
+     { "always", SEAL_SYSTEM_MAPPINGS_ALWAYS},
+     { }
+};
+
+static int __init early_seal_system_mappings_override(char *buf)
+{
+     if (!buf)
+             return -EINVAL;
+
+     seal_system_mappings = lookup_constant(value_table_sys_mapping,
+                     buf, seal_system_mappings);
+
+     return 0;
+}
+
+early_param("exec.seal_system_mappings", early_seal_system_mappings_override);
+
+static bool seal_system_mappings_enabled(void)
+{
Quoted 9 lines

fs/exec.c fs_initcall(init_fs_exec_sysctls); · line 2202

Quoted 5 lines
fs/exec.c +5 -0
+     if (seal_system_mappings == SEAL_SYSTEM_MAPPINGS_ALWAYS)
+             return true;
+
+     return false;
+}

This function seems unnecessary, it is called from another 3-4 line function only.

Quoted 1 line

It is more readable this way.

It really isn't - you don't have to cram everything into one if statement.

Quoted 20 lines
Quoted 8 lines

fs/exec.c fs_initcall(init_fs_exec_sysctls); · line 2206

Quoted 5 lines
fs/exec.c +4 -0
+
+void update_seal_exec_system_mappings(unsigned long *vm_flags)
+{
+     if (!(*vm_flags & VM_SEALED) && seal_system_mappings_enabled())

Why !(*vm_flags & VM_SEALED) here?

If vm_flags is already sealed, then there is no need to check seal_system_mappings_enabled.

Quoted 9 lines

fs/exec.c fs_initcall(init_fs_exec_sysctls); · line 2209

Quoted 3 lines
fs/exec.c +3 -0
+             *vm_flags |= VM_SEALED;
+
+}

Instead of passing a pointer around and checking enabled, why don't you have a function that just returns the VM_SEALED or 0 and just or it into the flags? This seems very heavy for what it does, why did you do it this way?

Quoted 1 line

Why is that heavy ? passing a pointer for updating variables is natural.

What you have here will pass a pointer to a function, dereference the pointer and potentially call another function to check seal system mappings is enabled to dereference a pointer to or a bit. I would hope the compiler improves this, but that depends on the compiler.

What you *could* do is call a function to check the seal system mappings is enabled and return the bit to set. The caller would just need to or the bit on value without dereferencing the pointer. We may be able to even inline the function you call.

So I am suggesting removing two dereferences and a function call (maybe two?) while making the code more compact and readable. This is what I mean by heavy.

Quoted 8 lines
Quoted 5 lines

The name is also very long and a bit odd, it could be used for other reasons, but you have _system_mappings on the end, and you use seal but it's mseal (or vm_seal)? Would mseal_flag() work?

It could be longer :-)
it means update_sealing_flag_for_executable_system_mappings.
mseal_flag is too short and not descriptive.

mseal_exec_flags() ?

Quoted 13 lines
Quoted 13 lines

include/linux/fs.h

Quoted 10 lines
fs/exec.c +5 -0
+#else
+void update_seal_exec_system_mappings(unsigned long *vm_flags)
+{
+}
+#endif /* CONFIG_64BIT */
include/linux/fs.h +0 -0
index 42444ec95c9b..6e44aca4b24b 100644

Again, I don't understand why fs.h is the place for mseal definitions?

Quoted 2 lines

include/linux/fs.h contains other exec related function signatures. So it is better to keep them at the same header.

I don't see this as an exec related function, it's changing vma flags. Besides this new function, the fs/exec.c deals with the stack flags. Well, this header has VM_MAY{READ/WRITE/EXEC} for nommu - but I don't think we can really count those here and they are used to translate things anyways.

Recently, we have been trying to modularize the vma code for better testing. Spreading the code across many files will make that testing harder to do in isolation.

Quoted 30 lines
Quoted 9 lines

include/linux/fs.h ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos); · line 3082

Quoted 6 lines
include/linux/fs.h +1 -0
@@ -3079,6 +3079,7 @@ ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos);
 extern ssize_t kernel_write(struct file *, const void *, size_t, loff_t *);
 extern ssize_t __kernel_write(struct file *, const void *, size_t, loff_t *);
 extern struct file * open_exec(const char *);
+extern void update_seal_exec_system_mappings(unsigned long *vm_flags);

We are dropping extern where possible now.

extern can be dropped, it appears not causing link error.

Quoted 20 lines
Quoted 8 lines
include/linux/fs.h +0 -0
 /* fs/dcache.c -- generic fs support functions */
 extern bool is_subdir(struct dentry *, struct dentry *);
kernel/events/uprobes.c +0 -0
index c47a0bf25e58..e9876fae8887 100644

kernel/events/uprobes.c static int xol_add_vma(struct mm_struct *mm, struct xol_area *area) · line 1510

Quoted 6 lines
kernel/events/uprobes.c +1 -1
@@ -1506,7 +1506,7 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
      }
 
      vma = _install_special_mapping(mm, area->vaddr, PAGE_SIZE,
-                             VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO,
+                             VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO|VM_SEALED,
                              &xol_mapping);

Changing all uprobes seems like something that should probably be mentioned more than just the note at the end of the change log, even if you think it won't have any impact. The note is even hidden at the end of a paragraph.

Quoted 7 lines
Quoted 6 lines

I would go as far as splitting this patch out as its own so that the subject line specifies that all uprobes will be VM_SEALED now.

Maybe it's fine but maybe it isn't and you've buried it so that it will be missed by virtually everyone.

I will add "It is sealed from creation." in the above "uprobe" section.

That in inadequate for what you are changing. Doing it this way risks breaking things because no one will notice.

Quoted 19 lines
Quoted 19 lines
Quoted 9 lines
kernel/events/uprobes.c +0 -0
      if (IS_ERR(vma)) {
              ret = PTR_ERR(vma);
mm/mmap.c +0 -0
index 57fd5ab2abe7..d4717e34a60d 100644

mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 2138

Quoted 6 lines
mm/mmap.c +1 -0
@@ -2133,6 +2133,7 @@ struct vm_area_struct *_install_special_mapping(
      unsigned long addr, unsigned long len,
      unsigned long vm_flags, const struct vm_special_mapping *spec)
 {
+     update_seal_exec_system_mappings(&vm_flags);
      return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
                                      &special_mapping_vmops);

If you were to return a flag, you could change the vm_flags argument to vm_flags | mseal_flag()

Quoted 1 line

passing pointer seems to be the most efficient way.

I disagree. Here is the godbolt.org output for gcc x86-64 14.2 of your code (with some added #defines to make it compile)

seal_system_mappings:
.long 1
seal_system_mappings_enabled:

        push    rbp
        mov     rbp, rsp
        mov     eax, DWORD PTR seal_system_mappings[rip]
        cmp     eax, 1
        jne     .L2
        mov     eax, 1
        jmp     .L3

.L2:
mov eax, 0
.L3:
pop rbp
ret
update_seal_exec_system_mappings:

        push    rbp
        mov     rbp, rsp
        sub     rsp, 8
        mov     QWORD PTR [rbp-8], rdi
        mov     rax, QWORD PTR [rbp-8]
        mov     rax, QWORD PTR [rax]
        and     eax, 2
        test    rax, rax
        jne     .L6
        call    seal_system_mappings_enabled
        test    al, al
        je      .L6
        mov     rax, QWORD PTR [rbp-8]
        mov     rax, QWORD PTR [rax]
        or      rax, 2
        mov     rdx, rax
        mov     rax, QWORD PTR [rbp-8]
        mov     QWORD PTR [rax], rdx

.L6:
nop
leave
ret
main:

        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     QWORD PTR [rbp-8], 0
        lea     rax, [rbp-8]
        mov     rdi, rax
        call    update_seal_exec_system_mappings
        mov     rax, QWORD PTR [rbp-8]

leave
ret

----- 48 lines -----
Here is what I am suggesting to do with replacing the passing of a pointer with a concise "vm_flags | mseal_exec_flags()" (with the same added #defines to make it compile)

seal_system_mappings:
.long 1
mseal_exec_flags:

        push    rbp
        mov     rbp, rsp
        mov     eax, DWORD PTR seal_system_mappings[rip]
        cmp     eax, 1
        jne     .L2
        mov     eax, 2
        jmp     .L3

.L2:
mov eax, 0
.L3:
pop rbp
ret
main:

        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     QWORD PTR [rbp-8], 0
        call    mseal_exec_flags
        mov     edx, eax
        mov     rax, QWORD PTR [rbp-8]
        or      eax, edx

leave
ret

----- 26 lines -----

So as you can see, there are less instructions in my version; there are 47.92% less lines of assembly.

Your vsyscall version is even worse.

It is very tiresome to do code inspections and be told flat-out "no" repetitively.

Liam

author
#10 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

Hi Liam

Quoted 13 lines · Liam R. Howlett
Quoted 6 lines
Quoted 4 lines

Does it make sense for this to live in exec? Couldn't you put it in the mm/mseal.c file? It's vma flags for mappings and you've put it in fs/exec?

If you are referring to utilities related to kernel cmdline, they should be in this file.

You created a wrapper for the command line, but then included the user in this file as well.

hugetlbfs reads the command line as well, in cmdline_parse_hugetlb_cma. That parser lives with the rest of the hugetlb code in hugetlb.c

Quoted 9 lines

I think this has to do with your view as this is an exec thing, where I think it's an mm thing. My arguments are that you are directly adding flags to vmas and it's dealing with mseal which has memory in the name with the file living in the mm/ directory. If I wanted to know what's using mseal, I'd start there and totally miss what you are adding here.

Besides applying a vma flag to exec mappings, why do you feel like it belongs in fs/ ?

The vdso/vvar/stack/heap alike are type of mappings belonging to processes, and are created during execve() syscall which is in fs/exec.c.

mm/mseal.c provides core memory sealing functionality and exec.c uses it. IMO, it is better to keep the provider (mm/mseal.c) and consumer (executable) separate.

To make modulization better, I can do below adjustment:
if (seal_system_mapping_enabled()) <-- implemented by fs/exec.c
add_vm_sealed() <- keep in include/linux/mm.h

However, if you have a strong opinion on this, I could move the parsing logic to mm/mseal.

Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

+void update_seal_exec_system_mappings(unsigned long *

The name is also very long and a bit odd, it could be used for other reasons, but you have _system_mappings on the end, and you use seal but it's mseal (or vm_seal)? Would mseal_flag() work?

It could be longer :-)
it means update_sealing_flag_for_executable_system_mappings.
mseal_flag is too short and not descriptive.

mseal_exec_flags() ?

It needs to be more descriptive because there are also stacks and heaps to be sealed. I suggest to use below name to make it shorter:

if (seal_system_mapping_enabled())
add_vm_sealed()

Quoted 99 lines
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
mm/mmap.c +0 -0
index 57fd5ab2abe7..d4717e34a60d 100644
@@ -2133,6 +2133,7 @@ struct vm_area_struct *_install_special_mapping(
      unsigned long addr, unsigned long len,
Quoted 9 lines

mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 2138

Quoted 5 lines
mm/mmap.c +1 -0
      unsigned long vm_flags, const struct vm_special_mapping *spec)
 {
+     update_seal_exec_system_mappings(&vm_flags);
      return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
                                      &special_mapping_vmops);

If you were to return a flag, you could change the vm_flags argument to vm_flags | mseal_flag()

Quoted 1 line

passing pointer seems to be the most efficient way.

I disagree. Here is the godbolt.org output for gcc x86-64 14.2 of your code (with some added #defines to make it compile)

seal_system_mappings:
.long 1
seal_system_mappings_enabled:

        push    rbp
        mov     rbp, rsp
        mov     eax, DWORD PTR seal_system_mappings[rip]
        cmp     eax, 1
        jne     .L2
        mov     eax, 1
        jmp     .L3

.L2:
mov eax, 0
.L3:
pop rbp
ret
update_seal_exec_system_mappings:

        push    rbp
        mov     rbp, rsp
        sub     rsp, 8
        mov     QWORD PTR [rbp-8], rdi
        mov     rax, QWORD PTR [rbp-8]
        mov     rax, QWORD PTR [rax]
        and     eax, 2
        test    rax, rax
        jne     .L6
        call    seal_system_mappings_enabled
        test    al, al
        je      .L6
        mov     rax, QWORD PTR [rbp-8]
        mov     rax, QWORD PTR [rax]
        or      rax, 2
        mov     rdx, rax
        mov     rax, QWORD PTR [rbp-8]
        mov     QWORD PTR [rax], rdx

.L6:
nop
leave
ret
main:

        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     QWORD PTR [rbp-8], 0
        lea     rax, [rbp-8]
        mov     rdi, rax
        call    update_seal_exec_system_mappings
        mov     rax, QWORD PTR [rbp-8]

leave
ret

----- 48 lines -----
Here is what I am suggesting to do with replacing the passing of a pointer with a concise "vm_flags | mseal_exec_flags()" (with the same added #defines to make it compile)

seal_system_mappings:
.long 1
mseal_exec_flags:

        push    rbp
        mov     rbp, rsp
        mov     eax, DWORD PTR seal_system_mappings[rip]
        cmp     eax, 1
        jne     .L2
        mov     eax, 2
        jmp     .L3

.L2:
mov eax, 0
.L3:
pop rbp
ret
main:

        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     QWORD PTR [rbp-8], 0
        call    mseal_exec_flags
        mov     edx, eax
        mov     rax, QWORD PTR [rbp-8]
        or      eax, edx
Quoted 8 lines

leave
ret

----- 26 lines -----

So as you can see, there are less instructions in my version; there are 47.92% less lines of assembly.

vm_flags already run out of space in 32 bit, sooner or later we will need to change that to *** a struct ***, passing address will be becoming necessary with struct. Since this is not a performance sensitive code path, 3 or 4 times during execve(), I think it would be good to start here.

-Jeff

#11 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

Quoted 26 lines · Jeff Xu

Hi Liam

Quoted 13 lines · Liam R. Howlett
Quoted 6 lines
Quoted 4 lines

Does it make sense for this to live in exec? Couldn't you put it in the mm/mseal.c file? It's vma flags for mappings and you've put it in fs/exec?

If you are referring to utilities related to kernel cmdline, they should be in this file.

You created a wrapper for the command line, but then included the user in this file as well.

hugetlbfs reads the command line as well, in cmdline_parse_hugetlb_cma. That parser lives with the rest of the hugetlb code in hugetlb.c

Quoted 9 lines

I think this has to do with your view as this is an exec thing, where I think it's an mm thing. My arguments are that you are directly adding flags to vmas and it's dealing with mseal which has memory in the name with the file living in the mm/ directory. If I wanted to know what's using mseal, I'd start there and totally miss what you are adding here.

Besides applying a vma flag to exec mappings, why do you feel like it belongs in fs/ ?

Quoted 7 lines

The vdso/vvar/stack/heap alike are type of mappings belonging to processes, and are created during execve() syscall which is in fs/exec.c.

mm/mseal.c provides core memory sealing functionality and exec.c uses it. IMO, it is better to keep the provider (mm/mseal.c) and consumer (executable) separate.

IMO, mseal should not be sitting in its own file since it's just a vma flag. The entire feature is to do with vma and vma flag checking. It should at least be globbed together in one file, as much as possible.

The way this and the help text is written means there is only the VM_SEALED flag that links this to the actual feature name. And with all the abstractions, that flag is 2-3 functions deep across 2-3 files.

In fact, there isn't a dependency for enabling mseal in the kconfig? Isn't that needed?

Please fix this.

Quoted 7 lines

To make modulization better, I can do below adjustment:
if (seal_system_mapping_enabled()) <-- implemented by fs/exec.c
add_vm_sealed() <- keep in include/linux/mm.h

However, if you have a strong opinion on this, I could move the parsing logic to mm/mseal.

Yes, please move the parsing logic together with the other mseal code.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

+void update_seal_exec_system_mappings(unsigned long *

The name is also very long and a bit odd, it could be used for other reasons, but you have _system_mappings on the end, and you use seal but it's mseal (or vm_seal)? Would mseal_flag() work?

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

It could be longer :-)
it means update_sealing_flag_for_executable_system_mappings.
mseal_flag is too short and not descriptive.

mseal_exec_flags() ?

It needs to be more descriptive because there are also stacks and heaps to be sealed.

Stopping vma modifications to vmas that exist specifically to be dynamic does not sound wise.

Quoted 4 lines

I suggest to use below name to make it shorter:

if (seal_system_mapping_enabled())
add_vm_sealed()

I am not sure what one you are renaming or what these functions would do. I think you need to look at your overall design and try to fit it into what exists instead of putting a call in a wrapper function (_install_special_mapping) to alter an argument.

Quoted 105 lines
Quoted 105 lines
Quoted 16 lines
Quoted 16 lines

mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 2138

Quoted 12 lines
mm/mmap.c +1 -0
index 57fd5ab2abe7..d4717e34a60d 100644
@@ -2133,6 +2133,7 @@ struct vm_area_struct *_install_special_mapping(
      unsigned long addr, unsigned long len,
      unsigned long vm_flags, const struct vm_special_mapping *spec)
 {
+     update_seal_exec_system_mappings(&vm_flags);
      return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
                                      &special_mapping_vmops);

If you were to return a flag, you could change the vm_flags argument to vm_flags | mseal_flag()

Quoted 1 line

passing pointer seems to be the most efficient way.

I disagree. Here is the godbolt.org output for gcc x86-64 14.2 of your code (with some added #defines to make it compile)

seal_system_mappings:
.long 1
seal_system_mappings_enabled:

        push    rbp
        mov     rbp, rsp
        mov     eax, DWORD PTR seal_system_mappings[rip]
        cmp     eax, 1
        jne     .L2
        mov     eax, 1
        jmp     .L3

.L2:
mov eax, 0
.L3:
pop rbp
ret
update_seal_exec_system_mappings:

        push    rbp
        mov     rbp, rsp
        sub     rsp, 8
        mov     QWORD PTR [rbp-8], rdi
        mov     rax, QWORD PTR [rbp-8]
        mov     rax, QWORD PTR [rax]
        and     eax, 2
        test    rax, rax
        jne     .L6
        call    seal_system_mappings_enabled
        test    al, al
        je      .L6
        mov     rax, QWORD PTR [rbp-8]
        mov     rax, QWORD PTR [rax]
        or      rax, 2
        mov     rdx, rax
        mov     rax, QWORD PTR [rbp-8]
        mov     QWORD PTR [rax], rdx

.L6:
nop
leave
ret
main:

        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     QWORD PTR [rbp-8], 0
        lea     rax, [rbp-8]
        mov     rdi, rax
        call    update_seal_exec_system_mappings
        mov     rax, QWORD PTR [rbp-8]

leave
ret

----- 48 lines -----
Here is what I am suggesting to do with replacing the passing of a pointer with a concise "vm_flags | mseal_exec_flags()" (with the same added #defines to make it compile)

seal_system_mappings:
.long 1
mseal_exec_flags:

        push    rbp
        mov     rbp, rsp
        mov     eax, DWORD PTR seal_system_mappings[rip]
        cmp     eax, 1
        jne     .L2
        mov     eax, 2
        jmp     .L3

.L2:
mov eax, 0
.L3:
pop rbp
ret
main:

        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     QWORD PTR [rbp-8], 0
        call    mseal_exec_flags
        mov     edx, eax
        mov     rax, QWORD PTR [rbp-8]
        or      eax, edx

leave
ret

----- 26 lines -----

Quoted 8 lines
Quoted 3 lines

So as you can see, there are less instructions in my version; there are 47.92% less lines of assembly.

vm_flags already run out of space in 32 bit, sooner or later we will need to change that to *** a struct ***, passing address will be becoming necessary with struct. Since this is not a performance sensitive code path, 3 or 4 times during execve(), I think it would be good to start here.

No.

I have pointed out why this is not 'the most efficient way' and you are now switching your argument to 'this will be needed in the future'.

Please fix this.

Liam

author
#12 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

Quoted 9 lines · Liam R. Howlett
Quoted 6 lines

To make modulization better, I can do below adjustment:
if (seal_system_mapping_enabled()) <-- implemented by fs/exec.c
add_vm_sealed() <- keep in include/linux/mm.h

However, if you have a strong opinion on this, I could move the parsing logic to mm/mseal.

Yes, please move the parsing logic together with the other mseal code.

Sure

Quoted 6 lines

I am not sure what one you are renaming or what these functions would do. I think you need to look at your overall design and try to fit it into what exists instead of putting a call in a wrapper function (_install_special_mapping) to alter an argument.

Please see V3.

Quoted 114 lines
Quoted 105 lines
Quoted 105 lines
Quoted 16 lines
Quoted 16 lines

mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 2138

Quoted 12 lines
mm/mmap.c +1 -0
index 57fd5ab2abe7..d4717e34a60d 100644
@@ -2133,6 +2133,7 @@ struct vm_area_struct *_install_special_mapping(
      unsigned long addr, unsigned long len,
      unsigned long vm_flags, const struct vm_special_mapping *spec)
 {
+     update_seal_exec_system_mappings(&vm_flags);
      return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
                                      &special_mapping_vmops);

If you were to return a flag, you could change the vm_flags argument to vm_flags | mseal_flag()

Quoted 1 line

passing pointer seems to be the most efficient way.

I disagree. Here is the godbolt.org output for gcc x86-64 14.2 of your code (with some added #defines to make it compile)

seal_system_mappings:
.long 1
seal_system_mappings_enabled:

        push    rbp
        mov     rbp, rsp
        mov     eax, DWORD PTR seal_system_mappings[rip]
        cmp     eax, 1
        jne     .L2
        mov     eax, 1
        jmp     .L3

.L2:
mov eax, 0
.L3:
pop rbp
ret
update_seal_exec_system_mappings:

        push    rbp
        mov     rbp, rsp
        sub     rsp, 8
        mov     QWORD PTR [rbp-8], rdi
        mov     rax, QWORD PTR [rbp-8]
        mov     rax, QWORD PTR [rax]
        and     eax, 2
        test    rax, rax
        jne     .L6
        call    seal_system_mappings_enabled
        test    al, al
        je      .L6
        mov     rax, QWORD PTR [rbp-8]
        mov     rax, QWORD PTR [rax]
        or      rax, 2
        mov     rdx, rax
        mov     rax, QWORD PTR [rbp-8]
        mov     QWORD PTR [rax], rdx

.L6:
nop
leave
ret
main:

        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     QWORD PTR [rbp-8], 0
        lea     rax, [rbp-8]
        mov     rdi, rax
        call    update_seal_exec_system_mappings
        mov     rax, QWORD PTR [rbp-8]

leave
ret

----- 48 lines -----
Here is what I am suggesting to do with replacing the passing of a pointer with a concise "vm_flags | mseal_exec_flags()" (with the same added #defines to make it compile)

seal_system_mappings:
.long 1
mseal_exec_flags:

        push    rbp
        mov     rbp, rsp
        mov     eax, DWORD PTR seal_system_mappings[rip]
        cmp     eax, 1
        jne     .L2
        mov     eax, 2
        jmp     .L3

.L2:
mov eax, 0
.L3:
pop rbp
ret
main:

        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     QWORD PTR [rbp-8], 0
        call    mseal_exec_flags
        mov     edx, eax
        mov     rax, QWORD PTR [rbp-8]
        or      eax, edx

leave
ret

----- 26 lines -----

Quoted 8 lines
Quoted 3 lines

So as you can see, there are less instructions in my version; there are 47.92% less lines of assembly.

vm_flags already run out of space in 32 bit, sooner or later we will need to change that to *** a struct ***, passing address will be becoming necessary with struct. Since this is not a performance sensitive code path, 3 or 4 times during execve(), I think it would be good to start here.

Quoted 7 lines

No.

I have pointed out why this is not 'the most efficient way' and you are now switching your argument to 'this will be needed in the future'.

Please fix this.

Sure, updated in V3

#13 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

In reply to [email protected]
kernel test robot noticed the following build errors:

Hi,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on brauner-vfs/vfs.all perf-tools-next/perf-tools-next tip/perf/core perf-tools/perf-tools linus/master v6.12-rc3 next-20241017] [cannot apply to kees/for-next/execve kees/for-next/pstore kees/for-next/kspp tip/x86/asm acme/perf/core]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/jeffxu-chromium-org/exec-seal-system-mappings/20241015-055145
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20241014215022.68530-2-jeffxu%40google.com
patch subject: [RFC PATCH v2 1/1] exec: seal system mappings
config: i386-defconfig (https://download.01.org/0day-ci/archive/20241018/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241018/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> kernel/events/uprobes.c:1507:42: error: use of undeclared identifier 'VM_SEALED'

1507 | VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO|VM_SEALED,
| ^
1 error generated.

vim +/VM_SEALED +1507 kernel/events/uprobes.c

1481
1482 /* Slot allocation for XOL */
1483 static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
1484 {
1485 struct vm_area_struct *vma;
1486 int ret;
1487
1488 if (mmap_write_lock_killable(mm))
1489 return -EINTR;
1490
1491 if (mm->uprobes_state.xol_area) {
1492 ret = -EALREADY;
1493 goto fail;
1494 }
1495
1496 if (!area->vaddr) {
1497 /* Try to map as high as possible, this is only a hint. */
1498 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE,
1499 PAGE_SIZE, 0, 0);
1500 if (IS_ERR_VALUE(area->vaddr)) {
1501 ret = area->vaddr;
1502 goto fail;
1503 }
1504 }
1505
1506 vma = _install_special_mapping(mm, area->vaddr, PAGE_SIZE,

> 1507					VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO|VM_SEALED,

1508 &xol_mapping);
1509 if (IS_ERR(vma)) {
1510 ret = PTR_ERR(vma);
1511 goto fail;
1512 }
1513
1514 ret = 0;
1515 /* pairs with get_xol_area() */
1516 smp_store_release(&mm->uprobes_state.xol_area, area); /* ^^^ */
1517 fail:
1518 mmap_write_unlock(mm);
1519
1520 return ret;
1521 }
1522

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#14 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

In reply to [email protected]
kernel test robot noticed the following build errors:

Hi,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on brauner-vfs/vfs.all perf-tools-next/perf-tools-next tip/perf/core perf-tools/perf-tools linus/master v6.12-rc3 next-20241017] [cannot apply to kees/for-next/execve kees/for-next/pstore kees/for-next/kspp tip/x86/asm acme/perf/core]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/jeffxu-chromium-org/exec-seal-system-mappings/20241015-055145
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20241014215022.68530-2-jeffxu%40google.com
patch subject: [RFC PATCH v2 1/1] exec: seal system mappings
config: i386-allyesconfig (https://download.01.org/0day-ci/archive/20241018/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241018/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

kernel/events/uprobes.c: In function 'xol_add_vma':

>> kernel/events/uprobes.c:1507:70: error: 'VM_SEALED' undeclared (first use in this function); did you mean 'VM_SHARED'?
    1507 |                                 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO|VM_SEALED,
         |                                                                      ^~~~~~~~~
         |                                                                      VM_SHARED

kernel/events/uprobes.c:1507:70: note: each undeclared identifier is reported only once for each function it appears in

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for GET_FREE_REGION Depends on [n]: SPARSEMEM [=n]
Selected by [y]:
- RESOURCE_KUNIT_TEST [=y] && RUNTIME_TESTING_MENU [=y] && KUNIT [=y]

vim +1507 kernel/events/uprobes.c

1481
1482 /* Slot allocation for XOL */
1483 static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
1484 {
1485 struct vm_area_struct *vma;
1486 int ret;
1487
1488 if (mmap_write_lock_killable(mm))
1489 return -EINTR;
1490
1491 if (mm->uprobes_state.xol_area) {
1492 ret = -EALREADY;
1493 goto fail;
1494 }
1495
1496 if (!area->vaddr) {
1497 /* Try to map as high as possible, this is only a hint. */
1498 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE,
1499 PAGE_SIZE, 0, 0);
1500 if (IS_ERR_VALUE(area->vaddr)) {
1501 ret = area->vaddr;
1502 goto fail;
1503 }
1504 }
1505
1506 vma = _install_special_mapping(mm, area->vaddr, PAGE_SIZE,

> 1507					VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO|VM_SEALED,

1508 &xol_mapping);
1509 if (IS_ERR(vma)) {
1510 ret = PTR_ERR(vma);
1511 goto fail;
1512 }
1513
1514 ret = 0;
1515 /* pairs with get_xol_area() */
1516 smp_store_release(&mm->uprobes_state.xol_area, area); /* ^^^ */
1517 fail:
1518 mmap_write_unlock(mm);
1519
1520 return ret;
1521 }
1522

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#15 lore raw

Re: [RFC PATCH v2 1/1] exec: seal system mappings

Quoted 21 lines · kernel test robot

Hi,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on brauner-vfs/vfs.all perf-tools-next/perf-tools-next tip/perf/core perf-tools/perf-tools linus/master v6.12-rc3 next-20241017] [cannot apply to kees/for-next/execve kees/for-next/pstore kees/for-next/kspp tip/x86/asm acme/perf/core]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/jeffxu-chromium-org/exec-seal-system-mappings/20241015-055145
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20241014215022.68530-2-jeffxu%40google.com
patch subject: [RFC PATCH v2 1/1] exec: seal system mappings
config: i386-allyesconfig (https://download.01.org/0day-ci/archive/20241018/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241018/[email protected]/reproduce)
Quoted 5 lines

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

This is fixed in V3.

Quoted 9 lines

All errors (new ones prefixed by >>):

kernel/events/uprobes.c: In function 'xol_add_vma':

Quoted 1 line
Quoted 1 line

kernel/events/uprobes.c:1507:70: error: 'VM_SEALED' undeclared (first use in this function); did you mean 'VM_SHARED'?

    1507 |                                 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO|VM_SEALED,
         |                                                                      ^~~~~~~~~
         |                                                                      VM_SHARED

kernel/events/uprobes.c:1507:70: note: each undeclared identifier is reported only once for each function it appears in

V3 revert to V1's behavior for uprobes, so no longer have changes in uprobes.c

#16 lore raw
In reply to [email protected]
Quoted 4 lines · * [email protected] <[email protected]> [241014 17:50]:

From: Jeff Xu <[email protected]>

Seal vdso, vvar, sigpage, uprobes and vsyscall.

Quoted 8 lines

Those mappings are readonly or executable only, sealing can protect them from ever changing during the life time of the process. For complete descriptions of memory sealing, please see mseal.rst [1].

System mappings such as vdso, vvar, and sigpage (for arm) are generated by the kernel during program initialization. These mappings are designated as non-writable, and sealing them will prevent them from ever becoming writeable.

^ or ever removed.

This is a pretty big deal. Platforms are trying to make it so that vdso is the fast path, but if they are removed then things stop using them and it's not a problem. This description is robbing them of the information they need to know that, and it's not in your change log either.

I had said before that you need to be clear about the inability to remove the mappings that are sealed, as the description above heavily implies that it is only stopping them from becoming writeable.

Quoted 25 lines

Unlike the aforementioned mappings, the uprobe mapping is not established during program startup. However, its lifetime is the same as the process's lifetime [2], thus sealable.

The vdso, vvar, sigpage, and uprobe mappings all invoke the _install_special_mapping() function. As no other mappings utilize this function, it is logical to incorporate sealing logic within _install_special_mapping(). This approach avoids the necessity of modifying code across various architecture-specific implementations.

The vsyscall mapping, which has its own initialization function, is sealed in the XONLY case, it seems to be the most common and secure case of using vsyscall.

It is important to note that the CHECKPOINT_RESTORE feature (CRIU) may alter the mapping of vdso, vvar, and sigpage during restore operations. Consequently, this feature cannot be universally enabled across all systems. To address this, a kernel configuration option has been introduced to enable or disable this functionality. Note, uprobe is always sealed and not controlled by this kernel configuration.

I tested CONFIG_SEAL_SYSTEM_MAPPINGS_ALWAYS with ChromeOS, which doesn’t use CHECKPOINT_RESTORE.

Quoted 7 lines

[1] Documentation/userspace-api/mseal.rst
[2] https://lore.kernel.org/all/CABi2SkU9BRUnqf70-nksuMCQ+yyiWjo3fM4XkRkL-NrCZxYAyg@mail.gmail.com/

History:
V2:
Seal uprobe always (Oleg Nesterov)
Update comments and description (Randy Dunlap, Liam R.Howlett, Oleg Nesterov)

The only update to the comment I see is the pointer to mseal.rst for a complete description?

Quoted 20 lines

Rebase to linux_main

V1:
https://lore.kernel.org/all/[email protected]/

Jeff Xu (1):
exec: seal system mappings

 .../admin-guide/kernel-parameters.txt         | 10 ++++
 arch/x86/entry/vsyscall/vsyscall_64.c         |  9 +++-
 fs/exec.c                                     | 53 +++++++++++++++++++
 include/linux/fs.h                            |  1 +
 kernel/events/uprobes.c                       |  2 +-
 mm/mmap.c                                     |  1 +
 security/Kconfig                              | 26 +++++++++
 7 files changed, 99 insertions(+), 3 deletions(-)
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
author
#17 lore raw
Quoted 22 lines · Liam R. Howlett
Quoted 4 lines · * [email protected] <[email protected]> [241014 17:50]:

From: Jeff Xu <[email protected]>

Seal vdso, vvar, sigpage, uprobes and vsyscall.

Quoted 8 lines

Those mappings are readonly or executable only, sealing can protect them from ever changing during the life time of the process. For complete descriptions of memory sealing, please see mseal.rst [1].

System mappings such as vdso, vvar, and sigpage (for arm) are generated by the kernel during program initialization. These mappings are designated as non-writable, and sealing them will prevent them from ever becoming writeable.

^ or ever removed.

This is a pretty big deal. Platforms are trying to make it so that vdso is the fast path, but if they are removed then things stop using them and it's not a problem. This description is robbing them of the information they need to know that, and it's not in your change log either.

Quoted 4 lines

I had said before that you need to be clear about the inability to remove the mappings that are sealed, as the description above heavily implies that it is only stopping them from becoming writeable.

The mseal.rst has the full description about memory sealing, I don't see the point to repeat all the blocked operations in the commit message here.

I don't know why you would think this heavily implies that only stopping them from becoming writable, There is already reminder: ** For complete descriptions ** of memory sealing, please see mseal.rst

Quoted 56 lines
Quoted 25 lines

Unlike the aforementioned mappings, the uprobe mapping is not established during program startup. However, its lifetime is the same as the process's lifetime [2], thus sealable.

The vdso, vvar, sigpage, and uprobe mappings all invoke the _install_special_mapping() function. As no other mappings utilize this function, it is logical to incorporate sealing logic within _install_special_mapping(). This approach avoids the necessity of modifying code across various architecture-specific implementations.

The vsyscall mapping, which has its own initialization function, is sealed in the XONLY case, it seems to be the most common and secure case of using vsyscall.

It is important to note that the CHECKPOINT_RESTORE feature (CRIU) may alter the mapping of vdso, vvar, and sigpage during restore operations. Consequently, this feature cannot be universally enabled across all systems. To address this, a kernel configuration option has been introduced to enable or disable this functionality. Note, uprobe is always sealed and not controlled by this kernel configuration.

I tested CONFIG_SEAL_SYSTEM_MAPPINGS_ALWAYS with ChromeOS, which doesn’t use CHECKPOINT_RESTORE.

Quoted 7 lines

[1] Documentation/userspace-api/mseal.rst
[2] https://lore.kernel.org/all/CABi2SkU9BRUnqf70-nksuMCQ+yyiWjo3fM4XkRkL-NrCZxYAyg@mail.gmail.com/

History:
V2:
Seal uprobe always (Oleg Nesterov)
Update comments and description (Randy Dunlap, Liam R.Howlett, Oleg Nesterov)

The only update to the comment I see is the pointer to mseal.rst for a complete description?

Quoted 20 lines

Rebase to linux_main

V1:
https://lore.kernel.org/all/[email protected]/

Jeff Xu (1):
exec: seal system mappings

 .../admin-guide/kernel-parameters.txt         | 10 ++++
 arch/x86/entry/vsyscall/vsyscall_64.c         |  9 +++-
 fs/exec.c                                     | 53 +++++++++++++++++++
 include/linux/fs.h                            |  1 +
 kernel/events/uprobes.c                       |  2 +-
 mm/mmap.c                                     |  1 +
 security/Kconfig                              | 26 +++++++++
 7 files changed, 99 insertions(+), 3 deletions(-)

--
2.47.0.rc1.288.g06298d1525-goog

#18 lore raw
Quoted 23 lines · Jeff Xu
Quoted 22 lines · Liam R. Howlett
Quoted 4 lines · * [email protected] <[email protected]> [241014 17:50]:

From: Jeff Xu <[email protected]>

Seal vdso, vvar, sigpage, uprobes and vsyscall.

Quoted 8 lines

Those mappings are readonly or executable only, sealing can protect them from ever changing during the life time of the process. For complete descriptions of memory sealing, please see mseal.rst [1].

System mappings such as vdso, vvar, and sigpage (for arm) are generated by the kernel during program initialization. These mappings are designated as non-writable, and sealing them will prevent them from ever becoming writeable.

^ or ever removed.

This is a pretty big deal. Platforms are trying to make it so that vdso is the fast path, but if they are removed then things stop using them and it's not a problem. This description is robbing them of the information they need to know that, and it's not in your change log either.

Quoted 7 lines
Quoted 4 lines

I had said before that you need to be clear about the inability to remove the mappings that are sealed, as the description above heavily implies that it is only stopping them from becoming writeable.

The mseal.rst has the full description about memory sealing, I don't see the point to repeat all the blocked operations in the commit message here.

That's not what I said or asked for.

It's three words to add.

Quoted 3 lines

I don't know why you would think this heavily implies that only stopping them from becoming writable,

Because you left it out of the description that states what this does, and it's three words that adds a whole lot of context.

I told you to add that detail because it is _way_ more important to almost everyone that you are making mappings exist for the lifetime of the process than the fact that not writeable mappings will still be not writeable.

One points out the fundamental change to the mappings you list while the other tells people how things won't change. I get that not changing is the point of the patch, but it's not the only thing it does.

Quoted 2 lines

There is already reminder: **
For complete descriptions ** of memory sealing, please see mseal.rst

Getting good code reviews is difficult. There are very few people who do a good job of the very few people who do it at all. If you want people to read your code, then you need to spoon feed it to them.

You have just put up a huge barrier to someone doing a code review at all, let alone a good code review. So if you want good code, you need to provide good information with a low bar of entrance. We can all agree that good review creates good code (well, better code, at least).

What you have done here is limited the number of people who are willing to even entertain the idea of looking at the following patch. And of the people who do read the patch, probably close to zero will read that document.

It's (usually) nothing personal, it's just human nature and a function of time.

Thanks,
Liam

author
#19 lore raw

Hi Liam

Quoted 5 lines · Liam R. Howlett
Quoted 3 lines · Jeff Xu
Quoted 2 lines · Liam R. Howlett

* [email protected] <[email protected]> [241014 17:50]:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

From: Jeff Xu <[email protected]>

Seal vdso, vvar, sigpage, uprobes and vsyscall.

Those mappings are readonly or executable only, sealing can protect them from ever changing during the life time of the process. For complete descriptions of memory sealing, please see mseal.rst [1].

I will mention unmap in the above sentence.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

System mappings such as vdso, vvar, and sigpage (for arm) are generated by the kernel during program initialization. These mappings are designated as non-writable, and sealing them will prevent them from ever becoming writeable.

^ or ever removed.

This section is about the mappings (vdso, etc) created during program initialization vs later time as uprobe, I will revise to make it clearer to the reader.

Thanks
-Jeff