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