robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2002 |
| 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 |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * creat08.c - Verifies that the group ID and setgid bit are |
| 23 | * set correctly when a new file is created. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * (ported from SPIE, section2/iosuite/creat5.c, |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 25 | * by Airong Zhang <zhanga@us.ibm.com>) |
| 26 | * CALLS |
| 27 | * creat |
| 28 | * |
| 29 | * ALGORITHM |
| 30 | * Create two directories, one with the group ID of this process |
| 31 | * and the setgid bit not set, and the other with a group ID |
| 32 | * other than that of this process and with the setgid bit set. |
| 33 | * In each directory, create a file with and without the setgid |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 34 | * bit set in the creation modes. Verify that the modes and group |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 35 | * ID are correct on each of the 4 files. |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 36 | * As root, create a file with the setgid bit on in the |
| 37 | * directory with the setgid bit. |
| 38 | * This tests the SVID3 create group semantics. |
| 39 | * |
| 40 | * USAGE |
| 41 | * creat08 |
| 42 | * |
| 43 | * RESTRICTIONS |
| 44 | * |
| 45 | */ |
| 46 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 47 | #include <stdio.h> /* needed by testhead.h */ |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 48 | #include <sys/types.h> |
| 49 | #include <sys/stat.h> |
| 50 | #include <sys/fcntl.h> |
| 51 | #include <errno.h> |
| 52 | #include <grp.h> |
| 53 | #include <pwd.h> |
| 54 | #include "test.h" |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 55 | |
| 56 | char *TCID = "creat08"; |
| 57 | int TST_TOTAL = 1; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 58 | int local_flag; |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 59 | |
| 60 | #define PASSED 1 |
| 61 | #define FAILED 0 |
| 62 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 63 | #define MODE_RWX (S_IRWXU|S_IRWXG|S_IRWXO) |
| 64 | #define MODE_SGID (S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 65 | #define DIR_A_TEMP "testdir.A.%d" |
| 66 | #define DIR_B_TEMP "testdir.B.%d" |
| 67 | #define SETGID "setgid" |
| 68 | #define NOSETGID "nosetgid" |
| 69 | #define ROOT_SETGID "root_setgid" |
| 70 | #define MSGSIZE 150 |
| 71 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 72 | static void cleanup(void); |
| 73 | static void setup(void); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 74 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 75 | static char DIR_A[MSGSIZE], DIR_B[MSGSIZE]; |
| 76 | static char setgid_A[MSGSIZE], nosetgid_A[MSGSIZE]; |
| 77 | static char setgid_B[MSGSIZE], nosetgid_B[MSGSIZE], root_setgid_B[MSGSIZE]; |
| 78 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | int main(int ac, char **av) |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 80 | { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 81 | struct stat buf; |
| 82 | struct group *group; |
| 83 | struct passwd *user1; |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 84 | gid_t group1_gid, group2_gid, mygid; |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 85 | uid_t save_myuid, user1_uid; |
| 86 | pid_t mypid; |
| 87 | |
Michal Simek | cfb6766 | 2011-09-15 13:09:02 +0200 | [diff] [blame] | 88 | int fd; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 89 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 90 | const char *msg; |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 91 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | /* |
| 93 | * parse standard options |
| 94 | */ |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 95 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
| 96 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 97 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 98 | setup(); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 99 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 100 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 101 | |
| 102 | local_flag = PASSED; |
| 103 | |
| 104 | save_myuid = getuid(); |
| 105 | mypid = getpid(); |
| 106 | sprintf(DIR_A, DIR_A_TEMP, mypid); |
| 107 | sprintf(DIR_B, DIR_B_TEMP, mypid); |
| 108 | sprintf(setgid_A, "%s/%s", DIR_A, SETGID); |
| 109 | sprintf(nosetgid_A, "%s/%s", DIR_A, NOSETGID); |
| 110 | sprintf(setgid_B, "%s/%s", DIR_B, SETGID); |
| 111 | sprintf(nosetgid_B, "%s/%s", DIR_B, NOSETGID); |
| 112 | sprintf(root_setgid_B, "%s/%s", DIR_B, ROOT_SETGID); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 113 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 114 | /* Get the uid of user1 */ |
robbiew | 977f57c | 2002-12-31 21:49:10 +0000 | [diff] [blame] | 115 | if ((user1 = getpwnam("nobody")) == NULL) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 116 | tst_brkm(TBROK | TERRNO, NULL, |
| 117 | "getpwnam(\"nobody\") failed"); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | user1_uid = user1->pw_uid; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 121 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 122 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 123 | * Get the group IDs of group1 and group2. |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 124 | */ |
robbiew | 977f57c | 2002-12-31 21:49:10 +0000 | [diff] [blame] | 125 | if ((group = getgrnam("nobody")) == NULL) { |
Salvatore Cro | ea9260d | 2011-11-29 15:54:09 +0100 | [diff] [blame] | 126 | if ((group = getgrnam("nogroup")) == NULL) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 127 | tst_brkm(TBROK | TERRNO, cleanup, |
| 128 | "getgrnam(\"nobody\") and " |
| 129 | "getgrnam(\"nogroup\") failed"); |
Salvatore Cro | ea9260d | 2011-11-29 15:54:09 +0100 | [diff] [blame] | 130 | } |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 131 | } |
| 132 | group1_gid = group->gr_gid; |
robbiew | 977f57c | 2002-12-31 21:49:10 +0000 | [diff] [blame] | 133 | if ((group = getgrnam("bin")) == NULL) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 134 | tst_brkm(TBROK | TERRNO, cleanup, |
| 135 | "getgrnam(\"bin\") failed"); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 136 | } |
| 137 | group2_gid = group->gr_gid; |
| 138 | |
| 139 | /*--------------------------------------------------------------*/ |
| 140 | /* Block0: Set up the parent directories */ |
| 141 | /*--------------------------------------------------------------*/ |
| 142 | /* |
| 143 | * Create a directory with group id the same as this process |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 144 | * and with no setgid bit. |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 145 | */ |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 146 | if (mkdir(DIR_A, MODE_RWX) == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 147 | tst_resm(TFAIL, "Creation of %s failed", DIR_A); |
| 148 | local_flag = FAILED; |
| 149 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 150 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 151 | if (chown(DIR_A, user1_uid, group2_gid) == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 152 | tst_resm(TFAIL, "Chown of %s failed", DIR_A); |
| 153 | local_flag = FAILED; |
| 154 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 155 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 156 | if (stat(DIR_A, &buf) == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 157 | tst_resm(TFAIL, "Stat of %s failed", DIR_A); |
| 158 | local_flag = FAILED; |
| 159 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 160 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 161 | /* Verify modes */ |
| 162 | if (buf.st_mode & S_ISGID) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | tst_resm(TFAIL, "%s: Incorrect modes, setgid bit set", |
| 164 | DIR_A); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 165 | local_flag = FAILED; |
| 166 | } |
| 167 | |
| 168 | /* Verify group ID */ |
| 169 | if (buf.st_gid != group2_gid) { |
| 170 | tst_resm(TFAIL, "%s: Incorrect group", DIR_A); |
vapier | dac6873 | 2009-08-28 12:15:15 +0000 | [diff] [blame] | 171 | tst_resm(TINFO, "got %u and %u", buf.st_gid, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 172 | group2_gid); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 173 | local_flag = FAILED; |
| 174 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 175 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 176 | /* |
| 177 | * Create a directory with group id different from that of |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 178 | * this process and with the setgid bit set. |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 179 | */ |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 180 | if (mkdir(DIR_B, MODE_RWX) == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 181 | tst_resm(TFAIL, "Creation of %s failed", DIR_B); |
| 182 | local_flag = FAILED; |
| 183 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 184 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 185 | if (chown(DIR_B, user1_uid, group2_gid) == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 186 | tst_resm(TFAIL, "Chown of %s failed", DIR_B); |
| 187 | local_flag = FAILED; |
| 188 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 189 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 190 | if (chmod(DIR_B, MODE_SGID) == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 191 | tst_resm(TFAIL, "Chmod of %s failed", DIR_B); |
| 192 | local_flag = FAILED; |
| 193 | } |
| 194 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 195 | if (stat(DIR_B, &buf) == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 196 | tst_resm(TFAIL, "Stat of %s failed", DIR_B); |
| 197 | local_flag = FAILED; |
| 198 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 199 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 200 | /* Verify modes */ |
| 201 | if (!(buf.st_mode & S_ISGID)) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 202 | tst_resm(TFAIL, |
| 203 | "%s: Incorrect modes, setgid bit not set", |
| 204 | DIR_B); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 205 | local_flag = FAILED; |
| 206 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 207 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 208 | /* Verify group ID */ |
| 209 | if (buf.st_gid != group2_gid) { |
| 210 | tst_resm(TFAIL, "%s: Incorrect group", DIR_B); |
vapier | dac6873 | 2009-08-28 12:15:15 +0000 | [diff] [blame] | 211 | tst_resm(TINFO, "got %u and %u", buf.st_gid, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 212 | group2_gid); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 213 | local_flag = FAILED; |
| 214 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 215 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 216 | if (local_flag == PASSED) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 217 | tst_resm(TPASS, "Test passed in block0."); |
| 218 | } else { |
| 219 | tst_resm(TFAIL, "Test failed in block0."); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 220 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 221 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 222 | local_flag = PASSED; |
| 223 | |
| 224 | /*--------------------------------------------------------------*/ |
| 225 | /* Block1: Create two files in testdir.A, one with the setgid */ |
| 226 | /* bit set in the creation modes and the other without. */ |
| 227 | /* Both should inherit the group ID of the process and */ |
| 228 | /* maintain the setgid bit as specified in the creation */ |
| 229 | /* modes. */ |
| 230 | /*--------------------------------------------------------------*/ |
| 231 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 232 | * Now become user1, group1 |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 233 | */ |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 234 | if (setgid(group1_gid) == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 235 | tst_resm(TINFO, |
| 236 | "Unable to set process group ID to group1"); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 237 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 238 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 239 | if (setreuid(-1, user1_uid) == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 240 | tst_resm(TINFO, "Unable to set process uid to user1"); |
| 241 | } |
| 242 | mygid = getgid(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 243 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 244 | /* |
| 245 | * Create the file with setgid not set |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 246 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 247 | fd = open(nosetgid_A, O_CREAT | O_EXCL | O_RDWR, MODE_RWX); |
Michal Simek | cfb6766 | 2011-09-15 13:09:02 +0200 | [diff] [blame] | 248 | if (fd == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 249 | tst_resm(TFAIL, "Creation of %s failed", nosetgid_A); |
| 250 | local_flag = FAILED; |
| 251 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 252 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 253 | if (stat(nosetgid_A, &buf) == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 254 | tst_resm(TFAIL, "Stat of %s failed", nosetgid_A); |
| 255 | local_flag = FAILED; |
| 256 | } |
| 257 | |
| 258 | /* Verify modes */ |
| 259 | if (buf.st_mode & S_ISGID) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 260 | tst_resm(TFAIL, "%s: Incorrect modes, setgid bit set", |
| 261 | nosetgid_A); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 262 | local_flag = FAILED; |
| 263 | } |
| 264 | |
| 265 | /* Verify group ID */ |
| 266 | if (buf.st_gid != mygid) { |
| 267 | tst_resm(TFAIL, "%s: Incorrect group", nosetgid_A); |
| 268 | local_flag = FAILED; |
| 269 | } |
Michal Simek | cfb6766 | 2011-09-15 13:09:02 +0200 | [diff] [blame] | 270 | close(fd); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 271 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 272 | /* |
| 273 | * Create the file with setgid set |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 274 | */ |
Michal Simek | cfb6766 | 2011-09-15 13:09:02 +0200 | [diff] [blame] | 275 | fd = open(setgid_A, O_CREAT | O_EXCL | O_RDWR, MODE_SGID); |
| 276 | if (fd == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 277 | tst_resm(TFAIL, "Creation of %s failed", setgid_A); |
| 278 | local_flag = FAILED; |
| 279 | } |
| 280 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 281 | if (stat(setgid_A, &buf) == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 282 | tst_resm(TFAIL, "Stat of %s failed", setgid_A); |
| 283 | local_flag = FAILED; |
| 284 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 285 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 286 | /* Verify modes */ |
| 287 | if (!(buf.st_mode & S_ISGID)) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 288 | tst_resm(TFAIL, |
| 289 | "%s: Incorrect modes, setgid bit not set", |
| 290 | setgid_A); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 291 | local_flag = FAILED; |
| 292 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 293 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 294 | /* Verify group ID */ |
| 295 | if (buf.st_gid != mygid) { |
| 296 | tst_resm(TFAIL, "%s: Incorrect group", setgid_A); |
vapier | dac6873 | 2009-08-28 12:15:15 +0000 | [diff] [blame] | 297 | tst_resm(TINFO, "got %u and %u", buf.st_gid, mygid); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 298 | local_flag = FAILED; |
| 299 | } |
Michal Simek | cfb6766 | 2011-09-15 13:09:02 +0200 | [diff] [blame] | 300 | close(fd); |
| 301 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 302 | if (local_flag == PASSED) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 303 | tst_resm(TPASS, "Test passed in block1."); |
| 304 | } else { |
| 305 | tst_resm(TFAIL, "Test failed in block1."); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 306 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 307 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 308 | local_flag = PASSED; |
| 309 | |
| 310 | /*--------------------------------------------------------------*/ |
| 311 | /* Block2: Create two files in testdir.B, one with the setgid */ |
| 312 | /* bit set in the creation modes and the other without. */ |
| 313 | /* Both should inherit the group ID of the parent */ |
| 314 | /* directory, group2. */ |
| 315 | /*--------------------------------------------------------------*/ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 316 | /* |
| 317 | * Create the file with setgid not set |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 318 | */ |
Michal Simek | cfb6766 | 2011-09-15 13:09:02 +0200 | [diff] [blame] | 319 | fd = creat(nosetgid_B, MODE_RWX); |
| 320 | if (fd == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 321 | tst_resm(TFAIL, "Creation of %s failed", nosetgid_B); |
| 322 | local_flag = FAILED; |
| 323 | } |
| 324 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 325 | if (stat(nosetgid_B, &buf) == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 326 | tst_resm(TFAIL, "Stat of %s failed", nosetgid_B); |
| 327 | local_flag = FAILED; |
| 328 | } |
| 329 | |
| 330 | /* Verify modes */ |
| 331 | if (buf.st_mode & S_ISGID) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 332 | tst_resm(TFAIL, |
| 333 | "%s: Incorrect modes, setgid bit should not be set", |
| 334 | nosetgid_B); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 335 | local_flag = FAILED; |
| 336 | } |
| 337 | |
| 338 | /* Verify group ID */ |
| 339 | if (buf.st_gid != group2_gid) { |
| 340 | tst_resm(TFAIL, "%s: Incorrect group", nosetgid_B); |
| 341 | local_flag = FAILED; |
| 342 | } |
Michal Simek | cfb6766 | 2011-09-15 13:09:02 +0200 | [diff] [blame] | 343 | close(fd); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 344 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 345 | /* |
| 346 | * Create the file with setgid set |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 347 | */ |
Michal Simek | cfb6766 | 2011-09-15 13:09:02 +0200 | [diff] [blame] | 348 | fd = creat(setgid_B, MODE_SGID); |
| 349 | if (fd == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 350 | tst_resm(TFAIL, "Creation of %s failed", setgid_B); |
| 351 | local_flag = FAILED; |
| 352 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 353 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 354 | if (stat(setgid_B, &buf) == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 355 | tst_resm(TFAIL, "Stat of %s failed", setgid_B); |
| 356 | local_flag = FAILED; |
| 357 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 358 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 359 | /* Verify group ID */ |
| 360 | if (buf.st_gid != group2_gid) { |
| 361 | tst_resm(TFAIL, "%s: Incorrect group", setgid_B); |
vapier | dac6873 | 2009-08-28 12:15:15 +0000 | [diff] [blame] | 362 | tst_resm(TFAIL, "got %u and %u", buf.st_gid, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 363 | group2_gid); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 364 | local_flag = FAILED; |
| 365 | } |
| 366 | |
| 367 | /* Verify modes */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 368 | if (!(buf.st_mode & S_ISGID)) { |
| 369 | tst_resm(TFAIL, |
| 370 | "%s: Incorrect modes, setgid bit should be set", |
| 371 | setgid_B); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 372 | local_flag = FAILED; |
| 373 | } |
Michal Simek | cfb6766 | 2011-09-15 13:09:02 +0200 | [diff] [blame] | 374 | close(fd); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 375 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 376 | if (local_flag == PASSED) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 377 | tst_resm(TPASS, "Test passed in block2."); |
| 378 | } else { |
| 379 | tst_resm(TFAIL, "Test failed in block2."); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 380 | } |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 381 | |
| 382 | local_flag = PASSED; |
| 383 | /*--------------------------------------------------------------*/ |
| 384 | /* Block3: Create a file in testdir.B, with the setgid bit set */ |
| 385 | /* in the creation modes and do so as root. The file */ |
| 386 | /* should inherit the group ID of the parent directory, */ |
| 387 | /* group2 and should have the setgid bit set. */ |
| 388 | /*--------------------------------------------------------------*/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 389 | /* Become root again */ |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 390 | if (setreuid(-1, save_myuid) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 391 | tst_resm(TFAIL | TERRNO, |
| 392 | "Changing back to root failed"); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 393 | local_flag = FAILED; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 394 | } |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 395 | |
| 396 | /* Create the file with setgid set */ |
Michal Simek | cfb6766 | 2011-09-15 13:09:02 +0200 | [diff] [blame] | 397 | fd = creat(root_setgid_B, MODE_SGID); |
| 398 | if (fd == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 399 | tst_resm(TFAIL, "Creation of %s failed", root_setgid_B); |
| 400 | local_flag = FAILED; |
| 401 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 402 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 403 | if (stat(root_setgid_B, &buf) == -1) { |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 404 | tst_resm(TFAIL, "Stat of %s failed", root_setgid_B); |
| 405 | local_flag = FAILED; |
| 406 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 407 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 408 | /* Verify modes */ |
| 409 | if (!(buf.st_mode & S_ISGID)) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 410 | tst_resm(TFAIL, |
| 411 | "%s: Incorrect modes, setgid bit not set", |
| 412 | root_setgid_B); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 413 | local_flag = FAILED; |
| 414 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 415 | |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 416 | /* Verify group ID */ |
| 417 | if (buf.st_gid != group2_gid) { |
| 418 | tst_resm(TFAIL, "%s: Incorrect group", root_setgid_B); |
vapier | dac6873 | 2009-08-28 12:15:15 +0000 | [diff] [blame] | 419 | tst_resm(TINFO, "got %u and %u", buf.st_gid, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 420 | group2_gid); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 421 | local_flag = FAILED; |
| 422 | } |
Michal Simek | cfb6766 | 2011-09-15 13:09:02 +0200 | [diff] [blame] | 423 | close(fd); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 424 | |
| 425 | if (local_flag == PASSED) { |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 426 | tst_resm(TPASS, "Test passed in block3"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 427 | } else { |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 428 | tst_resm(TFAIL, "Test failed in block3"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 429 | } |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 430 | cleanup(); |
| 431 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 432 | tst_exit(); |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Cyril Hrubis | abcd778 | 2012-06-28 17:45:48 +0200 | [diff] [blame] | 435 | static void setup(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 436 | { |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 437 | tst_require_root(NULL); |
Cyril Hrubis | abcd778 | 2012-06-28 17:45:48 +0200 | [diff] [blame] | 438 | tst_tmpdir(); |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 439 | } |
robbiew | 3506401 | 2002-12-30 21:34:26 +0000 | [diff] [blame] | 440 | |
Cyril Hrubis | abcd778 | 2012-06-28 17:45:48 +0200 | [diff] [blame] | 441 | static void cleanup(void) |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 442 | { |
| 443 | if (unlink(setgid_A) == -1) { |
| 444 | tst_resm(TBROK, "%s failed", setgid_A); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 445 | } |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 446 | if (unlink(nosetgid_A) == -1) { |
| 447 | tst_resm(TBROK, "unlink %s failed", nosetgid_A); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 448 | } |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 449 | if (rmdir(DIR_A) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 450 | tst_brkm(TBROK | TERRNO, NULL, "rmdir %s failed", DIR_A); |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 451 | } |
| 452 | if (unlink(setgid_B) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 453 | tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed", setgid_B); |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 454 | } |
| 455 | if (unlink(root_setgid_B) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 456 | tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed", |
| 457 | root_setgid_B); |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 458 | } |
| 459 | if (unlink(nosetgid_B) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 460 | tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed", nosetgid_B); |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 461 | } |
| 462 | if (rmdir(DIR_B) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 463 | tst_brkm(TBROK | TERRNO, NULL, "rmdir %s failed", DIR_B); |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 464 | } |
Cyril Hrubis | abcd778 | 2012-06-28 17:45:48 +0200 | [diff] [blame] | 465 | |
| 466 | tst_rmdir(); |
Salvatore Cro | ea9260d | 2011-11-29 15:54:09 +0100 | [diff] [blame] | 467 | } |