1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * 07/2001 ported by John George
5 * Copyright (C) 2021 SUSE LLC <mdoucha@suse.cz>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Test setresuid() when executed by root.
12 */
13
14 #include "tst_test.h"
15 #include "tst_uid.h"
16 #include "compat_tst_16.h"
17
18 static uid_t root_uid, main_uid, other_uid, neg_one = -1;
19
20 static struct test_data_t {
21 uid_t *real_uid;
22 uid_t *eff_uid;
23 uid_t *sav_uid;
24 uid_t *exp_real_uid;
25 uid_t *exp_eff_uid;
26 uid_t *exp_sav_uid;
27 char *test_msg;
28 } test_data[] = {
29 {&neg_one, &neg_one, &neg_one, &root_uid, &root_uid, &root_uid,
30 "After setresuid(-1, -1, -1),"},
31 {&neg_one, &neg_one, &main_uid, &root_uid, &root_uid, &main_uid,
32 "After setresuid(-1, -1, main),"},
33 {&neg_one, &other_uid, &neg_one, &root_uid, &other_uid, &main_uid,
34 "After setresuid(-1, other, -1),"},
35 {&neg_one, &neg_one, &root_uid, &root_uid, &other_uid, &root_uid,
36 "After setresuid(-1, -1, root),"},
37 {&neg_one, &neg_one, &other_uid, &root_uid, &other_uid, &other_uid,
38 "After setresuid(-1, -1, other),"},
39 {&neg_one, &root_uid, &neg_one, &root_uid, &root_uid, &other_uid,
40 "After setresuid(-1, root, -1),"},
41 {&main_uid, &neg_one, &neg_one, &main_uid, &root_uid, &other_uid,
42 "After setresuid(main, -1, -1)"},
43 {&neg_one, &root_uid, &neg_one, &main_uid, &root_uid, &other_uid,
44 "After setresuid(-1, root, -1),"},
45 {&root_uid, &neg_one, &root_uid, &root_uid, &root_uid, &root_uid,
46 "After setresuid(root, -1, -1),"},
47 };
48
setup(void)49 static void setup(void)
50 {
51 uid_t test_users[2];
52
53 root_uid = getuid();
54 tst_get_uids(test_users, 0, 2);
55 main_uid = test_users[0];
56 other_uid = test_users[1];
57
58 UID16_CHECK(root_uid, setresuid);
59 UID16_CHECK(main_uid, setresuid);
60 UID16_CHECK(other_uid, setresuid);
61 }
62
run(unsigned int n)63 static void run(unsigned int n)
64 {
65 const struct test_data_t *tc = test_data + n;
66
67 TST_EXP_PASS_SILENT(SETRESUID(*tc->real_uid, *tc->eff_uid,
68 *tc->sav_uid), "%s", tc->test_msg);
69
70 if (!TST_PASS)
71 return;
72
73 if (tst_check_resuid(tc->test_msg, *tc->exp_real_uid,
74 *tc->exp_eff_uid, *tc->exp_sav_uid))
75 tst_res(TPASS, "%s works as expected", tc->test_msg);
76 }
77
78 static struct tst_test test = {
79 .test = run,
80 .tcnt = ARRAY_SIZE(test_data),
81 .setup = setup,
82 .needs_root = 1,
83 };
84