1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4 * A user worker as the coredumping thread.
5 *
6 * io-wq workers and SQPOLL threads are threads of the process that never
7 * return to userspace. They block every signal but SIGKILL and SIGSTOP,
8 * but a tracer can replace that mask with PTRACE_SETSIGMASK and inject a
9 * coredump signal. get_signal() then runs vfs_coredump() in the worker.
10 * The worker's own exit bookkeeping runs only after the dump, so a
11 * zapped sibling that waits for it in its exit path deadlocks with the
12 * dumper and the whole thread group is stuck in D state.
13 *
14 * Inject SIGSEGV into a chosen thread and require that the thread group
15 * is gone in bounded time, either because the dump completed or because
16 * SIGKILL still works. A failure leaves the stuck process behind.
17 */
18 #include <ctype.h>
19 #include <errno.h>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <sys/mman.h>
23 #include <sys/ptrace.h>
24 #include <sys/resource.h>
25 #include <sys/stat.h>
26 #include <sys/syscall.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29 #include <linux/io_uring.h>
30
31 #include "coredump_test.h"
32
33 #ifndef PTRACE_SETSIGMASK
34 #define PTRACE_SETSIGMASK 0x420b
35 #endif
36
37 /* The dump of the tiny child takes well under a second. */
38 #define EXIT_TIMEOUT_MS 5000
39
FIXTURE_SETUP(coredump)40 FIXTURE_SETUP(coredump)
41 {
42 FILE *file;
43 int ret;
44
45 self->pid_coredump_server = -ESRCH;
46 self->fd_tmpfs_detached = -1;
47 file = fopen("/proc/sys/kernel/core_pattern", "r");
48 ASSERT_NE(NULL, file);
49
50 ret = fread(self->original_core_pattern, 1, sizeof(self->original_core_pattern), file);
51 ASSERT_TRUE(ret || feof(file));
52 ASSERT_LT(ret, sizeof(self->original_core_pattern));
53
54 self->original_core_pattern[ret] = '\0';
55
56 ret = fclose(file);
57 ASSERT_EQ(0, ret);
58 }
59
FIXTURE_TEARDOWN(coredump)60 FIXTURE_TEARDOWN(coredump)
61 {
62 const char *reason;
63 FILE *file;
64 int ret;
65
66 file = fopen("/proc/sys/kernel/core_pattern", "w");
67 if (!file) {
68 reason = "Unable to open core_pattern";
69 goto fail;
70 }
71
72 ret = fprintf(file, "%s", self->original_core_pattern);
73 if (ret < 0) {
74 reason = "Unable to write to core_pattern";
75 goto fail;
76 }
77
78 ret = fclose(file);
79 if (ret) {
80 reason = "Unable to close core_pattern";
81 goto fail;
82 }
83
84 return;
85 fail:
86 /* This should never happen */
87 fprintf(stderr, "Failed to cleanup coredump test: %s\n", reason);
88 }
89
90 /* A raw ring, no liburing. */
91 struct uring {
92 int fd;
93 struct io_uring_params params;
94 void *sq;
95 size_t sq_len;
96 struct io_uring_sqe *sqes;
97 size_t sqes_len;
98 unsigned int *sq_tail, *sq_mask, *sq_array;
99 unsigned int *cq_head, *cq_tail, *cq_mask;
100 struct io_uring_cqe *cqes;
101 };
102
uring_setup(struct uring * r,unsigned int flags)103 static int uring_setup(struct uring *r, unsigned int flags)
104 {
105 size_t cq_len;
106
107 memset(r, 0, sizeof(*r));
108 r->params.flags = flags;
109 if (flags & IORING_SETUP_SQPOLL)
110 r->params.sq_thread_idle = 2000;
111 r->fd = syscall(__NR_io_uring_setup, 8, &r->params);
112 if (r->fd < 0)
113 return -1;
114 if (!(r->params.features & IORING_FEAT_SINGLE_MMAP))
115 return -1;
116
117 r->sq_len = r->params.sq_off.array + r->params.sq_entries * sizeof(unsigned int);
118 cq_len = r->params.cq_off.cqes + r->params.cq_entries * sizeof(struct io_uring_cqe);
119 if (cq_len > r->sq_len)
120 r->sq_len = cq_len;
121 r->sq = mmap(NULL, r->sq_len, PROT_READ | PROT_WRITE,
122 MAP_SHARED | MAP_POPULATE, r->fd, IORING_OFF_SQ_RING);
123 if (r->sq == MAP_FAILED)
124 return -1;
125 r->sqes_len = r->params.sq_entries * sizeof(struct io_uring_sqe);
126 r->sqes = mmap(NULL, r->sqes_len, PROT_READ | PROT_WRITE,
127 MAP_SHARED | MAP_POPULATE, r->fd, IORING_OFF_SQES);
128 if (r->sqes == MAP_FAILED)
129 return -1;
130
131 r->sq_tail = r->sq + r->params.sq_off.tail;
132 r->sq_mask = r->sq + r->params.sq_off.ring_mask;
133 r->sq_array = r->sq + r->params.sq_off.array;
134 r->cq_head = r->sq + r->params.cq_off.head;
135 r->cq_tail = r->sq + r->params.cq_off.tail;
136 r->cq_mask = r->sq + r->params.cq_off.ring_mask;
137 r->cqes = r->sq + r->params.cq_off.cqes;
138 return 0;
139 }
140
141 /* Submit one sqe, wait for its completion and return the result. */
uring_submit_wait(struct uring * r,const struct io_uring_sqe * sqe)142 static int uring_submit_wait(struct uring *r, const struct io_uring_sqe *sqe)
143 {
144 unsigned int tail = *r->sq_tail, idx = tail & *r->sq_mask;
145 unsigned int flags = IORING_ENTER_GETEVENTS;
146 int i;
147
148 r->sqes[idx] = *sqe;
149 r->sq_array[idx] = idx;
150 __atomic_store_n(r->sq_tail, tail + 1, __ATOMIC_RELEASE);
151
152 if (r->params.flags & IORING_SETUP_SQPOLL)
153 flags |= IORING_ENTER_SQ_WAKEUP;
154
155 for (i = 0; i < 100; i++) {
156 if (syscall(__NR_io_uring_enter, r->fd, 1, 1, flags, NULL, 0) < 0 &&
157 errno != EINTR)
158 return -1;
159 if (__atomic_load_n(r->cq_tail, __ATOMIC_ACQUIRE) != *r->cq_head) {
160 unsigned int head = *r->cq_head;
161 int res = r->cqes[head & *r->cq_mask].res;
162
163 __atomic_store_n(r->cq_head, head + 1, __ATOMIC_RELEASE);
164 return res;
165 }
166 flags &= ~IORING_ENTER_SQ_WAKEUP;
167 usleep(10 * 1000);
168 }
169 return -1;
170 }
171
uring_available(unsigned int flags)172 static bool uring_available(unsigned int flags)
173 {
174 struct io_uring_params params = { .flags = flags };
175 int fd;
176
177 fd = syscall(__NR_io_uring_setup, 2, ¶ms);
178 if (fd < 0)
179 return false;
180 close(fd);
181 return true;
182 }
183
184 /*
185 * Keep a ring, an idle io-wq worker and with SQPOLL an SQPOLL thread
186 * alive. The last worker of a ring never exits on its idle timeout.
187 */
worker_child(bool sqpoll,int fd_ipc)188 static void worker_child(bool sqpoll, int fd_ipc)
189 {
190 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
191 struct io_uring_sqe sqe = {};
192 static char buf[64];
193 struct uring ring;
194 int memfd;
195
196 if (setrlimit(RLIMIT_CORE, &rl))
197 _exit(EXIT_FAILURE);
198
199 memfd = memfd_create("coredump_worker", 0);
200 if (memfd < 0 || write(memfd, "hello", 5) != 5)
201 _exit(EXIT_FAILURE);
202
203 if (uring_setup(&ring, sqpoll ? IORING_SETUP_SQPOLL : 0))
204 _exit(EXIT_FAILURE);
205
206 /* IOSQE_ASYNC forces the read through io-wq so a worker appears. */
207 sqe.opcode = IORING_OP_READ;
208 sqe.fd = memfd;
209 sqe.addr = (__u64)(uintptr_t)buf;
210 sqe.len = sizeof(buf);
211 sqe.flags = IOSQE_ASYNC;
212 if (uring_submit_wait(&ring, &sqe) != 5)
213 _exit(EXIT_FAILURE);
214
215 if (write_nointr(fd_ipc, "1", 1) != 1)
216 _exit(EXIT_FAILURE);
217 close(fd_ipc);
218
219 for (;;)
220 pause();
221 }
222
223 /* Find the thread of @pid whose comm starts with @prefix. */
find_thread(pid_t pid,const char * prefix)224 static pid_t find_thread(pid_t pid, const char *prefix)
225 {
226 char path[64], comm[64];
227 pid_t tid = -1;
228 struct dirent *de;
229 ssize_t bytes;
230 DIR *dir;
231 int fd;
232
233 snprintf(path, sizeof(path), "/proc/%d/task", pid);
234 dir = opendir(path);
235 if (!dir)
236 return -1;
237
238 while (tid < 0 && (de = readdir(dir))) {
239 if (!isdigit(de->d_name[0]))
240 continue;
241 snprintf(path, sizeof(path), "/proc/%d/task/%s/comm", pid, de->d_name);
242 fd = open(path, O_RDONLY | O_CLOEXEC);
243 if (fd < 0)
244 continue;
245 bytes = read(fd, comm, sizeof(comm) - 1);
246 close(fd);
247 if (bytes <= 0)
248 continue;
249 comm[bytes] = '\0';
250 if (!strncmp(comm, prefix, strlen(prefix)))
251 tid = atoi(de->d_name);
252 }
253 closedir(dir);
254 return tid;
255 }
256
257 /*
258 * Attach, stop the thread with SIGSTOP, drop the signal mask that
259 * copy_process() gave it and resume it with SIGSEGV. Returns 1 when the
260 * mask was changed, 0 when PTRACE_SETSIGMASK was refused (a user worker
261 * keeps its mask and the SIGSEGV stays pending), -1 on any other failure.
262 */
inject_coredump_signal(pid_t pid,pid_t tid)263 static int inject_coredump_signal(pid_t pid, pid_t tid)
264 {
265 __u64 mask = 0;
266 int status, ret = 1;
267
268 if (ptrace(PTRACE_SEIZE, tid, NULL, NULL))
269 return -1;
270 if (syscall(SYS_tgkill, pid, tid, SIGSTOP))
271 return -1;
272 if (waitpid(tid, &status, __WALL) != tid)
273 return -1;
274 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
275 return -1;
276 if (ptrace(PTRACE_SETSIGMASK, tid, sizeof(mask), &mask)) {
277 if (errno != EPERM)
278 return -1;
279 ret = 0;
280 }
281 if (ptrace(PTRACE_DETACH, tid, NULL, (void *)(long)SIGSEGV))
282 return -1;
283 return ret;
284 }
285
286 /* Reap @pid within @timeout_ms, -1 when it is still there. */
wait_exit(pid_t pid,int * status,int timeout_ms)287 static int wait_exit(pid_t pid, int *status, int timeout_ms)
288 {
289 int i;
290
291 for (i = 0; i < timeout_ms / 10; i++) {
292 pid_t ret = waitpid(pid, status, WNOHANG);
293
294 if (ret == pid)
295 return 0;
296 if (ret < 0)
297 return -1;
298 usleep(10 * 1000);
299 }
300 return -1;
301 }
302
log_threads(struct __test_metadata * const _metadata,pid_t pid)303 static void log_threads(struct __test_metadata *const _metadata, pid_t pid)
304 {
305 char path[64], line[256], comm[64] = {};
306 struct dirent *de;
307 DIR *dir;
308 FILE *f;
309
310 snprintf(path, sizeof(path), "/proc/%d/task", pid);
311 dir = opendir(path);
312 if (!dir)
313 return;
314 while ((de = readdir(dir))) {
315 if (!isdigit(de->d_name[0]))
316 continue;
317 snprintf(path, sizeof(path), "/proc/%d/task/%s/status", pid, de->d_name);
318 f = fopen(path, "r");
319 if (!f)
320 continue;
321 while (fgets(line, sizeof(line), f)) {
322 line[strcspn(line, "\n")] = '\0';
323 if (!strncmp(line, "Name:", 5))
324 snprintf(comm, sizeof(comm), "%s", line + 6);
325 else if (!strncmp(line, "State:", 6))
326 TH_LOG("tid %s (%s) %s", de->d_name, comm, line + 7);
327 }
328 fclose(f);
329 }
330 closedir(dir);
331 }
332
333 enum dumper {
334 DUMPER_MAIN,
335 DUMPER_WORKER,
336 DUMPER_SQPOLL,
337 };
338
run_dumper(struct __test_metadata * const _metadata,bool sqpoll,enum dumper dumper)339 static void run_dumper(struct __test_metadata *const _metadata, bool sqpoll,
340 enum dumper dumper)
341 {
342 bool killed = false;
343 char path[64], c;
344 int ipc[2], status, fd, ret;
345 pid_t pid, tid;
346
347 ASSERT_TRUE(set_core_pattern("/tmp/coredump.file.%p"));
348 ASSERT_EQ(pipe2(ipc, O_CLOEXEC), 0);
349
350 pid = fork();
351 ASSERT_GE(pid, 0);
352 if (pid == 0) {
353 close(ipc[0]);
354 worker_child(sqpoll, ipc[1]);
355 }
356 close(ipc[1]);
357 ASSERT_EQ(read_nointr(ipc[0], &c, 1), 1);
358 close(ipc[0]);
359
360 switch (dumper) {
361 case DUMPER_MAIN:
362 tid = pid;
363 break;
364 case DUMPER_WORKER:
365 tid = find_thread(pid, "iou-wrk-");
366 break;
367 case DUMPER_SQPOLL:
368 tid = find_thread(pid, "iou-sqp-");
369 break;
370 }
371 ASSERT_GT(tid, 0);
372 ret = inject_coredump_signal(pid, tid);
373 ASSERT_GE(ret, 0);
374 if (!ret) {
375 /* The signal sits on the worker, the group must be untouched. */
376 ASSERT_NE(dumper, DUMPER_MAIN);
377 TH_LOG("PTRACE_SETSIGMASK refused for tid %d, the SIGSEGV stays pending", tid);
378 ASSERT_EQ(wait_exit(pid, &status, 1000), -1);
379 kill(pid, SIGKILL);
380 ASSERT_EQ(wait_exit(pid, &status, EXIT_TIMEOUT_MS), 0);
381 ASSERT_TRUE(WIFSIGNALED(status));
382 ASSERT_EQ(WTERMSIG(status), SIGKILL);
383 return;
384 }
385
386 if (wait_exit(pid, &status, EXIT_TIMEOUT_MS)) {
387 /* No dump. Whatever happened, SIGKILL must still work. */
388 log_threads(_metadata, pid);
389 kill(pid, SIGKILL);
390 killed = true;
391 ASSERT_EQ(wait_exit(pid, &status, EXIT_TIMEOUT_MS), 0) {
392 TH_LOG("thread group %d is stuck after SIGSEGV to tid %d",
393 pid, tid);
394 }
395 }
396
397 ASSERT_TRUE(WIFSIGNALED(status));
398 if (killed) {
399 TH_LOG("tid %d did not dump, the group was killed instead", tid);
400 ASSERT_EQ(WTERMSIG(status), SIGKILL);
401 return;
402 }
403 ASSERT_EQ(WTERMSIG(status), SIGSEGV);
404 ASSERT_TRUE(WCOREDUMP(status));
405
406 snprintf(path, sizeof(path), "/tmp/coredump.file.%d", pid);
407 fd = open(path, O_RDONLY | O_CLOEXEC);
408 unlink(path);
409 ASSERT_GE(fd, 0);
410 ASSERT_TRUE(check_coredump_extent(fd));
411 close(fd);
412 }
413
414 /* The mechanics: an injected SIGSEGV into a normal thread dumps core. */
TEST_F(coredump,main_thread_dumper)415 TEST_F(coredump, main_thread_dumper)
416 {
417 if (!uring_available(0))
418 SKIP(return, "io_uring is not available");
419 run_dumper(_metadata, false, DUMPER_MAIN);
420 }
421
TEST_F(coredump,plain_worker_dumper)422 TEST_F(coredump, plain_worker_dumper)
423 {
424 if (!uring_available(0))
425 SKIP(return, "io_uring is not available");
426 run_dumper(_metadata, false, DUMPER_WORKER);
427 }
428
TEST_F(coredump,sqpoll_thread_dumper)429 TEST_F(coredump, sqpoll_thread_dumper)
430 {
431 if (!uring_available(IORING_SETUP_SQPOLL))
432 SKIP(return, "io_uring SQPOLL is not available");
433 run_dumper(_metadata, true, DUMPER_SQPOLL);
434 }
435
436 /*
437 * The SQPOLL thread leaves its loop on the zap and waits for its io-wq
438 * workers to exit before it parks. The dumping worker never does.
439 */
TEST_F(coredump,sqpoll_worker_dumper)440 TEST_F(coredump, sqpoll_worker_dumper)
441 {
442 if (!uring_available(IORING_SETUP_SQPOLL))
443 SKIP(return, "io_uring SQPOLL is not available");
444 run_dumper(_metadata, true, DUMPER_WORKER);
445 }
446
447 TEST_HARNESS_MAIN
448