t_user_ldt.c revision 1.1 1 1.1 maxv /* $NetBSD: t_user_ldt.c,v 1.1 2020/04/19 13:22:58 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.1 maxv #include <atf-c.h>
46 1.1 maxv
47 1.1 maxv static uint8_t *ldt_base;
48 1.1 maxv static bool user_ldt_supported;
49 1.1 maxv
50 1.1 maxv static void
51 1.1 maxv user_ldt_detect(void)
52 1.1 maxv {
53 1.1 maxv union descriptor desc;
54 1.1 maxv int ret;
55 1.1 maxv
56 1.1 maxv ret = i386_get_ldt(0, &desc, 1);
57 1.1 maxv user_ldt_supported = (ret != -1) || (errno != ENOTSUP);
58 1.1 maxv }
59 1.1 maxv
60 1.1 maxv static void
61 1.1 maxv init_ldt_base(void)
62 1.1 maxv {
63 1.1 maxv ldt_base = (uint8_t *)mmap(NULL, 2 * PAGE_SIZE, PROT_READ | PROT_WRITE,
64 1.1 maxv MAP_PRIVATE | MAP_ANON, -1, 0);
65 1.1 maxv if (ldt_base == MAP_FAILED)
66 1.1 maxv atf_tc_fail("mmap failed");
67 1.1 maxv munmap(ldt_base + PAGE_SIZE, PAGE_SIZE);
68 1.1 maxv }
69 1.1 maxv
70 1.1 maxv static void
71 1.1 maxv build_desc(union descriptor *desc, void *basep, uint32_t limit, int type,
72 1.1 maxv int dpl, int def32, int gran)
73 1.1 maxv {
74 1.1 maxv uintptr_t base = (uintptr_t)basep;
75 1.1 maxv
76 1.1 maxv limit--;
77 1.1 maxv
78 1.1 maxv desc->sd.sd_lolimit = limit & 0x0000ffff;
79 1.1 maxv desc->sd.sd_lobase = base & 0x00ffffff;
80 1.1 maxv desc->sd.sd_type = type & 0x1F;
81 1.1 maxv desc->sd.sd_dpl = dpl & 0x3;
82 1.1 maxv desc->sd.sd_p = 1;
83 1.1 maxv desc->sd.sd_hilimit = (limit & 0x00ff0000) >> 16;
84 1.1 maxv desc->sd.sd_xx = 0;
85 1.1 maxv desc->sd.sd_def32 = def32 ? 1 : 0;
86 1.1 maxv desc->sd.sd_gran = gran ? 1 : 0;
87 1.1 maxv desc->sd.sd_hibase = (base & 0xff000000) >> 24;
88 1.1 maxv }
89 1.1 maxv
90 1.1 maxv static void
91 1.1 maxv set_fs(unsigned int val)
92 1.1 maxv {
93 1.1 maxv __asm volatile("mov %0,%%fs"::"r" ((unsigned short)val));
94 1.1 maxv }
95 1.1 maxv
96 1.1 maxv static uint8_t __noinline
97 1.1 maxv get_fs_byte(const char *addr)
98 1.1 maxv {
99 1.1 maxv uint8_t val;
100 1.1 maxv __asm volatile (
101 1.1 maxv ".globl fs_read_begin; fs_read_begin:\n"
102 1.1 maxv "movb %%fs:%1,%0\n"
103 1.1 maxv ".globl fs_read_end; fs_read_end:\n"
104 1.1 maxv : "=q" (val) : "m" (*addr)
105 1.1 maxv );
106 1.1 maxv return val;
107 1.1 maxv }
108 1.1 maxv
109 1.1 maxv /* -------------------------------------------------------------------------- */
110 1.1 maxv
111 1.1 maxv ATF_TC(filter_ops);
112 1.1 maxv ATF_TC_HEAD(filter_ops, tc)
113 1.1 maxv {
114 1.1 maxv atf_tc_set_md_var(tc, "descr",
115 1.1 maxv "Ensure that the kernel correctly filters the descriptors");
116 1.1 maxv }
117 1.1 maxv ATF_TC_BODY(filter_ops, tc)
118 1.1 maxv {
119 1.1 maxv union descriptor desc;
120 1.1 maxv const int forbidden_types[] = {
121 1.1 maxv SDT_SYS286TSS,
122 1.1 maxv SDT_SYSLDT,
123 1.1 maxv SDT_SYS286BSY,
124 1.1 maxv SDT_SYS286CGT,
125 1.1 maxv SDT_SYSTASKGT,
126 1.1 maxv SDT_SYS286IGT,
127 1.1 maxv SDT_SYS286TGT,
128 1.1 maxv SDT_SYSNULL2,
129 1.1 maxv SDT_SYS386TSS,
130 1.1 maxv SDT_SYSNULL3,
131 1.1 maxv SDT_SYS386BSY,
132 1.1 maxv SDT_SYS386CGT,
133 1.1 maxv SDT_SYSNULL4,
134 1.1 maxv SDT_SYS386IGT,
135 1.1 maxv SDT_SYS386TGT
136 1.1 maxv };
137 1.1 maxv size_t i;
138 1.1 maxv
139 1.1 maxv if (!user_ldt_supported) {
140 1.1 maxv atf_tc_skip("USER_LDT disabled");
141 1.1 maxv }
142 1.1 maxv
143 1.1 maxv /* The first LDT slots should not be settable. */
144 1.1 maxv for (i = 0; i < 10; i++) {
145 1.1 maxv build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW,
146 1.1 maxv SEL_UPL, 1, 0);
147 1.1 maxv ATF_REQUIRE_EQ(i386_set_ldt(i, &desc, 1), -1);
148 1.1 maxv ATF_REQUIRE_EQ(errno, EINVAL);
149 1.1 maxv }
150 1.1 maxv
151 1.1 maxv /* SEL_KPL should not be allowed. */
152 1.1 maxv build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW, SEL_KPL, 1, 0);
153 1.1 maxv ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), -1);
154 1.1 maxv ATF_REQUIRE_EQ(errno, EACCES);
155 1.1 maxv
156 1.1 maxv /* Long-mode segments should not be allowed. */
157 1.1 maxv build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW, SEL_UPL, 1, 0);
158 1.1 maxv desc.sd.sd_xx = 0b11; /* sd_avl | sd_long */
159 1.1 maxv ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), -1);
160 1.1 maxv ATF_REQUIRE_EQ(errno, EACCES);
161 1.1 maxv
162 1.1 maxv /* No forbidden type should be allowed. */
163 1.1 maxv for (i = 0; i < __arraycount(forbidden_types); i++) {
164 1.1 maxv build_desc(&desc, ldt_base, PAGE_SIZE, forbidden_types[i],
165 1.1 maxv SEL_UPL, 1, 0);
166 1.1 maxv ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), -1);
167 1.1 maxv ATF_REQUIRE_EQ(errno, EACCES);
168 1.1 maxv }
169 1.1 maxv }
170 1.1 maxv
171 1.1 maxv /* -------------------------------------------------------------------------- */
172 1.1 maxv
173 1.1 maxv static volatile bool expect_crash;
174 1.1 maxv
175 1.1 maxv static void
176 1.1 maxv gp_handler(int signo, siginfo_t *sig, void *ctx)
177 1.1 maxv {
178 1.1 maxv ucontext_t *uctx = ctx;
179 1.1 maxv extern uint8_t fs_read_begin;
180 1.1 maxv
181 1.1 maxv if (!expect_crash) {
182 1.1 maxv atf_tc_fail("unexpected #GP");
183 1.1 maxv }
184 1.1 maxv
185 1.1 maxv ATF_REQUIRE(sig->si_signo == SIGSEGV);
186 1.1 maxv ATF_REQUIRE(sig->si_code == SEGV_ACCERR);
187 1.1 maxv
188 1.1 maxv if (uctx->uc_mcontext.__gregs[_REG_EIP] != (intptr_t)&fs_read_begin) {
189 1.1 maxv atf_tc_fail("got #GP on the wrong instruction");
190 1.1 maxv }
191 1.1 maxv
192 1.1 maxv set_fs(GSEL(GUDATA_SEL, SEL_UPL));
193 1.1 maxv
194 1.1 maxv atf_tc_pass();
195 1.1 maxv /* NOTREACHED */
196 1.1 maxv }
197 1.1 maxv
198 1.1 maxv ATF_TC(user_ldt);
199 1.1 maxv ATF_TC_HEAD(user_ldt, tc)
200 1.1 maxv {
201 1.1 maxv atf_tc_set_md_var(tc, "descr",
202 1.1 maxv "Ensure that USER_LDT works as expected");
203 1.1 maxv }
204 1.1 maxv ATF_TC_BODY(user_ldt, tc)
205 1.1 maxv {
206 1.1 maxv union descriptor desc;
207 1.1 maxv struct sigaction act;
208 1.1 maxv
209 1.1 maxv if (!user_ldt_supported) {
210 1.1 maxv atf_tc_skip("USER_LDT disabled");
211 1.1 maxv }
212 1.1 maxv
213 1.1 maxv memset(&act, 0, sizeof(act));
214 1.1 maxv act.sa_sigaction = gp_handler;
215 1.1 maxv act.sa_flags = SA_SIGINFO;
216 1.1 maxv ATF_REQUIRE_EQ(sigaction(SIGSEGV, &act, NULL), 0);
217 1.1 maxv
218 1.1 maxv build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW, SEL_UPL, 1, 0);
219 1.1 maxv ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), 256);
220 1.1 maxv
221 1.1 maxv set_fs(LSEL(256, SEL_UPL));
222 1.1 maxv
223 1.1 maxv ldt_base[666] = 123;
224 1.1 maxv ldt_base[PAGE_SIZE-1] = 213;
225 1.1 maxv __insn_barrier();
226 1.1 maxv ATF_REQUIRE_EQ(get_fs_byte((char *)666), 123);
227 1.1 maxv ATF_REQUIRE_EQ(get_fs_byte((char *)PAGE_SIZE-1), 213);
228 1.1 maxv
229 1.1 maxv /* This one should fault, and it concludes our test case. */
230 1.1 maxv expect_crash = true;
231 1.1 maxv get_fs_byte((char *)PAGE_SIZE);
232 1.1 maxv
233 1.1 maxv atf_tc_fail("test did not fault as expected");
234 1.1 maxv }
235 1.1 maxv
236 1.1 maxv /* -------------------------------------------------------------------------- */
237 1.1 maxv
238 1.1 maxv ATF_TP_ADD_TCS(tp)
239 1.1 maxv {
240 1.1 maxv user_ldt_detect();
241 1.1 maxv init_ldt_base();
242 1.1 maxv
243 1.1 maxv ATF_TP_ADD_TC(tp, filter_ops);
244 1.1 maxv ATF_TP_ADD_TC(tp, user_ldt);
245 1.1 maxv
246 1.1 maxv return atf_no_error();
247 1.1 maxv }
248