1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Crackerjack Project., 2007
4 * Ported from Crackerjack to LTP by Manas Kumar Nayak maknayak@in.ibm.com>
5 */
6
7 /*\
8 * [Description]
9 *
10 * Basic tests for the unshare() errors.
11 *
12 * - EINVAL on invalid flags
13 * - EPERM when process is missing required privileges
14 */
15
16 #define _GNU_SOURCE
17
18 #include <stdio.h>
19 #include <sys/wait.h>
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/syscall.h>
23 #include <sched.h>
24 #include <limits.h>
25 #include <unistd.h>
26 #include <pwd.h>
27
28 #include "tst_test.h"
29 #include "config.h"
30
31 #ifdef HAVE_UNSHARE
32
33 static uid_t nobody_uid;
34
35 static struct test_case_t {
36 int mode;
37 int expected_error;
38 const char *desc;
39 } tc[] = {
40 {-1, EINVAL, "-1"},
41 {CLONE_NEWNS, EPERM, "CLONE_NEWNS"}
42 };
43
run(unsigned int i)44 static void run(unsigned int i)
45 {
46 pid_t pid = SAFE_FORK();
47 if (pid == 0) {
48 if (tc[i].expected_error == EPERM)
49 SAFE_SETUID(nobody_uid);
50
51 TST_EXP_FAIL(unshare(tc[i].mode), tc[i].expected_error,
52 "unshare(%s)", tc[i].desc);
53 }
54 }
55
setup(void)56 static void setup(void)
57 {
58 struct passwd *ltpuser = SAFE_GETPWNAM("nobody");
59 nobody_uid = ltpuser->pw_uid;
60 }
61
62 static struct tst_test test = {
63 .tcnt = ARRAY_SIZE(tc),
64 .forks_child = 1,
65 .needs_tmpdir = 1,
66 .needs_root = 1,
67 .setup = setup,
68 .test = run,
69 };
70
71 #else
72 TST_TEST_TCONF("unshare is undefined.");
73 #endif
74