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: fchmod05 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * Test Description: |
| 24 | * Verify that, fchmod(2) will succeed to change the mode of a directory |
| 25 | * but fails to set the setgid bit on it if invoked by non-root (uid != 0) |
| 26 | * process with the following constraints, |
| 27 | * - the process is the owner of the directory. |
| 28 | * - the effective group ID or one of the supplementary group ID's of the |
| 29 | * process is not equal to the group ID of the directory. |
| 30 | * |
| 31 | * Expected Result: |
| 32 | * fchmod() should return value 0 on success and though succeeds to change |
| 33 | * the mode of a directory but fails to set setgid bit on it. |
| 34 | * |
| 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 | * fchmod05 [-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: |
| 69 | * This test should be run by 'non-super-user' only. |
| 70 | * |
| 71 | */ |
| 72 | |
vapier | 5b31d1b | 2006-08-21 06:46:26 +0000 | [diff] [blame] | 73 | #ifndef _GNU_SOURCE |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 74 | #define _GNU_SOURCE |
vapier | 5b31d1b | 2006-08-21 06:46:26 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | #include <stdio.h> |
| 78 | #include <stdlib.h> |
| 79 | #include <sys/types.h> |
| 80 | #include <sys/stat.h> |
| 81 | #include <sys/fcntl.h> |
| 82 | #include <errno.h> |
| 83 | #include <string.h> |
| 84 | #include <signal.h> |
robbiew | 15226cd | 2002-04-10 16:10:40 +0000 | [diff] [blame] | 85 | #include <unistd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | #include <grp.h> |
| 87 | #include <pwd.h> |
| 88 | |
| 89 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 91 | #define MODE_RWX (S_IRWXU | S_IRWXG | S_IRWXO) |
robbiew | cd50126 | 2002-06-10 17:42:53 +0000 | [diff] [blame] | 92 | #define PERMS 043777 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | #define TESTDIR "testdir" |
| 94 | |
| 95 | int fd; /* file descriptor for test directory */ |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 96 | char *TCID = "fchmod05"; |
| 97 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
| 99 | void setup(); /* Main setup function for test */ |
| 100 | void cleanup(); /* Main cleanup function for test */ |
| 101 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | { |
| 104 | struct stat stat_buf; /* stat struct */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 105 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 106 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | mode_t dir_mode; /* mode permissions set on test directory */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 108 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 109 | msg = parse_opts(ac, av, NULL, NULL); |
| 110 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } |
| 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | setup(); |
| 116 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 118 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 119 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 121 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | * Call fchmod(2) with mode argument |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | * to set setgid bit on TESTDIR. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | */ |
robbiew | 4898862 | 2001-09-10 18:33:12 +0000 | [diff] [blame] | 125 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | TEST(fchmod(fd, PERMS)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 127 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | if (TEST_RETURN == -1) { |
| 129 | tst_resm(TFAIL, "fchmod(%d, %#o) Failed, errno=%d : %s", |
| 130 | fd, PERMS, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 131 | continue; |
| 132 | } |
| 133 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 134 | * Get the directory information using |
| 135 | * fstat(2). |
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 (fstat(fd, &stat_buf) < 0) { |
| 138 | tst_brkm(TFAIL, cleanup, |
| 139 | "fstat(2) of %s failed, errno:%d", |
| 140 | TESTDIR, TEST_ERRNO); |
| 141 | } |
| 142 | dir_mode = stat_buf.st_mode; |
| 143 | if ((PERMS & ~S_ISGID) != dir_mode) { |
| 144 | tst_resm(TFAIL, "%s: Incorrect modes 0%03o, " |
| 145 | "Expected 0%03o", |
| 146 | TESTDIR, dir_mode, PERMS & ~S_ISGID); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 148 | tst_resm(TPASS, "Functionality of fchmod(%d, " |
| 149 | "%#o) successful", fd, |
| 150 | PERMS & ~S_ISGID); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 152 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 155 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 156 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | |
| 158 | /* |
| 159 | * void |
| 160 | * setup() - performs all ONE TIME setup for this test. |
| 161 | * Create a temporary directory and cd to it. |
| 162 | * Create a test directory under temporary directory. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 163 | * Invoke setuid to root program to modify group ownership |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | * on test directory. |
| 165 | * Open the test directory for reading. |
| 166 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 167 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | { |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 169 | struct passwd *nobody_u; |
| 170 | struct group *bin_group; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 172 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 174 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | TEST_PAUSE; |
| 177 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | tst_tmpdir(); |
| 179 | |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 180 | nobody_u = getpwnam("nobody"); |
| 181 | if (!nobody_u) |
| 182 | tst_brkm(TBROK, cleanup, |
| 183 | "Couldn't find uid of nobody: %s", strerror(errno)); |
| 184 | |
| 185 | bin_group = getgrnam("bin"); |
| 186 | if (!bin_group) |
| 187 | tst_brkm(TBROK, cleanup, |
| 188 | "Couldn't find gid of bin: %s", strerror(errno)); |
| 189 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | /* |
| 191 | * Create a test directory under temporary directory with specified |
| 192 | * mode permissions and change the gid of test directory to that of |
| 193 | * guest user. |
| 194 | */ |
| 195 | if (mkdir(TESTDIR, MODE_RWX) < 0) { |
| 196 | tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", TESTDIR); |
| 197 | } |
| 198 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 199 | if (setgroups(1, &nobody_u->pw_gid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 200 | tst_brkm(TBROK, cleanup, |
| 201 | "Couldn't change supplementary group Id: %s", |
| 202 | strerror(errno)); |
subrata_modak | 624a96e | 2009-09-16 14:43:43 +0000 | [diff] [blame] | 203 | |
| 204 | if (chown(TESTDIR, nobody_u->pw_uid, bin_group->gr_gid) == -1) |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 205 | tst_brkm(TBROK, cleanup, "Couldn't change owner of testdir: %s", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 206 | strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 207 | |
subrata_modak | 624a96e | 2009-09-16 14:43:43 +0000 | [diff] [blame] | 208 | /* change to nobody:nobody */ |
| 209 | if (setegid(nobody_u->pw_gid) == -1 || seteuid(nobody_u->pw_uid) == -1) |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 210 | tst_brkm(TBROK, cleanup, "Couldn't switch to nobody:nobody: %s", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 211 | strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 212 | |
| 213 | /* Open the test directory for reading */ |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 214 | fd = open(TESTDIR, O_RDONLY); |
| 215 | if (fd == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | tst_brkm(TBROK, cleanup, |
| 217 | "open(%s, O_RDONLY) failed, errno=%d : %s", |
| 218 | TESTDIR, errno, strerror(errno)); |
| 219 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 220 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * void |
| 224 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 225 | * completion or premature exit. |
| 226 | * Close the test directory opened in the setup(). |
| 227 | * Remove the test directory and temporary directory created in |
| 228 | * in the setup(). |
| 229 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 230 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 231 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 232 | |
| 233 | /* Close the test directory opened in the setup() */ |
| 234 | if (close(fd) == -1) { |
| 235 | tst_brkm(TBROK, NULL, |
| 236 | "close(%s) Failed, errno=%d : %s", |
| 237 | TESTDIR, errno, strerror(errno)); |
| 238 | } |
| 239 | |
subrata_modak | 0e9fc31 | 2009-06-15 18:57:19 +0000 | [diff] [blame] | 240 | setegid(0); |
| 241 | seteuid(0); |
| 242 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 243 | tst_rmdir(); |
| 244 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 245 | } |