• 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 
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 	if (access(dumpname, F_OK))
114 		tst_brk(TBROK | TERRNO, "Dump file was not found.");
115 
116 	dfd = SAFE_OPEN(dumpname, O_RDONLY);
117 
118 	read = SAFE_READ(0, dfd, &rbuf, sizeof(rbuf));
119 	while (read) {
120 		switch (rbuf[pos]) {
121 		case 'x':
122 			ycount = 0;
123 			expectc = 'y';
124 			break;
125 		case 'y':
126 			if (expectc == 'y') {
127 				ycount++;
128 			} else {
129 				expectc = 'x';
130 				break;
131 			}
132 
133 			if (ycount == YCOUNT)
134 				expectc = 'z';
135 			break;
136 		case 'z':
137 			if (expectc == 'z') {
138 				SAFE_CLOSE(dfd);
139 				return 1;
140 			}
141 		default:
142 			expectc = 'x';
143 		}
144 		if (++pos >= read) {
145 			read = SAFE_READ(0, dfd, &rbuf, sizeof(rbuf));
146 			pos = 0;
147 		}
148 	}
149 
150 	SAFE_CLOSE(dfd);
151 	return 0;
152 }
153 
run_child(int advice)154 static pid_t run_child(int advice)
155 {
156 	int status;
157 	pid_t pid;
158 	char *advstr =
159 		advice == MADV_DONTDUMP ? "MADV_DONTDUMP" : "MADV_DODUMP";
160 
161 	pid = SAFE_FORK();
162 	if (pid == 0) {
163 		if (madvise(fmem, FMEMSIZE, advice) == -1) {
164 			tst_res(TFAIL | TERRNO,
165 				"madvise(%p, %lu, %s) = -1",
166 				fmem,
167 				FMEMSIZE,
168 				advstr);
169 			exit(1);
170 		}
171 		abort();
172 	}
173 
174 	SAFE_WAITPID(pid, &status, 0);
175 	if (WIFSIGNALED(status) && WCOREDUMP(status))
176 		return pid;
177 	if (WIFEXITED(status))
178 		return 0;
179 
180 	tst_res(TCONF, "No coredump produced after signal (%d)",
181 		WTERMSIG(status));
182 
183 	return 0;
184 }
185 
run(unsigned int test_nr)186 static void run(unsigned int test_nr)
187 {
188 	pid_t pid;
189 
190 	if (!test_nr) {
191 		pid = run_child(MADV_DONTDUMP);
192 		if (pid && find_sequence(pid))
193 			tst_res(TFAIL,
194 				"Found sequence in dump when MADV_DONTDUMP set");
195 		else if (pid)
196 			tst_res(TPASS, "madvise(..., MADV_DONTDUMP)");
197 	} else {
198 		pid = run_child(MADV_DODUMP);
199 		if (pid && find_sequence(pid))
200 			tst_res(TPASS, "madvise(..., MADV_DODUMP)");
201 		else if (pid)
202 			tst_res(TFAIL,
203 				"No sequence in dump after MADV_DODUMP.");
204 	}
205 }
206 
207 static struct tst_test test = {
208 	.test = run,
209 	.tcnt = 2,
210 	.setup = setup,
211 	.cleanup = cleanup,
212 	.min_kver = "3.4.0",
213 	.needs_tmpdir = 1,
214 	.needs_root = 1,
215 	.forks_child = 1,
216 	.save_restore = (const char * const[]) {
217 		CORE_PATTERN,
218 		NULL,
219 	},
220 };
221