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