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) 2018 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2021 CTERA Networks. All Rights Reserved.
5*49cdfc7eSAndroid Build Coastguard Worker *
6*49cdfc7eSAndroid Build Coastguard Worker * User ns support by: Xiao Yang <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker * Forked from getxattr05.c by Amir Goldstein <[email protected]>
8*49cdfc7eSAndroid Build Coastguard Worker */
9*49cdfc7eSAndroid Build Coastguard Worker
10*49cdfc7eSAndroid Build Coastguard Worker /*\
11*49cdfc7eSAndroid Build Coastguard Worker * [Description]
12*49cdfc7eSAndroid Build Coastguard Worker * Check that fanotify groups and marks limits are enforced correctly.
13*49cdfc7eSAndroid Build Coastguard Worker * If user ns is supported, verify that global limit and per user ns
14*49cdfc7eSAndroid Build Coastguard Worker * limits are both enforced.
15*49cdfc7eSAndroid Build Coastguard Worker * Otherwise, we only check that global groups limit is enforced.
16*49cdfc7eSAndroid Build Coastguard Worker */
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
19*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
20*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_FANOTIFY_H
29*49cdfc7eSAndroid Build Coastguard Worker #include "fanotify.h"
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker #define MOUNT_PATH "fs_mnt"
32*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE MOUNT_PATH "/testfile"
33*49cdfc7eSAndroid Build Coastguard Worker #define SELF_USERNS "/proc/self/ns/user"
34*49cdfc7eSAndroid Build Coastguard Worker #define MAX_USERNS "/proc/sys/user/max_user_namespaces"
35*49cdfc7eSAndroid Build Coastguard Worker #define UID_MAP "/proc/self/uid_map"
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker #define GLOBAL_MAX_GROUPS "/proc/sys/fs/fanotify/max_user_groups"
38*49cdfc7eSAndroid Build Coastguard Worker #define GLOBAL_MAX_MARKS "/proc/sys/fs/fanotify/max_user_marks"
39*49cdfc7eSAndroid Build Coastguard Worker #define USERNS_MAX_GROUPS "/proc/sys/user/max_fanotify_groups"
40*49cdfc7eSAndroid Build Coastguard Worker #define USERNS_MAX_MARKS "/proc/sys/user/max_fanotify_marks"
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker /*
43*49cdfc7eSAndroid Build Coastguard Worker * In older kernels those limits were fixed in kernel.
44*49cdfc7eSAndroid Build Coastguard Worker * The fanotify_init() man page documents the max groups limit is 128, but the
45*49cdfc7eSAndroid Build Coastguard Worker * implementation actually allows one extra group.
46*49cdfc7eSAndroid Build Coastguard Worker */
47*49cdfc7eSAndroid Build Coastguard Worker #define DEFAULT_MAX_GROUPS 129
48*49cdfc7eSAndroid Build Coastguard Worker #define DEFAULT_MAX_MARKS 8192
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker static int orig_max_userns = -1;
51*49cdfc7eSAndroid Build Coastguard Worker static int user_ns_supported = 1;
52*49cdfc7eSAndroid Build Coastguard Worker static int max_groups = DEFAULT_MAX_GROUPS;
53*49cdfc7eSAndroid Build Coastguard Worker static int max_marks = DEFAULT_MAX_MARKS;
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
56*49cdfc7eSAndroid Build Coastguard Worker const char *tname;
57*49cdfc7eSAndroid Build Coastguard Worker unsigned int init_flags;
58*49cdfc7eSAndroid Build Coastguard Worker /* 0: without userns, 1: with userns */
59*49cdfc7eSAndroid Build Coastguard Worker int set_userns;
60*49cdfc7eSAndroid Build Coastguard Worker /* 0: don't map root UID in userns, 1: map root UID in userns */
61*49cdfc7eSAndroid Build Coastguard Worker int map_root;
62*49cdfc7eSAndroid Build Coastguard Worker /* 0: unlimited groups in userns */
63*49cdfc7eSAndroid Build Coastguard Worker int max_user_groups;
64*49cdfc7eSAndroid Build Coastguard Worker /* 0: unlimited marks in userns */
65*49cdfc7eSAndroid Build Coastguard Worker int max_user_marks;
66*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
67*49cdfc7eSAndroid Build Coastguard Worker {
68*49cdfc7eSAndroid Build Coastguard Worker "Global groups limit in init user ns",
69*49cdfc7eSAndroid Build Coastguard Worker FAN_CLASS_NOTIF,
70*49cdfc7eSAndroid Build Coastguard Worker 0, 0, 0, 0,
71*49cdfc7eSAndroid Build Coastguard Worker },
72*49cdfc7eSAndroid Build Coastguard Worker {
73*49cdfc7eSAndroid Build Coastguard Worker "Global groups limit in privileged user ns",
74*49cdfc7eSAndroid Build Coastguard Worker FANOTIFY_REQUIRED_USER_INIT_FLAGS,
75*49cdfc7eSAndroid Build Coastguard Worker 1, 1, 0, 0,
76*49cdfc7eSAndroid Build Coastguard Worker },
77*49cdfc7eSAndroid Build Coastguard Worker {
78*49cdfc7eSAndroid Build Coastguard Worker "Local groups limit in unprivileged user ns",
79*49cdfc7eSAndroid Build Coastguard Worker FANOTIFY_REQUIRED_USER_INIT_FLAGS,
80*49cdfc7eSAndroid Build Coastguard Worker 1, 0, 10, 0,
81*49cdfc7eSAndroid Build Coastguard Worker },
82*49cdfc7eSAndroid Build Coastguard Worker {
83*49cdfc7eSAndroid Build Coastguard Worker "Local marks limit in unprivileged user ns",
84*49cdfc7eSAndroid Build Coastguard Worker FANOTIFY_REQUIRED_USER_INIT_FLAGS,
85*49cdfc7eSAndroid Build Coastguard Worker 1, 0, 0, 10,
86*49cdfc7eSAndroid Build Coastguard Worker },
87*49cdfc7eSAndroid Build Coastguard Worker };
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker /* Verify that groups and marks cannot be created beyond limit */
verify_user_limits(unsigned int init_flags,int groups,int marks)90*49cdfc7eSAndroid Build Coastguard Worker static void verify_user_limits(unsigned int init_flags, int groups, int marks)
91*49cdfc7eSAndroid Build Coastguard Worker {
92*49cdfc7eSAndroid Build Coastguard Worker int i, fd = 0, ret = 0;
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i <= groups; i++) {
95*49cdfc7eSAndroid Build Coastguard Worker fd = fanotify_init(init_flags, O_RDONLY);
96*49cdfc7eSAndroid Build Coastguard Worker /*
97*49cdfc7eSAndroid Build Coastguard Worker * Don't bother closing fd's, the child process will exit
98*49cdfc7eSAndroid Build Coastguard Worker * and all fd's will be closed.
99*49cdfc7eSAndroid Build Coastguard Worker */
100*49cdfc7eSAndroid Build Coastguard Worker if (fd < 0)
101*49cdfc7eSAndroid Build Coastguard Worker break;
102*49cdfc7eSAndroid Build Coastguard Worker
103*49cdfc7eSAndroid Build Coastguard Worker ret = fanotify_mark(fd, FAN_MARK_ADD, FAN_OPEN, AT_FDCWD,
104*49cdfc7eSAndroid Build Coastguard Worker TEST_FILE);
105*49cdfc7eSAndroid Build Coastguard Worker if (ret < 0)
106*49cdfc7eSAndroid Build Coastguard Worker break;
107*49cdfc7eSAndroid Build Coastguard Worker
108*49cdfc7eSAndroid Build Coastguard Worker }
109*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0 && i > groups) {
110*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
111*49cdfc7eSAndroid Build Coastguard Worker "Created %d groups and marks - "
112*49cdfc7eSAndroid Build Coastguard Worker "groups limit (%d) exceeded", i, groups);
113*49cdfc7eSAndroid Build Coastguard Worker } else if (!ret && i > marks) {
114*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
115*49cdfc7eSAndroid Build Coastguard Worker "Created %d groups and marks - "
116*49cdfc7eSAndroid Build Coastguard Worker "marks limit (%d) exceeded", i, marks);
117*49cdfc7eSAndroid Build Coastguard Worker } else if (ret < 0 && errno == ENOSPC && marks < groups) {
118*49cdfc7eSAndroid Build Coastguard Worker /*
119*49cdfc7eSAndroid Build Coastguard Worker * ENOSPC is to be returned to the calling process when
120*49cdfc7eSAndroid Build Coastguard Worker * fanotify marks limit is reached.
121*49cdfc7eSAndroid Build Coastguard Worker */
122*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
123*49cdfc7eSAndroid Build Coastguard Worker "Created %d marks - "
124*49cdfc7eSAndroid Build Coastguard Worker "below marks limit (%d)", i, marks);
125*49cdfc7eSAndroid Build Coastguard Worker } else if (fd < 0 && errno == EMFILE) {
126*49cdfc7eSAndroid Build Coastguard Worker /*
127*49cdfc7eSAndroid Build Coastguard Worker * EMFILE is to be returned to the calling process when
128*49cdfc7eSAndroid Build Coastguard Worker * fanotify groups limit is reached.
129*49cdfc7eSAndroid Build Coastguard Worker */
130*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
131*49cdfc7eSAndroid Build Coastguard Worker "Created %d groups - "
132*49cdfc7eSAndroid Build Coastguard Worker "below groups limit (%d)", i, groups);
133*49cdfc7eSAndroid Build Coastguard Worker } else if (errno == EPERM) {
134*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF,
135*49cdfc7eSAndroid Build Coastguard Worker "unprivileged fanotify not supported by kernel?");
136*49cdfc7eSAndroid Build Coastguard Worker } else if (fd < 0) {
137*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO,
138*49cdfc7eSAndroid Build Coastguard Worker "fd=%d, fanotify_init(%x, O_RDONLY) failed",
139*49cdfc7eSAndroid Build Coastguard Worker fd, init_flags);
140*49cdfc7eSAndroid Build Coastguard Worker } else if (ret < 0) {
141*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO,
142*49cdfc7eSAndroid Build Coastguard Worker "ret=%d, fanotify_mark(%d, FAN_MARK_ADD, FAN_OPEN, "
143*49cdfc7eSAndroid Build Coastguard Worker "AT_FDCWD, '" TEST_FILE "') failed", ret, fd);
144*49cdfc7eSAndroid Build Coastguard Worker }
145*49cdfc7eSAndroid Build Coastguard Worker }
146*49cdfc7eSAndroid Build Coastguard Worker
do_unshare(int map_root)147*49cdfc7eSAndroid Build Coastguard Worker static void do_unshare(int map_root)
148*49cdfc7eSAndroid Build Coastguard Worker {
149*49cdfc7eSAndroid Build Coastguard Worker int res;
150*49cdfc7eSAndroid Build Coastguard Worker
151*49cdfc7eSAndroid Build Coastguard Worker /* unshare() should support CLONE_NEWUSER flag since Linux 3.8 */
152*49cdfc7eSAndroid Build Coastguard Worker res = unshare(CLONE_NEWUSER);
153*49cdfc7eSAndroid Build Coastguard Worker if (res == -1)
154*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TFAIL | TERRNO, "unshare(CLONE_NEWUSER) failed");
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker if (map_root) {
157*49cdfc7eSAndroid Build Coastguard Worker /*
158*49cdfc7eSAndroid Build Coastguard Worker * uid_map file should exist since Linux 3.8 because
159*49cdfc7eSAndroid Build Coastguard Worker * it is available on Linux 3.5
160*49cdfc7eSAndroid Build Coastguard Worker */
161*49cdfc7eSAndroid Build Coastguard Worker SAFE_ACCESS(UID_MAP, F_OK);
162*49cdfc7eSAndroid Build Coastguard Worker
163*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(UID_MAP, "%d %d %d", 0, 0, 1);
164*49cdfc7eSAndroid Build Coastguard Worker }
165*49cdfc7eSAndroid Build Coastguard Worker }
166*49cdfc7eSAndroid Build Coastguard Worker
test_fanotify(unsigned int n)167*49cdfc7eSAndroid Build Coastguard Worker static void test_fanotify(unsigned int n)
168*49cdfc7eSAndroid Build Coastguard Worker {
169*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
170*49cdfc7eSAndroid Build Coastguard Worker int groups = max_groups;
171*49cdfc7eSAndroid Build Coastguard Worker int marks = max_marks;
172*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
173*49cdfc7eSAndroid Build Coastguard Worker
174*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Test #%d: %s", n, tc->tname);
175*49cdfc7eSAndroid Build Coastguard Worker
176*49cdfc7eSAndroid Build Coastguard Worker if (tc->set_userns && !user_ns_supported) {
177*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF, "fanotify inside user namespace is not supported");
178*49cdfc7eSAndroid Build Coastguard Worker return;
179*49cdfc7eSAndroid Build Coastguard Worker }
180*49cdfc7eSAndroid Build Coastguard Worker
181*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
182*49cdfc7eSAndroid Build Coastguard Worker if (!pid) {
183*49cdfc7eSAndroid Build Coastguard Worker if (tc->set_userns) {
184*49cdfc7eSAndroid Build Coastguard Worker do_unshare(tc->map_root);
185*49cdfc7eSAndroid Build Coastguard Worker /* Not changing global limits, only per userns limits */
186*49cdfc7eSAndroid Build Coastguard Worker if (tc->max_user_groups && tc->max_user_groups < groups) {
187*49cdfc7eSAndroid Build Coastguard Worker /* Further limit user ns groups */
188*49cdfc7eSAndroid Build Coastguard Worker marks = groups = tc->max_user_groups;
189*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(USERNS_MAX_GROUPS, "%d", groups);
190*49cdfc7eSAndroid Build Coastguard Worker }
191*49cdfc7eSAndroid Build Coastguard Worker if (tc->max_user_marks && tc->max_user_marks < marks) {
192*49cdfc7eSAndroid Build Coastguard Worker /* Further limit user ns marks */
193*49cdfc7eSAndroid Build Coastguard Worker marks = tc->max_user_marks;
194*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(USERNS_MAX_MARKS, "%d", marks);
195*49cdfc7eSAndroid Build Coastguard Worker }
196*49cdfc7eSAndroid Build Coastguard Worker }
197*49cdfc7eSAndroid Build Coastguard Worker verify_user_limits(tc->init_flags, groups, marks);
198*49cdfc7eSAndroid Build Coastguard Worker exit(0);
199*49cdfc7eSAndroid Build Coastguard Worker }
200*49cdfc7eSAndroid Build Coastguard Worker
201*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children();
202*49cdfc7eSAndroid Build Coastguard Worker }
203*49cdfc7eSAndroid Build Coastguard Worker
setup_rlimit(unsigned int max_files)204*49cdfc7eSAndroid Build Coastguard Worker static void setup_rlimit(unsigned int max_files)
205*49cdfc7eSAndroid Build Coastguard Worker {
206*49cdfc7eSAndroid Build Coastguard Worker struct rlimit rlim;
207*49cdfc7eSAndroid Build Coastguard Worker
208*49cdfc7eSAndroid Build Coastguard Worker SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlim);
209*49cdfc7eSAndroid Build Coastguard Worker rlim.rlim_cur = max_files;
210*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlim);
211*49cdfc7eSAndroid Build Coastguard Worker }
212*49cdfc7eSAndroid Build Coastguard Worker
setup(void)213*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
214*49cdfc7eSAndroid Build Coastguard Worker {
215*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(TEST_FILE, 0666, NULL);
216*49cdfc7eSAndroid Build Coastguard Worker /* Check for kernel fanotify support */
217*49cdfc7eSAndroid Build Coastguard Worker REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS(FAN_REPORT_FID, TEST_FILE);
218*49cdfc7eSAndroid Build Coastguard Worker
219*49cdfc7eSAndroid Build Coastguard Worker /*
220*49cdfc7eSAndroid Build Coastguard Worker * The default value of max_user_namespaces is set to 0 on some distros,
221*49cdfc7eSAndroid Build Coastguard Worker * We need to change the default value to call unshare().
222*49cdfc7eSAndroid Build Coastguard Worker */
223*49cdfc7eSAndroid Build Coastguard Worker if (access(SELF_USERNS, F_OK) != 0) {
224*49cdfc7eSAndroid Build Coastguard Worker user_ns_supported = 0;
225*49cdfc7eSAndroid Build Coastguard Worker } else if (!access(MAX_USERNS, F_OK)) {
226*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF(MAX_USERNS, "%d", &orig_max_userns);
227*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(MAX_USERNS, "%d", 10);
228*49cdfc7eSAndroid Build Coastguard Worker }
229*49cdfc7eSAndroid Build Coastguard Worker
230*49cdfc7eSAndroid Build Coastguard Worker /*
231*49cdfc7eSAndroid Build Coastguard Worker * In older kernels those limits were fixed in kernel and fanotify is
232*49cdfc7eSAndroid Build Coastguard Worker * not permitted inside user ns.
233*49cdfc7eSAndroid Build Coastguard Worker */
234*49cdfc7eSAndroid Build Coastguard Worker if (access(GLOBAL_MAX_GROUPS, F_OK) && errno == ENOENT) {
235*49cdfc7eSAndroid Build Coastguard Worker user_ns_supported = 0;
236*49cdfc7eSAndroid Build Coastguard Worker } else {
237*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF(GLOBAL_MAX_GROUPS, "%d", &max_groups);
238*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF(GLOBAL_MAX_MARKS, "%d", &max_marks);
239*49cdfc7eSAndroid Build Coastguard Worker }
240*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "max_fanotify_groups=%d max_fanotify_marks=%d",
241*49cdfc7eSAndroid Build Coastguard Worker max_groups, max_marks);
242*49cdfc7eSAndroid Build Coastguard Worker
243*49cdfc7eSAndroid Build Coastguard Worker /* Make sure we are not limited by nr of open files */
244*49cdfc7eSAndroid Build Coastguard Worker setup_rlimit(max_groups * 2);
245*49cdfc7eSAndroid Build Coastguard Worker }
246*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)247*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
248*49cdfc7eSAndroid Build Coastguard Worker {
249*49cdfc7eSAndroid Build Coastguard Worker if (orig_max_userns != -1)
250*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(MAX_USERNS, "%d", orig_max_userns);
251*49cdfc7eSAndroid Build Coastguard Worker }
252*49cdfc7eSAndroid Build Coastguard Worker
253*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
254*49cdfc7eSAndroid Build Coastguard Worker .test = test_fanotify,
255*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
256*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
257*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
258*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
259*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
260*49cdfc7eSAndroid Build Coastguard Worker .mount_device = 1,
261*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MOUNT_PATH,
262*49cdfc7eSAndroid Build Coastguard Worker };
263*49cdfc7eSAndroid Build Coastguard Worker #else
264*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("system doesn't have required fanotify support");
265*49cdfc7eSAndroid Build Coastguard Worker #endif
266