kill05.c revision 84f181fdd8092dd722c76283d82585b5d404b6a6
1865695bbc89088b9526ea9045410e5afb70a985cplars/*
2865695bbc89088b9526ea9045410e5afb70a985cplars *
3865695bbc89088b9526ea9045410e5afb70a985cplars *   Copyright (c) International Business Machines  Corp., 2001
4865695bbc89088b9526ea9045410e5afb70a985cplars *
5865695bbc89088b9526ea9045410e5afb70a985cplars *   This program is free software;  you can redistribute it and/or modify
6865695bbc89088b9526ea9045410e5afb70a985cplars *   it under the terms of the GNU General Public License as published by
7865695bbc89088b9526ea9045410e5afb70a985cplars *   the Free Software Foundation; either version 2 of the License, or
8865695bbc89088b9526ea9045410e5afb70a985cplars *   (at your option) any later version.
9865695bbc89088b9526ea9045410e5afb70a985cplars *
10865695bbc89088b9526ea9045410e5afb70a985cplars *   This program is distributed in the hope that it will be useful,
11865695bbc89088b9526ea9045410e5afb70a985cplars *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12865695bbc89088b9526ea9045410e5afb70a985cplars *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13865695bbc89088b9526ea9045410e5afb70a985cplars *   the GNU General Public License for more details.
14865695bbc89088b9526ea9045410e5afb70a985cplars *
15865695bbc89088b9526ea9045410e5afb70a985cplars *   You should have received a copy of the GNU General Public License
16865695bbc89088b9526ea9045410e5afb70a985cplars *   along with this program;  if not, write to the Free Software
17865695bbc89088b9526ea9045410e5afb70a985cplars *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18865695bbc89088b9526ea9045410e5afb70a985cplars */
19865695bbc89088b9526ea9045410e5afb70a985cplars
20865695bbc89088b9526ea9045410e5afb70a985cplars/*
21865695bbc89088b9526ea9045410e5afb70a985cplars * NAME
22865695bbc89088b9526ea9045410e5afb70a985cplars *	kill05.c
23865695bbc89088b9526ea9045410e5afb70a985cplars *
24865695bbc89088b9526ea9045410e5afb70a985cplars * DESCRIPTION
25865695bbc89088b9526ea9045410e5afb70a985cplars *	Test case to check that kill() fails when passed a pid owned by another
26865695bbc89088b9526ea9045410e5afb70a985cplars *	user.
27865695bbc89088b9526ea9045410e5afb70a985cplars *
28865695bbc89088b9526ea9045410e5afb70a985cplars * ALGORITHM
29865695bbc89088b9526ea9045410e5afb70a985cplars *	call setup
30865695bbc89088b9526ea9045410e5afb70a985cplars *	loop if the -i option was given
31865695bbc89088b9526ea9045410e5afb70a985cplars *	setup a shared memory segment to for a flag which will notify
32865695bbc89088b9526ea9045410e5afb70a985cplars *	ltpuser1's process that life is not worth living in a continuous loop.
33865695bbc89088b9526ea9045410e5afb70a985cplars *	fork a child and set the euid to ltpuser1
34865695bbc89088b9526ea9045410e5afb70a985cplars *	set the parents euid to ltpuser2
35865695bbc89088b9526ea9045410e5afb70a985cplars *	execute the kill system call on ltpuser1's pid
36865695bbc89088b9526ea9045410e5afb70a985cplars *	check the return value
37865695bbc89088b9526ea9045410e5afb70a985cplars *	if return value is not -1
38865695bbc89088b9526ea9045410e5afb70a985cplars *		issue a FAIL message, break remaining tests and cleanup
39865695bbc89088b9526ea9045410e5afb70a985cplars *      if we are doing functional testing
40865695bbc89088b9526ea9045410e5afb70a985cplars *              if the errno was set to 1 (Operation not permitted)
41865695bbc89088b9526ea9045410e5afb70a985cplars *                      issue a PASS message
42865695bbc89088b9526ea9045410e5afb70a985cplars *              otherwise
43865695bbc89088b9526ea9045410e5afb70a985cplars *                      issue a FAIL message
44865695bbc89088b9526ea9045410e5afb70a985cplars *	call cleanup
45865695bbc89088b9526ea9045410e5afb70a985cplars *
46865695bbc89088b9526ea9045410e5afb70a985cplars * USAGE
47865695bbc89088b9526ea9045410e5afb70a985cplars *  kill05 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
48865695bbc89088b9526ea9045410e5afb70a985cplars *     where,  -c n : Run n copies concurrently.
49865695bbc89088b9526ea9045410e5afb70a985cplars *             -e   : Turn on errno logging.
50865695bbc89088b9526ea9045410e5afb70a985cplars *             -i n : Execute test n times.
51865695bbc89088b9526ea9045410e5afb70a985cplars *             -I x : Execute test for x seconds.
52865695bbc89088b9526ea9045410e5afb70a985cplars *             -P x : Pause for x seconds between iterations.
53865695bbc89088b9526ea9045410e5afb70a985cplars *             -t   : Turn on syscall timing.
54865695bbc89088b9526ea9045410e5afb70a985cplars *
55865695bbc89088b9526ea9045410e5afb70a985cplars * HISTORY
56865695bbc89088b9526ea9045410e5afb70a985cplars *	07/2001 Ported by Wayne Boyer
57865695bbc89088b9526ea9045410e5afb70a985cplars *
58c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak *      26/02/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
594bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *      - Fix wrong return value check on shmat system call (leading to
60c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak *        segfault in case of error with this syscall).
61c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak *      - Fix deletion of IPC memory segment. Segment was not correctly
62c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak *        deleted due to the change of uid during the test.
63c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak *
64865695bbc89088b9526ea9045410e5afb70a985cplars * RESTRICTIONS
65865695bbc89088b9526ea9045410e5afb70a985cplars *	This test must be run as root.
66865695bbc89088b9526ea9045410e5afb70a985cplars *	Looping with the -i option does not work correctly.
67865695bbc89088b9526ea9045410e5afb70a985cplars */
68865695bbc89088b9526ea9045410e5afb70a985cplars
6984f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper#include <sys/types.h>
7084f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper#include <sys/ipc.h>
7184f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper#include <sys/shm.h>
7284f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper#include <sys/wait.h>
73865695bbc89088b9526ea9045410e5afb70a985cplars#include <errno.h>
74865695bbc89088b9526ea9045410e5afb70a985cplars#include <pwd.h>
75865695bbc89088b9526ea9045410e5afb70a985cplars#include <signal.h>
7684f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper#include <string.h>
7784f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper#include <stdio.h>
7884f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper#include <stdlib.h>
79865695bbc89088b9526ea9045410e5afb70a985cplars#include <unistd.h>
8084f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper
8184f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper#include "test.h"
8284f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper#include "usctest.h"
8384f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper#include "safe_macros.h"
84865695bbc89088b9526ea9045410e5afb70a985cplars
85b8360ee9a4b3e0ae1707116f48bfdbcee9ba2dberobbiewextern void rm_shm(int);
86b8360ee9a4b3e0ae1707116f48bfdbcee9ba2dberobbiew
87865695bbc89088b9526ea9045410e5afb70a985cplarsvoid cleanup(void);
88865695bbc89088b9526ea9045410e5afb70a985cplarsvoid setup(void);
89d34d581c6a320e356a6cda923c7aa399479e812crobbiewvoid do_child(void);
90cd5a7800406e972d01edbcc82f094b15b27e4aafsubrata_modakvoid do_master_child(char **av);
91865695bbc89088b9526ea9045410e5afb70a985cplars
9256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakchar *TCID = "kill05";
93865695bbc89088b9526ea9045410e5afb70a985cplarsint TST_TOTAL = 1;
94865695bbc89088b9526ea9045410e5afb70a985cplarsint shmid1 = -1;
95104a4d9f38b7c9e4fda847f62f59f7ba2d18ccf4subrata_modakextern key_t semkey;
96865695bbc89088b9526ea9045410e5afb70a985cplarsint *flag;
97865695bbc89088b9526ea9045410e5afb70a985cplars
9856207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint exp_enos[] = { EPERM, 0 };
99865695bbc89088b9526ea9045410e5afb70a985cplars
100865695bbc89088b9526ea9045410e5afb70a985cplarsextern int getipckey();
101865695bbc89088b9526ea9045410e5afb70a985cplars
102865695bbc89088b9526ea9045410e5afb70a985cplars#define TEST_SIG SIGKILL
103865695bbc89088b9526ea9045410e5afb70a985cplars
104b8360ee9a4b3e0ae1707116f48bfdbcee9ba2dberobbiewint main(int ac, char **av)
105865695bbc89088b9526ea9045410e5afb70a985cplars{
10656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	char *msg;		/* message returned from parse_opts */
107c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	pid_t pid;
108b8360ee9a4b3e0ae1707116f48bfdbcee9ba2dberobbiew	int status;
109865695bbc89088b9526ea9045410e5afb70a985cplars
110865695bbc89088b9526ea9045410e5afb70a985cplars	/* parse standard options */
11184f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
11260fa8014af7534eaefa901200c8df4b74ce422e6Garrett Cooper		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
113d34d581c6a320e356a6cda923c7aa399479e812crobbiew#ifdef UCLINUX
114d34d581c6a320e356a6cda923c7aa399479e812crobbiew	maybe_run_child(&do_child, "");
115d34d581c6a320e356a6cda923c7aa399479e812crobbiew#endif
116d34d581c6a320e356a6cda923c7aa399479e812crobbiew
11756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	setup();		/* global setup */
118865695bbc89088b9526ea9045410e5afb70a985cplars
119c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	pid = FORK_OR_VFORK();
12084f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (pid == -1)
121c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak		tst_brkm(TBROK, cleanup, "Fork failed");
12284f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	else if (pid == 0)
123cd5a7800406e972d01edbcc82f094b15b27e4aafsubrata_modak		do_master_child(av);
124c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak
12584f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (waitpid(pid, &status, 0) == -1)
12684f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		tst_resm(TBROK|TERRNO, "waitpid failed");
12784f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
12884f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		tst_resm(TFAIL, "child exited abnormally");
129c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	cleanup();
1307d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	tst_exit();
131c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak}
132c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak
133c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak/*
134c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak * do_master_child()
135c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak */
13656207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid do_master_child(char **av)
137c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak{
138c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	pid_t pid1;
139c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	int status;
140c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak
141c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	char user1name[] = "nobody";
142c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	char user2name[] = "bin";
143c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak
144c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	struct passwd *ltpuser1, *ltpuser2;
145c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak
14684f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	ltpuser1 = SAFE_GETPWNAM(NULL, user1name);
14784f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	ltpuser2 = SAFE_GETPWNAM(NULL, user2name);
148865695bbc89088b9526ea9045410e5afb70a985cplars
149865695bbc89088b9526ea9045410e5afb70a985cplars	TEST_EXP_ENOS(exp_enos);
150865695bbc89088b9526ea9045410e5afb70a985cplars
15184f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	Tst_count = 0;
152865695bbc89088b9526ea9045410e5afb70a985cplars
15384f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	*flag = 0;
154865695bbc89088b9526ea9045410e5afb70a985cplars
15584f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	pid1 = FORK_OR_VFORK();
156865695bbc89088b9526ea9045410e5afb70a985cplars
15784f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (pid1 == -1)
15884f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		tst_brkm(TBROK|TERRNO, cleanup, "Fork failed");
159865695bbc89088b9526ea9045410e5afb70a985cplars
16084f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (pid1 == 0) {
161865695bbc89088b9526ea9045410e5afb70a985cplars
16284f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		if (setreuid(ltpuser1->pw_uid, ltpuser1->pw_uid) == -1) {
16384f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper			perror("setreuid failed (in child)");
16484f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper			exit(1);
16584f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		}
166d34d581c6a320e356a6cda923c7aa399479e812crobbiew#ifdef UCLINUX
16784f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		if (self_exec(av[0], "") < 0) {
16884f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper			perror("self_exec failed");
16984f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper			exit(1);
17084f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		}
171d34d581c6a320e356a6cda923c7aa399479e812crobbiew#else
17284f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		do_child();
173d34d581c6a320e356a6cda923c7aa399479e812crobbiew#endif
17484f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	}
17584f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (setreuid(ltpuser2->pw_uid, ltpuser2->pw_uid) == -1) {
17684f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		perror("seteuid failed");
17784f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		exit(1);
17884f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	}
179865695bbc89088b9526ea9045410e5afb70a985cplars
18084f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	TEST(kill(pid1, TEST_SIG));
181865695bbc89088b9526ea9045410e5afb70a985cplars
18284f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	/* signal the child that we're done */
18384f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	*flag = 1;
184865695bbc89088b9526ea9045410e5afb70a985cplars
18584f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (waitpid(pid1, &status, 0) == -1) {
18684f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		perror("waitpid failed");
18784f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		exit(1);
18884f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	}
189865695bbc89088b9526ea9045410e5afb70a985cplars
19084f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (TEST_RETURN != -1) {
19184f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		printf("kill succeeded unexpectedly\n");
19284f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		exit(1);
19384f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	}
194865695bbc89088b9526ea9045410e5afb70a985cplars
19584f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	/*
19684f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	 * Check to see if the errno was set to the expected
19784f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	 * value of 1 : EPERM
19884f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	 */
19984f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (TEST_ERRNO == EPERM) {
20084f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		printf("kill failed with EPERM\n");
20184f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		exit(0);
202865695bbc89088b9526ea9045410e5afb70a985cplars	}
20384f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	perror("kill failed unexpectedly");
20484f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	exit(1);
205865695bbc89088b9526ea9045410e5afb70a985cplars}
206865695bbc89088b9526ea9045410e5afb70a985cplars
20756207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid do_child()
208d34d581c6a320e356a6cda923c7aa399479e812crobbiew{
209d34d581c6a320e356a6cda923c7aa399479e812crobbiew	pid_t my_pid;
210d34d581c6a320e356a6cda923c7aa399479e812crobbiew
211d34d581c6a320e356a6cda923c7aa399479e812crobbiew	my_pid = getpid();
21256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	while (1) {
21317bef931d997ac25ca2a2121ff5b77c0b2459f4bGarrett Cooper		if (*flag == 1)
214d34d581c6a320e356a6cda923c7aa399479e812crobbiew			exit(0);
21517bef931d997ac25ca2a2121ff5b77c0b2459f4bGarrett Cooper		else
21656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			sleep(1);
217d34d581c6a320e356a6cda923c7aa399479e812crobbiew	}
218d34d581c6a320e356a6cda923c7aa399479e812crobbiew}
219865695bbc89088b9526ea9045410e5afb70a985cplars
22056207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid setup(void)
221865695bbc89088b9526ea9045410e5afb70a985cplars{
22217bef931d997ac25ca2a2121ff5b77c0b2459f4bGarrett Cooper	tst_require_root(NULL);
223865695bbc89088b9526ea9045410e5afb70a985cplars
224865695bbc89088b9526ea9045410e5afb70a985cplars	TEST_PAUSE;
225865695bbc89088b9526ea9045410e5afb70a985cplars
226c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	tst_tmpdir();
227c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak
22856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	semkey = getipckey();
229865695bbc89088b9526ea9045410e5afb70a985cplars
23084f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if ((shmid1 = shmget(semkey, (int)getpagesize(), 0666|IPC_CREAT)) == -1)
231865695bbc89088b9526ea9045410e5afb70a985cplars		tst_brkm(TBROK, cleanup, "Failed to setup shared memory");
232865695bbc89088b9526ea9045410e5afb70a985cplars
23317bef931d997ac25ca2a2121ff5b77c0b2459f4bGarrett Cooper	if ((flag = (int *)shmat(shmid1, 0, 0)) == (int *)-1)
234ae54285103a88f835a9dc79b489474b550c3355dvapier		tst_brkm(TBROK|TERRNO, cleanup,
23584f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper			 "Failed to attach shared memory:%d", shmid1);
236865695bbc89088b9526ea9045410e5afb70a985cplars}
237865695bbc89088b9526ea9045410e5afb70a985cplars
23856207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid cleanup(void)
239865695bbc89088b9526ea9045410e5afb70a985cplars{
240865695bbc89088b9526ea9045410e5afb70a985cplars	TEST_CLEANUP;
241865695bbc89088b9526ea9045410e5afb70a985cplars
242865695bbc89088b9526ea9045410e5afb70a985cplars	rm_shm(shmid1);
243865695bbc89088b9526ea9045410e5afb70a985cplars
24484f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	tst_rmdir();
24517bef931d997ac25ca2a2121ff5b77c0b2459f4bGarrett Cooper}
246