robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 13 | * with this program; if not, write the Free Software Foundation, Inc., |
| 14 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 15 | * |
| 16 | */ |
| 17 | /********************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 19 | * TEST IDENTIFIER : capset02 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | * |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 21 | * EXECUTED BY : anyone |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 23 | * TEST TITLE : Tests for error conditions. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 25 | * TEST CASE TOTAL : 4 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 27 | * AUTHOR : Saji Kumar.V.R <saji.kumar@wipro.com> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 28 | * |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 29 | * SIGNALS |
| 30 | * Uses SIGUSR1 to pause before test if option set. |
| 31 | * (See the parse_opts(3) man page). |
| 32 | * |
| 33 | * DESCRIPTION |
| 34 | * Verify that |
| 35 | * 1) capset() fails with errno set to EFAULT if an invalid address |
| 36 | * is given for header |
| 37 | * 2) capset() fails with errno set to EFAULT if an invalid address |
| 38 | * is given for data |
| 39 | * 3) capset() fails with errno set to EINVAL if an invalid value |
| 40 | * is given for header->version |
| 41 | * 4) capset() fails with errno set to EPERM the process does not |
| 42 | * have enough privilege to set capabilities |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 43 | * |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 44 | * |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 45 | * Setup: |
| 46 | * Setup signal handling. |
| 47 | * Pause for SIGUSR1 if option specified. |
| 48 | * Call capget() to save current capability data |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 49 | * |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 50 | * Test: |
| 51 | * Loop if the proper options are given. |
| 52 | * do test specific setup. |
| 53 | * call capset with proper arguments |
| 54 | * if capset() fails with expected errno |
| 55 | * Test passed |
| 56 | * Otherwise |
| 57 | * Test failed |
| 58 | * do test specific cleanup |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 59 | * |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 60 | * Cleanup: |
| 61 | * Print errno log and/or timing stats if options given |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 62 | * |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 63 | * USAGE: <for command-line> |
| 64 | * capset02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p] |
| 65 | * where, -c n : Run n copies concurrently. |
| 66 | * -e : Turn on errno logging. |
| 67 | * -h : Show help screen |
| 68 | * -f : Turn off functional testing |
| 69 | * -i n : Execute test n times. |
| 70 | * -I x : Execute test for x seconds. |
| 71 | * -p : Pause for SIGUSR1 before starting |
| 72 | * -P x : Pause for x seconds between iterations. |
| 73 | * -t : Turn on syscall timing. |
| 74 | * |
| 75 | ****************************************************************/ |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 76 | #include <sys/types.h> |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 77 | #include <sys/wait.h> |
| 78 | #include <errno.h> |
| 79 | #include <pwd.h> |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 80 | #include <signal.h> |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 81 | #include <string.h> |
| 82 | #include <unistd.h> |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 83 | #include "test.h" |
| 84 | #include "usctest.h" |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 85 | #include "linux_syscall_numbers.h" |
robbiew | e775759 | 2003-10-01 17:23:49 +0000 | [diff] [blame] | 86 | |
| 87 | /**************************************************************************/ |
robbiew | e775759 | 2003-10-01 17:23:49 +0000 | [diff] [blame] | 88 | /* */ |
| 89 | /* Some archs do not have the manpage documented sys/capability.h file, */ |
robbiew | 04a8ba5 | 2003-10-02 17:38:04 +0000 | [diff] [blame] | 90 | /* and require the use of the line below */ |
| 91 | |
| 92 | #include <linux/capability.h> |
| 93 | |
| 94 | /* If you are having issues with including this file and have the sys/ */ |
| 95 | /* version, then you may want to try switching to it. -Robbie W. */ |
robbiew | e775759 | 2003-10-01 17:23:49 +0000 | [diff] [blame] | 96 | /**************************************************************************/ |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 97 | |
| 98 | #define INVALID_VERSION 0 |
| 99 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 100 | static void setup(); |
| 101 | static void cleanup(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 102 | static void test_setup(int, char *); |
| 103 | static void child_func(); |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 104 | |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 105 | static pid_t child_pid = -1; |
| 106 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 107 | char *TCID = "capset02"; /* Test program identifier. */ |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 108 | static int exp_enos[] = { EFAULT, EINVAL, EPERM, 0 }; |
| 109 | |
| 110 | static struct __user_cap_header_struct header; |
| 111 | static struct __user_cap_data_struct data; |
| 112 | |
| 113 | struct test_case_t { |
| 114 | cap_user_header_t headerp; |
| 115 | cap_user_data_t datap; |
| 116 | int exp_errno; |
| 117 | char *errdesc; |
| 118 | } test_cases[] = { |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 119 | #ifndef UCLINUX |
| 120 | /* Skip since uClinux does not implement memory protection */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | { |
| 122 | (cap_user_header_t) - 1, &data, EFAULT, "EFAULT"}, { |
| 123 | &header, (cap_user_data_t) - 1, EFAULT, "EFAULT"}, |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 124 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | { |
| 126 | &header, &data, EINVAL, "EINVAL"}, { |
| 127 | &header, &data, EPERM, "EPERM"},}; |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 128 | |
| 129 | int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]); |
| 130 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | int main(int ac, char **av) |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 132 | { |
| 133 | |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 134 | int lc, i; |
| 135 | char *msg; |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 136 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 137 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 138 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 139 | #ifdef UCLINUX |
| 140 | maybe_run_child(&child_func, ""); |
| 141 | #endif |
| 142 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 143 | setup(); |
| 144 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 145 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 146 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 147 | Tst_count = 0; |
| 148 | |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 149 | #ifdef UCLINUX |
| 150 | i = 2; |
| 151 | #else |
| 152 | i = 0; |
| 153 | #endif |
| 154 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 155 | for (; i < TST_TOTAL; i++) { |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 156 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 157 | test_setup(i, av[0]); |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 158 | TEST(ltp_syscall(__NR_capset, test_cases[i].headerp, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 159 | test_cases[i].datap)); |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 160 | |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 161 | if (TEST_RETURN == -1 && |
| 162 | TEST_ERRNO == test_cases[i].exp_errno) { |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 163 | tst_resm(TPASS, "capset() returned -1," |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | " errno: %s", test_cases[i].errdesc); |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 165 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 166 | tst_resm(TFAIL | TTERRNO, |
| 167 | "Test Failed, capset() returned %ld", |
| 168 | TEST_RETURN); |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 169 | } |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 170 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 171 | } |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 172 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 173 | cleanup(); |
| 174 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 175 | tst_exit(); |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 176 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 177 | } |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 178 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | void setup() |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 180 | { |
| 181 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 182 | TEST_EXP_ENOS(exp_enos); |
| 183 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 184 | TEST_PAUSE; |
| 185 | |
| 186 | /* |
| 187 | * Save current capability data. |
| 188 | * header.version must be _LINUX_CAPABILITY_VERSION |
| 189 | */ |
| 190 | header.version = _LINUX_CAPABILITY_VERSION; |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 191 | if (ltp_syscall(__NR_capget, &header, &data) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 192 | tst_brkm(TBROK | TERRNO, NULL, "capget failed"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 193 | } |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 194 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 195 | void cleanup() |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 196 | { |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 197 | if (0 < child_pid) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 198 | kill(child_pid, SIGTERM); |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 199 | wait(NULL); |
| 200 | } |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 201 | TEST_CLEANUP; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 202 | } |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 203 | |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 204 | void child_func() |
| 205 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 206 | for (;;) { |
| 207 | sleep(10); |
| 208 | } |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 209 | } |
| 210 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 211 | void test_setup(int i, char *argv0) |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 212 | { |
robbiew | 9b0da82 | 2005-10-04 16:28:39 +0000 | [diff] [blame] | 213 | char nobody_uid[] = "nobody"; |
| 214 | struct passwd *ltpuser; |
| 215 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 216 | switch (i) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 217 | case 0: |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 218 | break; |
| 219 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 220 | case 1: |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 221 | header.version = _LINUX_CAPABILITY_VERSION; |
| 222 | header.pid = 0; |
| 223 | break; |
| 224 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 225 | case 2: |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 226 | header.version = INVALID_VERSION; |
| 227 | header.pid = 0; |
| 228 | break; |
| 229 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 230 | case 3: |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 231 | header.version = _LINUX_CAPABILITY_VERSION; |
| 232 | /* |
| 233 | * when a non-zero pid is specified, process should have |
| 234 | * CAP_SETPCAP capability to change capabilities. |
| 235 | * by default, CAP_SETPCAP is not enabled. So giving |
| 236 | * a non-zero pid results in capset() failing with |
| 237 | * errno EPERM |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 238 | * |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 239 | * Note: this seems to have changed with recent kernels |
| 240 | * => create a child and try to set its capabilities |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 241 | */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 242 | child_pid = FORK_OR_VFORK(); |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 243 | if (child_pid == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 244 | tst_brkm(TBROK | TERRNO, cleanup, "fork failed"); |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 245 | else if (child_pid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 246 | #ifdef UCLINUX |
| 247 | if (self_exec(argv0, "") < 0) { |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 248 | perror("self_exec failed"); |
| 249 | exit(1); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 250 | } |
| 251 | #else |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 252 | child_func(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 253 | #endif |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 254 | } else { |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 255 | header.pid = child_pid; |
robbiew | 9b0da82 | 2005-10-04 16:28:39 +0000 | [diff] [blame] | 256 | ltpuser = getpwnam(nobody_uid); |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 257 | if (ltpuser == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 258 | tst_brkm(TBROK | TERRNO, cleanup, |
| 259 | "getpwnam failed"); |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 260 | if (seteuid(ltpuser->pw_uid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 261 | tst_brkm(TBROK | TERRNO, cleanup, |
| 262 | "seteuid failed"); |
robbiew | 9b0da82 | 2005-10-04 16:28:39 +0000 | [diff] [blame] | 263 | |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 264 | } |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 265 | break; |
| 266 | |
| 267 | } |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 268 | } |