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 [email protected]
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 "safe_macros.h"
61
62 /*
63 * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
64 * rewriting all tests to the new API.
65 */
66 #include "compat_16.h"
67
68 #define TEST_USER "nobody"
69 #define MODE_RWX S_IRWXU | S_IRWXG | S_IRWXO
70 #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
71 #define DIR_TEMP "testdir_1"
72 #define TEST_FILE1 "tfile_1"
73 #define SFILE1 "sfile_1"
74 #define TEST_FILE2 "testdir_1/tfile_2"
75 #define SFILE2 "testdir_1/sfile_2"
76 #define TFILE3 "t_file"
77 #define SFILE3 "t_file/sfile"
78
79 TCID_DEFINE(lchown02);
80 int TST_TOTAL = 7;
81
82 static void setup(void);
83 static void cleanup(void);
84 static void setup_eperm(int pos);
85 static void setup_eacces(int pos);
86 static void setup_enotdir(int pos);
87 static void setup_longpath(int pos);
88 static void setup_efault(int pos);
89
90 static char path[PATH_MAX + 2];
91
92 struct test_case_t {
93 char *pathname;
94 char *desc;
95 int exp_errno;
96 void (*setup) (int pos);
97 };
98
99 static struct test_case_t test_cases[] = {
100 {SFILE1, "Process is not owner/root", EPERM, setup_eperm},
101 {SFILE2, "Search permission denied", EACCES, setup_eacces},
102 {NULL, "Unaccessible address space", EFAULT, setup_efault},
103 {path, "Pathname too long", ENAMETOOLONG, setup_longpath},
104 {SFILE3, "Path contains regular file", ENOTDIR, setup_enotdir},
105 {"", "Pathname is empty", ENOENT, NULL},
106 {NULL, NULL, 0, NULL}
107 };
108
109 static struct passwd *ltpuser;
110
main(int argc,char * argv[])111 int main(int argc, char *argv[])
112 {
113 int lc;
114 uid_t user_id;
115 gid_t group_id;
116 int i;
117
118 tst_parse_opts(argc, argv, NULL, NULL);
119
120 setup();
121
122 user_id = geteuid();
123 UID16_CHECK(user_id, lchown, cleanup);
124 group_id = getegid();
125 GID16_CHECK(group_id, lchown, cleanup);
126
127 for (lc = 0; TEST_LOOPING(lc); lc++) {
128 tst_count = 0;
129
130 for (i = 0; test_cases[i].desc != NULL; i++) {
131 char *file_name = test_cases[i].pathname;
132 char *test_desc = test_cases[i].desc;
133
134 /*
135 * Call lchown(2) to test different test conditions.
136 * verify that it fails with -1 return value and
137 * sets appropriate errno.
138 */
139 TEST(LCHOWN(cleanup, file_name, user_id, group_id));
140
141 /* Check return code from lchown(2) */
142 if (TEST_RETURN == -1) {
143 if (TEST_ERRNO == test_cases[i].exp_errno) {
144 tst_resm(TPASS,
145 "lchown(2) fails, %s, errno:%d",
146 test_desc, TEST_ERRNO);
147 } else {
148 tst_resm(TFAIL, "lchown(2) fails, %s, "
149 "errno:%d, expected errno:%d",
150 test_desc, TEST_ERRNO,
151 test_cases[i].exp_errno);
152 }
153 } else {
154 tst_resm(TFAIL, "lchown(2) returned %ld, "
155 "expected -1, errno:%d", TEST_RETURN,
156 test_cases[i].exp_errno);
157 }
158 }
159 }
160
161 cleanup();
162 tst_exit();
163 }
164
setup(void)165 static void setup(void)
166 {
167 int i;
168
169 tst_sig(FORK, DEF_HANDLER, cleanup);
170
171 tst_require_root();
172
173 TEST_PAUSE;
174
175 /* change euid and gid to nobody */
176 ltpuser = getpwnam(TEST_USER);
177
178 if (ltpuser == NULL)
179 tst_brkm(TBROK, cleanup, "getpwnam failed");
180
181 if (setgid(ltpuser->pw_uid) == -1)
182 tst_resm(TBROK | TERRNO, "setgid failed");
183
184 tst_tmpdir();
185
186 for (i = 0; test_cases[i].desc != NULL; i++)
187 if (test_cases[i].setup != NULL)
188 test_cases[i].setup(i);
189 }
190
191 /*
192 * setup_eperm() - setup function for a test condition for which lchown(2)
193 * returns -1 and sets errno to EPERM.
194 *
195 * Create test file and symlink with uid 0.
196 */
setup_eperm(int pos LTP_ATTRIBUTE_UNUSED)197 static void setup_eperm(int pos LTP_ATTRIBUTE_UNUSED)
198 {
199 int fd;
200
201 /* create a testfile */
202 if ((fd = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1)
203 tst_brkm(TBROK | TERRNO, cleanup, "open failed");
204
205 SAFE_CLOSE(cleanup, fd);
206
207 /* become root once more */
208 if (seteuid(0) == -1)
209 tst_resm(TBROK | TERRNO, "setuid(0) failed");
210
211 /* create symling to testfile */
212 SAFE_SYMLINK(cleanup, TEST_FILE1, SFILE1);
213
214 /* back to the user nobody */
215 if (seteuid(ltpuser->pw_uid) == -1)
216 tst_resm(TBROK | TERRNO, "seteuid(%d) failed", ltpuser->pw_uid);
217 }
218
219 /*
220 * setup_eaccess() - setup function for a test condition for which lchown(2)
221 * returns -1 and sets errno to EACCES.
222 *
223 * Create a test directory under temporary directory and create a test file
224 * under this directory with mode "0666" permissions.
225 * Modify the mode permissions on test directory such that process will not
226 * have search permissions on test directory.
227 */
setup_eacces(int pos LTP_ATTRIBUTE_UNUSED)228 static void setup_eacces(int pos LTP_ATTRIBUTE_UNUSED)
229 {
230 int fd;
231
232 /* create a test directory */
233 SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
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 SAFE_CLOSE(cleanup, fd);
240
241 /* create a symlink of testfile */
242 SAFE_SYMLINK(cleanup, TEST_FILE2, SFILE2);
243
244 /* modify mode permissions on test directory */
245 SAFE_CHMOD(cleanup, DIR_TEMP, FILE_MODE);
246 }
247
248 /*
249 * setup_efault() -- setup for a test condition where lchown(2) returns -1 and
250 * sets errno to EFAULT.
251 *
252 * Create "bad address" by explicitly mmaping anonymous page that may not be
253 * accesed (see PROT_NONE).
254 */
setup_efault(int pos)255 static void setup_efault(int pos)
256 {
257 test_cases[pos].pathname = tst_get_bad_addr(cleanup);
258 }
259
260 /*
261 * setup_enotdir() - setup function for a test condition for which chown(2)
262 * returns -1 and sets errno to ENOTDIR.
263 *
264 * Create a regular file "t_file" to call lchown(2) on "t_file/sfile" later.
265 */
setup_enotdir(int pos LTP_ATTRIBUTE_UNUSED)266 static void setup_enotdir(int pos LTP_ATTRIBUTE_UNUSED)
267 {
268 int fd;
269
270 /* create a testfile under temporary directory */
271 if ((fd = open(TFILE3, O_RDWR | O_CREAT, MODE_RWX)) == -1) {
272 tst_brkm(TBROK | TERRNO, cleanup, "open(2) %s failed", TFILE3);
273 }
274
275 SAFE_CLOSE(cleanup, fd);
276 }
277
278 /*
279 * longpath_setup() - setup to create a node with a name length exceeding
280 * the length of PATH_MAX.
281 */
setup_longpath(int pos)282 static void setup_longpath(int pos)
283 {
284 memset(test_cases[pos].pathname, 'a', PATH_MAX + 1);
285 test_cases[pos].pathname[PATH_MAX + 1] = '\0';
286 }
287
cleanup(void)288 static void cleanup(void)
289 {
290 if (seteuid(0) == -1) {
291 tst_resm(TINFO | TERRNO,
292 "seteuid(2) failed to set the effective uid to 0");
293 }
294
295 tst_rmdir();
296 }
297