1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 Richard Palethorpe <[email protected]>
4 * Copyright (c) 2017 SUSE LLC
5 */
6 /*
7 * Check that memory marked with MADV_DONTDUMP is not included in a core dump
8 * and check that the same memory then marked with MADV_DODUMP is included in
9 * a core dump.
10 *
11 * In order to reliably find the core dump this test temporarily changes the
12 * system wide core_pattern setting. Meaning all core dumps will be sent to the
13 * test's temporary dir until the setting is restored during cleanup.
14 *
15 * Test flow: map memory,
16 * write generated character sequence to memory,
17 * start child process,
18 * mark memory with MADV_DONTDUMP in child,
19 * abort child,
20 * scan child's core dump for character sequence,
21 * if the sequence is not found it is a pass otherwise a fail,
22 */
23
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <sys/prctl.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32
33 #include "tst_test.h"
34 #include "lapi/mmap.h"
35
36 #define CORE_PATTERN "/proc/sys/kernel/core_pattern"
37 #define CORE_FILTER "/proc/self/coredump_filter"
38 #define YCOUNT 0x500L
39 #define FMEMSIZE (YCOUNT + 0x2L)
40 #define CORENAME_MAX_SIZE 512
41
42 static int dfd;
43 static void *fmem;
44
setup(void)45 static void setup(void)
46 {
47 char cwd[1024];
48 char tmpcpattern[1048];
49 char *fmemc;
50 int i;
51 unsigned int filter;
52 struct rlimit limit;
53
54 limit.rlim_max = RLIM_INFINITY;
55 limit.rlim_cur = limit.rlim_max;
56 SAFE_SETRLIMIT(RLIMIT_CORE, &limit);
57
58 switch (prctl(PR_GET_DUMPABLE)) {
59 case 0:
60 tst_brk(TCONF, "Process is not dumpable.");
61 case 1:
62 break;
63 default:
64 tst_brk(TBROK | TERRNO, "prctl(PR_GET_DUMPABLE)");
65 }
66
67 SAFE_FILE_SCANF(CORE_FILTER, "%x", &filter);
68 if (!(0x1 & filter))
69 tst_brk(TCONF, "Anonymous private memory is not dumpable.");
70
71 SAFE_GETCWD(cwd, sizeof(cwd));
72 snprintf(tmpcpattern, sizeof(tmpcpattern), "%s/dump-%%p", cwd);
73 tst_res(TINFO, "Temporary core pattern is '%s'", tmpcpattern);
74 SAFE_FILE_PRINTF(CORE_PATTERN, "%s", tmpcpattern);
75
76 fmem = SAFE_MMAP(NULL,
77 FMEMSIZE,
78 PROT_READ | PROT_WRITE,
79 MAP_ANONYMOUS | MAP_PRIVATE,
80 -1,
81 0);
82
83 /*
84 * Write a generated character sequence to the mapped memory,
85 * which we later look for in the core dump.
86 */
87 fmemc = (char *)fmem;
88 *fmemc = 'x';
89 for (i = 0; i < YCOUNT; i++)
90 fmemc[i + 1] = 'y';
91 fmemc[++i] = 'z';
92 }
93
cleanup(void)94 static void cleanup(void)
95 {
96 if (fmem)
97 SAFE_MUNMAP(fmem, FMEMSIZE);
98
99 if (dfd > 0)
100 SAFE_CLOSE(dfd);
101 }
102
find_sequence(int pid)103 static int find_sequence(int pid)
104 {
105 char expectc = 'x';
106 ssize_t read, pos = 0;
107 char rbuf[1024];
108 int ycount = 0;
109 char dumpname[256];
110
111 snprintf(dumpname, 256, "dump-%d", pid);
112 tst_res(TINFO, "Dump file should be %s", dumpname);
113 SAFE_ACCESS(dumpname, F_OK);
114
115 dfd = SAFE_OPEN(dumpname, O_RDONLY);
116
117 read = SAFE_READ(0, dfd, &rbuf, sizeof(rbuf));
118 while (read) {
119 switch (rbuf[pos]) {
120 case 'x':
121 ycount = 0;
122 expectc = 'y';
123 break;
124 case 'y':
125 if (expectc == 'y') {
126 ycount++;
127 } else {
128 expectc = 'x';
129 break;
130 }
131
132 if (ycount == YCOUNT)
133 expectc = 'z';
134 break;
135 case 'z':
136 if (expectc == 'z') {
137 SAFE_CLOSE(dfd);
138 return 1;
139 }
140 default:
141 expectc = 'x';
142 }
143 if (++pos >= read) {
144 read = SAFE_READ(0, dfd, &rbuf, sizeof(rbuf));
145 pos = 0;
146 }
147 }
148
149 SAFE_CLOSE(dfd);
150 return 0;
151 }
152
run_child(int advice)153 static pid_t run_child(int advice)
154 {
155 int status;
156 pid_t pid;
157 char *advstr =
158 advice == MADV_DONTDUMP ? "MADV_DONTDUMP" : "MADV_DODUMP";
159
160 pid = SAFE_FORK();
161 if (pid == 0) {
162 if (madvise(fmem, FMEMSIZE, advice) == -1) {
163 tst_res(TFAIL | TERRNO,
164 "madvise(%p, %lu, %s) = -1",
165 fmem,
166 FMEMSIZE,
167 advstr);
168 exit(1);
169 }
170 abort();
171 }
172
173 SAFE_WAITPID(pid, &status, 0);
174 if (WIFSIGNALED(status) && WCOREDUMP(status))
175 return pid;
176 if (WIFEXITED(status))
177 return 0;
178
179 tst_res(TCONF, "No coredump produced after signal (%d)",
180 WTERMSIG(status));
181
182 return 0;
183 }
184
run(unsigned int test_nr)185 static void run(unsigned int test_nr)
186 {
187 pid_t pid;
188
189 if (!test_nr) {
190 pid = run_child(MADV_DONTDUMP);
191 if (pid && find_sequence(pid))
192 tst_res(TFAIL,
193 "Found sequence in dump when MADV_DONTDUMP set");
194 else if (pid)
195 tst_res(TPASS, "madvise(..., MADV_DONTDUMP)");
196 } else {
197 pid = run_child(MADV_DODUMP);
198 if (pid && find_sequence(pid))
199 tst_res(TPASS, "madvise(..., MADV_DODUMP)");
200 else if (pid)
201 tst_res(TFAIL,
202 "No sequence in dump after MADV_DODUMP.");
203 }
204 }
205
206 static struct tst_test test = {
207 .test = run,
208 .tcnt = 2,
209 .setup = setup,
210 .cleanup = cleanup,
211 .needs_tmpdir = 1,
212 .needs_root = 1,
213 .forks_child = 1,
214 .save_restore = (const struct tst_path_val[]) {
215 {CORE_PATTERN, NULL, TST_SR_TCONF},
216 {}
217 },
218 };
219