Skip to contentSkip to conversation

exec: inherit HWCAPs from the parent process

PATCH In review Andrei Vagin 5 participants 14 messages Mar 23 – Apr 15

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

v5 cover exec: inherit HWCAPs from the parent process

This patch series introduces a mechanism to inherit hardware capabilities (AT_HWCAP, AT_HWCAP2, etc.) from a parent process when they have been modified via prctl.

To support C/R operations (snapshots, live migration) in heterogeneous clusters, we must ensure that processes utilize CPU features available on all potential target nodes. To solve this, we need to advertise a common feature set across the cluster.

Initially, a cgroup-based approach was considered, but it was decided that inheriting HWCAPs from a parent process that has set its own auxiliary vector via prctl is a simpler and more flexible solution.

This implementation adds a new mm flag MMF_USER_HWCAP, which is set when the auxiliary vector is modified via prctl(PR_SET_MM_AUXV). When execve() is called, if the current process has MMF_USER_HWCAP set, the HWCAP values are extracted from the current auxiliary vector and inherited by the new process.

The first patch fixes AUXV size calculation for ELF_HWCAP3 and ELF_HWCAP4 in binfmt_elf_fdpic and updates AT_VECTOR_SIZE_BASE.

The second patch implements the core inheritance logic in execve().

The third patch adds a selftest to verify that HWCAPs are correctly inherited across execve().

v5:
- Fix reading of HWCAPs from auxiliary vectors of compat processes.
- Defer HWCAP masking until ELF table creation (create_elf_tables) to handle compat process correctly.
- arm64: Disable HWCAP inheritance on architecture switch (e.g., AArch64 to AArch32) by clearing MMF_USER_HWCAP, as HWCAP bits have completely different meanings across these architectures.

v4: minor fixes based on feedback from the previous version.
v3: synchronize saved_auxv access with arg_lock

v1: https://lkml.org/lkml/2025/12/5/65
v2: https://lkml.org/lkml/2026/1/8/219
v3: https://lkml.org/lkml/2026/2/9/1233
v4: https://lkml.org/lkml/2026/2/17/963

Cc Andrew Morton <[email protected]>
Cc Chen Ridong <[email protected]>
Cc Christian Brauner <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Eric Biederman <[email protected]>
Cc Kees Cook <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Michal Koutny <[email protected]>
Cc Cyrill Gorcunov <[email protected]>

Andrei Vagin (4):
exec: inherit HWCAPs from the parent process
arm64: elf: clear MMF_USER_HWCAP on architecture switch
mm: synchronize saved_auxv access with arg_lock selftests/exec: add test for HWCAP inheritance

 arch/arm64/include/asm/elf.h                 |  12 ++-
 fs/binfmt_elf.c                              |  13 ++-
 fs/binfmt_elf_fdpic.c                        |  13 ++-
 fs/exec.c                                    |  54 ++++++++++
 fs/proc/base.c                               |  12 ++-
 include/linux/binfmts.h                      |  11 ++
 include/linux/mm_types.h                     |   3 +-
 kernel/fork.c                                |   8 ++
 kernel/sys.c                                 |  30 +++---
 tools/testing/selftests/exec/.gitignore      |   1 +
 tools/testing/selftests/exec/Makefile        |   1 +
 tools/testing/selftests/exec/hwcap_inherit.c | 105 +++++++++++++++++++
 12 files changed, 234 insertions(+), 29 deletions(-)
 create mode 100644 tools/testing/selftests/exec/hwcap_inherit.c
Signature · 1 line
2.53.0.959.g497ff81fa9-goog
#1 lore raw

PATCH v5 1/4 exec: inherit HWCAPs from the parent process

Introduces a mechanism to inherit hardware capabilities (AT_HWCAP, AT_HWCAP2, etc.) from a parent process when they have been modified via prctl.

To support C/R operations (snapshots, live migration) in heterogeneous clusters, we must ensure that processes utilize CPU features available on all potential target nodes. To solve this, we need to advertise a common feature set across the cluster.

This patch adds a new mm flag MMF_USER_HWCAP, which is set when the auxiliary vector is modified via prctl(PR_SET_MM, PR_SET_MM_AUXV). When execve() is called, if the current process has MMF_USER_HWCAP set, the HWCAP values are extracted from the current auxiliary vector and stored in the linux_binprm structure. These values are then used to populate the auxiliary vector of the new process, effectively inheriting the hardware capabilities.

The inherited HWCAPs are masked with the hardware capabilities supported by the current kernel to ensure that we don't report more features than actually supported. This is important to avoid unexpected behavior, especially for processes with additional privileges.

---
 fs/binfmt_elf.c          | 13 ++++++---
 fs/binfmt_elf_fdpic.c    | 13 ++++++---
 fs/exec.c                | 62 ++++++++++++++++++++++++++++++++++++++++
 include/linux/binfmts.h  | 11 +++++++
 include/linux/mm_types.h |  2 ++
 kernel/fork.c            |  3 ++
 kernel/sys.c             |  5 +++-
 7 files changed, 100 insertions(+), 9 deletions(-)
fs/binfmt_elf.c +9 -4
index fb857faaf0d6..d99db73c76f0 100644
@@ -183,6 +183,7 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
 	int ei_index;
 	const struct cred *cred = current_cred();
 	struct vm_area_struct *vma;
+	bool user_hwcap = mm_flags_test(MMF_USER_HWCAP, mm);
 
 	/*
 	 * In some cases (e.g. Hyper-Threading), we want to avoid L1
@@ -247,7 +248,8 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
 	 */
 	ARCH_DLINFO;
 #endif
-	NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
+	NEW_AUX_ENT(AT_HWCAP, user_hwcap ?
+			      (bprm->hwcap & ELF_HWCAP) : ELF_HWCAP);
 	NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
 	NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
 	NEW_AUX_ENT(AT_PHDR, phdr_addr);
@@ -265,13 +267,16 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
 	NEW_AUX_ENT(AT_SECURE, bprm->secureexec);
 	NEW_AUX_ENT(AT_RANDOM, (elf_addr_t)(unsigned long)u_rand_bytes);
 #ifdef ELF_HWCAP2
-	NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2);
+	NEW_AUX_ENT(AT_HWCAP2, user_hwcap ?
+			       (bprm->hwcap2 & ELF_HWCAP2) : ELF_HWCAP2);
 #endif
 #ifdef ELF_HWCAP3
-	NEW_AUX_ENT(AT_HWCAP3, ELF_HWCAP3);
+	NEW_AUX_ENT(AT_HWCAP3, user_hwcap ?
+			       (bprm->hwcap3 & ELF_HWCAP3) : ELF_HWCAP3);
 #endif
 #ifdef ELF_HWCAP4
