plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 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_modak | c185725 | 2008-02-27 14:55:13 +0000 | [diff] [blame] | 58 | * 26/02/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 59 | * - Fix wrong return value check on shmat system call (leading to |
subrata_modak | c185725 | 2008-02-27 14:55:13 +0000 | [diff] [blame] | 60 | * 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 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | * RESTRICTIONS |
| 65 | * This test must be run as root. |
| 66 | * Looping with the -i option does not work correctly. |
| 67 | */ |
| 68 | |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 69 | #include <sys/types.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | #include <sys/ipc.h> |
| 71 | #include <sys/shm.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | #include <sys/wait.h> |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 73 | #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 Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 82 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
robbiew | b8360ee | 2003-03-26 22:04:58 +0000 | [diff] [blame] | 84 | extern void rm_shm(int); |
| 85 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | void cleanup(void); |
| 87 | void setup(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 88 | void do_child(void); |
subrata_modak | cd5a780 | 2009-02-26 11:36:24 +0000 | [diff] [blame] | 89 | void do_master_child(char **av); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | char *TCID = "kill05"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | int TST_TOTAL = 1; |
| 93 | int shmid1 = -1; |
subrata_modak | 104a4d9 | 2008-03-24 08:29:39 +0000 | [diff] [blame] | 94 | extern key_t semkey; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | int *flag; |
| 96 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | extern int getipckey(); |
| 98 | |
| 99 | #define TEST_SIG SIGKILL |
| 100 | |
robbiew | b8360ee | 2003-03-26 22:04:58 +0000 | [diff] [blame] | 101 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | { |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 103 | const char *msg; |
subrata_modak | c185725 | 2008-02-27 14:55:13 +0000 | [diff] [blame] | 104 | pid_t pid; |
robbiew | b8360ee | 2003-03-26 22:04:58 +0000 | [diff] [blame] | 105 | int status; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 107 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 108 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 109 | #ifdef UCLINUX |
| 110 | maybe_run_child(&do_child, ""); |
| 111 | #endif |
| 112 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
subrata_modak | c185725 | 2008-02-27 14:55:13 +0000 | [diff] [blame] | 115 | pid = FORK_OR_VFORK(); |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 116 | if (pid == -1) |
subrata_modak | c185725 | 2008-02-27 14:55:13 +0000 | [diff] [blame] | 117 | tst_brkm(TBROK, cleanup, "Fork failed"); |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 118 | else if (pid == 0) |
subrata_modak | cd5a780 | 2009-02-26 11:36:24 +0000 | [diff] [blame] | 119 | do_master_child(av); |
subrata_modak | c185725 | 2008-02-27 14:55:13 +0000 | [diff] [blame] | 120 | |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 121 | if (waitpid(pid, &status, 0) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 122 | tst_resm(TBROK | TERRNO, "waitpid failed"); |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 123 | else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) |
| 124 | tst_resm(TFAIL, "child exited abnormally"); |
Francesco Rundo | 832fa38 | 2011-07-14 15:26:23 +0200 | [diff] [blame] | 125 | else |
| 126 | tst_resm(TPASS, "received expected errno(EPERM)"); |
subrata_modak | c185725 | 2008-02-27 14:55:13 +0000 | [diff] [blame] | 127 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 128 | tst_exit(); |
subrata_modak | c185725 | 2008-02-27 14:55:13 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Garrett Cooper | e935790 | 2011-05-04 09:57:34 -0700 | [diff] [blame] | 131 | void wait_for_flag(int value) |
| 132 | { |
| 133 | while (1) { |
| 134 | if (*flag == value) |
| 135 | break; |
| 136 | else |
| 137 | sleep(1); |
| 138 | } |
| 139 | } |
| 140 | |
subrata_modak | c185725 | 2008-02-27 14:55:13 +0000 | [diff] [blame] | 141 | /* |
| 142 | * do_master_child() |
| 143 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | void do_master_child(char **av) |
subrata_modak | c185725 | 2008-02-27 14:55:13 +0000 | [diff] [blame] | 145 | { |
subrata_modak | c185725 | 2008-02-27 14:55:13 +0000 | [diff] [blame] | 146 | pid_t pid1; |
| 147 | int status; |
| 148 | |
| 149 | char user1name[] = "nobody"; |
| 150 | char user2name[] = "bin"; |
| 151 | |
subrata_modak | c185725 | 2008-02-27 14:55:13 +0000 | [diff] [blame] | 152 | struct passwd *ltpuser1, *ltpuser2; |
| 153 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 154 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 156 | *flag = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 158 | pid1 = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 160 | if (pid1 == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 161 | tst_brkm(TBROK | TERRNO, cleanup, "Fork failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 163 | if (pid1 == 0) { |
Peng Haitao | d3f45db | 2011-03-08 13:42:02 +0800 | [diff] [blame] | 164 | ltpuser1 = SAFE_GETPWNAM(NULL, user1name); |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 165 | if (setreuid(ltpuser1->pw_uid, ltpuser1->pw_uid) == -1) { |
| 166 | perror("setreuid failed (in child)"); |
| 167 | exit(1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | } |
Garrett Cooper | e935790 | 2011-05-04 09:57:34 -0700 | [diff] [blame] | 169 | *flag = 1; |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 170 | #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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | } |
Peng Haitao | d3f45db | 2011-03-08 13:42:02 +0800 | [diff] [blame] | 179 | ltpuser2 = SAFE_GETPWNAM(NULL, user2name); |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 180 | if (setreuid(ltpuser2->pw_uid, ltpuser2->pw_uid) == -1) { |
| 181 | perror("seteuid failed"); |
| 182 | exit(1); |
| 183 | } |
| 184 | |
Garrett Cooper | e935790 | 2011-05-04 09:57:34 -0700 | [diff] [blame] | 185 | /* wait until child sets its euid */ |
| 186 | wait_for_flag(1); |
| 187 | |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 188 | TEST(kill(pid1, TEST_SIG)); |
| 189 | |
| 190 | /* signal the child that we're done */ |
Garrett Cooper | e935790 | 2011-05-04 09:57:34 -0700 | [diff] [blame] | 191 | *flag = 2; |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 192 | |
| 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); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 215 | void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 216 | { |
Garrett Cooper | e935790 | 2011-05-04 09:57:34 -0700 | [diff] [blame] | 217 | wait_for_flag(2); |
| 218 | exit(0); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 219 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 220 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 221 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 222 | { |
Garrett Cooper | 17bef93 | 2010-12-20 16:47:44 -0800 | [diff] [blame] | 223 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 224 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | TEST_PAUSE; |
| 226 | |
subrata_modak | c185725 | 2008-02-27 14:55:13 +0000 | [diff] [blame] | 227 | tst_tmpdir(); |
| 228 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 229 | semkey = getipckey(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 230 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 231 | if ((shmid1 = shmget(semkey, getpagesize(), 0666 | IPC_CREAT)) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 232 | tst_brkm(TBROK, cleanup, "Failed to setup shared memory"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 233 | |
Cyril Hrubis | d2db480 | 2014-09-24 17:08:17 +0200 | [diff] [blame] | 234 | if ((flag = shmat(shmid1, 0, 0)) == (int *)-1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 235 | tst_brkm(TBROK | TERRNO, cleanup, |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 236 | "Failed to attach shared memory:%d", shmid1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 237 | } |
| 238 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 239 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 240 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 241 | rm_shm(shmid1); |
| 242 | |
Garrett Cooper | 84f181f | 2011-01-14 00:12:03 -0800 | [diff] [blame] | 243 | tst_rmdir(); |
Garrett Cooper | 17bef93 | 2010-12-20 16:47:44 -0800 | [diff] [blame] | 244 | } |