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) 2021 SUSE LLC <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker /*\
6*49cdfc7eSAndroid Build Coastguard Worker * [Description]
7*49cdfc7eSAndroid Build Coastguard Worker *
8*49cdfc7eSAndroid Build Coastguard Worker * CVE-2018-13405
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Check for possible privilege escalation through creating files with setgid
11*49cdfc7eSAndroid Build Coastguard Worker * bit set inside a setgid directory owned by a group which the user does not
12*49cdfc7eSAndroid Build Coastguard Worker * belong to.
13*49cdfc7eSAndroid Build Coastguard Worker *
14*49cdfc7eSAndroid Build Coastguard Worker * Fixed in:
15*49cdfc7eSAndroid Build Coastguard Worker *
16*49cdfc7eSAndroid Build Coastguard Worker * commit 0fa3ecd87848c9c93c2c828ef4c3a8ca36ce46c7
17*49cdfc7eSAndroid Build Coastguard Worker * Author: Linus Torvalds <[email protected]>
18*49cdfc7eSAndroid Build Coastguard Worker * Date: Tue Jul 3 17:10:19 2018 -0700
19*49cdfc7eSAndroid Build Coastguard Worker *
20*49cdfc7eSAndroid Build Coastguard Worker * Fix up non-directory creation in SGID directories
21*49cdfc7eSAndroid Build Coastguard Worker *
22*49cdfc7eSAndroid Build Coastguard Worker * This fix is incomplete if file is on xfs filesystem.
23*49cdfc7eSAndroid Build Coastguard Worker *
24*49cdfc7eSAndroid Build Coastguard Worker * Fixed in:
25*49cdfc7eSAndroid Build Coastguard Worker *
26*49cdfc7eSAndroid Build Coastguard Worker * commit 01ea173e103edd5ec41acec65b9261b87e123fc2
27*49cdfc7eSAndroid Build Coastguard Worker * Author: Christoph Hellwig <[email protected]>
28*49cdfc7eSAndroid Build Coastguard Worker * Date: Fri Jan 22 16:48:18 2021 -0800
29*49cdfc7eSAndroid Build Coastguard Worker *
30*49cdfc7eSAndroid Build Coastguard Worker * xfs: fix up non-directory creation in SGID directories
31*49cdfc7eSAndroid Build Coastguard Worker *
32*49cdfc7eSAndroid Build Coastguard Worker * When use acl or umask, it still has bug.
33*49cdfc7eSAndroid Build Coastguard Worker *
34*49cdfc7eSAndroid Build Coastguard Worker * Fixed in:
35*49cdfc7eSAndroid Build Coastguard Worker *
36*49cdfc7eSAndroid Build Coastguard Worker * commit 1639a49ccdce58ea248841ed9b23babcce6dbb0b
37*49cdfc7eSAndroid Build Coastguard Worker * Author: Yang Xu <[email protected]>
38*49cdfc7eSAndroid Build Coastguard Worker * Date: Thu July 14 14:11:27 2022 +0800
39*49cdfc7eSAndroid Build Coastguard Worker *
40*49cdfc7eSAndroid Build Coastguard Worker * fs: move S_ISGID stripping into the vfs_*() helpers
41*49cdfc7eSAndroid Build Coastguard Worker */
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
47*49cdfc7eSAndroid Build Coastguard Worker #include "tst_uid.h"
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker #define MODE_RWX 0777
50*49cdfc7eSAndroid Build Coastguard Worker #define MODE_SGID (S_ISGID|0777)
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT "mntpoint"
53*49cdfc7eSAndroid Build Coastguard Worker #define WORKDIR MNTPOINT "/testdir"
54*49cdfc7eSAndroid Build Coastguard Worker #define CREAT_FILE WORKDIR "/creat.tmp"
55*49cdfc7eSAndroid Build Coastguard Worker #define OPEN_FILE WORKDIR "/open.tmp"
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker static gid_t free_gid;
58*49cdfc7eSAndroid Build Coastguard Worker static int fd = -1;
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
61*49cdfc7eSAndroid Build Coastguard Worker const char *msg;
62*49cdfc7eSAndroid Build Coastguard Worker int mask;
63*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
64*49cdfc7eSAndroid Build Coastguard Worker {"umask(0)", 0},
65*49cdfc7eSAndroid Build Coastguard Worker {"umask(S_IXGRP)", S_IXGRP}
66*49cdfc7eSAndroid Build Coastguard Worker };
67*49cdfc7eSAndroid Build Coastguard Worker
setup(void)68*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
69*49cdfc7eSAndroid Build Coastguard Worker {
70*49cdfc7eSAndroid Build Coastguard Worker struct stat buf;
71*49cdfc7eSAndroid Build Coastguard Worker struct passwd *ltpuser = SAFE_GETPWNAM("nobody");
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "User nobody: uid = %d, gid = %d", (int)ltpuser->pw_uid,
74*49cdfc7eSAndroid Build Coastguard Worker (int)ltpuser->pw_gid);
75*49cdfc7eSAndroid Build Coastguard Worker free_gid = tst_get_free_gid(ltpuser->pw_gid);
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker /* Create directories and set permissions */
78*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(WORKDIR, MODE_RWX);
79*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHOWN(WORKDIR, ltpuser->pw_uid, free_gid);
80*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHMOD(WORKDIR, MODE_SGID);
81*49cdfc7eSAndroid Build Coastguard Worker SAFE_STAT(WORKDIR, &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 not set", WORKDIR);
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", WORKDIR,
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 /* Switch user */
92*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETGID(ltpuser->pw_gid);
93*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETREUID(-1, ltpuser->pw_uid);
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker
file_test(const char * name)96*49cdfc7eSAndroid Build Coastguard Worker static void file_test(const char *name)
97*49cdfc7eSAndroid Build Coastguard Worker {
98*49cdfc7eSAndroid Build Coastguard Worker struct stat buf;
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker SAFE_STAT(name, &buf);
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker if (buf.st_gid != free_gid) {
103*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s: Incorrect group, %u != %u", name,
104*49cdfc7eSAndroid Build Coastguard Worker buf.st_gid, free_gid);
105*49cdfc7eSAndroid Build Coastguard Worker } else {
106*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "%s: Owned by correct group", name);
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker if (buf.st_mode & S_ISGID)
110*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s: Setgid bit is set", name);
111*49cdfc7eSAndroid Build Coastguard Worker else
112*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "%s: Setgid bit not set", name);
113*49cdfc7eSAndroid Build Coastguard Worker }
114*49cdfc7eSAndroid Build Coastguard Worker
run(unsigned int n)115*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
116*49cdfc7eSAndroid Build Coastguard Worker {
117*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
118*49cdfc7eSAndroid Build Coastguard Worker
119*49cdfc7eSAndroid Build Coastguard Worker umask(tc->mask);
120*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "File created with %s", tc->msg);
121*49cdfc7eSAndroid Build Coastguard Worker
122*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_CREAT(CREAT_FILE, MODE_SGID);
123*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
124*49cdfc7eSAndroid Build Coastguard Worker file_test(CREAT_FILE);
125*49cdfc7eSAndroid Build Coastguard Worker
126*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(OPEN_FILE, O_CREAT | O_EXCL | O_RDWR, MODE_SGID);
127*49cdfc7eSAndroid Build Coastguard Worker file_test(OPEN_FILE);
128*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
129*49cdfc7eSAndroid Build Coastguard Worker
130*49cdfc7eSAndroid Build Coastguard Worker /* Cleanup between loops */
131*49cdfc7eSAndroid Build Coastguard Worker tst_purge_dir(WORKDIR);
132*49cdfc7eSAndroid Build Coastguard Worker }
133*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)134*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
135*49cdfc7eSAndroid Build Coastguard Worker {
136*49cdfc7eSAndroid Build Coastguard Worker if (fd >= 0)
137*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
138*49cdfc7eSAndroid Build Coastguard Worker }
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
141*49cdfc7eSAndroid Build Coastguard Worker .test = run,
142*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
143*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
144*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
145*49cdfc7eSAndroid Build Coastguard Worker .all_filesystems = 1,
146*49cdfc7eSAndroid Build Coastguard Worker .mount_device = 1,
147*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MNTPOINT,
148*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
149*49cdfc7eSAndroid Build Coastguard Worker .skip_filesystems = (const char*[]) {
150*49cdfc7eSAndroid Build Coastguard Worker "exfat",
151*49cdfc7eSAndroid Build Coastguard Worker "ntfs",
152*49cdfc7eSAndroid Build Coastguard Worker "vfat",
153*49cdfc7eSAndroid Build Coastguard Worker NULL
154*49cdfc7eSAndroid Build Coastguard Worker },
155*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) {
156*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "0fa3ecd87848"},
157*49cdfc7eSAndroid Build Coastguard Worker {"CVE", "2018-13405"},
158*49cdfc7eSAndroid Build Coastguard Worker {"CVE", "2021-4037"},
159*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "01ea173e103e"},
160*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "1639a49ccdce"},
161*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "426b4ca2d6a5"},
162*49cdfc7eSAndroid Build Coastguard Worker {}
163*49cdfc7eSAndroid Build Coastguard Worker },
164*49cdfc7eSAndroid Build Coastguard Worker };
165