blob: a8f98120dd2f4d02c68be95c05a91df678847645 [file] [log] [blame]
robbiew3a77e6e2003-01-06 20:43:47 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew3a77e6e2003-01-06 20:43:47 +000018 */
19
20/*
Wanlong Gao4d1cf772013-02-04 17:11:18 +080021 * Description:
22 * Verifies that the group ID and setgid bit are
23 * set correctly when a new file is created using open.
robbiew3a77e6e2003-01-06 20:43:47 +000024 *
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_modak4bb656a2009-02-26 12:02:09 +000030 * bit set in the creation modes. Verify that the modes and group
subrata_modakbdbaec52009-02-26 12:14:51 +000031 * ID are correct on each of the 4 files.
robbiew3a77e6e2003-01-06 20:43:47 +000032 * 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.
robbiew3a77e6e2003-01-06 20:43:47 +000035 */
36
Wanlong Gao4d1cf772013-02-04 17:11:18 +080037#include <stdio.h>
robbiew3a77e6e2003-01-06 20:43:47 +000038#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>
robbiew3a77e6e2003-01-06 20:43:47 +000044#include "test.h"
45
46char *TCID = "open10";
47int TST_TOTAL = 1;
Wanlong Gao4d1cf772013-02-04 17:11:18 +080048static int local_flag;
robbiew3a77e6e2003-01-06 20:43:47 +000049
50#define PASSED 1
51#define FAILED 0
52
Wanlong Gao4d1cf772013-02-04 17:11:18 +080053#define MODE_RWX (S_IRWXU | S_IRWXG | S_IRWXO)
54#define MODE_SGID (S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO)
robbiew3a77e6e2003-01-06 20:43:47 +000055#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 Gao4d1cf772013-02-04 17:11:18 +080062static void setup(void);
63static void cleanup(void);
subrata_modakc2e80ed2009-08-22 16:01:09 +000064
subrata_modak56207ce2009-03-23 13:35:39 +000065int main(int ac, char *av[])
robbiew3a77e6e2003-01-06 20:43:47 +000066{
67 int ret;
68 struct stat buf;
69 struct group *group;
70 struct passwd *user1;
subrata_modak56207ce2009-03-23 13:35:39 +000071 char DIR_A[MSGSIZE], DIR_B[MSGSIZE];
robbiew3a77e6e2003-01-06 20:43:47 +000072 char setgid_A[MSGSIZE], nosetgid_A[MSGSIZE];
73 char setgid_B[MSGSIZE], nosetgid_B[MSGSIZE], root_setgid_B[MSGSIZE];
subrata_modak4bb656a2009-02-26 12:02:09 +000074 gid_t group1_gid, group2_gid, mygid;
robbiew3a77e6e2003-01-06 20:43:47 +000075 uid_t save_myuid, user1_uid;
76 pid_t mypid;
77
Cyril Hrubis89af32a2012-10-24 16:39:11 +020078 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020079 const char *msg;
robbiew3a77e6e2003-01-06 20:43:47 +000080 int fail_count = 0;
81
Wanlong Gao4d1cf772013-02-04 17:11:18 +080082 msg = parse_opts(ac, av, NULL, NULL);
83 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080084 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew3a77e6e2003-01-06 20:43:47 +000085
Wanlong Gao4d1cf772013-02-04 17:11:18 +080086 setup();
subrata_modakc2e80ed2009-08-22 16:01:09 +000087
robbiew3a77e6e2003-01-06 20:43:47 +000088 for (lc = 0; TEST_LOOPING(lc); lc++) {
subrata_modak4bb656a2009-02-26 12:02:09 +000089 local_flag = PASSED;
robbiew3a77e6e2003-01-06 20:43:47 +000090
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 Gao4d1cf772013-02-04 17:11:18 +0800102 user1 = getpwnam("nobody");
103 if (user1 == NULL)
subrata_modakc2e80ed2009-08-22 16:01:09 +0000104 tst_brkm(TBROK, cleanup, "nobody not in /etc/passwd");
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800105
robbiew3a77e6e2003-01-06 20:43:47 +0000106 user1_uid = user1->pw_uid;
107
108 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000109 * Get the group IDs of group1 and group2.
robbiew3a77e6e2003-01-06 20:43:47 +0000110 */
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800111 group = getgrnam("nobody");
112 if (group == NULL) {
113 group = getgrnam("nogroup");
114 if (group == NULL) {
subrata_modakc2e80ed2009-08-22 16:01:09 +0000115 tst_brkm(TBROK, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000116 "nobody/nogroup not in /etc/group");
subrata_modak5e2689f2008-09-30 09:06:12 +0000117 }
robbiew3a77e6e2003-01-06 20:43:47 +0000118 }
119 group1_gid = group->gr_gid;
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800120 group = getgrnam("bin");
121 if (group == NULL)
subrata_modakc2e80ed2009-08-22 16:01:09 +0000122 tst_brkm(TBROK, cleanup, "bin not in /etc/group");
robbiew3a77e6e2003-01-06 20:43:47 +0000123
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800124 group2_gid = group->gr_gid;
robbiew3a77e6e2003-01-06 20:43:47 +0000125
126 /*
127 * Create a directory with group id the same as this process
subrata_modak4bb656a2009-02-26 12:02:09 +0000128 * and with no setgid bit.
robbiew3a77e6e2003-01-06 20:43:47 +0000129 */
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800130 if (mkdir(DIR_A, MODE_RWX) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 tst_resm(TFAIL | TERRNO, "mkdir(%s) failed", DIR_A);
robbiew3a77e6e2003-01-06 20:43:47 +0000132 local_flag = FAILED;
133 }
134
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800135 if (chown(DIR_A, user1_uid, group2_gid) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 tst_resm(TFAIL | TERRNO, "chown(%s) failed", DIR_A);
robbiew3a77e6e2003-01-06 20:43:47 +0000137 local_flag = FAILED;
138 }
139
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800140 if (stat(DIR_A, &buf) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 tst_resm(TFAIL | TERRNO, "stat(%s) failed", DIR_A);
robbiew3a77e6e2003-01-06 20:43:47 +0000142 local_flag = FAILED;
143 }
144
145 /* Verify modes */
146 if (buf.st_mode & S_ISGID) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000147 tst_resm(TFAIL, "%s: Incorrect modes, setgid bit set",
subrata_modak56207ce2009-03-23 13:35:39 +0000148 DIR_A);
robbiew3a77e6e2003-01-06 20:43:47 +0000149 local_flag = FAILED;
150 }
151
152 /* Verify group ID */
153 if (buf.st_gid != group2_gid) {
vapierf2ed15b2009-08-28 13:35:01 +0000154 tst_resm(TFAIL, "%s: Incorrect group (got %d and %d)",
155 DIR_A, buf.st_gid, group2_gid);
robbiew3a77e6e2003-01-06 20:43:47 +0000156 local_flag = FAILED;
157 }
158
159 /*
160 * Create a directory with group id different from that of
subrata_modak4bb656a2009-02-26 12:02:09 +0000161 * this process and with the setgid bit set.
robbiew3a77e6e2003-01-06 20:43:47 +0000162 */
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800163 if (mkdir(DIR_B, MODE_RWX) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164 tst_resm(TFAIL | TERRNO, "mkdir(%s) failed", DIR_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000165 local_flag = FAILED;
166 }
167
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800168 if (chown(DIR_B, user1_uid, group2_gid) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 tst_resm(TFAIL | TERRNO, "chown(%s) failed", DIR_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000170 local_flag = FAILED;
171 }
172
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800173 if (chmod(DIR_B, MODE_SGID) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 tst_resm(TFAIL | TERRNO, "chmod(%s) failed", DIR_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000175 local_flag = FAILED;
176 }
177
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800178 if (stat(DIR_B, &buf) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179 tst_resm(TFAIL | TERRNO, "stat(%s) failed", DIR_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000180 local_flag = FAILED;
181 }
182
183 /* Verify modes */
184 if (!(buf.st_mode & S_ISGID)) {
subrata_modak56207ce2009-03-23 13:35:39 +0000185 tst_resm(TFAIL,
186 "%s: Incorrect modes, setgid bit not set",
187 DIR_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000188 local_flag = FAILED;
189 }
190
191 /* Verify group ID */
192 if (buf.st_gid != group2_gid) {
vapierf2ed15b2009-08-28 13:35:01 +0000193 tst_resm(TFAIL, "%s: Incorrect group (got %d and %d)",
194 DIR_B, buf.st_gid, group2_gid);
robbiew3a77e6e2003-01-06 20:43:47 +0000195 local_flag = FAILED;
196 }
197
198 if (local_flag == PASSED) {
subrata_modak56207ce2009-03-23 13:35:39 +0000199 tst_resm(TPASS, "Test passed in block0.");
robbiew3a77e6e2003-01-06 20:43:47 +0000200 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000201 tst_resm(TFAIL, "Test failed in block0.");
202 fail_count++;
203 }
robbiew3a77e6e2003-01-06 20:43:47 +0000204
subrata_modak56207ce2009-03-23 13:35:39 +0000205 local_flag = PASSED;
robbiew3a77e6e2003-01-06 20:43:47 +0000206
robbiew3a77e6e2003-01-06 20:43:47 +0000207 /*
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800208 * 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.
robbiew3a77e6e2003-01-06 20:43:47 +0000213 */
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800214 if (setgid(group1_gid) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800215 tst_resm(TINFO,
216 "Unable to set process group ID to group1");
robbiew3a77e6e2003-01-06 20:43:47 +0000217 }
218
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800219 if (setreuid(-1, user1_uid) < 0)
vapierf2ed15b2009-08-28 13:35:01 +0000220 tst_resm(TINFO, "Unable to set process uid to user1");
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800221
robbiew3a77e6e2003-01-06 20:43:47 +0000222 mygid = getgid();
223
subrata_modak4bb656a2009-02-26 12:02:09 +0000224 /*
225 * Create the file with setgid not set
robbiew3a77e6e2003-01-06 20:43:47 +0000226 */
vapierf2ed15b2009-08-28 13:35:01 +0000227 ret = open(nosetgid_A, O_CREAT | O_EXCL | O_RDWR, MODE_RWX);
228 if (ret < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800229 tst_resm(TFAIL | TERRNO, "open(%s) failed", nosetgid_A);
robbiew3a77e6e2003-01-06 20:43:47 +0000230 local_flag = FAILED;
231 }
Subrata Modak76a720a2010-07-03 21:08:18 +0530232 close(ret);
robbiew3a77e6e2003-01-06 20:43:47 +0000233
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800234 if (stat(nosetgid_A, &buf) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800235 tst_resm(TFAIL | TERRNO, "stat(%s) failed", nosetgid_A);
robbiew3a77e6e2003-01-06 20:43:47 +0000236 local_flag = FAILED;
237 }
238
239 /* Verify modes */
240 if (buf.st_mode & S_ISGID) {
subrata_modak56207ce2009-03-23 13:35:39 +0000241 tst_resm(TFAIL, "%s: Incorrect modes, setgid bit set",
242 nosetgid_A);
robbiew3a77e6e2003-01-06 20:43:47 +0000243 local_flag = FAILED;
244 }
245
246 /* Verify group ID */
247 if (buf.st_gid != mygid) {
vapierf2ed15b2009-08-28 13:35:01 +0000248 tst_resm(TFAIL, "%s: Incorrect group (got %d and %d)",
249 nosetgid_A, buf.st_gid, mygid);
robbiew3a77e6e2003-01-06 20:43:47 +0000250 local_flag = FAILED;
251 }
252
subrata_modak4bb656a2009-02-26 12:02:09 +0000253 /*
254 * Create the file with setgid set
robbiew3a77e6e2003-01-06 20:43:47 +0000255 */
vapierf2ed15b2009-08-28 13:35:01 +0000256 ret = open(setgid_A, O_CREAT | O_EXCL | O_RDWR, MODE_SGID);
257 if (ret < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800258 tst_resm(TFAIL | TERRNO, "open(%s) failed", setgid_A);
robbiew3a77e6e2003-01-06 20:43:47 +0000259 local_flag = FAILED;
260 }
Subrata Modak76a720a2010-07-03 21:08:18 +0530261 close(ret);
robbiew3a77e6e2003-01-06 20:43:47 +0000262
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800263 if (stat(setgid_A, &buf) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800264 tst_resm(TFAIL | TERRNO, "stat(%s) failed", setgid_A);
robbiew3a77e6e2003-01-06 20:43:47 +0000265 local_flag = FAILED;
266 }
267
268 /* Verify modes */
269 if (!(buf.st_mode & S_ISGID)) {
subrata_modak56207ce2009-03-23 13:35:39 +0000270 tst_resm(TFAIL,
271 "%s: Incorrect modes, setgid bit not set",
272 setgid_A);
robbiew3a77e6e2003-01-06 20:43:47 +0000273 local_flag = FAILED;
274 }
275
276 /* Verify group ID */
277 if (buf.st_gid != mygid) {
vapierf2ed15b2009-08-28 13:35:01 +0000278 tst_resm(TFAIL, "%s: Incorrect group (%d and %d)",
279 setgid_A, buf.st_gid, mygid);
robbiew3a77e6e2003-01-06 20:43:47 +0000280 local_flag = FAILED;
281 }
282
283 if (local_flag == PASSED) {
subrata_modak56207ce2009-03-23 13:35:39 +0000284 tst_resm(TPASS, "Test passed in block1.");
robbiew3a77e6e2003-01-06 20:43:47 +0000285 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000286 tst_resm(TFAIL, "Test failed in block1.");
287 fail_count++;
288 }
robbiew3a77e6e2003-01-06 20:43:47 +0000289
subrata_modak56207ce2009-03-23 13:35:39 +0000290 local_flag = PASSED;
robbiew3a77e6e2003-01-06 20:43:47 +0000291
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800292 /*
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_modak4bb656a2009-02-26 12:02:09 +0000299 /*
300 * Create the file with setgid not set
robbiew3a77e6e2003-01-06 20:43:47 +0000301 */
vapierf2ed15b2009-08-28 13:35:01 +0000302 ret = open(nosetgid_B, O_CREAT | O_EXCL | O_RDWR, MODE_RWX);
303 if (ret < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800304 tst_resm(TFAIL | TERRNO, "open(%s) failed", nosetgid_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000305 local_flag = FAILED;
306 }
Subrata Modak76a720a2010-07-03 21:08:18 +0530307 close(ret);
robbiew3a77e6e2003-01-06 20:43:47 +0000308
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800309 if (stat(nosetgid_B, &buf) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800310 tst_resm(TFAIL | TERRNO, "stat(%s) failed", nosetgid_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000311 local_flag = FAILED;
312 }
313
314 /* Verify modes */
315 if (buf.st_mode & S_ISGID) {
subrata_modak56207ce2009-03-23 13:35:39 +0000316 tst_resm(TFAIL,
317 "%s: Incorrect modes, setgid bit should be set",
318 nosetgid_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000319 local_flag = FAILED;
320 }
321
322 /* Verify group ID */
323 if (buf.st_gid != group2_gid) {
vapierf2ed15b2009-08-28 13:35:01 +0000324 tst_resm(TFAIL, "%s: Incorrect group (got %d and %d)",
325 nosetgid_B, buf.st_gid, group2_gid);
robbiew3a77e6e2003-01-06 20:43:47 +0000326 local_flag = FAILED;
327 }
328
subrata_modak4bb656a2009-02-26 12:02:09 +0000329 /*
330 * Create the file with setgid set
robbiew3a77e6e2003-01-06 20:43:47 +0000331 */
vapierf2ed15b2009-08-28 13:35:01 +0000332 ret = open(setgid_B, O_CREAT | O_EXCL | O_RDWR, MODE_SGID);
333 if (ret < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800334 tst_resm(TFAIL | TERRNO, "open(%s) failed", setgid_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000335 local_flag = FAILED;
336 }
Subrata Modak76a720a2010-07-03 21:08:18 +0530337 close(ret);
robbiew3a77e6e2003-01-06 20:43:47 +0000338
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800339 if (stat(setgid_B, &buf) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800340 tst_resm(TFAIL | TERRNO, "stat(%s) failed", setgid_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000341 local_flag = FAILED;
342 }
343
344 /* Verify group ID */
345 if (buf.st_gid != group2_gid) {
vapierf2ed15b2009-08-28 13:35:01 +0000346 tst_resm(TFAIL, "%s: Incorrect group (got %d and %d)",
347 setgid_B, buf.st_gid, group2_gid);
robbiew3a77e6e2003-01-06 20:43:47 +0000348 local_flag = FAILED;
349 }
350
351 /* Verify modes */
352 if (!(buf.st_mode & S_ISGID)) {
subrata_modak56207ce2009-03-23 13:35:39 +0000353 tst_resm(TFAIL,
354 "%s: Incorrect modes, setgid bit not set",
355 setgid_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000356 local_flag = FAILED;
357 }
358
359 if (local_flag == PASSED) {
subrata_modak56207ce2009-03-23 13:35:39 +0000360 tst_resm(TPASS, "Test passed in block2.");
robbiew3a77e6e2003-01-06 20:43:47 +0000361 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000362 tst_resm(TFAIL, "Test failed in block2.");
363 fail_count++;
364 }
robbiew3a77e6e2003-01-06 20:43:47 +0000365
subrata_modak56207ce2009-03-23 13:35:39 +0000366 local_flag = PASSED;
robbiew3a77e6e2003-01-06 20:43:47 +0000367
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800368 /*
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 */
robbiew3a77e6e2003-01-06 20:43:47 +0000374
375 /* Become root again */
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800376 if (setreuid(-1, save_myuid) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800377 tst_resm(TFAIL | TERRNO,
378 "Changing back to root failed");
robbiew3a77e6e2003-01-06 20:43:47 +0000379 local_flag = FAILED;
380 }
381
382 /* Create the file with setgid set */
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800383 ret = open(root_setgid_B, O_CREAT | O_EXCL | O_RDWR, MODE_SGID);
384 if (ret < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800385 tst_resm(TFAIL | TERRNO, "open(%s) failed",
386 root_setgid_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000387 local_flag = FAILED;
388 }
Subrata Modak76a720a2010-07-03 21:08:18 +0530389 close(ret);
robbiew3a77e6e2003-01-06 20:43:47 +0000390
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800391 if (stat(root_setgid_B, &buf) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800392 tst_resm(TFAIL | TERRNO, "stat(%s) failed",
393 root_setgid_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000394 local_flag = FAILED;
395 }
396
397 /* Verify modes */
398 if (!(buf.st_mode & S_ISGID)) {
subrata_modak56207ce2009-03-23 13:35:39 +0000399 tst_resm(TFAIL,
400 "%s: Incorrect modes, setgid bit not set",
401 root_setgid_B);
robbiew3a77e6e2003-01-06 20:43:47 +0000402 local_flag = FAILED;
403 }
404
405 /* Verify group ID */
406 if (buf.st_gid != group2_gid) {
vapierf2ed15b2009-08-28 13:35:01 +0000407 tst_resm(TFAIL, "%s: Incorrect group (got %d and %d)",
408 root_setgid_B, buf.st_gid, group2_gid);
robbiew3a77e6e2003-01-06 20:43:47 +0000409 local_flag = FAILED;
410 }
411
412 if (local_flag == PASSED) {
subrata_modak56207ce2009-03-23 13:35:39 +0000413 tst_resm(TPASS, "Test passed in block3.");
robbiew3a77e6e2003-01-06 20:43:47 +0000414 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000415 tst_resm(TFAIL, "Test failed in block3.");
416 fail_count++;
417 }
robbiew3a77e6e2003-01-06 20:43:47 +0000418
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800419 /*
420 * Clean up any files created by test before call to anyfail.
421 * Remove the directories.
422 */
423 if (unlink(setgid_A) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800424 tst_resm(TWARN | TERRNO, "unlink(%s) failed", setgid_A);
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800425 if (unlink(nosetgid_A) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800426 tst_resm(TWARN | TERRNO, "unlink(%s) failed",
427 nosetgid_A);
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800428 if (rmdir(DIR_A) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800429 tst_resm(TWARN | TERRNO, "rmdir(%s) failed", DIR_A);
robbiew3a77e6e2003-01-06 20:43:47 +0000430
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800431 if (unlink(setgid_B) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800432 tst_resm(TWARN | TERRNO, "unlink(%s) failed", setgid_B);
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800433 if (unlink(root_setgid_B) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800434 tst_resm(TWARN | TERRNO, "unlink(%s) failed",
435 root_setgid_B);
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800436 if (unlink(nosetgid_B) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800437 tst_resm(TWARN | TERRNO, "unlink(%s) failed",
438 nosetgid_B);
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800439 if (rmdir(DIR_B) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800440 tst_resm(TWARN | TERRNO, "rmdir(%s) failed", DIR_B);
vapierf2ed15b2009-08-28 13:35:01 +0000441
subrata_modak56207ce2009-03-23 13:35:39 +0000442 if (fail_count == 0) {
443 tst_resm(TPASS, "Test passed.");
robbiew3a77e6e2003-01-06 20:43:47 +0000444 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000445 tst_resm(TFAIL,
446 "Test failed because of above failures.");
robbiew3a77e6e2003-01-06 20:43:47 +0000447 }
448
449 }
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800450
subrata_modakc2e80ed2009-08-22 16:01:09 +0000451 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800452 tst_exit();
robbiew3a77e6e2003-01-06 20:43:47 +0000453}
subrata_modakc2e80ed2009-08-22 16:01:09 +0000454
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800455static void setup(void)
subrata_modakc2e80ed2009-08-22 16:01:09 +0000456{
Wanlong Gao4d1cf772013-02-04 17:11:18 +0800457 tst_require_root(NULL);
458 tst_sig(NOFORK, DEF_HANDLER, cleanup);
459 TEST_PAUSE;
460 tst_tmpdir();
461}
462
463static void cleanup(void)
464{
subrata_modakc2e80ed2009-08-22 16:01:09 +0000465 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700466}