Skip to content

Commit ebdd094

Browse files
ebiedermgregkh
authored andcommitted
mnt: Update unprivileged remount test
commit 4a44a19 upstream. - MNT_NODEV should be irrelevant except when reading back mount flags, no longer specify MNT_NODEV on remount. - Test MNT_NODEV on devpts where it is meaningful even for unprivileged mounts. - Add a test to verify that remount of a prexisting mount with the same flags is allowed and does not change those flags. - Cleanup up the definitions of MS_REC, MS_RELATIME, MS_STRICTATIME that are used when the code is built in an environment without them. - Correct the test error messages when tests fail. There were not 5 tests that tested MS_RELATIME. Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent aad34f7 commit ebdd094

1 file changed

Lines changed: 142 additions & 30 deletions

File tree

‎tools/testing/selftests/mount/unprivileged-remount-test.c‎

Lines changed: 142 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <sys/types.h>
77
#include <sys/mount.h>
88
#include <sys/wait.h>
9+
#include <sys/vfs.h>
10+
#include <sys/statvfs.h>
911
#include <stdlib.h>
1012
#include <unistd.h>
1113
#include <fcntl.h>
@@ -32,11 +34,14 @@
3234
# define CLONE_NEWPID 0x20000000
3335
#endif
3436

37+
#ifndef MS_REC
38+
# define MS_REC 16384
39+
#endif
3540
#ifndef MS_RELATIME
36-
#define MS_RELATIME (1 << 21)
41+
# define MS_RELATIME (1 << 21)
3742
#endif
3843
#ifndef MS_STRICTATIME
39-
#define MS_STRICTATIME (1 << 24)
44+
# define MS_STRICTATIME (1 << 24)
4045
#endif
4146

4247
static void die(char *fmt, ...)
@@ -87,6 +92,45 @@ static void write_file(char *filename, char *fmt, ...)
8792
}
8893
}
8994

95+
static int read_mnt_flags(const char *path)
96+
{
97+
int ret;
98+
struct statvfs stat;
99+
int mnt_flags;
100+
101+
ret = statvfs(path, &stat);
102+
if (ret != 0) {
103+
die("statvfs of %s failed: %s\n",
104+
path, strerror(errno));
105+
}
106+
if (stat.f_flag & ~(ST_RDONLY | ST_NOSUID | ST_NODEV | \
107+
ST_NOEXEC | ST_NOATIME | ST_NODIRATIME | ST_RELATIME | \
108+
ST_SYNCHRONOUS | ST_MANDLOCK)) {
109+
die("Unrecognized mount flags\n");
110+
}
111+
mnt_flags = 0;
112+
if (stat.f_flag & ST_RDONLY)
113+
mnt_flags |= MS_RDONLY;
114+
if (stat.f_flag & ST_NOSUID)
115+
mnt_flags |= MS_NOSUID;
116+
if (stat.f_flag & ST_NODEV)
117+
mnt_flags |= MS_NODEV;
118+
if (stat.f_flag & ST_NOEXEC)
119+
mnt_flags |= MS_NOEXEC;
120+
if (stat.f_flag & ST_NOATIME)
121+
mnt_flags |= MS_NOATIME;
122+
if (stat.f_flag & ST_NODIRATIME)
123+
mnt_flags |= MS_NODIRATIME;
124+
if (stat.f_flag & ST_RELATIME)
125+
mnt_flags |= MS_RELATIME;
126+
if (stat.f_flag & ST_SYNCHRONOUS)
127+
mnt_flags |= MS_SYNCHRONOUS;
128+
if (stat.f_flag & ST_MANDLOCK)
129+
mnt_flags |= ST_MANDLOCK;
130+
131+
return mnt_flags;
132+
}
133+
90134
static void create_and_enter_userns(void)
91135
{
92136
uid_t uid;
@@ -118,7 +162,8 @@ static void create_and_enter_userns(void)
118162
}
119163

