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