1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2011-2017 Red Hat, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * This is a reproducer of CVE-2011-0999, which fixed by mainline commit
10*49cdfc7eSAndroid Build Coastguard Worker * a7d6e4ecdb76 ("thp: prevent hugepages during args/env copying into the user stack")
11*49cdfc7eSAndroid Build Coastguard Worker *
12*49cdfc7eSAndroid Build Coastguard Worker * "Transparent hugepages can only be created if rmap is fully
13*49cdfc7eSAndroid Build Coastguard Worker * functional. So we must prevent hugepages to be created while
14*49cdfc7eSAndroid Build Coastguard Worker * is_vma_temporary_stack() is true."
15*49cdfc7eSAndroid Build Coastguard Worker *
16*49cdfc7eSAndroid Build Coastguard Worker * It will cause a panic something like this, if the patch didn't get
17*49cdfc7eSAndroid Build Coastguard Worker * applied:
18*49cdfc7eSAndroid Build Coastguard Worker *
19*49cdfc7eSAndroid Build Coastguard Worker * ```
20*49cdfc7eSAndroid Build Coastguard Worker * kernel BUG at mm/huge_memory.c:1260!
21*49cdfc7eSAndroid Build Coastguard Worker * invalid opcode: 0000 [#1] SMP
22*49cdfc7eSAndroid Build Coastguard Worker * last sysfs file: /sys/devices/system/cpu/cpu23/cache/index2/shared_cpu_map
23*49cdfc7eSAndroid Build Coastguard Worker * ```
24*49cdfc7eSAndroid Build Coastguard Worker *
25*49cdfc7eSAndroid Build Coastguard Worker * Due to commit da029c11e6b1 which reduced the stack size considerably, we
26*49cdfc7eSAndroid Build Coastguard Worker * now perform a binary search to find the largest possible argument we can
27*49cdfc7eSAndroid Build Coastguard Worker * use. Only the first iteration of the test performs the search; subsequent
28*49cdfc7eSAndroid Build Coastguard Worker * iterations use the result of the search which is stored in some shared
29*49cdfc7eSAndroid Build Coastguard Worker * memory.
30*49cdfc7eSAndroid Build Coastguard Worker */
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <sys/resource.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
41*49cdfc7eSAndroid Build Coastguard Worker #include "mem.h"
42*49cdfc7eSAndroid Build Coastguard Worker #include "tst_minmax.h"
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker #define ARGS_SZ (256 * 32)
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker static struct bisection {
47*49cdfc7eSAndroid Build Coastguard Worker long left;
48*49cdfc7eSAndroid Build Coastguard Worker long right;
49*49cdfc7eSAndroid Build Coastguard Worker long mid;
50*49cdfc7eSAndroid Build Coastguard Worker } *bst;
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker static char *args[ARGS_SZ];
53*49cdfc7eSAndroid Build Coastguard Worker static char *arg;
54*49cdfc7eSAndroid Build Coastguard Worker
thp_test(void)55*49cdfc7eSAndroid Build Coastguard Worker static void thp_test(void)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker long prev_left;
58*49cdfc7eSAndroid Build Coastguard Worker int pid;
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker while (bst->right - bst->left > 1) {
61*49cdfc7eSAndroid Build Coastguard Worker pid_t pid = SAFE_FORK();
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker if (!pid) {
64*49cdfc7eSAndroid Build Coastguard Worker /* We set mid to left assuming exec will succeed. If
65*49cdfc7eSAndroid Build Coastguard Worker * exec fails with E2BIG (and thus returns) then we
66*49cdfc7eSAndroid Build Coastguard Worker * restore left and set right to mid instead.
67*49cdfc7eSAndroid Build Coastguard Worker */
68*49cdfc7eSAndroid Build Coastguard Worker prev_left = bst->left;
69*49cdfc7eSAndroid Build Coastguard Worker bst->mid = (bst->left + bst->right) / 2;
70*49cdfc7eSAndroid Build Coastguard Worker bst->left = bst->mid;
71*49cdfc7eSAndroid Build Coastguard Worker args[bst->mid] = NULL;
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker TEST(execvp("true", args));
74*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR != E2BIG)
75*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TTERRNO, "execvp(\"true\", ...)");
76*49cdfc7eSAndroid Build Coastguard Worker bst->left = prev_left;
77*49cdfc7eSAndroid Build Coastguard Worker bst->right = bst->mid;
78*49cdfc7eSAndroid Build Coastguard Worker exit(0);
79*49cdfc7eSAndroid Build Coastguard Worker }
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children();
82*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "left: %ld, right: %ld, mid: %ld",
83*49cdfc7eSAndroid Build Coastguard Worker bst->left, bst->right, bst->mid);
84*49cdfc7eSAndroid Build Coastguard Worker }
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker /* We end with mid == right or mid == left where right - left =
87*49cdfc7eSAndroid Build Coastguard Worker * 1. Regardless we must use left because right is only set to values
88*49cdfc7eSAndroid Build Coastguard Worker * which are too large.
89*49cdfc7eSAndroid Build Coastguard Worker */
90*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
91*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
92*49cdfc7eSAndroid Build Coastguard Worker args[bst->left] = NULL;
93*49cdfc7eSAndroid Build Coastguard Worker TEST(execvp("true", args));
94*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR != E2BIG)
95*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TTERRNO, "execvp(\"true\", ...)");
96*49cdfc7eSAndroid Build Coastguard Worker exit(0);
97*49cdfc7eSAndroid Build Coastguard Worker }
98*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children();
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "system didn't crash.");
101*49cdfc7eSAndroid Build Coastguard Worker }
102*49cdfc7eSAndroid Build Coastguard Worker
setup(void)103*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
104*49cdfc7eSAndroid Build Coastguard Worker {
105*49cdfc7eSAndroid Build Coastguard Worker struct rlimit rl = {
106*49cdfc7eSAndroid Build Coastguard Worker .rlim_cur = RLIM_INFINITY,
107*49cdfc7eSAndroid Build Coastguard Worker .rlim_max = RLIM_INFINITY,
108*49cdfc7eSAndroid Build Coastguard Worker };
109*49cdfc7eSAndroid Build Coastguard Worker int i;
110*49cdfc7eSAndroid Build Coastguard Worker long arg_len, arg_count;
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker bst = SAFE_MMAP(NULL, sizeof(*bst),
113*49cdfc7eSAndroid Build Coastguard Worker PROT_READ | PROT_WRITE,
114*49cdfc7eSAndroid Build Coastguard Worker MAP_SHARED | MAP_ANONYMOUS, -1, 0);
115*49cdfc7eSAndroid Build Coastguard Worker bst->left = 0;
116*49cdfc7eSAndroid Build Coastguard Worker bst->right = ARGS_SZ;
117*49cdfc7eSAndroid Build Coastguard Worker
118*49cdfc7eSAndroid Build Coastguard Worker arg_len = sysconf(_SC_PAGESIZE);
119*49cdfc7eSAndroid Build Coastguard Worker arg = SAFE_MALLOC(arg_len);
120*49cdfc7eSAndroid Build Coastguard Worker memset(arg, 'c', arg_len - 1);
121*49cdfc7eSAndroid Build Coastguard Worker arg[arg_len - 1] = '\0';
122*49cdfc7eSAndroid Build Coastguard Worker
123*49cdfc7eSAndroid Build Coastguard Worker args[0] = "true";
124*49cdfc7eSAndroid Build Coastguard Worker arg_count = ARGS_SZ;
125*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Using %ld args of size %ld", arg_count, arg_len);
126*49cdfc7eSAndroid Build Coastguard Worker for (i = 1; i < arg_count; i++)
127*49cdfc7eSAndroid Build Coastguard Worker args[i] = arg;
128*49cdfc7eSAndroid Build Coastguard Worker
129*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETRLIMIT(RLIMIT_STACK, &rl);
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)132*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
133*49cdfc7eSAndroid Build Coastguard Worker {
134*49cdfc7eSAndroid Build Coastguard Worker free(arg);
135*49cdfc7eSAndroid Build Coastguard Worker }
136*49cdfc7eSAndroid Build Coastguard Worker
137*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
138*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
139*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
140*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
141*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
142*49cdfc7eSAndroid Build Coastguard Worker .test_all = thp_test,
143*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) {
144*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "a7d6e4ecdb76"},
145*49cdfc7eSAndroid Build Coastguard Worker {"CVE", "2011-0999"},
146*49cdfc7eSAndroid Build Coastguard Worker {}
147*49cdfc7eSAndroid Build Coastguard Worker }
148*49cdfc7eSAndroid Build Coastguard Worker };
149