blob: 0b3f981b24ec1b789e92c7b305751292a2b27cff [file] [log] [blame]
robbiew35064012002-12-30 21:34:26 +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
robbiew35064012002-12-30 21:34:26 +000018 */
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_modak4bb656a2009-02-26 12:02:09 +000024 * (ported from SPIE, section2/iosuite/creat5.c,
robbiew35064012002-12-30 21:34:26 +000025 * 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_modak4bb656a2009-02-26 12:02:09 +000034 * bit set in the creation modes. Verify that the modes and group
subrata_modakbdbaec52009-02-26 12:14:51 +000035 * ID are correct on each of the 4 files.
robbiew35064012002-12-30 21:34:26 +000036 * 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_modak56207ce2009-03-23 13:35:39 +000047#include <stdio.h> /* needed by testhead.h */
robbiew35064012002-12-30 21:34:26 +000048#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"
robbiew35064012002-12-30 21:34:26 +000055
56char *TCID = "creat08";
57int TST_TOTAL = 1;
subrata_modak56207ce2009-03-23 13:35:39 +000058int local_flag;
robbiew35064012002-12-30 21:34:26 +000059
60#define PASSED 1
61#define FAILED 0
62
Garrett Cooper87bc45c2010-12-17 01:56:08 -080063#define MODE_RWX (S_IRWXU|S_IRWXG|S_IRWXO)
64#define MODE_SGID (S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
robbiew35064012002-12-30 21:34:26 +000065#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 Cooper87bc45c2010-12-17 01:56:08 -080072static void cleanup(void);
73static void setup(void);
robbiew35064012002-12-30 21:34:26 +000074
Garrett Cooper87bc45c2010-12-17 01:56:08 -080075static char DIR_A[MSGSIZE], DIR_B[MSGSIZE];
76static char setgid_A[MSGSIZE], nosetgid_A[MSGSIZE];
77static char setgid_B[MSGSIZE], nosetgid_B[MSGSIZE], root_setgid_B[MSGSIZE];
78
subrata_modak56207ce2009-03-23 13:35:39 +000079int main(int ac, char **av)
robbiew35064012002-12-30 21:34:26 +000080{
robbiew35064012002-12-30 21:34:26 +000081 struct stat buf;
82 struct group *group;
83 struct passwd *user1;
subrata_modak4bb656a2009-02-26 12:02:09 +000084 gid_t group1_gid, group2_gid, mygid;
robbiew35064012002-12-30 21:34:26 +000085 uid_t save_myuid, user1_uid;
86 pid_t mypid;
87
Michal Simekcfb67662011-09-15 13:09:02 +020088 int fd;
Cyril Hrubis89af32a2012-10-24 16:39:11 +020089 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020090 const char *msg;
robbiew35064012002-12-30 21:34:26 +000091
subrata_modak56207ce2009-03-23 13:35:39 +000092 /*
93 * parse standard options
94 */
Garrett Cooper87bc45c2010-12-17 01:56:08 -080095 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
96 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew35064012002-12-30 21:34:26 +000097
Garrett Cooper87bc45c2010-12-17 01:56:08 -080098 setup();
robbiew35064012002-12-30 21:34:26 +000099
robbiew35064012002-12-30 21:34:26 +0000100 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_modakbdbaec52009-02-26 12:14:51 +0000113
robbiew35064012002-12-30 21:34:26 +0000114 /* Get the uid of user1 */
robbiew977f57c2002-12-31 21:49:10 +0000115 if ((user1 = getpwnam("nobody")) == NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800116 tst_brkm(TBROK | TERRNO, NULL,
117 "getpwnam(\"nobody\") failed");
robbiew35064012002-12-30 21:34:26 +0000118 }
119
120 user1_uid = user1->pw_uid;
subrata_modakbdbaec52009-02-26 12:14:51 +0000121
robbiew35064012002-12-30 21:34:26 +0000122 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000123 * Get the group IDs of group1 and group2.
robbiew35064012002-12-30 21:34:26 +0000124 */
robbiew977f57c2002-12-31 21:49:10 +0000125 if ((group = getgrnam("nobody")) == NULL) {
Salvatore Croea9260d2011-11-29 15:54:09 +0100126 if ((group = getgrnam("nogroup")) == NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 tst_brkm(TBROK | TERRNO, cleanup,
128 "getgrnam(\"nobody\") and "
129 "getgrnam(\"nogroup\") failed");
Salvatore Croea9260d2011-11-29 15:54:09 +0100130 }
robbiew35064012002-12-30 21:34:26 +0000131 }
132 group1_gid = group->gr_gid;
robbiew977f57c2002-12-31 21:49:10 +0000133 if ((group = getgrnam("bin")) == NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134 tst_brkm(TBROK | TERRNO, cleanup,
135 "getgrnam(\"bin\") failed");
robbiew35064012002-12-30 21:34:26 +0000136 }
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_modak4bb656a2009-02-26 12:02:09 +0000144 * and with no setgid bit.
robbiew35064012002-12-30 21:34:26 +0000145 */
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800146 if (mkdir(DIR_A, MODE_RWX) == -1) {
robbiew35064012002-12-30 21:34:26 +0000147 tst_resm(TFAIL, "Creation of %s failed", DIR_A);
148 local_flag = FAILED;
149 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000150
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800151 if (chown(DIR_A, user1_uid, group2_gid) == -1) {
robbiew35064012002-12-30 21:34:26 +0000152 tst_resm(TFAIL, "Chown of %s failed", DIR_A);
153 local_flag = FAILED;
154 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000155
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800156 if (stat(DIR_A, &buf) == -1) {
robbiew35064012002-12-30 21:34:26 +0000157 tst_resm(TFAIL, "Stat of %s failed", DIR_A);
158 local_flag = FAILED;
159 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000160
robbiew35064012002-12-30 21:34:26 +0000161 /* Verify modes */
162 if (buf.st_mode & S_ISGID) {
subrata_modak56207ce2009-03-23 13:35:39 +0000163 tst_resm(TFAIL, "%s: Incorrect modes, setgid bit set",
164 DIR_A);
robbiew35064012002-12-30 21:34:26 +0000165 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);
vapierdac68732009-08-28 12:15:15 +0000171 tst_resm(TINFO, "got %u and %u", buf.st_gid,
subrata_modak56207ce2009-03-23 13:35:39 +0000172 group2_gid);
robbiew35064012002-12-30 21:34:26 +0000173 local_flag = FAILED;
174 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000175
robbiew35064012002-12-30 21:34:26 +0000176 /*
177 * Create a directory with group id different from that of
subrata_modak4bb656a2009-02-26 12:02:09 +0000178 * this process and with the setgid bit set.
robbiew35064012002-12-30 21:34:26 +0000179 */
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800180 if (mkdir(DIR_B, MODE_RWX) == -1) {
robbiew35064012002-12-30 21:34:26 +0000181 tst_resm(TFAIL, "Creation of %s failed", DIR_B);
182 local_flag = FAILED;
183 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000184
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800185 if (chown(DIR_B, user1_uid, group2_gid) == -1) {
robbiew35064012002-12-30 21:34:26 +0000186 tst_resm(TFAIL, "Chown of %s failed", DIR_B);
187 local_flag = FAILED;
188 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000189
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800190 if (chmod(DIR_B, MODE_SGID) == -1) {
robbiew35064012002-12-30 21:34:26 +0000191 tst_resm(TFAIL, "Chmod of %s failed", DIR_B);
192 local_flag = FAILED;
193 }
194
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800195 if (stat(DIR_B, &buf) == -1) {
robbiew35064012002-12-30 21:34:26 +0000196 tst_resm(TFAIL, "Stat of %s failed", DIR_B);
197 local_flag = FAILED;
198 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000199
robbiew35064012002-12-30 21:34:26 +0000200 /* Verify modes */
201 if (!(buf.st_mode & S_ISGID)) {
subrata_modak56207ce2009-03-23 13:35:39 +0000202 tst_resm(TFAIL,
203 "%s: Incorrect modes, setgid bit not set",
204 DIR_B);
robbiew35064012002-12-30 21:34:26 +0000205 local_flag = FAILED;
206 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000207
robbiew35064012002-12-30 21:34:26 +0000208 /* Verify group ID */
209 if (buf.st_gid != group2_gid) {
210 tst_resm(TFAIL, "%s: Incorrect group", DIR_B);
vapierdac68732009-08-28 12:15:15 +0000211 tst_resm(TINFO, "got %u and %u", buf.st_gid,
subrata_modak56207ce2009-03-23 13:35:39 +0000212 group2_gid);
robbiew35064012002-12-30 21:34:26 +0000213 local_flag = FAILED;
214 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000215
robbiew35064012002-12-30 21:34:26 +0000216 if (local_flag == PASSED) {
subrata_modak56207ce2009-03-23 13:35:39 +0000217 tst_resm(TPASS, "Test passed in block0.");
218 } else {
219 tst_resm(TFAIL, "Test failed in block0.");
subrata_modak56207ce2009-03-23 13:35:39 +0000220 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000221
robbiew35064012002-12-30 21:34:26 +0000222 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_modak4bb656a2009-02-26 12:02:09 +0000232 * Now become user1, group1
robbiew35064012002-12-30 21:34:26 +0000233 */
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800234 if (setgid(group1_gid) == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000235 tst_resm(TINFO,
236 "Unable to set process group ID to group1");
robbiew35064012002-12-30 21:34:26 +0000237 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000238
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800239 if (setreuid(-1, user1_uid) == -1) {
robbiew35064012002-12-30 21:34:26 +0000240 tst_resm(TINFO, "Unable to set process uid to user1");
241 }
242 mygid = getgid();
subrata_modakbdbaec52009-02-26 12:14:51 +0000243
subrata_modak4bb656a2009-02-26 12:02:09 +0000244 /*
245 * Create the file with setgid not set
robbiew35064012002-12-30 21:34:26 +0000246 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800247 fd = open(nosetgid_A, O_CREAT | O_EXCL | O_RDWR, MODE_RWX);
Michal Simekcfb67662011-09-15 13:09:02 +0200248 if (fd == -1) {
robbiew35064012002-12-30 21:34:26 +0000249 tst_resm(TFAIL, "Creation of %s failed", nosetgid_A);
250 local_flag = FAILED;
251 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000252
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800253 if (stat(nosetgid_A, &buf) == -1) {
robbiew35064012002-12-30 21:34:26 +0000254 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_modak56207ce2009-03-23 13:35:39 +0000260 tst_resm(TFAIL, "%s: Incorrect modes, setgid bit set",
261 nosetgid_A);
robbiew35064012002-12-30 21:34:26 +0000262 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 Simekcfb67662011-09-15 13:09:02 +0200270 close(fd);
robbiew35064012002-12-30 21:34:26 +0000271
subrata_modak4bb656a2009-02-26 12:02:09 +0000272 /*
273 * Create the file with setgid set
robbiew35064012002-12-30 21:34:26 +0000274 */
Michal Simekcfb67662011-09-15 13:09:02 +0200275 fd = open(setgid_A, O_CREAT | O_EXCL | O_RDWR, MODE_SGID);
276 if (fd == -1) {
robbiew35064012002-12-30 21:34:26 +0000277 tst_resm(TFAIL, "Creation of %s failed", setgid_A);
278 local_flag = FAILED;
279 }
280
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800281 if (stat(setgid_A, &buf) == -1) {
robbiew35064012002-12-30 21:34:26 +0000282 tst_resm(TFAIL, "Stat of %s failed", setgid_A);
283 local_flag = FAILED;
284 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000285
robbiew35064012002-12-30 21:34:26 +0000286 /* Verify modes */
287 if (!(buf.st_mode & S_ISGID)) {
subrata_modak56207ce2009-03-23 13:35:39 +0000288 tst_resm(TFAIL,
289 "%s: Incorrect modes, setgid bit not set",
290 setgid_A);
robbiew35064012002-12-30 21:34:26 +0000291 local_flag = FAILED;
292 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000293
robbiew35064012002-12-30 21:34:26 +0000294 /* Verify group ID */
295 if (buf.st_gid != mygid) {
296 tst_resm(TFAIL, "%s: Incorrect group", setgid_A);
vapierdac68732009-08-28 12:15:15 +0000297 tst_resm(TINFO, "got %u and %u", buf.st_gid, mygid);
robbiew35064012002-12-30 21:34:26 +0000298 local_flag = FAILED;
299 }
Michal Simekcfb67662011-09-15 13:09:02 +0200300 close(fd);
301
robbiew35064012002-12-30 21:34:26 +0000302 if (local_flag == PASSED) {
subrata_modak56207ce2009-03-23 13:35:39 +0000303 tst_resm(TPASS, "Test passed in block1.");
304 } else {
305 tst_resm(TFAIL, "Test failed in block1.");
subrata_modak56207ce2009-03-23 13:35:39 +0000306 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000307
robbiew35064012002-12-30 21:34:26 +0000308 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_modak4bb656a2009-02-26 12:02:09 +0000316 /*
317 * Create the file with setgid not set
robbiew35064012002-12-30 21:34:26 +0000318 */
Michal Simekcfb67662011-09-15 13:09:02 +0200319 fd = creat(nosetgid_B, MODE_RWX);
320 if (fd == -1) {
robbiew35064012002-12-30 21:34:26 +0000321 tst_resm(TFAIL, "Creation of %s failed", nosetgid_B);
322 local_flag = FAILED;
323 }
324
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800325 if (stat(nosetgid_B, &buf) == -1) {
robbiew35064012002-12-30 21:34:26 +0000326 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_modak56207ce2009-03-23 13:35:39 +0000332 tst_resm(TFAIL,
333 "%s: Incorrect modes, setgid bit should not be set",
334 nosetgid_B);
robbiew35064012002-12-30 21:34:26 +0000335 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 Simekcfb67662011-09-15 13:09:02 +0200343 close(fd);
subrata_modakbdbaec52009-02-26 12:14:51 +0000344
subrata_modak4bb656a2009-02-26 12:02:09 +0000345 /*
346 * Create the file with setgid set
robbiew35064012002-12-30 21:34:26 +0000347 */
Michal Simekcfb67662011-09-15 13:09:02 +0200348 fd = creat(setgid_B, MODE_SGID);
349 if (fd == -1) {
robbiew35064012002-12-30 21:34:26 +0000350 tst_resm(TFAIL, "Creation of %s failed", setgid_B);
351 local_flag = FAILED;
352 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000353
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800354 if (stat(setgid_B, &buf) == -1) {
robbiew35064012002-12-30 21:34:26 +0000355 tst_resm(TFAIL, "Stat of %s failed", setgid_B);
356 local_flag = FAILED;
357 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000358
robbiew35064012002-12-30 21:34:26 +0000359 /* Verify group ID */
360 if (buf.st_gid != group2_gid) {
361 tst_resm(TFAIL, "%s: Incorrect group", setgid_B);
vapierdac68732009-08-28 12:15:15 +0000362 tst_resm(TFAIL, "got %u and %u", buf.st_gid,
subrata_modak56207ce2009-03-23 13:35:39 +0000363 group2_gid);
robbiew35064012002-12-30 21:34:26 +0000364 local_flag = FAILED;
365 }
366
367 /* Verify modes */
subrata_modak56207ce2009-03-23 13:35:39 +0000368 if (!(buf.st_mode & S_ISGID)) {
369 tst_resm(TFAIL,
370 "%s: Incorrect modes, setgid bit should be set",
371 setgid_B);
robbiew35064012002-12-30 21:34:26 +0000372 local_flag = FAILED;
373 }
Michal Simekcfb67662011-09-15 13:09:02 +0200374 close(fd);
subrata_modakbdbaec52009-02-26 12:14:51 +0000375
robbiew35064012002-12-30 21:34:26 +0000376 if (local_flag == PASSED) {
subrata_modak56207ce2009-03-23 13:35:39 +0000377 tst_resm(TPASS, "Test passed in block2.");
378 } else {
379 tst_resm(TFAIL, "Test failed in block2.");
subrata_modak56207ce2009-03-23 13:35:39 +0000380 }
robbiew35064012002-12-30 21:34:26 +0000381
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_modak56207ce2009-03-23 13:35:39 +0000389 /* Become root again */
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800390 if (setreuid(-1, save_myuid) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800391 tst_resm(TFAIL | TERRNO,
392 "Changing back to root failed");
robbiew35064012002-12-30 21:34:26 +0000393 local_flag = FAILED;
subrata_modak56207ce2009-03-23 13:35:39 +0000394 }
robbiew35064012002-12-30 21:34:26 +0000395
396 /* Create the file with setgid set */
Michal Simekcfb67662011-09-15 13:09:02 +0200397 fd = creat(root_setgid_B, MODE_SGID);
398 if (fd == -1) {
robbiew35064012002-12-30 21:34:26 +0000399 tst_resm(TFAIL, "Creation of %s failed", root_setgid_B);
400 local_flag = FAILED;
401 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000402
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800403 if (stat(root_setgid_B, &buf) == -1) {
robbiew35064012002-12-30 21:34:26 +0000404 tst_resm(TFAIL, "Stat of %s failed", root_setgid_B);
405 local_flag = FAILED;
406 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000407
robbiew35064012002-12-30 21:34:26 +0000408 /* Verify modes */
409 if (!(buf.st_mode & S_ISGID)) {
subrata_modak56207ce2009-03-23 13:35:39 +0000410 tst_resm(TFAIL,
411 "%s: Incorrect modes, setgid bit not set",
412 root_setgid_B);
robbiew35064012002-12-30 21:34:26 +0000413 local_flag = FAILED;
414 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000415
robbiew35064012002-12-30 21:34:26 +0000416 /* Verify group ID */
417 if (buf.st_gid != group2_gid) {
418 tst_resm(TFAIL, "%s: Incorrect group", root_setgid_B);
vapierdac68732009-08-28 12:15:15 +0000419 tst_resm(TINFO, "got %u and %u", buf.st_gid,
subrata_modak56207ce2009-03-23 13:35:39 +0000420 group2_gid);
robbiew35064012002-12-30 21:34:26 +0000421 local_flag = FAILED;
422 }
Michal Simekcfb67662011-09-15 13:09:02 +0200423 close(fd);
robbiew35064012002-12-30 21:34:26 +0000424
425 if (local_flag == PASSED) {
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800426 tst_resm(TPASS, "Test passed in block3");
subrata_modak56207ce2009-03-23 13:35:39 +0000427 } else {
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800428 tst_resm(TFAIL, "Test failed in block3");
subrata_modak56207ce2009-03-23 13:35:39 +0000429 }
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800430 cleanup();
431 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800432 tst_exit();
robbiew35064012002-12-30 21:34:26 +0000433}
434
Cyril Hrubisabcd7782012-06-28 17:45:48 +0200435static void setup(void)
subrata_modak56207ce2009-03-23 13:35:39 +0000436{
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800437 tst_require_root(NULL);
Cyril Hrubisabcd7782012-06-28 17:45:48 +0200438 tst_tmpdir();
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800439}
robbiew35064012002-12-30 21:34:26 +0000440
Cyril Hrubisabcd7782012-06-28 17:45:48 +0200441static void cleanup(void)
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800442{
443 if (unlink(setgid_A) == -1) {
444 tst_resm(TBROK, "%s failed", setgid_A);
subrata_modak56207ce2009-03-23 13:35:39 +0000445 }
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800446 if (unlink(nosetgid_A) == -1) {
447 tst_resm(TBROK, "unlink %s failed", nosetgid_A);
subrata_modak56207ce2009-03-23 13:35:39 +0000448 }
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800449 if (rmdir(DIR_A) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800450 tst_brkm(TBROK | TERRNO, NULL, "rmdir %s failed", DIR_A);
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800451 }
452 if (unlink(setgid_B) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800453 tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed", setgid_B);
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800454 }
455 if (unlink(root_setgid_B) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800456 tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed",
457 root_setgid_B);
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800458 }
459 if (unlink(nosetgid_B) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800460 tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed", nosetgid_B);
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800461 }
462 if (rmdir(DIR_B) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800463 tst_brkm(TBROK | TERRNO, NULL, "rmdir %s failed", DIR_B);
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800464 }
Cyril Hrubisabcd7782012-06-28 17:45:48 +0200465
466 tst_rmdir();
Salvatore Croea9260d2011-11-29 15:54:09 +0100467}