1 // SPDX-License-Identifier: GPL-2.0-only
3 * fsgsbase_restore.c, test ptrace vs fsgsbase
4 * Copyright (c) 2020 Andy Lutomirski
6 * This test case simulates a tracer redirecting tracee execution to
7 * a function and then restoring tracee state using PTRACE_GETREGS and
8 * PTRACE_SETREGS. This is similar to what gdb does when doing
9 * 'p func()'. The catch is that this test has the called function
10 * modify a segment register. This makes sure that ptrace correctly
11 * restores segment state when using PTRACE_SETREGS.
13 * This is not part of fsgsbase.c, because that test is 64-bit only.
21 #include <sys/syscall.h>
25 #include <asm/prctl.h>
26 #include <sys/prctl.h>
30 #include <sys/ptrace.h>
34 #define EXPECTED_VALUE 0x1337f00d
43 * Defined in clang_helpers_[32|64].S, because unlike gcc, clang inline asm does
44 * not support segmentation prefixes.
46 unsigned int dereference_seg_base(void);
48 static void init_seg(void)
50 unsigned int *target
= mmap(
51 NULL
, sizeof(unsigned int),
52 PROT_READ
| PROT_WRITE
,
53 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_32BIT
, -1, 0);
54 if (target
== MAP_FAILED
)
57 *target
= EXPECTED_VALUE
;
59 printf("\tsegment base address = 0x%lx\n", (unsigned long)target
);
61 struct user_desc desc
= {
63 .base_addr
= (unsigned int)(uintptr_t)target
,
64 .limit
= sizeof(unsigned int) - 1,
66 .contents
= 0, /* Data, grow-up */
72 if (syscall(SYS_modify_ldt
, 1, &desc
, sizeof(desc
)) == 0) {
73 printf("\tusing LDT slot 0\n");
74 asm volatile ("mov %0, %" SEG :: "rm" ((unsigned short)0x7));
76 /* No modify_ldt for us (configured out, perhaps) */
78 struct user_desc
*low_desc
= mmap(
80 PROT_READ
| PROT_WRITE
,
81 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_32BIT
, -1, 0);
82 memcpy(low_desc
, &desc
, sizeof(desc
));
84 low_desc
->entry_number
= -1;
86 /* 32-bit set_thread_area */
88 asm volatile ("int $0x80"
89 : "=a" (ret
), "+m" (*low_desc
)
90 : "a" (243), "b" (low_desc
)
92 : "r8", "r9", "r10", "r11"
95 memcpy(&desc
, low_desc
, sizeof(desc
));
96 munmap(low_desc
, sizeof(desc
));
99 printf("[NOTE]\tcould not create a segment -- can't test anything\n");
102 printf("\tusing GDT slot %d\n", desc
.entry_number
);
104 unsigned short sel
= (unsigned short)((desc
.entry_number
<< 3) | 0x3);
105 asm volatile ("mov %0, %" SEG :: "rm" (sel
));
109 static void tracee_zap_segment(void)
112 * The tracer will redirect execution here. This is meant to
113 * work like gdb's 'p func()' feature. The tricky bit is that
114 * we modify a segment register in order to make sure that ptrace
115 * can correctly restore segment registers.
117 printf("\tTracee: in tracee_zap_segment()\n");
120 * Write a nonzero selector with base zero to the segment register.
121 * Using a null selector would defeat the test on AMD pre-Zen2
122 * CPUs, as such CPUs don't clear the base when loading a null
126 asm volatile ("mov %%ss, %0\n\t"
130 pid_t pid
= getpid(), tid
= syscall(SYS_gettid
);
132 printf("\tTracee is going back to sleep\n");
133 syscall(SYS_tgkill
, pid
, tid
, SIGSTOP
);
135 /* Should not get here. */
137 printf("[FAIL]\tTracee hit unreachable code\n");
144 printf("\tSetting up a segment\n");
147 unsigned int val
= dereference_seg_base();
148 if (val
!= EXPECTED_VALUE
) {
149 printf("[FAIL]\tseg[0] == %x; should be %x\n", val
, EXPECTED_VALUE
);
152 printf("[OK]\tThe segment points to the right place.\n");
159 prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0, 0);
161 if (ptrace(PTRACE_TRACEME
, 0, 0, 0) != 0)
162 err(1, "PTRACE_TRACEME");
164 pid_t pid
= getpid(), tid
= syscall(SYS_gettid
);
166 printf("\tTracee will take a nap until signaled\n");
167 syscall(SYS_tgkill
, pid
, tid
, SIGSTOP
);
169 printf("\tTracee was resumed. Will re-check segment.\n");
171 val
= dereference_seg_base();
172 if (val
!= EXPECTED_VALUE
) {
173 printf("[FAIL]\tseg[0] == %x; should be %x\n", val
, EXPECTED_VALUE
);
177 printf("[OK]\tThe segment points to the right place.\n");
183 /* Wait for SIGSTOP. */
184 if (waitpid(chld
, &status
, 0) != chld
|| !WIFSTOPPED(status
))
187 struct user_regs_struct regs
;
189 if (ptrace(PTRACE_GETREGS
, chld
, NULL
, ®s
) != 0)
190 err(1, "PTRACE_GETREGS");
193 printf("\tChild GS=0x%lx, GSBASE=0x%lx\n", (unsigned long)regs
.gs
, (unsigned long)regs
.gs_base
);
195 printf("\tChild FS=0x%lx\n", (unsigned long)regs
.xfs
);
198 struct user_regs_struct regs2
= regs
;
200 regs2
.rip
= (unsigned long)tracee_zap_segment
;
201 regs2
.rsp
-= 128; /* Don't clobber the redzone. */
203 regs2
.eip
= (unsigned long)tracee_zap_segment
;
206 printf("\tTracer: redirecting tracee to tracee_zap_segment()\n");
207 if (ptrace(PTRACE_SETREGS
, chld
, NULL
, ®s2
) != 0)
208 err(1, "PTRACE_GETREGS");
209 if (ptrace(PTRACE_CONT
, chld
, NULL
, NULL
) != 0)
210 err(1, "PTRACE_GETREGS");
212 /* Wait for SIGSTOP. */
213 if (waitpid(chld
, &status
, 0) != chld
|| !WIFSTOPPED(status
))
216 printf("\tTracer: restoring tracee state\n");
217 if (ptrace(PTRACE_SETREGS
, chld
, NULL
, ®s
) != 0)
218 err(1, "PTRACE_GETREGS");
219 if (ptrace(PTRACE_DETACH
, chld
, NULL
, NULL
) != 0)
220 err(1, "PTRACE_GETREGS");
222 /* Wait for SIGSTOP. */
223 if (waitpid(chld
, &status
, 0) != chld
)
226 if (WIFSIGNALED(status
)) {
227 printf("[FAIL]\tTracee crashed\n");
231 if (!WIFEXITED(status
)) {
232 printf("[FAIL]\tTracee stopped for an unexpected reason: %d\n", status
);
236 int exitcode
= WEXITSTATUS(status
);
238 printf("[FAIL]\tTracee reported failure\n");
242 printf("[OK]\tAll is well.\n");