xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/chmod/chmod03.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  *  07/2001 Ported by Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Verify that, chmod(2) will succeed to change the mode of a file or directory
11*49cdfc7eSAndroid Build Coastguard Worker  * and set the sticky bit on it if invoked by non-root (uid != 0)
12*49cdfc7eSAndroid Build Coastguard Worker  * process with the following constraints:
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * - the process is the owner of the file or directory.
15*49cdfc7eSAndroid Build Coastguard Worker  * - the effective group ID or one of the supplementary group ID's of the
16*49cdfc7eSAndroid Build Coastguard Worker  *   process is equal to the group ID of the file or directory.
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE   S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
23*49cdfc7eSAndroid Build Coastguard Worker #define DIR_MODE	S_IRWXU | S_IRWXG | S_IRWXO
24*49cdfc7eSAndroid Build Coastguard Worker #define PERMS		01777
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE	"testfile"
27*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR		"testdir_3"
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
30*49cdfc7eSAndroid Build Coastguard Worker 	char *name;
31*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
32*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
33*49cdfc7eSAndroid Build Coastguard Worker 	{TESTFILE, "verify permissions of file"},
34*49cdfc7eSAndroid Build Coastguard Worker 	{TESTDIR, "verify permissions of directory"},
35*49cdfc7eSAndroid Build Coastguard Worker };
36*49cdfc7eSAndroid Build Coastguard Worker 
verify_chmod(unsigned int n)37*49cdfc7eSAndroid Build Coastguard Worker static void verify_chmod(unsigned int n)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker 	struct stat stat_buf;
40*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(chmod(tc->name, PERMS), "chmod(%s, %04o)",
43*49cdfc7eSAndroid Build Coastguard Worker 		tc->name, PERMS);
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	if (!TST_PASS)
46*49cdfc7eSAndroid Build Coastguard Worker 		return;
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(tc->name, &stat_buf);
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	if ((stat_buf.st_mode & PERMS) != PERMS) {
51*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "stat(%s) mode=%04o",
52*49cdfc7eSAndroid Build Coastguard Worker 			tc->name, stat_buf.st_mode);
53*49cdfc7eSAndroid Build Coastguard Worker 	} else {
54*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "stat(%s) mode=%04o",
55*49cdfc7eSAndroid Build Coastguard Worker 			tc->name, stat_buf.st_mode);
56*49cdfc7eSAndroid Build Coastguard Worker 	}
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)59*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker 	char nobody_uid[] = "nobody";
62*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *ltpuser;
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	ltpuser = SAFE_GETPWNAM(nobody_uid);
65*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(ltpuser->pw_uid);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TESTFILE, FILE_MODE, NULL);
68*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TESTDIR, DIR_MODE);
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
72*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
73*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
74*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_chmod,
75*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
76*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
77*49cdfc7eSAndroid Build Coastguard Worker };
78