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