/*
* Copyright (c) 2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Maxime Villard.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
static void
build_desc(union descriptor *desc, void *basep, uint32_t limit, int type,
int dpl, int def32, int gran)
{
uintptr_t base = (uintptr_t)basep;
ATF_TC(filter_ops);
ATF_TC_HEAD(filter_ops, tc)
{
atf_tc_set_md_var(tc, "descr",
"Ensure that the kernel correctly filters the descriptors");
}
ATF_TC_BODY(filter_ops, tc)
{
union descriptor desc;
const int forbidden_types[] = {
SDT_SYS286TSS,
SDT_SYSLDT,
SDT_SYS286BSY,
SDT_SYS286CGT,
SDT_SYSTASKGT,
SDT_SYS286IGT,
SDT_SYS286TGT,
SDT_SYSNULL2,
SDT_SYS386TSS,
SDT_SYSNULL3,
SDT_SYS386BSY,
SDT_SYS386CGT,
SDT_SYSNULL4,
SDT_SYS386IGT,
SDT_SYS386TGT
};
size_t i;
if (!user_ldt_supported) {
atf_tc_skip("USER_LDT disabled");
}
/* The first LDT slots should not be settable. */
for (i = 0; i < 10; i++) {
build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW,
SEL_UPL, 1, 0);
ATF_REQUIRE_EQ(i386_set_ldt(i, &desc, 1), -1);
ATF_REQUIRE_EQ(errno, EINVAL);
}
/* SEL_KPL should not be allowed. */
build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW, SEL_KPL, 1, 0);
ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), -1);
ATF_REQUIRE_EQ(errno, EACCES);
/* Long-mode segments should not be allowed. */
build_desc(&desc, ldt_base, PAGE_SIZE, SDT_MEMRW, SEL_UPL, 1, 0);
desc.sd.sd_xx = 0b11; /* sd_avl | sd_long */
ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), -1);
ATF_REQUIRE_EQ(errno, EACCES);
/* No forbidden type should be allowed. */
for (i = 0; i < __arraycount(forbidden_types); i++) {
build_desc(&desc, ldt_base, PAGE_SIZE, forbidden_types[i],
SEL_UPL, 1, 0);
ATF_REQUIRE_EQ(i386_set_ldt(256, &desc, 1), -1);
ATF_REQUIRE_EQ(errno, EACCES);
}