xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/x86/fsgsbase_restore.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * fsgsbase_restore.c, test ptrace vs fsgsbase
4*4882a593Smuzhiyun  * Copyright (c) 2020 Andy Lutomirski
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This test case simulates a tracer redirecting tracee execution to
7*4882a593Smuzhiyun  * a function and then restoring tracee state using PTRACE_GETREGS and
8*4882a593Smuzhiyun  * PTRACE_SETREGS.  This is similar to what gdb does when doing
9*4882a593Smuzhiyun  * 'p func()'.  The catch is that this test has the called function
10*4882a593Smuzhiyun  * modify a segment register.  This makes sure that ptrace correctly
11*4882a593Smuzhiyun  * restores segment state when using PTRACE_SETREGS.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This is not part of fsgsbase.c, because that test is 64-bit only.
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define _GNU_SOURCE
17*4882a593Smuzhiyun #include <stdio.h>
18*4882a593Smuzhiyun #include <stdlib.h>
19*4882a593Smuzhiyun #include <stdbool.h>
20*4882a593Smuzhiyun #include <string.h>
21*4882a593Smuzhiyun #include <sys/syscall.h>
22*4882a593Smuzhiyun #include <unistd.h>
23*4882a593Smuzhiyun #include <err.h>
24*4882a593Smuzhiyun #include <sys/user.h>
25*4882a593Smuzhiyun #include <asm/prctl.h>
26*4882a593Smuzhiyun #include <sys/prctl.h>
27*4882a593Smuzhiyun #include <asm/ldt.h>
28*4882a593Smuzhiyun #include <sys/mman.h>
29*4882a593Smuzhiyun #include <stddef.h>
30*4882a593Smuzhiyun #include <sys/ptrace.h>
31*4882a593Smuzhiyun #include <sys/wait.h>
32*4882a593Smuzhiyun #include <stdint.h>
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #define EXPECTED_VALUE 0x1337f00d
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #ifdef __x86_64__
37*4882a593Smuzhiyun # define SEG "%gs"
38*4882a593Smuzhiyun #else
39*4882a593Smuzhiyun # define SEG "%fs"
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun 
dereference_seg_base(void)42*4882a593Smuzhiyun static unsigned int dereference_seg_base(void)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	int ret;
45*4882a593Smuzhiyun 	asm volatile ("mov %" SEG ":(0), %0" : "=rm" (ret));
46*4882a593Smuzhiyun 	return ret;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun 
init_seg(void)49*4882a593Smuzhiyun static void init_seg(void)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	unsigned int *target = mmap(
52*4882a593Smuzhiyun 		NULL, sizeof(unsigned int),
53*4882a593Smuzhiyun 		PROT_READ | PROT_WRITE,
54*4882a593Smuzhiyun 		MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
55*4882a593Smuzhiyun 	if (target == MAP_FAILED)
56*4882a593Smuzhiyun 		err(1, "mmap");
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	*target = EXPECTED_VALUE;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	printf("\tsegment base address = 0x%lx\n", (unsigned long)target);
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	struct user_desc desc = {
63*4882a593Smuzhiyun 		.entry_number    = 0,
64*4882a593Smuzhiyun 		.base_addr       = (unsigned int)(uintptr_t)target,
65*4882a593Smuzhiyun 		.limit           = sizeof(unsigned int) - 1,
66*4882a593Smuzhiyun 		.seg_32bit       = 1,
67*4882a593Smuzhiyun 		.contents        = 0, /* Data, grow-up */
68*4882a593Smuzhiyun 		.read_exec_only  = 0,
69*4882a593Smuzhiyun 		.limit_in_pages  = 0,
70*4882a593Smuzhiyun 		.seg_not_present = 0,
71*4882a593Smuzhiyun 		.useable         = 0
72*4882a593Smuzhiyun 	};
73*4882a593Smuzhiyun 	if (syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)) == 0) {
74*4882a593Smuzhiyun 		printf("\tusing LDT slot 0\n");
75*4882a593Smuzhiyun 		asm volatile ("mov %0, %" SEG :: "rm" ((unsigned short)0x7));
76*4882a593Smuzhiyun 	} else {
77*4882a593Smuzhiyun 		/* No modify_ldt for us (configured out, perhaps) */
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 		struct user_desc *low_desc = mmap(
80*4882a593Smuzhiyun 			NULL, sizeof(desc),
81*4882a593Smuzhiyun 			PROT_READ | PROT_WRITE,
82*4882a593Smuzhiyun 			MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
83*4882a593Smuzhiyun 		memcpy(low_desc, &desc, sizeof(desc));
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 		low_desc->entry_number = -1;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 		/* 32-bit set_thread_area */
88*4882a593Smuzhiyun 		long ret;
89*4882a593Smuzhiyun 		asm volatile ("int $0x80"
90*4882a593Smuzhiyun 			      : "=a" (ret), "+m" (*low_desc)
91*4882a593Smuzhiyun 			      : "a" (243), "b" (low_desc)
92*4882a593Smuzhiyun #ifdef __x86_64__
93*4882a593Smuzhiyun 			      : "r8", "r9", "r10", "r11"
94*4882a593Smuzhiyun #endif
95*4882a593Smuzhiyun 			);
96*4882a593Smuzhiyun 		memcpy(&desc, low_desc, sizeof(desc));
97*4882a593Smuzhiyun 		munmap(low_desc, sizeof(desc));
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 		if (ret != 0) {
100*4882a593Smuzhiyun 			printf("[NOTE]\tcould not create a segment -- can't test anything\n");
101*4882a593Smuzhiyun 			exit(0);
102*4882a593Smuzhiyun 		}
103*4882a593Smuzhiyun 		printf("\tusing GDT slot %d\n", desc.entry_number);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 		unsigned short sel = (unsigned short)((desc.entry_number << 3) | 0x3);
106*4882a593Smuzhiyun 		asm volatile ("mov %0, %" SEG :: "rm" (sel));
107*4882a593Smuzhiyun 	}
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun 
tracee_zap_segment(void)110*4882a593Smuzhiyun static void tracee_zap_segment(void)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	/*
113*4882a593Smuzhiyun 	 * The tracer will redirect execution here.  This is meant to
114*4882a593Smuzhiyun 	 * work like gdb's 'p func()' feature.  The tricky bit is that
115*4882a593Smuzhiyun 	 * we modify a segment register in order to make sure that ptrace
116*4882a593Smuzhiyun 	 * can correctly restore segment registers.
117*4882a593Smuzhiyun 	 */
118*4882a593Smuzhiyun 	printf("\tTracee: in tracee_zap_segment()\n");
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	/*
121*4882a593Smuzhiyun 	 * Write a nonzero selector with base zero to the segment register.
122*4882a593Smuzhiyun 	 * Using a null selector would defeat the test on AMD pre-Zen2
123*4882a593Smuzhiyun 	 * CPUs, as such CPUs don't clear the base when loading a null
124*4882a593Smuzhiyun 	 * selector.
125*4882a593Smuzhiyun 	 */
126*4882a593Smuzhiyun 	unsigned short sel;
127*4882a593Smuzhiyun 	asm volatile ("mov %%ss, %0\n\t"
128*4882a593Smuzhiyun 		      "mov %0, %" SEG
129*4882a593Smuzhiyun 		      : "=rm" (sel));
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	pid_t pid = getpid(), tid = syscall(SYS_gettid);
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	printf("\tTracee is going back to sleep\n");
134*4882a593Smuzhiyun 	syscall(SYS_tgkill, pid, tid, SIGSTOP);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	/* Should not get here. */
137*4882a593Smuzhiyun 	while (true) {
138*4882a593Smuzhiyun 		printf("[FAIL]\tTracee hit unreachable code\n");
139*4882a593Smuzhiyun 		pause();
140*4882a593Smuzhiyun 	}
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
main()143*4882a593Smuzhiyun int main()
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	printf("\tSetting up a segment\n");
146*4882a593Smuzhiyun 	init_seg();
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	unsigned int val = dereference_seg_base();
149*4882a593Smuzhiyun 	if (val != EXPECTED_VALUE) {
150*4882a593Smuzhiyun 		printf("[FAIL]\tseg[0] == %x; should be %x\n", val, EXPECTED_VALUE);
151*4882a593Smuzhiyun 		return 1;
152*4882a593Smuzhiyun 	}
153*4882a593Smuzhiyun 	printf("[OK]\tThe segment points to the right place.\n");
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	pid_t chld = fork();
156*4882a593Smuzhiyun 	if (chld < 0)
157*4882a593Smuzhiyun 		err(1, "fork");
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	if (chld == 0) {
160*4882a593Smuzhiyun 		prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0, 0);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 		if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
163*4882a593Smuzhiyun 			err(1, "PTRACE_TRACEME");
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 		pid_t pid = getpid(), tid = syscall(SYS_gettid);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 		printf("\tTracee will take a nap until signaled\n");
168*4882a593Smuzhiyun 		syscall(SYS_tgkill, pid, tid, SIGSTOP);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 		printf("\tTracee was resumed.  Will re-check segment.\n");
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 		val = dereference_seg_base();
173*4882a593Smuzhiyun 		if (val != EXPECTED_VALUE) {
174*4882a593Smuzhiyun 			printf("[FAIL]\tseg[0] == %x; should be %x\n", val, EXPECTED_VALUE);
175*4882a593Smuzhiyun 			exit(1);
176*4882a593Smuzhiyun 		}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 		printf("[OK]\tThe segment points to the right place.\n");
179*4882a593Smuzhiyun 		exit(0);
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	int status;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	/* Wait for SIGSTOP. */
185*4882a593Smuzhiyun 	if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
186*4882a593Smuzhiyun 		err(1, "waitpid");
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	struct user_regs_struct regs;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	if (ptrace(PTRACE_GETREGS, chld, NULL, &regs) != 0)
191*4882a593Smuzhiyun 		err(1, "PTRACE_GETREGS");
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun #ifdef __x86_64__
194*4882a593Smuzhiyun 	printf("\tChild GS=0x%lx, GSBASE=0x%lx\n", (unsigned long)regs.gs, (unsigned long)regs.gs_base);
195*4882a593Smuzhiyun #else
196*4882a593Smuzhiyun 	printf("\tChild FS=0x%lx\n", (unsigned long)regs.xfs);
197*4882a593Smuzhiyun #endif
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	struct user_regs_struct regs2 = regs;
200*4882a593Smuzhiyun #ifdef __x86_64__
201*4882a593Smuzhiyun 	regs2.rip = (unsigned long)tracee_zap_segment;
202*4882a593Smuzhiyun 	regs2.rsp -= 128;	/* Don't clobber the redzone. */
203*4882a593Smuzhiyun #else
204*4882a593Smuzhiyun 	regs2.eip = (unsigned long)tracee_zap_segment;
205*4882a593Smuzhiyun #endif
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	printf("\tTracer: redirecting tracee to tracee_zap_segment()\n");
208*4882a593Smuzhiyun 	if (ptrace(PTRACE_SETREGS, chld, NULL, &regs2) != 0)
209*4882a593Smuzhiyun 		err(1, "PTRACE_GETREGS");
210*4882a593Smuzhiyun 	if (ptrace(PTRACE_CONT, chld, NULL, NULL) != 0)
211*4882a593Smuzhiyun 		err(1, "PTRACE_GETREGS");
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	/* Wait for SIGSTOP. */
214*4882a593Smuzhiyun 	if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
215*4882a593Smuzhiyun 		err(1, "waitpid");
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	printf("\tTracer: restoring tracee state\n");
218*4882a593Smuzhiyun 	if (ptrace(PTRACE_SETREGS, chld, NULL, &regs) != 0)
219*4882a593Smuzhiyun 		err(1, "PTRACE_GETREGS");
220*4882a593Smuzhiyun 	if (ptrace(PTRACE_DETACH, chld, NULL, NULL) != 0)
221*4882a593Smuzhiyun 		err(1, "PTRACE_GETREGS");
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	/* Wait for SIGSTOP. */
224*4882a593Smuzhiyun 	if (waitpid(chld, &status, 0) != chld)
225*4882a593Smuzhiyun 		err(1, "waitpid");
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	if (WIFSIGNALED(status)) {
228*4882a593Smuzhiyun 		printf("[FAIL]\tTracee crashed\n");
229*4882a593Smuzhiyun 		return 1;
230*4882a593Smuzhiyun 	}
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	if (!WIFEXITED(status)) {
233*4882a593Smuzhiyun 		printf("[FAIL]\tTracee stopped for an unexpected reason: %d\n", status);
234*4882a593Smuzhiyun 		return 1;
235*4882a593Smuzhiyun 	}
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	int exitcode = WEXITSTATUS(status);
238*4882a593Smuzhiyun 	if (exitcode != 0) {
239*4882a593Smuzhiyun 		printf("[FAIL]\tTracee reported failure\n");
240*4882a593Smuzhiyun 		return 1;
241*4882a593Smuzhiyun 	}
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	printf("[OK]\tAll is well.\n");
244*4882a593Smuzhiyun 	return 0;
245*4882a593Smuzhiyun }
246