xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/open/open10.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., 2002
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2021 SUSE LLC <[email protected]>
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 the group ID and setgid bit are set correctly when a new file
10*49cdfc7eSAndroid Build Coastguard Worker  * is created.
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_uid.h"
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #define MODE_RWX        0777
20*49cdfc7eSAndroid Build Coastguard Worker #define MODE_SGID       (S_ISGID|0777)
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #define DIR_A		"dir_a"
23*49cdfc7eSAndroid Build Coastguard Worker #define DIR_B		"dir_b"
24*49cdfc7eSAndroid Build Coastguard Worker #define SETGID_A	DIR_A "/setgid"
25*49cdfc7eSAndroid Build Coastguard Worker #define NOSETGID_A	DIR_A "/nosetgid"
26*49cdfc7eSAndroid Build Coastguard Worker #define SETGID_B	DIR_B "/setgid"
27*49cdfc7eSAndroid Build Coastguard Worker #define NOSETGID_B	DIR_B "/nosetgid"
28*49cdfc7eSAndroid Build Coastguard Worker #define ROOT_SETGID	DIR_B "/root_setgid"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker static char *tmpdir;
31*49cdfc7eSAndroid Build Coastguard Worker static uid_t orig_uid, nobody_uid;
32*49cdfc7eSAndroid Build Coastguard Worker static gid_t nobody_gid, free_gid;
33*49cdfc7eSAndroid Build Coastguard Worker static int fd = -1;
34*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)35*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *ltpuser = SAFE_GETPWNAM("nobody");
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	orig_uid = getuid();
40*49cdfc7eSAndroid Build Coastguard Worker 	nobody_uid = ltpuser->pw_uid;
41*49cdfc7eSAndroid Build Coastguard Worker 	nobody_gid = ltpuser->pw_gid;
42*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "User nobody: uid = %d, gid = %d", (int)nobody_uid,
43*49cdfc7eSAndroid Build Coastguard Worker 		(int)nobody_gid);
44*49cdfc7eSAndroid Build Coastguard Worker 	free_gid = tst_get_free_gid(nobody_gid);
45*49cdfc7eSAndroid Build Coastguard Worker 	tmpdir = tst_get_tmpdir();
46*49cdfc7eSAndroid Build Coastguard Worker }
47*49cdfc7eSAndroid Build Coastguard Worker 
file_test(const char * name,mode_t mode,int sgid,gid_t gid)48*49cdfc7eSAndroid Build Coastguard Worker static void file_test(const char *name, mode_t mode, int sgid, gid_t gid)
49*49cdfc7eSAndroid Build Coastguard Worker {
50*49cdfc7eSAndroid Build Coastguard Worker 	struct stat buf;
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(name, O_CREAT | O_EXCL | O_RDWR, mode);
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
54*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(name, &buf);
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	if (buf.st_gid != gid) {
57*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "%s: Incorrect group, %u != %u", name,
58*49cdfc7eSAndroid Build Coastguard Worker 			buf.st_gid, gid);
59*49cdfc7eSAndroid Build Coastguard Worker 	} else {
60*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "%s: Owned by correct group", name);
61*49cdfc7eSAndroid Build Coastguard Worker 	}
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	if (sgid < 0) {
64*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "%s: Skipping setgid bit check", name);
65*49cdfc7eSAndroid Build Coastguard Worker 		return;
66*49cdfc7eSAndroid Build Coastguard Worker 	}
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	if (buf.st_mode & S_ISGID)
69*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(sgid ? TPASS : TFAIL, "%s: Setgid bit is set", name);
70*49cdfc7eSAndroid Build Coastguard Worker 	else
71*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(sgid ? TFAIL : TPASS, "%s: Setgid bit not set", name);
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker 
run(void)74*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
75*49cdfc7eSAndroid Build Coastguard Worker {
76*49cdfc7eSAndroid Build Coastguard Worker 	struct stat buf;
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	/* Create directories and set permissions */
79*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(DIR_A, MODE_RWX);
80*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHOWN(DIR_A, nobody_uid, free_gid);
81*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(DIR_A, &buf);
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	if (buf.st_mode & S_ISGID)
84*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "%s: Setgid bit is set", DIR_A);
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	if (buf.st_gid != free_gid) {
87*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "%s: Incorrect group, %u != %u", DIR_A,
88*49cdfc7eSAndroid Build Coastguard Worker 			buf.st_gid, free_gid);
89*49cdfc7eSAndroid Build Coastguard Worker 	}
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(DIR_B, MODE_RWX);
92*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHOWN(DIR_B, nobody_uid, free_gid);
93*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(DIR_B, MODE_SGID);
94*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(DIR_B, &buf);
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	if (!(buf.st_mode & S_ISGID))
97*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "%s: Setgid bit not set", DIR_B);
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 	if (buf.st_gid != free_gid) {
100*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "%s: Incorrect group, %u != %u", DIR_B,
101*49cdfc7eSAndroid Build Coastguard Worker 			buf.st_gid, free_gid);
102*49cdfc7eSAndroid Build Coastguard Worker 	}
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 	/* Switch to user nobody and create two files in DIR_A */
105*49cdfc7eSAndroid Build Coastguard Worker 	/* Both files should inherit GID from the process */
106*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETGID(nobody_gid);
107*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETREUID(-1, nobody_uid);
108*49cdfc7eSAndroid Build Coastguard Worker 	file_test(NOSETGID_A, MODE_RWX, 0, nobody_gid);
109*49cdfc7eSAndroid Build Coastguard Worker 	file_test(SETGID_A, MODE_SGID, 1, nobody_gid);
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 	/* Create two files in DIR_B and validate owner and permissions */
112*49cdfc7eSAndroid Build Coastguard Worker 	/* Both files should inherit GID from the parent directory */
113*49cdfc7eSAndroid Build Coastguard Worker 	file_test(NOSETGID_B, MODE_RWX, 0, free_gid);
114*49cdfc7eSAndroid Build Coastguard Worker 	/*
115*49cdfc7eSAndroid Build Coastguard Worker 	 * CVE 2018-13405 (privilege escalation using setgid bit) has its
116*49cdfc7eSAndroid Build Coastguard Worker 	 * own test, skip setgid check here
117*49cdfc7eSAndroid Build Coastguard Worker 	 */
118*49cdfc7eSAndroid Build Coastguard Worker 	file_test(SETGID_B, MODE_SGID, -1, free_gid);
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	/* Switch back to root UID and create a file in DIR_B */
121*49cdfc7eSAndroid Build Coastguard Worker 	/* The file should inherid GID from parent directory */
122*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETREUID(-1, orig_uid);
123*49cdfc7eSAndroid Build Coastguard Worker 	file_test(ROOT_SETGID, MODE_SGID, 1, free_gid);
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker 	/* Cleanup between loops */
126*49cdfc7eSAndroid Build Coastguard Worker 	tst_purge_dir(tmpdir);
127*49cdfc7eSAndroid Build Coastguard Worker }
128*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)129*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
130*49cdfc7eSAndroid Build Coastguard Worker {
131*49cdfc7eSAndroid Build Coastguard Worker 	if (fd >= 0)
132*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker 	free(tmpdir);
135*49cdfc7eSAndroid Build Coastguard Worker }
136*49cdfc7eSAndroid Build Coastguard Worker 
137*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
138*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
139*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
140*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
141*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
142*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
143*49cdfc7eSAndroid Build Coastguard Worker };
144