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