-	NEW_AUX_ENT(AT_HWCAP4, ELF_HWCAP4);
+	NEW_AUX_ENT(AT_HWCAP4, user_hwcap ?
+			       (bprm->hwcap4 & ELF_HWCAP4) : ELF_HWCAP4);
 #endif
 	NEW_AUX_ENT(AT_EXECFN, bprm->exec);
 	if (k_platform) {
fs/binfmt_elf_fdpic.c +9 -4
index 95b65aab7daa..92c88471455a 100644
@@ -508,6 +508,7 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm,
 	unsigned long flags = 0;
 	int ei_index;
 	elf_addr_t *elf_info;
+	bool user_hwcap = mm_flags_test(MMF_USER_HWCAP, mm);
 
 #ifdef CONFIG_MMU
 	/* In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
@@ -629,15 +630,19 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm,
 	 */
 	ARCH_DLINFO;
 #endif
-	NEW_AUX_ENT(AT_HWCAP,	ELF_HWCAP);
+	NEW_AUX_ENT(AT_HWCAP,	user_hwcap ?
+				(bprm->hwcap & ELF_HWCAP) : ELF_HWCAP);
 #ifdef ELF_HWCAP2
-	NEW_AUX_ENT(AT_HWCAP2,	ELF_HWCAP2);
+	NEW_AUX_ENT(AT_HWCAP2,	user_hwcap ?
+				(bprm->hwcap2 & ELF_HWCAP2) : ELF_HWCAP2);
 #endif
 #ifdef ELF_HWCAP3
-	NEW_AUX_ENT(AT_HWCAP3,	ELF_HWCAP3);
+	NEW_AUX_ENT(AT_HWCAP3,	user_hwcap ?
+				(bprm->hwcap3 & ELF_HWCAP3) : ELF_HWCAP3);
 #endif
 #ifdef ELF_HWCAP4
-	NEW_AUX_ENT(AT_HWCAP4,	ELF_HWCAP4);
+	NEW_AUX_ENT(AT_HWCAP4,	user_hwcap ?
+				(bprm->hwcap4 & ELF_HWCAP4) : ELF_HWCAP4);
 #endif
 	NEW_AUX_ENT(AT_PAGESZ,	PAGE_SIZE);
 	NEW_AUX_ENT(AT_CLKTCK,	CLOCKS_PER_SEC);
fs/exec.c +62 -0

74 lines not on this page · raw message

include/linux/binfmts.h +11 -0
index 65abd5ab8836..94a3dcf9b1d2 100644
@@ -2,6 +2,7 @@
 #ifndef _LINUX_BINFMTS_H
 #define _LINUX_BINFMTS_H
 
+#include <linux/elf.h>
 #include <linux/sched.h>
 #include <linux/unistd.h>
 #include <asm/exec.h>
@@ -67,6 +68,16 @@ struct linux_binprm {
 	unsigned long exec;
 
 	struct rlimit rlim_stack; /* Saved RLIMIT_STACK used during exec. */
+	unsigned long hwcap;
+#ifdef ELF_HWCAP2
+	unsigned long hwcap2;
+#endif
+#ifdef ELF_HWCAP3
+	unsigned long hwcap3;
+#endif
+#ifdef ELF_HWCAP4
+	unsigned long hwcap4;
+#endif
 
 	char buf[BINPRM_BUF_SIZE];
 } __randomize_layout;
include/linux/mm_types.h +2 -0
index 3cc8ae722886..62dde645f469 100644
@@ -1919,6 +1919,8 @@ enum {
 #define MMF_TOPDOWN		31	/* mm searches top down by default */
 #define MMF_TOPDOWN_MASK	BIT(MMF_TOPDOWN)
 
+#define MMF_USER_HWCAP		32	/* user-defined HWCAPs */
+
 #define MMF_INIT_LEGACY_MASK	(MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\
 				 MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\
 				 MMF_VM_MERGE_ANY_MASK | MMF_TOPDOWN_MASK)
kernel/fork.c +3 -0
index bc2bf58b93b6..2ac277aa078c 100644
@@ -1105,6 +1105,9 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 
 		__mm_flags_overwrite_word(mm, mmf_init_legacy_flags(flags));
 		mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK;
+
+		if (mm_flags_test(MMF_USER_HWCAP, current->mm))
+			mm_flags_set(MMF_USER_HWCAP, mm);
 	} else {
 		__mm_flags_overwrite_word(mm, default_dump_filter);
 		mm->def_flags = 0;
kernel/sys.c +4 -1
index cdbf8513caf6..e4b0fa2f6845 100644
@@ -2157,8 +2157,10 @@ static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data
 	 * not introduce additional locks here making the kernel
 	 * more complex.
 	 */
-	if (prctl_map.auxv_size)
+	if (prctl_map.auxv_size) {
 		memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
+		mm_flags_set(MMF_USER_HWCAP, mm);
+	}
 
 	mmap_read_unlock(mm);
 	return 0;
@@ -2190,6 +2192,7 @@ static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
 
 	task_lock(current);
 	memcpy(mm->saved_auxv, user_auxv, len);
+	mm_flags_set(MMF_USER_HWCAP, mm);
 	task_unlock(current);
 
 	return 0;
Signature · 1 line
2.53.0.983.g0bb29b3bc5-goog
reviewer
#2 lore raw
Quoted 17 lines · Andrei Vagin

Introduces a mechanism to inherit hardware capabilities (AT_HWCAP, AT_HWCAP2, etc.) from a parent process when they have been modified via prctl.

To support C/R operations (snapshots, live migration) in heterogeneous clusters, we must ensure that processes utilize CPU features available on all potential target nodes. To solve this, we need to advertise a common feature set across the cluster.

This patch adds a new mm flag MMF_USER_HWCAP, which is set when the auxiliary vector is modified via prctl(PR_SET_MM, PR_SET_MM_AUXV). When execve() is called, if the current process has MMF_USER_HWCAP set, the HWCAP values are extracted from the current auxiliary vector and stored in the linux_binprm structure. These values are then used to populate the auxiliary vector of the new process, effectively inheriting the hardware capabilities.

Quoted 4 lines

The inherited HWCAPs are masked with the hardware capabilities supported by the current kernel to ensure that we don't report more features than actually supported. This is important to avoid unexpected behavior, especially for processes with additional privileges.

At a high level, I don't think that's going to be sufficient:

* On an architecture with other userspace accessible feature identification mechanism registers (e.g. ID registers), userspace might read those. So you might need to hide stuff there too, and that's going to require architecture-specific interfaces to manage.

It's possible that some code checks HWCAPs and others check ID registers, and mismatch between the two could be problematic.

* If the HWCAPs can be inherited by a more privileged task, then a malicious user could use this to hide security features (e.g. shadow stack or pointer authentication on arm64), and make it easier to attack that task. While not a direct attack, it would undermine those features.

Mark.

Quoted 255 lines
---
 fs/binfmt_elf.c          | 13 ++++++---
 fs/binfmt_elf_fdpic.c    | 13 ++++++---
 fs/exec.c                | 62 ++++++++++++++++++++++++++++++++++++++++
 include/linux/binfmts.h  | 11 +++++++
 include/linux/mm_types.h |  2 ++
 kernel/fork.c            |  3 ++
 kernel/sys.c             |  5 +++-
 7 files changed, 100 insertions(+), 9 deletions(-)
fs/binfmt_elf.c +9 -4
index fb857faaf0d6..d99db73c76f0 100644
@@ -183,6 +183,7 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
 	int ei_index;
 	const struct cred *cred = current_cred();
 	struct vm_area_struct *vma;
+	bool user_hwcap = mm_flags_test(MMF_USER_HWCAP, mm);
 
 	/*
 	 * In some cases (e.g. Hyper-Threading), we want to avoid L1
@@ -247,7 +248,8 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
 	 */
 	ARCH_DLINFO;
 #endif
-	NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
+	NEW_AUX_ENT(AT_HWCAP, user_hwcap ?
+			      (bprm->hwcap & ELF_HWCAP) : ELF_HWCAP);
 	NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
 	NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
 	NEW_AUX_ENT(AT_PHDR, phdr_addr);
@@ -265,13 +267,16 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
 	NEW_AUX_ENT(AT_SECURE, bprm->secureexec);
 	NEW_AUX_ENT(AT_RANDOM, (elf_addr_t)(unsigned long)u_rand_bytes);
 #ifdef ELF_HWCAP2
-	NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2);
+	NEW_AUX_ENT(AT_HWCAP2, user_hwcap ?
+			       (bprm->hwcap2 & ELF_HWCAP2) : ELF_HWCAP2);
 #endif
 #ifdef ELF_HWCAP3
-	NEW_AUX_ENT(AT_HWCAP3, ELF_HWCAP3);
+	NEW_AUX_ENT(AT_HWCAP3, user_hwcap ?
+			       (bprm->hwcap3 & ELF_HWCAP3) : ELF_HWCAP3);
 #endif
 #ifdef ELF_HWCAP4
-	NEW_AUX_ENT(AT_HWCAP4, ELF_HWCAP4);
+	NEW_AUX_ENT(AT_HWCAP4, user_hwcap ?
+			       (bprm->hwcap4 & ELF_HWCAP4) : ELF_HWCAP4);
 #endif
 	NEW_AUX_ENT(AT_EXECFN, bprm->exec);
 	if (k_platform) {
fs/binfmt_elf_fdpic.c +9 -4
index 95b65aab7daa..92c88471455a 100644
@@ -508,6 +508,7 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm,
 	unsigned long flags = 0;
 	int ei_index;
 	elf_addr_t *elf_info;
+	bool user_hwcap = mm_flags_test(MMF_USER_HWCAP, mm);
 
 #ifdef CONFIG_MMU
 	/* In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
@@ -629,15 +630,19 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm,
 	 */
 	ARCH_DLINFO;
 #endif
-	NEW_AUX_ENT(AT_HWCAP,	ELF_HWCAP);
+	NEW_AUX_ENT(AT_HWCAP,	user_hwcap ?
+				(bprm->hwcap & ELF_HWCAP) : ELF_HWCAP);
 #ifdef ELF_HWCAP2
-	NEW_AUX_ENT(AT_HWCAP2,	ELF_HWCAP2);
+	NEW_AUX_ENT(AT_HWCAP2,	user_hwcap ?
+				(bprm->hwcap2 & ELF_HWCAP2) : ELF_HWCAP2);
 #endif
 #ifdef ELF_HWCAP3
-	NEW_AUX_ENT(AT_HWCAP3,	ELF_HWCAP3);
+	NEW_AUX_ENT(AT_HWCAP3,	user_hwcap ?
+				(bprm->hwcap3 & ELF_HWCAP3) : ELF_HWCAP3);
 #endif
 #ifdef ELF_HWCAP4
-	NEW_AUX_ENT(AT_HWCAP4,	ELF_HWCAP4);
+	NEW_AUX_ENT(AT_HWCAP4,	user_hwcap ?
+				(bprm->hwcap4 & ELF_HWCAP4) : ELF_HWCAP4);
 #endif
 	NEW_AUX_ENT(AT_PAGESZ,	PAGE_SIZE);
 	NEW_AUX_ENT(AT_CLKTCK,	CLOCKS_PER_SEC);
fs/exec.c +62 -0
index 9ea3a775d51e..1cd7d87a0e79 100644
@@ -1775,6 +1775,65 @@ static int bprm_execve(struct linux_binprm *bprm)
 	return retval;
 }
 
+static void inherit_hwcap(struct linux_binprm *bprm)
+{
+	struct mm_struct *mm = current->mm;
+	bool compat = in_compat_syscall();
+	int i, n;
+
+#ifdef ELF_HWCAP4
+	n = 4;
+#elif defined(ELF_HWCAP3)
+	n = 3;
+#elif defined(ELF_HWCAP2)
+	n = 2;
+#else
+	n = 1;
+#endif
+
+	for (i = 0; n && i < AT_VECTOR_SIZE; i += 2) {
+		unsigned long type, val;
+
+		if (!compat) {
+			type = mm->saved_auxv[i];
+			val = mm->saved_auxv[i + 1];
+		} else {
+			compat_uptr_t *auxv = (compat_uptr_t *)mm->saved_auxv;
+
+			type = auxv[i];
+			val = auxv[i + 1];
+		}
+
+		switch (type) {
+		case AT_NULL:
+			goto done;
+		case AT_HWCAP:
+			bprm->hwcap = val;
+			break;
+#ifdef ELF_HWCAP2
+		case AT_HWCAP2:
+			bprm->hwcap2 = val;
+			break;
+#endif
+#ifdef ELF_HWCAP3
+		case AT_HWCAP3:
+			bprm->hwcap3 = val;
+			break;
+#endif
+#ifdef ELF_HWCAP4
+		case AT_HWCAP4:
+			bprm->hwcap4 = val;
+			break;
+#endif
+		default:
+			continue;
+		}
+		n--;
+	}
+done:
+	mm_flags_set(MMF_USER_HWCAP, bprm->mm);
+}
+
 static int do_execveat_common(int fd, struct filename *filename,
 			      struct user_arg_ptr argv,
 			      struct user_arg_ptr envp,
@@ -1843,6 +1902,9 @@ static int do_execveat_common(int fd, struct filename *filename,
 			     current->comm, bprm->filename);
 	}
 
