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) 2023 SUSE LLC Andrea Cervesato <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * Drop root privileges, create a container with CLONE_NEWUTS and verify that
10*49cdfc7eSAndroid Build Coastguard Worker * we receive a permission error.
11*49cdfc7eSAndroid Build Coastguard Worker */
12*49cdfc7eSAndroid Build Coastguard Worker
13*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
14*49cdfc7eSAndroid Build Coastguard Worker
15*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker static char *str_op;
20*49cdfc7eSAndroid Build Coastguard Worker
run(void)21*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker const struct tst_clone_args cargs = {
24*49cdfc7eSAndroid Build Coastguard Worker .flags = CLONE_NEWUTS,
25*49cdfc7eSAndroid Build Coastguard Worker .exit_signal = SIGCHLD,
26*49cdfc7eSAndroid Build Coastguard Worker };
27*49cdfc7eSAndroid Build Coastguard Worker struct passwd *pw;
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Dropping root privileges");
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker pw = SAFE_GETPWNAM("nobody");
32*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETRESUID(pw->pw_uid, pw->pw_uid, pw->pw_uid);
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker if (!str_op || !strcmp(str_op, "clone")) {
35*49cdfc7eSAndroid Build Coastguard Worker TEST(tst_clone(&cargs));
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1)
38*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "clone3() fails as expected");
39*49cdfc7eSAndroid Build Coastguard Worker else if (TST_RET == -2)
40*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "clone() fails as expected");
41*49cdfc7eSAndroid Build Coastguard Worker else
42*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "tst_clone returns %ld", TST_RET);
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_PASS(errno == EPERM);
45*49cdfc7eSAndroid Build Coastguard Worker } else {
46*49cdfc7eSAndroid Build Coastguard Worker if (!SAFE_FORK()) {
47*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_EQ_LI(unshare(CLONE_NEWUTS), -1);
48*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_PASS(errno == EPERM);
49*49cdfc7eSAndroid Build Coastguard Worker return;
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
55*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
56*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
57*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
58*49cdfc7eSAndroid Build Coastguard Worker .needs_checkpoints = 1,
59*49cdfc7eSAndroid Build Coastguard Worker .options = (struct tst_option[]) {
60*49cdfc7eSAndroid Build Coastguard Worker { "m:", &str_op, "Test execution mode <clone|unshare>" },
61*49cdfc7eSAndroid Build Coastguard Worker {},
62*49cdfc7eSAndroid Build Coastguard Worker },
63*49cdfc7eSAndroid Build Coastguard Worker };
64