1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *
4  * This program is free software;  you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12  * the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program;  if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * Test Name: lchown02
21  *
22  * Test Description:
23  *   Verify that,
24  *   1) lchown(2) returns -1 and sets errno to EPERM if the effective user id
25  *	of process does not match the owner of the file and the process is
26  *	not super user.
27  *   2) lchown(2) returns -1 and sets errno to EACCES if search permission is
28  *	denied on a component of the path prefix.
29  *   3) lchown(2) returns -1 and sets errno to EFAULT if pathname points
30  *	outside user's accessible address space.
31  *   4) lchown(2) returns -1 and sets errno to ENAMETOOLONG if the pathname
32  *	component is too long.
33  *   5) lchown(2) returns -1 and sets errno to ENOTDIR if the directory
34  *	component in pathname is not a directory.
35  *   6) lchown(2) returns -1 and sets errno to ENOENT if the specified file
36  *	does not exists.
37  *
38  * Expected Result:
39  *  lchown() should fail with return value -1 and set expected errno.
40  *
41  * HISTORY
42  *	07/2001 Ported by Wayne Boyer
43  *      11/2010 Rewritten by Cyril Hrubis chrubis@suse.cz
44  */
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <errno.h>
51 #include <string.h>
52 #include <signal.h>
53 #include <grp.h>
54 #include <pwd.h>
55 #include <sys/types.h>
56 #include <sys/stat.h>
57 #include <sys/mman.h>
58 
59 #include "test.h"
60 #include "compat_16.h"
61 
62 #define TEST_USER       "nobody"
63 #define MODE_RWX	S_IRWXU | S_IRWXG | S_IRWXO
64 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
65 #define DIR_TEMP	"testdir_1"
66 #define TEST_FILE1	"tfile_1"
67 #define SFILE1		"sfile_1"
68 #define TEST_FILE2	"testdir_1/tfile_2"
69 #define SFILE2		"testdir_1/sfile_2"
70 #define TFILE3          "t_file"
71 #define SFILE3		"t_file/sfile"
72 
73 TCID_DEFINE(lchown02);
74 int TST_TOTAL = 7;
75 
76 static void setup_eperm(int pos);
77 static void setup_eacces(int pos);
78 static void setup_enotdir(int pos);
79 static void setup_longpath(int pos);
80 static void setup_efault(int pos);
81 static void setup_highaddress(int pos);
82 
83 static char path[PATH_MAX + 2];
84 
85 struct test_case_t {
86 	char *pathname;
87 	char *desc;
88 	int exp_errno;
89 	void (*setup) (int pos);
90 };
91 
92 static struct test_case_t test_cases[] = {
93 	{SFILE1, "Process is not owner/root", EPERM, setup_eperm},
94 	{SFILE2, "Search permission denied", EACCES, setup_eacces},
95 	{NULL, "Address beyond address space", EFAULT, setup_highaddress},
96 	{NULL, "Unaccessible address space", EFAULT, setup_efault},
97 	{path, "Pathname too long", ENAMETOOLONG, setup_longpath},
98 	{SFILE3, "Path contains regular file", ENOTDIR, setup_enotdir},
99 	{"", "Pathname is empty", ENOENT, NULL},
100 	{NULL, NULL, 0, NULL}
101 };
102 
103 static struct passwd *ltpuser;
104 
105 static void setup(void);
106 static void cleanup(void);
107 
main(int argc,char * argv[])108 int main(int argc, char *argv[])
109 {
110 	int lc;
111 	uid_t user_id;
112 	gid_t group_id;
113 	int i;
114 
115 	tst_parse_opts(argc, argv, NULL, NULL);
116 
117 	setup();
118 
119 	user_id = geteuid();
120 	UID16_CHECK(user_id, lchown, cleanup);
121 	group_id = getegid();
122 	GID16_CHECK(group_id, lchown, cleanup);
123 
124 	for (lc = 0; TEST_LOOPING(lc); lc++) {
125 		tst_count = 0;
126 
127 		for (i = 0; test_cases[i].desc != NULL; i++) {
128 			char *file_name = test_cases[i].pathname;
129 			char *test_desc = test_cases[i].desc;
130 
131 			/*
132 			 * Call lchown(2) to test different test conditions.
133 			 * verify that it fails with -1 return value and
134 			 * sets appropriate errno.
135 			 */
136 			TEST(LCHOWN(cleanup, file_name, user_id, group_id));
137 
138 			/* Check return code from lchown(2) */
139 			if (TEST_RETURN == -1) {
140 				if (TEST_ERRNO == test_cases[i].exp_errno) {
141 					tst_resm(TPASS,
142 						 "lchown(2) fails, %s, errno:%d",
143 						 test_desc, TEST_ERRNO);
144 				} else {
145 					tst_resm(TFAIL, "lchown(2) fails, %s, "
146 						 "errno:%d, expected errno:%d",
147 						 test_desc, TEST_ERRNO,
148 						 test_cases[i].exp_errno);
149 				}
150 			} else {
151 				tst_resm(TFAIL, "lchown(2) returned %ld, "
152 					 "expected -1, errno:%d", TEST_RETURN,
153 					 test_cases[i].exp_errno);
154 			}
155 		}
156 	}
157 
158 	cleanup();
159 	tst_exit();
160 }
161 
setup(void)162 static void setup(void)
163 {
164 	int i;
165 
166 	tst_sig(FORK, DEF_HANDLER, cleanup);
167 
168 	tst_require_root();
169 
170 	TEST_PAUSE;
171 
172 	/* change euid and gid to nobody */
173 	ltpuser = getpwnam(TEST_USER);
174 
175 	if (ltpuser == NULL)
176 		tst_brkm(TBROK, cleanup, "getpwnam failed");
177 
178 	if (setgid(ltpuser->pw_uid) == -1)
179 		tst_resm(TBROK | TERRNO, "setgid failed");
180 
181 	tst_tmpdir();
182 
183 	for (i = 0; test_cases[i].desc != NULL; i++)
184 		if (test_cases[i].setup != NULL)
185 			test_cases[i].setup(i);
186 }
187 
188 /*
189  * setup_eperm() - setup function for a test condition for which lchown(2)
190  *	           returns -1 and sets errno to EPERM.
191  *
192  * Create test file and symlink with uid 0.
193  */
setup_eperm(int pos LTP_ATTRIBUTE_UNUSED)194 static void setup_eperm(int pos LTP_ATTRIBUTE_UNUSED)
195 {
196 	int fd;
197 
198 	/* create a testfile */
199 	if ((fd = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1)
200 		tst_brkm(TBROK | TERRNO, cleanup, "open failed");
201 
202 	if (close(fd) == -1)
203 		tst_brkm(TBROK | TERRNO, cleanup, "close failed");
204 
205 	/* become root once more */
206 	if (seteuid(0) == -1)
207 		tst_resm(TBROK | TERRNO, "setuid(0) failed");
208 
209 	/* create symling to testfile */
210 	if (symlink(TEST_FILE1, SFILE1) < 0)
211 		tst_brkm(TBROK | TERRNO, cleanup, "symlink failed");
212 
213 	/* back to the user nobody */
214 	if (seteuid(ltpuser->pw_uid) == -1)
215 		tst_resm(TBROK | TERRNO, "seteuid(%d) failed", ltpuser->pw_uid);
216 }
217 
218 /*
219  * setup_eaccess() - setup function for a test condition for which lchown(2)
220  *	             returns -1 and sets errno to EACCES.
221  *
222  *  Create a test directory under temporary directory and create a test file
223  *  under this directory with mode "0666" permissions.
224  *  Modify the mode permissions on test directory such that process will not
225  *  have search permissions on test directory.
226  */
setup_eacces(int pos LTP_ATTRIBUTE_UNUSED)227 static void setup_eacces(int pos LTP_ATTRIBUTE_UNUSED)
228 {
229 	int fd;
230 
231 	/* create a test directory */
232 	if (mkdir(DIR_TEMP, MODE_RWX) == -1)
233 		tst_brkm(TBROK | TERRNO, cleanup, "mkdir failed");
234 
235 	/* create a file under test directory */
236 	if ((fd = open(TEST_FILE2, O_RDWR | O_CREAT, 0666)) == -1)
237 		tst_brkm(TBROK | TERRNO, cleanup, "open failed");
238 
239 	if (close(fd) == -1)
240 		tst_brkm(TBROK | TERRNO, cleanup, "close failed");
241 
242 	/* create a symlink of testfile */
243 	if (symlink(TEST_FILE2, SFILE2) < 0) {
244 		tst_brkm(TBROK | TERRNO, cleanup, "symlink(2) %s to %s failed",
245 			 TEST_FILE2, SFILE2);
246 	}
247 
248 	/* modify mode permissions on test directory */
249 	if (chmod(DIR_TEMP, FILE_MODE) < 0) {
250 		tst_brkm(TBROK | TERRNO, cleanup, "chmod(2) %s failed",
251 			 DIR_TEMP);
252 	}
253 }
254 
255 /*
256  * setup_efault() -- setup for a test condition where lchown(2) returns -1 and
257  *                   sets errno to EFAULT.
258  *
259  * Create "bad address" by explicitly mmaping anonymous page that may not be
260  * accesed (see PROT_NONE).
261  */
setup_efault(int pos)262 static void setup_efault(int pos)
263 {
264 	char *bad_addr = 0;
265 
266 	bad_addr = mmap(NULL, 1, PROT_NONE,
267 			MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, -1, 0);
268 
269 	if (bad_addr == MAP_FAILED)
270 		tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
271 
272 	test_cases[pos].pathname = bad_addr;
273 }
274 
275 /*
276  * setup_efault() -- setup for a test condition where lchown(2) returns -1 and
277  *                   sets errno to EFAULT.
278  *
279  * Use ltp function get_high_address() to compute high address.
280  */
setup_highaddress(int pos)281 static void setup_highaddress(int pos)
282 {
283 	test_cases[pos].pathname = get_high_address();
284 }
285 
286 /*
287  * setup_enotdir() - setup function for a test condition for which chown(2)
288  *	             returns -1 and sets errno to ENOTDIR.
289  *
290  * Create a regular file "t_file" to call lchown(2) on "t_file/sfile" later.
291  */
setup_enotdir(int pos LTP_ATTRIBUTE_UNUSED)292 static void setup_enotdir(int pos LTP_ATTRIBUTE_UNUSED)
293 {
294 	int fd;
295 
296 	/* create a testfile under temporary directory */
297 	if ((fd = open(TFILE3, O_RDWR | O_CREAT, MODE_RWX)) == -1) {
298 		tst_brkm(TBROK | TERRNO, cleanup, "open(2) %s failed", TFILE3);
299 	}
300 
301 	if (close(fd) == -1) {
302 		tst_brkm(TBROK | TERRNO, cleanup, "close(2) %s failed", TFILE3);
303 	}
304 }
305 
306 /*
307  * longpath_setup() - setup to create a node with a name length exceeding
308  *                    the length of PATH_MAX.
309  */
setup_longpath(int pos)310 static void setup_longpath(int pos)
311 {
312 	memset(test_cases[pos].pathname, 'a', PATH_MAX + 1);
313 	test_cases[pos].pathname[PATH_MAX + 1] = '\0';
314 }
315 
cleanup(void)316 static void cleanup(void)
317 {
318 	if (seteuid(0) == -1) {
319 		tst_resm(TINFO | TERRNO,
320 			 "seteuid(2) failed to set the effective uid to 0");
321 	}
322 
323 	tst_rmdir();
324 }
325