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: chmod05 |
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: |
robbiew | ec91fc4 | 2002-06-10 17:32:41 +0000 | [diff] [blame] | 24 | //wjh |
| 25 | //This test seems to be invalid see comments below |
| 26 | //The man page for chmod doesn't seem to indicate that the setgid bit can |
| 27 | //only be set by root |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 28 | * Verify that, chmod(2) will succeed to change the mode of a directory |
| 29 | * but fails to set the setgid bit on it if invoked by non-root (uid != 0) |
| 30 | * process with the following constraints, |
| 31 | * - the process is the owner of the directory. |
| 32 | * - the effective group ID or one of the supplementary group ID's of the |
| 33 | * process is not equal to the group ID of the directory. |
| 34 | * |
| 35 | * Expected Result: |
| 36 | * chmod() should return value 0 on success and though succeeds to change |
| 37 | * the mode of a directory but fails to set setgid bit on it. |
| 38 | * |
| 39 | * Algorithm: |
| 40 | * Setup: |
| 41 | * Setup signal handling. |
| 42 | * Create temporary directory. |
| 43 | * Pause for SIGUSR1 if option specified. |
| 44 | * |
| 45 | * Test: |
| 46 | * Loop if the proper options are given. |
| 47 | * Execute system call |
| 48 | * Check return code, if system call failed (return=-1) |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 49 | * Log the errno and Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | * Otherwise, |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 51 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | * if successful, |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 53 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | * Otherwise, |
| 55 | * Issue Functionality-Fail message. |
| 56 | * Cleanup: |
| 57 | * Print errno log and/or timing stats if options given |
| 58 | * Delete the temporary directory created. |
| 59 | * |
| 60 | * Usage: <for command-line> |
| 61 | * chmod05 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t] |
| 62 | * where, -c n : Run n copies concurrently. |
| 63 | * -e : Turn on errno logging. |
| 64 | * -f : Turn off functionality Testing. |
| 65 | * -i n : Execute test n times. |
| 66 | * -I x : Execute test for x seconds. |
| 67 | * -P x : Pause for x seconds between iterations. |
| 68 | * -t : Turn on syscall timing. |
| 69 | * |
| 70 | * HISTORY |
| 71 | * 07/2001 Ported by Wayne Boyer |
robbiew | ec91fc4 | 2002-06-10 17:32:41 +0000 | [diff] [blame] | 72 | * 05/2002 Fixes by wjhuie and Ferhan Shareef |
| 73 | * changed conditional to check for valid tests only and |
| 74 | * in a more logically understandable way |
| 75 | * 07/2002 Additional fixes to #defines to allow comparisions to work. |
| 76 | * -Robbie Williamson |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | * |
| 78 | * RESTRICTIONS: |
Zeng Linggang | 52d294a | 2014-02-18 23:22:51 +0800 | [diff] [blame] | 79 | * This test should be run by root. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | * |
| 81 | */ |
| 82 | |
vapier | 5b31d1b | 2006-08-21 06:46:26 +0000 | [diff] [blame] | 83 | #ifndef _GNU_SOURCE |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | #define _GNU_SOURCE |
vapier | 5b31d1b | 2006-08-21 06:46:26 +0000 | [diff] [blame] | 85 | #endif |
| 86 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | #include <stdio.h> |
| 88 | #include <stdlib.h> |
| 89 | #include <sys/types.h> |
| 90 | #include <sys/stat.h> |
| 91 | #include <sys/fcntl.h> |
| 92 | #include <errno.h> |
| 93 | #include <string.h> |
| 94 | #include <signal.h> |
robbiew | 15226cd | 2002-04-10 16:10:40 +0000 | [diff] [blame] | 95 | #include <unistd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | #include <grp.h> |
| 97 | #include <pwd.h> |
| 98 | |
| 99 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
vapier | 7b7103c | 2006-02-11 04:34:36 +0000 | [diff] [blame] | 101 | #define DEBUG 0 |
| 102 | |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 103 | #define MODE_RWX (mode_t)(S_IRWXU | S_IRWXG | S_IRWXO) |
robbiew | ec91fc4 | 2002-06-10 17:32:41 +0000 | [diff] [blame] | 104 | #define DIR_MODE (mode_t)(S_ISVTX | S_ISGID | S_IFDIR) |
| 105 | #define PERMS (mode_t)(MODE_RWX | DIR_MODE) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | #define TESTDIR "testdir" |
| 107 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 108 | char *TCID = "chmod05"; |
| 109 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
| 111 | void setup(); /* Main setup function for test */ |
| 112 | void cleanup(); /* Main cleanup function for test */ |
| 113 | |
robbiew | ec91fc4 | 2002-06-10 17:32:41 +0000 | [diff] [blame] | 114 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | struct stat stat_buf; /* stat struct */ |
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; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | mode_t dir_mode; /* mode permissions set on test directory */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 121 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 123 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 126 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 127 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 128 | tst_count = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 129 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | TEST(chmod(TESTDIR, PERMS)); |
| 131 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | if (TEST_RETURN == -1) { |
vapier | 4599609 | 2009-08-28 11:49:07 +0000 | [diff] [blame] | 133 | tst_resm(TFAIL, "chmod(%s, %#o) failed", |
| 134 | TESTDIR, PERMS); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | continue; |
| 136 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 137 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 138 | * Get the directory information using |
| 139 | * stat(2). |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 141 | if (stat(TESTDIR, &stat_buf) < 0) { |
| 142 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 143 | "stat(%s) failed", TESTDIR); |
| 144 | } |
| 145 | dir_mode = stat_buf.st_mode; |
vapier | 7b7103c | 2006-02-11 04:34:36 +0000 | [diff] [blame] | 146 | #if DEBUG |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 147 | printf("DIR_MODE = 0%03o\n", DIR_MODE); |
| 148 | printf("MODE_RWX = 0%03o\n", MODE_RWX); |
| 149 | printf("PERMS = 0%03o\n", PERMS); |
| 150 | printf("dir_mode = 0%03o\n", dir_mode); |
vapier | 7b7103c | 2006-02-11 04:34:36 +0000 | [diff] [blame] | 151 | #endif |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 152 | if ((PERMS & ~S_ISGID) != dir_mode) |
| 153 | tst_resm(TFAIL, "%s: Incorrect modes 0%03o, " |
| 154 | "Expected 0%03o", TESTDIR, dir_mode, |
| 155 | PERMS & ~S_ISGID); |
| 156 | else |
| 157 | tst_resm(TPASS, |
| 158 | "Functionality of chmod(%s, %#o) successful", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | TESTDIR, PERMS); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 160 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | cleanup(); |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 163 | tst_exit(); |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 164 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * void |
| 168 | * setup() - performs all ONE TIME setup for this test. |
| 169 | * Create a temporary directory and cd to it. |
| 170 | * Create a test directory under temporary directory. |
robbiew | ec91fc4 | 2002-06-10 17:32:41 +0000 | [diff] [blame] | 171 | //wjh we are root so do we really need this kluged helper program? |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 172 | * Invoke setuid to root program to modify group ownership |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | * on test directory. |
| 174 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 175 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | { |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 177 | struct passwd *nobody_u; |
| 178 | struct group *bin_group; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 180 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 182 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 184 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 186 | nobody_u = getpwnam("nobody"); |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 187 | if (nobody_u == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 188 | tst_brkm(TBROK | TERRNO, cleanup, |
| 189 | "getpwnam(\"nobody\") failed"); |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 190 | |
| 191 | bin_group = getgrnam("bin"); |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 192 | if (bin_group == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 193 | tst_brkm(TBROK | TERRNO, cleanup, "getgrnam(\"bin\") failed"); |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 194 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 195 | /* |
| 196 | * Create a test directory under temporary directory with specified |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 197 | * mode permissions and change the gid of test directory to nobody's |
| 198 | * gid. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 199 | */ |
vapier | 4599609 | 2009-08-28 11:49:07 +0000 | [diff] [blame] | 200 | if (mkdir(TESTDIR, MODE_RWX) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 201 | tst_brkm(TBROK | TERRNO, cleanup, "mkdir(%s) failed", TESTDIR); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 202 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 203 | if (setgroups(1, &nobody_u->pw_gid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 204 | tst_brkm(TBROK | TERRNO, cleanup, |
| 205 | "setgroups to nobody's gid failed"); |
subrata_modak | 624a96e | 2009-09-16 14:43:43 +0000 | [diff] [blame] | 206 | |
| 207 | if (chown(TESTDIR, nobody_u->pw_uid, bin_group->gr_gid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 208 | tst_brkm(TBROK | TERRNO, cleanup, |
| 209 | "chowning testdir to nobody:bin failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 210 | |
subrata_modak | 624a96e | 2009-09-16 14:43:43 +0000 | [diff] [blame] | 211 | /* change to nobody:nobody */ |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 212 | if (setegid(nobody_u->pw_gid) == -1 || seteuid(nobody_u->pw_uid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 213 | tst_brkm(TBROK | TERRNO, cleanup, |
| 214 | "Couldn't switch to nobody:nobody"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 215 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 217 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | { |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 219 | if (setegid(0) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 220 | tst_resm(TWARN | TERRNO, "setegid(0) failed"); |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 221 | if (seteuid(0) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 222 | tst_resm(TWARN | TERRNO, "seteuid(0) failed"); |
subrata_modak | 96a6520 | 2009-06-09 16:54:50 +0000 | [diff] [blame] | 223 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 224 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 225 | } |