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