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

Commit 21f9310

Browse files
committed
exec: Avoid pathological argc, envc, and bprm->p values
Make sure nothing goes wrong with the string counters or the bprm's belief about the stack pointer. Add checks and matching self-tests. Take special care for !CONFIG_MMU, since argmin is not exposed there. For 32-bit validation, 32-bit UML was used: $ tools/testing/kunit/kunit.py run \ --make_options CROSS_COMPILE=i686-linux-gnu- \ --make_options SUBARCH=i386 \ exec For !MMU validation, m68k was used: $ tools/testing/kunit/kunit.py run \ --arch m68k --make_option CROSS_COMPILE=m68k-linux-gnu- \ exec Link: https://lore.kernel.org/r/20240520021615.741800-2-keescook@chromium.org Link: https://lore.kernel.org/r/20240621205046.4001362-2-kees@kernel.org Signed-off-by: Kees Cook <kees@kernel.org>
1 parent 084ebf7 commit 21f9310

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

fs/exec.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,9 @@ static inline int bprm_set_stack_limit(struct linux_binprm *bprm,
490490
unsigned long limit)
491491
{
492492
#ifdef CONFIG_MMU
493+
/* Avoid a pathological bprm->p. */
494+
if (bprm->p < limit)
495+
return -E2BIG;
493496
bprm->argmin = bprm->p - limit;
494497
#endif
495498
return 0;
@@ -531,6 +534,9 @@ static int bprm_stack_limits(struct linux_binprm *bprm)
531534
* of argument strings even with small stacks
532535
*/
533536
limit = max_t(unsigned long, limit, ARG_MAX);
537+
/* Reject totally pathological counts. */
538+
if (bprm->argc < 0 || bprm->envc < 0)
539+
return -E2BIG;
534540
/*
535541
* We must account for the size of all the argv and envp pointers to
536542
* the argv and envp strings, since they will also take up space in
@@ -544,7 +550,9 @@ static int bprm_stack_limits(struct linux_binprm *bprm)
544550
* argc can never be 0, to keep them from walking envp by accident.
545551
* See do_execveat_common().
546552
*/
547-
ptr_size = (max(bprm->argc, 1) + bprm->envc) * sizeof(void *);
553+
if (check_add_overflow(max(bprm->argc, 1), bprm->envc, &ptr_size) ||
554+
check_mul_overflow(ptr_size, sizeof(void *), &ptr_size))
555+
return -E2BIG;
548556
if (limit <= ptr_size)
549557
return -E2BIG;
550558
limit -= ptr_size;

fs/exec_test.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,34 @@ struct bprm_stack_limits_result {
88
};
99

1010
static const struct bprm_stack_limits_result bprm_stack_limits_results[] = {
11-
/* Giant values produce -E2BIG */
11+
/* Negative argc/envc counts produce -E2BIG */
12+
{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
13+
.argc = INT_MIN, .envc = INT_MIN }, .expected_rc = -E2BIG },
14+
{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
15+
.argc = 5, .envc = -1 }, .expected_rc = -E2BIG },
16+
{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
17+
.argc = -1, .envc = 10 }, .expected_rc = -E2BIG },
18+
/* The max value of argc or envc is MAX_ARG_STRINGS. */
1219
{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
1320
.argc = INT_MAX, .envc = INT_MAX }, .expected_rc = -E2BIG },
21+
{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
22+
.argc = MAX_ARG_STRINGS, .envc = MAX_ARG_STRINGS }, .expected_rc = -E2BIG },
23+
{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
24+
.argc = 0, .envc = MAX_ARG_STRINGS }, .expected_rc = -E2BIG },
25+
{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
26+
.argc = MAX_ARG_STRINGS, .envc = 0 }, .expected_rc = -E2BIG },
27+
/*
28+
* On 32-bit system these argc and envc counts, while likely impossible
29+
* to represent within the associated TASK_SIZE, could overflow the
30+
* limit calculation, and bypass the ptr_size <= limit check.
31+
*/
32+
{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
33+
.argc = 0x20000001, .envc = 0x20000001 }, .expected_rc = -E2BIG },
34+
#ifdef CONFIG_MMU
35+
/* Make sure a pathological bprm->p doesn't cause an overflow. */
36+
{ { .p = sizeof(void *), .rlim_stack.rlim_cur = ULONG_MAX,
37+
.argc = 10, .envc = 10 }, .expected_rc = -E2BIG },
38+
#endif
1439
/*
1540
* 0 rlim_stack will get raised to ARG_MAX. With 1 string pointer,
1641
* we should see p - ARG_MAX + sizeof(void *).
@@ -88,6 +113,7 @@ static void exec_test_bprm_stack_limits(struct kunit *test)
88113
/* Double-check the constants. */
89114
KUNIT_EXPECT_EQ(test, _STK_LIM, SZ_8M);
90115
KUNIT_EXPECT_EQ(test, ARG_MAX, 32 * SZ_4K);
116+
KUNIT_EXPECT_EQ(test, MAX_ARG_STRINGS, 0x7FFFFFFF);
91117

92118
for (int i = 0; i < ARRAY_SIZE(bprm_stack_limits_results); i++) {
93119
const struct bprm_stack_limits_result *result = &bprm_stack_limits_results[i];

0 commit comments

Comments
 (0)