• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016 Richard Palethorpe <richiejp@f-m.fm>
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 
45 static const char * const save_restore[] = {
46 	CORE_PATTERN,
47 	NULL,
48 };
49 
setup(void)50 static void setup(void)
51 {
52 	char cwd[1024];
53 	char tmpcpattern[1048];
54 	char *fmemc;
55 	int i;
56 	unsigned int filter;
57 	struct rlimit limit;
58 
59 	limit.rlim_max = RLIM_INFINITY;
60 	limit.rlim_cur = limit.rlim_max;
61 	SAFE_SETRLIMIT(RLIMIT_CORE, &limit);
62 
63 	switch (prctl(PR_GET_DUMPABLE)) {
64 	case 0:
65 		tst_brk(TCONF, "Process is not dumpable.");
66 	case 1:
67 		break;
68 	default:
69 		tst_brk(TBROK | TERRNO, "prctl(PR_GET_DUMPABLE)");
70 	}
71 
72 	SAFE_FILE_SCANF(CORE_FILTER, "%x", &filter);
73 	if (!(0x1 & filter))
74 		tst_brk(TCONF, "Anonymous private memory is not dumpable.");
75 
76 	SAFE_GETCWD(cwd, sizeof(cwd));
77 	snprintf(tmpcpattern, sizeof(tmpcpattern), "%s/dump-%%p", cwd);
78 	tst_res(TINFO, "Temporary core pattern is '%s'", tmpcpattern);
79 	SAFE_FILE_PRINTF(CORE_PATTERN, "%s", tmpcpattern);
80 
81 	fmem = SAFE_MMAP(NULL,
82 			 FMEMSIZE,
83 			 PROT_READ | PROT_WRITE,
84 			 MAP_ANONYMOUS | MAP_PRIVATE,
85 			 -1,
86 			 0);
87 
88 	/*
89 	 * Write a generated character sequence to the mapped memory,
90 	 * which we later look for in the core dump.
91 	 */
92 	fmemc = (char *)fmem;
93 	*fmemc = 'x';
94 	for (i = 0; i < YCOUNT; i++)
95 		fmemc[i + 1] = 'y';
96 	fmemc[++i] = 'z';
97 }
98 
cleanup(void)99 static void cleanup(void)
100 {
101 	if (fmem)
102 		SAFE_MUNMAP(fmem, FMEMSIZE);
103 
104 	if (dfd > 0)
105 		SAFE_CLOSE(dfd);
106 }
107 
find_sequence(int pid)108 static int find_sequence(int pid)
109 {
110 	char expectc = 'x';
111 	ssize_t read, pos = 0;
112 	char rbuf[1024];
113 	int ycount = 0;
114 	char dumpname[256];
115 
116 	snprintf(dumpname, 256, "dump-%d", pid);
117 	tst_res(TINFO, "Dump file should be %s", dumpname);
118 	if (access(dumpname, F_OK))
119 		tst_brk(TBROK | TERRNO, "Dump file was not found.");
120 
121 	dfd = SAFE_OPEN(dumpname, O_RDONLY);
122 
123 	read = SAFE_READ(0, dfd, &rbuf, sizeof(rbuf));
124 	while (read) {
125 		switch (rbuf[pos]) {
126 		case 'x':
127 			ycount = 0;
128 			expectc = 'y';
129 			break;
130 		case 'y':
131 			if (expectc == 'y') {
132 				ycount++;
133 			} else {
134 				expectc = 'x';
135 				break;
136 			}
137 
138 			if (ycount == YCOUNT)
139 				expectc = 'z';
140 			break;
141 		case 'z':
142 			if (expectc == 'z') {
143 				SAFE_CLOSE(dfd);
144 				return 1;
145 			}
146 		default:
147 			expectc = 'x';
148 		}
149 		if (++pos >= read) {
150 			read = SAFE_READ(0, dfd, &rbuf, sizeof(rbuf));
151 			pos = 0;
152 		}
153 	}
154 
155 	SAFE_CLOSE(dfd);
156 	return 0;
157 }
158 
run_child(int advice)159 static pid_t run_child(int advice)
160 {
161 	int status;
162 	pid_t pid;
163 	char *advstr =
164 		advice == MADV_DONTDUMP ? "MADV_DONTDUMP" : "MADV_DODUMP";
165 
166 	pid = SAFE_FORK();
167 	if (pid == 0) {
168 		if (madvise(fmem, FMEMSIZE, advice) == -1) {
169 			tst_res(TFAIL | TERRNO,
170 				"madvise(%p, %lu, %s) = -1",
171 				fmem,
172 				FMEMSIZE,
173 				advstr);
174 			exit(1);
175 		}
176 		abort();
177 	}
178 
179 	SAFE_WAITPID(pid, &status, 0);
180 	if (WIFSIGNALED(status) && WCOREDUMP(status))
181 		return pid;
182 	if (WIFEXITED(status))
183 		return 0;
184 
185 	tst_res(TCONF, "No coredump produced after signal (%d)",
186 		WTERMSIG(status));
187 
188 	return 0;
189 }
190 
run(unsigned int test_nr)191 static void run(unsigned int test_nr)
192 {
193 	pid_t pid;
194 
195 	if (!test_nr) {
196 		pid = run_child(MADV_DONTDUMP);
197 		if (pid && find_sequence(pid))
198 			tst_res(TFAIL,
199 				"Found sequence in dump when MADV_DONTDUMP set");
200 		else if (pid)
201 			tst_res(TPASS, "madvise(..., MADV_DONTDUMP)");
202 	} else {
203 		pid = run_child(MADV_DODUMP);
204 		if (pid && find_sequence(pid))
205 			tst_res(TPASS, "madvise(..., MADV_DODUMP)");
206 		else if (pid)
207 			tst_res(TFAIL,
208 				"No sequence in dump after MADV_DODUMP.");
209 	}
210 }
211 
212 static struct tst_test test = {
213 	.test = run,
214 	.tcnt = 2,
215 	.setup = setup,
216 	.cleanup = cleanup,
217 	.min_kver = "3.4.0",
218 	.needs_tmpdir = 1,
219 	.needs_root = 1,
220 	.forks_child = 1,
221 	.save_restore = save_restore
222 };
223