1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Huawei Technologies Co., Ltd., 2015
4 * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
5 */
6
7 /*\
8 * [Description]
9 *
10 * Verify that the user ID and group ID, which are inside a container,
11 * can be modified by its parent process.
12 */
13
14 #define _GNU_SOURCE
15
16 #include <stdio.h>
17 #include "tst_test.h"
18 #include "lapi/sched.h"
19
child_fn1(void)20 static void child_fn1(void)
21 {
22 int uid, gid;
23
24 TST_CHECKPOINT_WAIT(0);
25
26 uid = geteuid();
27 gid = getegid();
28
29 TST_EXP_EQ_LI(uid, 100);
30 TST_EXP_EQ_LI(gid, 100);
31 }
32
run(void)33 static void run(void)
34 {
35 const struct tst_clone_args args = {
36 .flags = CLONE_NEWUSER,
37 .exit_signal = SIGCHLD,
38 };
39 int childpid;
40 int parentuid;
41 int parentgid;
42 char path[BUFSIZ];
43
44 childpid = SAFE_CLONE(&args);
45 if (!childpid) {
46 child_fn1();
47 return;
48 }
49
50 parentuid = geteuid();
51 parentgid = getegid();
52
53 sprintf(path, "/proc/%d/uid_map", childpid);
54 SAFE_FILE_PRINTF(path, "100 %d 1", parentuid);
55
56 if (access("/proc/self/setgroups", F_OK) == 0) {
57 sprintf(path, "/proc/%d/setgroups", childpid);
58 SAFE_FILE_PRINTF(path, "deny");
59 }
60
61 sprintf(path, "/proc/%d/gid_map", childpid);
62 SAFE_FILE_PRINTF(path, "100 %d 1", parentgid);
63
64 TST_CHECKPOINT_WAKE(0);
65 }
66
67 static struct tst_test test = {
68 .test_all = run,
69 .needs_root = 1,
70 .forks_child = 1,
71 .needs_checkpoints = 1,
72 .needs_kconfigs = (const char *[]) {
73 "CONFIG_USER_NS",
74 NULL,
75 },
76 };
77