• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  * Author: Wayne Boyer
5  *
6  * Test Description:
7  *  Verify that, fchmod(2) will succeed to change the mode of a directory
8  *  but fails to set the setgid bit on it if invoked by non-root (uid != 0)
9  *  process with the following constraints,
10  *	- the process is the owner of the directory.
11  *	- the effective group ID or one of the supplementary group ID's of the
12  *	  process is not equal to the group ID of the directory.
13  *
14  * Expected Result:
15  *  fchmod() should return value 0 on success and though succeeds to change
16  *  the mode of a directory but fails to set setgid bit on it.
17  */
18 
19 #include <pwd.h>
20 #include <errno.h>
21 
22 #include "tst_test.h"
23 #include "fchmod.h"
24 
25 #define PERMS_DIR	043777
26 
27 static int fd;
28 
verify_fchmod(void)29 static void verify_fchmod(void)
30 {
31 	struct stat stat_buf;
32 	mode_t dir_mode;
33 
34 	TEST(fchmod(fd, PERMS_DIR));
35 	if (TST_RET == -1)
36 		tst_res(TFAIL | TTERRNO, "fchmod() failed unexpectly");
37 
38 	SAFE_FSTAT(fd, &stat_buf);
39 	dir_mode = stat_buf.st_mode;
40 
41 	if ((PERMS_DIR & ~S_ISGID) != dir_mode) {
42 		tst_res(TFAIL, "%s: Incorrect modes 0%03o, Expected 0%03o",
43 			TESTDIR, dir_mode & ~S_ISGID, PERMS_DIR);
44 	} else {
45 		tst_res(TPASS, "Functionality of fchmod(%d, %#o) successful",
46 			fd, PERMS_DIR);
47 	}
48 }
49 
setup(void)50 static void setup(void)
51 {
52 	struct passwd *ltpuser;
53 	struct group *ltpgroup;
54 
55 	ltpuser = SAFE_GETPWNAM("nobody");
56 	ltpgroup = SAFE_GETGRNAM("bin");
57 
58 	SAFE_MKDIR(TESTDIR, DIR_MODE);
59 
60 	if (setgroups(1, &ltpuser->pw_gid) == -1) {
61 		tst_brk(TBROK, "Couldn't change supplementary group Id: %s",
62 			tst_strerrno(TST_ERR));
63 	}
64 
65 	SAFE_CHOWN(TESTDIR, ltpuser->pw_uid, ltpgroup->gr_gid);
66 
67 	SAFE_SETEGID(ltpuser->pw_gid);
68 	SAFE_SETEUID(ltpuser->pw_uid);
69 
70 	fd = SAFE_OPEN(TESTDIR, O_RDONLY);
71 }
72 
cleanup(void)73 static void cleanup(void)
74 {
75 	if (fd > 0)
76 		SAFE_CLOSE(fd);
77 
78 	SAFE_SETEGID(0);
79 	SAFE_SETEUID(0);
80 }
81 
82 static struct tst_test test = {
83 	.test_all = verify_fchmod,
84 	.needs_root = 1,
85 	.setup = setup,
86 	.cleanup = cleanup,
87 	.needs_tmpdir = 1,
88 };
89