1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *  07/2001 Ported by Wayne Boyer
4  * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
5  *
6  * This program is free software;  you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14  * the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program;  if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * Test Name: chmod06
23  *
24  * Test Description:
25  *   Verify that,
26  *   1) chmod(2) returns -1 and sets errno to EPERM if the effective user id
27  *	of process does not match the owner of the file and the process is
28  *	not super user.
29  *   2) chmod(2) returns -1 and sets errno to EACCES if search permission is
30  *	denied on a component of the path prefix.
31  *   3) chmod(2) returns -1 and sets errno to EFAULT if pathname points
32  *	outside user's accessible address space.
33  *   4) chmod(2) returns -1 and sets errno to ENAMETOOLONG if the pathname
34  *	component is too long.
35  *   5) chmod(2) returns -1 and sets errno to ENOTDIR if the directory
36  *	component in pathname is not a directory.
37  *   6) chmod(2) returns -1 and sets errno to ENOENT if the specified file
38  *	does not exists.
39  */
40 
41 #ifndef _GNU_SOURCE
42 # define _GNU_SOURCE
43 #endif
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <errno.h>
50 #include <string.h>
51 #include <signal.h>
52 #include <grp.h>
53 #include <pwd.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <sys/mman.h>
57 #include <sys/mount.h>
58 
59 #include "test.h"
60 #include "safe_macros.h"
61 
62 #define MODE_RWX	(S_IRWXU|S_IRWXG|S_IRWXO)
63 #define FILE_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
64 #define DIR_TEMP	"testdir_1"
65 #define TEST_FILE1	"tfile_1"
66 #define TEST_FILE2	"testdir_1/tfile_2"
67 #define TEST_FILE3	"t_file/tfile_3"
68 #define TEST_FILE4	"test_file4"
69 #define MNT_POINT	"mntpoint"
70 
71 #define DIR_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
72 			 S_IXGRP|S_IROTH|S_IXOTH)
73 
74 static char long_path[PATH_MAX + 2];
75 
76 static const char *device;
77 static int mount_flag;
78 static uid_t nobody_uid;
79 
80 static void set_root(void);
81 static void set_nobody(void);
82 
83 struct test_case_t {
84 	char *pathname;
85 	mode_t mode;
86 	int exp_errno;
87 	void (*setup)(void);
88 	void (*cleanup)(void);
89 } tc[] = {
90 	{TEST_FILE1, FILE_MODE, EPERM, set_nobody, set_root},
91 	{TEST_FILE2, FILE_MODE, EACCES, set_nobody, set_root},
92 	{(char *)-1, FILE_MODE, EFAULT, NULL, NULL},
93 	{(char *)-2, FILE_MODE, EFAULT, NULL, NULL},
94 	{long_path, FILE_MODE, ENAMETOOLONG, NULL, NULL},
95 	{"", FILE_MODE, ENOENT, NULL, NULL},
96 	{TEST_FILE3, FILE_MODE, ENOTDIR, NULL, NULL},
97 	{MNT_POINT, FILE_MODE, EROFS, NULL, NULL},
98 	{TEST_FILE4, FILE_MODE, ELOOP, NULL, NULL},
99 };
100 
101 char *TCID = "chmod06";
102 int TST_TOTAL = ARRAY_SIZE(tc);
103 
104 static char *bad_addr = 0;
105 
106 static void setup(void);
107 static void cleanup(void);
108 
main(int ac,char ** av)109 int main(int ac, char **av)
110 {
111 	int lc;
112 	int i;
113 
114 	tst_parse_opts(ac, av, NULL, NULL);
115 
116 	setup();
117 
118 	for (lc = 0; TEST_LOOPING(lc); lc++) {
119 		tst_count = 0;
120 
121 		for (i = 0; i < TST_TOTAL; i++) {
122 
123 			if (tc[i].setup)
124 				tc[i].setup();
125 
126 			TEST(chmod(tc[i].pathname, tc[i].mode));
127 
128 			if (tc[i].cleanup)
129 				tc[i].cleanup();
130 
131 			if (TEST_RETURN != -1) {
132 				tst_resm(TFAIL, "chmod succeeded unexpectedly");
133 				continue;
134 			}
135 
136 			if (TEST_ERRNO == tc[i].exp_errno)
137 				tst_resm(TPASS | TTERRNO,
138 					 "chmod failed as expected");
139 			else
140 				tst_resm(TFAIL | TTERRNO,
141 					 "chmod failed unexpectedly; "
142 					 "expected %d - %s",
143 					 tc[i].exp_errno,
144 					 tst_strerrno(tc[i].exp_errno));
145 		}
146 
147 	}
148 
149 	cleanup();
150 	tst_exit();
151 }
152 
set_root(void)153 void set_root(void)
154 {
155 	SAFE_SETEUID(cleanup, 0);
156 }
157 
set_nobody(void)158 void set_nobody(void)
159 {
160 	SAFE_SETEUID(cleanup, nobody_uid);
161 }
162 
setup(void)163 void setup(void)
164 {
165 	struct passwd *nobody;
166 	const char *fs_type;
167 
168 	tst_sig(FORK, DEF_HANDLER, cleanup);
169 
170 	tst_require_root();
171 
172 	TEST_PAUSE;
173 
174 	nobody = SAFE_GETPWNAM(NULL, "nobody");
175 	nobody_uid = nobody->pw_uid;
176 
177 	bad_addr = SAFE_MMAP(NULL, 0, 1, PROT_NONE,
178 			MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
179 	tc[3].pathname = bad_addr;
180 
181 	tst_tmpdir();
182 
183 	fs_type = tst_dev_fs_type();
184 	device = tst_acquire_device(cleanup);
185 
186 	if (!device)
187 		tst_brkm(TCONF, cleanup, "Failed to obtain block device");
188 
189 	SAFE_TOUCH(cleanup, TEST_FILE1, 0666, NULL);
190 	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
191 	SAFE_TOUCH(cleanup, TEST_FILE2, 0666, NULL);
192 	SAFE_CHMOD(cleanup, DIR_TEMP, FILE_MODE);
193 	SAFE_TOUCH(cleanup, "t_file", MODE_RWX, NULL);
194 
195 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
196 
197 	SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
198 
199 	/*
200 	 * mount a read-only file system for test EROFS
201 	 */
202 	if (mount(device, MNT_POINT, fs_type, MS_RDONLY, NULL) < 0) {
203 		tst_brkm(TBROK | TERRNO, cleanup,
204 			 "mount device:%s failed", device);
205 	}
206 	mount_flag = 1;
207 
208 	memset(long_path, 'a', PATH_MAX+1);
209 
210 	/*
211 	 * create two symbolic links who point to each other for
212 	 * test ELOOP.
213 	 */
214 	SAFE_SYMLINK(cleanup, "test_file4", "test_file5");
215 	SAFE_SYMLINK(cleanup, "test_file5", "test_file4");
216 }
217 
cleanup(void)218 static void cleanup(void)
219 {
220 	if (chmod(DIR_TEMP, MODE_RWX) == -1)
221 		tst_resm(TBROK | TERRNO, "chmod(%s) failed", DIR_TEMP);
222 
223 	if (mount_flag && tst_umount(MNT_POINT) < 0) {
224 		tst_brkm(TBROK | TERRNO, NULL,
225 			 "umount device:%s failed", device);
226 	}
227 
228 	if (device)
229 		tst_release_device(device);
230 
231 	tst_rmdir();
232 }
233