xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setfsuid/setfsuid04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*****************************************************************************
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Kerlabs 2008.                                               *
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2008                *
4*49cdfc7eSAndroid Build Coastguard Worker  * Created by Renaud Lottiaux                                                *
5*49cdfc7eSAndroid Build Coastguard Worker  *                                                                           *
6*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software;  you can redistribute it and/or modify     *
7*49cdfc7eSAndroid Build Coastguard Worker  * it under the terms of the GNU General Public License as published by      *
8*49cdfc7eSAndroid Build Coastguard Worker  * the Free Software Foundation; either version 2 of the License, or         *
9*49cdfc7eSAndroid Build Coastguard Worker  * (at your option) any later version.                                       *
10*49cdfc7eSAndroid Build Coastguard Worker  *                                                                           *
11*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it will be useful,           *
12*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY;  without even the implied warranty of           *
13*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                 *
14*49cdfc7eSAndroid Build Coastguard Worker  * the GNU General Public License for more details.                          *
15*49cdfc7eSAndroid Build Coastguard Worker  *                                                                           *
16*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License         *
17*49cdfc7eSAndroid Build Coastguard Worker  * along with this program;  if not, write to the Free Software Foundation,  *
18*49cdfc7eSAndroid Build Coastguard Worker  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA          *
19*49cdfc7eSAndroid Build Coastguard Worker  *****************************************************************************/
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker /*
22*49cdfc7eSAndroid Build Coastguard Worker  * Check if setfsuid behaves correctly with file permissions.
23*49cdfc7eSAndroid Build Coastguard Worker  * The test creates a file as ROOT with permissions 0644, does a setfsuid
24*49cdfc7eSAndroid Build Coastguard Worker  * and then tries to open the file with RDWR permissions.
25*49cdfc7eSAndroid Build Coastguard Worker  * The same test is done in a fork to check if new UIDs are correctly
26*49cdfc7eSAndroid Build Coastguard Worker  * passed to the son.
27*49cdfc7eSAndroid Build Coastguard Worker  */
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
41*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
42*49cdfc7eSAndroid Build Coastguard Worker #include "compat_16.h"
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker TCID_DEFINE(setfsuid04);
45*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker static char nobody_uid[] = "nobody";
48*49cdfc7eSAndroid Build Coastguard Worker static char testfile[] = "setfsuid04_testfile";
49*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *ltpuser;
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker static int fd = -1;
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
54*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
55*49cdfc7eSAndroid Build Coastguard Worker static void do_master_child(void);
56*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)57*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	setup();
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	pid = tst_fork();
66*49cdfc7eSAndroid Build Coastguard Worker 	if (pid < 0)
67*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup, "Fork failed");
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	if (pid == 0)
70*49cdfc7eSAndroid Build Coastguard Worker 		do_master_child();
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	tst_record_childstatus(cleanup, pid);
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
75*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker 
do_master_child(void)78*49cdfc7eSAndroid Build Coastguard Worker static void do_master_child(void)
79*49cdfc7eSAndroid Build Coastguard Worker {
80*49cdfc7eSAndroid Build Coastguard Worker 	int pid;
81*49cdfc7eSAndroid Build Coastguard Worker 	int status;
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	if (SETFSUID(NULL, ltpuser->pw_uid) == -1) {
84*49cdfc7eSAndroid Build Coastguard Worker 		perror("setfsuid failed");
85*49cdfc7eSAndroid Build Coastguard Worker 		exit(TFAIL);
86*49cdfc7eSAndroid Build Coastguard Worker 	}
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	/* Test 1: Check the process with new uid cannot open the file
89*49cdfc7eSAndroid Build Coastguard Worker 	 *         with RDWR permissions.
90*49cdfc7eSAndroid Build Coastguard Worker 	 */
91*49cdfc7eSAndroid Build Coastguard Worker 	TEST(open(testfile, O_RDWR));
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN != -1) {
94*49cdfc7eSAndroid Build Coastguard Worker 		close(TEST_RETURN);
95*49cdfc7eSAndroid Build Coastguard Worker 		printf("open succeeded unexpectedly\n");
96*49cdfc7eSAndroid Build Coastguard Worker 		exit(TFAIL);
97*49cdfc7eSAndroid Build Coastguard Worker 	}
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_ERRNO == EACCES) {
100*49cdfc7eSAndroid Build Coastguard Worker 		printf("open failed with EACCESS as expected\n");
101*49cdfc7eSAndroid Build Coastguard Worker 	} else {
102*49cdfc7eSAndroid Build Coastguard Worker 		printf("open returned unexpected errno - %d\n", TEST_ERRNO);
103*49cdfc7eSAndroid Build Coastguard Worker 		exit(TFAIL);
104*49cdfc7eSAndroid Build Coastguard Worker 	}
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	/* Test 2: Check a son process cannot open the file
107*49cdfc7eSAndroid Build Coastguard Worker 	 *         with RDWR permissions.
108*49cdfc7eSAndroid Build Coastguard Worker 	 */
109*49cdfc7eSAndroid Build Coastguard Worker 	pid = tst_fork();
110*49cdfc7eSAndroid Build Coastguard Worker 	if (pid < 0) {
111*49cdfc7eSAndroid Build Coastguard Worker 		perror("Fork failed");
112*49cdfc7eSAndroid Build Coastguard Worker 		exit(TFAIL);
113*49cdfc7eSAndroid Build Coastguard Worker 	}
114*49cdfc7eSAndroid Build Coastguard Worker 
115*49cdfc7eSAndroid Build Coastguard Worker 	if (pid == 0) {
116*49cdfc7eSAndroid Build Coastguard Worker 		/* Test to open the file in son process */
117*49cdfc7eSAndroid Build Coastguard Worker 		TEST(open(testfile, O_RDWR));
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN != -1) {
120*49cdfc7eSAndroid Build Coastguard Worker 			close(TEST_RETURN);
121*49cdfc7eSAndroid Build Coastguard Worker 			printf("open succeeded unexpectedly\n");
122*49cdfc7eSAndroid Build Coastguard Worker 			exit(TFAIL);
123*49cdfc7eSAndroid Build Coastguard Worker 		}
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_ERRNO == EACCES) {
126*49cdfc7eSAndroid Build Coastguard Worker 			printf("open failed with EACCESS as expected\n");
127*49cdfc7eSAndroid Build Coastguard Worker 		} else {
128*49cdfc7eSAndroid Build Coastguard Worker 			printf("open returned unexpected errno - %d\n",
129*49cdfc7eSAndroid Build Coastguard Worker 			       TEST_ERRNO);
130*49cdfc7eSAndroid Build Coastguard Worker 			exit(TFAIL);
131*49cdfc7eSAndroid Build Coastguard Worker 		}
132*49cdfc7eSAndroid Build Coastguard Worker 	} else {
133*49cdfc7eSAndroid Build Coastguard Worker 		/* Wait for son completion */
134*49cdfc7eSAndroid Build Coastguard Worker 		if (waitpid(pid, &status, 0) == -1) {
135*49cdfc7eSAndroid Build Coastguard Worker 			perror("waitpid failed");
136*49cdfc7eSAndroid Build Coastguard Worker 			exit(TFAIL);
137*49cdfc7eSAndroid Build Coastguard Worker 		}
138*49cdfc7eSAndroid Build Coastguard Worker 
139*49cdfc7eSAndroid Build Coastguard Worker 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
140*49cdfc7eSAndroid Build Coastguard Worker 			exit(WEXITSTATUS(status));
141*49cdfc7eSAndroid Build Coastguard Worker 	}
142*49cdfc7eSAndroid Build Coastguard Worker 
143*49cdfc7eSAndroid Build Coastguard Worker 	/* Test 3: Fallback to initial uid and check we can again open
144*49cdfc7eSAndroid Build Coastguard Worker 	 *         the file with RDWR permissions.
145*49cdfc7eSAndroid Build Coastguard Worker 	 */
146*49cdfc7eSAndroid Build Coastguard Worker 	tst_count++;
147*49cdfc7eSAndroid Build Coastguard Worker 	if (SETFSUID(NULL, 0) == -1) {
148*49cdfc7eSAndroid Build Coastguard Worker 		perror("setfsuid failed");
149*49cdfc7eSAndroid Build Coastguard Worker 		exit(TFAIL);
150*49cdfc7eSAndroid Build Coastguard Worker 	}
151*49cdfc7eSAndroid Build Coastguard Worker 
152*49cdfc7eSAndroid Build Coastguard Worker 	TEST(open(testfile, O_RDWR));
153*49cdfc7eSAndroid Build Coastguard Worker 
154*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN == -1) {
155*49cdfc7eSAndroid Build Coastguard Worker 		perror("open failed unexpectedly");
156*49cdfc7eSAndroid Build Coastguard Worker 		exit(TFAIL);
157*49cdfc7eSAndroid Build Coastguard Worker 	} else {
158*49cdfc7eSAndroid Build Coastguard Worker 		printf("open call succeeded\n");
159*49cdfc7eSAndroid Build Coastguard Worker 		close(TEST_RETURN);
160*49cdfc7eSAndroid Build Coastguard Worker 	}
161*49cdfc7eSAndroid Build Coastguard Worker 	exit(TPASS);
162*49cdfc7eSAndroid Build Coastguard Worker }
163*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)164*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
165*49cdfc7eSAndroid Build Coastguard Worker {
166*49cdfc7eSAndroid Build Coastguard Worker 	tst_require_root();
167*49cdfc7eSAndroid Build Coastguard Worker 
168*49cdfc7eSAndroid Build Coastguard Worker 	ltpuser = getpwnam(nobody_uid);
169*49cdfc7eSAndroid Build Coastguard Worker 	if (ltpuser == NULL)
170*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup, "getpwnam failed for user id %s",
171*49cdfc7eSAndroid Build Coastguard Worker 			nobody_uid);
172*49cdfc7eSAndroid Build Coastguard Worker 
173*49cdfc7eSAndroid Build Coastguard Worker 	UID16_CHECK(ltpuser->pw_uid, setfsuid, cleanup);
174*49cdfc7eSAndroid Build Coastguard Worker 
175*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
176*49cdfc7eSAndroid Build Coastguard Worker 
177*49cdfc7eSAndroid Build Coastguard Worker 	/* Create test file */
178*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(cleanup, testfile, O_CREAT | O_RDWR, 0644);
179*49cdfc7eSAndroid Build Coastguard Worker 
180*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
181*49cdfc7eSAndroid Build Coastguard Worker 
182*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
183*49cdfc7eSAndroid Build Coastguard Worker }
184*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)185*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
186*49cdfc7eSAndroid Build Coastguard Worker {
187*49cdfc7eSAndroid Build Coastguard Worker 	close(fd);
188*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
189*49cdfc7eSAndroid Build Coastguard Worker }
190