1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
4 * AUTHOR: Madhu T L <madhu.tarikere@wipro.com>
5 * Copyright (C) 2021 SUSE LLC <mdoucha@suse.cz>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Verify that setresgid() will successfully set the expected GID when called
12 * by root with the following combinations of arguments:
13 *
14 * - setresgid(-1, -1, -1)
15 * - setresgid(-1, -1, other)
16 * - setresgid(-1, other, -1)
17 * - setresgid(other, -1, -1)
18 * - setresgid(root, root, root)
19 * - setresgid(root, main, main)
20 */
21
22 #include "tst_test.h"
23 #include "tst_uid.h"
24 #include "compat_tst_16.h"
25
26 struct test_case_t {
27 gid_t *rgid;
28 gid_t *egid;
29 gid_t *sgid;
30 gid_t *exp_rgid;
31 gid_t *exp_egid;
32 gid_t *exp_sgid;
33 char *desc;
34 };
35
36 static gid_t root_gid, main_gid, other_gid, neg = -1;
37
38 /* Don't change order of these test cases */
39 static struct test_case_t test_cases[] = {
40 {&neg, &neg, &neg, &root_gid, &main_gid, &main_gid,
41 "setresgid(-1, -1, -1)"},
42 {&neg, &neg, &other_gid, &root_gid, &main_gid, &other_gid,
43 "setresgid(-1, -1, other)"},
44 {&neg, &other_gid, &neg, &root_gid, &other_gid, &other_gid,
45 "setresgid(-1, other, -1)"},
46 {&other_gid, &neg, &neg, &other_gid, &other_gid, &other_gid,
47 "setresgid(other, -1, -1)"},
48 {&root_gid, &root_gid, &root_gid, &root_gid, &root_gid, &root_gid,
49 "setresgid(root, root, root)"},
50 {&root_gid, &main_gid, &main_gid, &root_gid, &main_gid, &main_gid,
51 "setresgid(root, main, main)"},
52 };
53
setup(void)54 static void setup(void)
55 {
56 gid_t test_groups[3];
57
58 root_gid = test_groups[0] = getgid();
59 tst_get_gids(test_groups, 1, 3);
60 main_gid = test_groups[1];
61 other_gid = test_groups[2];
62
63 GID16_CHECK(root_gid, setresgid);
64 GID16_CHECK(main_gid, setresgid);
65 GID16_CHECK(other_gid, setresgid);
66
67 SAFE_SETRESGID(-1, main_gid, main_gid);
68 }
69
run(unsigned int n)70 static void run(unsigned int n)
71 {
72 const struct test_case_t *tc = test_cases + n;
73
74 TST_EXP_PASS_SILENT(SETRESGID(*tc->rgid, *tc->egid, *tc->sgid), "%s",
75 tc->desc);
76
77 if (!TST_PASS)
78 return;
79
80 if (tst_check_resgid(tc->desc, *tc->exp_rgid, *tc->exp_egid,
81 *tc->exp_sgid))
82 tst_res(TPASS, "%s works as expected", tc->desc);
83 }
84
85 static struct tst_test test = {
86 .test = run,
87 .tcnt = ARRAY_SIZE(test_cases),
88 .setup = setup,
89 .needs_root = 1,
90 };
91