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