1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * Test case to check that kill() fails when passed a pid owned by another user.
6*49cdfc7eSAndroid Build Coastguard Worker *
7*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
8*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * 26/02/2008 Renaud Lottiaux ([email protected])
11*49cdfc7eSAndroid Build Coastguard Worker * - Fix wrong return value check on shmat system call (leading to
12*49cdfc7eSAndroid Build Coastguard Worker * segfault in case of error with this syscall).
13*49cdfc7eSAndroid Build Coastguard Worker * - Fix deletion of IPC memory segment. Segment was not correctly
14*49cdfc7eSAndroid Build Coastguard Worker * deleted due to the change of uid during the test.
15*49cdfc7eSAndroid Build Coastguard Worker *
16*49cdfc7eSAndroid Build Coastguard Worker * RESTRICTIONS
17*49cdfc7eSAndroid Build Coastguard Worker * This test must be run as root.
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "libnewipc.h"
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_sysv_ipc.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_uid.h"
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker static uid_t test_users[2];
30*49cdfc7eSAndroid Build Coastguard Worker static int *flag;
31*49cdfc7eSAndroid Build Coastguard Worker static int shm_id = -1;
32*49cdfc7eSAndroid Build Coastguard Worker static key_t shm_key;
33*49cdfc7eSAndroid Build Coastguard Worker
wait_for_flag(int value)34*49cdfc7eSAndroid Build Coastguard Worker static void wait_for_flag(int value)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker while (1) {
37*49cdfc7eSAndroid Build Coastguard Worker if (*flag == value)
38*49cdfc7eSAndroid Build Coastguard Worker break;
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker usleep(100);
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker
do_master_child(void)44*49cdfc7eSAndroid Build Coastguard Worker static void do_master_child(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker pid_t pid1;
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker *flag = 0;
49*49cdfc7eSAndroid Build Coastguard Worker pid1 = SAFE_FORK();
50*49cdfc7eSAndroid Build Coastguard Worker if (pid1 == 0) {
51*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETREUID(test_users[0], test_users[0]);
52*49cdfc7eSAndroid Build Coastguard Worker *flag = 1;
53*49cdfc7eSAndroid Build Coastguard Worker wait_for_flag(2);
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker exit(0);
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETREUID(test_users[1], test_users[1]);
59*49cdfc7eSAndroid Build Coastguard Worker wait_for_flag(1);
60*49cdfc7eSAndroid Build Coastguard Worker TEST(kill(pid1, SIGKILL));
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker *flag = 2;
63*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAITPID(pid1, NULL, 0);
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == 0)
66*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TFAIL, "kill succeeded unexpectedly");
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == EPERM)
69*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "kill failed with EPERM");
70*49cdfc7eSAndroid Build Coastguard Worker else
71*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "kill failed expected EPERM, but got");
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker
verify_kill(void)74*49cdfc7eSAndroid Build Coastguard Worker static void verify_kill(void)
75*49cdfc7eSAndroid Build Coastguard Worker {
76*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
79*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
80*49cdfc7eSAndroid Build Coastguard Worker do_master_child();
81*49cdfc7eSAndroid Build Coastguard Worker exit(0);
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children();
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker
setup(void)87*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
88*49cdfc7eSAndroid Build Coastguard Worker {
89*49cdfc7eSAndroid Build Coastguard Worker shm_key = GETIPCKEY();
90*49cdfc7eSAndroid Build Coastguard Worker shm_id = SAFE_SHMGET(shm_key, getpagesize(), 0666 | IPC_CREAT);
91*49cdfc7eSAndroid Build Coastguard Worker flag = SAFE_SHMAT(shm_id, 0, 0);
92*49cdfc7eSAndroid Build Coastguard Worker tst_get_uids(test_users, 0, 2);
93*49cdfc7eSAndroid Build Coastguard Worker }
94*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)95*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
96*49cdfc7eSAndroid Build Coastguard Worker {
97*49cdfc7eSAndroid Build Coastguard Worker if (shm_id != -1)
98*49cdfc7eSAndroid Build Coastguard Worker SAFE_SHMCTL(shm_id, IPC_RMID, NULL);
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
102*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
103*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
104*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_kill,
105*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
106*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
107*49cdfc7eSAndroid Build Coastguard Worker };
108