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