1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 
4 #include <sys/ptrace.h>
5 #include <sys/types.h>
6 #include <sys/wait.h>
7 #include <sys/syscall.h>
8 #include <sys/user.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <stddef.h>
12 #include <stdio.h>
13 #include <err.h>
14 #include <string.h>
15 #include <asm/ptrace-abi.h>
16 #include <sys/auxv.h>
17 
18 /* Bitness-agnostic defines for user_regs_struct fields. */
19 #ifdef __x86_64__
20 # define user_syscall_nr	orig_rax
21 # define user_arg0		rdi
22 # define user_arg1		rsi
23 # define user_arg2		rdx
24 # define user_arg3		r10
25 # define user_arg4		r8
26 # define user_arg5		r9
27 # define user_ip		rip
28 # define user_ax		rax
29 #else
30 # define user_syscall_nr	orig_eax
31 # define user_arg0		ebx
32 # define user_arg1		ecx
33 # define user_arg2		edx
34 # define user_arg3		esi
35 # define user_arg4		edi
36 # define user_arg5		ebp
37 # define user_ip		eip
38 # define user_ax		eax
39 #endif
40 
41 static int nerrs = 0;
42 
43 struct syscall_args32 {
44 	uint32_t nr, arg0, arg1, arg2, arg3, arg4, arg5;
45 };
46 
47 #ifdef __i386__
48 extern void sys32_helper(struct syscall_args32 *, void *);
49 extern void int80_and_ret(void);
50 #endif
51 
52 /*
53  * Helper to invoke int80 with controlled regs and capture the final regs.
54  */
55 static void do_full_int80(struct syscall_args32 *args)
56 {
57 #ifdef __x86_64__
58 	register unsigned long bp asm("bp") = args->arg5;
59 	asm volatile ("int $0x80"
60 		      : "+a" (args->nr),
61 			"+b" (args->arg0), "+c" (args->arg1), "+d" (args->arg2),
62 			"+S" (args->arg3), "+D" (args->arg4), "+r" (bp)
63 			: : "r8", "r9", "r10", "r11");
64 	args->arg5 = bp;
65 #else
66 	sys32_helper(args, int80_and_ret);
67 #endif
68 }
69 
70 #ifdef __i386__
71 static void (*vsyscall32)(void);
72 
73 /*
74  * Nasty helper to invoke AT_SYSINFO (i.e. __kernel_vsyscall) with
75  * controlled regs and capture the final regs.  This is so nasty that it
76  * crashes my copy of gdb :)
77  */
78 static void do_full_vsyscall32(struct syscall_args32 *args)
79 {
80 	sys32_helper(args, vsyscall32);
81 }
82 #endif
83 
84 static siginfo_t wait_trap(pid_t chld)
85 {
86 	siginfo_t si;
87 	if (waitid(P_PID, chld, &si, WEXITED|WSTOPPED) != 0)
88 		err(1, "waitid");
89 	if (si.si_pid != chld)
90 		errx(1, "got unexpected pid in event\n");
91 	if (si.si_code != CLD_TRAPPED)
92 		errx(1, "got unexpected event type %d\n", si.si_code);
93 	return si;
94 }
95 
96 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
97 		       int flags)
98 {
99 	struct sigaction sa;
100 	memset(&sa, 0, sizeof(sa));
101 	sa.sa_sigaction = handler;
102 	sa.sa_flags = SA_SIGINFO | flags;
103 	sigemptyset(&sa.sa_mask);
104 	if (sigaction(sig, &sa, 0))
105 		err(1, "sigaction");
106 }
107 
108 static void setsigign(int sig, int flags)
109 {
110 	struct sigaction sa;
111 	memset(&sa, 0, sizeof(sa));
112 	sa.sa_sigaction = (void *)SIG_IGN;
113 	sa.sa_flags = flags;
114 	sigemptyset(&sa.sa_mask);
115 	if (sigaction(sig, &sa, 0))
116 		err(1, "sigaction");
117 }
118 
119 static void clearhandler(int sig)
120 {
121 	struct sigaction sa;
122 	memset(&sa, 0, sizeof(sa));
123 	sa.sa_handler = SIG_DFL;
124 	sigemptyset(&sa.sa_mask);
125 	if (sigaction(sig, &sa, 0))
126 		err(1, "sigaction");
127 }
128 
129 #ifdef __x86_64__
130 # define REG_BP REG_RBP
131 #else
132 # define REG_BP REG_EBP
133 #endif
134 
135 static void empty_handler(int sig, siginfo_t *si, void *ctx_void)
136 {
137 }
138 
139 static void test_sys32_regs(void (*do_syscall)(struct syscall_args32 *))
140 {
141 	struct syscall_args32 args = {
142 		.nr = 224,	/* gettid */
143 		.arg0 = 10, .arg1 = 11, .arg2 = 12,
144 		.arg3 = 13, .arg4 = 14, .arg5 = 15,
145 	};
146 
147 	do_syscall(&args);
148 
149 	if (args.nr != getpid() ||
150 	    args.arg0 != 10 || args.arg1 != 11 || args.arg2 != 12 ||
151 	    args.arg3 != 13 || args.arg4 != 14 || args.arg5 != 15) {
152 		printf("[FAIL]\tgetpid() failed to preserve regs\n");
153 		nerrs++;
154 	} else {
155 		printf("[OK]\tgetpid() preserves regs\n");
156 	}
157 
158 	sethandler(SIGUSR1, empty_handler, 0);
159 
160 	args.nr = 37;	/* kill */
161 	args.arg0 = getpid();
162 	args.arg1 = SIGUSR1;
163 	do_syscall(&args);
164 	if (args.nr != 0 ||
165 	    args.arg0 != getpid() || args.arg1 != SIGUSR1 || args.arg2 != 12 ||
166 	    args.arg3 != 13 || args.arg4 != 14 || args.arg5 != 15) {
167 		printf("[FAIL]\tkill(getpid(), SIGUSR1) failed to preserve regs\n");
168 		nerrs++;
169 	} else {
170 		printf("[OK]\tkill(getpid(), SIGUSR1) preserves regs\n");
171 	}
172 	clearhandler(SIGUSR1);
173 }
174 
175 static void test_ptrace_syscall_restart(void)
176 {
177 	printf("[RUN]\tptrace-induced syscall restart\n");
178 	pid_t chld = fork();
179 	if (chld < 0)
180 		err(1, "fork");
181 
182 	if (chld == 0) {
183 		if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
184 			err(1, "PTRACE_TRACEME");
185 
186 		printf("\tChild will make one syscall\n");
187 		raise(SIGSTOP);
188 
189 		syscall(SYS_gettid, 10, 11, 12, 13, 14, 15);
190 		_exit(0);
191 	}
192 
193 	int status;
194 
195 	/* Wait for SIGSTOP. */
196 	if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
197 		err(1, "waitpid");
198 
199 	struct user_regs_struct regs;
200 
201 	printf("[RUN]\tSYSEMU\n");
202 	if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
203 		err(1, "PTRACE_SYSEMU");
204 	wait_trap(chld);
205 
206 	if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
207 		err(1, "PTRACE_GETREGS");
208 
209 	if (regs.user_syscall_nr != SYS_gettid ||
210 	    regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
211 	    regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
212 	    regs.user_arg4 != 14 || regs.user_arg5 != 15) {
213 		printf("[FAIL]\tInitial args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
214 		nerrs++;
215 	} else {
216 		printf("[OK]\tInitial nr and args are correct\n");
217 	}
218 
219 	printf("[RUN]\tRestart the syscall (ip = 0x%lx)\n",
220 	       (unsigned long)regs.user_ip);
221 
222 	/*
223 	 * This does exactly what it appears to do if syscall is int80 or
224 	 * SYSCALL64.  For SYSCALL32 or SYSENTER, though, this is highly
225 	 * magical.  It needs to work so that ptrace and syscall restart
226 	 * work as expected.
227 	 */
228 	regs.user_ax = regs.user_syscall_nr;
229 	regs.user_ip -= 2;
230 	if (ptrace(PTRACE_SETREGS, chld, 0, &regs) != 0)
231 		err(1, "PTRACE_SETREGS");
232 
233 	if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
234 		err(1, "PTRACE_SYSEMU");
235 	wait_trap(chld);
236 
237 	if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
238 		err(1, "PTRACE_GETREGS");
239 
240 	if (regs.user_syscall_nr != SYS_gettid ||
241 	    regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
242 	    regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
243 	    regs.user_arg4 != 14 || regs.user_arg5 != 15) {
244 		printf("[FAIL]\tRestart nr or args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
245 		nerrs++;
246 	} else {
247 		printf("[OK]\tRestarted nr and args are correct\n");
248 	}
249 
250 	printf("[RUN]\tChange nr and args and restart the syscall (ip = 0x%lx)\n",
251 	       (unsigned long)regs.user_ip);
252 
253 	regs.user_ax = SYS_getpid;
254 	regs.user_arg0 = 20;
255 	regs.user_arg1 = 21;
256 	regs.user_arg2 = 22;
257 	regs.user_arg3 = 23;
258 	regs.user_arg4 = 24;
259 	regs.user_arg5 = 25;
260 	regs.user_ip -= 2;
261 
262 	if (ptrace(PTRACE_SETREGS, chld, 0, &regs) != 0)
263 		err(1, "PTRACE_SETREGS");
264 
265 	if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
266 		err(1, "PTRACE_SYSEMU");
267 	wait_trap(chld);
268 
269 	if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
270 		err(1, "PTRACE_GETREGS");
271 
272 	if (regs.user_syscall_nr != SYS_getpid ||
273 	    regs.user_arg0 != 20 || regs.user_arg1 != 21 || regs.user_arg2 != 22 ||
274 	    regs.user_arg3 != 23 || regs.user_arg4 != 24 || regs.user_arg5 != 25) {
275 		printf("[FAIL]\tRestart nr or args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
276 		nerrs++;
277 	} else {
278 		printf("[OK]\tReplacement nr and args are correct\n");
279 	}
280 
281 	if (ptrace(PTRACE_CONT, chld, 0, 0) != 0)
282 		err(1, "PTRACE_CONT");
283 	if (waitpid(chld, &status, 0) != chld)
284 		err(1, "waitpid");
285 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
286 		printf("[FAIL]\tChild failed\n");
287 		nerrs++;
288 	} else {
289 		printf("[OK]\tChild exited cleanly\n");
290 	}
291 }
292 
293 static void test_restart_under_ptrace(void)
294 {
295 	printf("[RUN]\tkernel syscall restart under ptrace\n");
296 	pid_t chld = fork();
297 	if (chld < 0)
298 		err(1, "fork");
299 
300 	if (chld == 0) {
301 		if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
302 			err(1, "PTRACE_TRACEME");
303 
304 		printf("\tChild will take a nap until signaled\n");
305 		setsigign(SIGUSR1, SA_RESTART);
306 		raise(SIGSTOP);
307 
308 		syscall(SYS_pause, 0, 0, 0, 0, 0, 0);
309 		_exit(0);
310 	}
311 
312 	int status;
313 
314 	/* Wait for SIGSTOP. */
315 	if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
316 		err(1, "waitpid");
317 
318 	struct user_regs_struct regs;
319 
320 	printf("[RUN]\tSYSCALL\n");
321 	if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
322 		err(1, "PTRACE_SYSCALL");
323 	wait_trap(chld);
324 
325 	/* We should be stopped at pause(2) entry. */
326 
327 	if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
328 		err(1, "PTRACE_GETREGS");
329 
330 	if (regs.user_syscall_nr != SYS_pause ||
331 	    regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
332 	    regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
333 	    regs.user_arg4 != 0 || regs.user_arg5 != 0) {
334 		printf("[FAIL]\tInitial args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
335 		nerrs++;
336 	} else {
337 		printf("[OK]\tInitial nr and args are correct\n");
338 	}
339 
340 	/* Interrupt it. */
341 	kill(chld, SIGUSR1);
342 
343 	/* Advance.  We should be stopped at exit. */
344 	printf("[RUN]\tSYSCALL\n");
345 	if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
346 		err(1, "PTRACE_SYSCALL");
347 	wait_trap(chld);
348 
349 	if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
350 		err(1, "PTRACE_GETREGS");
351 
352 	if (regs.user_syscall_nr != SYS_pause ||
353 	    regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
354 	    regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
355 	    regs.user_arg4 != 0 || regs.user_arg5 != 0) {
356 		printf("[FAIL]\tArgs after SIGUSR1 are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
357 		nerrs++;
358 	} else {
359 		printf("[OK]\tArgs after SIGUSR1 are correct (ax = %ld)\n",
360 		       (long)regs.user_ax);
361 	}
362 
363 	/* Poke the regs back in.  This must not break anything. */
364 	if (ptrace(PTRACE_SETREGS, chld, 0, &regs) != 0)
365 		err(1, "PTRACE_SETREGS");
366 
367 	/* Catch the (ignored) SIGUSR1. */
368 	if (ptrace(PTRACE_CONT, chld, 0, 0) != 0)
369 		err(1, "PTRACE_CONT");
370 	if (waitpid(chld, &status, 0) != chld)
371 		err(1, "waitpid");
372 	if (!WIFSTOPPED(status)) {
373 		printf("[FAIL]\tChild was stopped for SIGUSR1 (status = 0x%x)\n", status);
374 		nerrs++;
375 	} else {
376 		printf("[OK]\tChild got SIGUSR1\n");
377 	}
378 
379 	/* The next event should be pause(2) again. */
380 	printf("[RUN]\tStep again\n");
381 	if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
382 		err(1, "PTRACE_SYSCALL");
383 	wait_trap(chld);
384 
385 	/* We should be stopped at pause(2) entry. */
386 
387 	if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
388 		err(1, "PTRACE_GETREGS");
389 
390 	if (regs.user_syscall_nr != SYS_pause ||
391 	    regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
392 	    regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
393 	    regs.user_arg4 != 0 || regs.user_arg5 != 0) {
394 		printf("[FAIL]\tpause did not restart (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
395 		nerrs++;
396 	} else {
397 		printf("[OK]\tpause(2) restarted correctly\n");
398 	}
399 
400 	/* Kill it. */
401 	kill(chld, SIGKILL);
402 	if (waitpid(chld, &status, 0) != chld)
403 		err(1, "waitpid");
404 }
405 
406 int main()
407 {
408 	printf("[RUN]\tCheck int80 return regs\n");
409 	test_sys32_regs(do_full_int80);
410 
411 #if defined(__i386__) && (!defined(__GLIBC__) || __GLIBC__ > 2 || __GLIBC_MINOR__ >= 16)
412 	vsyscall32 = (void *)getauxval(AT_SYSINFO);
413 	printf("[RUN]\tCheck AT_SYSINFO return regs\n");
414 	test_sys32_regs(do_full_vsyscall32);
415 #endif
416 
417 	test_ptrace_syscall_restart();
418 
419 	test_restart_under_ptrace();
420 
421 	return 0;
422 }
423