2 * fsgsbase.c, an fsgsbase test
3 * Copyright (c) 2014-2016 Andy Lutomirski
12 #include <sys/syscall.h>
16 #include <asm/prctl.h>
17 #include <sys/prctl.h>
20 #include <sys/ucontext.h>
22 #include <linux/futex.h>
28 # error This test is 64-bit only
31 static volatile sig_atomic_t want_segv
;
32 static volatile unsigned long segv_addr
;
36 static void sethandler(int sig
, void (*handler
)(int, siginfo_t
*, void *),
40 memset(&sa
, 0, sizeof(sa
));
41 sa
.sa_sigaction
= handler
;
42 sa
.sa_flags
= SA_SIGINFO
| flags
;
43 sigemptyset(&sa
.sa_mask
);
44 if (sigaction(sig
, &sa
, 0))
48 static void clearhandler(int sig
)
51 memset(&sa
, 0, sizeof(sa
));
52 sa
.sa_handler
= SIG_DFL
;
53 sigemptyset(&sa
.sa_mask
);
54 if (sigaction(sig
, &sa
, 0))
58 static void sigsegv(int sig
, siginfo_t
*si
, void *ctx_void
)
60 ucontext_t
*ctx
= (ucontext_t
*)ctx_void
;
63 clearhandler(SIGSEGV
);
64 return; /* Crash cleanly. */
68 segv_addr
= (unsigned long)si
->si_addr
;
70 ctx
->uc_mcontext
.gregs
[REG_RIP
] += 4; /* Skip the faulting mov */
74 enum which_base
{ FS
, GS
};
76 static unsigned long read_base(enum which_base which
)
80 * Unless we have FSGSBASE, there's no direct way to do this from
81 * user mode. We can get at it indirectly using signals, though.
88 /* Use a constant-length instruction here. */
89 asm volatile ("mov %%fs:(%%rcx), %%rax" : : "c" (offset
) : "rax");
91 asm volatile ("mov %%gs:(%%rcx), %%rax" : : "c" (offset
) : "rax");
94 return segv_addr
+ offset
;
97 * If that didn't segfault, try the other end of the address space.
98 * Unless we get really unlucky and run into the vsyscall page, this
99 * is guaranteed to segfault.
102 offset
= (ULONG_MAX
>> 1) + 1;
104 asm volatile ("mov %%fs:(%%rcx), %%rax"
105 : : "c" (offset
) : "rax");
107 asm volatile ("mov %%gs:(%%rcx), %%rax"
108 : : "c" (offset
) : "rax");
111 return segv_addr
+ offset
;
116 static void check_gs_value(unsigned long value
)
121 printf("[RUN]\tARCH_SET_GS to 0x%lx\n", value
);
122 if (syscall(SYS_arch_prctl
, ARCH_SET_GS
, value
) != 0)
123 err(1, "ARCH_SET_GS");
125 asm volatile ("mov %%gs, %0" : "=rm" (sel
));
126 base
= read_base(GS
);
128 printf("[OK]\tGSBASE was set as expected (selector 0x%hx)\n",
132 printf("[FAIL]\tGSBASE was not as expected: got 0x%lx (selector 0x%hx)\n",
136 if (syscall(SYS_arch_prctl
, ARCH_GET_GS
, &base
) != 0)
137 err(1, "ARCH_GET_GS");
139 printf("[OK]\tARCH_GET_GS worked as expected (selector 0x%hx)\n",
143 printf("[FAIL]\tARCH_GET_GS was not as expected: got 0x%lx (selector 0x%hx)\n",
148 static void mov_0_gs(unsigned long initial_base
, bool schedule
)
150 unsigned long base
, arch_base
;
152 printf("[RUN]\tARCH_SET_GS to 0x%lx then mov 0 to %%gs%s\n", initial_base
, schedule
? " and schedule " : "");
153 if (syscall(SYS_arch_prctl
, ARCH_SET_GS
, initial_base
) != 0)
154 err(1, "ARCH_SET_GS");
159 asm volatile ("mov %0, %%gs" : : "rm" (0));
160 base
= read_base(GS
);
161 if (syscall(SYS_arch_prctl
, ARCH_GET_GS
, &arch_base
) != 0)
162 err(1, "ARCH_GET_GS");
163 if (base
== arch_base
) {
164 printf("[OK]\tGSBASE is 0x%lx\n", base
);
167 printf("[FAIL]\tGSBASE changed to 0x%lx but kernel reports 0x%lx\n", base
, arch_base
);
171 static volatile unsigned long remote_base
;
172 static volatile bool remote_hard_zero
;
173 static volatile unsigned int ftx
;
176 * ARCH_SET_FS/GS(0) may or may not program a selector of zero. HARD_ZERO
177 * means to force the selector to zero to improve test coverage.
179 #define HARD_ZERO 0xa1fa5f343cb85fa4
181 static void do_remote_base()
183 unsigned long to_set
= remote_base
;
184 bool hard_zero
= false;
185 if (to_set
== HARD_ZERO
) {
190 if (syscall(SYS_arch_prctl
, ARCH_SET_GS
, to_set
) != 0)
191 err(1, "ARCH_SET_GS");
194 asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0));
197 asm volatile ("mov %%gs, %0" : "=rm" (sel
));
198 printf("\tother thread: ARCH_SET_GS(0x%lx)%s -- sel is 0x%hx\n",
199 to_set
, hard_zero
? " and clear gs" : "", sel
);
202 void do_unexpected_base(void)
205 * The goal here is to try to arrange for GS == 0, GSBASE !=
206 * 0, and for the the kernel the think that GSBASE == 0.
208 * To make the test as reliable as possible, this uses
209 * explicit descriptorss. (This is not the only way. This
210 * could use ARCH_SET_GS with a low, nonzero base, but the
211 * relevant side effect of ARCH_SET_GS could change.)
214 /* Step 1: tell the kernel that we have GSBASE == 0. */
215 if (syscall(SYS_arch_prctl
, ARCH_SET_GS
, 0) != 0)
216 err(1, "ARCH_SET_GS");
218 /* Step 2: change GSBASE without telling the kernel. */
219 struct user_desc desc
= {
221 .base_addr
= 0xBAADF00D,
224 .contents
= 0, /* Data, grow-up */
227 .seg_not_present
= 0,
230 if (syscall(SYS_modify_ldt
, 1, &desc
, sizeof(desc
)) == 0) {
231 printf("\tother thread: using LDT slot 0\n");
232 asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0x7));
234 /* No modify_ldt for us (configured out, perhaps) */
236 struct user_desc
*low_desc
= mmap(
238 PROT_READ
| PROT_WRITE
,
239 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_32BIT
, -1, 0);
240 memcpy(low_desc
, &desc
, sizeof(desc
));
242 low_desc
->entry_number
= -1;
244 /* 32-bit set_thread_area */
246 asm volatile ("int $0x80"
247 : "=a" (ret
) : "a" (243), "b" (low_desc
)
249 memcpy(&desc
, low_desc
, sizeof(desc
));
250 munmap(low_desc
, sizeof(desc
));
253 printf("[NOTE]\tcould not create a segment -- test won't do anything\n");
256 printf("\tother thread: using GDT slot %d\n", desc
.entry_number
);
257 asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)((desc
.entry_number
<< 3) | 0x3)));
261 * Step 3: set the selector back to zero. On AMD chips, this will
265 asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0));
268 static void *threadproc(void *ctx
)
272 syscall(SYS_futex
, &ftx
, FUTEX_WAIT
, 0, NULL
, NULL
, 0);
279 do_unexpected_base();
281 errx(1, "helper thread got bad command");
284 syscall(SYS_futex
, &ftx
, FUTEX_WAKE
, 0, NULL
, NULL
, 0);
288 static void set_gs_and_switch_to(unsigned long local
, unsigned long remote
)
292 bool hard_zero
= false;
293 if (local
== HARD_ZERO
) {
298 printf("[RUN]\tARCH_SET_GS(0x%lx)%s, then schedule to 0x%lx\n",
299 local
, hard_zero
? " and clear gs" : "", remote
);
300 if (syscall(SYS_arch_prctl
, ARCH_SET_GS
, local
) != 0)
301 err(1, "ARCH_SET_GS");
303 asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0));
305 if (read_base(GS
) != local
) {
307 printf("[FAIL]\tGSBASE wasn't set as expected\n");
310 remote_base
= remote
;
312 syscall(SYS_futex
, &ftx
, FUTEX_WAKE
, 0, NULL
, NULL
, 0);
314 syscall(SYS_futex
, &ftx
, FUTEX_WAIT
, 1, NULL
, NULL
, 0);
316 base
= read_base(GS
);
318 printf("[OK]\tGSBASE remained 0x%lx\n", local
);
321 printf("[FAIL]\tGSBASE changed to 0x%lx\n", base
);
325 static void test_unexpected_base(void)
329 printf("[RUN]\tARCH_SET_GS(0), clear gs, then manipulate GSBASE in a different thread\n");
330 if (syscall(SYS_arch_prctl
, ARCH_SET_GS
, 0) != 0)
331 err(1, "ARCH_SET_GS");
332 asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0));
335 syscall(SYS_futex
, &ftx
, FUTEX_WAKE
, 0, NULL
, NULL
, 0);
337 syscall(SYS_futex
, &ftx
, FUTEX_WAIT
, 1, NULL
, NULL
, 0);
339 base
= read_base(GS
);
341 printf("[OK]\tGSBASE remained 0\n");
344 printf("[FAIL]\tGSBASE changed to 0x%lx\n", base
);
352 sethandler(SIGSEGV
, sigsegv
, 0);
356 check_gs_value(0x200000000);
358 check_gs_value(0x200000000);
361 for (int sched
= 0; sched
< 2; sched
++) {
362 mov_0_gs(0, !!sched
);
363 mov_0_gs(1, !!sched
);
364 mov_0_gs(0x200000000, !!sched
);
367 /* Set up for multithreading. */
372 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0)
373 err(1, "sched_setaffinity to CPU 0"); /* should never fail */
375 if (pthread_create(&thread
, 0, threadproc
, 0) != 0)
376 err(1, "pthread_create");
378 static unsigned long bases_with_hard_zero
[] = {
379 0, HARD_ZERO
, 1, 0x200000000,
382 for (int local
= 0; local
< 4; local
++) {
383 for (int remote
= 0; remote
< 4; remote
++) {
384 set_gs_and_switch_to(bases_with_hard_zero
[local
],
385 bases_with_hard_zero
[remote
]);
389 test_unexpected_base();
391 ftx
= 3; /* Kill the thread. */
392 syscall(SYS_futex
, &ftx
, FUTEX_WAKE
, 0, NULL
, NULL
, 0);
394 if (pthread_join(thread
, NULL
) != 0)
395 err(1, "pthread_join");
397 return nerrs
== 0 ? 0 : 1;