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