summaryrefslogtreecommitdiffstats
path: root/fs/exec_test.c
blob: 7c77d039680bbe42414423f27146d394b33d5aa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// SPDX-License-Identifier: GPL-2.0-only
#include <kunit/test.h>

struct bprm_stack_limits_result {
	struct linux_binprm bprm;
	int expected_rc;
	unsigned long expected_argmin;
};

static const struct bprm_stack_limits_result bprm_stack_limits_results[] = {
	/* Negative argc/envc counts produce -E2BIG */
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
	    .argc = INT_MIN, .envc = INT_MIN }, .expected_rc = -E2BIG },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
	    .argc = 5, .envc = -1 }, .expected_rc = -E2BIG },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
	    .argc = -1, .envc = 10 }, .expected_rc = -E2BIG },
	/* The max value of argc or envc is MAX_ARG_STRINGS. */
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
	    .argc = INT_MAX, .envc = INT_MAX }, .expected_rc = -E2BIG },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
	    .argc = MAX_ARG_STRINGS, .envc = MAX_ARG_STRINGS }, .expected_rc = -E2BIG },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
	    .argc = 0, .envc = MAX_ARG_STRINGS }, .expected_rc = -E2BIG },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
	    .argc = MAX_ARG_STRINGS, .envc = 0 }, .expected_rc = -E2BIG },
	/*
	 * On 32-bit system these argc and envc counts, while likely impossible
	 * to represent within the associated TASK_SIZE, could overflow the
	 * limit calculation, and bypass the ptr_size <= limit check.
	 */
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX,
	    .argc = 0x20000001, .envc = 0x20000001 }, .expected_rc = -E2BIG },
#ifdef CONFIG_MMU
	/* Make sure a pathological bprm->p doesn't cause an overflow. */
	{ { .p = sizeof(void *), .rlim_stack.rlim_cur = ULONG_MAX,
	    .argc = 10, .envc = 10 }, .expected_rc = -E2BIG },