+	if (mm_flags_test(MMF_USER_HWCAP, current->mm))
+		inherit_hwcap(bprm);
+
 	return bprm_execve(bprm);
 }
 
include/linux/binfmts.h +11 -0
index 65abd5ab8836..94a3dcf9b1d2 100644
@@ -2,6 +2,7 @@
 #ifndef _LINUX_BINFMTS_H
 #define _LINUX_BINFMTS_H
 
+#include <linux/elf.h>
 #include <linux/sched.h>
 #include <linux/unistd.h>
 #include <asm/exec.h>
@@ -67,6 +68,16 @@ struct linux_binprm {
 	unsigned long exec;
 
 	struct rlimit rlim_stack; /* Saved RLIMIT_STACK used during exec. */
+	unsigned long hwcap;
+#ifdef ELF_HWCAP2
+	unsigned long hwcap2;
+#endif
+#ifdef ELF_HWCAP3
+	unsigned long hwcap3;
+#endif
+#ifdef ELF_HWCAP4
+	unsigned long hwcap4;
+#endif
 
 	char buf[BINPRM_BUF_SIZE];
 } __randomize_layout;
include/linux/mm_types.h +2 -0
index 3cc8ae722886..62dde645f469 100644
@@ -1919,6 +1919,8 @@ enum {
 #define MMF_TOPDOWN		31	/* mm searches top down by default */
 #define MMF_TOPDOWN_MASK	BIT(MMF_TOPDOWN)
 
+#define MMF_USER_HWCAP		32	/* user-defined HWCAPs */
+
 #define MMF_INIT_LEGACY_MASK	(MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\
 				 MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\
 				 MMF_VM_MERGE_ANY_MASK | MMF_TOPDOWN_MASK)
kernel/fork.c +3 -0
index bc2bf58b93b6..2ac277aa078c 100644
@@ -1105,6 +1105,9 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 
 		__mm_flags_overwrite_word(mm, mmf_init_legacy_flags(flags));
 		mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK;
