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: chown03 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, chown(2) succeeds to change the group of a file specified |
| 25 | * by path when called by non-root user with the following constraints, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 26 | * - euid of the process is equal to the owner of the file. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | * - the intended gid is either egid, or one of the supplementary gids |
| 28 | * of the process. |
| 29 | * Also, verify that chown() clears the setuid/setgid bits set on the file. |
| 30 | * |
| 31 | * Expected Result: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 32 | * 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] | 33 | * the numeric values contained in owner and group respectively. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 34 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | * Algorithm: |
| 36 | * Setup: |
| 37 | * Setup signal handling. |
| 38 | * Create temporary directory. |
| 39 | * Pause for SIGUSR1 if option specified. |
| 40 | * |
| 41 | * Test: |
| 42 | * Loop if the proper options are given. |
| 43 | * Execute system call |
| 44 | * Check return code, if system call failed (return=-1) |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 45 | * Log the errno and Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | * Otherwise, |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 47 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | * if successful, |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 49 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | * Otherwise, |
| 51 | * Issue Functionality-Fail message. |
| 52 | * Cleanup: |
| 53 | * Print errno log and/or timing stats if options given |
| 54 | * Delete the temporary directory created. |
| 55 | * |
| 56 | * Usage: <for command-line> |
| 57 | * chown03 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 58 | * where, -c n : Run n copies concurrently. |
| 59 | * -f : Turn off functionality Testing. |
| 60 | * -i n : Execute test n times. |
| 61 | * -I x : Execute test for x seconds. |
| 62 | * -P x : Pause for x seconds between iterations. |
| 63 | * -t : Turn on syscall timing. |
| 64 | * |
| 65 | * HISTORY |
| 66 | * 07/2001 Ported by Wayne Boyer |
| 67 | * |
| 68 | * RESTRICTIONS: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | * |
| 70 | */ |
| 71 | |
| 72 | #include <stdio.h> |
| 73 | #include <stdlib.h> |
| 74 | #include <sys/types.h> |
| 75 | #include <sys/stat.h> |
| 76 | #include <sys/fcntl.h> |
| 77 | #include <errno.h> |
| 78 | #include <string.h> |
| 79 | #include <signal.h> |
| 80 | #include <grp.h> |
| 81 | #include <pwd.h> |
| 82 | |
| 83 | #include "test.h" |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 84 | #include "compat_16.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | |
Garrett Cooper | 4f00dbe | 2010-12-17 16:05:52 -0800 | [diff] [blame] | 86 | #define FILE_MODE (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) |
| 87 | #define NEW_PERMS (S_IFREG|S_IRWXU|S_IRWXG|S_ISUID|S_ISGID) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | #define TESTFILE "testfile" |
| 89 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 90 | TCID_DEFINE(chown03); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | int TST_TOTAL = 1; /* Total number of test conditions */ |
robbiew | 6eabc14 | 2001-08-31 15:23:29 +0000 | [diff] [blame] | 92 | char nobody_uid[] = "nobody"; |
| 93 | struct passwd *ltpuser; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
| 95 | void setup(); /* setup function for the test */ |
| 96 | void cleanup(); /* cleanup function for the test */ |
| 97 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | { |
| 100 | struct stat stat_buf; /* stat(2) struct contents */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 101 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 102 | const char *msg; |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 103 | uid_t user_id; /* Owner id of the test file. */ |
| 104 | gid_t group_id; /* Group id of the test file. */ |
robbiew | 6eabc14 | 2001-08-31 15:23:29 +0000 | [diff] [blame] | 105 | |
Garrett Cooper | 4f00dbe | 2010-12-17 16:05:52 -0800 | [diff] [blame] | 106 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 108 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | setup(); |
| 110 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 112 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 113 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 115 | UID16_CHECK((user_id = geteuid()), "chown", cleanup) |
| 116 | GID16_CHECK((group_id = getegid()), "chown", cleanup) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 118 | TEST(CHOWN(cleanup, TESTFILE, -1, group_id)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | tst_resm(TFAIL | TTERRNO, "chown(%s, ..) failed", |
| 122 | TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | continue; |
| 124 | } |
| 125 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 126 | if (stat(TESTFILE, &stat_buf) == -1) |
| 127 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 128 | "stat failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 130 | if (stat_buf.st_uid != user_id || |
| 131 | stat_buf.st_gid != group_id) |
| 132 | tst_resm(TFAIL, "%s: Incorrect ownership" |
| 133 | "set to %d %d, Expected %d %d", |
| 134 | TESTFILE, stat_buf.st_uid, |
| 135 | stat_buf.st_gid, user_id, group_id); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 137 | if (stat_buf.st_mode != |
| 138 | (NEW_PERMS & ~(S_ISUID | S_ISGID))) |
| 139 | tst_resm(TFAIL, "%s: incorrect mode permissions" |
| 140 | " %#o, Expected %#o", TESTFILE, |
| 141 | stat_buf.st_mode, |
| 142 | NEW_PERMS & ~(S_ISUID | S_ISGID)); |
| 143 | else |
| 144 | tst_resm(TPASS, "chown(%s, ..) was successful", |
| 145 | TESTFILE); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 146 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | cleanup(); |
Garrett Cooper | 4f00dbe | 2010-12-17 16:05:52 -0800 | [diff] [blame] | 149 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 150 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * void |
| 154 | * setup() - performs all ONE TIME setup for this test. |
| 155 | * Create a temporary directory and change directory to it. |
| 156 | * Create a test file under temporary directory and close it |
| 157 | * Change the group ownership on testfile. |
| 158 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 159 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 161 | int fd; /* file handler for testfile */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | |
plars | 725c280 | 2004-09-21 21:22:34 +0000 | [diff] [blame] | 163 | TEST_PAUSE; |
| 164 | |
Garrett Cooper | 4f00dbe | 2010-12-17 16:05:52 -0800 | [diff] [blame] | 165 | tst_require_root(NULL); |
| 166 | |
Caspar Zhang | de92307 | 2011-05-11 17:39:54 +0800 | [diff] [blame] | 167 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 168 | |
| 169 | tst_tmpdir(); |
| 170 | |
Garrett Cooper | 4f00dbe | 2010-12-17 16:05:52 -0800 | [diff] [blame] | 171 | ltpuser = getpwnam(nobody_uid); |
| 172 | if (ltpuser == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 173 | tst_brkm(TBROK | TERRNO, NULL, "getpwnam(\"nobody\") failed"); |
Garrett Cooper | 4f00dbe | 2010-12-17 16:05:52 -0800 | [diff] [blame] | 174 | if (setegid(ltpuser->pw_gid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 175 | tst_brkm(TBROK | TERRNO, NULL, "setegid(%d) failed", |
| 176 | ltpuser->pw_gid); |
Garrett Cooper | 4f00dbe | 2010-12-17 16:05:52 -0800 | [diff] [blame] | 177 | if (seteuid(ltpuser->pw_uid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 178 | tst_brkm(TBROK | TERRNO, NULL, "seteuid(%d) failed", |
| 179 | ltpuser->pw_uid); |
plars | 725c280 | 2004-09-21 21:22:34 +0000 | [diff] [blame] | 180 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | /* Create a test file under temporary directory */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 182 | if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) |
| 183 | tst_brkm(TBROK | TERRNO, cleanup, |
| 184 | "open(%s, O_RDWR|O_CREAT, %o) failed", TESTFILE, |
| 185 | FILE_MODE); |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 186 | |
| 187 | if (seteuid(0) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 188 | tst_brkm(TBROK | TERRNO, cleanup, "seteuid(0) failed"); |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 189 | |
| 190 | if (fchown(fd, -1, 0) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 191 | tst_brkm(TBROK | TERRNO, cleanup, "fchown failed"); |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 192 | |
| 193 | if (fchmod(fd, NEW_PERMS) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 194 | tst_brkm(TBROK | TERRNO, cleanup, "fchmod failed"); |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 195 | |
| 196 | if (seteuid(ltpuser->pw_uid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 197 | tst_brkm(TBROK | TERRNO, cleanup, "seteuid to nobody failed"); |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 198 | |
vapier | 3fdf88a | 2009-08-28 11:59:17 +0000 | [diff] [blame] | 199 | if (close(fd) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 200 | tst_brkm(TBROK | TERRNO, cleanup, "closing %s failed", |
| 201 | TESTFILE); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 202 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 204 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 205 | { |
Garrett Cooper | 4f00dbe | 2010-12-17 16:05:52 -0800 | [diff] [blame] | 206 | if (setegid(0) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 207 | tst_resm(TWARN | TERRNO, "setegid(0) failed"); |
Garrett Cooper | 4f00dbe | 2010-12-17 16:05:52 -0800 | [diff] [blame] | 208 | if (seteuid(0) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 209 | tst_resm(TWARN | TERRNO, "seteuid(0) failed"); |
subrata_modak | a0a3e41 | 2009-06-15 18:58:25 +0000 | [diff] [blame] | 210 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 211 | tst_rmdir(); |
| 212 | |
Caspar Zhang | de92307 | 2011-05-11 17:39:54 +0800 | [diff] [blame] | 213 | } |