Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 084ebf7

Browse files
committed
execve: Keep bprm->argmin behind CONFIG_MMU
When argmin was added in commit 655c16a ("exec: separate MM_ANONPAGES and RLIMIT_STACK accounting"), it was intended only for validating stack limits on CONFIG_MMU[1]. All checking for reaching the limit (argmin) is wrapped in CONFIG_MMU ifdef checks, though setting argmin was not. That argmin is only supposed to be used under CONFIG_MMU was rediscovered recently[2], and I don't want to trip over this again. Move argmin's declaration into the existing CONFIG_MMU area, and add helpers functions so the MMU tests can be consolidated. Link: https://lore.kernel.org/all/20181126122307.GA1660@redhat.com [1] Link: https://lore.kernel.org/all/202406211253.7037F69@keescook/ [2] Link: https://lore.kernel.org/r/20240621205046.4001362-1-kees@kernel.org Signed-off-by: Kees Cook <kees@kernel.org>
1 parent 2a97388 commit 084ebf7

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

fs/exec.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,23 @@ static int count_strings_kernel(const char *const *argv)
486486
return i;
487487
}
488488

489+
static inline int bprm_set_stack_limit(struct linux_binprm *bprm,
490+
unsigned long limit)
491+
{
492+
#ifdef CONFIG_MMU
493+
bprm->argmin = bprm->p - limit;
494+
#endif
495+
return 0;
496+
}
497+
static inline bool bprm_hit_stack_limit(struct linux_binprm *bprm)
498+
{
499+
#ifdef CONFIG_MMU
500+
return bprm->p < bprm->argmin;
501+
#else
502+
return false;
503+
#endif
504+
}
505+
489506
/*
490507
* Calculate bprm->argmin from:
491508
* - _STK_LIM
@@ -532,8 +549,7 @@ static int bprm_stack_limits(struct linux_binprm *bprm)
532549
return -E2BIG;
533550
limit -= ptr_size;
534551

535-
bprm->argmin = bprm->p - limit;
536-
return 0;
552+
return bprm_set_stack_limit(bprm, limit);
537553
}
538554

539555
/*
@@ -571,10 +587,8 @@ static int copy_strings(int argc, struct user_arg_ptr argv,
571587
pos = bprm->p;
572588
str += len;
573589
bprm->p -= len;
574-
#ifdef CONFIG_MMU
575-
if (bprm->p < bprm->argmin)
590+
if (bprm_hit_stack_limit(bprm))
576591
goto out;
577-
#endif
578592

579593
while (len > 0) {
580594
int offset, bytes_to_copy;
@@ -649,7 +663,7 @@ int copy_string_kernel(const char *arg, struct linux_binprm *bprm)
649663
/* We're going to work our way backwards. */
650664
arg += len;
651665
bprm->p -= len;
652-
if (IS_ENABLED(CONFIG_MMU) && bprm->p < bprm->argmin)
666+
if (bprm_hit_stack_limit(bprm))
653667
return -E2BIG;
654668

655669
while (len > 0) {

fs/exec_test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ static void exec_test_bprm_stack_limits(struct kunit *test)
9696

9797
rc = bprm_stack_limits(&bprm);
9898
KUNIT_EXPECT_EQ_MSG(test, rc, result->expected_rc, "on loop %d", i);
99+
#ifdef CONFIG_MMU
99100
KUNIT_EXPECT_EQ_MSG(test, bprm.argmin, result->expected_argmin, "on loop %d", i);
101+
#endif
100102
}
101103
}
102104

include/linux/binfmts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ struct linux_binprm {
1919
#ifdef CONFIG_MMU
2020
struct vm_area_struct *vma;
2121
unsigned long vma_pages;
22+
unsigned long argmin; /* rlimit marker for copy_strings() */
2223
#else
2324
# define MAX_ARG_PAGES 32
2425
struct page *page[MAX_ARG_PAGES];
2526
#endif
2627
struct mm_struct *mm;
2728
unsigned long p; /* current top of mem */
28-
unsigned long argmin; /* rlimit marker for copy_strings() */
2929
unsigned int
3030
/* Should an execfd be passed to userspace? */
3131
have_execfd:1,

0 commit comments

Comments
 (0)