xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setgid/setgid02.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  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Test if setgid() system call sets errno to EPERM correctly.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * [Algorithm]
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * Call setgid() to set the gid to that of root. Run this test as
15*49cdfc7eSAndroid Build Coastguard Worker  * nobody, and expect to get EPERM.
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker #include "compat_tst_16.h"
21*49cdfc7eSAndroid Build Coastguard Worker 
run(void)22*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
23*49cdfc7eSAndroid Build Coastguard Worker {
24*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *rootpwent;
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker 	rootpwent = SAFE_GETPWNAM("root");
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker 	GID16_CHECK(rootpwent->pw_gid, setgid);
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(SETGID(rootpwent->pw_gid), EPERM);
31*49cdfc7eSAndroid Build Coastguard Worker }
32*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)33*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
34*49cdfc7eSAndroid Build Coastguard Worker {
35*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *nobody = SAFE_GETPWNAM("nobody");
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETGID(nobody->pw_gid);
38*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETUID(nobody->pw_uid);
39*49cdfc7eSAndroid Build Coastguard Worker }
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
42*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
43*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
44*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
45*49cdfc7eSAndroid Build Coastguard Worker };
46