• 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  *
5  * Test Description:
6  *  Verify that, fchmod() will succeed to change the mode of a file/directory
7  *  set the sticky bit on it if invoked by root (uid = 0) process with
8  *  the following constraints,
9  *	- the process is not the owner of the file/directory.
10  *	- the effective group ID or one of the supplementary group ID's of the
11  *	  process is equal to the group ID of the file/directory.
12  *
13  * Expected Result:
14  *  fchmod() should return value 0 on success and succeeds to set sticky bit
15  *  on the specified file.
16  */
17 
18 #include <pwd.h>
19 #include <grp.h>
20 #include <errno.h>
21 
22 #include "tst_test.h"
23 #include "fchmod.h"
24 
25 static int fd;
26 
verify_fchmod(void)27 static void verify_fchmod(void)
28 {
29 	struct stat stat_buf;
30 	mode_t file_mode;
31 
32 	TEST(fchmod(fd, PERMS));
33 	if (TST_RET == -1)
34 		tst_res(TFAIL | TTERRNO, "fchmod() failed unexpectly");
35 
36 	SAFE_FSTAT(fd, &stat_buf);
37 	file_mode = stat_buf.st_mode;
38 
39 	if ((file_mode & ~S_IFREG) != PERMS) {
40 		tst_res(TFAIL, "%s: Incorrect modes 0%03o, Expected 0%03o",
41 			TESTFILE, file_mode, PERMS);
42 	} else {
43 		tst_res(TPASS, "Functionality of fchmod(%d, %#o) Successful",
44 			fd, PERMS);
45 	}
46 }
47 
setup(void)48 static void setup(void)
49 {
50 	struct passwd *ltpuser;
51 	struct group *ltpgroup;
52 
53 	ltpuser = SAFE_GETPWNAM("nobody");
54 	ltpgroup = SAFE_GETGRNAM_FALLBACK("users", "daemon");
55 
56 	fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
57 	SAFE_CHOWN(TESTFILE, ltpuser->pw_uid, ltpgroup->gr_gid);
58 	SAFE_SETGID(ltpgroup->gr_gid);
59 }
60 
cleanup(void)61 static void cleanup(void)
62 {
63 	if (fd > 0)
64 		SAFE_CLOSE(fd);
65 }
66 
67 static struct tst_test test = {
68 	.test_all = verify_fchmod,
69 	.needs_root = 1,
70 	.setup = setup,
71 	.cleanup = cleanup,
72 	.needs_tmpdir = 1,
73 };
74