1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Dan Kegel 2003
4f08c3bdfSopenharmony_ci * Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Verify that setegid() sets the effective UID of the calling process
11f08c3bdfSopenharmony_ci * correctly, and does not modify the saved GID and real GID.
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <pwd.h>
15f08c3bdfSopenharmony_ci#include "tst_test.h"
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_cistatic gid_t nobody_gid;
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic void setup(void)
20f08c3bdfSopenharmony_ci{
21f08c3bdfSopenharmony_ci	struct passwd *nobody;
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci	nobody = SAFE_GETPWNAM("nobody");
24f08c3bdfSopenharmony_ci	nobody_gid = nobody->pw_gid;
25f08c3bdfSopenharmony_ci}
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic void setegid_verify(void)
28f08c3bdfSopenharmony_ci{
29f08c3bdfSopenharmony_ci	gid_t cur_rgid, cur_egid, cur_sgid;
30f08c3bdfSopenharmony_ci	gid_t orig_rgid, orig_egid, orig_sgid;
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	SAFE_GETRESGID(&orig_rgid, &orig_egid, &orig_sgid);
33f08c3bdfSopenharmony_ci	tst_res(TINFO, "getresgid() reports rgid: %d, egid: %d, sgid: %d",
34f08c3bdfSopenharmony_ci			orig_rgid, orig_egid, orig_sgid);
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	tst_res(TINFO, "call setegid(nobody_gid %d)", nobody_gid);
37f08c3bdfSopenharmony_ci	SAFE_SETEGID(nobody_gid);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	SAFE_GETRESGID(&cur_rgid, &cur_egid, &cur_sgid);
40f08c3bdfSopenharmony_ci	tst_res(TINFO, "getresgid() reports rgid: %d, egid: %d, sgid: %d",
41f08c3bdfSopenharmony_ci			cur_rgid, cur_egid, cur_sgid);
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	TST_EXP_EQ_LU(nobody_gid, cur_egid);
44f08c3bdfSopenharmony_ci	TST_EXP_EQ_LU(orig_rgid, cur_rgid);
45f08c3bdfSopenharmony_ci	TST_EXP_EQ_LU(orig_sgid, cur_sgid);
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	SAFE_SETEGID(orig_egid);
48f08c3bdfSopenharmony_ci	SAFE_GETRESGID(&cur_rgid, &cur_egid, &orig_sgid);
49f08c3bdfSopenharmony_ci	TST_EXP_EQ_LU(orig_egid, cur_egid);
50f08c3bdfSopenharmony_ci}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic struct tst_test test = {
53f08c3bdfSopenharmony_ci	.setup = setup,
54f08c3bdfSopenharmony_ci	.test_all = setegid_verify,
55f08c3bdfSopenharmony_ci	.needs_root = 1
56f08c3bdfSopenharmony_ci};
57