• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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() fails with EPERM if unprivileged user tries to set
12  * process group ID which requires higher permissions.
13  */
14 
15 #include <sys/types.h>
16 #include <pwd.h>
17 
18 #include "tst_test.h"
19 #include "tst_uid.h"
20 #include "compat_tst_16.h"
21 
22 struct test_case_t {
23 	gid_t *rgid;
24 	gid_t *egid;
25 	gid_t *sgid;
26 	gid_t *exp_rgid;
27 	gid_t *exp_egid;
28 	gid_t *exp_sgid;
29 	char *desc;
30 };
31 
32 static gid_t nobody_gid, other_gid, neg = -1;
33 
34 static struct test_case_t test_cases[] = {
35 	{&neg, &neg, &other_gid, &nobody_gid, &nobody_gid, &nobody_gid,
36 		"setresgid(-1, -1, other)"},
37 	{&neg, &other_gid, &neg, &nobody_gid, &nobody_gid, &nobody_gid,
38 		"setresgid(-1, other, -1)"},
39 	{&other_gid, &neg, &neg, &nobody_gid, &nobody_gid, &nobody_gid,
40 		"setresgid(other, -1, -1)"},
41 	{&other_gid, &other_gid, &other_gid, &nobody_gid, &nobody_gid,
42 		&nobody_gid, "setresgid(other, other, other)"},
43 };
44 
setup(void)45 static void setup(void)
46 {
47 	gid_t test_groups[2];
48 	struct passwd *pw = SAFE_GETPWNAM("nobody");
49 
50 	nobody_gid = test_groups[0] = pw->pw_gid;
51 	tst_get_gids(test_groups, 1, 2);
52 	other_gid = test_groups[1];
53 
54 	GID16_CHECK(nobody_gid, setresgid);
55 	GID16_CHECK(other_gid, setresgid);
56 
57 	/* Set real/effective/saved gid to nobody */
58 	SAFE_SETRESGID(nobody_gid, nobody_gid, nobody_gid);
59 	SAFE_SETUID(pw->pw_uid);
60 }
61 
run(unsigned int n)62 static void run(unsigned int n)
63 {
64 	const struct test_case_t *tc = test_cases + n;
65 
66 	TST_EXP_FAIL(SETRESGID(*tc->rgid, *tc->egid, *tc->sgid), EPERM, "%s",
67 		tc->desc);
68 
69 	if (!TST_PASS)
70 		return;
71 
72 	tst_check_resgid(tc->desc, *tc->exp_rgid, *tc->exp_egid,
73 		*tc->exp_sgid);
74 }
75 
76 static struct tst_test test = {
77 	.test = run,
78 	.tcnt = ARRAY_SIZE(test_cases),
79 	.setup = setup,
80 	.needs_root = 1,
81 };
82