• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2019 Federico Bonfiglio fedebonfi95@gmail.com
4  */
5 
6 /*
7  * Test ioctl_ns with NS_GET_USERNS request.
8  *
9  * Owning user namespace of process calling ioctl is out of scope,
10  * which should make the call fail with EPERM.
11  *
12  */
13 #define _GNU_SOURCE
14 
15 #include <errno.h>
16 #include "tst_test.h"
17 #include "lapi/ioctl_ns.h"
18 
setup(void)19 static void setup(void)
20 {
21 	int exists = access("/proc/self/ns/user", F_OK);
22 
23 	if (exists < 0)
24 		tst_res(TCONF, "namespace not available");
25 }
26 
run(void)27 static void run(void)
28 {
29 	int fd, parent_fd;
30 
31 	fd = SAFE_OPEN("/proc/self/ns/user", O_RDONLY);
32 	parent_fd = ioctl(fd, NS_GET_USERNS);
33 	if (parent_fd == -1) {
34 		if (errno == ENOTTY)
35 			tst_brk(TCONF, "ioctl(NS_GET_USERNS) not implemented");
36 
37 		if (errno == EPERM)
38 			tst_res(TPASS, "NS_GET_USERNS fails with EPERM");
39 		else
40 			tst_res(TFAIL | TERRNO, "unexpected ioctl error");
41 	} else {
42 		SAFE_CLOSE(fd);
43 		tst_res(TFAIL, "call to ioctl succeded");
44 	}
45 }
46 
47 static struct tst_test test = {
48 	.test_all = run,
49 	.min_kver = "4.9",
50 	.setup = setup
51 };
52