xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setuid/setuid03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  * Copyright (c) Linux Test Project, 2009-2022
5  */
6 
7 /*\
8  * [Description]
9  *
10  * This test will switch to nobody user for correct error code collection.
11  * Verify setuid returns errno EPERM when it switches to root_user.
12  */
13 
14 #include <errno.h>
15 #include <pwd.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include "tst_test.h"
19 #include "compat_tst_16.h"
20 
21 #define ROOT_USER	0
22 
verify_setuid(void)23 static void verify_setuid(void)
24 {
25 	TST_EXP_FAIL(SETUID(ROOT_USER), EPERM);
26 }
27 
setup(void)28 static void setup(void)
29 {
30 	struct passwd *pw;
31 	uid_t uid;
32 
33 	pw = SAFE_GETPWNAM("nobody");
34 	uid = pw->pw_uid;
35 
36 	SAFE_SETUID(uid);
37 
38 	umask(0);
39 }
40 
41 static struct tst_test test = {
42 	.setup = setup,
43 	.needs_root = 1,
44 	.test_all =  verify_setuid,
45 };
46