+
+		if (mm_flags_test(MMF_USER_HWCAP, current->mm))
+			mm_flags_set(MMF_USER_HWCAP, mm);
 	} else {
 		__mm_flags_overwrite_word(mm, default_dump_filter);
 		mm->def_flags = 0;
kernel/sys.c +4 -1
index cdbf8513caf6..e4b0fa2f6845 100644
@@ -2157,8 +2157,10 @@ static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data
 	 * not introduce additional locks here making the kernel
 	 * more complex.
 	 */
-	if (prctl_map.auxv_size)
+	if (prctl_map.auxv_size) {
 		memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
+		mm_flags_set(MMF_USER_HWCAP, mm);
+	}
 
 	mmap_read_unlock(mm);
 	return 0;
@@ -2190,6 +2192,7 @@ static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
 
 	task_lock(current);
 	memcpy(mm->saved_auxv, user_auxv, len);
+	mm_flags_set(MMF_USER_HWCAP, mm);
 	task_unlock(current);
 
 	return 0;
Signature · 1 line
2.53.0.983.g0bb29b3bc5-goog
maintainer
#3 lore raw
Quoted 30 lines · Mark Rutland
Quoted 17 lines · Andrei Vagin

Introduces a mechanism to inherit hardware capabilities (AT_HWCAP, AT_HWCAP2, etc.) from a parent process when they have been modified via prctl.

To support C/R operations (snapshots, live migration) in heterogeneous clusters, we must ensure that processes utilize CPU features available on all potential target nodes. To solve this, we need to advertise a common feature set across the cluster.

This patch adds a new mm flag MMF_USER_HWCAP, which is set when the auxiliary vector is modified via prctl(PR_SET_MM, PR_SET_MM_AUXV). When execve() is called, if the current process has MMF_USER_HWCAP set, the HWCAP values are extracted from the current auxiliary vector and stored in the linux_binprm structure. These values are then used to populate the auxiliary vector of the new process, effectively inheriting the hardware capabilities.

Quoted 4 lines

The inherited HWCAPs are masked with the hardware capabilities supported by the current kernel to ensure that we don't report more features than actually supported. This is important to avoid unexpected behavior, especially for processes with additional privileges.

At a high level, I don't think that's going to be sufficient:

* On an architecture with other userspace accessible feature identification mechanism registers (e.g. ID registers), userspace might read those. So you might need to hide stuff there too, and that's going to require architecture-specific interfaces to manage.

Quoted 8 lines

It's possible that some code checks HWCAPs and others check ID registers, and mismatch between the two could be problematic.

* If the HWCAPs can be inherited by a more privileged task, then a malicious user could use this to hide security features (e.g. shadow stack or pointer authentication on arm64), and make it easier to attack that task. While not a direct attack, it would undermine those features.

Yeah, this looks like a non-starter to me on arm64. Even if it was extended to apply the same treatment to the idregs, many of the hwcap features can't actually be disabled by the kernel and so you still run the risk of a task that probes for the presence of a feature using something like a SIGILL handler or, perhaps more likely, assumes that the presence of one hwcap implies the presence of another. And then there are the applications that just base everything off the MIDR...

There's also kvm, which provides a roundabout way to query some features of the underlying hardware.

You're probably better off using/extending the idreg overrides we have in arch/arm64/kernel/pi/idreg-override.c so that you can make your cluster of heterogeneous machines look alike.

On the other hand, if munging the hwcaps happens to be sufficient for this particular use-case, can't it be handled entirely in userspace (e.g. by hacking libc?)

Will

#4 lore raw

Hi Mark and Will,

Thanks for the feedback. Please read the inline comments.

Quoted 32 lines · Will Deacon
Quoted 30 lines · Mark Rutland
Quoted 17 lines · Andrei Vagin

Introduces a mechanism to inherit hardware capabilities (AT_HWCAP, AT_HWCAP2, etc.) from a parent process when they have been modified via prctl.

To support C/R operations (snapshots, live migration) in heterogeneous clusters, we must ensure that processes utilize CPU features available on all potential target nodes. To solve this, we need to advertise a common feature set across the cluster.

This patch adds a new mm flag MMF_USER_HWCAP, which is set when the auxiliary vector is modified via prctl(PR_SET_MM, PR_SET_MM_AUXV). When execve() is called, if the current process has MMF_USER_HWCAP set, the HWCAP values are extracted from the current auxiliary vector and stored in the linux_binprm structure. These values are then used to populate the auxiliary vector of the new process, effectively inheriting the hardware capabilities.

Quoted 4 lines

The inherited HWCAPs are masked with the hardware capabilities supported by the current kernel to ensure that we don't report more features than actually supported. This is important to avoid unexpected behavior, especially for processes with additional privileges.

At a high level, I don't think that's going to be sufficient:

* On an architecture with other userspace accessible feature identification mechanism registers (e.g. ID registers), userspace might read those. So you might need to hide stuff there too, and that's going to require architecture-specific interfaces to manage.

Quoted 8 lines
Quoted 8 lines

It's possible that some code checks HWCAPs and others check ID registers, and mismatch between the two could be problematic.

* If the HWCAPs can be inherited by a more privileged task, then a malicious user could use this to hide security features (e.g. shadow stack or pointer authentication on arm64), and make it easier to attack that task. While not a direct attack, it would undermine those features.

I agree with Mark that only a privileged process have to be able to mask certain hardware features. Currently, PR_SET_MM_AUXV is guarded by CAP_SYS_RESOURCE, but PR_SET_MM_MAP allows changing the auxiliary vector without specific capabilities. This is definitely the issue. To address this, I think we can consider to introduce a new prctl command to enable HWCAP inheritance explicitly.

Quoted 8 lines

Yeah, this looks like a non-starter to me on arm64. Even if it was extended to apply the same treatment to the idregs, many of the hwcap features can't actually be disabled by the kernel and so you still run the risk of a task that probes for the presence of a feature using something like a SIGILL handler or, perhaps more likely, assumes that the presence of one hwcap implies the presence of another. And then there are the applications that just base everything off the MIDR...

The goal of this mechanism is not to provide strict architectural enforcement or to trap the use of hardware features; rather, it is to provide a consistent discovery interface for applications. I chose the HWCAP vector because it mirrors the existing behavior of running an older kernel on newer hardware: while ID registers might report a feature as physically present, the HWCAPs will omit it if the kernel lacks support. Applications are generally expected to treat HWCAPs as the source of truth for which features are safe to use, even if the underlying hardware is technically capable of more.

Another significant advantage of using HWCAPs is that many applications already rely on them for feature detection. This interface allows these applications to work correctly "out-of-the-box" in a migrated environment without requiring any userspace modifications. I understand that some apps may use other detection methods; however, there it no gurantee that these applications will work correctly after migration to another machine.

Quoted 7 lines

There's also kvm, which provides a roundabout way to query some features of the underlying hardware.

You're probably better off using/extending the idreg overrides we have in arch/arm64/kernel/pi/idreg-override.c so that you can make your cluster of heterogeneous machines look alike.

IIRC, idreg-override/cpuid-masking usually works for an entire machine. We actually need to have a mechanism that will work on a per-container basis. Workloads inside one cluster can have different migration/snapshot requirements. Some are pinned to a specific node, others are never migrated, while others need to be migratable across a cluster or even between clusters. We need a mechanism that can be tunable on a per-container/per-process basis.

Quoted 4 lines

On the other hand, if munging the hwcaps happens to be sufficient for this particular use-case, can't it be handled entirely in userspace (e.g. by hacking libc?)

CRIU often handles workloads with a mix of runtimes: some linked against glibc, some against musl, and others like Go that bypass libc entirely. CRIU is mostly used to handle containers that can run multiple processes possible based on different runtimes. It means available cpu features should not be only specified for one runtime, they have to be passed across different runtimes. I think the pure userspace solution is near infeasible in this case.

Thanks,
Andrei

#5 lore raw
Quoted 4 lines · Andrei Vagin

Hi Mark and Will,

Thanks for the feedback. Please read the inline comments.

Mark, Will, just checking in to see if my explanation makes sense to you. Let me know if you have any further feedback or questions.

Thanks,
Andrei

Quoted 108 lines · Will Deacon
Quoted 32 lines · Mark Rutland
Quoted 30 lines · Andrei Vagin
Quoted 17 lines

Introduces a mechanism to inherit hardware capabilities (AT_HWCAP, AT_HWCAP2, etc.) from a parent process when they have been modified via prctl.

To support C/R operations (snapshots, live migration) in heterogeneous clusters, we must ensure that processes utilize CPU features available on all potential target nodes. To solve this, we need to advertise a common feature set across the cluster.

This patch adds a new mm flag MMF_USER_HWCAP, which is set when the auxiliary vector is modified via prctl(PR_SET_MM, PR_SET_MM_AUXV). When execve() is called, if the current process has MMF_USER_HWCAP set, the HWCAP values are extracted from the current auxiliary vector and stored in the linux_binprm structure. These values are then used to populate the auxiliary vector of the new process, effectively inheriting the hardware capabilities.

Quoted 4 lines

The inherited HWCAPs are masked with the hardware capabilities supported by the current kernel to ensure that we don't report more features than actually supported. This is important to avoid unexpected behavior, especially for processes with additional privileges.

At a high level, I don't think that's going to be sufficient:

* On an architecture with other userspace accessible feature identification mechanism registers (e.g. ID registers), userspace might read those. So you might need to hide stuff there too, and that's going to require architecture-specific interfaces to manage.

Quoted 8 lines
Quoted 8 lines

It's possible that some code checks HWCAPs and others check ID registers, and mismatch between the two could be problematic.

* If the HWCAPs can be inherited by a more privileged task, then a malicious user could use this to hide security features (e.g. shadow stack or pointer authentication on arm64), and make it easier to attack that task. While not a direct attack, it would undermine those features.

I agree with Mark that only a privileged process have to be able to mask certain hardware features. Currently, PR_SET_MM_AUXV is guarded by CAP_SYS_RESOURCE, but PR_SET_MM_MAP allows changing the auxiliary vector without specific capabilities. This is definitely the issue. To address this, I think we can consider to introduce a new prctl command to enable HWCAP inheritance explicitly.

Quoted 8 lines

Yeah, this looks like a non-starter to me on arm64. Even if it was extended to apply the same treatment to the idregs, many of the hwcap features can't actually be disabled by the kernel and so you still run the risk of a task that probes for the presence of a feature using something like a SIGILL handler or, perhaps more likely, assumes that the presence of one hwcap implies the presence of another. And then there are the applications that just base everything off the MIDR...

The goal of this mechanism is not to provide strict architectural enforcement or to trap the use of hardware features; rather, it is to provide a consistent discovery interface for applications. I chose the HWCAP vector because it mirrors the existing behavior of running an older kernel on newer hardware: while ID registers might report a feature as physically present, the HWCAPs will omit it if the kernel lacks support. Applications are generally expected to treat HWCAPs as the source of truth for which features are safe to use, even if the underlying hardware is technically capable of more.

Another significant advantage of using HWCAPs is that many applications already rely on them for feature detection. This interface allows these applications to work correctly "out-of-the-box" in a migrated environment without requiring any userspace modifications. I understand that some apps may use other detection methods; however, there it no gurantee that these applications will work correctly after migration to another machine.

Quoted 7 lines

There's also kvm, which provides a roundabout way to query some features of the underlying hardware.

You're probably better off using/extending the idreg overrides we have in arch/arm64/kernel/pi/idreg-override.c so that you can make your cluster of heterogeneous machines look alike.

IIRC, idreg-override/cpuid-masking usually works for an entire machine. We actually need to have a mechanism that will work on a per-container basis. Workloads inside one cluster can have different migration/snapshot requirements. Some are pinned to a specific node, others are never migrated, while others need to be migratable across a cluster or even between clusters. We need a mechanism that can be tunable on a per-container/per-process basis.

Quoted 4 lines

On the other hand, if munging the hwcaps happens to be sufficient for this particular use-case, can't it be handled entirely in userspace (e.g. by hacking libc?)

CRIU often handles workloads with a mix of runtimes: some linked against glibc, some against musl, and others like Go that bypass libc entirely. CRIU is mostly used to handle containers that can run multiple processes possible based on different runtimes. It means available cpu features should not be only specified for one runtime, they have to be passed across different runtimes. I think the pure userspace solution is near infeasible in this case.

Thanks,
Andrei

reviewer
#6 lore raw
In reply to Andrei Vagin
Quoted 61 lines · Andrei Vagin

Hi Mark and Will,

Thanks for the feedback. Please read the inline comments.

Quoted 32 lines · Will Deacon
Quoted 30 lines · Mark Rutland
Quoted 17 lines · Andrei Vagin

Introduces a mechanism to inherit hardware capabilities (AT_HWCAP, AT_HWCAP2, etc.) from a parent process when they have been modified via prctl.

To support C/R operations (snapshots, live migration) in heterogeneous clusters, we must ensure that processes utilize CPU features available on all potential target nodes. To solve this, we need to advertise a common feature set across the cluster.

This patch adds a new mm flag MMF_USER_HWCAP, which is set when the auxiliary vector is modified via prctl(PR_SET_MM, PR_SET_MM_AUXV). When execve() is called, if the current process has MMF_USER_HWCAP set, the HWCAP values are extracted from the current auxiliary vector and stored in the linux_binprm structure. These values are then used to populate the auxiliary vector of the new process, effectively inheriting the hardware capabilities.

Quoted 4 lines

The inherited HWCAPs are masked with the hardware capabilities supported by the current kernel to ensure that we don't report more features than actually supported. This is important to avoid unexpected behavior, especially for processes with additional privileges.

At a high level, I don't think that's going to be sufficient:

* On an architecture with other userspace accessible feature identification mechanism registers (e.g. ID registers), userspace might read those. So you might need to hide stuff there too, and that's going to require architecture-specific interfaces to manage.

Quoted 8 lines
Quoted 8 lines

It's possible that some code checks HWCAPs and others check ID registers, and mismatch between the two could be problematic.

* If the HWCAPs can be inherited by a more privileged task, then a malicious user could use this to hide security features (e.g. shadow stack or pointer authentication on arm64), and make it easier to attack that task. While not a direct attack, it would undermine those features.

I agree with Mark that only a privileged process have to be able to mask certain hardware features. Currently, PR_SET_MM_AUXV is guarded by CAP_SYS_RESOURCE, but PR_SET_MM_MAP allows changing the auxiliary vector without specific capabilities. This is definitely the issue. To address this, I think we can consider to introduce a new prctl command to enable HWCAP inheritance explicitly.

Quoted 7 lines

Yeah, this looks like a non-starter to me on arm64. Even if it was extended to apply the same treatment to the idregs, many of the hwcap features can't actually be disabled by the kernel and so you still run the risk of a task that probes for the presence of a feature using something like a SIGILL handler or, perhaps more likely, assumes that the presence of one hwcap implies the presence of another. And then there are the applications that just base everything off the MIDR...

Quoted 7 lines

The goal of this mechanism is not to provide strict architectural enforcement or to trap the use of hardware features; rather, it is to provide a consistent discovery interface for applications. I chose the HWCAP vector because it mirrors the existing behavior of running an older kernel on newer hardware: while ID registers might report a feature as physically present, the HWCAPs will omit it if the kernel lacks support.

On arm64, the view of the ID registers that userspace gets *only* exposes features that the kernel knows about, as userspace reads of those registers are trapped+emulated by the kernel. On arm64 it's not true to say that something appears in those but not the HWCAPs.

I understand that might be different on other architectures, and so maybe this approach is sufficient on other architectures, but it is not sufficient on arm64.

Quoted 3 lines

Applications are generally expected to treat HWCAPs as the source of truth for which features are safe to use, even if the underlying hardware is technically capable of more.

I'm fairly certain that there are arm64 applications (and libraries) which check only the ID register values, and not the HWCAPs.

Architecturally, there are features which are detected via other mechanisms (e.g. CHKFEAT), for which HWCAPs are also irrelevant. Even if that happens to be ok today, there are almost certainly future uses that will not be compatible with the scheme you propose.

I don't think we can say "applications must check the HWCAPs", when we know that applications and libraries legitimately don't always do that.

Quoted 7 lines

Another significant advantage of using HWCAPs is that many applications already rely on them for feature detection. This interface allows these applications to work correctly "out-of-the-box" in a migrated environment without requiring any userspace modifications. I understand that some apps may use other detection methods; however, there it no gurantee that these applications will work correctly after migration to another machine.

I think the existince of applications that detect features by other (legitimate!) means implies that there's no guarantee that this feature is useful and will remain useful going forwards.

For example, what do you plan to do if an application or library starts doing something legitimate that causes it to become incompatible with this scheme?

I don't want to be in a position where userspace is asked to steer clear of legitimate mechanisms, or where architecture code suddently has to pick up a lot of complexity to make this work.

Quoted 7 lines
Quoted 6 lines

There's also kvm, which provides a roundabout way to query some features of the underlying hardware.

You're probably better off using/extending the idreg overrides we have in arch/arm64/kernel/pi/idreg-override.c so that you can make your cluster of heterogeneous machines look alike.

Quoted 7 lines

IIRC, idreg-override/cpuid-masking usually works for an entire machine. We actually need to have a mechanism that will work on a per-container basis. Workloads inside one cluster can have different migration/snapshot requirements. Some are pinned to a specific node, others are never migrated, while others need to be migratable across a cluster or even between clusters. We need a mechanism that can be tunable on a per-container/per-process basis.

I think that's theoretically possible, BUT it will require substantially more complexity, to address the issues that Will and I have mentioned. I don't think people are very happy to pick up that complexity.

There are many other aspects that are going to be problematic for heterogeneous migration. Even if you hide the HWCAP for a stateful feature (e.g. SME), it might appear in one machine's signal frames (and be mandatory there), but might not appear in anothers, and so migration might not work either way. Likewise, that state can appear via ptrace.

Thanks,
Mark.

#7 lore raw
Quoted 134 lines · Mark Rutland
Quoted 61 lines · Andrei Vagin

Hi Mark and Will,

Thanks for the feedback. Please read the inline comments.

Quoted 32 lines · Will Deacon
Quoted 30 lines · Mark Rutland
Quoted 17 lines · Andrei Vagin

Introduces a mechanism to inherit hardware capabilities (AT_HWCAP, AT_HWCAP2, etc.) from a parent process when they have been modified via prctl.

To support C/R operations (snapshots, live migration) in heterogeneous clusters, we must ensure that processes utilize CPU features available on all potential target nodes. To solve this, we need to advertise a common feature set across the cluster.

This patch adds a new mm flag MMF_USER_HWCAP, which is set when the auxiliary vector is modified via prctl(PR_SET_MM, PR_SET_MM_AUXV). When execve() is called, if the current process has MMF_USER_HWCAP set, the HWCAP values are extracted from the current auxiliary vector and stored in the linux_binprm structure. These values are then used to populate the auxiliary vector of the new process, effectively inheriting the hardware capabilities.

Quoted 4 lines

The inherited HWCAPs are masked with the hardware capabilities supported by the current kernel to ensure that we don't report more features than actually supported. This is important to avoid unexpected behavior, especially for processes with additional privileges.

At a high level, I don't think that's going to be sufficient:

* On an architecture with other userspace accessible feature identification mechanism registers (e.g. ID registers), userspace might read those. So you might need to hide stuff there too, and that's going to require architecture-specific interfaces to manage.

Quoted 8 lines
Quoted 8 lines

It's possible that some code checks HWCAPs and others check ID registers, and mismatch between the two could be problematic.

* If the HWCAPs can be inherited by a more privileged task, then a malicious user could use this to hide security features (e.g. shadow stack or pointer authentication on arm64), and make it easier to attack that task. While not a direct attack, it would undermine those features.

I agree with Mark that only a privileged process have to be able to mask certain hardware features. Currently, PR_SET_MM_AUXV is guarded by CAP_SYS_RESOURCE, but PR_SET_MM_MAP allows changing the auxiliary vector without specific capabilities. This is definitely the issue. To address this, I think we can consider to introduce a new prctl command to enable HWCAP inheritance explicitly.

Quoted 7 lines

Yeah, this looks like a non-starter to me on arm64. Even if it was extended to apply the same treatment to the idregs, many of the hwcap features can't actually be disabled by the kernel and so you still run the risk of a task that probes for the presence of a feature using something like a SIGILL handler or, perhaps more likely, assumes that the presence of one hwcap implies the presence of another. And then there are the applications that just base everything off the MIDR...

Quoted 7 lines

The goal of this mechanism is not to provide strict architectural enforcement or to trap the use of hardware features; rather, it is to provide a consistent discovery interface for applications. I chose the HWCAP vector because it mirrors the existing behavior of running an older kernel on newer hardware: while ID registers might report a feature as physically present, the HWCAPs will omit it if the kernel lacks support.

On arm64, the view of the ID registers that userspace gets *only* exposes features that the kernel knows about, as userspace reads of those registers are trapped+emulated by the kernel. On arm64 it's not true to say that something appears in those but not the HWCAPs.

I understand that might be different on other architectures, and so maybe this approach is sufficient on other architectures, but it is not sufficient on arm64.

Quoted 3 lines

Applications are generally expected to treat HWCAPs as the source of truth for which features are safe to use, even if the underlying hardware is technically capable of more.

I'm fairly certain that there are arm64 applications (and libraries) which check only the ID register values, and not the HWCAPs.

Architecturally, there are features which are detected via other mechanisms (e.g. CHKFEAT), for which HWCAPs are also irrelevant. Even if that happens to be ok today, there are almost certainly future uses that will not be compatible with the scheme you propose.

I don't think we can say "applications must check the HWCAPs", when we know that applications and libraries legitimately don't always do that.

Quoted 7 lines

Another significant advantage of using HWCAPs is that many applications already rely on them for feature detection. This interface allows these applications to work correctly "out-of-the-box" in a migrated environment without requiring any userspace modifications. I understand that some apps may use other detection methods; however, there it no gurantee that these applications will work correctly after migration to another machine.

I think the existince of applications that detect features by other (legitimate!) means implies that there's no guarantee that this feature is useful and will remain useful going forwards.

For example, what do you plan to do if an application or library starts doing something legitimate that causes it to become incompatible with this scheme?

I don't want to be in a position where userspace is asked to steer clear of legitimate mechanisms, or where architecture code suddently has to pick up a lot of complexity to make this work.

Quoted 7 lines
Quoted 6 lines

There's also kvm, which provides a roundabout way to query some features of the underlying hardware.

You're probably better off using/extending the idreg overrides we have in arch/arm64/kernel/pi/idreg-override.c so that you can make your cluster of heterogeneous machines look alike.

Quoted 7 lines

IIRC, idreg-override/cpuid-masking usually works for an entire machine. We actually need to have a mechanism that will work on a per-container basis. Workloads inside one cluster can have different migration/snapshot requirements. Some are pinned to a specific node, others are never migrated, while others need to be migratable across a cluster or even between clusters. We need a mechanism that can be tunable on a per-container/per-process basis.

I think that's theoretically possible, BUT it will require substantially more complexity, to address the issues that Will and I have mentioned. I don't think people are very happy to pick up that complexity.

Quoted 5 lines

There are many other aspects that are going to be problematic for heterogeneous migration. Even if you hide the HWCAP for a stateful feature (e.g. SME), it might appear in one machine's signal frames (and be mandatory there), but might not appear in anothers, and so migration might not work either way. Likewise, that state can appear via ptrace.

Hi Mark,

I understand all these points and they are valid. However, as I mentioned, we are not trying to introduce a mechanism that will strictly enforce feature sets for every container. While we would like to have that functionality, as you and will mentioned, it would require substantially more complexity to address, and maintainers would unlikely to pick up that complexity. Even masking ID registers on a per-container basis would introduce extra complexity that could make architecture maintainers unhappy. There were a few attempts to introduce container CPUID masking on x86_64 in the past.

In CRIU, we are not aiming to handle every possible workload. Our goal is to target workloads where developers are ready to cooperate and willing to make adjustments to be C/R compatible. The goal here is to provide developers with clear instructions on what they can do to ensure their applications are C/R compatible. When I say "workloads", I mean this in a broad sense. A container might pack a set of tools with different runtimes (Go, Java, libc-based). All these runtimes should detect only allowed features.

Returning to the subject of this patchset: this series extends the role of hwcaps. With this change, we would establish that hwcaps is the "source of truth" for which features an application can safely use. Any other features available on the current CPU would not be guaranteed to remain available after migration to another machine.

After this discussion, I found that the current version missed one major
thing: there should be a signal indicating that hwcaps must be used for feature detection. Since we will need to integrate this interface into libc, Go, and other runtimes, they definitely should not rely just on hwcaps by default, especially in the early stages. This can be solved via the prctl command. Libraries like libc would call prctl(PR_USER_HWCAP_ENABLED). If this returns true, the runtime knows that only the features explicitly listed in hwcaps should be used.

You are right, the controlled feature set will be limited to features the kernel knows about. And yes, we would need to report CPU features in hwcaps even if the kernel isn't directly involved in handling them. Honestly, I am not certain if this is the "right" interface for that, and I would be happy to consider other ideas. I understand that these hwcaps will not work right out of the box, but we need a way to solve this problem. Having a centralized API for CPU/kernel feature detection seems like the right direction.

As for signal frame size and extended states like SVE/SME, we aware about this problem. However, it is partly mitigated by the fact that if an application does not use some features, those states are not placed in the signal frame. In the future, when we construct/reload a signal frame, we could look at a process feature set for a process and generate a frame according to those features...

Thanks,
Andrei

reviewer
#8 lore raw
Quoted 8 lines · Andrei Vagin

Hi Mark,

I understand all these points and they are valid. However, as I mentioned, we are not trying to introduce a mechanism that will strictly enforce feature sets for every container. While we would like to have that functionality, as you and will mentioned, it would require substantially more complexity to address, and maintainers would unlikely to pick up that complexity.

The crux of my complaint here is that unless you do that (to some degree), this is not going to work reliably, even with the constraints you outline.

Further, I disagree with your proposed solution of pushing more constraints onto userspace (to also consider HWCAPs as overriding other mechainsms, etc).

I think that as-is, the approach is flawed.

Quoted 5 lines

Even masking ID registers on a per-container basis would introduce extra complexity that could make architecture maintainers unhappy. There were a few attempts to introduce container CPUID masking on x86_64 in the past.

Quoted 8 lines

In CRIU, we are not aiming to handle every possible workload. Our goal is to target workloads where developers are ready to cooperate and willing to make adjustments to be C/R compatible. The goal here is to provide developers with clear instructions on what they can do to ensure their applications are C/R compatible. When I say "workloads", I mean this in a broad sense. A container might pack a set of tools with different runtimes (Go, Java, libc-based). All these runtimes should detect only allowed features.

I do not think that arbitrary applications (and libraries!) should have to pick up additional constraints that are unnecessary without CRIU, especially where that goes against deliberate design decisions (e.g. features in arm64's HINT instruction space, which are designed to be usable in fast paths WITHOUT needing explicit checks of things like HWCAPs). Note that those typically *do* have kernel controls.

I think there's a much larger problem space than you anticipate, and adding an incomplete solution now is just going to introduce a maintenance burden.

Quoted 6 lines

Returning to the subject of this patchset: this series extends the role of hwcaps. With this change, we would establish that hwcaps is the "source of truth" for which features an application can safely use. Any other features available on the current CPU would not be guaranteed to remain available after migration to another machine.

Quoted 8 lines

After this discussion, I found that the current version missed one major
thing: there should be a signal indicating that hwcaps must be used for feature detection. Since we will need to integrate this interface into libc, Go, and other runtimes, they definitely should not rely just on hwcaps by default, especially in the early stages. This can be solved via the prctl command. Libraries like libc would call prctl(PR_USER_HWCAP_ENABLED). If this returns true, the runtime knows that only the features explicitly listed in hwcaps should be used.

I do not think we should be pushing that shape of constraint onto userspace.

Quoted 3 lines

You are right, the controlled feature set will be limited to features the kernel knows about. And yes, we would need to report CPU features in hwcaps even if the kernel isn't directly involved in handling them.

To be clear, that is not what I am arguing.

As I mentioned before, the way this works on arm64 is that the kernel only exposes what it is aware of, even in the ID regs accessible to userspace. We usually *can* hide features, and do that for cases of mismatched big.LITTLE, virtual machines, etc.

Quoted 5 lines

Honestly, I am not certain if this is the "right" interface for that, and I would be happy to consider other ideas. I understand that these hwcaps will not work right out of the box, but we need a way to solve this problem. Having a centralized API for CPU/kernel feature detection seems like the right direction.

I think that for better or worse the approach you are tkaing here simply does not solve enough of the problem to actually be worthwhile.

Quoted 4 lines

As for signal frame size and extended states like SVE/SME, we aware about this problem. However, it is partly mitigated by the fact that if an application does not use some features, those states are not placed in the signal frame.

That is not true. The kernel can and will create signal frames for architectural state that a task might never have touched.

Generally arm64 creates signal frames for features when the feature *exists*, regardless of whether the task has actively manipulated the relevant state. For example, on systems with SVE a trivial SVE signal frame gets created even if a task only uses the FPSIMD registers, and on systms with SME a TPIDR2 signal frame gets created even if the task has never read/written TPIDR2.

When restoring, an unrecognised signal frame is treated as invalid, and we can require that certain signal frames are present.

Quoted 3 lines

In the future, when we construct/reload a signal frame, we could look at a process feature set for a process and generate a frame according to those features...

When you say 'we' here, are you talking about within the kernel, or within the userspace C/R mechanism?

Mark.

#9 lore raw

Hi Mark,

Thanks for the feedback and sorry for the delay, was on vacation. Please see my comments inline.

Quoted 15 lines · Mark Rutland
Quoted 8 lines · Andrei Vagin

Hi Mark,

I understand all these points and they are valid. However, as I mentioned, we are not trying to introduce a mechanism that will strictly enforce feature sets for every container. While we would like to have that functionality, as you and will mentioned, it would require substantially more complexity to address, and maintainers would unlikely to pick up that complexity.

The crux of my complaint here is that unless you do that (to some degree), this is not going to work reliably, even with the constraints you outline.

Quoted 5 lines

Further, I disagree with your proposed solution of pushing more constraints onto userspace (to also consider HWCAPs as overriding other mechainsms, etc).

I think that as-is, the approach is flawed.

I would really appreciate it if we could move this conversation toward how we can make it work.

Quoted 22 lines
Quoted 6 lines

Even masking ID registers on a per-container basis would introduce extra complexity that could make architecture maintainers unhappy. There were a few attempts to introduce container CPUID masking on x86_64 in the past.

Quoted 8 lines

In CRIU, we are not aiming to handle every possible workload. Our goal is to target workloads where developers are ready to cooperate and willing to make adjustments to be C/R compatible. The goal here is to provide developers with clear instructions on what they can do to ensure their applications are C/R compatible. When I say "workloads", I mean this in a broad sense. A container might pack a set of tools with different runtimes (Go, Java, libc-based). All these runtimes should detect only allowed features.

I do not think that arbitrary applications (and libraries!) should have to pick up additional constraints that are unnecessary without CRIU, especially where that goes against deliberate design decisions (e.g. features in arm64's HINT instruction space, which are designed to be usable in fast paths WITHOUT needing explicit checks of things like HWCAPs). Note that those typically *do* have kernel controls.

Quoted 3 lines

I think there's a much larger problem space than you anticipate, and adding an incomplete solution now is just going to introduce a maintenance burden.

I am not adding arbitrary constraints for standard non-CRIU use cases. Previously, I suggested that standard libraries would need to call prctl to determine if hwcaps should be used for feature detection. However, we can avoid this extra syscall by adding the new HWCAP2_CR bit. Then libraries will simply check this bit in auxv[AT_HWCAP2], meaning the overhead for "non-criu" cases is just a single bit check.

As for HINT instructions, there are two class of instructions.

The first one doesn't change a process state and they are not required any special handling in term of checkpoint/restore. If a process is checkpointed on a newer cpu, and restore it on an older cpu, the older hardware will simply skip over that instructions. The architectural state (registers, memory) should remain consistent.

The second class such as PAC are instructions that actually change a process state. These instructions require kernel/userspace coordination. For example, usage of PAC keys can be controlled from userspace via prctl. I mean when support for new instructions is implemented in the kernel, we will need to consider that userspace should be able to control them.

Quoted 16 lines
Quoted 7 lines

Returning to the subject of this patchset: this series extends the role of hwcaps. With this change, we would establish that hwcaps is the "source of truth" for which features an application can safely use. Any other features available on the current CPU would not be guaranteed to remain available after migration to another machine.

Quoted 8 lines

After this discussion, I found that the current version missed one major
thing: there should be a signal indicating that hwcaps must be used for feature detection. Since we will need to integrate this interface into libc, Go, and other runtimes, they definitely should not rely just on hwcaps by default, especially in the early stages. This can be solved via the prctl command. Libraries like libc would call prctl(PR_USER_HWCAP_ENABLED). If this returns true, the runtime knows that only the features explicitly listed in hwcaps should be used.

Quoted 2 lines

I do not think we should be pushing that shape of constraint onto userspace.

Look at the previous command.

Quoted 11 lines
Quoted 4 lines

You are right, the controlled feature set will be limited to features the kernel knows about. And yes, we would need to report CPU features in hwcaps even if the kernel isn't directly involved in handling them.

To be clear, that is not what I am arguing.

As I mentioned before, the way this works on arm64 is that the kernel only exposes what it is aware of, even in the ID regs accessible to userspace. We usually *can* hide features, and do that for cases of mismatched big.LITTLE, virtual machines, etc.

I understand that. My point was that the kernel would need to report features in hwcaps even if they don't require specific kernel-side handling.

Quoted 9 lines
Quoted 6 lines

Honestly, I am not certain if this is the "right" interface for that, and I would be happy to consider other ideas. I understand that these hwcaps will not work right out of the box, but we need a way to solve this problem. Having a centralized API for CPU/kernel feature detection seems like the right direction.

I think that for better or worse the approach you are tkaing here simply does not solve enough of the problem to actually be worthwhile.

This approach mimics solutions that some CRIU users are already implementing in userspace, but those only work when the user controls/ recompiles all their libraries. I am open to other ideas, but we need a path forward.

Quoted 16 lines
Quoted 5 lines

As for signal frame size and extended states like SVE/SME, we aware about this problem. However, it is partly mitigated by the fact that if an application does not use some features, those states are not placed in the signal frame.

That is not true. The kernel can and will create signal frames for architectural state that a task might never have touched.

Generally arm64 creates signal frames for features when the feature *exists*, regardless of whether the task has actively manipulated the relevant state. For example, on systems with SVE a trivial SVE signal frame gets created even if a task only uses the FPSIMD registers, and on systms with SME a TPIDR2 signal frame gets created even if the task has never read/written TPIDR2.

Quoted 2 lines

When restoring, an unrecognised signal frame is treated as invalid, and we can require that certain signal frames are present.

You are right; that was my mistake. My only explanation for why we don't see this failure often is that C/R is rarely triggered while a process is actually
inside a signal handler. This is definitely a problem that still needs to be solved.

Quoted 7 lines
Quoted 4 lines

In the future, when we construct/reload a signal frame, we could look at a process feature set for a process and generate a frame according to those features...

When you say 'we' here, are you talking about within the kernel, or within the userspace C/R mechanism?

... within the kernel.

Thanks,
Andrei

Tested-by on v5 1/4
#10 lore raw
In reply to Andrei Vagin
Quoted 17 lines · Andrei Vagin

Introduces a mechanism to inherit hardware capabilities (AT_HWCAP, AT_HWCAP2, etc.) from a parent process when they have been modified via prctl.

To support C/R operations (snapshots, live migration) in heterogeneous clusters, we must ensure that processes utilize CPU features available on all potential target nodes. To solve this, we need to advertise a common feature set across the cluster.

This patch adds a new mm flag MMF_USER_HWCAP, which is set when the auxiliary vector is modified via prctl(PR_SET_MM, PR_SET_MM_AUXV). When execve() is called, if the current process has MMF_USER_HWCAP set, the HWCAP values are extracted from the current auxiliary vector and stored in the linux_binprm structure. These values are then used to populate the auxiliary vector of the new process, effectively inheriting the hardware capabilities.

Quoted 8 lines

The inherited HWCAPs are masked with the hardware capabilities supported by the current kernel to ensure that we don't report more features than actually supported. This is important to avoid unexpected behavior, especially for processes with additional privileges.

v5 fixed the issue I've observed here:

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

Quoted 241 lines
---
  fs/binfmt_elf.c          | 13 ++++++---
  fs/binfmt_elf_fdpic.c    | 13 ++++++---
  fs/exec.c                | 62 ++++++++++++++++++++++++++++++++++++++++
  include/linux/binfmts.h  | 11 +++++++
  include/linux/mm_types.h |  2 ++
  kernel/fork.c            |  3 ++
  kernel/sys.c             |  5 +++-
  7 files changed, 100 insertions(+), 9 deletions(-)
fs/binfmt_elf.c +9 -4
index fb857faaf0d6..d99db73c76f0 100644
@@ -183,6 +183,7 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
  	int ei_index;
  	const struct cred *cred = current_cred();
  	struct vm_area_struct *vma;
+	bool user_hwcap = mm_flags_test(MMF_USER_HWCAP, mm);
 
  	/*
  	 * In some cases (e.g. Hyper-Threading), we want to avoid L1
@@ -247,7 +248,8 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
  	 */
  	ARCH_DLINFO;
  #endif
-	NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
+	NEW_AUX_ENT(AT_HWCAP, user_hwcap ?
+			      (bprm->hwcap & ELF_HWCAP) : ELF_HWCAP);
  	NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
  	NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
  	NEW_AUX_ENT(AT_PHDR, phdr_addr);
@@ -265,13 +267,16 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
  	NEW_AUX_ENT(AT_SECURE, bprm->secureexec);
  	NEW_AUX_ENT(AT_RANDOM, (elf_addr_t)(unsigned long)u_rand_bytes);
  #ifdef ELF_HWCAP2
-	NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2);
+	NEW_AUX_ENT(AT_HWCAP2, user_hwcap ?
+			       (bprm->hwcap2 & ELF_HWCAP2) : ELF_HWCAP2);
  #endif
  #ifdef ELF_HWCAP3
-	NEW_AUX_ENT(AT_HWCAP3, ELF_HWCAP3);
+	NEW_AUX_ENT(AT_HWCAP3, user_hwcap ?
+			       (bprm->hwcap3 & ELF_HWCAP3) : ELF_HWCAP3);
  #endif
  #ifdef ELF_HWCAP4
-	NEW_AUX_ENT(AT_HWCAP4, ELF_HWCAP4);
+	NEW_AUX_ENT(AT_HWCAP4, user_hwcap ?
+			       (bprm->hwcap4 & ELF_HWCAP4) : ELF_HWCAP4);
  #endif
  	NEW_AUX_ENT(AT_EXECFN, bprm->exec);
  	if (k_platform) {
fs/binfmt_elf_fdpic.c +9 -4
index 95b65aab7daa..92c88471455a 100644
@@ -508,6 +508,7 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm,
  	unsigned long flags = 0;
  	int ei_index;
  	elf_addr_t *elf_info;
+	bool user_hwcap = mm_flags_test(MMF_USER_HWCAP, mm);
 
  #ifdef CONFIG_MMU
  	/* In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
@@ -629,15 +630,19 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm,
  	 */
  	ARCH_DLINFO;
  #endif
-	NEW_AUX_ENT(AT_HWCAP,	ELF_HWCAP);
+	NEW_AUX_ENT(AT_HWCAP,	user_hwcap ?
+				(bprm->hwcap & ELF_HWCAP) : ELF_HWCAP);
  #ifdef ELF_HWCAP2
-	NEW_AUX_ENT(AT_HWCAP2,	ELF_HWCAP2);
+	NEW_AUX_ENT(AT_HWCAP2,	user_hwcap ?
+				(bprm->hwcap2 & ELF_HWCAP2) : ELF_HWCAP2);
  #endif
  #ifdef ELF_HWCAP3
-	NEW_AUX_ENT(AT_HWCAP3,	ELF_HWCAP3);
+	NEW_AUX_ENT(AT_HWCAP3,	user_hwcap ?
+				(bprm->hwcap3 & ELF_HWCAP3) : ELF_HWCAP3);
  #endif
  #ifdef ELF_HWCAP4
-	NEW_AUX_ENT(AT_HWCAP4,	ELF_HWCAP4);
+	NEW_AUX_ENT(AT_HWCAP4,	user_hwcap ?
+				(bprm->hwcap4 & ELF_HWCAP4) : ELF_HWCAP4);
  #endif
  	NEW_AUX_ENT(AT_PAGESZ,	PAGE_SIZE);
  	NEW_AUX_ENT(AT_CLKTCK,	CLOCKS_PER_SEC);
fs/exec.c +62 -0
index 9ea3a775d51e..1cd7d87a0e79 100644
@@ -1775,6 +1775,65 @@ static int bprm_execve(struct linux_binprm *bprm)
  	return retval;
  }
 
+static void inherit_hwcap(struct linux_binprm *bprm)
+{
+	struct mm_struct *mm = current->mm;
+	bool compat = in_compat_syscall();
+	int i, n;
+
+#ifdef ELF_HWCAP4
+	n = 4;
+#elif defined(ELF_HWCAP3)
+	n = 3;
+#elif defined(ELF_HWCAP2)
+	n = 2;
+#else
+	n = 1;
+#endif
+
+	for (i = 0; n && i < AT_VECTOR_SIZE; i += 2) {
+		unsigned long type, val;
+
+		if (!compat) {
+			type = mm->saved_auxv[i];
+			val = mm->saved_auxv[i + 1];
+		} else {
+			compat_uptr_t *auxv = (compat_uptr_t *)mm->saved_auxv;
+
+			type = auxv[i];
+			val = auxv[i + 1];
+		}
+
+		switch (type) {
+		case AT_NULL:
+			goto done;
+		case AT_HWCAP:
+			bprm->hwcap = val;
+			break;
+#ifdef ELF_HWCAP2
+		case AT_HWCAP2:
+			bprm->hwcap2 = val;
+			break;
+#endif
+#ifdef ELF_HWCAP3
+		case AT_HWCAP3:
+			bprm->hwcap3 = val;
+			break;
+#endif
+#ifdef ELF_HWCAP4
+		case AT_HWCAP4:
+			bprm->hwcap4 = val;
+			break;
+#endif
+		default:
+			continue;
+		}
+		n--;
+	}
+done:
+	mm_flags_set(MMF_USER_HWCAP, bprm->mm);
+}
+
  static int do_execveat_common(int fd, struct filename *filename,
  			      struct user_arg_ptr argv,
  			      struct user_arg_ptr envp,
@@ -1843,6 +1902,9 @@ static int do_execveat_common(int fd, struct filename *filename,
  			     current->comm, bprm->filename);
  	}
 
+	if (mm_flags_test(MMF_USER_HWCAP, current->mm))
+		inherit_hwcap(bprm);
+
  	return bprm_execve(bprm);
  }
 
include/linux/binfmts.h +11 -0
index 65abd5ab8836..94a3dcf9b1d2 100644
@@ -2,6 +2,7 @@
  #ifndef _LINUX_BINFMTS_H
  #define _LINUX_BINFMTS_H
 
+#include <linux/elf.h>
  #include <linux/sched.h>
  #include <linux/unistd.h>
  #include <asm/exec.h>
@@ -67,6 +68,16 @@ struct linux_binprm {
  	unsigned long exec;
 
  	struct rlimit rlim_stack; /* Saved RLIMIT_STACK used during exec. */
+	unsigned long hwcap;
+#ifdef ELF_HWCAP2
+	unsigned long hwcap2;
+#endif
+#ifdef ELF_HWCAP3
+	unsigned long hwcap3;
+#endif
+#ifdef ELF_HWCAP4
+	unsigned long hwcap4;
+#endif
 
  	char buf[BINPRM_BUF_SIZE];
  } __randomize_layout;
include/linux/mm_types.h +2 -0
index 3cc8ae722886..62dde645f469 100644
@@ -1919,6 +1919,8 @@ enum {
  #define MMF_TOPDOWN		31	/* mm searches top down by default */
  #define MMF_TOPDOWN_MASK	BIT(MMF_TOPDOWN)
 
+#define MMF_USER_HWCAP		32	/* user-defined HWCAPs */
+
  #define MMF_INIT_LEGACY_MASK	(MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\
  				 MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\
  				 MMF_VM_MERGE_ANY_MASK | MMF_TOPDOWN_MASK)
kernel/fork.c +3 -0
index bc2bf58b93b6..2ac277aa078c 100644
@@ -1105,6 +1105,9 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 
  		__mm_flags_overwrite_word(mm, mmf_init_legacy_flags(flags));
  		mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK;
+
+		if (mm_flags_test(MMF_USER_HWCAP, current->mm))
+			mm_flags_set(MMF_USER_HWCAP, mm);
  	} else {
  		__mm_flags_overwrite_word(mm, default_dump_filter);
  		mm->def_flags = 0;
kernel/sys.c +3 -1
index cdbf8513caf6..e4b0fa2f6845 100644
@@ -2157,8 +2157,10 @@ static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data
  	 * not introduce additional locks here making the kernel
  	 * more complex.
  	 */
-	if (prctl_map.auxv_size)
+	if (prctl_map.auxv_size) {
  		memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
+		mm_flags_set(MMF_USER_HWCAP, mm);
+	}
 
  	mmap_read_unlock(mm);
  	return 0;

kernel/sys.c static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr, · line 2198

Quoted 7 lines
kernel/sys.c +1 -0
@@ -2190,6 +2192,7 @@ static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
 
  	task_lock(current);
  	memcpy(mm->saved_auxv, user_auxv, len);
+	mm_flags_set(MMF_USER_HWCAP, mm);
  	task_unlock(current);
 
  	return 0;

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
#11 lore raw

PATCH v5 2/4 arm64: elf: clear MMF_USER_HWCAP on architecture switch

In reply to Andrei Vagin

The HWCAP bits have different meanings between AArch64 and AArch32, so HWCAP inheritance is not applicable when switching architectures. Inherited HWCAP vectors can lead to unpredictable side effects. For example, bit 0 in AArch64 signifies FP support, whereas in AArch32 it signifies SWP instruction support.

Fix this by clearing the MMF_USER_HWCAP flag in SET_PERSONALITY and COMPAT_SET_PERSONALITY if the architecture is changing. This ensures that create_elf_tables() will use the default kernel HWCAPs for the new process.

---
 arch/arm64/include/asm/elf.h | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/arm64/include/asm/elf.h +9 -3
index d2779d604c7b..2049d42e2e6a 100644
@@ -160,7 +160,10 @@ typedef struct user_fpsimd_state elf_fpregset_t;
 
 #define SET_PERSONALITY(ex)						\
 ({									\
-	clear_thread_flag(TIF_32BIT);					\
+	if (test_thread_flag(TIF_32BIT)) {				\
+		mm_flags_clear(MMF_USER_HWCAP, current->mm);		\
+		clear_thread_flag(TIF_32BIT);				\
+	}								\
 	current->personality &= ~READ_IMPLIES_EXEC;			\
 })
 
@@ -223,8 +226,11 @@ int compat_elf_check_arch(const struct elf32_hdr *);
  */
 #define COMPAT_SET_PERSONALITY(ex)					\
 ({									\
-	set_thread_flag(TIF_32BIT);					\
- })
+	if (!test_thread_flag(TIF_32BIT)) {				\
+		mm_flags_clear(MMF_USER_HWCAP, current->mm);		\
+		set_thread_flag(TIF_32BIT);				\
+	}								\
+})
 #ifdef CONFIG_COMPAT_VDSO
 #define COMPAT_ARCH_DLINFO						\
 do {									\
Signature · 1 line
2.53.0.983.g0bb29b3bc5-goog
#12 lore raw

PATCH v5 3/4 mm: synchronize saved_auxv access with arg_lock

In reply to Andrei Vagin

The mm->saved_auxv array stores the auxiliary vector, which can be modified via prctl(PR_SET_MM_AUXV) or prctl(PR_SET_MM_MAP). Previously, accesses to saved_auxv were not synchronized. This was a intentional trade-off, as the vector was only used to provide information to userspace via /proc/PID/auxv or prctl(PR_GET_AUXV), and consistency between the auxv values left to userspace.

With the introduction of hardware capability (HWCAP) inheritance during execve, the kernel now relies on the contents of saved_auxv to configure the execution environment of new processes. An unsynchronized read during execve could result in a new process inheriting an inconsistent set of capabilities if the parent process updates its auxiliary vector concurrently.

While it is still not strictly required to guarantee the consistency of auxv values on the kernel side, doing so is relatively straightforward. This change implements synchronization using arg_lock.

---
 fs/exec.c                |  2 ++
 fs/proc/base.c           | 12 +++++++++---
 include/linux/mm_types.h |  1 -
 kernel/fork.c            |  7 ++++++-
 kernel/sys.c             | 29 ++++++++++++++---------------
 5 files changed, 31 insertions(+), 20 deletions(-)
fs/exec.c +2 -0
index 1cd7d87a0e79..dea868d058fa 100644
@@ -1791,6 +1791,7 @@ static void inherit_hwcap(struct linux_binprm *bprm)
 	n = 1;
 #endif
 
+	spin_lock(&mm->arg_lock);
 	for (i = 0; n && i < AT_VECTOR_SIZE; i += 2) {
 		unsigned long type, val;
 
@@ -1831,6 +1832,7 @@ static void inherit_hwcap(struct linux_binprm *bprm)
 		n--;
 	}
 done:
+	spin_unlock(&mm->arg_lock);
 	mm_flags_set(MMF_USER_HWCAP, bprm->mm);
 }
 
