t_user_ldt.c revision 1.4 1 1.4 maxv /* $NetBSD: t_user_ldt.c,v 1.4 2020/07/03 16:07:52 maxv Exp $ */
2 1.1 maxv
3 1.1 maxv /*
4 1.1 maxv * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.1 maxv * All rights reserved.
6 1.1 maxv *
7 1.1 maxv * This code is derived from software contributed to The NetBSD Foundation
8 1.1 maxv * by Maxime Villard.
9 1.1 maxv *
10 1.1 maxv * Redistribution and use in source and binary forms, with or without
11 1.1 maxv * modification, are permitted provided that the following conditions
12 1.1 maxv * are met:
13 1.1 maxv * 1. Redistributions of source code must retain the above copyright
14 1.1 maxv * notice, this list of conditions and the following disclaimer.
15 1.1 maxv * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 maxv * notice, this list of conditions and the following disclaimer in the
17 1.1 maxv * documentation and/or other materials provided with the distribution.
18 1.1 maxv *
19 1.1 maxv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 maxv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 maxv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 maxv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 maxv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 maxv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 maxv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 maxv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 maxv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 maxv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 maxv * POSSIBILITY OF SUCH DAMAGE.
30 1.1 maxv */
31 1.1 maxv
32 1.1 maxv #include <stdio.h>
33 1.1 maxv #include <stdlib.h>
34 1.1 maxv #include <string.h>
35 1.1 maxv #include <unistd.h>
36 1.1 maxv #include <errno.h>
37 1.1 maxv #include <signal.h>
38 1.1 maxv
39 1.1 maxv #include <sys/types.h>
40 1.1 maxv #include <sys/mman.h>
41 1.1 maxv #include <machine/segments.h>
42 1.1 maxv #include <machine/sysarch.h>
43 1.1 maxv #include <machine/vmparam.h>
44 1.1 maxv
45 1.2 maxv #define _LOCORE /* XXX a bit of a hack, but whatever */
46 1.2 maxv #include <machine/gdt.h>
47 1.2 maxv #undef _LOCORE
48 1.2 maxv
49 1.1 maxv #include <atf-c.h>
50 1.1 maxv
51 1.1 maxv static uint8_t *ldt_base;
52 1.1 maxv static bool user_ldt_supported;
53 1.1 maxv
54 1.1 maxv static void
55 1.1 maxv user_ldt_detect(void)
56 1.1 maxv {
57 1.1 maxv union descriptor desc;
58 1.1 maxv int ret;
59 1.1 maxv
60 1.1 maxv ret = i386_get_ldt(0, &desc, 1);
61 1.3 maxv user_ldt_supported = (ret != -1) || (errno != ENOSYS);
62 1.1 maxv }
63 1.1 maxv
64 1.1 maxv static void
65 1.1 maxv init_ldt_base(void)
66 1.1 maxv {
67 1.1 maxv ldt_base = (uint8_t *)mmap(NULL, 2 * PAGE_SIZE, PROT_READ | PROT_WRITE,
68 1.1 maxv MAP_PRIVATE | MAP_ANON, -1, 0);
69 1.1 maxv if (ldt_base == MAP_FAILED)
70 1.1 maxv atf_tc_fail("mmap failed");
71 1.1 maxv munmap(ldt_base + PAGE_SIZE, PAGE_SIZE);
72 1.1 maxv }
73 1.1 maxv
74 1.1 maxv static void
75 1.1 maxv build_desc(union descriptor *desc, void *basep, uint32_t limit, int type,
76 1.1 maxv int dpl, int def32, int gran)
77 1.1 maxv {
78 1.1 maxv uintptr_t base = (uintptr_t)basep;
79 1.1 maxv
80 1.1 maxv limit--;
81 1.1 maxv
82 1.1 maxv desc->sd.sd_lolimit = limit & 0x0000ffff;
83 1.1 maxv desc->sd.sd_lobase = base & 0x00ffffff;
84 1.1 maxv desc->sd.sd_type = type & 0x1F;
85 1.1 maxv desc->sd.sd_dpl = dpl & 0x3;
86 1.1 maxv desc->sd.sd_p = 1;
87 1.1 maxv desc->sd.sd_hilimit = (limit & 0x00ff0000) >> 16;
88 1.1 maxv desc->sd.sd_xx = 0;
89 1.1 maxv desc->sd.sd_def32 = def32 ? 1 : 0;
90 1.1 maxv desc->sd.sd_gran = gran ? 1 : 0;
91 1.1 maxv desc->sd.sd_hibase = (base & 0xff000000) >> 24;
92 1.1 maxv }
93 1.1 maxv
94 1.1 maxv static void
95 1.4 maxv set_ds(unsigned int val)
96 1.4 maxv {
97 1.4 maxv __asm volatile("mov %0,%%ds"::"r" ((unsigned short)val));
98 1.4 maxv }
99 1.4 maxv
100 1.4 maxv static void
101 1.1 maxv set_fs(unsigned int val)
102 1.1 maxv {
103 1.1 maxv __asm volatile("mov %0,%%fs"::"r" ((unsigned short)val));
104 1.1 maxv }
105 1.1 maxv
106 1.1 maxv static uint8_t __noinline
107 1.1 maxv get_fs_byte(const char *addr)
108 1.1 maxv {
109 1.1 maxv uint8_t val;
110 1.1 maxv __asm volatile (
111 1.1 maxv ".globl fs_read_begin; fs_read_begin:\n"
112 1.1 maxv "movb %%fs:%1,%0\n"
113 1.1 maxv ".globl fs_read_end; fs_read_end:\n"
114 1.1 maxv : "=q" (val) : "m" (*addr)
115 1.1 maxv );
116 1.1 maxv return val;
117 1.1 maxv }
118 1.1 maxv
119 1.1 maxv /* -------------------------------------------------------------------------- */
120 1.1 maxv
121 1.1 maxv ATF_TC(filter_ops);
122 1.1 maxv ATF_TC_HEAD(filter_ops, tc)
123 1.1 maxv {
124 1.1 maxv atf_tc_set_md_var(tc, "descr",
125 1.1 maxv "Ensure that the kernel correctly filters the descriptors");
126 1.1 maxv }
127 1.1 maxv ATF_TC_BODY(filter_ops, tc)
128 1.1 maxv {
129 1.1 maxv union descriptor desc;
130 1.1 maxv const int forbidden_types[] = {
131 1.1 maxv SDT_SYS286TSS,
132 1.1 maxv SDT_SYSLDT,
133 1.1 maxv SDT_SYS286BSY,
134 1.1 maxv SDT_SYS286CGT,
135 1.1 maxv SDT_SYSTASKGT,
136 1.1 maxv SDT_SYS286IGT,
137 1.1 maxv SDT_SYS286TGT,
138 1.1 maxv SDT_SYSNULL2,
139 1.1 maxv SDT_SYS386TSS,
140 1.1 maxv SDT_SYSNULL3,
141 1.1 maxv SDT_SYS386BSY,
142 1.1 maxv SDT_SYS386CGT,
143 1.1 maxv SDT_SYSNULL4,
144 1.1 maxv SDT_SYS386IGT,
145 1.1 maxv SDT_SYS386TGT
146 1.1 maxv };
147 1.1 maxv size_t i;
148 1.1 maxv
149 1.1 maxv if (!user_ldt_supported) {
150 1.1 maxv atf_tc_skip("USER_LDT disabled");
151 1.1 maxv }
152 1.1 maxv
153 1.1 maxv /* The first LDT slots should not be settable. */
154 1.1 maxv for (i = 0; i < 10; i++) {
155 1.1 maxv build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW,
156 1.1 maxv SEL_UPL, 1, 0);
157 1.1 maxv ATF_REQUIRE_EQ(i386_set_ldt(i, &desc, 1), -1);
158 1.1 maxv ATF_REQUIRE_EQ(errno, EINVAL);
159 1.1 maxv }
160 1.1 maxv
161 1.1 maxv /* SEL_KPL should not be allowed. */
162 1.1 maxv build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW, SEL_KPL, 1, 0);
163 1.1 maxv ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), -1);
164 1.1 maxv ATF_REQUIRE_EQ(errno, EACCES);
165 1.1 maxv
166 1.1 maxv /* Long-mode segments should not be allowed. */
167 1.1 maxv build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW, SEL_UPL, 1, 0);
168 1.1 maxv desc.sd.sd_xx = 0b11; /* sd_avl | sd_long */
169 1.1 maxv ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), -1);
170 1.1 maxv ATF_REQUIRE_EQ(errno, EACCES);
171 1.1 maxv
172 1.1 maxv /* No forbidden type should be allowed. */
173 1.1 maxv for (i = 0; i < __arraycount(forbidden_types); i++) {
174 1.1 maxv build_desc(&desc, ldt_base, PAGE_SIZE, forbidden_types[i],
175 1.1 maxv SEL_UPL, 1, 0);
176 1.1 maxv ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), -1);
177 1.1 maxv ATF_REQUIRE_EQ(errno, EACCES);
178 1.1 maxv }
179 1.2 maxv
180 1.2 maxv /* Check the slot limit. */
181 1.2 maxv build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW, SEL_UPL, 1, 0);
182 1.2 maxv ATF_REQUIRE_EQ(i386_set_ldt(MAX_USERLDT_SLOTS-1, &desc, 1),
183 1.2 maxv MAX_USERLDT_SLOTS-1);
184 1.2 maxv ATF_REQUIRE_EQ(i386_set_ldt(MAX_USERLDT_SLOTS, &desc, 1), -1);
185 1.2 maxv ATF_REQUIRE_EQ(errno, EINVAL);
186 1.1 maxv }
187 1.1 maxv
188 1.1 maxv /* -------------------------------------------------------------------------- */
189 1.1 maxv
190 1.4 maxv static void
191 1.4 maxv iretq_gp__gp_handler(int signo, siginfo_t *sig, void *ctx)
192 1.4 maxv {
193 1.4 maxv ATF_REQUIRE(sig->si_signo == SIGSEGV);
194 1.4 maxv ATF_REQUIRE(sig->si_code == SEGV_ACCERR);
195 1.4 maxv atf_tc_pass();
196 1.4 maxv /* NOTREACHED */
197 1.4 maxv }
198 1.4 maxv
199 1.4 maxv ATF_TC(iretq_gp);
200 1.4 maxv ATF_TC_HEAD(iretq_gp, tc)
201 1.4 maxv {
202 1.4 maxv atf_tc_set_md_var(tc, "descr",
203 1.4 maxv "Ensure that the kernel correctly handles iretq #GP faults");
204 1.4 maxv }
205 1.4 maxv ATF_TC_BODY(iretq_gp, tc)
206 1.4 maxv {
207 1.4 maxv union descriptor desc;
208 1.4 maxv struct sigaction act;
209 1.4 maxv
210 1.4 maxv if (!user_ldt_supported) {
211 1.4 maxv atf_tc_skip("USER_LDT disabled");
212 1.4 maxv }
213 1.4 maxv
214 1.4 maxv /* Build a desc, set %ds to it. */
215 1.4 maxv build_desc(&desc, 0, 0xFFFFFUL, SDT_MEMRWA, SEL_UPL, 1, 1);
216 1.4 maxv ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), 256);
217 1.4 maxv set_ds(LSEL(256, SEL_UPL));
218 1.4 maxv
219 1.4 maxv /* Register the fault handler. */
220 1.4 maxv memset(&act, 0, sizeof(act));
221 1.4 maxv act.sa_sigaction = iretq_gp__gp_handler;
222 1.4 maxv act.sa_flags = SA_SIGINFO;
223 1.4 maxv ATF_REQUIRE_EQ(sigaction(SIGSEGV, &act, NULL), 0);
224 1.4 maxv
225 1.4 maxv /* Set NULL on %ds, iretq should fault. */
226 1.4 maxv memset(&desc, 0, sizeof(desc));
227 1.4 maxv ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), 256);
228 1.4 maxv
229 1.4 maxv atf_tc_fail("test did not fault as expected");
230 1.4 maxv }
231 1.4 maxv
232 1.4 maxv /* -------------------------------------------------------------------------- */
233 1.4 maxv
234 1.4 maxv static void
235 1.4 maxv iretq_np__np_handler(int signo, siginfo_t *sig, void *ctx)
236 1.4 maxv {
237 1.4 maxv ATF_REQUIRE(sig->si_signo == SIGBUS);
238 1.4 maxv ATF_REQUIRE(sig->si_code == BUS_ADRERR);
239 1.4 maxv atf_tc_pass();
240 1.4 maxv /* NOTREACHED */
241 1.4 maxv }
242 1.4 maxv
243 1.4 maxv ATF_TC(iretq_np);
244 1.4 maxv ATF_TC_HEAD(iretq_np, tc)
245 1.4 maxv {
246 1.4 maxv atf_tc_set_md_var(tc, "descr",
247 1.4 maxv "Ensure that the kernel correctly handles iretq #NP faults");
248 1.4 maxv }
249 1.4 maxv ATF_TC_BODY(iretq_np, tc)
250 1.4 maxv {
251 1.4 maxv union descriptor desc;
252 1.4 maxv struct sigaction act;
253 1.4 maxv
254 1.4 maxv if (!user_ldt_supported) {
255 1.4 maxv atf_tc_skip("USER_LDT disabled");
256 1.4 maxv }
257 1.4 maxv
258 1.4 maxv /* Build a desc, set %ds to it. */
259 1.4 maxv build_desc(&desc, 0, 0xFFFFFFFFUL, SDT_MEMRWA, SEL_UPL, 1, 1);
260 1.4 maxv ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), 256);
261 1.4 maxv set_ds(LSEL(256, SEL_UPL));
262 1.4 maxv
263 1.4 maxv /* Register the fault handler. */
264 1.4 maxv memset(&act, 0, sizeof(act));
265 1.4 maxv act.sa_sigaction = iretq_np__np_handler;
266 1.4 maxv act.sa_flags = SA_SIGINFO;
267 1.4 maxv ATF_REQUIRE_EQ(sigaction(SIGBUS, &act, NULL), 0);
268 1.4 maxv
269 1.4 maxv /* Set non-present on %ds, iretq should fault. */
270 1.4 maxv desc.sd.sd_p = 0;
271 1.4 maxv ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), 256);
272 1.4 maxv
273 1.4 maxv atf_tc_fail("test did not fault as expected");
274 1.4 maxv }
275 1.4 maxv
276 1.4 maxv /* -------------------------------------------------------------------------- */
277 1.4 maxv
278 1.1 maxv static volatile bool expect_crash;
279 1.1 maxv
280 1.1 maxv static void
281 1.4 maxv user_ldt__gp_handler(int signo, siginfo_t *sig, void *ctx)
282 1.1 maxv {
283 1.1 maxv ucontext_t *uctx = ctx;
284 1.1 maxv extern uint8_t fs_read_begin;
285 1.1 maxv
286 1.1 maxv if (!expect_crash) {
287 1.1 maxv atf_tc_fail("unexpected #GP");
288 1.1 maxv }
289 1.1 maxv
290 1.1 maxv ATF_REQUIRE(sig->si_signo == SIGSEGV);
291 1.1 maxv ATF_REQUIRE(sig->si_code == SEGV_ACCERR);
292 1.1 maxv
293 1.1 maxv if (uctx->uc_mcontext.__gregs[_REG_EIP] != (intptr_t)&fs_read_begin) {
294 1.1 maxv atf_tc_fail("got #GP on the wrong instruction");
295 1.1 maxv }
296 1.1 maxv
297 1.1 maxv set_fs(GSEL(GUDATA_SEL, SEL_UPL));
298 1.1 maxv
299 1.1 maxv atf_tc_pass();
300 1.1 maxv /* NOTREACHED */
301 1.1 maxv }
302 1.1 maxv
303 1.1 maxv ATF_TC(user_ldt);
304 1.1 maxv ATF_TC_HEAD(user_ldt, tc)
305 1.1 maxv {
306 1.1 maxv atf_tc_set_md_var(tc, "descr",
307 1.1 maxv "Ensure that USER_LDT works as expected");
308 1.1 maxv }
309 1.1 maxv ATF_TC_BODY(user_ldt, tc)
310 1.1 maxv {
311 1.1 maxv union descriptor desc;
312 1.1 maxv struct sigaction act;
313 1.1 maxv
314 1.1 maxv if (!user_ldt_supported) {
315 1.1 maxv atf_tc_skip("USER_LDT disabled");
316 1.1 maxv }
317 1.1 maxv
318 1.1 maxv memset(&act, 0, sizeof(act));
319 1.4 maxv act.sa_sigaction = user_ldt__gp_handler;
320 1.1 maxv act.sa_flags = SA_SIGINFO;
321 1.1 maxv ATF_REQUIRE_EQ(sigaction(SIGSEGV, &act, NULL), 0);
322 1.1 maxv
323 1.1 maxv build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW, SEL_UPL, 1, 0);
324 1.1 maxv ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), 256);
325 1.1 maxv
326 1.1 maxv set_fs(LSEL(256, SEL_UPL));
327 1.1 maxv
328 1.1 maxv ldt_base[666] = 123;
329 1.1 maxv ldt_base[PAGE_SIZE-1] = 213;
330 1.1 maxv __insn_barrier();
331 1.1 maxv ATF_REQUIRE_EQ(get_fs_byte((char *)666), 123);
332 1.1 maxv ATF_REQUIRE_EQ(get_fs_byte((char *)PAGE_SIZE-1), 213);
333 1.1 maxv
334 1.1 maxv /* This one should fault, and it concludes our test case. */
335 1.1 maxv expect_crash = true;
336 1.1 maxv get_fs_byte((char *)PAGE_SIZE);
337 1.1 maxv
338 1.1 maxv atf_tc_fail("test did not fault as expected");
339 1.1 maxv }
340 1.1 maxv
341 1.1 maxv /* -------------------------------------------------------------------------- */
342 1.1 maxv
343 1.1 maxv ATF_TP_ADD_TCS(tp)
344 1.1 maxv {
345 1.1 maxv user_ldt_detect();
346 1.1 maxv init_ldt_base();
347 1.1 maxv
348 1.1 maxv ATF_TP_ADD_TC(tp, filter_ops);
349 1.4 maxv ATF_TP_ADD_TC(tp, iretq_gp);
350 1.4 maxv ATF_TP_ADD_TC(tp, iretq_np);
351 1.1 maxv ATF_TP_ADD_TC(tp, user_ldt);
352 1.1 maxv
353 1.1 maxv return atf_no_error();
354 1.1 maxv }
355