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" |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 84 | #include "linux_syscall_numbers.h" |
robbiew | e775759 | 2003-10-01 17:23:49 +0000 | [diff] [blame] | 85 | |
| 86 | /**************************************************************************/ |
robbiew | e775759 | 2003-10-01 17:23:49 +0000 | [diff] [blame] | 87 | /* */ |
| 88 | /* Some archs do not have the manpage documented sys/capability.h file, */ |
robbiew | 04a8ba5 | 2003-10-02 17:38:04 +0000 | [diff] [blame] | 89 | /* and require the use of the line below */ |
| 90 | |
| 91 | #include <linux/capability.h> |
| 92 | |
| 93 | /* If you are having issues with including this file and have the sys/ */ |
| 94 | /* version, then you may want to try switching to it. -Robbie W. */ |
robbiew | e775759 | 2003-10-01 17:23:49 +0000 | [diff] [blame] | 95 | /**************************************************************************/ |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 96 | |
| 97 | #define INVALID_VERSION 0 |
| 98 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 99 | static void setup(void); |
| 100 | static void cleanup(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 101 | static void test_setup(int, char *); |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 102 | static void child_func(void); |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 103 | |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 104 | static pid_t child_pid = -1; |
| 105 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 106 | char *TCID = "capset02"; |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 107 | |
| 108 | static struct __user_cap_header_struct header; |
| 109 | static struct __user_cap_data_struct data; |
| 110 | |
| 111 | struct test_case_t { |
| 112 | cap_user_header_t headerp; |
| 113 | cap_user_data_t datap; |
| 114 | int exp_errno; |
| 115 | char *errdesc; |
| 116 | } test_cases[] = { |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 117 | #ifndef UCLINUX |
| 118 | /* Skip since uClinux does not implement memory protection */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | { |
| 120 | (cap_user_header_t) - 1, &data, EFAULT, "EFAULT"}, { |
| 121 | &header, (cap_user_data_t) - 1, EFAULT, "EFAULT"}, |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 122 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 123 | { |
| 124 | &header, &data, EINVAL, "EINVAL"}, { |
| 125 | &header, &data, EPERM, "EPERM"},}; |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 126 | |
| 127 | int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]); |
| 128 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | int main(int ac, char **av) |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 130 | { |
| 131 | |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 132 | int lc, i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 133 | const char *msg; |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 134 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 135 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 136 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 137 | #ifdef UCLINUX |
| 138 | maybe_run_child(&child_func, ""); |
| 139 | #endif |
| 140 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 141 | setup(); |
| 142 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 143 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 144 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 145 | tst_count = 0; |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 146 | |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 147 | #ifdef UCLINUX |
| 148 | i = 2; |
| 149 | #else |
| 150 | i = 0; |
| 151 | #endif |
| 152 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 153 | for (; i < TST_TOTAL; i++) { |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 154 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 155 | test_setup(i, av[0]); |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 156 | TEST(ltp_syscall(__NR_capset, test_cases[i].headerp, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 157 | test_cases[i].datap)); |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 158 | |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 159 | if (TEST_RETURN == -1 && |
| 160 | TEST_ERRNO == test_cases[i].exp_errno) { |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 161 | tst_resm(TPASS, "capset() returned -1," |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | " errno: %s", test_cases[i].errdesc); |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 163 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | tst_resm(TFAIL | TTERRNO, |
| 165 | "Test Failed, capset() returned %ld", |
| 166 | TEST_RETURN); |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 167 | } |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 168 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 169 | } |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 170 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 171 | cleanup(); |
| 172 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 173 | tst_exit(); |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 174 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 175 | } |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 176 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 177 | void setup(void) |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 178 | { |
| 179 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 180 | TEST_PAUSE; |
| 181 | |
| 182 | /* |
| 183 | * Save current capability data. |
| 184 | * header.version must be _LINUX_CAPABILITY_VERSION |
| 185 | */ |
| 186 | header.version = _LINUX_CAPABILITY_VERSION; |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 187 | if (ltp_syscall(__NR_capget, &header, &data) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 188 | tst_brkm(TBROK | TERRNO, NULL, "capget failed"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 189 | } |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 190 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 191 | void cleanup(void) |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 192 | { |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 193 | if (0 < child_pid) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 194 | kill(child_pid, SIGTERM); |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 195 | wait(NULL); |
| 196 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 197 | } |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 198 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 199 | void child_func(void) |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 200 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 201 | for (;;) { |
| 202 | sleep(10); |
| 203 | } |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 204 | } |
| 205 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 206 | void test_setup(int i, char *argv0) |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 207 | { |
robbiew | 9b0da82 | 2005-10-04 16:28:39 +0000 | [diff] [blame] | 208 | char nobody_uid[] = "nobody"; |
| 209 | struct passwd *ltpuser; |
| 210 | |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 211 | switch (i) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 212 | case 0: |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 213 | break; |
| 214 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 215 | case 1: |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 216 | header.version = _LINUX_CAPABILITY_VERSION; |
| 217 | header.pid = 0; |
| 218 | break; |
| 219 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 220 | case 2: |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 221 | header.version = INVALID_VERSION; |
| 222 | header.pid = 0; |
| 223 | break; |
| 224 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 225 | case 3: |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 226 | header.version = _LINUX_CAPABILITY_VERSION; |
| 227 | /* |
| 228 | * when a non-zero pid is specified, process should have |
| 229 | * CAP_SETPCAP capability to change capabilities. |
| 230 | * by default, CAP_SETPCAP is not enabled. So giving |
| 231 | * a non-zero pid results in capset() failing with |
| 232 | * errno EPERM |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 233 | * |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 234 | * Note: this seems to have changed with recent kernels |
| 235 | * => create a child and try to set its capabilities |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 236 | */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 237 | child_pid = FORK_OR_VFORK(); |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 238 | if (child_pid == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 239 | tst_brkm(TBROK | TERRNO, cleanup, "fork failed"); |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 240 | else if (child_pid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 241 | #ifdef UCLINUX |
| 242 | if (self_exec(argv0, "") < 0) { |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 243 | perror("self_exec failed"); |
| 244 | exit(1); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 245 | } |
| 246 | #else |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 247 | child_func(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 248 | #endif |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 249 | } else { |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 250 | header.pid = child_pid; |
robbiew | 9b0da82 | 2005-10-04 16:28:39 +0000 | [diff] [blame] | 251 | ltpuser = getpwnam(nobody_uid); |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 252 | if (ltpuser == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 253 | tst_brkm(TBROK | TERRNO, cleanup, |
| 254 | "getpwnam failed"); |
Garrett Cooper | e499f93 | 2010-12-17 11:00:51 -0800 | [diff] [blame] | 255 | if (seteuid(ltpuser->pw_uid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 256 | tst_brkm(TBROK | TERRNO, cleanup, |
| 257 | "seteuid failed"); |
robbiew | 9b0da82 | 2005-10-04 16:28:39 +0000 | [diff] [blame] | 258 | |
robbiew | 267a1d7 | 2005-03-21 22:48:56 +0000 | [diff] [blame] | 259 | } |
robbiew | a5d67cb | 2003-01-01 20:44:12 +0000 | [diff] [blame] | 260 | break; |
| 261 | |
| 262 | } |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 263 | } |