diff options
author | bluhm <> | 2019-11-19 19:57:04 +0000 |
---|---|---|
committer | bluhm <> | 2019-11-19 19:57:04 +0000 |
commit | 9185f840eda265016178aeb0dcdba964f8f6f3e2 (patch) | |
tree | da100b3712514c566fe948116f7926ad7f725401 /src/regress/lib/libc/sys/t_ptrace.c | |
parent | 6a6fe688152b422f3d65c970dad56e7d9d28b1ee (diff) | |
download | openbsd-9185f840eda265016178aeb0dcdba964f8f6f3e2.tar.gz openbsd-9185f840eda265016178aeb0dcdba964f8f6f3e2.tar.bz2 openbsd-9185f840eda265016178aeb0dcdba964f8f6f3e2.zip |
Import NetBSD system call regression tests. They were written with
ATF (Automated Testing Framework), so we use a small wrapper to map
it to our bsd.regress.mk framework. Only half of the 80 NetBSD
tests have been taken, the others need more work to adapt. Of them
34 syscall tests pass.
Moritz Buhl ported the tests to OpenBSD.
Diffstat (limited to 'src/regress/lib/libc/sys/t_ptrace.c')
-rw-r--r-- | src/regress/lib/libc/sys/t_ptrace.c | 231 |
1 files changed, 231 insertions, 0 deletions
diff --git a/src/regress/lib/libc/sys/t_ptrace.c b/src/regress/lib/libc/sys/t_ptrace.c new file mode 100644 index 0000000000..756c9c514e --- /dev/null +++ b/src/regress/lib/libc/sys/t_ptrace.c | |||
@@ -0,0 +1,231 @@ | |||
1 | /* $OpenBSD: t_ptrace.c,v 1.1.1.1 2019/11/19 19:57:04 bluhm Exp $ */ | ||
2 | /* $NetBSD: t_ptrace.c,v 1.4 2018/05/14 12:44:40 kamil Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c) 2016 The NetBSD Foundation, Inc. | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | ||
18 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | ||
21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
27 | * POSSIBILITY OF SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #include "macros.h" | ||
31 | |||
32 | #include <sys/cdefs.h> | ||
33 | __RCSID("$NetBSD: t_ptrace.c,v 1.4 2018/05/14 12:44:40 kamil Exp $"); | ||
34 | |||
35 | #include <sys/param.h> | ||
36 | #include <sys/types.h> | ||
37 | #include <sys/ptrace.h> | ||
38 | #include <sys/stat.h> | ||
39 | #include <sys/sysctl.h> | ||
40 | #include <err.h> | ||
41 | #include <errno.h> | ||
42 | #include <unistd.h> | ||
43 | |||
44 | #include "atf-c.h" | ||
45 | |||
46 | #include "h_macros.h" | ||
47 | |||
48 | /* | ||
49 | * A child process cannot call atf functions and expect them to magically | ||
50 | * work like in the parent. | ||
51 | * The printf(3) messaging from a child will not work out of the box as well | ||
52 | * without estabilishing a communication protocol with its parent. To not | ||
53 | * overcomplicate the tests - do not log from a child and use err(3)/errx(3) | ||
54 | * wrapped with FORKEE_ASSERT()/FORKEE_ASSERTX() as that is guaranteed to work. | ||
55 | */ | ||
56 | #define FORKEE_ASSERTX(x) \ | ||
57 | do { \ | ||
58 | int ret = (x); \ | ||
59 | if (!ret) \ | ||
60 | errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s", \ | ||
61 | __FILE__, __LINE__, __func__, #x); \ | ||
62 | } while (0) | ||
63 | |||
64 | #define FORKEE_ASSERT(x) \ | ||
65 | do { \ | ||
66 | int ret = (x); \ | ||
67 | if (!ret) \ | ||
68 | err(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s", \ | ||
69 | __FILE__, __LINE__, __func__, #x); \ | ||
70 | } while (0) | ||
71 | |||
72 | ATF_TC(attach_pid0); | ||
73 | ATF_TC_HEAD(attach_pid0, tc) | ||
74 | { | ||
75 | atf_tc_set_md_var(tc, "descr", | ||
76 | "Assert that a debugger cannot attach to PID 0"); | ||
77 | } | ||
78 | |||
79 | ATF_TC_BODY(attach_pid0, tc) | ||
80 | { | ||
81 | errno = 0; | ||
82 | ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 0, NULL, 0) == -1); | ||
83 | } | ||
84 | |||
85 | ATF_TC(attach_pid1); | ||
86 | ATF_TC_HEAD(attach_pid1, tc) | ||
87 | { | ||
88 | atf_tc_set_md_var(tc, "descr", | ||
89 | "Assert that a debugger cannot attach to PID 1 (as non-root)"); | ||
90 | |||
91 | atf_tc_set_md_var(tc, "require.user", "unprivileged"); | ||
92 | } | ||
93 | |||
94 | ATF_TC_BODY(attach_pid1, tc) | ||
95 | { | ||
96 | ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 1, NULL, 0) == -1); | ||
97 | } | ||
98 | |||
99 | ATF_TC(attach_pid1_securelevel); | ||
100 | ATF_TC_HEAD(attach_pid1_securelevel, tc) | ||
101 | { | ||
102 | atf_tc_set_md_var(tc, "descr", | ||
103 | "Assert that a debugger cannot attach to PID 1 with " | ||
104 | "securelevel >= 0 (as root)"); | ||
105 | |||
106 | atf_tc_set_md_var(tc, "require.user", "root"); | ||
107 | } | ||
108 | |||
109 | ATF_TC_BODY(attach_pid1_securelevel, tc) | ||
110 | { | ||
111 | int level; | ||
112 | size_t len = sizeof(level); | ||
113 | |||
114 | ATF_REQUIRE(sysctlbyname("kern.securelevel", &level, &len, NULL, 0) | ||
115 | != -1); | ||
116 | |||
117 | if (level < 0) { | ||
118 | atf_tc_skip("Test must be run with securelevel >= 0"); | ||
119 | } | ||
120 | |||
121 | ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 1, NULL, 0) == -1); | ||
122 | } | ||
123 | |||
124 | ATF_TC(attach_self); | ||
125 | ATF_TC_HEAD(attach_self, tc) | ||
126 | { | ||
127 | atf_tc_set_md_var(tc, "descr", | ||
128 | "Assert that a debugger cannot attach to self (as it's nonsense)"); | ||
129 | } | ||
130 | |||
131 | ATF_TC_BODY(attach_self, tc) | ||
132 | { | ||
133 | ATF_REQUIRE_ERRNO(EINVAL, ptrace(PT_ATTACH, getpid(), NULL, 0) == -1); | ||
134 | } | ||
135 | |||
136 | ATF_TC(attach_chroot); | ||
137 | ATF_TC_HEAD(attach_chroot, tc) | ||
138 | { | ||
139 | atf_tc_set_md_var(tc, "descr", | ||
140 | "Assert that a debugger cannot trace another process unless the " | ||
141 | "process's root directory is at or below the tracing process's " | ||
142 | "root"); | ||
143 | |||
144 | atf_tc_set_md_var(tc, "require.user", "root"); | ||
145 | } | ||
146 | |||
147 | ATF_TC_BODY(attach_chroot, tc) | ||
148 | { | ||
149 | char buf[PATH_MAX]; | ||
150 | pid_t child; | ||
151 | int fds_toparent[2], fds_fromparent[2]; | ||
152 | int rv; | ||
153 | uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */ | ||
154 | |||
155 | (void)memset(buf, '\0', sizeof(buf)); | ||
156 | ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL); | ||
157 | (void)strlcat(buf, "/dir", sizeof(buf)); | ||
158 | |||
159 | ATF_REQUIRE(mkdir(buf, 0500) == 0); | ||
160 | ATF_REQUIRE(chdir(buf) == 0); | ||
161 | |||
162 | ATF_REQUIRE(pipe(fds_toparent) == 0); | ||
163 | ATF_REQUIRE(pipe(fds_fromparent) == 0); | ||
164 | child = atf_utils_fork(); | ||
165 | if (child == 0) { | ||
166 | FORKEE_ASSERT(close(fds_toparent[0]) == 0); | ||
167 | FORKEE_ASSERT(close(fds_fromparent[1]) == 0); | ||
168 | |||
169 | FORKEE_ASSERT(chroot(buf) == 0); | ||
170 | |||
171 | rv = write(fds_toparent[1], &msg, sizeof(msg)); | ||
172 | FORKEE_ASSERTX(rv == sizeof(msg)); | ||
173 | |||
174 | ATF_REQUIRE_ERRNO(EPERM, | ||
175 | ptrace(PT_ATTACH, getppid(), NULL, 0) == -1); | ||
176 | |||
177 | rv = read(fds_fromparent[0], &msg, sizeof(msg)); | ||
178 | FORKEE_ASSERTX(rv == sizeof(msg)); | ||
179 | |||
180 | _exit(0); | ||
181 | } | ||
182 | ATF_REQUIRE(close(fds_toparent[1]) == 0); | ||
183 | ATF_REQUIRE(close(fds_fromparent[0]) == 0); | ||
184 | |||
185 | printf("Waiting for chrooting of the child PID %d", child); | ||
186 | rv = read(fds_toparent[0], &msg, sizeof(msg)); | ||
187 | ATF_REQUIRE(rv == sizeof(msg)); | ||
188 | |||
189 | printf("Child is ready, it will try to PT_ATTACH to parent\n"); | ||
190 | rv = write(fds_fromparent[1], &msg, sizeof(msg)); | ||
191 | ATF_REQUIRE(rv == sizeof(msg)); | ||
192 | |||
193 | printf("fds_fromparent is no longer needed - close it\n"); | ||
194 | ATF_REQUIRE(close(fds_fromparent[1]) == 0); | ||
195 | |||
196 | printf("fds_toparent is no longer needed - close it\n"); | ||
197 | ATF_REQUIRE(close(fds_toparent[0]) == 0); | ||
198 | } | ||
199 | |||
200 | ATF_TC(traceme_twice); | ||
201 | ATF_TC_HEAD(traceme_twice, tc) | ||
202 | { | ||
203 | atf_tc_set_md_var(tc, "descr", | ||
204 | "Assert that a process cannot mark its parent a debugger twice"); | ||
205 | } | ||
206 | |||
207 | ATF_TC_BODY(traceme_twice, tc) | ||
208 | { | ||
209 | |||
210 | printf("Mark the parent process (PID %d) a debugger of PID %d", | ||
211 | getppid(), getpid()); | ||
212 | ATF_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) == 0); | ||
213 | |||
214 | printf("Mark the parent process (PID %d) a debugger of PID %d again", | ||
215 | getppid(), getpid()); | ||
216 | ATF_REQUIRE_ERRNO(EBUSY, ptrace(PT_TRACE_ME, 0, NULL, 0) == -1); | ||
217 | } | ||
218 | |||
219 | ATF_TP_ADD_TCS(tp) | ||
220 | { | ||
221 | setvbuf(stdout, NULL, _IONBF, 0); | ||
222 | setvbuf(stderr, NULL, _IONBF, 0); | ||
223 | ATF_TP_ADD_TC(tp, attach_pid0); | ||
224 | ATF_TP_ADD_TC(tp, attach_pid1); | ||
225 | ATF_TP_ADD_TC(tp, attach_pid1_securelevel); | ||
226 | ATF_TP_ADD_TC(tp, attach_self); | ||
227 | ATF_TP_ADD_TC(tp, attach_chroot); | ||
228 | ATF_TP_ADD_TC(tp, traceme_twice); | ||
229 | |||
230 | return atf_no_error(); | ||
231 | } | ||