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) Linux Test Project, 2014-2020
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * errno tests for setns(2) - reassociate thread with a namespace
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
8*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
9*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include <sched.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
16*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
17*49cdfc7eSAndroid Build Coastguard Worker #include "setns.h"
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker static const char nobody_uid[] = "nobody";
20*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *ltpuser;
21*49cdfc7eSAndroid Build Coastguard Worker static int regular_fd;
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker struct testcase_t {
24*49cdfc7eSAndroid Build Coastguard Worker const char *msg;
25*49cdfc7eSAndroid Build Coastguard Worker int fd;
26*49cdfc7eSAndroid Build Coastguard Worker int ns_type;
27*49cdfc7eSAndroid Build Coastguard Worker int exp_ret;
28*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
29*49cdfc7eSAndroid Build Coastguard Worker int skip;
30*49cdfc7eSAndroid Build Coastguard Worker void (*setup) (struct testcase_t *, int i);
31*49cdfc7eSAndroid Build Coastguard Worker void (*cleanup) (struct testcase_t *);
32*49cdfc7eSAndroid Build Coastguard Worker };
33*49cdfc7eSAndroid Build Coastguard Worker
setup0(struct testcase_t * t,int i)34*49cdfc7eSAndroid Build Coastguard Worker static void setup0(struct testcase_t *t, int i)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker t->ns_type = ns_types[i];
37*49cdfc7eSAndroid Build Coastguard Worker }
38*49cdfc7eSAndroid Build Coastguard Worker
setup1(struct testcase_t * t,int i)39*49cdfc7eSAndroid Build Coastguard Worker static void setup1(struct testcase_t *t, int i)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker t->ns_type = ns_types[i];
42*49cdfc7eSAndroid Build Coastguard Worker t->fd = regular_fd;
43*49cdfc7eSAndroid Build Coastguard Worker }
44*49cdfc7eSAndroid Build Coastguard Worker
setup2(struct testcase_t * t,int i)45*49cdfc7eSAndroid Build Coastguard Worker static void setup2(struct testcase_t *t, int i)
46*49cdfc7eSAndroid Build Coastguard Worker {
47*49cdfc7eSAndroid Build Coastguard Worker t->fd = ns_fds[i];
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker
setup3(struct testcase_t * t,int i)50*49cdfc7eSAndroid Build Coastguard Worker static void setup3(struct testcase_t *t, int i)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker if (ns_total < 2) {
53*49cdfc7eSAndroid Build Coastguard Worker t->skip = 1;
54*49cdfc7eSAndroid Build Coastguard Worker return;
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker t->fd = ns_fds[i];
58*49cdfc7eSAndroid Build Coastguard Worker t->ns_type = ns_types[(i+1) % ns_total];
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker
setup4(struct testcase_t * t,int i)61*49cdfc7eSAndroid Build Coastguard Worker static void setup4(struct testcase_t *t, int i)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(ltpuser->pw_uid);
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker t->fd = ns_fds[i];
66*49cdfc7eSAndroid Build Coastguard Worker t->ns_type = ns_types[i];
67*49cdfc7eSAndroid Build Coastguard Worker }
68*49cdfc7eSAndroid Build Coastguard Worker
cleanup4(LTP_ATTRIBUTE_UNUSED struct testcase_t * t)69*49cdfc7eSAndroid Build Coastguard Worker static void cleanup4(LTP_ATTRIBUTE_UNUSED struct testcase_t *t)
70*49cdfc7eSAndroid Build Coastguard Worker {
71*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(0);
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker static struct testcase_t tcases[] = {
75*49cdfc7eSAndroid Build Coastguard Worker {
76*49cdfc7eSAndroid Build Coastguard Worker .msg = "invalid fd",
77*49cdfc7eSAndroid Build Coastguard Worker .fd = -1,
78*49cdfc7eSAndroid Build Coastguard Worker .exp_ret = -1,
79*49cdfc7eSAndroid Build Coastguard Worker .exp_errno = EBADF,
80*49cdfc7eSAndroid Build Coastguard Worker .setup = setup0,
81*49cdfc7eSAndroid Build Coastguard Worker },
82*49cdfc7eSAndroid Build Coastguard Worker {
83*49cdfc7eSAndroid Build Coastguard Worker .msg = "regular file fd",
84*49cdfc7eSAndroid Build Coastguard Worker .exp_ret = -1,
85*49cdfc7eSAndroid Build Coastguard Worker .exp_errno = EINVAL,
86*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
87*49cdfc7eSAndroid Build Coastguard Worker },
88*49cdfc7eSAndroid Build Coastguard Worker {
89*49cdfc7eSAndroid Build Coastguard Worker .msg = "invalid ns_type",
90*49cdfc7eSAndroid Build Coastguard Worker .ns_type = -1,
91*49cdfc7eSAndroid Build Coastguard Worker .exp_ret = -1,
92*49cdfc7eSAndroid Build Coastguard Worker .exp_errno = EINVAL,
93*49cdfc7eSAndroid Build Coastguard Worker .setup = setup2,
94*49cdfc7eSAndroid Build Coastguard Worker },
95*49cdfc7eSAndroid Build Coastguard Worker {
96*49cdfc7eSAndroid Build Coastguard Worker .msg = "mismatch ns_type/fd",
97*49cdfc7eSAndroid Build Coastguard Worker .exp_ret = -1,
98*49cdfc7eSAndroid Build Coastguard Worker .exp_errno = EINVAL,
99*49cdfc7eSAndroid Build Coastguard Worker .setup = setup3,
100*49cdfc7eSAndroid Build Coastguard Worker },
101*49cdfc7eSAndroid Build Coastguard Worker {
102*49cdfc7eSAndroid Build Coastguard Worker .msg = "without CAP_SYS_ADMIN",
103*49cdfc7eSAndroid Build Coastguard Worker .exp_ret = -1,
104*49cdfc7eSAndroid Build Coastguard Worker .exp_errno = EPERM,
105*49cdfc7eSAndroid Build Coastguard Worker .setup = setup4,
106*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup4,
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker };
109*49cdfc7eSAndroid Build Coastguard Worker
test_setns(unsigned int testno)110*49cdfc7eSAndroid Build Coastguard Worker static void test_setns(unsigned int testno)
111*49cdfc7eSAndroid Build Coastguard Worker {
112*49cdfc7eSAndroid Build Coastguard Worker int ret, i;
113*49cdfc7eSAndroid Build Coastguard Worker struct testcase_t *t = &tcases[testno];
114*49cdfc7eSAndroid Build Coastguard Worker
115*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ns_total; i++) {
116*49cdfc7eSAndroid Build Coastguard Worker if (t->setup)
117*49cdfc7eSAndroid Build Coastguard Worker t->setup(t, i);
118*49cdfc7eSAndroid Build Coastguard Worker
119*49cdfc7eSAndroid Build Coastguard Worker if (t->skip) {
120*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF, "skip %s", t->msg);
121*49cdfc7eSAndroid Build Coastguard Worker continue;
122*49cdfc7eSAndroid Build Coastguard Worker }
123*49cdfc7eSAndroid Build Coastguard Worker
124*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "setns(%d, 0x%x)", t->fd, t->ns_type);
125*49cdfc7eSAndroid Build Coastguard Worker ret = tst_syscall(__NR_setns, t->fd, t->ns_type);
126*49cdfc7eSAndroid Build Coastguard Worker if (ret == t->exp_ret) {
127*49cdfc7eSAndroid Build Coastguard Worker if (ret == -1 && errno == t->exp_errno) {
128*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "%s exp_errno=%d (%s)",
129*49cdfc7eSAndroid Build Coastguard Worker t->msg, t->exp_errno,
130*49cdfc7eSAndroid Build Coastguard Worker tst_strerrno(t->exp_errno));
131*49cdfc7eSAndroid Build Coastguard Worker } else {
132*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL|TERRNO, "%s exp_errno=%d (%s)",
133*49cdfc7eSAndroid Build Coastguard Worker t->msg, t->exp_errno,
134*49cdfc7eSAndroid Build Coastguard Worker tst_strerrno(t->exp_errno));
135*49cdfc7eSAndroid Build Coastguard Worker }
136*49cdfc7eSAndroid Build Coastguard Worker } else {
137*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s ret=%d expected=%d",
138*49cdfc7eSAndroid Build Coastguard Worker t->msg, ret, t->exp_ret);
139*49cdfc7eSAndroid Build Coastguard Worker }
140*49cdfc7eSAndroid Build Coastguard Worker
141*49cdfc7eSAndroid Build Coastguard Worker if (t->cleanup)
142*49cdfc7eSAndroid Build Coastguard Worker t->cleanup(t);
143*49cdfc7eSAndroid Build Coastguard Worker }
144*49cdfc7eSAndroid Build Coastguard Worker }
145*49cdfc7eSAndroid Build Coastguard Worker
setup(void)146*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
147*49cdfc7eSAndroid Build Coastguard Worker {
148*49cdfc7eSAndroid Build Coastguard Worker /* runtime check if syscall is supported */
149*49cdfc7eSAndroid Build Coastguard Worker tst_syscall(__NR_setns, -1, 0);
150*49cdfc7eSAndroid Build Coastguard Worker
151*49cdfc7eSAndroid Build Coastguard Worker init_available_ns();
152*49cdfc7eSAndroid Build Coastguard Worker if (ns_total == 0)
153*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF, "no ns types/proc entries");
154*49cdfc7eSAndroid Build Coastguard Worker
155*49cdfc7eSAndroid Build Coastguard Worker ltpuser = SAFE_GETPWNAM(nobody_uid);
156*49cdfc7eSAndroid Build Coastguard Worker regular_fd = SAFE_OPEN("dummy", O_RDWR|O_CREAT, 0600);
157*49cdfc7eSAndroid Build Coastguard Worker SAFE_UNLINK("dummy");
158*49cdfc7eSAndroid Build Coastguard Worker }
159*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)160*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
161*49cdfc7eSAndroid Build Coastguard Worker {
162*49cdfc7eSAndroid Build Coastguard Worker close_ns_fds();
163*49cdfc7eSAndroid Build Coastguard Worker if (regular_fd)
164*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(regular_fd);
165*49cdfc7eSAndroid Build Coastguard Worker }
166*49cdfc7eSAndroid Build Coastguard Worker
167*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
168*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
169*49cdfc7eSAndroid Build Coastguard Worker .test = test_setns,
170*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
171*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
172*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
173*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
174*49cdfc7eSAndroid Build Coastguard Worker };
175*49cdfc7eSAndroid Build Coastguard Worker
176