120164
static
121-
bool test_unpriv_remount(int mount_flags, int remount_flags, int invalid_flags)
165+
bool test_unpriv_remount(const char *fstype, const char *mount_options,
166+
int mount_flags, int remount_flags, int invalid_flags)
122167
{
123168
pid_t child;
124169

@@ -151,9 +196,11 @@ bool test_unpriv_remount(int mount_flags, int remount_flags, int invalid_flags)
151196
strerror(errno));
152197
}
153198

154-
if (mount("testing", "/tmp", "ramfs", mount_flags, NULL) != 0) {
155-
die("mount of /tmp failed: %s\n",
156-
strerror(errno));
199+
if (mount("testing", "/tmp", fstype, mount_flags, mount_options) != 0) {
200+
die("mount of %s with options '%s' on /tmp failed: %s\n",
201+
fstype,
202+
mount_options? mount_options : "",
203+
strerror(errno));
157204
}
158205

159206
create_and_enter_userns();
@@ -181,62 +228,127 @@ bool test_unpriv_remount(int mount_flags, int remount_flags, int invalid_flags)
181228

182229
static bool test_unpriv_remount_simple(int mount_flags)
183230
{
184-
return test_unpriv_remount(mount_flags, mount_flags, 0);
231+
return test_unpriv_remount("ramfs", NULL, mount_flags, mount_flags, 0);
185232
}
186233

187234
static bool test_unpriv_remount_atime(int mount_flags, int invalid_flags)
188235
{
189-
return test_unpriv_remount(mount_flags, mount_flags, invalid_flags);
236+
return test_unpriv_remount("ramfs", NULL, mount_flags, mount_flags,
237+
invalid_flags);
238+
}
239+
240+
static bool test_priv_mount_unpriv_remount(void)
241+
{
242+
pid_t child;
243+
int ret;
244+
const char *orig_path = "/dev";
245+
const char *dest_path = "/tmp";
246+
int orig_mnt_flags, remount_mnt_flags;
247+
248+
child = fork();
249+
if (child == -1) {
250+
die("fork failed: %s\n",
251+
strerror(errno));
252+
}
253+
if (child != 0) { /* parent */
254+
pid_t pid;
255+
int status;
256+
pid = waitpid(child, &status, 0);
257+
if (pid == -1) {
258+
die("waitpid failed: %s\n",
259+
strerror(errno));
260+
}
261+
if (pid != child) {
262+
die("waited for %d got %d\n",
263+
child, pid);
264+
}
265+
if (!WIFEXITED(status)) {
266+
die("child did not terminate cleanly\n");
267+
}
268+
return WEXITSTATUS(status) == EXIT_SUCCESS ? true : false;
269+
}
270+
271+
orig_mnt_flags = read_mnt_flags(orig_path);
272+
273+
create_and_enter_userns();
274+
ret = unshare(CLONE_NEWNS);
275+
if (ret != 0) {
276+
die("unshare(CLONE_NEWNS) failed: %s\n",
277+
strerror(errno));
278+
}
279+
280+
ret = mount(orig_path, dest_path, "bind", MS_BIND | MS_REC, NULL);
281+
if (ret != 0) {
282+
die("recursive bind mount of %s onto %s failed: %s\n",
283+
orig_path, dest_path, strerror(errno));
284+
}
285+
286+
ret = mount(dest_path, dest_path, "none",
287+
MS_REMOUNT | MS_BIND | orig_mnt_flags , NULL);
288+
if (ret != 0) {
289+
/* system("cat /proc/self/mounts"); */
290+
die("remount of /tmp failed: %s\n",
291+
strerror(errno));
292+
}
293+
294+
remount_mnt_flags = read_mnt_flags(dest_path);
295+
if (orig_mnt_flags != remount_mnt_flags) {
296+
die("Mount flags unexpectedly changed during remount of %s originally mounted on %s\n",
297+
dest_path, orig_path);
298+
}
299+
exit(EXIT_SUCCESS);
190300
}
191301

192302
int main(int argc, char **argv)
193303
{
194-
if (!test_unpriv_remount_simple(MS_RDONLY|MS_NODEV)) {
304+
if (!test_unpriv_remount_simple(MS_RDONLY)) {
195305
die("MS_RDONLY malfunctions\n");
196306
}
197-
if (!test_unpriv_remount_simple(MS_NODEV)) {
307+
if (!test_unpriv_remount("devpts", "newinstance", MS_NODEV, MS_NODEV, 0)) {
198308
die("MS_NODEV malfunctions\n");
199309
}
200-
if (!test_unpriv_remount_simple(MS_NOSUID|MS_NODEV)) {
310+
if (!test_unpriv_remount_simple(MS_NOSUID)) {
201311
die("MS_NOSUID malfunctions\n");
202312
}
203-
if (!test_unpriv_remount_simple(MS_NOEXEC|MS_NODEV)) {
313+
if (!test_unpriv_remount_simple(MS_NOEXEC)) {
204314
die("MS_NOEXEC malfunctions\n");
205315
}
206-
if (!test_unpriv_remount_atime(MS_RELATIME|MS_NODEV,
207-
MS_NOATIME|MS_NODEV))
316+
if (!test_unpriv_remount_atime(MS_RELATIME,
317+
MS_NOATIME))
208318
{
209319
die("MS_RELATIME malfunctions\n");
210320
}
211-
if (!test_unpriv_remount_atime(MS_STRICTATIME|MS_NODEV,
212-
MS_NOATIME|MS_NODEV))
321+
if (!test_unpriv_remount_atime(MS_STRICTATIME,
322+
MS_NOATIME))
213323
{
214324
die("MS_STRICTATIME malfunctions\n");
215325
}
216-
if (!test_unpriv_remount_atime(MS_NOATIME|MS_NODEV,
217-
MS_STRICTATIME|MS_NODEV))
326+
if (!test_unpriv_remount_atime(MS_NOATIME,
327+
MS_STRICTATIME))
218328
{
219-
die("MS_RELATIME malfunctions\n");
329+
die("MS_NOATIME malfunctions\n");
220330
}
221-
if (!test_unpriv_remount_atime(MS_RELATIME|MS_NODIRATIME|MS_NODEV,
222-
MS_NOATIME|MS_NODEV))
331+
if (!test_unpriv_remount_atime(MS_RELATIME|MS_NODIRATIME,
332+
MS_NOATIME))
223333
{
224-
die("MS_RELATIME malfunctions\n");
334+
die("MS_RELATIME|MS_NODIRATIME malfunctions\n");
225335
}
226-
if (!test_unpriv_remount_atime(MS_STRICTATIME|MS_NODIRATIME|MS_NODEV,
227-
MS_NOATIME|MS_NODEV))
336+
if (!test_unpriv_remount_atime(MS_STRICTATIME|MS_NODIRATIME,
337+
MS_NOATIME))
228338
{
229-
die("MS_RELATIME malfunctions\n");
339+
die("MS_STRICTATIME|MS_NODIRATIME malfunctions\n");
230340
}
231-
if (!test_unpriv_remount_atime(MS_NOATIME|MS_NODIRATIME|MS_NODEV,
232-
MS_STRICTATIME|MS_NODEV))
341+
if (!test_unpriv_remount_atime(MS_NOATIME|MS_NODIRATIME,
342+
MS_STRICTATIME))
233343
{
234-
die("MS_RELATIME malfunctions\n");
344+
die("MS_NOATIME|MS_DIRATIME malfunctions\n");
235345
}
236-
if (!test_unpriv_remount(MS_STRICTATIME|MS_NODEV, MS_NODEV,
237-
MS_NOATIME|MS_NODEV))
346+
if (!test_unpriv_remount("ramfs", NULL, MS_STRICTATIME, 0, MS_NOATIME))
238347
{
239348
die("Default atime malfunctions\n");
240349
}
350+
if (!test_priv_mount_unpriv_remount()) {
351+
die("Mount flags unexpectedly changed after remount\n");
352+
}
241353
return EXIT_SUCCESS;
242354
}

0 commit comments

Comments
 (0)