xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setgid/setgid03.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  *  Ported by Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Algorithm]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * As a root sets current group id to nobody and expects success.
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
15*49cdfc7eSAndroid Build Coastguard Worker #include <compat_tst_16.h>
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *nobody;
18*49cdfc7eSAndroid Build Coastguard Worker 
run(void)19*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
20*49cdfc7eSAndroid Build Coastguard Worker {
21*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(SETGID(nobody->pw_gid));
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker 	if (getgid() != nobody->pw_gid)
24*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "setgid failed to set gid to nobody gid");
25*49cdfc7eSAndroid Build Coastguard Worker 	else
26*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "functionality of getgid() is correct");
27*49cdfc7eSAndroid Build Coastguard Worker }
28*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)29*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker 	nobody = SAFE_GETPWNAM("nobody");
32*49cdfc7eSAndroid Build Coastguard Worker 	GID16_CHECK(nobody->pw_gid, setgid);
33*49cdfc7eSAndroid Build Coastguard Worker }
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
36*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
37*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
38*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
39*49cdfc7eSAndroid Build Coastguard Worker };
40