• 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_PARENT request.
8  *
9  * Parent process tries to get parent of initial namespace, which should
10  * fail with EPERM because it has no parent.
11  *
12  * Child process has a new pid namespace, which should make the call fail
13  * with EPERM error.
14  *
15  */
16 #define _GNU_SOURCE
17 
18 #include <errno.h>
19 #include <sched.h>
20 #include <stdlib.h>
21 #include "tst_test.h"
22 #include "lapi/ioctl_ns.h"
23 #include "lapi/namespaces_constants.h"
24 
25 #define STACK_SIZE (1024 * 1024)
26 
27 static char *child_stack;
28 
setup(void)29 static void setup(void)
30 {
31 	int exists = access("/proc/self/ns/pid", F_OK);
32 
33 	if (exists < 0)
34 		tst_res(TCONF, "namespace not available");
35 
36 	child_stack = ltp_alloc_stack(STACK_SIZE);
37 	if (!child_stack)
38 		tst_brk(TBROK|TERRNO, "stack alloc");
39 }
40 
cleanup(void)41 static void cleanup(void)
42 {
43 	free(child_stack);
44 }
45 
test_ns_get_parent(void)46 static void test_ns_get_parent(void)
47 {
48 	int fd, parent_fd;
49 
50 	fd = SAFE_OPEN("/proc/self/ns/pid", O_RDONLY);
51 	parent_fd = ioctl(fd, NS_GET_PARENT);
52 	if (parent_fd == -1) {
53 		if (errno == ENOTTY)
54 			tst_brk(TCONF, "ioctl(NS_GET_PARENT) not implemented");
55 
56 		if (errno == EPERM)
57 			tst_res(TPASS, "NS_GET_PARENT fails with EPERM");
58 		else
59 			tst_res(TFAIL | TERRNO, "unexpected ioctl error");
60 	} else {
61 		SAFE_CLOSE(fd);
62 		tst_res(TFAIL, "call to ioctl succeded");
63 	}
64 }
65 
child(void * arg LTP_ATTRIBUTE_UNUSED)66 static int child(void *arg LTP_ATTRIBUTE_UNUSED)
67 {
68 	test_ns_get_parent();
69 	return 0;
70 }
71 
run(void)72 static void run(void)
73 {
74 	test_ns_get_parent();
75 
76 	if (ltp_clone(CLONE_NEWPID | SIGCHLD, &child, 0,
77 		STACK_SIZE, child_stack) == -1)
78 		tst_brk(TBROK | TERRNO, "ltp_clone failed");
79 }
80 
81 static struct tst_test test = {
82 	.test_all = run,
83 	.forks_child = 1,
84 	.needs_root = 1,
85 	.min_kver = "4.9",
86 	.setup = setup,
87 	.cleanup = cleanup,
88 };
89