1 /* $NetBSD: t_backtrace_sandbox.c,v 1.2 2025/01/27 17:02:50 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2025 Kyle Evans <kevans@FreeBSD.org> 5 * 6 * SPDX-License-Identifier: BSD-2-Clause 7 */ 8 #include <sys/cdefs.h> 9 __RCSID("$NetBSD: t_backtrace_sandbox.c,v 1.2 2025/01/27 17:02:50 riastradh Exp $"); 10 11 #include <sys/param.h> 12 #ifdef __FreeBSD__ 13 #include <sys/capsicum.h> 14 #define __arraycount(a) nitems(a) 15 #endif 16 17 #include <execinfo.h> 18 #include <signal.h> 19 #include <stdio.h> 20 #include <string.h> 21 #include <unistd.h> 22 23 #include <atf-c.h> 24 25 #define BT_FUNCTIONS 10 26 27 ATF_TC(backtrace_sandbox); 28 ATF_TC_HEAD(backtrace_sandbox, tc) 29 { 30 atf_tc_set_md_var(tc, "descr", 31 "Test backtrace_sandbox_init(3) in sandbox"); 32 #ifndef __FreeBSD__ 33 atf_tc_set_md_var(tc, "require.user", "root"); 34 #endif 35 } 36 37 ATF_TC_BODY(backtrace_sandbox, tc) 38 { 39 void *addr[BT_FUNCTIONS]; 40 char **syms; 41 size_t frames; 42 43 frames = backtrace(addr, __arraycount(addr)); 44 ATF_REQUIRE(frames > 0); 45 46 syms = backtrace_symbols_fmt(addr, frames, "%n"); 47 ATF_REQUIRE(strcmp(syms[0], "atfu_backtrace_sandbox_body") == 0); 48 49 backtrace_sandbox_init(); 50 #ifdef __FreeBSD__ 51 cap_enter(); 52 #else 53 ATF_REQUIRE(chroot("/tmp") == 0); 54 #endif 55 56 syms = backtrace_symbols_fmt(addr, frames, "%n"); 57 ATF_REQUIRE(strcmp(syms[0], "atfu_backtrace_sandbox_body") == 0); 58 } 59 60 ATF_TP_ADD_TCS(tp) 61 { 62 63 ATF_TP_ADD_TC(tp, backtrace_sandbox); 64 65 return atf_no_error(); 66 } 67