Home | History | Annotate | Line # | Download | only in libi386
t_user_ldt.c revision 1.2
      1  1.2  maxv /*	$NetBSD: t_user_ldt.c,v 1.2 2020/04/26 12:13:10 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.1  maxv 	user_ldt_supported = (ret != -1) || (errno != ENOTSUP);
     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.1  maxv set_fs(unsigned int val)
     96  1.1  maxv {
     97  1.1  maxv 	__asm volatile("mov %0,%%fs"::"r" ((unsigned short)val));
     98  1.1  maxv }
     99  1.1  maxv 
    100  1.1  maxv static uint8_t __noinline
    101  1.1  maxv get_fs_byte(const char *addr)
    102  1.1  maxv {
    103  1.1  maxv 	uint8_t val;
    104  1.1  maxv 	__asm volatile (
    105  1.1  maxv 		".globl fs_read_begin; fs_read_begin:\n"
    106  1.1  maxv 		"movb %%fs:%1,%0\n"
    107  1.1  maxv 		".globl fs_read_end; fs_read_end:\n"
    108  1.1  maxv 		: "=q" (val) : "m" (*addr)
    109  1.1  maxv 	);
    110  1.1  maxv 	return val;
    111  1.1  maxv }
    112  1.1  maxv 
    113  1.1  maxv /* -------------------------------------------------------------------------- */
    114  1.1  maxv 
    115  1.1  maxv ATF_TC(filter_ops);
    116  1.1  maxv ATF_TC_HEAD(filter_ops, tc)
    117  1.1  maxv {
    118  1.1  maxv 	atf_tc_set_md_var(tc, "descr",
    119  1.1  maxv 	    "Ensure that the kernel correctly filters the descriptors");
    120  1.1  maxv }
    121  1.1  maxv ATF_TC_BODY(filter_ops, tc)
    122  1.1  maxv {
    123  1.1  maxv 	union descriptor desc;
    124  1.1  maxv 	const int forbidden_types[] = {
    125  1.1  maxv 		SDT_SYS286TSS,
    126  1.1  maxv 		SDT_SYSLDT,
    127  1.1  maxv 		SDT_SYS286BSY,
    128  1.1  maxv 		SDT_SYS286CGT,
    129  1.1  maxv 		SDT_SYSTASKGT,
    130  1.1  maxv 		SDT_SYS286IGT,
    131  1.1  maxv 		SDT_SYS286TGT,
    132  1.1  maxv 		SDT_SYSNULL2,
    133  1.1  maxv 		SDT_SYS386TSS,
    134  1.1  maxv 		SDT_SYSNULL3,
    135  1.1  maxv 		SDT_SYS386BSY,
    136  1.1  maxv 		SDT_SYS386CGT,
    137  1.1  maxv 		SDT_SYSNULL4,
    138  1.1  maxv 		SDT_SYS386IGT,
    139  1.1  maxv 		SDT_SYS386TGT
    140  1.1  maxv 	};
    141  1.1  maxv 	size_t i;
    142  1.1  maxv 
    143  1.1  maxv 	if (!user_ldt_supported) {
    144  1.1  maxv 		atf_tc_skip("USER_LDT disabled");
    145  1.1  maxv 	}
    146  1.1  maxv 
    147  1.1  maxv 	/* The first LDT slots should not be settable. */
    148  1.1  maxv 	for (i = 0; i < 10; i++) {
    149  1.1  maxv 		build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW,
    150  1.1  maxv 		    SEL_UPL, 1, 0);
    151  1.1  maxv 		ATF_REQUIRE_EQ(i386_set_ldt(i, &desc, 1), -1);
    152  1.1  maxv 		ATF_REQUIRE_EQ(errno, EINVAL);
    153  1.1  maxv 	}
    154  1.1  maxv 
    155  1.1  maxv 	/* SEL_KPL should not be allowed. */
    156  1.1  maxv 	build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW, SEL_KPL, 1, 0);
    157  1.1  maxv 	ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), -1);
    158  1.1  maxv 	ATF_REQUIRE_EQ(errno, EACCES);
    159  1.1  maxv 
    160  1.1  maxv 	/* Long-mode segments should not be allowed. */
    161  1.1  maxv 	build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW, SEL_UPL, 1, 0);
    162  1.1  maxv 	desc.sd.sd_xx = 0b11; /* sd_avl | sd_long */
    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 	/* No forbidden type should be allowed. */
    167  1.1  maxv 	for (i = 0; i < __arraycount(forbidden_types); i++) {
    168  1.1  maxv 		build_desc(&desc, ldt_base, PAGE_SIZE, forbidden_types[i],
    169  1.1  maxv 		    SEL_UPL, 1, 0);
    170  1.1  maxv 		ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), -1);
    171  1.1  maxv 		ATF_REQUIRE_EQ(errno, EACCES);
    172  1.1  maxv 	}
    173  1.2  maxv 
    174  1.2  maxv 	/* Check the slot limit. */
    175  1.2  maxv 	build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW, SEL_UPL, 1, 0);
    176  1.2  maxv 	ATF_REQUIRE_EQ(i386_set_ldt(MAX_USERLDT_SLOTS-1, &desc, 1),
    177  1.2  maxv 	    MAX_USERLDT_SLOTS-1);
    178  1.2  maxv 	ATF_REQUIRE_EQ(i386_set_ldt(MAX_USERLDT_SLOTS, &desc, 1), -1);
    179  1.2  maxv 	ATF_REQUIRE_EQ(errno, EINVAL);
    180  1.1  maxv }
    181  1.1  maxv 
    182  1.1  maxv /* -------------------------------------------------------------------------- */
    183  1.1  maxv 
    184  1.1  maxv static volatile bool expect_crash;
    185  1.1  maxv 
    186  1.1  maxv static void
    187  1.1  maxv gp_handler(int signo, siginfo_t *sig, void *ctx)
    188  1.1  maxv {
    189  1.1  maxv 	ucontext_t *uctx = ctx;
    190  1.1  maxv 	extern uint8_t fs_read_begin;
    191  1.1  maxv 
    192  1.1  maxv 	if (!expect_crash) {
    193  1.1  maxv 		atf_tc_fail("unexpected #GP");
    194  1.1  maxv 	}
    195  1.1  maxv 
    196  1.1  maxv 	ATF_REQUIRE(sig->si_signo == SIGSEGV);
    197  1.1  maxv 	ATF_REQUIRE(sig->si_code == SEGV_ACCERR);
    198  1.1  maxv 
    199  1.1  maxv 	if (uctx->uc_mcontext.__gregs[_REG_EIP] != (intptr_t)&fs_read_begin) {
    200  1.1  maxv 		atf_tc_fail("got #GP on the wrong instruction");
    201  1.1  maxv 	}
    202  1.1  maxv 
    203  1.1  maxv 	set_fs(GSEL(GUDATA_SEL, SEL_UPL));
    204  1.1  maxv 
    205  1.1  maxv 	atf_tc_pass();
    206  1.1  maxv 	/* NOTREACHED */
    207  1.1  maxv }
    208  1.1  maxv 
    209  1.1  maxv ATF_TC(user_ldt);
    210  1.1  maxv ATF_TC_HEAD(user_ldt, tc)
    211  1.1  maxv {
    212  1.1  maxv 	atf_tc_set_md_var(tc, "descr",
    213  1.1  maxv 	    "Ensure that USER_LDT works as expected");
    214  1.1  maxv }
    215  1.1  maxv ATF_TC_BODY(user_ldt, tc)
    216  1.1  maxv {
    217  1.1  maxv 	union descriptor desc;
    218  1.1  maxv 	struct sigaction act;
    219  1.1  maxv 
    220  1.1  maxv 	if (!user_ldt_supported) {
    221  1.1  maxv 		atf_tc_skip("USER_LDT disabled");
    222  1.1  maxv 	}
    223  1.1  maxv 
    224  1.1  maxv 	memset(&act, 0, sizeof(act));
    225  1.1  maxv 	act.sa_sigaction = gp_handler;
    226  1.1  maxv 	act.sa_flags = SA_SIGINFO;
    227  1.1  maxv 	ATF_REQUIRE_EQ(sigaction(SIGSEGV, &act, NULL), 0);
    228  1.1  maxv 
    229  1.1  maxv 	build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW, SEL_UPL, 1, 0);
    230  1.1  maxv 	ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), 256);
    231  1.1  maxv 
    232  1.1  maxv 	set_fs(LSEL(256, SEL_UPL));
    233  1.1  maxv 
    234  1.1  maxv 	ldt_base[666] = 123;
    235  1.1  maxv 	ldt_base[PAGE_SIZE-1] = 213;
    236  1.1  maxv 	__insn_barrier();
    237  1.1  maxv 	ATF_REQUIRE_EQ(get_fs_byte((char *)666), 123);
    238  1.1  maxv 	ATF_REQUIRE_EQ(get_fs_byte((char *)PAGE_SIZE-1), 213);
    239  1.1  maxv 
    240  1.1  maxv 	/* This one should fault, and it concludes our test case. */
    241  1.1  maxv 	expect_crash = true;
    242  1.1  maxv 	get_fs_byte((char *)PAGE_SIZE);
    243  1.1  maxv 
    244  1.1  maxv 	atf_tc_fail("test did not fault as expected");
    245  1.1  maxv }
    246  1.1  maxv 
    247  1.1  maxv /* -------------------------------------------------------------------------- */
    248  1.1  maxv 
    249  1.1  maxv ATF_TP_ADD_TCS(tp)
    250  1.1  maxv {
    251  1.1  maxv 	user_ldt_detect();
    252  1.1  maxv 	init_ldt_base();
    253  1.1  maxv 
    254  1.1  maxv 	ATF_TP_ADD_TC(tp, filter_ops);
    255  1.1  maxv 	ATF_TP_ADD_TC(tp, user_ldt);
    256  1.1  maxv 
    257  1.1  maxv 	return atf_no_error();
    258  1.1  maxv }
    259