]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
c270684a RC |
2 | |
3 | #include <unistd.h> | |
4 | ||
5 | #include "alloc-util.h" | |
c270684a RC |
6 | #include "fd-util.h" |
7 | #include "fs-util.h" | |
1ed34d75 | 8 | #include "id128-util.h" |
c270684a RC |
9 | #include "macro.h" |
10 | #include "mkdir.h" | |
d944dc95 | 11 | #include "path-util.h" |
c270684a | 12 | #include "rm-rf.h" |
1ed34d75 | 13 | #include "stdio-util.h" |
c270684a RC |
14 | #include "string-util.h" |
15 | #include "strv.h" | |
27964854 | 16 | #include "tests.h" |
e4de7287 | 17 | #include "tmpfile-util.h" |
f14f1806 | 18 | #include "user-util.h" |
c270684a | 19 | #include "util.h" |
9590065f | 20 | #include "virt.h" |
c270684a | 21 | |
27964854 ZJS |
22 | static const char *arg_test_dir = NULL; |
23 | ||
d944dc95 | 24 | static void test_chase_symlinks(void) { |
c9825701 | 25 | _cleanup_free_ char *result = NULL; |
27964854 | 26 | char *temp; |
b12d25a8 | 27 | const char *top, *p, *pslash, *q, *qslash; |
1f56e4ce | 28 | struct stat st; |
1ed34d75 | 29 | int r, pfd; |
d944dc95 | 30 | |
27964854 ZJS |
31 | log_info("/* %s */", __func__); |
32 | ||
33 | temp = strjoina(arg_test_dir ?: "/tmp", "/test-chase.XXXXXX"); | |
d944dc95 LP |
34 | assert_se(mkdtemp(temp)); |
35 | ||
36 | top = strjoina(temp, "/top"); | |
37 | assert_se(mkdir(top, 0700) >= 0); | |
38 | ||
39 | p = strjoina(top, "/dot"); | |
27964854 ZJS |
40 | if (symlink(".", p) < 0) { |
41 | assert_se(IN_SET(errno, EINVAL, ENOSYS, ENOTTY, EPERM)); | |
42 | log_tests_skipped_errno(errno, "symlink() not possible"); | |
43 | goto cleanup; | |
44 | }; | |
d944dc95 LP |
45 | |
46 | p = strjoina(top, "/dotdot"); | |
47 | assert_se(symlink("..", p) >= 0); | |
48 | ||
49 | p = strjoina(top, "/dotdota"); | |
50 | assert_se(symlink("../a", p) >= 0); | |
51 | ||
52 | p = strjoina(temp, "/a"); | |
53 | assert_se(symlink("b", p) >= 0); | |
54 | ||
55 | p = strjoina(temp, "/b"); | |
56 | assert_se(symlink("/usr", p) >= 0); | |
57 | ||
58 | p = strjoina(temp, "/start"); | |
59 | assert_se(symlink("top/dot/dotdota", p) >= 0); | |
60 | ||
df878e68 ZJS |
61 | /* Paths that use symlinks underneath the "root" */ |
62 | ||
c4f4fce7 | 63 | r = chase_symlinks(p, NULL, 0, &result); |
a9fb0867 | 64 | assert_se(r > 0); |
d944dc95 | 65 | assert_se(path_equal(result, "/usr")); |
b12d25a8 | 66 | result = mfree(result); |
d944dc95 | 67 | |
b12d25a8 ZJS |
68 | pslash = strjoina(p, "/"); |
69 | r = chase_symlinks(pslash, NULL, 0, &result); | |
70 | assert_se(r > 0); | |
71 | assert_se(path_equal(result, "/usr/")); | |
d944dc95 | 72 | result = mfree(result); |
b12d25a8 | 73 | |
c4f4fce7 | 74 | r = chase_symlinks(p, temp, 0, &result); |
d944dc95 LP |
75 | assert_se(r == -ENOENT); |
76 | ||
b12d25a8 ZJS |
77 | r = chase_symlinks(pslash, temp, 0, &result); |
78 | assert_se(r == -ENOENT); | |
79 | ||
d944dc95 | 80 | q = strjoina(temp, "/usr"); |
a9fb0867 | 81 | |
cb638b5e | 82 | r = chase_symlinks(p, temp, CHASE_NONEXISTENT, &result); |
a9fb0867 LP |
83 | assert_se(r == 0); |
84 | assert_se(path_equal(result, q)); | |
b12d25a8 | 85 | result = mfree(result); |
a9fb0867 | 86 | |
b12d25a8 | 87 | qslash = strjoina(q, "/"); |
d944dc95 | 88 | |
b12d25a8 ZJS |
89 | r = chase_symlinks(pslash, temp, CHASE_NONEXISTENT, &result); |
90 | assert_se(r == 0); | |
91 | assert_se(path_equal(result, qslash)); | |
f4b85a0f | 92 | result = mfree(result); |
b12d25a8 ZJS |
93 | |
94 | assert_se(mkdir(q, 0700) >= 0); | |
95 | ||
c4f4fce7 | 96 | r = chase_symlinks(p, temp, 0, &result); |
a9fb0867 | 97 | assert_se(r > 0); |
d944dc95 | 98 | assert_se(path_equal(result, q)); |
b12d25a8 ZJS |
99 | result = mfree(result); |
100 | ||
101 | r = chase_symlinks(pslash, temp, 0, &result); | |
102 | assert_se(r > 0); | |
103 | assert_se(path_equal(result, qslash)); | |
104 | result = mfree(result); | |
d944dc95 LP |
105 | |
106 | p = strjoina(temp, "/slash"); | |
107 | assert_se(symlink("/", p) >= 0); | |
108 | ||
c4f4fce7 | 109 | r = chase_symlinks(p, NULL, 0, &result); |
a9fb0867 | 110 | assert_se(r > 0); |
d944dc95 | 111 | assert_se(path_equal(result, "/")); |
d944dc95 | 112 | result = mfree(result); |
b12d25a8 | 113 | |
c4f4fce7 | 114 | r = chase_symlinks(p, temp, 0, &result); |
a9fb0867 | 115 | assert_se(r > 0); |
d944dc95 | 116 | assert_se(path_equal(result, temp)); |
b12d25a8 | 117 | result = mfree(result); |
d944dc95 | 118 | |
df878e68 ZJS |
119 | /* Paths that would "escape" outside of the "root" */ |
120 | ||
121 | p = strjoina(temp, "/6dots"); | |
122 | assert_se(symlink("../../..", p) >= 0); | |
123 | ||
c4f4fce7 | 124 | r = chase_symlinks(p, temp, 0, &result); |
a9fb0867 | 125 | assert_se(r > 0 && path_equal(result, temp)); |
b12d25a8 | 126 | result = mfree(result); |
df878e68 ZJS |
127 | |
128 | p = strjoina(temp, "/6dotsusr"); | |
129 | assert_se(symlink("../../../usr", p) >= 0); | |
130 | ||
c4f4fce7 | 131 | r = chase_symlinks(p, temp, 0, &result); |
a9fb0867 | 132 | assert_se(r > 0 && path_equal(result, q)); |
b12d25a8 | 133 | result = mfree(result); |
df878e68 ZJS |
134 | |
135 | p = strjoina(temp, "/top/8dotsusr"); | |
136 | assert_se(symlink("../../../../usr", p) >= 0); | |
137 | ||
c4f4fce7 | 138 | r = chase_symlinks(p, temp, 0, &result); |
a9fb0867 | 139 | assert_se(r > 0 && path_equal(result, q)); |
b12d25a8 | 140 | result = mfree(result); |
df878e68 ZJS |
141 | |
142 | /* Paths that contain repeated slashes */ | |
143 | ||
d944dc95 LP |
144 | p = strjoina(temp, "/slashslash"); |
145 | assert_se(symlink("///usr///", p) >= 0); | |
146 | ||
c4f4fce7 | 147 | r = chase_symlinks(p, NULL, 0, &result); |
a9fb0867 | 148 | assert_se(r > 0); |
d944dc95 | 149 | assert_se(path_equal(result, "/usr")); |
d944dc95 | 150 | result = mfree(result); |
b12d25a8 | 151 | |
c4f4fce7 | 152 | r = chase_symlinks(p, temp, 0, &result); |
a9fb0867 | 153 | assert_se(r > 0); |
d944dc95 | 154 | assert_se(path_equal(result, q)); |
b12d25a8 | 155 | result = mfree(result); |
d944dc95 | 156 | |
df878e68 ZJS |
157 | /* Paths using . */ |
158 | ||
c4f4fce7 | 159 | r = chase_symlinks("/etc/./.././", NULL, 0, &result); |
a9fb0867 | 160 | assert_se(r > 0); |
d944dc95 | 161 | assert_se(path_equal(result, "/")); |
d944dc95 | 162 | result = mfree(result); |
b12d25a8 | 163 | |
c4f4fce7 | 164 | r = chase_symlinks("/etc/./.././", "/etc", 0, &result); |
a9fb0867 | 165 | assert_se(r > 0 && path_equal(result, "/etc")); |
d944dc95 | 166 | result = mfree(result); |
b12d25a8 | 167 | |
95f35ccc YW |
168 | r = chase_symlinks("/../.././//../../etc", NULL, 0, &result); |
169 | assert_se(r > 0); | |
170 | assert_se(streq(result, "/etc")); | |
171 | result = mfree(result); | |
172 | ||
173 | r = chase_symlinks("/../.././//../../test-chase.fsldajfl", NULL, CHASE_NONEXISTENT, &result); | |
174 | assert_se(r == 0); | |
175 | assert_se(streq(result, "/test-chase.fsldajfl")); | |
176 | result = mfree(result); | |
177 | ||
178 | r = chase_symlinks("/../.././//../../etc", "/", CHASE_PREFIX_ROOT, &result); | |
179 | assert_se(r > 0); | |
180 | assert_se(streq(result, "/etc")); | |
181 | result = mfree(result); | |
182 | ||
183 | r = chase_symlinks("/../.././//../../test-chase.fsldajfl", "/", CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &result); | |
184 | assert_se(r == 0); | |
185 | assert_se(streq(result, "/test-chase.fsldajfl")); | |
186 | result = mfree(result); | |
187 | ||
c4f4fce7 | 188 | r = chase_symlinks("/etc/machine-id/foo", NULL, 0, &result); |
d944dc95 | 189 | assert_se(r == -ENOTDIR); |
b12d25a8 | 190 | result = mfree(result); |
d944dc95 | 191 | |
df878e68 ZJS |
192 | /* Path that loops back to self */ |
193 | ||
d944dc95 LP |
194 | p = strjoina(temp, "/recursive-symlink"); |
195 | assert_se(symlink("recursive-symlink", p) >= 0); | |
c4f4fce7 | 196 | r = chase_symlinks(p, NULL, 0, &result); |
d944dc95 LP |
197 | assert_se(r == -ELOOP); |
198 | ||
a9fb0867 LP |
199 | /* Path which doesn't exist */ |
200 | ||
201 | p = strjoina(temp, "/idontexist"); | |
202 | r = chase_symlinks(p, NULL, 0, &result); | |
203 | assert_se(r == -ENOENT); | |
204 | ||
cb638b5e | 205 | r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result); |
a9fb0867 LP |
206 | assert_se(r == 0); |
207 | assert_se(path_equal(result, p)); | |
208 | result = mfree(result); | |
209 | ||
210 | p = strjoina(temp, "/idontexist/meneither"); | |
211 | r = chase_symlinks(p, NULL, 0, &result); | |
212 | assert_se(r == -ENOENT); | |
213 | ||
cb638b5e | 214 | r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result); |
a9fb0867 LP |
215 | assert_se(r == 0); |
216 | assert_se(path_equal(result, p)); | |
217 | result = mfree(result); | |
218 | ||
219 | /* Path which doesn't exist, but contains weird stuff */ | |
220 | ||
221 | p = strjoina(temp, "/idontexist/.."); | |
222 | r = chase_symlinks(p, NULL, 0, &result); | |
223 | assert_se(r == -ENOENT); | |
224 | ||
cb638b5e | 225 | r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result); |
a9fb0867 LP |
226 | assert_se(r == -ENOENT); |
227 | ||
877777d7 CCW |
228 | p = strjoina(temp, "/target"); |
229 | q = strjoina(temp, "/top"); | |
230 | assert_se(symlink(q, p) >= 0); | |
231 | p = strjoina(temp, "/target/idontexist"); | |
232 | r = chase_symlinks(p, NULL, 0, &result); | |
233 | assert_se(r == -ENOENT); | |
234 | ||
f14f1806 LP |
235 | if (geteuid() == 0) { |
236 | p = strjoina(temp, "/priv1"); | |
237 | assert_se(mkdir(p, 0755) >= 0); | |
238 | ||
239 | q = strjoina(p, "/priv2"); | |
240 | assert_se(mkdir(q, 0755) >= 0); | |
241 | ||
242 | assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) >= 0); | |
243 | ||
244 | assert_se(chown(q, UID_NOBODY, GID_NOBODY) >= 0); | |
245 | assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) >= 0); | |
246 | ||
247 | assert_se(chown(p, UID_NOBODY, GID_NOBODY) >= 0); | |
248 | assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) >= 0); | |
249 | ||
250 | assert_se(chown(q, 0, 0) >= 0); | |
36c97dec | 251 | assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) == -ENOLINK); |
f14f1806 LP |
252 | |
253 | assert_se(rmdir(q) >= 0); | |
254 | assert_se(symlink("/etc/passwd", q) >= 0); | |
36c97dec | 255 | assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) == -ENOLINK); |
f14f1806 LP |
256 | |
257 | assert_se(chown(p, 0, 0) >= 0); | |
258 | assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) >= 0); | |
259 | } | |
260 | ||
1ed34d75 LP |
261 | p = strjoina(temp, "/machine-id-test"); |
262 | assert_se(symlink("/usr/../etc/./machine-id", p) >= 0); | |
263 | ||
264 | pfd = chase_symlinks(p, NULL, CHASE_OPEN, NULL); | |
265 | if (pfd != -ENOENT) { | |
1ed34d75 LP |
266 | _cleanup_close_ int fd = -1; |
267 | sd_id128_t a, b; | |
268 | ||
269 | assert_se(pfd >= 0); | |
270 | ||
f2324783 | 271 | fd = fd_reopen(pfd, O_RDONLY|O_CLOEXEC); |
1ed34d75 | 272 | assert_se(fd >= 0); |
1ed34d75 LP |
273 | safe_close(pfd); |
274 | ||
275 | assert_se(id128_read_fd(fd, ID128_PLAIN, &a) >= 0); | |
276 | assert_se(sd_id128_get_machine(&b) >= 0); | |
277 | assert_se(sd_id128_equal(a, b)); | |
278 | } | |
279 | ||
1f56e4ce FB |
280 | /* Test CHASE_NOFOLLOW */ |
281 | ||
282 | p = strjoina(temp, "/target"); | |
283 | q = strjoina(temp, "/symlink"); | |
284 | assert_se(symlink(p, q) >= 0); | |
285 | pfd = chase_symlinks(q, NULL, CHASE_OPEN|CHASE_NOFOLLOW, &result); | |
286 | assert_se(pfd > 0); | |
287 | assert_se(path_equal(result, q)); | |
288 | assert_se(fstat(pfd, &st) >= 0); | |
289 | assert_se(S_ISLNK(st.st_mode)); | |
290 | result = mfree(result); | |
291 | ||
292 | /* s1 -> s2 -> nonexistent */ | |
293 | q = strjoina(temp, "/s1"); | |
294 | assert_se(symlink("s2", q) >= 0); | |
295 | p = strjoina(temp, "/s2"); | |
296 | assert_se(symlink("nonexistent", p) >= 0); | |
297 | pfd = chase_symlinks(q, NULL, CHASE_OPEN|CHASE_NOFOLLOW, &result); | |
298 | assert_se(pfd > 0); | |
299 | assert_se(path_equal(result, q)); | |
300 | assert_se(fstat(pfd, &st) >= 0); | |
301 | assert_se(S_ISLNK(st.st_mode)); | |
302 | result = mfree(result); | |
303 | ||
49eb3659 LP |
304 | /* Test CHASE_ONE */ |
305 | ||
306 | p = strjoina(temp, "/start"); | |
307 | r = chase_symlinks(p, NULL, CHASE_STEP, &result); | |
308 | assert_se(r == 0); | |
309 | p = strjoina(temp, "/top/dot/dotdota"); | |
310 | assert_se(streq(p, result)); | |
311 | result = mfree(result); | |
312 | ||
313 | r = chase_symlinks(p, NULL, CHASE_STEP, &result); | |
314 | assert_se(r == 0); | |
315 | p = strjoina(temp, "/top/./dotdota"); | |
316 | assert_se(streq(p, result)); | |
317 | result = mfree(result); | |
318 | ||
319 | r = chase_symlinks(p, NULL, CHASE_STEP, &result); | |
320 | assert_se(r == 0); | |
321 | p = strjoina(temp, "/top/../a"); | |
322 | assert_se(streq(p, result)); | |
323 | result = mfree(result); | |
324 | ||
325 | r = chase_symlinks(p, NULL, CHASE_STEP, &result); | |
326 | assert_se(r == 0); | |
327 | p = strjoina(temp, "/a"); | |
328 | assert_se(streq(p, result)); | |
329 | result = mfree(result); | |
330 | ||
331 | r = chase_symlinks(p, NULL, CHASE_STEP, &result); | |
332 | assert_se(r == 0); | |
333 | p = strjoina(temp, "/b"); | |
334 | assert_se(streq(p, result)); | |
335 | result = mfree(result); | |
336 | ||
337 | r = chase_symlinks(p, NULL, CHASE_STEP, &result); | |
338 | assert_se(r == 0); | |
339 | assert_se(streq("/usr", result)); | |
340 | result = mfree(result); | |
341 | ||
342 | r = chase_symlinks("/usr", NULL, CHASE_STEP, &result); | |
343 | assert_se(r > 0); | |
344 | assert_se(streq("/usr", result)); | |
345 | result = mfree(result); | |
346 | ||
27964854 | 347 | cleanup: |
d944dc95 LP |
348 | assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); |
349 | } | |
350 | ||
c270684a | 351 | static void test_unlink_noerrno(void) { |
27964854 | 352 | char *name; |
c270684a RC |
353 | int fd; |
354 | ||
27964854 ZJS |
355 | log_info("/* %s */", __func__); |
356 | ||
357 | name = strjoina(arg_test_dir ?: "/tmp", "/test-close_nointr.XXXXXX"); | |
646853bd | 358 | fd = mkostemp_safe(name); |
c270684a RC |
359 | assert_se(fd >= 0); |
360 | assert_se(close_nointr(fd) >= 0); | |
361 | ||
362 | { | |
363 | PROTECT_ERRNO; | |
0192cbdb | 364 | errno = -42; |
c270684a | 365 | assert_se(unlink_noerrno(name) >= 0); |
0192cbdb | 366 | assert_se(errno == -42); |
c270684a | 367 | assert_se(unlink_noerrno(name) < 0); |
0192cbdb | 368 | assert_se(errno == -42); |
c270684a RC |
369 | } |
370 | } | |
371 | ||
372 | static void test_readlink_and_make_absolute(void) { | |
27964854 ZJS |
373 | const char *tempdir, *name, *name2, *name_alias; |
374 | _cleanup_free_ char *r1 = NULL, *r2 = NULL, *pwd = NULL; | |
375 | ||
376 | log_info("/* %s */", __func__); | |
377 | ||
378 | tempdir = strjoina(arg_test_dir ?: "/tmp", "/test-readlink_and_make_absolute"); | |
379 | name = strjoina(tempdir, "/original"); | |
380 | name2 = "test-readlink_and_make_absolute/original"; | |
381 | name_alias = strjoina(arg_test_dir ?: "/tmp", "/test-readlink_and_make_absolute-alias"); | |
c270684a | 382 | |
37c1d5e9 | 383 | assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid(), MKDIR_WARN_MODE) >= 0); |
c270684a RC |
384 | assert_se(touch(name) >= 0); |
385 | ||
27964854 ZJS |
386 | if (symlink(name, name_alias) < 0) { |
387 | assert_se(IN_SET(errno, EINVAL, ENOSYS, ENOTTY, EPERM)); | |
388 | log_tests_skipped_errno(errno, "symlink() not possible"); | |
389 | } else { | |
390 | assert_se(readlink_and_make_absolute(name_alias, &r1) >= 0); | |
391 | assert_se(streq(r1, name)); | |
392 | assert_se(unlink(name_alias) >= 0); | |
c270684a | 393 | |
27964854 | 394 | assert_se(safe_getcwd(&pwd) >= 0); |
cd76d4c2 | 395 | |
27964854 ZJS |
396 | assert_se(chdir(tempdir) >= 0); |
397 | assert_se(symlink(name2, name_alias) >= 0); | |
398 | assert_se(readlink_and_make_absolute(name_alias, &r2) >= 0); | |
399 | assert_se(streq(r2, name)); | |
400 | assert_se(unlink(name_alias) >= 0); | |
c270684a | 401 | |
27964854 ZJS |
402 | assert_se(chdir(pwd) >= 0); |
403 | } | |
cd76d4c2 | 404 | |
c270684a RC |
405 | assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); |
406 | } | |
407 | ||
408 | static void test_get_files_in_directory(void) { | |
409 | _cleanup_strv_free_ char **l = NULL, **t = NULL; | |
410 | ||
27964854 | 411 | assert_se(get_files_in_directory(arg_test_dir ?: "/tmp", &l) >= 0); |
c270684a RC |
412 | assert_se(get_files_in_directory(".", &t) >= 0); |
413 | assert_se(get_files_in_directory(".", NULL) >= 0); | |
414 | } | |
415 | ||
34a8f081 | 416 | static void test_var_tmp(void) { |
4245eb50 | 417 | _cleanup_free_ char *tmpdir_backup = NULL, *temp_backup = NULL, *tmp_backup = NULL; |
992e8f22 | 418 | const char *tmp_dir = NULL, *t; |
34a8f081 | 419 | |
27964854 ZJS |
420 | log_info("/* %s */", __func__); |
421 | ||
992e8f22 LP |
422 | t = getenv("TMPDIR"); |
423 | if (t) { | |
424 | tmpdir_backup = strdup(t); | |
425 | assert_se(tmpdir_backup); | |
426 | } | |
34a8f081 | 427 | |
4245eb50 MAP |
428 | t = getenv("TEMP"); |
429 | if (t) { | |
430 | temp_backup = strdup(t); | |
431 | assert_se(temp_backup); | |
432 | } | |
433 | ||
434 | t = getenv("TMP"); | |
435 | if (t) { | |
436 | tmp_backup = strdup(t); | |
437 | assert_se(tmp_backup); | |
438 | } | |
439 | ||
85e55d14 YW |
440 | assert_se(unsetenv("TMPDIR") >= 0); |
441 | assert_se(unsetenv("TEMP") >= 0); | |
442 | assert_se(unsetenv("TMP") >= 0); | |
34a8f081 | 443 | |
992e8f22 LP |
444 | assert_se(var_tmp_dir(&tmp_dir) >= 0); |
445 | assert_se(streq(tmp_dir, "/var/tmp")); | |
34a8f081 | 446 | |
992e8f22 LP |
447 | assert_se(setenv("TMPDIR", "/tmp", true) >= 0); |
448 | assert_se(streq(getenv("TMPDIR"), "/tmp")); | |
34a8f081 | 449 | |
992e8f22 LP |
450 | assert_se(var_tmp_dir(&tmp_dir) >= 0); |
451 | assert_se(streq(tmp_dir, "/tmp")); | |
34a8f081 | 452 | |
992e8f22 LP |
453 | assert_se(setenv("TMPDIR", "/88_does_not_exist_88", true) >= 0); |
454 | assert_se(streq(getenv("TMPDIR"), "/88_does_not_exist_88")); | |
34a8f081 | 455 | |
992e8f22 LP |
456 | assert_se(var_tmp_dir(&tmp_dir) >= 0); |
457 | assert_se(streq(tmp_dir, "/var/tmp")); | |
34a8f081 | 458 | |
992e8f22 LP |
459 | if (tmpdir_backup) { |
460 | assert_se(setenv("TMPDIR", tmpdir_backup, true) >= 0); | |
461 | assert_se(streq(getenv("TMPDIR"), tmpdir_backup)); | |
34a8f081 | 462 | } |
4245eb50 MAP |
463 | |
464 | if (temp_backup) { | |
465 | assert_se(setenv("TEMP", temp_backup, true) >= 0); | |
466 | assert_se(streq(getenv("TEMP"), temp_backup)); | |
467 | } | |
468 | ||
469 | if (tmp_backup) { | |
470 | assert_se(setenv("TMP", tmp_backup, true) >= 0); | |
471 | assert_se(streq(getenv("TMP"), tmp_backup)); | |
472 | } | |
34a8f081 OW |
473 | } |
474 | ||
49bfc877 | 475 | static void test_dot_or_dot_dot(void) { |
27964854 ZJS |
476 | log_info("/* %s */", __func__); |
477 | ||
49bfc877 LP |
478 | assert_se(!dot_or_dot_dot(NULL)); |
479 | assert_se(!dot_or_dot_dot("")); | |
480 | assert_se(!dot_or_dot_dot("xxx")); | |
481 | assert_se(dot_or_dot_dot(".")); | |
482 | assert_se(dot_or_dot_dot("..")); | |
483 | assert_se(!dot_or_dot_dot(".foo")); | |
484 | assert_se(!dot_or_dot_dot("..foo")); | |
485 | } | |
486 | ||
57a4359e LP |
487 | static void test_access_fd(void) { |
488 | _cleanup_(rmdir_and_freep) char *p = NULL; | |
489 | _cleanup_close_ int fd = -1; | |
27964854 | 490 | const char *a; |
57a4359e | 491 | |
27964854 ZJS |
492 | log_info("/* %s */", __func__); |
493 | ||
494 | a = strjoina(arg_test_dir ?: "/tmp", "/access-fd.XXXXXX"); | |
495 | assert_se(mkdtemp_malloc(a, &p) >= 0); | |
57a4359e LP |
496 | |
497 | fd = open(p, O_RDONLY|O_DIRECTORY|O_CLOEXEC); | |
498 | assert_se(fd >= 0); | |
499 | ||
500 | assert_se(access_fd(fd, R_OK) >= 0); | |
501 | assert_se(access_fd(fd, F_OK) >= 0); | |
502 | assert_se(access_fd(fd, W_OK) >= 0); | |
503 | ||
504 | assert_se(fchmod(fd, 0000) >= 0); | |
505 | ||
506 | assert_se(access_fd(fd, F_OK) >= 0); | |
507 | ||
508 | if (geteuid() == 0) { | |
509 | assert_se(access_fd(fd, R_OK) >= 0); | |
510 | assert_se(access_fd(fd, W_OK) >= 0); | |
511 | } else { | |
512 | assert_se(access_fd(fd, R_OK) == -EACCES); | |
513 | assert_se(access_fd(fd, W_OK) == -EACCES); | |
514 | } | |
515 | } | |
516 | ||
9e3fa6e8 LP |
517 | static void test_touch_file(void) { |
518 | uid_t test_uid, test_gid; | |
519 | _cleanup_(rm_rf_physical_and_freep) char *p = NULL; | |
520 | struct stat st; | |
521 | const char *a; | |
522 | usec_t test_mtime; | |
9590065f | 523 | int r; |
9e3fa6e8 | 524 | |
27964854 ZJS |
525 | log_info("/* %s */", __func__); |
526 | ||
9e3fa6e8 LP |
527 | test_uid = geteuid() == 0 ? 65534 : getuid(); |
528 | test_gid = geteuid() == 0 ? 65534 : getgid(); | |
529 | ||
530 | test_mtime = usec_sub_unsigned(now(CLOCK_REALTIME), USEC_PER_WEEK); | |
531 | ||
27964854 ZJS |
532 | a = strjoina(arg_test_dir ?: "/dev/shm", "/touch-file-XXXXXX"); |
533 | assert_se(mkdtemp_malloc(a, &p) >= 0); | |
9e3fa6e8 LP |
534 | |
535 | a = strjoina(p, "/regular"); | |
27964854 ZJS |
536 | r = touch_file(a, false, test_mtime, test_uid, test_gid, 0640); |
537 | if (r < 0) { | |
538 | assert_se(IN_SET(r, -EINVAL, -ENOSYS, -ENOTTY, -EPERM)); | |
539 | log_tests_skipped_errno(errno, "touch_file() not possible"); | |
540 | return; | |
541 | } | |
542 | ||
9e3fa6e8 LP |
543 | assert_se(lstat(a, &st) >= 0); |
544 | assert_se(st.st_uid == test_uid); | |
545 | assert_se(st.st_gid == test_gid); | |
546 | assert_se(S_ISREG(st.st_mode)); | |
547 | assert_se((st.st_mode & 0777) == 0640); | |
548 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
549 | ||
550 | a = strjoina(p, "/dir"); | |
551 | assert_se(mkdir(a, 0775) >= 0); | |
552 | assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0); | |
553 | assert_se(lstat(a, &st) >= 0); | |
554 | assert_se(st.st_uid == test_uid); | |
555 | assert_se(st.st_gid == test_gid); | |
556 | assert_se(S_ISDIR(st.st_mode)); | |
557 | assert_se((st.st_mode & 0777) == 0640); | |
558 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
559 | ||
560 | a = strjoina(p, "/fifo"); | |
561 | assert_se(mkfifo(a, 0775) >= 0); | |
562 | assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0); | |
563 | assert_se(lstat(a, &st) >= 0); | |
564 | assert_se(st.st_uid == test_uid); | |
565 | assert_se(st.st_gid == test_gid); | |
566 | assert_se(S_ISFIFO(st.st_mode)); | |
567 | assert_se((st.st_mode & 0777) == 0640); | |
568 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
569 | ||
570 | a = strjoina(p, "/sock"); | |
571 | assert_se(mknod(a, 0775 | S_IFSOCK, 0) >= 0); | |
572 | assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0); | |
573 | assert_se(lstat(a, &st) >= 0); | |
574 | assert_se(st.st_uid == test_uid); | |
575 | assert_se(st.st_gid == test_gid); | |
576 | assert_se(S_ISSOCK(st.st_mode)); | |
577 | assert_se((st.st_mode & 0777) == 0640); | |
578 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
579 | ||
580 | if (geteuid() == 0) { | |
581 | a = strjoina(p, "/cdev"); | |
9590065f YW |
582 | r = mknod(a, 0775 | S_IFCHR, makedev(0, 0)); |
583 | if (r < 0 && errno == EPERM && detect_container() > 0) { | |
584 | log_notice("Running in unprivileged container? Skipping remaining tests in %s", __func__); | |
585 | return; | |
586 | } | |
587 | assert_se(r >= 0); | |
9e3fa6e8 LP |
588 | assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0); |
589 | assert_se(lstat(a, &st) >= 0); | |
590 | assert_se(st.st_uid == test_uid); | |
591 | assert_se(st.st_gid == test_gid); | |
592 | assert_se(S_ISCHR(st.st_mode)); | |
593 | assert_se((st.st_mode & 0777) == 0640); | |
594 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
595 | ||
596 | a = strjoina(p, "/bdev"); | |
597 | assert_se(mknod(a, 0775 | S_IFBLK, makedev(0, 0)) >= 0); | |
598 | assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0); | |
599 | assert_se(lstat(a, &st) >= 0); | |
600 | assert_se(st.st_uid == test_uid); | |
601 | assert_se(st.st_gid == test_gid); | |
602 | assert_se(S_ISBLK(st.st_mode)); | |
603 | assert_se((st.st_mode & 0777) == 0640); | |
604 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
605 | } | |
606 | ||
607 | a = strjoina(p, "/lnk"); | |
608 | assert_se(symlink("target", a) >= 0); | |
609 | assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0); | |
610 | assert_se(lstat(a, &st) >= 0); | |
611 | assert_se(st.st_uid == test_uid); | |
612 | assert_se(st.st_gid == test_gid); | |
613 | assert_se(S_ISLNK(st.st_mode)); | |
614 | assert_se((st.st_mode & 0777) == 0640); | |
615 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
616 | } | |
617 | ||
43767d9d LP |
618 | static void test_unlinkat_deallocate(void) { |
619 | _cleanup_free_ char *p = NULL; | |
620 | _cleanup_close_ int fd = -1; | |
621 | struct stat st; | |
622 | ||
27964854 ZJS |
623 | log_info("/* %s */", __func__); |
624 | ||
625 | assert_se(tempfn_random_child(arg_test_dir, "unlink-deallocation", &p) >= 0); | |
43767d9d LP |
626 | |
627 | fd = open(p, O_WRONLY|O_CLOEXEC|O_CREAT|O_EXCL, 0600); | |
628 | assert_se(fd >= 0); | |
629 | ||
630 | assert_se(write(fd, "hallo\n", 6) == 6); | |
631 | ||
632 | assert_se(fstat(fd, &st) >= 0); | |
633 | assert_se(st.st_size == 6); | |
634 | assert_se(st.st_blocks > 0); | |
635 | assert_se(st.st_nlink == 1); | |
636 | ||
637 | assert_se(unlinkat_deallocate(AT_FDCWD, p, 0) >= 0); | |
638 | ||
639 | assert_se(fstat(fd, &st) >= 0); | |
27964854 ZJS |
640 | assert_se(IN_SET(st.st_size, 0, 6)); /* depending on whether hole punching worked the size will be 6 |
641 | (it worked) or 0 (we had to resort to truncation) */ | |
43767d9d LP |
642 | assert_se(st.st_blocks == 0); |
643 | assert_se(st.st_nlink == 0); | |
644 | } | |
645 | ||
11b29a96 LP |
646 | static void test_fsync_directory_of_file(void) { |
647 | _cleanup_close_ int fd = -1; | |
648 | ||
27964854 ZJS |
649 | log_info("/* %s */", __func__); |
650 | ||
651 | fd = open_tmpfile_unlinkable(arg_test_dir, O_RDWR); | |
11b29a96 LP |
652 | assert_se(fd >= 0); |
653 | ||
654 | assert_se(fsync_directory_of_file(fd) >= 0); | |
655 | } | |
656 | ||
4a5d7761 | 657 | static void test_rename_noreplace(void) { |
7158b4b3 | 658 | static const char* const table[] = { |
4a5d7761 LP |
659 | "/reg", |
660 | "/dir", | |
661 | "/fifo", | |
662 | "/socket", | |
663 | "/symlink", | |
664 | NULL | |
665 | }; | |
666 | ||
667 | _cleanup_(rm_rf_physical_and_freep) char *z = NULL; | |
27964854 | 668 | const char *j = NULL; |
4a5d7761 LP |
669 | char **a, **b; |
670 | ||
27964854 ZJS |
671 | log_info("/* %s */", __func__); |
672 | ||
673 | if (arg_test_dir) | |
674 | j = strjoina(arg_test_dir, "/testXXXXXX"); | |
7158b4b3 | 675 | assert_se(mkdtemp_malloc(j, &z) >= 0); |
4a5d7761 LP |
676 | |
677 | j = strjoina(z, table[0]); | |
678 | assert_se(touch(j) >= 0); | |
679 | ||
680 | j = strjoina(z, table[1]); | |
681 | assert_se(mkdir(j, 0777) >= 0); | |
682 | ||
683 | j = strjoina(z, table[2]); | |
684 | (void) mkfifo(j, 0777); | |
685 | ||
686 | j = strjoina(z, table[3]); | |
687 | (void) mknod(j, S_IFSOCK | 0777, 0); | |
688 | ||
689 | j = strjoina(z, table[4]); | |
690 | (void) symlink("foobar", j); | |
691 | ||
692 | STRV_FOREACH(a, (char**) table) { | |
693 | _cleanup_free_ char *x = NULL, *y = NULL; | |
694 | ||
695 | x = strjoin(z, *a); | |
696 | assert_se(x); | |
697 | ||
698 | if (access(x, F_OK) < 0) { | |
699 | assert_se(errno == ENOENT); | |
700 | continue; | |
701 | } | |
702 | ||
703 | STRV_FOREACH(b, (char**) table) { | |
b81b9d40 | 704 | _cleanup_free_ char *w = NULL; |
4a5d7761 | 705 | |
b81b9d40 YW |
706 | w = strjoin(w, *b); |
707 | assert_se(w); | |
708 | ||
709 | if (access(w, F_OK) < 0) { | |
4a5d7761 LP |
710 | assert_se(errno == ENOENT); |
711 | continue; | |
712 | } | |
713 | ||
b81b9d40 | 714 | assert_se(rename_noreplace(AT_FDCWD, w, AT_FDCWD, y) == -EEXIST); |
4a5d7761 LP |
715 | } |
716 | ||
717 | y = strjoin(z, "/somethingelse"); | |
718 | assert_se(y); | |
719 | ||
720 | assert_se(rename_noreplace(AT_FDCWD, x, AT_FDCWD, y) >= 0); | |
721 | assert_se(rename_noreplace(AT_FDCWD, y, AT_FDCWD, x) >= 0); | |
722 | } | |
723 | } | |
724 | ||
c270684a | 725 | int main(int argc, char *argv[]) { |
27964854 ZJS |
726 | test_setup_logging(LOG_INFO); |
727 | ||
728 | arg_test_dir = argv[1]; | |
729 | ||
c270684a | 730 | test_unlink_noerrno(); |
c270684a | 731 | test_get_files_in_directory(); |
496c486f | 732 | test_readlink_and_make_absolute(); |
34a8f081 | 733 | test_var_tmp(); |
d944dc95 | 734 | test_chase_symlinks(); |
49bfc877 | 735 | test_dot_or_dot_dot(); |
57a4359e | 736 | test_access_fd(); |
9e3fa6e8 | 737 | test_touch_file(); |
43767d9d | 738 | test_unlinkat_deallocate(); |
11b29a96 | 739 | test_fsync_directory_of_file(); |
4a5d7761 | 740 | test_rename_noreplace(); |
c270684a RC |
741 | |
742 | return 0; | |
743 | } |