#endif
	/*
	 * 0 rlim_stack will get raised to ARG_MAX. With 1 string pointer,
	 * we should see p - ARG_MAX + sizeof(void *).
	 */
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0,
	    .argc = 1, .envc = 0 }, .expected_argmin = ULONG_MAX - ARG_MAX + sizeof(void *)},
	/* Validate that argc is always raised to a minimum of 1. */
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0,
	    .argc = 0, .envc = 0 }, .expected_argmin = ULONG_MAX - ARG_MAX + sizeof(void *)},
	/*
	 * 0 rlim_stack will get raised to ARG_MAX. With pointers filling ARG_MAX,
	 * we should see -E2BIG. (Note argc is always raised to at least 1.)
	 */
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0,
	    .argc = ARG_MAX / sizeof(void *), .envc = 0 }, .expected_rc = -E2BIG },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0,
	    .argc = 0, .envc = ARG_MAX / sizeof(void *) - 1 }, .expected_rc = -E2BIG },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0,
	    .argc = ARG_MAX / sizeof(void *) + 1, .envc = 0 }, .expected_rc = -E2BIG },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0,
	    .argc = 0, .envc = ARG_MAX / sizeof(void *) }, .expected_rc = -E2BIG },
	/* And with one less, we see space for exactly 1 pointer. */
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0,
	    .argc = (ARG_MAX / sizeof(void *)) - 1, .envc = 0 },
	  .expected_argmin = ULONG_MAX - sizeof(void *) },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0,
	    .argc = 0, .envc = (ARG_MAX / sizeof(void *)) - 2, },
	  .expected_argmin = ULONG_MAX - sizeof(void *) },
	/* If we raise rlim_stack / 4 to exactly ARG_MAX, nothing changes. */
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ARG_MAX * 4,
	    .argc = ARG_MAX / sizeof(void *), .envc = 0 }, .expected_rc = -E2BIG },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ARG_MAX * 4,
	    .argc = 0, .envc = ARG_MAX / sizeof(void *) - 1 }, .expected_rc = -E2BIG },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ARG_MAX * 4,
	    .argc = ARG_MAX / sizeof(void *) + 1, .envc = 0 }, .expected_rc = -E2BIG },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ARG_MAX * 4,
	    .argc = 0, .envc = ARG_MAX / sizeof(void *) }, .expected_rc = -E2BIG },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ARG_MAX * 4,
	    .argc = (ARG_MAX / sizeof(void *)) - 1, .envc = 0 },
	  .expected_argmin = ULONG_MAX - sizeof(void *) },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = ARG_MAX * 4,
	    .argc = 0, .envc = (ARG_MAX / sizeof(void *)) - 2, },
	  .expected_argmin = ULONG_MAX - sizeof(void *) },
	/* But raising it another pointer * 4 will provide space for 1 more pointer. */
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = (ARG_MAX + sizeof(void *)) * 4,
	    .argc = ARG_MAX / sizeof(void *), .envc = 0 },
	  .expected_argmin = ULONG_MAX - sizeof(void *) },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = (ARG_MAX + sizeof(void *)) * 4,
	    .argc = 0, .envc = ARG_MAX / sizeof(void *) - 1 },
	  .expected_argmin = ULONG_MAX - sizeof(void *) },
	/* Raising rlim_stack / 4 to _STK_LIM / 4 * 3 will see more space. */
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 4 * (_STK_LIM / 4 * 3),
	    .argc = 0, .envc = 0 },
	  .expected_argmin = ULONG_MAX - (_STK_LIM / 4 * 3) + sizeof(void *) },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 4 * (_STK_LIM / 4 * 3),
	    .argc = 0, .envc = 0 },
	  .expected_argmin = ULONG_MAX - (_STK_LIM / 4 * 3) + sizeof(void *) },
	/* But raising it any further will see no increase. */
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 4 * (_STK_LIM / 4 * 3 + sizeof(void *)),
	    .argc = 0, .envc = 0 },
	  .expected_argmin = ULONG_MAX - (_STK_LIM / 4 * 3) + sizeof(void *) },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 4 * (_STK_LIM / 4 *  + sizeof(void *)),
	    .argc = 0, .envc = 0 },
	  .expected_argmin = ULONG_MAX - (_STK_LIM / 4 * 3) + sizeof(void *) },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 4 * _STK_LIM,
	    .argc = 0, .envc = 0 },
	  .expected_argmin = ULONG_MAX - (_STK_LIM / 4 * 3) + sizeof(void *) },
	{ { .p = ULONG_MAX, .rlim_stack.rlim_cur = 4 * _STK_LIM,
	    .argc = 0, .envc = 0 },
	  .expected_argmin = ULONG_MAX - (_STK_LIM / 4 * 3) + sizeof(void *) },
};

static void exec_test_bprm_stack_limits(struct kunit *test)
{
	/* Double-check the constants. */
	KUNIT_EXPECT_EQ(test, _STK_LIM, SZ_8M);
	KUNIT_EXPECT_EQ(test, ARG_MAX, 32 * SZ_4K);
	KUNIT_EXPECT_EQ(test, MAX_ARG_STRINGS, 0x7FFFFFFF);

	for (int i = 0; i < ARRAY_SIZE(bprm_stack_limits_results); i++) {
		const struct bprm_stack_limits_result *result = &bprm_stack_limits_results[i];
		struct linux_binprm bprm = result->bprm;
		int rc;

		rc = bprm_stack_limits(&bprm);
		KUNIT_EXPECT_EQ_MSG(test, rc, result->expected_rc, "on loop %d", i);
#ifdef CONFIG_MMU
		KUNIT_EXPECT_EQ_MSG(test, bprm.argmin, result->expected_argmin, "on loop %d", i);
#endif
	}
}

static struct kunit_case exec_test_cases[] = {
	KUNIT_CASE(exec_test_bprm_stack_limits),
	{},
};

static struct kunit_suite exec_test_suite = {
	.name = "exec",
	.test_cases = exec_test_cases,
};

kunit_test_suite(exec_test_suite);