1 /* $NetBSD: t_ptrace.c,v 1.18 2017/01/13 21:30:41 christos Exp $ */
4 * Copyright (c) 2016 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: t_ptrace.c,v 1.18 2017/01/13 21:30:41 christos Exp $");
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/ptrace.h>
36 #include <sys/sysctl.h>
46 * A child process cannot call atf functions and expect them to magically
47 * work like in the parent.
48 * The printf(3) messaging from a child will not work out of the box as well
49 * without estabilishing a communication protocol with its parent. To not
50 * overcomplicate the tests - do not log from a child and use err(3)/errx(3)
51 * wrapped with FORKEE_ASSERT()/FORKEE_ASSERTX() as that is guaranteed to work.
53 #define FORKEE_ASSERTX(x) \
57 errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s", \
58 __FILE__, __LINE__, __func__, #x); \
61 #define FORKEE_ASSERT(x) \
65 err(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s", \
66 __FILE__, __LINE__, __func__, #x); \
70 ATF_TC_HEAD(attach_pid0, tc)
72 atf_tc_set_md_var(tc, "descr",
73 "Assert that a debugger cannot attach to PID 0");
76 ATF_TC_BODY(attach_pid0, tc)
79 ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 0, NULL, 0) == -1);
83 ATF_TC_HEAD(attach_pid1, tc)
85 atf_tc_set_md_var(tc, "descr",
86 "Assert that a debugger cannot attach to PID 1 (as non-root)");
88 atf_tc_set_md_var(tc, "require.user", "unprivileged");
91 ATF_TC_BODY(attach_pid1, tc)
93 ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 1, NULL, 0) == -1);
96 ATF_TC(attach_pid1_securelevel);
97 ATF_TC_HEAD(attach_pid1_securelevel, tc)
99 atf_tc_set_md_var(tc, "descr",
100 "Assert that a debugger cannot attach to PID 1 with "
101 "securelevel >= 1 (as root)");
103 atf_tc_set_md_var(tc, "require.user", "root");
106 ATF_TC_BODY(attach_pid1_securelevel, tc)
109 size_t len = sizeof(level);
111 ATF_REQUIRE(sysctlbyname("kern.securelevel", &level, &len, NULL, 0)
115 atf_tc_skip("Test must be run with securelevel >= 1");
118 ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 1, NULL, 0) == -1);
122 ATF_TC_HEAD(attach_self, tc)
124 atf_tc_set_md_var(tc, "descr",
125 "Assert that a debugger cannot attach to self (as it's nonsense)");
128 ATF_TC_BODY(attach_self, tc)
130 ATF_REQUIRE_ERRNO(EINVAL, ptrace(PT_ATTACH, getpid(), NULL, 0) == -1);
133 ATF_TC(attach_chroot);
134 ATF_TC_HEAD(attach_chroot, tc)
136 atf_tc_set_md_var(tc, "descr",
137 "Assert that a debugger cannot trace another process unless the "
138 "process's root directory is at or below the tracing process's "
141 atf_tc_set_md_var(tc, "require.user", "root");
144 ATF_TC_BODY(attach_chroot, tc)
148 int fds_toparent[2], fds_fromparent[2];
150 uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
152 (void)memset(buf, '\0', sizeof(buf));
153 ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
154 (void)strlcat(buf, "/dir", sizeof(buf));
156 ATF_REQUIRE(mkdir(buf, 0500) == 0);
157 ATF_REQUIRE(chdir(buf) == 0);
159 ATF_REQUIRE(pipe(fds_toparent) == 0);
160 ATF_REQUIRE(pipe(fds_fromparent) == 0);
161 child = atf_utils_fork();
163 FORKEE_ASSERT(close(fds_toparent[0]) == 0);
164 FORKEE_ASSERT(close(fds_fromparent[1]) == 0);
166 FORKEE_ASSERT(chroot(buf) == 0);
168 rv = write(fds_toparent[1], &msg, sizeof(msg));
169 FORKEE_ASSERTX(rv == sizeof(msg));
171 ATF_REQUIRE_ERRNO(EPERM,
172 ptrace(PT_ATTACH, getppid(), NULL, 0) == -1);
174 rv = read(fds_fromparent[0], &msg, sizeof(msg));
175 FORKEE_ASSERTX(rv == sizeof(msg));
179 ATF_REQUIRE(close(fds_toparent[1]) == 0);
180 ATF_REQUIRE(close(fds_fromparent[0]) == 0);
182 printf("Waiting for chrooting of the child PID %d", child);
183 rv = read(fds_toparent[0], &msg, sizeof(msg));
184 ATF_REQUIRE(rv == sizeof(msg));
186 printf("Child is ready, it will try to PT_ATTACH to parent\n");
187 rv = write(fds_fromparent[1], &msg, sizeof(msg));
188 ATF_REQUIRE(rv == sizeof(msg));
190 printf("fds_fromparent is no longer needed - close it\n");
191 ATF_REQUIRE(close(fds_fromparent[1]) == 0);
193 printf("fds_toparent is no longer needed - close it\n");
194 ATF_REQUIRE(close(fds_toparent[0]) == 0);
199 setvbuf(stdout, NULL, _IONBF, 0);
200 setvbuf(stderr, NULL, _IONBF, 0);
201 ATF_TP_ADD_TC(tp, attach_pid0);
202 ATF_TP_ADD_TC(tp, attach_pid1);
203 ATF_TP_ADD_TC(tp, attach_pid1_securelevel);
204 ATF_TP_ADD_TC(tp, attach_self);
205 ATF_TP_ADD_TC(tp, attach_chroot);
207 return atf_no_error();