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