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