1 /* $NetBSD: t_kill.c,v 1.2 2024/07/15 06:19:07 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_kill.c,v 1.2 2024/07/15 06:19:07 riastradh Exp $");
33
34 #include <sys/wait.h>
35
36 #include <errno.h>
37 #include <limits.h>
38 #include <pwd.h>
39 #include <signal.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42
43 #include <atf-c.h>
44
45 ATF_TC(kill_basic);
ATF_TC_HEAD(kill_basic,tc)46 ATF_TC_HEAD(kill_basic, tc)
47 {
48 atf_tc_set_md_var(tc, "descr", "Test that kill(2) works");
49 }
50
ATF_TC_BODY(kill_basic,tc)51 ATF_TC_BODY(kill_basic, tc)
52 {
53 const int sig[] = { SIGHUP, SIGINT, SIGKILL, SIGTERM };
54 pid_t pid;
55 size_t i;
56 int sta;
57
58 for (i = 0; i < __arraycount(sig); i++) {
59
60 pid = fork();
61 ATF_REQUIRE(pid >= 0);
62
63 switch (pid) {
64
65 case 0:
66 pause();
67 break;
68
69 default:
70 ATF_REQUIRE(kill(pid, sig[i]) == 0);
71 }
72
73 (void)wait(&sta);
74
75 if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != sig[i])
76 atf_tc_fail("kill(2) failed to kill child");
77 }
78 }
79
80 ATF_TC(kill_err);
ATF_TC_HEAD(kill_err,tc)81 ATF_TC_HEAD(kill_err, tc)
82 {
83 atf_tc_set_md_var(tc, "descr", "Test error conditions of kill(2)");
84 }
85
ATF_TC_BODY(kill_err,tc)86 ATF_TC_BODY(kill_err, tc)
87 {
88 int rv, sta;
89 pid_t pid;
90
91 pid = fork();
92 ATF_REQUIRE(pid >= 0);
93
94 if (pid == 0) {
95
96 errno = 0;
97 rv = kill(getpid(), -1);
98
99 if (rv == 0 || errno != EINVAL)
100 _exit(EINVAL);
101
102 errno = 0;
103 rv = kill(INT_MAX, SIGUSR1);
104
105 if (rv == 0 || errno != ESRCH)
106 _exit(ESRCH);
107
108 _exit(EXIT_SUCCESS);
109 }
110
111 (void)wait(&sta);
112
113 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
114
115 if (WEXITSTATUS(sta) == EINVAL)
116 atf_tc_fail("expected EINVAL, but kill(2) succeeded");
117
118 if (WEXITSTATUS(sta) == ESRCH)
119 atf_tc_fail("expected ESRCH, but kill(2) succeeded");
120
121 atf_tc_fail("unknown error from kill(2)");
122 }
123 }
124
125 ATF_TC(kill_perm);
ATF_TC_HEAD(kill_perm,tc)126 ATF_TC_HEAD(kill_perm, tc)
127 {
128 atf_tc_set_md_var(tc, "descr", "Test kill(2) permissions");
129 atf_tc_set_md_var(tc, "require.user", "root");
130 }
131
ATF_TC_BODY(kill_perm,tc)132 ATF_TC_BODY(kill_perm, tc)
133 {
134 struct passwd *pw;
135 pid_t cpid, ppid;
136 uid_t cuid = 0;
137 uid_t puid = 0;
138 int sta;
139
140 /*
141 * Test that kill(2) fails when called
142 * for a PID owned by another user.
143 */
144 pw = getpwnam("operator");
145
146 if (pw != NULL)
147 cuid = pw->pw_uid;
148
149 pw = getpwnam("nobody");
150
151 if (pw != NULL)
152 puid = pw->pw_uid;
153
154 if (cuid == 0 || puid == 0 || cuid == puid)
155 atf_tc_fail("getpwnam(3) failed");
156
157 ppid = fork();
158
159 if (ppid < 0)
160 _exit(EXIT_FAILURE);
161
162 if (ppid == 0) {
163
164 cpid = fork();
165
166 if (cpid < 0)
167 _exit(EXIT_FAILURE);
168
169 if (cpid == 0) {
170
171 if (setuid(cuid) < 0)
172 _exit(EXIT_FAILURE);
173 else {
174 (void)sleep(1);
175 }
176
177 _exit(EXIT_SUCCESS);
178 }
179
180 /*
181 * Try to kill the child after having
182 * set the real and effective UID.
183 */
184 if (setuid(puid) != 0)
185 _exit(EXIT_FAILURE);
186
187 errno = 0;
188
189 if (kill(cpid, SIGKILL) == 0)
190 _exit(EPERM);
191
192 if (errno != EPERM)
193 _exit(EPERM);
194
195 (void)waitpid(cpid, &sta, 0);
196
197 _exit(EXIT_SUCCESS);
198 }
199
200 (void)waitpid(ppid, &sta, 0);
201
202 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) == EPERM)
203 atf_tc_fail("killed a process of another user");
204
205 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
206 atf_tc_fail("unknown error from kill(2)");
207 }
208
209 ATF_TC(kill_pgrp_neg);
ATF_TC_HEAD(kill_pgrp_neg,tc)210 ATF_TC_HEAD(kill_pgrp_neg, tc)
211 {
212 atf_tc_set_md_var(tc, "descr", "Test kill(2) with process group, #2");
213 }
214
ATF_TC_BODY(kill_pgrp_neg,tc)215 ATF_TC_BODY(kill_pgrp_neg, tc)
216 {
217 const int maxiter = 3;
218 pid_t cpid, ppid;
219 int i, sta;
220
221 ppid = fork();
222 ATF_REQUIRE(ppid >= 0);
223
224 if (ppid == 0) {
225
226 ATF_REQUIRE(setpgid(0, 0) == 0);
227
228 for (i = 0; i < maxiter; i++) {
229
230 cpid = fork();
231 ATF_REQUIRE(cpid >= 0);
232
233 if (cpid == 0)
234 pause();
235 }
236
237 /*
238 * Test the variant of killpg(3); if the process number
239 * is negative but not -1, the signal should be sent to
240 * all processes whose process group ID is equal to the
241 * absolute value of the process number.
242 */
243 ATF_REQUIRE(kill(-getpgrp(), SIGKILL) == 0);
244
245 (void)sleep(1);
246
247 _exit(EXIT_SUCCESS);
248 }
249
250 (void)waitpid(ppid, &sta, 0);
251
252 if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGKILL)
253 atf_tc_fail("failed to kill(2) a process group");
254 }
255
256 ATF_TC(kill_pgrp_zero);
ATF_TC_HEAD(kill_pgrp_zero,tc)257 ATF_TC_HEAD(kill_pgrp_zero, tc)
258 {
259 atf_tc_set_md_var(tc, "descr", "Test kill(2) with process group, #1");
260 }
261
ATF_TC_BODY(kill_pgrp_zero,tc)262 ATF_TC_BODY(kill_pgrp_zero, tc)
263 {
264 const int maxiter = 3;
265 pid_t cpid, ppid;
266 int i, sta;
267
268 ppid = fork();
269 ATF_REQUIRE(ppid >= 0);
270
271 if (ppid == 0) {
272
273 ATF_REQUIRE(setpgid(0, 0) == 0);
274
275 for (i = 0; i < maxiter; i++) {
276
277 cpid = fork();
278 ATF_REQUIRE(cpid >= 0);
279
280 if (cpid == 0)
281 pause();
282 }
283
284 /*
285 * If the supplied process number is zero,
286 * the signal should be sent to all processes
287 * under the current process group.
288 */
289 ATF_REQUIRE(kill(0, SIGKILL) == 0);
290
291 (void)sleep(1);
292
293 _exit(EXIT_SUCCESS);
294 }
295
296 (void)waitpid(ppid, &sta, 0);
297
298 if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGKILL)
299 atf_tc_fail("failed to kill(2) a process group");
300 }
301
302 ATF_TC(kill_int_min);
ATF_TC_HEAD(kill_int_min,tc)303 ATF_TC_HEAD(kill_int_min, tc)
304 {
305 atf_tc_set_md_var(tc, "descr", "Test kill(INT_MIN) fails with ESRCH");
306 }
307
ATF_TC_BODY(kill_int_min,tc)308 ATF_TC_BODY(kill_int_min, tc)
309 {
310
311 ATF_CHECK_ERRNO(ESRCH, kill(INT_MIN, 0));
312 }
313
ATF_TP_ADD_TCS(tp)314 ATF_TP_ADD_TCS(tp)
315 {
316
317 ATF_TP_ADD_TC(tp, kill_basic);
318 ATF_TP_ADD_TC(tp, kill_err);
319 ATF_TP_ADD_TC(tp, kill_perm);
320 ATF_TP_ADD_TC(tp, kill_pgrp_neg);
321 ATF_TP_ADD_TC(tp, kill_pgrp_zero);
322 ATF_TP_ADD_TC(tp, kill_int_min);
323
324 return atf_no_error();
325 }
326