1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Ported by John George
5 * Copyright (C) 2021 SUSE LLC <mdoucha@suse.cz>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Test setreuid() when executed by an unpriviledged user.
12 */
13
14 #include <sys/types.h>
15 #include <pwd.h>
16
17 #include "tst_test.h"
18 #include "tst_uid.h"
19 #include "compat_tst_16.h"
20
21 static uid_t root_uid, nobody_uid, other_uid, neg_one = -1;
22
23 static struct test_data_t {
24 uid_t *real_uid;
25 uid_t *eff_uid;
26 int exp_ret;
27 uid_t *exp_real_uid;
28 uid_t *exp_eff_uid;
29 uid_t *exp_sav_uid;
30 const char *test_msg;
31 } test_data[] = {
32 {&nobody_uid, &nobody_uid, 0, &nobody_uid, &nobody_uid, &nobody_uid,
33 "setreuid(nobody, nobody)"},
34 {&neg_one, &nobody_uid, 0, &nobody_uid, &nobody_uid, &nobody_uid,
35 "setreuid(-1, nobody)"},
36 {&nobody_uid, &neg_one, 0, &nobody_uid, &nobody_uid, &nobody_uid,
37 "setreuid(nobody, -1)"},
38 {&neg_one, &neg_one, 0, &nobody_uid, &nobody_uid, &nobody_uid,
39 "setreuid(-1, -1)"},
40 {&neg_one, &root_uid, -1, &nobody_uid, &nobody_uid, &nobody_uid,
41 "setreuid(-1, root)"},
42 {&root_uid, &neg_one, -1, &nobody_uid, &nobody_uid, &nobody_uid,
43 "setreuid(root, -1)"},
44 {&root_uid, &root_uid, -1, &nobody_uid, &nobody_uid,
45 &nobody_uid, "setreuid(root, root)"},
46 {&root_uid, &nobody_uid, -1, &nobody_uid, &nobody_uid,
47 &nobody_uid, "setreuid(root, nobody)"},
48 {&root_uid, &other_uid, -1, &nobody_uid, &nobody_uid,
49 &nobody_uid, "setreuid(root, other)"},
50 {&other_uid, &root_uid, -1, &nobody_uid, &nobody_uid,
51 &nobody_uid, "setreuid(other, root)"},
52 {&other_uid, &neg_one, -1, &nobody_uid, &nobody_uid,
53 &nobody_uid, "setreuid(other, -1)"},
54 {&other_uid, &other_uid, -1, &nobody_uid, &nobody_uid,
55 &nobody_uid, "setreuid(other, other)"},
56 {&other_uid, &nobody_uid, -1, &nobody_uid, &nobody_uid,
57 &nobody_uid, "setreuid(other, nobody)"},
58 {&nobody_uid, &other_uid, -1, &nobody_uid, &nobody_uid,
59 &nobody_uid, "setreuid(nobody, other)"},
60 };
61
setup(void)62 static void setup(void)
63 {
64 uid_t test_users[2];
65 struct passwd *pw;
66
67 root_uid = getuid();
68 pw = SAFE_GETPWNAM("nobody");
69 nobody_uid = test_users[0] = pw->pw_uid;
70 tst_get_uids(test_users, 1, 2);
71 other_uid = test_users[1];
72
73 UID16_CHECK(root_uid, setreuid);
74 UID16_CHECK(nobody_uid, setreuid);
75 UID16_CHECK(other_uid, setreuid);
76
77 SAFE_SETUID(nobody_uid);
78 }
79
run(unsigned int n)80 static void run(unsigned int n)
81 {
82 const struct test_data_t *tc = test_data + n;
83
84 if (tc->exp_ret) {
85 TST_EXP_FAIL(SETREUID(*tc->real_uid, *tc->eff_uid), EPERM,
86 "%s", tc->test_msg);
87 } else {
88 TST_EXP_PASS(SETREUID(*tc->real_uid, *tc->eff_uid), "%s",
89 tc->test_msg);
90 }
91
92 if (!TST_PASS)
93 return;
94
95 tst_check_resuid(tc->test_msg, *tc->exp_real_uid, *tc->exp_eff_uid,
96 *tc->exp_sav_uid);
97 }
98
99 static struct tst_test test = {
100 .test = run,
101 .tcnt = ARRAY_SIZE(test_data),
102 .setup = setup,
103 .needs_root = 1,
104 };
105