exec: randomize the argument block's in-page offset
Subsystems (4)
cover exec: randomize the argument block's in-page offset
Patch 1 randomizes where the argv, envp and execfn strings sit within the first stack page, as KSPP issue 283 asks; today their low address bits never change. Patch 2 adds a selftest.
Patch 1 repeats bprm_fill_uid()'s set-id checks; I can send a shared helper as a preparatory patch if preferred.
Tested in QEMU, compared with unpatched for-next/execve (b36b990c50b1), on x86_64 with ia32, arm64 (4K pages with AArch32, and 64K pages), and parisc 32-bit and 64-bit (STACK_GROWSUP):
- The selftest passes; unpatched, it fails.
- 10000 execs give 3709-3764 distinct offsets with 4K pages and 9259 with 64K, as a uniform draw predicts.
- Cost: about 0.5 more minor page faults per exec on average, and up to a page of RLIMIT_STACK; exec time is within noise.
- No change in LTP execve and execveat, CRIU dump and restore, or programs that rewrite argv[0] (setproctitle(), sshd, postgres), and no KASAN, UBSAN or lockdep reports under exec and chmod races.
The series is based on mainline (864c07de0c7d), which now includes for-next/execve and the const mnt_idmap conversion. On that base it adds no W=1 warnings with GCC 14 or clang 19 on x86_64 and arm64, and the selftest passes on x86_64.
Brian Witte (2):
exec: randomize the argument block's offset within its page selftests/exec: test the argument block's in-page offset
fs/exec.c | 32 +++ tools/testing/selftests/exec/.gitignore | 3 + tools/testing/selftests/exec/Makefile | 5 + .../testing/selftests/exec/argblock-offset.c | 214 ++++++++++++++++++ .../testing/selftests/exec/argblock-report.c | 24 ++ 5 files changed, 278 insertions(+) create mode 100644 tools/testing/selftests/exec/argblock-offset.c create mode 100644 tools/testing/selftests/exec/argblock-report.c
base-commit: 864c07de0c7dcbca58b61625dfa2e461ae63b242
Signature · 1 line
2.47.3
PATCH 1/2 exec: randomize the argument block's offset within its page
bprm->p starts one word below STACK_TOP_MAX, and setup_arg_pages() moves the stack only by whole pages, so the argv, envp and execfn strings sit at the same offset within their page on every exec. Three execs on x86_64 put argv[0] at 0x7ffeee30af9f, 0x7ffdf3709f9f and 0x7ffc871e0f9f.
Lower bprm->p by a random amount under a page before the strings are
copied: about 12 bits of entropy with 4K pages and 16 with 64K. bprm->argmin is derived from bprm->p, so the argument size limit is unchanged, but up to a page of RLIMIT_STACK is used.
ADDR_NO_RANDOMIZE disables the offset; PF_RANDOMIZE cannot be used, since load_elf_binary() sets it after the strings are copied. PER_CLEAR_ON_SETID also clears the flag for set-id execs only after the copy, and moving the strings then would mean copying them all. So bprm_fill_uid()'s set-id checks are repeated early, without its inode_lock(), and a set-id exec gets the offset regardless of the flag. A racing chmod or sysctl write can make the offset disagree with the ELF loader but never affects the credentials. Only the file passed to execve() is checked: set-id interpreters, file capabilities and LSM transitions keep today's fixed offset under the flag. The checks need bprm->file, so they run in bprm_mm_init(), not create_init_stack_vma().
The idea comes from PaX RANDUSTACK; linux-hardened carries a version by Daniel Micay and Levente Polyak.
--- fs/exec.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
fs/exec.c +32 -0
@@ -70,6 +70,7 @@#include <linux/user_events.h> #include <linux/rseq.h> #include <linux/ksm.h> +#include <linux/random.h> #include <linux/uaccess.h> #include <asm/mmu_context.h>
@@ -195,6 +196,33 @@ static bool valid_arg_len(struct linux_binprm *bprm, long len) return len <= MAX_ARG_STRLEN; } +/* +* Called before credentials exist: treat ADDR_NO_RANDOMIZE as cleared for +* files bprm_fill_uid() will treat as set-id. +*/ +static bool bprm_randomize_args (struct linux_binprm *bprm) +{ +struct file *file = bprm->file; +const struct mnt_idmap *idmap =file_mnt_idmap (file); +struct inode *inode =file_inode (file); +/* bprm->cred, not yet set, will be in the caller's namespace. */ +struct user_namespace *ns =current_user_ns (); +umode_t mode; + +if (!READ_ONCE (randomize_va_space)) +return false; +if (!(current->personality &ADDR_NO_RANDOMIZE )) +return true; +if (!mnt_may_suid (file->f_path.mnt) ||task_no_new_privs (current)) +return false; + mode =READ_ONCE (inode->i_mode); +if (!(mode &S_ISUID ) && + (mode & (S_ISGID |S_IXGRP )) != (S_ISGID |S_IXGRP )) +return false; +return vfsuid_has_mapping (ns,i_uid_into_vfsuid (idmap, inode)) && +vfsgid_has_mapping (ns,i_gid_into_vfsgid (idmap, inode)); +} +#else static inline void acct_arg_size (struct linux_binprm *bprm,unsigned long pages)
@@ -279,6 +307,10 @@ static int bprm_mm_init(struct linux_binprm *bprm) err =create_init_stack_vma (bprm->mm, &bprm->vma, &bprm->p);if (err) goto err; + +/* Randomize the strings' offset within their page; keep the top word. */ +if (bprm_randomize_args (bprm)) + bprm->p -=get_random_u32_below (PAGE_SIZE -sizeof (void *));#endif return 0 ;
Signature · 1 line
2.47.3
PATCH 2/2 selftests/exec: test the argument block's in-page offset
In reply to Brian WitteExec a helper, argblock-report, 64 times per case. Each case checks that the in-page offset of argv[0] varies exactly when the stack top chosen by the ELF loader does:
- under ADDR_NO_RANDOMIZE: fixed;
- by default: random;
- a set-user-ID copy under ADDR_NO_RANDOMIZE: random;
- that copy on a nosuid mount, under no_new_privs, or in a new user namespace where its owner has no mapping: fixed.
The tests use the kselftest harness. The fixture keeps the copy in a private temporary directory and removes it on teardown. The copy is owned by the caller, so no privilege is needed.
--- tools/testing/selftests/exec/.gitignore | 3 + tools/testing/selftests/exec/Makefile | 5 + .../testing/selftests/exec/argblock-offset.c | 214 ++++++++++++++++++ .../testing/selftests/exec/argblock-report.c | 24 ++ 4 files changed, 246 insertions(+) create mode 100644 tools/testing/selftests/exec/argblock-offset.c create mode 100644 tools/testing/selftests/exec/argblock-report.c
tools/testing/selftests/exec/.gitignore +3 -0
@@ -9,6 +9,9 @@ execveat.ephemeral execveat.denatured non-regular null-argv +/argblock-offset +/argblock-offset.* +/argblock-report /check-exec /false /inc
tools/testing/selftests/exec/Makefile +5 -0
@@ -21,6 +21,11 @@ TEST_GEN_PROGS += recursion-depth TEST_GEN_PROGS += null-argv TEST_GEN_PROGS += check-exec +# The argument block's in-page offset must be random exactly when the +# ELF loader randomizes the stack. Unprivileged. +TEST_GEN_PROGS += argblock-offset +TEST_GEN_FILES += argblock-report + # binfmt_misc must not be reachable as an exec source or as a stacking layer, # or an 'F' entry can pin the instance that owns it. Unprivileged, no bpf. TEST_GEN_PROGS += binfmt_misc_selfpin
tools/testing/selftests/exec/argblock-offset.c new file +214 -0
214 lines not on this page · raw message
tools/testing/selftests/exec/argblock-report.c new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Helper for argblock-offset: print the in-page offset of argv[0] and the +* end of the [stack] VMA, which the ELF loader chooses. +*/ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int main (int argc,char **argv) +{ +unsigned long end =0 ; +char line[256 ]; +FILE *maps =fopen ("/proc/self/maps" ,"r" ); + +while (maps &&fgets (line,sizeof (line), maps)) +if (strstr (line,"[stack]" )) +sscanf (line,"%*[0-9a-f]-%lx" , &end); +if (argc <1 || !end) +return 1 ; +printf ("%lx %lx\n" , (unsigned long )argv[0 ] & (sysconf (_SC_PAGESIZE) -1 ), + end); +return 0 ; +}
Signature · 1 line
2.47.3