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