xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/chmod/chmod07.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  * Test Name: chmod07
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
10*49cdfc7eSAndroid Build Coastguard Worker  *  Verify that, chmod(2) will succeed to change the mode of a file/directory
11*49cdfc7eSAndroid Build Coastguard Worker  *  and sets the sticky bit on it if invoked by root (uid = 0) process with
12*49cdfc7eSAndroid Build Coastguard Worker  *  the following constraints,
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  * Expected Result:
18*49cdfc7eSAndroid Build Coastguard Worker  *  chmod() should return value 0 on success and succeeds to set sticky bit
19*49cdfc7eSAndroid Build Coastguard Worker  *  on the specified file.
20*49cdfc7eSAndroid Build Coastguard Worker  */
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <grp.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
35*49cdfc7eSAndroid Build Coastguard Worker #define PERMS		01777	/* Permissions with sticky bit set. */
36*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE	"testfile"
37*49cdfc7eSAndroid Build Coastguard Worker 
test_chmod(void)38*49cdfc7eSAndroid Build Coastguard Worker void test_chmod(void)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker 	struct stat stat_buf;
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	/*
43*49cdfc7eSAndroid Build Coastguard Worker 	 * Call chmod(2) with specified mode argument
44*49cdfc7eSAndroid Build Coastguard Worker 	 * (sticky-bit set) on testfile.
45*49cdfc7eSAndroid Build Coastguard Worker 	 */
46*49cdfc7eSAndroid Build Coastguard Worker 	TEST(chmod(TESTFILE, PERMS));
47*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
48*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TFAIL | TTERRNO, "chmod(%s, %#o) failed",
49*49cdfc7eSAndroid Build Coastguard Worker 				TESTFILE, PERMS);
50*49cdfc7eSAndroid Build Coastguard Worker 	}
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	if (stat(TESTFILE, &stat_buf) == -1) {
53*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TFAIL | TTERRNO, "stat failed");
54*49cdfc7eSAndroid Build Coastguard Worker 	}
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	/* Check for expected mode permissions */
57*49cdfc7eSAndroid Build Coastguard Worker 	if ((stat_buf.st_mode & PERMS) == PERMS) {
58*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Functionality of chmod(%s, %#o) successful",
59*49cdfc7eSAndroid Build Coastguard Worker 				TESTFILE, PERMS);
60*49cdfc7eSAndroid Build Coastguard Worker 	} else {
61*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "%s: Incorrect modes 0%03o; expected 0%03o",
62*49cdfc7eSAndroid Build Coastguard Worker 				TESTFILE, stat_buf.st_mode, PERMS);
63*49cdfc7eSAndroid Build Coastguard Worker 	}
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)66*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
67*49cdfc7eSAndroid Build Coastguard Worker {
68*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *ltpuser;
69*49cdfc7eSAndroid Build Coastguard Worker 	struct group *ltpgroup;
70*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
71*49cdfc7eSAndroid Build Coastguard Worker 	gid_t group1_gid;
72*49cdfc7eSAndroid Build Coastguard Worker 	uid_t user1_uid;
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	ltpuser = SAFE_GETPWNAM("nobody");
75*49cdfc7eSAndroid Build Coastguard Worker 	user1_uid = ltpuser->pw_uid;
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	ltpgroup = SAFE_GETGRNAM_FALLBACK("users", "daemon");
78*49cdfc7eSAndroid Build Coastguard Worker 	group1_gid = ltpgroup->gr_gid;
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
81*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
82*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHOWN(TESTFILE, user1_uid, group1_gid);
83*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETGID(group1_gid);
84*49cdfc7eSAndroid Build Coastguard Worker }
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
87*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
88*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
89*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
90*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = test_chmod,
91*49cdfc7eSAndroid Build Coastguard Worker };
92