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 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * Test Name: chown02 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, when chown(2) invoked by super-user to change the owner and |
| 25 | * group of a file specified by path to any numeric owner(uid)/group(gid) |
| 26 | * values, |
| 27 | * - clears setuid and setgid bits set on an executable file. |
| 28 | * - preserves setgid bit set on a non-group-executable file. |
| 29 | * |
| 30 | * Expected Result: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 31 | * chown(2) should return 0 and the ownership set on the file should match |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | * the numeric values contained in owner and group respectively. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 33 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | * Algorithm: |
| 35 | * Setup: |
| 36 | * Setup signal handling. |
| 37 | * Create temporary directory. |
| 38 | * Pause for SIGUSR1 if option specified. |
| 39 | * |
| 40 | * Test: |
| 41 | * Loop if the proper options are given. |
| 42 | * Execute system call |
| 43 | * Check return code, if system call failed (return=-1) |
| 44 | * Log the errno and Issue a FAIL message. |
| 45 | * Otherwise, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 46 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | * if successful, |
| 48 | * Issue Functionality-Pass message. |
| 49 | * Otherwise, |
| 50 | * Issue Functionality-Fail message. |
| 51 | * Cleanup: |
| 52 | * Print errno log and/or timing stats if options given |
| 53 | * Delete the temporary directory created. |
| 54 | * |
| 55 | * Usage: <for command-line> |
| 56 | * chown02 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 57 | * where, -c n : Run n copies concurrently. |
| 58 | * -f : Turn off functionality Testing. |
| 59 | * -i n : Execute test n times. |
| 60 | * -I x : Execute test for x seconds. |
| 61 | * -P x : Pause for x seconds between iterations. |
| 62 | * -t : Turn on syscall timing. |
| 63 | * |
| 64 | * HISTORY |
| 65 | * 07/2001 Ported by Wayne Boyer |
| 66 | * |
| 67 | * RESTRICTIONS: |
| 68 | * This test should be run by 'super-user' (root) only. |
| 69 | * |
| 70 | */ |
| 71 | |
| 72 | #include <stdio.h> |
| 73 | #include <sys/types.h> |
| 74 | #include <sys/stat.h> |
| 75 | #include <sys/fcntl.h> |
| 76 | #include <errno.h> |
| 77 | #include <string.h> |
| 78 | #include <signal.h> |
| 79 | |
| 80 | #include "test.h" |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 81 | #include "compat_16.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 83 | #define FILE_MODE (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) |
| 84 | #define NEW_PERMS1 (S_IFREG|S_IRWXU|S_IRWXG|S_ISUID|S_ISGID) |
| 85 | #define NEW_PERMS2 (S_IFREG|S_IRWXU|S_ISGID) |
| 86 | #define EXP_PERMS (S_IFREG|S_IRWXU|S_IRWXG) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | #define TESTFILE1 "testfile1" |
| 88 | #define TESTFILE2 "testfile2" |
| 89 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 90 | TCID_DEFINE(chown02); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | int setup1(); /* Test specific setup functions */ |
| 93 | int setup2(); |
| 94 | |
| 95 | struct test_case_t { |
| 96 | char *pathname; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | uid_t user_id; |
| 98 | gid_t group_id; |
| 99 | int test_flag; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | int (*setupfunc) (); |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 101 | } test_cases[] = { |
| 102 | /* setuid/setgid bits cleared */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 103 | { |
| 104 | TESTFILE1, 700, 701, 1, setup1}, |
| 105 | /* setgid bit not cleared */ |
| 106 | { |
| 107 | TESTFILE2, 700, 701, 2, setup2},}; |
| 108 | |
Cyril Hrubis | b863a0b | 2014-09-24 13:15:29 +0200 | [diff] [blame] | 109 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
| 111 | void setup(); /* setup function for the test */ |
| 112 | void cleanup(); /* cleanup function for the test */ |
| 113 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | { |
| 116 | struct stat stat_buf; /* stat(2) struct contents */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 117 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 118 | const char *msg; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 119 | int i; |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 120 | uid_t user_id; /* user id of the user set for testfile */ |
| 121 | gid_t group_id; /* group id of the user set for testfile */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | int test_flag; /* test condition specific flag variable */ |
| 123 | char *file_name; /* ptr. for test file name */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 125 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 127 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | setup(); |
| 129 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 131 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 132 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 134 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 136 | file_name = test_cases[i].pathname; |
| 137 | user_id = test_cases[i].user_id; |
| 138 | group_id = test_cases[i].group_id; |
| 139 | test_flag = test_cases[i].test_flag; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 141 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | * Call chown(2) with different user id and |
| 143 | * group id (numeric values) to set it on testfile. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | */ |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 145 | TEST(CHOWN(cleanup, file_name, user_id, group_id)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 148 | tst_resm(TFAIL | TTERRNO, |
| 149 | "chown(%s, ..) failed", file_name); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | continue; |
| 151 | } |
| 152 | |
| 153 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 154 | * Get the testfile information using stat(2). |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 156 | if (stat(file_name, &stat_buf) < 0) { |
| 157 | tst_brkm(TFAIL, cleanup, "stat(2) of " |
| 158 | "%s failed, errno:%d", |
| 159 | file_name, TEST_ERRNO); |
| 160 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 162 | /* |
| 163 | * Check for expected Ownership ids |
| 164 | * set on testfile. |
| 165 | */ |
| 166 | if (stat_buf.st_uid != user_id || |
| 167 | stat_buf.st_gid != group_id) { |
| 168 | tst_brkm(TFAIL, cleanup, "%s: incorrect" |
| 169 | " ownership set, Expected %d " |
| 170 | "%d", file_name, |
| 171 | user_id, group_id); |
| 172 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 174 | /* |
| 175 | * Verify that S_ISUID/S_ISGID bits set on the |
| 176 | * testfile(s) in setup()s are cleared by |
| 177 | * chown(). |
| 178 | */ |
| 179 | if (test_flag == 1 && |
| 180 | (stat_buf.st_mode & (S_ISUID | S_ISGID)) != 0) { |
| 181 | tst_resm(TFAIL, |
| 182 | "%s: incorrect mode " |
| 183 | "permissions %#o, Expected " |
| 184 | "%#o", file_name, NEW_PERMS1, |
| 185 | EXP_PERMS); |
| 186 | } else if (test_flag == 2 |
| 187 | && (stat_buf.st_mode & S_ISGID) == 0) { |
| 188 | tst_resm(TFAIL, |
| 189 | "%s: Incorrect mode " |
| 190 | "permissions %#o, Expected " |
| 191 | "%#o", file_name, |
| 192 | stat_buf.st_mode, NEW_PERMS2); |
| 193 | } else { |
| 194 | tst_resm(TPASS, |
| 195 | "chown(%s, ..) succeeded", |
| 196 | file_name); |
| 197 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 199 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | cleanup(); |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 202 | tst_exit(); |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 203 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * void |
| 207 | * setup() - performs all ONE TIME setup for this test. |
| 208 | * Create a temporary directory and change directory to it. |
| 209 | * Create a test file under temporary directory and close it |
| 210 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 211 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 212 | { |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 213 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 216 | |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 217 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 219 | TEST_PAUSE; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 220 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 221 | tst_tmpdir(); |
| 222 | |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 223 | /* call iividual setup functions */ |
| 224 | for (i = 0; i < TST_TOTAL; i++) |
| 225 | test_cases[i].setupfunc(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 226 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * int |
| 230 | * setup1() - Setup function for chown(2) to verify setuid/setgid bits |
| 231 | * set on an executable file will not be cleared. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 232 | * Creat a testfile and set setuid/setgid bits on the mode of file.$ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 233 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 234 | int setup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 235 | { |
| 236 | int fd; /* File descriptor for testfile1 */ |
| 237 | |
| 238 | /* Creat a testfile and close it */ |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 239 | if ((fd = open(TESTFILE1, O_RDWR | O_CREAT, FILE_MODE)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 240 | tst_brkm(TBROK | TERRNO, cleanup, |
| 241 | "open(%s, O_RDWR|O_CREAT, %o) failed", |
| 242 | TESTFILE1, FILE_MODE); |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 243 | if (close(fd) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 244 | tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed", |
| 245 | TESTFILE1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 246 | |
| 247 | /* Set setuid/setgid bits on the test file created */ |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 248 | if (chmod(TESTFILE1, NEW_PERMS1) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 249 | tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, ..) failed", |
| 250 | TESTFILE1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 251 | return 0; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 252 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 253 | |
| 254 | /* |
| 255 | * int |
| 256 | * setup2() - Setup function for chown(2) to verify setgid bit set |
| 257 | * set on non-group executable file will not be cleared. |
| 258 | * Creat a testfile and set setgid bit on the mode of file. |
| 259 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 260 | int setup2(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 261 | { |
| 262 | int fd; /* File descriptor for testfile2 */ |
| 263 | |
| 264 | /* Creat a testfile and close it */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 265 | if ((fd = open(TESTFILE2, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 266 | tst_brkm(TBROK | TERRNO, cleanup, |
| 267 | "open(%s, O_RDWR|O_CREAT, %o) failed", |
| 268 | TESTFILE2, FILE_MODE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 269 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 270 | /* Set setgid bit on the test file created */ |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 271 | if (fchmod(fd, NEW_PERMS2) != 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 272 | tst_brkm(TBROK | TERRNO, cleanup, "fchmod failed"); |
Garrett Cooper | 05ef3ef | 2010-12-17 16:05:21 -0800 | [diff] [blame] | 273 | if (close(fd) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 274 | tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed", |
| 275 | TESTFILE2); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * void |
| 281 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 282 | * completion or premature exit. |
| 283 | * Remove the test directory and testfile created in the setup. |
| 284 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 285 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 286 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 287 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 288 | tst_rmdir(); |
| 289 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 290 | } |