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
174548c6cf9bcdd96d8303caa4130ab638b61f8a30Wanlong Gao *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "safe_macros.h"
83865695bbc89088b9526ea9045410e5afb70a985cplars
84b8360ee9a4b3e0ae1707116f48bfdbcee9ba2dberobbiewextern void rm_shm(int);
85b8360ee9a4b3e0ae1707116f48bfdbcee9ba2dberobbiew
86865695bbc89088b9526ea9045410e5afb70a985cplarsvoid cleanup(void);
87865695bbc89088b9526ea9045410e5afb70a985cplarsvoid setup(void);
88d34d581c6a320e356a6cda923c7aa399479e812crobbiewvoid do_child(void);
89cd5a7800406e972d01edbcc82f094b15b27e4aafsubrata_modakvoid do_master_child(char **av);
90865695bbc89088b9526ea9045410e5afb70a985cplars
9156207cec7732e09c216c751c0b5f88a242bacae6subrata_modakchar *TCID = "kill05";
92865695bbc89088b9526ea9045410e5afb70a985cplarsint TST_TOTAL = 1;
93865695bbc89088b9526ea9045410e5afb70a985cplarsint shmid1 = -1;
94104a4d9f38b7c9e4fda847f62f59f7ba2d18ccf4subrata_modakextern key_t semkey;
95865695bbc89088b9526ea9045410e5afb70a985cplarsint *flag;
96865695bbc89088b9526ea9045410e5afb70a985cplars
97865695bbc89088b9526ea9045410e5afb70a985cplarsextern int getipckey();
98865695bbc89088b9526ea9045410e5afb70a985cplars
99865695bbc89088b9526ea9045410e5afb70a985cplars#define TEST_SIG SIGKILL
100865695bbc89088b9526ea9045410e5afb70a985cplars
101b8360ee9a4b3e0ae1707116f48bfdbcee9ba2dberobbiewint main(int ac, char **av)
102865695bbc89088b9526ea9045410e5afb70a985cplars{
103c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	pid_t pid;
104b8360ee9a4b3e0ae1707116f48bfdbcee9ba2dberobbiew	int status;
105865695bbc89088b9526ea9045410e5afb70a985cplars
106d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis	tst_parse_opts(ac, av, NULL, NULL);
107d34d581c6a320e356a6cda923c7aa399479e812crobbiew#ifdef UCLINUX
108d34d581c6a320e356a6cda923c7aa399479e812crobbiew	maybe_run_child(&do_child, "");
109d34d581c6a320e356a6cda923c7aa399479e812crobbiew#endif
110d34d581c6a320e356a6cda923c7aa399479e812crobbiew
11156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	setup();		/* global setup */
112865695bbc89088b9526ea9045410e5afb70a985cplars
113c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	pid = FORK_OR_VFORK();
11484f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (pid == -1)
115c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak		tst_brkm(TBROK, cleanup, "Fork failed");
11684f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	else if (pid == 0)
117cd5a7800406e972d01edbcc82f094b15b27e4aafsubrata_modak		do_master_child(av);
118c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak
11984f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (waitpid(pid, &status, 0) == -1)
120354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK | TERRNO, "waitpid failed");
12184f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
12284f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		tst_resm(TFAIL, "child exited abnormally");
123832fa385af0928ac3e33a913756b642ef01ac80eFrancesco Rundo	else
124832fa385af0928ac3e33a913756b642ef01ac80eFrancesco Rundo		tst_resm(TPASS, "received expected errno(EPERM)");
125c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	cleanup();
1267d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	tst_exit();
127c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak}
128c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak
129e9357906046223c2c09be1349e9748b07d24a39aGarrett Coopervoid wait_for_flag(int value)
130e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper{
131e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper	while (1) {
132e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper		if (*flag == value)
133e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper			break;
134e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper		else
135e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper			sleep(1);
136e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper	}
137e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper}
138e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper
139c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak/*
140c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak * do_master_child()
141c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak */
14256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid do_master_child(char **av)
143c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak{
144c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	pid_t pid1;
145c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	int status;
146c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak
147c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	char user1name[] = "nobody";
148c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	char user2name[] = "bin";
149c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak
150c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	struct passwd *ltpuser1, *ltpuser2;
151c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak
152d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang	tst_count = 0;
153865695bbc89088b9526ea9045410e5afb70a985cplars
15484f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	*flag = 0;
155865695bbc89088b9526ea9045410e5afb70a985cplars
15684f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	pid1 = FORK_OR_VFORK();
157865695bbc89088b9526ea9045410e5afb70a985cplars
15884f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (pid1 == -1)
159354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK | TERRNO, cleanup, "Fork failed");
160865695bbc89088b9526ea9045410e5afb70a985cplars
16184f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (pid1 == 0) {
162d3f45db7dce1d7f29167e68ba6a41d8c4fe49575Peng Haitao		ltpuser1 = SAFE_GETPWNAM(NULL, user1name);
16384f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		if (setreuid(ltpuser1->pw_uid, ltpuser1->pw_uid) == -1) {
16484f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper			perror("setreuid failed (in child)");
16584f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper			exit(1);
16684f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		}
167e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper		*flag = 1;
168d34d581c6a320e356a6cda923c7aa399479e812crobbiew#ifdef UCLINUX
16984f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		if (self_exec(av[0], "") < 0) {
17084f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper			perror("self_exec failed");
17184f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper			exit(1);
17284f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		}
173d34d581c6a320e356a6cda923c7aa399479e812crobbiew#else
17484f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		do_child();
175d34d581c6a320e356a6cda923c7aa399479e812crobbiew#endif
17684f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	}
177d3f45db7dce1d7f29167e68ba6a41d8c4fe49575Peng Haitao	ltpuser2 = SAFE_GETPWNAM(NULL, user2name);
17884f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (setreuid(ltpuser2->pw_uid, ltpuser2->pw_uid) == -1) {
17984f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		perror("seteuid failed");
18084f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		exit(1);
18184f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	}
182865695bbc89088b9526ea9045410e5afb70a985cplars
183e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper	/* wait until child sets its euid */
184e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper	wait_for_flag(1);
185e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper
18684f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	TEST(kill(pid1, TEST_SIG));
187865695bbc89088b9526ea9045410e5afb70a985cplars
18884f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	/* signal the child that we're done */
189e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper	*flag = 2;
190865695bbc89088b9526ea9045410e5afb70a985cplars
19184f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (waitpid(pid1, &status, 0) == -1) {
19284f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		perror("waitpid failed");
19384f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		exit(1);
19484f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	}
195865695bbc89088b9526ea9045410e5afb70a985cplars
19684f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (TEST_RETURN != -1) {
19784f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		printf("kill succeeded unexpectedly\n");
19884f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		exit(1);
19984f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	}
200865695bbc89088b9526ea9045410e5afb70a985cplars
20184f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	/*
20284f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	 * Check to see if the errno was set to the expected
20384f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	 * value of 1 : EPERM
20484f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	 */
20584f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	if (TEST_ERRNO == EPERM) {
20684f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		printf("kill failed with EPERM\n");
20784f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper		exit(0);
208865695bbc89088b9526ea9045410e5afb70a985cplars	}
20984f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	perror("kill failed unexpectedly");
21084f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	exit(1);
211865695bbc89088b9526ea9045410e5afb70a985cplars}
212865695bbc89088b9526ea9045410e5afb70a985cplars
213c57fba5535abf457e33dd7a986b6c512d95cdef6Mike Frysingervoid do_child(void)
214d34d581c6a320e356a6cda923c7aa399479e812crobbiew{
215e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper	wait_for_flag(2);
216e9357906046223c2c09be1349e9748b07d24a39aGarrett Cooper	exit(0);
217d34d581c6a320e356a6cda923c7aa399479e812crobbiew}
218865695bbc89088b9526ea9045410e5afb70a985cplars
21956207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid setup(void)
220865695bbc89088b9526ea9045410e5afb70a985cplars{
221d1e794d62b1bf619df8390535e4c2a58899b1145Cyril Hrubis	tst_require_root();
222865695bbc89088b9526ea9045410e5afb70a985cplars
223865695bbc89088b9526ea9045410e5afb70a985cplars	TEST_PAUSE;
224865695bbc89088b9526ea9045410e5afb70a985cplars
225c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak	tst_tmpdir();
226c185725bb81ffe7762e43275ad9212725157ebd3subrata_modak
22756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	semkey = getipckey();
228865695bbc89088b9526ea9045410e5afb70a985cplars
229354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if ((shmid1 = shmget(semkey, getpagesize(), 0666 | IPC_CREAT)) == -1)
230865695bbc89088b9526ea9045410e5afb70a985cplars		tst_brkm(TBROK, cleanup, "Failed to setup shared memory");
231865695bbc89088b9526ea9045410e5afb70a985cplars
232d2db48030ce485af049d78a1690afc6df99bb689Cyril Hrubis	if ((flag = shmat(shmid1, 0, 0)) == (int *)-1)
233354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK | TERRNO, cleanup,
23484f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper			 "Failed to attach shared memory:%d", shmid1);
235865695bbc89088b9526ea9045410e5afb70a985cplars}
236865695bbc89088b9526ea9045410e5afb70a985cplars
23756207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid cleanup(void)
238865695bbc89088b9526ea9045410e5afb70a985cplars{
239865695bbc89088b9526ea9045410e5afb70a985cplars	rm_shm(shmid1);
240865695bbc89088b9526ea9045410e5afb70a985cplars
24184f181fdd8092dd722c76283d82585b5d404b6a6Garrett Cooper	tst_rmdir();
24217bef931d997ac25ca2a2121ff5b77c0b2459f4bGarrett Cooper}
243