Skip to content

Commit 8199e6f

Browse files
amir73ilbrauner
authored andcommitted
selftests/filesystems: create setup_userns() helper
Add helper to utils.c and use it in statmount userns tests. Reviewed-by: John Hubbard <[email protected]> Signed-off-by: Amir Goldstein <[email protected]> Link: https://lore.kernel.org/[email protected] Reviewed-by: Christian Brauner <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent e897b9b commit 8199e6f

File tree

3 files changed

+68
-57
lines changed

3 files changed

+68
-57
lines changed

tools/testing/selftests/filesystems/statmount/statmount_test_ns.c

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -79,66 +79,10 @@ static int get_mnt_ns_id(const char *mnt_ns, uint64_t *mnt_ns_id)
7979
return NSID_PASS;
8080
}
8181

82-
static int write_file(const char *path, const char *val)
83-
{
84-
int fd = open(path, O_WRONLY);
85-
size_t len = strlen(val);
86-
int ret;
87-
88-
if (fd == -1) {
89-
ksft_print_msg("opening %s for write: %s\n", path, strerror(errno));
90-
return NSID_ERROR;
91-
}
92-
93-
ret = write(fd, val, len);
94-
if (ret == -1) {
95-
ksft_print_msg("writing to %s: %s\n", path, strerror(errno));
96-
return NSID_ERROR;
97-
}
98-
if (ret != len) {
99-
ksft_print_msg("short write to %s\n", path);
100-
return NSID_ERROR;
101-
}
102-
103-
ret = close(fd);
104-
if (ret == -1) {
105-
ksft_print_msg("closing %s\n", path);
106-
return NSID_ERROR;
107-
}
108-
109-
return NSID_PASS;
110-
}
111-
11282
static int setup_namespace(void)
11383
{
114-
int ret;
115-
char buf[32];
116-
uid_t uid = getuid();
117-
gid_t gid = getgid();
118-
119-
ret = unshare(CLONE_NEWNS|CLONE_NEWUSER|CLONE_NEWPID);
120-
if (ret == -1)
121-
ksft_exit_fail_msg("unsharing mountns and userns: %s\n",
122-
strerror(errno));
123-
124-
sprintf(buf, "0 %d 1", uid);
125-
ret = write_file("/proc/self/uid_map", buf);
126-
if (ret != NSID_PASS)
127-
return ret;
128-
ret = write_file("/proc/self/setgroups", "deny");
129-
if (ret != NSID_PASS)
130-
return ret;
131-
sprintf(buf, "0 %d 1", gid);
132-
ret = write_file("/proc/self/gid_map", buf);
133-
if (ret != NSID_PASS)
134-
return ret;
135-
136-
ret = mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL);
137-
if (ret == -1) {
138-
ksft_print_msg("making mount tree private: %s\n",
139-
strerror(errno));
84+
if (setup_userns() != 0)
14085
return NSID_ERROR;
141-
}
14286

14387
return NSID_PASS;
14488
}

tools/testing/selftests/filesystems/utils.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <sys/types.h>
1919
#include <sys/wait.h>
2020
#include <sys/xattr.h>
21+
#include <sys/mount.h>
2122

2223
#include "../kselftest.h"
2324
#include "wrappers.h"
@@ -449,6 +450,71 @@ static int create_userns_hierarchy(struct userns_hierarchy *h)
449450
return fret;
450451
}
451452

453+
static int write_file(const char *path, const char *val)
454+
{
455+
int fd = open(path, O_WRONLY);
456+
size_t len = strlen(val);
457+
int ret;
458+
459+
if (fd == -1) {
460+
ksft_print_msg("opening %s for write: %s\n", path, strerror(errno));
461+
return -1;
462+
}
463+
464+
ret = write(fd, val, len);
465+
if (ret == -1) {
466+
ksft_print_msg("writing to %s: %s\n", path, strerror(errno));
467+
return -1;
468+
}
469+
if (ret != len) {
470+
ksft_print_msg("short write to %s\n", path);
471+
return -1;
472+
}
473+
474+
ret = close(fd);
475+
if (ret == -1) {
476+
ksft_print_msg("closing %s\n", path);
477+
return -1;
478+
}
479+
480+
return 0;
481+
}
482+
483+
int setup_userns(void)
484+
{
485+
int ret;
486+
char buf[32];
487+
uid_t uid = getuid();
488+
gid_t gid = getgid();
489+
490+
ret = unshare(CLONE_NEWNS|CLONE_NEWUSER|CLONE_NEWPID);
491+
if (ret) {
492+
ksft_exit_fail_msg("unsharing mountns and userns: %s\n",
493+
strerror(errno));
494+
return ret;
495+
}
496+
497+
sprintf(buf, "0 %d 1", uid);
498+
ret = write_file("/proc/self/uid_map", buf);
499+
if (ret)
500+
return ret;
501+
ret = write_file("/proc/self/setgroups", "deny");
502+
if (ret)
503+
return ret;
504+
sprintf(buf, "0 %d 1", gid);
505+
ret = write_file("/proc/self/gid_map", buf);
506+
if (ret)
507+
return ret;
508+
509+
ret = mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL);
510+
if (ret) {
511+
ksft_print_msg("making mount tree private: %s\n", strerror(errno));
512+
return ret;
513+
}
514+
515+
return 0;
516+
}
517+
452518
/* caps_down - lower all effective caps */
453519
int caps_down(void)
454520
{

tools/testing/selftests/filesystems/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern int caps_down(void);
2727
extern int cap_down(cap_value_t down);
2828

2929
extern bool switch_ids(uid_t uid, gid_t gid);
30+
extern int setup_userns(void);
3031

3132
static inline bool switch_userns(int fd, uid_t uid, gid_t gid, bool drop_caps)
3233
{

0 commit comments

Comments
 (0)