blob: f6ea53a5809abcf4643091c7ba08e1ef63b0a8fa [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
22 * kill05.c
23 *
24 * DESCRIPTION
25 * Test case to check that kill() fails when passed a pid owned by another
26 * user.
27 *
28 * ALGORITHM
29 * call setup
30 * loop if the -i option was given
31 * setup a shared memory segment to for a flag which will notify
32 * ltpuser1's process that life is not worth living in a continuous loop.
33 * fork a child and set the euid to ltpuser1
34 * set the parents euid to ltpuser2
35 * execute the kill system call on ltpuser1's pid
36 * check the return value
37 * if return value is not -1
38 * issue a FAIL message, break remaining tests and cleanup
39 * if we are doing functional testing
40 * if the errno was set to 1 (Operation not permitted)
41 * issue a PASS message
42 * otherwise
43 * issue a FAIL message
44 * call cleanup
45 *
46 * USAGE
47 * kill05 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
48 * where, -c n : Run n copies concurrently.
49 * -e : Turn on errno logging.
50 * -i n : Execute test n times.
51 * -I x : Execute test for x seconds.
52 * -P x : Pause for x seconds between iterations.
53 * -t : Turn on syscall timing.
54 *
55 * HISTORY
56 * 07/2001 Ported by Wayne Boyer
57 *
subrata_modakc1857252008-02-27 14:55:13 +000058 * 26/02/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
subrata_modak4bb656a2009-02-26 12:02:09 +000059 * - Fix wrong return value check on shmat system call (leading to
subrata_modakc1857252008-02-27 14:55:13 +000060 * segfault in case of error with this syscall).
61 * - Fix deletion of IPC memory segment. Segment was not correctly
62 * deleted due to the change of uid during the test.
63 *
plars865695b2001-08-27 22:15:12 +000064 * RESTRICTIONS
65 * This test must be run as root.
66 * Looping with the -i option does not work correctly.
67 */
68
Garrett Cooper84f181f2011-01-14 00:12:03 -080069#include <sys/types.h>
plars865695b2001-08-27 22:15:12 +000070#include <sys/ipc.h>
71#include <sys/shm.h>
plars865695b2001-08-27 22:15:12 +000072#include <sys/wait.h>
Garrett Cooper84f181f2011-01-14 00:12:03 -080073#include <errno.h>
74#include <pwd.h>
75#include <signal.h>
76#include <string.h>
77#include <stdio.h>
78#include <stdlib.h>
79#include <unistd.h>
80
81#include "test.h"
Garrett Cooper84f181f2011-01-14 00:12:03 -080082#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000083
robbiewb8360ee2003-03-26 22:04:58 +000084extern void rm_shm(int);
85
plars865695b2001-08-27 22:15:12 +000086void cleanup(void);
87void setup(void);
robbiewd34d5812005-07-11 22:28:09 +000088void do_child(void);
subrata_modakcd5a7802009-02-26 11:36:24 +000089void do_master_child(char **av);
plars865695b2001-08-27 22:15:12 +000090
subrata_modak56207ce2009-03-23 13:35:39 +000091char *TCID = "kill05";
plars865695b2001-08-27 22:15:12 +000092int TST_TOTAL = 1;
93int shmid1 = -1;
subrata_modak104a4d92008-03-24 08:29:39 +000094extern key_t semkey;
plars865695b2001-08-27 22:15:12 +000095int *flag;
96
plars865695b2001-08-27 22:15:12 +000097extern int getipckey();
98
99#define TEST_SIG SIGKILL
100
robbiewb8360ee2003-03-26 22:04:58 +0000101int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000102{
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200103 const char *msg;
subrata_modakc1857252008-02-27 14:55:13 +0000104 pid_t pid;
robbiewb8360ee2003-03-26 22:04:58 +0000105 int status;
plars865695b2001-08-27 22:15:12 +0000106
Garrett Cooper84f181f2011-01-14 00:12:03 -0800107 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800108 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewd34d5812005-07-11 22:28:09 +0000109#ifdef UCLINUX
110 maybe_run_child(&do_child, "");
111#endif
112
subrata_modak56207ce2009-03-23 13:35:39 +0000113 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +0000114
subrata_modakc1857252008-02-27 14:55:13 +0000115 pid = FORK_OR_VFORK();
Garrett Cooper84f181f2011-01-14 00:12:03 -0800116 if (pid == -1)
subrata_modakc1857252008-02-27 14:55:13 +0000117 tst_brkm(TBROK, cleanup, "Fork failed");
Garrett Cooper84f181f2011-01-14 00:12:03 -0800118 else if (pid == 0)
subrata_modakcd5a7802009-02-26 11:36:24 +0000119 do_master_child(av);
subrata_modakc1857252008-02-27 14:55:13 +0000120
Garrett Cooper84f181f2011-01-14 00:12:03 -0800121 if (waitpid(pid, &status, 0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 tst_resm(TBROK | TERRNO, "waitpid failed");
Garrett Cooper84f181f2011-01-14 00:12:03 -0800123 else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
124 tst_resm(TFAIL, "child exited abnormally");
Francesco Rundo832fa382011-07-14 15:26:23 +0200125 else
126 tst_resm(TPASS, "received expected errno(EPERM)");
subrata_modakc1857252008-02-27 14:55:13 +0000127 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800128 tst_exit();
subrata_modakc1857252008-02-27 14:55:13 +0000129}
130
Garrett Coopere9357902011-05-04 09:57:34 -0700131void wait_for_flag(int value)
132{
133 while (1) {
134 if (*flag == value)
135 break;
136 else
137 sleep(1);
138 }
139}
140
subrata_modakc1857252008-02-27 14:55:13 +0000141/*
142 * do_master_child()
143 */
subrata_modak56207ce2009-03-23 13:35:39 +0000144void do_master_child(char **av)
subrata_modakc1857252008-02-27 14:55:13 +0000145{
subrata_modakc1857252008-02-27 14:55:13 +0000146 pid_t pid1;
147 int status;
148
149 char user1name[] = "nobody";
150 char user2name[] = "bin";
151
subrata_modakc1857252008-02-27 14:55:13 +0000152 struct passwd *ltpuser1, *ltpuser2;
153
Caspar Zhangd59a6592013-03-07 14:59:12 +0800154 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000155
Garrett Cooper84f181f2011-01-14 00:12:03 -0800156 *flag = 0;
plars865695b2001-08-27 22:15:12 +0000157
Garrett Cooper84f181f2011-01-14 00:12:03 -0800158 pid1 = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +0000159
Garrett Cooper84f181f2011-01-14 00:12:03 -0800160 if (pid1 == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800161 tst_brkm(TBROK | TERRNO, cleanup, "Fork failed");
plars865695b2001-08-27 22:15:12 +0000162
Garrett Cooper84f181f2011-01-14 00:12:03 -0800163 if (pid1 == 0) {
Peng Haitaod3f45db2011-03-08 13:42:02 +0800164 ltpuser1 = SAFE_GETPWNAM(NULL, user1name);
Garrett Cooper84f181f2011-01-14 00:12:03 -0800165 if (setreuid(ltpuser1->pw_uid, ltpuser1->pw_uid) == -1) {
166 perror("setreuid failed (in child)");
167 exit(1);
plars865695b2001-08-27 22:15:12 +0000168 }
Garrett Coopere9357902011-05-04 09:57:34 -0700169 *flag = 1;
Garrett Cooper84f181f2011-01-14 00:12:03 -0800170#ifdef UCLINUX
171 if (self_exec(av[0], "") < 0) {
172 perror("self_exec failed");
173 exit(1);
174 }
175#else
176 do_child();
177#endif
plars865695b2001-08-27 22:15:12 +0000178 }
Peng Haitaod3f45db2011-03-08 13:42:02 +0800179 ltpuser2 = SAFE_GETPWNAM(NULL, user2name);
Garrett Cooper84f181f2011-01-14 00:12:03 -0800180 if (setreuid(ltpuser2->pw_uid, ltpuser2->pw_uid) == -1) {
181 perror("seteuid failed");
182 exit(1);
183 }
184
Garrett Coopere9357902011-05-04 09:57:34 -0700185 /* wait until child sets its euid */
186 wait_for_flag(1);
187
Garrett Cooper84f181f2011-01-14 00:12:03 -0800188 TEST(kill(pid1, TEST_SIG));
189
190 /* signal the child that we're done */
Garrett Coopere9357902011-05-04 09:57:34 -0700191 *flag = 2;
Garrett Cooper84f181f2011-01-14 00:12:03 -0800192
193 if (waitpid(pid1, &status, 0) == -1) {
194 perror("waitpid failed");
195 exit(1);
196 }
197
198 if (TEST_RETURN != -1) {
199 printf("kill succeeded unexpectedly\n");
200 exit(1);
201 }
202
203 /*
204 * Check to see if the errno was set to the expected
205 * value of 1 : EPERM
206 */
207 if (TEST_ERRNO == EPERM) {
208 printf("kill failed with EPERM\n");
209 exit(0);
210 }
211 perror("kill failed unexpectedly");
212 exit(1);
plars865695b2001-08-27 22:15:12 +0000213}
214
Mike Frysingerc57fba52014-04-09 18:56:30 -0400215void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000216{
Garrett Coopere9357902011-05-04 09:57:34 -0700217 wait_for_flag(2);
218 exit(0);
robbiewd34d5812005-07-11 22:28:09 +0000219}
plars865695b2001-08-27 22:15:12 +0000220
subrata_modak56207ce2009-03-23 13:35:39 +0000221void setup(void)
plars865695b2001-08-27 22:15:12 +0000222{
Garrett Cooper17bef932010-12-20 16:47:44 -0800223 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000224
plars865695b2001-08-27 22:15:12 +0000225 TEST_PAUSE;
226
subrata_modakc1857252008-02-27 14:55:13 +0000227 tst_tmpdir();
228
subrata_modak56207ce2009-03-23 13:35:39 +0000229 semkey = getipckey();
plars865695b2001-08-27 22:15:12 +0000230
Wanlong Gao354ebb42012-12-07 10:10:04 +0800231 if ((shmid1 = shmget(semkey, getpagesize(), 0666 | IPC_CREAT)) == -1)
plars865695b2001-08-27 22:15:12 +0000232 tst_brkm(TBROK, cleanup, "Failed to setup shared memory");
plars865695b2001-08-27 22:15:12 +0000233
Cyril Hrubisd2db4802014-09-24 17:08:17 +0200234 if ((flag = shmat(shmid1, 0, 0)) == (int *)-1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800235 tst_brkm(TBROK | TERRNO, cleanup,
Garrett Cooper84f181f2011-01-14 00:12:03 -0800236 "Failed to attach shared memory:%d", shmid1);
plars865695b2001-08-27 22:15:12 +0000237}
238
subrata_modak56207ce2009-03-23 13:35:39 +0000239void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000240{
plars865695b2001-08-27 22:15:12 +0000241 rm_shm(shmid1);
242
Garrett Cooper84f181f2011-01-14 00:12:03 -0800243 tst_rmdir();
Garrett Cooper17bef932010-12-20 16:47:44 -0800244}