xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setregid/setregid03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * Ported by John George
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*
9*49cdfc7eSAndroid Build Coastguard Worker  * Test setregid() when executed by a non-root user.
10*49cdfc7eSAndroid Build Coastguard Worker  */
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_uid.h"
16*49cdfc7eSAndroid Build Coastguard Worker #include "compat_tst_16.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static int fail = -1;
19*49cdfc7eSAndroid Build Coastguard Worker static int pass;
20*49cdfc7eSAndroid Build Coastguard Worker static gid_t primary_gid, secondary_gid, neg_one = -1;
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker struct tcase {
23*49cdfc7eSAndroid Build Coastguard Worker 	gid_t *real_gid;
24*49cdfc7eSAndroid Build Coastguard Worker 	gid_t *eff_gid;
25*49cdfc7eSAndroid Build Coastguard Worker 	int *exp_ret;
26*49cdfc7eSAndroid Build Coastguard Worker 	gid_t *exp_real_usr;
27*49cdfc7eSAndroid Build Coastguard Worker 	gid_t *exp_eff_usr;
28*49cdfc7eSAndroid Build Coastguard Worker 	char *test_msg;
29*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
30*49cdfc7eSAndroid Build Coastguard Worker 	{
31*49cdfc7eSAndroid Build Coastguard Worker 	&primary_gid, &secondary_gid, &pass, &primary_gid, &secondary_gid,
32*49cdfc7eSAndroid Build Coastguard Worker 		    "After setregid(primary, secondary),"}, {
33*49cdfc7eSAndroid Build Coastguard Worker 	&neg_one, &primary_gid, &pass, &primary_gid, &primary_gid,
34*49cdfc7eSAndroid Build Coastguard Worker 		    "After setregid(-1, primary)"}, {
35*49cdfc7eSAndroid Build Coastguard Worker 	&neg_one, &secondary_gid, &pass, &primary_gid, &secondary_gid,
36*49cdfc7eSAndroid Build Coastguard Worker 		    "After setregid(-1, secondary),"}, {
37*49cdfc7eSAndroid Build Coastguard Worker 	&secondary_gid, &neg_one, &pass, &secondary_gid, &secondary_gid,
38*49cdfc7eSAndroid Build Coastguard Worker 		    "After setregid(secondary, -1),"}, {
39*49cdfc7eSAndroid Build Coastguard Worker 	&neg_one, &neg_one, &pass, &secondary_gid, &secondary_gid,
40*49cdfc7eSAndroid Build Coastguard Worker 		    "After setregid(-1, -1),"}, {
41*49cdfc7eSAndroid Build Coastguard Worker 	&neg_one, &secondary_gid, &pass, &secondary_gid, &secondary_gid,
42*49cdfc7eSAndroid Build Coastguard Worker 		    "After setregid(-1, secondary),"}, {
43*49cdfc7eSAndroid Build Coastguard Worker 	&secondary_gid, &neg_one, &pass, &secondary_gid, &secondary_gid,
44*49cdfc7eSAndroid Build Coastguard Worker 		    "After setregid(secondary, -1),"}, {
45*49cdfc7eSAndroid Build Coastguard Worker 	&secondary_gid, &secondary_gid, &pass, &secondary_gid, &secondary_gid,
46*49cdfc7eSAndroid Build Coastguard Worker 		    "After setregid(secondary, secondary),"}, {
47*49cdfc7eSAndroid Build Coastguard Worker 	&primary_gid, &neg_one, &fail, &secondary_gid, &secondary_gid,
48*49cdfc7eSAndroid Build Coastguard Worker 		    "After setregid(primary, -1)"}, {
49*49cdfc7eSAndroid Build Coastguard Worker 	&neg_one, &primary_gid, &fail, &secondary_gid, &secondary_gid,
50*49cdfc7eSAndroid Build Coastguard Worker 		    "After setregid(-1, primary)"}, {
51*49cdfc7eSAndroid Build Coastguard Worker 	&primary_gid, &primary_gid, &fail, &secondary_gid, &secondary_gid,
52*49cdfc7eSAndroid Build Coastguard Worker 		    "After setregid(primary, primary)"},};
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 	struct passwd *nobody;
57*49cdfc7eSAndroid Build Coastguard Worker 	gid_t test_groups[2];
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	nobody = SAFE_GETPWNAM("nobody");
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	tst_get_gids(test_groups, 0, 2);
62*49cdfc7eSAndroid Build Coastguard Worker 	primary_gid = test_groups[0];
63*49cdfc7eSAndroid Build Coastguard Worker 	secondary_gid = test_groups[1];
64*49cdfc7eSAndroid Build Coastguard Worker 	GID16_CHECK(primary_gid, setregid);
65*49cdfc7eSAndroid Build Coastguard Worker 	GID16_CHECK(secondary_gid, setregid);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	/* set the appropriate ownership values */
68*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETREGID(primary_gid, secondary_gid);
69*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(nobody->pw_uid);
70*49cdfc7eSAndroid Build Coastguard Worker }
71*49cdfc7eSAndroid Build Coastguard Worker 
test_success(struct tcase * tc)72*49cdfc7eSAndroid Build Coastguard Worker static void test_success(struct tcase *tc)
73*49cdfc7eSAndroid Build Coastguard Worker {
74*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 0)
75*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "setregid(%d, %d) failed unexpectedly",
76*49cdfc7eSAndroid Build Coastguard Worker 			*tc->real_gid, *tc->eff_gid);
77*49cdfc7eSAndroid Build Coastguard Worker 	else
78*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "setregid(%d, %d) succeeded as expected",
79*49cdfc7eSAndroid Build Coastguard Worker 			*tc->real_gid, *tc->eff_gid);
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker 
test_failure(struct tcase * tc)82*49cdfc7eSAndroid Build Coastguard Worker static void test_failure(struct tcase *tc)
83*49cdfc7eSAndroid Build Coastguard Worker {
84*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == 0)
85*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "setregid(%d, %d) succeeded unexpectedly",
86*49cdfc7eSAndroid Build Coastguard Worker 			*tc->real_gid, *tc->eff_gid);
87*49cdfc7eSAndroid Build Coastguard Worker 	else if (TST_ERR == EPERM)
88*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "setregid(%d, %d) failed as expected",
89*49cdfc7eSAndroid Build Coastguard Worker 			*tc->real_gid, *tc->eff_gid);
90*49cdfc7eSAndroid Build Coastguard Worker 	else
91*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
92*49cdfc7eSAndroid Build Coastguard Worker 			"setregid(%d, %d) did not set errno value as expected",
93*49cdfc7eSAndroid Build Coastguard Worker 			*tc->real_gid, *tc->eff_gid);
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker 
gid_verify(gid_t rg,gid_t eg,char * when)96*49cdfc7eSAndroid Build Coastguard Worker static void gid_verify(gid_t rg, gid_t eg, char *when)
97*49cdfc7eSAndroid Build Coastguard Worker {
98*49cdfc7eSAndroid Build Coastguard Worker 	if ((getgid() != rg) || (getegid() != eg)) {
99*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
100*49cdfc7eSAndroid Build Coastguard Worker 			 when, getgid(), getegid());
101*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Expected: real gid = %d; effective gid = %d",
102*49cdfc7eSAndroid Build Coastguard Worker 			 rg, eg);
103*49cdfc7eSAndroid Build Coastguard Worker 	} else {
104*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS,
105*49cdfc7eSAndroid Build Coastguard Worker 			"real or effective gid was modified as expected");
106*49cdfc7eSAndroid Build Coastguard Worker 	}
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int i)109*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
110*49cdfc7eSAndroid Build Coastguard Worker {
111*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[i];
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	/* Set the real or effective group id */
114*49cdfc7eSAndroid Build Coastguard Worker 	TEST(SETREGID(*tc->real_gid, *tc->eff_gid));
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	if (*tc->exp_ret == 0)
117*49cdfc7eSAndroid Build Coastguard Worker 		test_success(tc);
118*49cdfc7eSAndroid Build Coastguard Worker 	else
119*49cdfc7eSAndroid Build Coastguard Worker 		test_failure(tc);
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 	gid_verify(*tc->exp_real_usr, *tc->exp_eff_usr, tc->test_msg);
122*49cdfc7eSAndroid Build Coastguard Worker }
123*49cdfc7eSAndroid Build Coastguard Worker 
run_all(void)124*49cdfc7eSAndroid Build Coastguard Worker void run_all(void)
125*49cdfc7eSAndroid Build Coastguard Worker {
126*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker 	if (!SAFE_FORK()) {
129*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < ARRAY_SIZE(tcases); i++)
130*49cdfc7eSAndroid Build Coastguard Worker 			run(i);
131*49cdfc7eSAndroid Build Coastguard Worker 	}
132*49cdfc7eSAndroid Build Coastguard Worker }
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
135*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
136*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
137*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run_all,
138*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
139*49cdfc7eSAndroid Build Coastguard Worker };
140