1 1.3 christos /* $NetBSD: t_backtrace_sandbox.c,v 1.3 2025/01/30 16:13:51 christos Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2025 Kyle Evans <kevans (at) FreeBSD.org> 5 1.1 christos * 6 1.1 christos * SPDX-License-Identifier: BSD-2-Clause 7 1.1 christos */ 8 1.1 christos #include <sys/cdefs.h> 9 1.3 christos __RCSID("$NetBSD: t_backtrace_sandbox.c,v 1.3 2025/01/30 16:13:51 christos Exp $"); 10 1.1 christos 11 1.1 christos #include <sys/param.h> 12 1.3 christos #include <sys/wait.h> 13 1.1 christos #ifdef __FreeBSD__ 14 1.1 christos #include <sys/capsicum.h> 15 1.1 christos #define __arraycount(a) nitems(a) 16 1.1 christos #endif 17 1.1 christos 18 1.1 christos #include <execinfo.h> 19 1.1 christos #include <signal.h> 20 1.1 christos #include <stdio.h> 21 1.1 christos #include <string.h> 22 1.3 christos #include <stdlib.h> 23 1.1 christos #include <unistd.h> 24 1.1 christos 25 1.1 christos #include <atf-c.h> 26 1.1 christos 27 1.1 christos #define BT_FUNCTIONS 10 28 1.1 christos 29 1.2 riastrad ATF_TC(backtrace_sandbox); 30 1.1 christos ATF_TC_HEAD(backtrace_sandbox, tc) 31 1.1 christos { 32 1.1 christos atf_tc_set_md_var(tc, "descr", 33 1.1 christos "Test backtrace_sandbox_init(3) in sandbox"); 34 1.1 christos #ifndef __FreeBSD__ 35 1.1 christos atf_tc_set_md_var(tc, "require.user", "root"); 36 1.1 christos #endif 37 1.1 christos } 38 1.1 christos 39 1.1 christos ATF_TC_BODY(backtrace_sandbox, tc) 40 1.1 christos { 41 1.1 christos void *addr[BT_FUNCTIONS]; 42 1.1 christos char **syms; 43 1.1 christos size_t frames; 44 1.3 christos pid_t pid; 45 1.3 christos int status; 46 1.1 christos 47 1.1 christos frames = backtrace(addr, __arraycount(addr)); 48 1.1 christos ATF_REQUIRE(frames > 0); 49 1.1 christos 50 1.1 christos syms = backtrace_symbols_fmt(addr, frames, "%n"); 51 1.1 christos ATF_REQUIRE(strcmp(syms[0], "atfu_backtrace_sandbox_body") == 0); 52 1.1 christos 53 1.3 christos pid = fork(); 54 1.3 christos ATF_REQUIRE(pid >= 0); 55 1.3 christos 56 1.3 christos if (pid == 0) { 57 1.3 christos 58 1.3 christos backtrace_sandbox_init(); 59 1.1 christos #ifdef __FreeBSD__ 60 1.3 christos cap_enter(); 61 1.1 christos #else 62 1.3 christos if (chroot("/tmp") != 0) 63 1.3 christos _exit(EXIT_FAILURE); 64 1.1 christos #endif 65 1.1 christos 66 1.3 christos syms = backtrace_symbols_fmt(addr, frames, "%n"); 67 1.3 christos if (strcmp(syms[0], "atfu_backtrace_sandbox_body") != 0) 68 1.3 christos _exit(EXIT_FAILURE); 69 1.3 christos 70 1.3 christos backtrace_sandbox_fini(); 71 1.3 christos 72 1.3 christos _exit(EXIT_SUCCESS); 73 1.3 christos } 74 1.3 christos 75 1.3 christos (void)wait(&status); 76 1.3 christos 77 1.3 christos if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS) 78 1.3 christos atf_tc_fail("resolving symbols in chroot failed"); 79 1.3 christos 80 1.1 christos } 81 1.1 christos 82 1.1 christos ATF_TP_ADD_TCS(tp) 83 1.1 christos { 84 1.1 christos 85 1.1 christos ATF_TP_ADD_TC(tp, backtrace_sandbox); 86 1.1 christos 87 1.1 christos return atf_no_error(); 88 1.1 christos } 89