fs/proc/base.c +9 -3
index 4c863d17dfb4..b5496cec888e 100644
@@ -1083,14 +1083,20 @@ static ssize_t auxv_read(struct file *file, char __user *buf,
 {
 	struct mm_struct *mm = file->private_data;
 	unsigned int nwords = 0;
+	unsigned long saved_auxv[AT_VECTOR_SIZE];
 
 	if (!mm)
 		return 0;
+
+	spin_lock(&mm->arg_lock);
+	memcpy(saved_auxv, mm->saved_auxv, sizeof(saved_auxv));
+	spin_unlock(&mm->arg_lock);
+
 	do {
 		nwords += 2;
-	} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
-	return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
-				       nwords * sizeof(mm->saved_auxv[0]));
+	} while (saved_auxv[nwords - 2] != 0); /* AT_NULL */
+	return simple_read_from_buffer(buf, count, ppos, saved_auxv,
+				       nwords * sizeof(saved_auxv[0]));
 }
 
 static const struct file_operations proc_auxv_operations = {
include/linux/mm_types.h +0 -1
index 62dde645f469..10351af5851b 100644
@@ -1255,7 +1255,6 @@ struct mm_struct {
 		unsigned long start_code, end_code, start_data, end_data;
 		unsigned long start_brk, brk, start_stack;
 		unsigned long arg_start, arg_end, env_start, env_end;
-
 		unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
 
 #ifdef CONFIG_ARCH_HAS_ELF_CORE_EFLAGS
kernel/fork.c +6 -1
index 2ac277aa078c..3880ce0d44f9 100644
@@ -1106,8 +1106,13 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 		__mm_flags_overwrite_word(mm, mmf_init_legacy_flags(flags));
 		mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK;
 
-		if (mm_flags_test(MMF_USER_HWCAP, current->mm))
+		if (mm_flags_test(MMF_USER_HWCAP, current->mm)) {
+			spin_lock(&current->mm->arg_lock);
 			mm_flags_set(MMF_USER_HWCAP, mm);
+			memcpy(mm->saved_auxv, current->mm->saved_auxv,
+			       sizeof(mm->saved_auxv));
+			spin_unlock(&current->mm->arg_lock);
+		}
 	} else {
 		__mm_flags_overwrite_word(mm, default_dump_filter);
 		mm->def_flags = 0;
kernel/sys.c +14 -15
index e4b0fa2f6845..c679b5797e73 100644
@@ -2147,20 +2147,11 @@ static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data
 	mm->arg_end	= prctl_map.arg_end;
 	mm->env_start	= prctl_map.env_start;
 	mm->env_end	= prctl_map.env_end;
-	spin_unlock(&mm->arg_lock);
-
-	/*
-	 * Note this update of @saved_auxv is lockless thus
-	 * if someone reads this member in procfs while we're
-	 * updating -- it may get partly updated results. It's
-	 * known and acceptable trade off: we leave it as is to
-	 * not introduce additional locks here making the kernel
-	 * more complex.
-	 */
 	if (prctl_map.auxv_size) {
-		memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
 		mm_flags_set(MMF_USER_HWCAP, mm);
+		memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
 	}
+	spin_unlock(&mm->arg_lock);
 
 	mmap_read_unlock(mm);
 	return 0;
@@ -2190,10 +2181,10 @@ static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
 
 	BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
 
-	task_lock(current);
-	memcpy(mm->saved_auxv, user_auxv, len);
+	spin_lock(&mm->arg_lock);
 	mm_flags_set(MMF_USER_HWCAP, mm);
-	task_unlock(current);
+	memcpy(mm->saved_auxv, user_auxv, len);
+	spin_unlock(&mm->arg_lock);
 
 	return 0;
 }
@@ -2481,9 +2472,17 @@ static inline int prctl_get_mdwe(unsigned long arg2, unsigned long arg3,
 static int prctl_get_auxv(void __user *addr, unsigned long len)
 {
 	struct mm_struct *mm = current->mm;
+	unsigned long auxv[AT_VECTOR_SIZE];
 	unsigned long size = min_t(unsigned long, sizeof(mm->saved_auxv), len);
 
-	if (size && copy_to_user(addr, mm->saved_auxv, size))
+	if (!size)
+		return sizeof(mm->saved_auxv);
+
+	spin_lock(&mm->arg_lock);
+	memcpy(auxv, mm->saved_auxv, size);
+	spin_unlock(&mm->arg_lock);
+
+	if (copy_to_user(addr, auxv, size))
 		return -EFAULT;
 	return sizeof(mm->saved_auxv);
 }
Signature · 1 line
2.53.0.983.g0bb29b3bc5-goog
#13 lore raw

PATCH v5 4/4 selftests/exec: add test for HWCAP inheritance

In reply to Andrei Vagin

Verify that HWCAPs are correctly inherited/preserved across execve() when modified via prctl(PR_SET_MM_AUXV).

The test performs the following steps:
* reads the current AUXV using prctl(PR_GET_AUXV);
* finds an HWCAP entry and toggles its most significant bit;
* replaces the AUXV of the current process with the modified one using prctl(PR_SET_MM, PR_SET_MM_AUXV);
* executes itself to verify that the new program sees the modified HWCAP value.

---
 tools/testing/selftests/exec/.gitignore      |   1 +
 tools/testing/selftests/exec/Makefile        |   1 +
 tools/testing/selftests/exec/hwcap_inherit.c | 105 +++++++++++++++++++
 3 files changed, 107 insertions(+)
 create mode 100644 tools/testing/selftests/exec/hwcap_inherit.c
tools/testing/selftests/exec/.gitignore +1 -0
index 7f3d1ae762ec..2ff245fd0ba6 100644
@@ -19,3 +19,4 @@ null-argv
 xxxxxxxx*
 pipe
 S_I*.test
+hwcap_inherit
\ No newline at end of file
tools/testing/selftests/exec/Makefile +1 -0
index 45a3cfc435cf..e73005965e05 100644
@@ -20,6 +20,7 @@ TEST_FILES := Makefile
 TEST_GEN_PROGS += recursion-depth
 TEST_GEN_PROGS += null-argv
 TEST_GEN_PROGS += check-exec
+TEST_GEN_PROGS += hwcap_inherit
 
 EXTRA_CLEAN := $(OUTPUT)/subdir.moved $(OUTPUT)/execveat.moved $(OUTPUT)/xxxxx*	\
 	       $(OUTPUT)/S_I*.test
tools/testing/selftests/exec/hwcap_inherit.c new file +105 -0

105 lines not on this page · raw message

Signature · 1 line
2.53.0.983.g0bb29b3bc5-goog