1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) Linux Test Project, 2022
4 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
5 */
6
7 /*\
8 * [Description]
9 *
10 * Check mount(2) system call with various flags.
11 *
12 * Verify that mount(2) syscall passes for each flag setting and validate
13 * the flags:
14 *
15 * - MS_RDONLY - mount read-only
16 * - MS_NODEV - disallow access to device special files
17 * - MS_NOEXEC - disallow program execution
18 * - MS_REMOUNT - alter flags of a mounted FS
19 * - MS_NOSUID - ignore suid and sgid bits
20 * - MS_NOATIME - do not update access times
21 * - MS_NODIRATIME - only update access_time for directory instead of all types
22 * - MS_STRICTATIME - always update access times
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <sys/vfs.h>
30 #include <pwd.h>
31 #include "tst_test.h"
32 #include "lapi/mount.h"
33
34 #define MNTPOINT "mntpoint"
35 #define TESTBIN "mount03_suid_child"
36 #define BIN_PATH MNTPOINT "/" TESTBIN
37 #define TEST_STR "abcdefghijklmnopqrstuvwxyz"
38 #define FILE_MODE 0644
39 #define SUID_MODE (0511 | S_ISUID)
40
41 #define CHECK_ENOENT(x) ((x) == -1 && errno == ENOENT)
42
43 static int otfd;
44 static char file[PATH_MAX];
45 static char dir[PATH_MAX];
46 static uid_t nobody_uid;
47 static gid_t nobody_gid;
48
test_rdonly(void)49 static void test_rdonly(void)
50 {
51 snprintf(file, PATH_MAX, "%s/rdonly", MNTPOINT);
52 TST_EXP_FAIL(otfd = open(file, O_CREAT | O_RDWR, 0700), EROFS);
53 }
54
test_nodev(void)55 static void test_nodev(void)
56 {
57 snprintf(file, PATH_MAX, "%s/nodev", MNTPOINT);
58 SAFE_MKNOD(file, S_IFBLK | 0777, 0);
59 TST_EXP_FAIL(otfd = open(file, O_RDWR, 0700), EACCES);
60 SAFE_UNLINK(file);
61 }
62
test_noexec(void)63 static void test_noexec(void)
64 {
65 snprintf(file, PATH_MAX, "%s/noexec", MNTPOINT);
66 otfd = SAFE_OPEN(file, O_CREAT | O_RDWR, 0700);
67 TST_EXP_FAIL(execlp(file, basename(file), NULL), EACCES);
68 }
69
test_remount(void)70 static void test_remount(void)
71 {
72 SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, MS_REMOUNT, NULL);
73 snprintf(file, PATH_MAX, "%s/remount", MNTPOINT);
74 TST_EXP_FD(otfd = open(file, O_CREAT | O_RDWR, 0700));
75 }
76
test_nosuid(void)77 static void test_nosuid(void)
78 {
79 int ret;
80 struct stat st;
81
82 if (!SAFE_FORK()) {
83 SAFE_CP(TESTBIN, BIN_PATH);
84
85 ret = TST_RETRY_FN_EXP_BACKOFF(access(BIN_PATH, F_OK), !CHECK_ENOENT, 15);
86 if (CHECK_ENOENT(ret))
87 tst_brk(TBROK, "Timeout, %s does not exist", BIN_PATH);
88
89 SAFE_STAT(BIN_PATH, &st);
90 if (st.st_mode != SUID_MODE)
91 SAFE_CHMOD(BIN_PATH, SUID_MODE);
92
93 SAFE_SETREUID(nobody_uid, nobody_uid);
94 SAFE_EXECL(BIN_PATH, BIN_PATH, NULL);
95 tst_brk(TFAIL | TTERRNO, "Failed to execute %s", BIN_PATH);
96 }
97
98 tst_reap_children();
99 }
100
test_file_dir_noatime(int update_fatime,int update_datime)101 static void test_file_dir_noatime(int update_fatime, int update_datime)
102 {
103 time_t atime, dir_atime;
104 struct stat st, dir_st;
105 char readbuf[20];
106 DIR *test_dir;
107
108 snprintf(file, PATH_MAX, "%s/noatime", MNTPOINT);
109 TST_EXP_FD_SILENT(otfd = open(file, O_CREAT | O_RDWR, 0700));
110
111 snprintf(dir, PATH_MAX, "%s/nodiratime", MNTPOINT);
112 if (access(dir, F_OK) == -1 && errno == ENOENT)
113 SAFE_MKDIR(dir, 0700);
114
115 SAFE_WRITE(1, otfd, TEST_STR, strlen(TEST_STR));
116 SAFE_FSTAT(otfd, &st);
117 atime = st.st_atime;
118
119 test_dir = SAFE_OPENDIR(dir);
120 SAFE_STAT(dir, &dir_st);
121 SAFE_READDIR(test_dir);
122 SAFE_CLOSEDIR(test_dir);
123 dir_atime = dir_st.st_atime;
124
125 usleep(1001000);
126
127 SAFE_READ(0, otfd, readbuf, sizeof(readbuf));
128 SAFE_FSTAT(otfd, &st);
129
130 test_dir = SAFE_OPENDIR(dir);
131 SAFE_READDIR(test_dir);
132 SAFE_CLOSEDIR(test_dir);
133 SAFE_STAT(dir, &dir_st);
134
135 if (update_fatime) {
136 if (st.st_atime > atime)
137 tst_res(TPASS, "st.st_atime(%ld) > atime(%ld)",
138 st.st_atime, atime);
139 else
140 tst_res(TFAIL, "st.st_atime(%ld) < atime(%ld)",
141 st.st_atime, atime);
142 } else {
143 TST_EXP_EQ_LI(st.st_atime, atime);
144 }
145
146 if (update_datime) {
147 if (dir_st.st_atime > dir_atime)
148 tst_res(TPASS, "dir_st.st_atime(%ld) > dir_atime(%ld)",
149 dir_st.st_atime, dir_atime);
150 else
151 tst_res(TFAIL, "dir_st.st_atime(%ld) < dir_atime(%ld)",
152 dir_st.st_atime, dir_atime);
153 } else {
154 TST_EXP_EQ_LI(dir_st.st_atime, dir_atime);
155 }
156 }
157
test_noatime(void)158 static void test_noatime(void)
159 {
160 test_file_dir_noatime(0, 0);
161 }
162
test_nodiratime(void)163 static void test_nodiratime(void)
164 {
165 test_file_dir_noatime(1, 0);
166 }
167
test_strictatime(void)168 static void test_strictatime(void)
169 {
170 test_file_dir_noatime(1, 1);
171 }
172
173 #define FLAG_DESC(x) .flag = x, .flag2 = x, .desc = #x
174 #define FLAG_DESC2(x) .flag2 = x, .desc = #x
175 static struct tcase {
176 unsigned int flag;
177 unsigned int flag2;
178 char *desc;
179 void (*test)(void);
180 } tcases[] = {
181 {FLAG_DESC(MS_RDONLY), test_rdonly},
182 {FLAG_DESC(MS_NODEV), test_nodev},
183 {FLAG_DESC(MS_NOEXEC), test_noexec},
184 {MS_RDONLY, FLAG_DESC2(MS_REMOUNT), test_remount},
185 {FLAG_DESC(MS_NOSUID), test_nosuid},
186 {FLAG_DESC(MS_NOATIME), test_noatime},
187 {FLAG_DESC(MS_NODIRATIME), test_nodiratime},
188 {FLAG_DESC(MS_STRICTATIME), test_strictatime}
189 };
190
setup(void)191 static void setup(void)
192 {
193 struct passwd *ltpuser = SAFE_GETPWNAM("nobody");
194
195 nobody_uid = ltpuser->pw_uid;
196 nobody_gid = ltpuser->pw_gid;
197 }
198
cleanup(void)199 static void cleanup(void)
200 {
201 if (otfd >= 0)
202 SAFE_CLOSE(otfd);
203
204 if (tst_is_mounted(MNTPOINT))
205 SAFE_UMOUNT(MNTPOINT);
206 }
207
208
run(unsigned int n)209 static void run(unsigned int n)
210 {
211 struct tcase *tc = &tcases[n];
212 struct statfs stfs;
213
214 tst_res(TINFO, "Testing flag %s", tc->desc);
215
216 TST_EXP_PASS_SILENT(mount(tst_device->dev, MNTPOINT, tst_device->fs_type,
217 tc->flag, NULL));
218 if (!TST_PASS)
219 return;
220
221 if (tc->test)
222 tc->test();
223
224 SAFE_STATFS(MNTPOINT, &stfs);
225 if (tc->flag == MS_STRICTATIME) {
226 if (stfs.f_flags & (MS_NOATIME | MS_RELATIME))
227 tst_res(TFAIL, "statfs() gets the incorrect mount flag");
228 else
229 tst_res(TPASS, "statfs() gets the correct mount flag");
230 cleanup();
231 return;
232 }
233
234 if (stfs.f_flags & tc->flag2)
235 tst_res(TPASS, "statfs() gets the correct mount flag");
236 else
237 tst_res(TFAIL, "statfs() gets the incorrect mount flag");
238
239 cleanup();
240 }
241
242 static struct tst_test test = {
243 .tcnt = ARRAY_SIZE(tcases),
244 .test = run,
245 .setup = setup,
246 .cleanup = cleanup,
247 .needs_root = 1,
248 .format_device = 1,
249 .resource_files = (const char *const[]) {
250 TESTBIN,
251 NULL,
252 },
253 .forks_child = 1,
254 .child_needs_reinit = 1,
255 .mntpoint = MNTPOINT,
256 .all_filesystems = 1,
257 .skip_filesystems = (const char *const []){
258 "exfat",
259 "vfat",
260 "ntfs",
261 NULL
262 },
263 };
264