xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setreuid/setreuid07.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Kerlabs 2008.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2008
5*49cdfc7eSAndroid Build Coastguard Worker  * Created by Renaud Lottiaux
6*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Check if setreuid behaves correctly with file permissions.
13*49cdfc7eSAndroid Build Coastguard Worker  * The test creates a file as ROOT with permissions 0644, does a setreuid
14*49cdfc7eSAndroid Build Coastguard Worker  * and then tries to open the file with RDWR permissions.
15*49cdfc7eSAndroid Build Coastguard Worker  * The same test is done in a fork to check if new UIDs are correctly
16*49cdfc7eSAndroid Build Coastguard Worker  * passed to the child process.
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "compat_tst_16.h"
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #define TEMPFILE "testfile"
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *ltpuser;
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 	int fd;
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	ltpuser = SAFE_GETPWNAM("nobody");
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	UID16_CHECK(ltpuser->pw_uid, setreuid);
36*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(TEMPFILE, O_CREAT | O_RDWR, 0644);
37*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker 
run(void)40*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS_SILENT(SETREUID(-1, ltpuser->pw_uid));
45*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL2(open(TEMPFILE, O_RDWR), EACCES);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_FORK();
48*49cdfc7eSAndroid Build Coastguard Worker 	if (pid == 0) {
49*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_FAIL2(open(TEMPFILE, O_RDWR), EACCES);
50*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
51*49cdfc7eSAndroid Build Coastguard Worker 	}
52*49cdfc7eSAndroid Build Coastguard Worker 	tst_reap_children();
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS_SILENT(SETREUID(-1, 0));
55*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FD(open(TEMPFILE, O_RDWR));
56*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(TST_RET);
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
60*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
61*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
62*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
63*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
64*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
65*49cdfc7eSAndroid Build Coastguard Worker };
66