xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/chmod/chmod05.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: chmod05
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 directory
11*49cdfc7eSAndroid Build Coastguard Worker  *  but fails to set the setgid 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  *	- the process is the owner of the 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 not equal to the group ID of the 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 though succeeds to change
19*49cdfc7eSAndroid Build Coastguard Worker  *  the mode of a directory but fails to set setgid bit on it.
20*49cdfc7eSAndroid Build Coastguard Worker  *
21*49cdfc7eSAndroid Build Coastguard Worker  */
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #ifndef _GNU_SOURCE
24*49cdfc7eSAndroid Build Coastguard Worker # define _GNU_SOURCE
25*49cdfc7eSAndroid Build Coastguard Worker #endif
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <grp.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
40*49cdfc7eSAndroid Build Coastguard Worker #include "tst_uid.h"
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker #define MODE_RWX	(mode_t)(S_IRWXU | S_IRWXG | S_IRWXO)
43*49cdfc7eSAndroid Build Coastguard Worker #define DIR_MODE	(mode_t)(S_ISVTX | S_ISGID | S_IFDIR)
44*49cdfc7eSAndroid Build Coastguard Worker #define PERMS		(mode_t)(MODE_RWX | DIR_MODE)
45*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR		"testdir"
46*49cdfc7eSAndroid Build Coastguard Worker 
test_chmod(void)47*49cdfc7eSAndroid Build Coastguard Worker static void test_chmod(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker 	struct stat stat_buf;
50*49cdfc7eSAndroid Build Coastguard Worker 	mode_t dir_mode;
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	TEST(chmod(TESTDIR, PERMS));
53*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
54*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "chmod(%s, %#o) failed", TESTDIR, PERMS);
55*49cdfc7eSAndroid Build Coastguard Worker 		return;
56*49cdfc7eSAndroid Build Coastguard Worker 	}
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(TESTDIR, &stat_buf);
59*49cdfc7eSAndroid Build Coastguard Worker 	dir_mode = stat_buf.st_mode;
60*49cdfc7eSAndroid Build Coastguard Worker 	if ((PERMS & ~S_ISGID) != dir_mode) {
61*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "%s: Incorrect modes 0%03o, "
62*49cdfc7eSAndroid Build Coastguard Worker 				"Expected 0%03o", TESTDIR, dir_mode,
63*49cdfc7eSAndroid Build Coastguard Worker 				PERMS & ~S_ISGID);
64*49cdfc7eSAndroid Build Coastguard Worker 	} else {
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Functionality of chmod(%s, %#o) successful",
66*49cdfc7eSAndroid Build Coastguard Worker 				TESTDIR, PERMS);
67*49cdfc7eSAndroid Build Coastguard Worker 	}
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)70*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *nobody_u;
73*49cdfc7eSAndroid Build Coastguard Worker 	gid_t free_gid;
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	nobody_u = SAFE_GETPWNAM("nobody");
76*49cdfc7eSAndroid Build Coastguard Worker 	free_gid = tst_get_free_gid(nobody_u->pw_gid);
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	/*
79*49cdfc7eSAndroid Build Coastguard Worker 	 * Create a test directory under temporary directory with specified
80*49cdfc7eSAndroid Build Coastguard Worker 	 * mode permissions and change the gid of test directory to nobody's
81*49cdfc7eSAndroid Build Coastguard Worker 	 * gid.
82*49cdfc7eSAndroid Build Coastguard Worker 	 */
83*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TESTDIR, MODE_RWX);
84*49cdfc7eSAndroid Build Coastguard Worker 	if (setgroups(1, &nobody_u->pw_gid) == -1)
85*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "setgroups to nobody's gid failed");
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHOWN(TESTDIR, nobody_u->pw_uid, free_gid);
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	/* change to nobody:nobody */
90*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEGID(nobody_u->pw_gid);
91*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(nobody_u->pw_uid);
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
95*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root	= 1,
96*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir	= 1,
97*49cdfc7eSAndroid Build Coastguard Worker 	.setup		= setup,
98*49cdfc7eSAndroid Build Coastguard Worker 	.test_all	= test_chmod,
99*49cdfc7eSAndroid Build Coastguard Worker };
100