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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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.
24 * (ported from SPIE, section2/iosuite/creat5.c,
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
34 * bit set in the creation modes. Verify that the modes and group
35 * ID are correct on each of the 4 files.
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
47 #include <stdio.h> /* needed by testhead.h */
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"
55
56 char *TCID = "creat08";
57 int TST_TOTAL = 1;
58 int local_flag;
59
60 #define PASSED 1
61 #define FAILED 0
62
63 #define MODE_RWX (S_IRWXU|S_IRWXG|S_IRWXO)
64 #define MODE_SGID (S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
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
72 static void cleanup(void);
73 static void setup(void);
74
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
main(int ac,char ** av)79 int main(int ac, char **av)
80 {
81 struct stat buf;
82 struct group *group;
83 struct passwd *user1;
84 gid_t group1_gid, group2_gid, mygid;
85 uid_t save_myuid, user1_uid;
86 pid_t mypid;
87
88 int fd;
89 int lc;
90
91 tst_parse_opts(ac, av, NULL, NULL);
92
93 setup();
94
95 for (lc = 0; TEST_LOOPING(lc); lc++) {
96
97 local_flag = PASSED;
98
99 save_myuid = getuid();
100 mypid = getpid();
101 sprintf(DIR_A, DIR_A_TEMP, mypid);
102 sprintf(DIR_B, DIR_B_TEMP, mypid);
103 sprintf(setgid_A, "%s/%s", DIR_A, SETGID);
104 sprintf(nosetgid_A, "%s/%s", DIR_A, NOSETGID);
105 sprintf(setgid_B, "%s/%s", DIR_B, SETGID);
106 sprintf(nosetgid_B, "%s/%s", DIR_B, NOSETGID);
107 sprintf(root_setgid_B, "%s/%s", DIR_B, ROOT_SETGID);
108
109 /* Get the uid of user1 */
110 if ((user1 = getpwnam("nobody")) == NULL) {
111 tst_brkm(TBROK | TERRNO, NULL,
112 "getpwnam(\"nobody\") failed");
113 }
114
115 user1_uid = user1->pw_uid;
116
117 /*
118 * Get the group IDs of group1 and group2.
119 */
120 if ((group = getgrnam("nobody")) == NULL) {
121 if ((group = getgrnam("nogroup")) == NULL) {
122 tst_brkm(TBROK | TERRNO, cleanup,
123 "getgrnam(\"nobody\") and "
124 "getgrnam(\"nogroup\") failed");
125 }
126 }
127 group1_gid = group->gr_gid;
128 if ((group = getgrnam("bin")) == NULL) {
129 tst_brkm(TBROK | TERRNO, cleanup,
130 "getgrnam(\"bin\") failed");
131 }
132 group2_gid = group->gr_gid;
133
134 /*--------------------------------------------------------------*/
135 /* Block0: Set up the parent directories */
136 /*--------------------------------------------------------------*/
137 /*
138 * Create a directory with group id the same as this process
139 * and with no setgid bit.
140 */
141 if (mkdir(DIR_A, MODE_RWX) == -1) {
142 tst_resm(TFAIL, "Creation of %s failed", DIR_A);
143 local_flag = FAILED;
144 }
145
146 if (chown(DIR_A, user1_uid, group2_gid) == -1) {
147 tst_resm(TFAIL, "Chown of %s failed", DIR_A);
148 local_flag = FAILED;
149 }
150
151 if (stat(DIR_A, &buf) == -1) {
152 tst_resm(TFAIL, "Stat of %s failed", DIR_A);
153 local_flag = FAILED;
154 }
155
156 /* Verify modes */
157 if (buf.st_mode & S_ISGID) {
158 tst_resm(TFAIL, "%s: Incorrect modes, setgid bit set",
159 DIR_A);
160 local_flag = FAILED;
161 }
162
163 /* Verify group ID */
164 if (buf.st_gid != group2_gid) {
165 tst_resm(TFAIL, "%s: Incorrect group", DIR_A);
166 tst_resm(TINFO, "got %u and %u", buf.st_gid,
167 group2_gid);
168 local_flag = FAILED;
169 }
170
171 /*
172 * Create a directory with group id different from that of
173 * this process and with the setgid bit set.
174 */
175 if (mkdir(DIR_B, MODE_RWX) == -1) {
176 tst_resm(TFAIL, "Creation of %s failed", DIR_B);
177 local_flag = FAILED;
178 }
179
180 if (chown(DIR_B, user1_uid, group2_gid) == -1) {
181 tst_resm(TFAIL, "Chown of %s failed", DIR_B);
182 local_flag = FAILED;
183 }
184
185 if (chmod(DIR_B, MODE_SGID) == -1) {
186 tst_resm(TFAIL, "Chmod of %s failed", DIR_B);
187 local_flag = FAILED;
188 }
189
190 if (stat(DIR_B, &buf) == -1) {
191 tst_resm(TFAIL, "Stat of %s failed", DIR_B);
192 local_flag = FAILED;
193 }
194
195 /* Verify modes */
196 if (!(buf.st_mode & S_ISGID)) {
197 tst_resm(TFAIL,
198 "%s: Incorrect modes, setgid bit not set",
199 DIR_B);
200 local_flag = FAILED;
201 }
202
203 /* Verify group ID */
204 if (buf.st_gid != group2_gid) {
205 tst_resm(TFAIL, "%s: Incorrect group", DIR_B);
206 tst_resm(TINFO, "got %u and %u", buf.st_gid,
207 group2_gid);
208 local_flag = FAILED;
209 }
210
211 if (local_flag == PASSED) {
212 tst_resm(TPASS, "Test passed in block0.");
213 } else {
214 tst_resm(TFAIL, "Test failed in block0.");
215 }
216
217 local_flag = PASSED;
218
219 /*--------------------------------------------------------------*/
220 /* Block1: Create two files in testdir.A, one with the setgid */
221 /* bit set in the creation modes and the other without. */
222 /* Both should inherit the group ID of the process and */
223 /* maintain the setgid bit as specified in the creation */
224 /* modes. */
225 /*--------------------------------------------------------------*/
226 /*
227 * Now become user1, group1
228 */
229 if (setgid(group1_gid) == -1) {
230 tst_resm(TINFO,
231 "Unable to set process group ID to group1");
232 }
233
234 if (setreuid(-1, user1_uid) == -1) {
235 tst_resm(TINFO, "Unable to set process uid to user1");
236 }
237 mygid = getgid();
238
239 /*
240 * Create the file with setgid not set
241 */
242 fd = open(nosetgid_A, O_CREAT | O_EXCL | O_RDWR, MODE_RWX);
243 if (fd == -1) {
244 tst_resm(TFAIL, "Creation of %s failed", nosetgid_A);
245 local_flag = FAILED;
246 }
247
248 if (stat(nosetgid_A, &buf) == -1) {
249 tst_resm(TFAIL, "Stat of %s failed", nosetgid_A);
250 local_flag = FAILED;
251 }
252
253 /* Verify modes */
254 if (buf.st_mode & S_ISGID) {
255 tst_resm(TFAIL, "%s: Incorrect modes, setgid bit set",
256 nosetgid_A);
257 local_flag = FAILED;
258 }
259
260 /* Verify group ID */
261 if (buf.st_gid != mygid) {
262 tst_resm(TFAIL, "%s: Incorrect group", nosetgid_A);
263 local_flag = FAILED;
264 }
265 close(fd);
266
267 /*
268 * Create the file with setgid set
269 */
270 fd = open(setgid_A, O_CREAT | O_EXCL | O_RDWR, MODE_SGID);
271 if (fd == -1) {
272 tst_resm(TFAIL, "Creation of %s failed", setgid_A);
273 local_flag = FAILED;
274 }
275
276 if (stat(setgid_A, &buf) == -1) {
277 tst_resm(TFAIL, "Stat of %s failed", setgid_A);
278 local_flag = FAILED;
279 }
280
281 /* Verify modes */
282 if (!(buf.st_mode & S_ISGID)) {
283 tst_resm(TFAIL,
284 "%s: Incorrect modes, setgid bit not set",
285 setgid_A);
286 local_flag = FAILED;
287 }
288
289 /* Verify group ID */
290 if (buf.st_gid != mygid) {
291 tst_resm(TFAIL, "%s: Incorrect group", setgid_A);
292 tst_resm(TINFO, "got %u and %u", buf.st_gid, mygid);
293 local_flag = FAILED;
294 }
295 close(fd);
296
297 if (local_flag == PASSED) {
298 tst_resm(TPASS, "Test passed in block1.");
299 } else {
300 tst_resm(TFAIL, "Test failed in block1.");
301 }
302
303 local_flag = PASSED;
304
305 /*--------------------------------------------------------------*/
306 /* Block2: Create two files in testdir.B, one with the setgid */
307 /* bit set in the creation modes and the other without. */
308 /* Both should inherit the group ID of the parent */
309 /* directory, group2. */
310 /*--------------------------------------------------------------*/
311 /*
312 * Create the file with setgid not set
313 */
314 fd = creat(nosetgid_B, MODE_RWX);
315 if (fd == -1) {
316 tst_resm(TFAIL, "Creation of %s failed", nosetgid_B);
317 local_flag = FAILED;
318 }
319
320 if (stat(nosetgid_B, &buf) == -1) {
321 tst_resm(TFAIL, "Stat of %s failed", nosetgid_B);
322 local_flag = FAILED;
323 }
324
325 /* Verify modes */
326 if (buf.st_mode & S_ISGID) {
327 tst_resm(TFAIL,
328 "%s: Incorrect modes, setgid bit should not be set",
329 nosetgid_B);
330 local_flag = FAILED;
331 }
332
333 /* Verify group ID */
334 if (buf.st_gid != group2_gid) {
335 tst_resm(TFAIL, "%s: Incorrect group", nosetgid_B);
336 local_flag = FAILED;
337 }
338 close(fd);
339
340 /*
341 * Create the file with setgid set
342 */
343 fd = creat(setgid_B, MODE_SGID);
344 if (fd == -1) {
345 tst_resm(TFAIL, "Creation of %s failed", setgid_B);
346 local_flag = FAILED;
347 }
348
349 if (stat(setgid_B, &buf) == -1) {
350 tst_resm(TFAIL, "Stat of %s failed", setgid_B);
351 local_flag = FAILED;
352 }
353
354 /* Verify group ID */
355 if (buf.st_gid != group2_gid) {
356 tst_resm(TFAIL, "%s: Incorrect group", setgid_B);
357 tst_resm(TFAIL, "got %u and %u", buf.st_gid,
358 group2_gid);
359 local_flag = FAILED;
360 }
361
362 /* Verify modes */
363 if (!(buf.st_mode & S_ISGID)) {
364 tst_resm(TFAIL,
365 "%s: Incorrect modes, setgid bit should be set",
366 setgid_B);
367 local_flag = FAILED;
368 }
369 close(fd);
370
371 if (local_flag == PASSED) {
372 tst_resm(TPASS, "Test passed in block2.");
373 } else {
374 tst_resm(TFAIL, "Test failed in block2.");
375 }
376
377 local_flag = PASSED;
378 /*--------------------------------------------------------------*/
379 /* Block3: Create a file in testdir.B, with the setgid bit set */
380 /* in the creation modes and do so as root. The file */
381 /* should inherit the group ID of the parent directory, */
382 /* group2 and should have the setgid bit set. */
383 /*--------------------------------------------------------------*/
384 /* Become root again */
385 if (setreuid(-1, save_myuid) == -1) {
386 tst_resm(TFAIL | TERRNO,
387 "Changing back to root failed");
388 local_flag = FAILED;
389 }
390
391 /* Create the file with setgid set */
392 fd = creat(root_setgid_B, MODE_SGID);
393 if (fd == -1) {
394 tst_resm(TFAIL, "Creation of %s failed", root_setgid_B);
395 local_flag = FAILED;
396 }
397
398 if (stat(root_setgid_B, &buf) == -1) {
399 tst_resm(TFAIL, "Stat of %s failed", root_setgid_B);
400 local_flag = FAILED;
401 }
402
403 /* Verify modes */
404 if (!(buf.st_mode & S_ISGID)) {
405 tst_resm(TFAIL,
406 "%s: Incorrect modes, setgid bit not set",
407 root_setgid_B);
408 local_flag = FAILED;
409 }
410
411 /* Verify group ID */
412 if (buf.st_gid != group2_gid) {
413 tst_resm(TFAIL, "%s: Incorrect group", root_setgid_B);
414 tst_resm(TINFO, "got %u and %u", buf.st_gid,
415 group2_gid);
416 local_flag = FAILED;
417 }
418 close(fd);
419
420 if (local_flag == PASSED) {
421 tst_resm(TPASS, "Test passed in block3");
422 } else {
423 tst_resm(TFAIL, "Test failed in block3");
424 }
425 cleanup();
426 }
427 tst_exit();
428 }
429
setup(void)430 static void setup(void)
431 {
432 tst_require_root();
433 tst_tmpdir();
434 }
435
cleanup(void)436 static void cleanup(void)
437 {
438 if (unlink(setgid_A) == -1) {
439 tst_resm(TBROK, "%s failed", setgid_A);
440 }
441 if (unlink(nosetgid_A) == -1) {
442 tst_resm(TBROK, "unlink %s failed", nosetgid_A);
443 }
444 if (rmdir(DIR_A) == -1) {
445 tst_brkm(TBROK | TERRNO, NULL, "rmdir %s failed", DIR_A);
446 }
447 if (unlink(setgid_B) == -1) {
448 tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed", setgid_B);
449 }
450 if (unlink(root_setgid_B) == -1) {
451 tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed",
452 root_setgid_B);
453 }
454 if (unlink(nosetgid_B) == -1) {
455 tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed", nosetgid_B);
456 }
457 if (rmdir(DIR_B) == -1) {
458 tst_brkm(TBROK | TERRNO, NULL, "rmdir %s failed", DIR_B);
459 }
460
461 tst_rmdir();
462 }
463