]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-fs-util.c
Merge pull request #18886 from anitazha/shutdownconsole
[thirdparty/systemd.git] / src / test / test-fs-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c270684a
RC
2
3#include <unistd.h>
4
5#include "alloc-util.h"
10981424 6#include "copy.h"
c270684a 7#include "fd-util.h"
10981424 8#include "fileio.h"
c270684a 9#include "fs-util.h"
1ed34d75 10#include "id128-util.h"
c270684a
RC
11#include "macro.h"
12#include "mkdir.h"
d944dc95 13#include "path-util.h"
06fed305 14#include "random-util.h"
c270684a 15#include "rm-rf.h"
1ed34d75 16#include "stdio-util.h"
c270684a
RC
17#include "string-util.h"
18#include "strv.h"
27964854 19#include "tests.h"
e4de7287 20#include "tmpfile-util.h"
c1447be4 21#include "umask-util.h"
f14f1806 22#include "user-util.h"
c270684a 23#include "util.h"
9590065f 24#include "virt.h"
c270684a 25
27964854
ZJS
26static const char *arg_test_dir = NULL;
27
d944dc95 28static void test_chase_symlinks(void) {
c9825701 29 _cleanup_free_ char *result = NULL;
27964854 30 char *temp;
b12d25a8 31 const char *top, *p, *pslash, *q, *qslash;
1f56e4ce 32 struct stat st;
1ed34d75 33 int r, pfd;
d944dc95 34
27964854
ZJS
35 log_info("/* %s */", __func__);
36
37 temp = strjoina(arg_test_dir ?: "/tmp", "/test-chase.XXXXXX");
d944dc95
LP
38 assert_se(mkdtemp(temp));
39
40 top = strjoina(temp, "/top");
41 assert_se(mkdir(top, 0700) >= 0);
42
43 p = strjoina(top, "/dot");
27964854
ZJS
44 if (symlink(".", p) < 0) {
45 assert_se(IN_SET(errno, EINVAL, ENOSYS, ENOTTY, EPERM));
46 log_tests_skipped_errno(errno, "symlink() not possible");
47 goto cleanup;
48 };
d944dc95
LP
49
50 p = strjoina(top, "/dotdot");
51 assert_se(symlink("..", p) >= 0);
52
53 p = strjoina(top, "/dotdota");
54 assert_se(symlink("../a", p) >= 0);
55
56 p = strjoina(temp, "/a");
57 assert_se(symlink("b", p) >= 0);
58
59 p = strjoina(temp, "/b");
60 assert_se(symlink("/usr", p) >= 0);
61
62 p = strjoina(temp, "/start");
63 assert_se(symlink("top/dot/dotdota", p) >= 0);
64
df878e68
ZJS
65 /* Paths that use symlinks underneath the "root" */
66
a5648b80 67 r = chase_symlinks(p, NULL, 0, &result, NULL);
a9fb0867 68 assert_se(r > 0);
d944dc95 69 assert_se(path_equal(result, "/usr"));
b12d25a8 70 result = mfree(result);
d944dc95 71
b12d25a8 72 pslash = strjoina(p, "/");
a5648b80 73 r = chase_symlinks(pslash, NULL, 0, &result, NULL);
b12d25a8
ZJS
74 assert_se(r > 0);
75 assert_se(path_equal(result, "/usr/"));
d944dc95 76 result = mfree(result);
b12d25a8 77
a5648b80 78 r = chase_symlinks(p, temp, 0, &result, NULL);
d944dc95
LP
79 assert_se(r == -ENOENT);
80
a5648b80 81 r = chase_symlinks(pslash, temp, 0, &result, NULL);
b12d25a8
ZJS
82 assert_se(r == -ENOENT);
83
d944dc95 84 q = strjoina(temp, "/usr");
a9fb0867 85
a5648b80 86 r = chase_symlinks(p, temp, CHASE_NONEXISTENT, &result, NULL);
a9fb0867
LP
87 assert_se(r == 0);
88 assert_se(path_equal(result, q));
b12d25a8 89 result = mfree(result);
a9fb0867 90
b12d25a8 91 qslash = strjoina(q, "/");
d944dc95 92
a5648b80 93 r = chase_symlinks(pslash, temp, CHASE_NONEXISTENT, &result, NULL);
b12d25a8
ZJS
94 assert_se(r == 0);
95 assert_se(path_equal(result, qslash));
f4b85a0f 96 result = mfree(result);
b12d25a8
ZJS
97
98 assert_se(mkdir(q, 0700) >= 0);
99
a5648b80 100 r = chase_symlinks(p, temp, 0, &result, NULL);
a9fb0867 101 assert_se(r > 0);
d944dc95 102 assert_se(path_equal(result, q));
b12d25a8
ZJS
103 result = mfree(result);
104
a5648b80 105 r = chase_symlinks(pslash, temp, 0, &result, NULL);
b12d25a8
ZJS
106 assert_se(r > 0);
107 assert_se(path_equal(result, qslash));
108 result = mfree(result);
d944dc95
LP
109
110 p = strjoina(temp, "/slash");
111 assert_se(symlink("/", p) >= 0);
112
a5648b80 113 r = chase_symlinks(p, NULL, 0, &result, NULL);
a9fb0867 114 assert_se(r > 0);
d944dc95 115 assert_se(path_equal(result, "/"));
d944dc95 116 result = mfree(result);
b12d25a8 117
a5648b80 118 r = chase_symlinks(p, temp, 0, &result, NULL);
a9fb0867 119 assert_se(r > 0);
d944dc95 120 assert_se(path_equal(result, temp));
b12d25a8 121 result = mfree(result);
d944dc95 122
df878e68
ZJS
123 /* Paths that would "escape" outside of the "root" */
124
125 p = strjoina(temp, "/6dots");
126 assert_se(symlink("../../..", p) >= 0);
127
a5648b80 128 r = chase_symlinks(p, temp, 0, &result, NULL);
a9fb0867 129 assert_se(r > 0 && path_equal(result, temp));
b12d25a8 130 result = mfree(result);
df878e68
ZJS
131
132 p = strjoina(temp, "/6dotsusr");
133 assert_se(symlink("../../../usr", p) >= 0);
134
a5648b80 135 r = chase_symlinks(p, temp, 0, &result, NULL);
a9fb0867 136 assert_se(r > 0 && path_equal(result, q));
b12d25a8 137 result = mfree(result);
df878e68
ZJS
138
139 p = strjoina(temp, "/top/8dotsusr");
140 assert_se(symlink("../../../../usr", p) >= 0);
141
a5648b80 142 r = chase_symlinks(p, temp, 0, &result, NULL);
a9fb0867 143 assert_se(r > 0 && path_equal(result, q));
b12d25a8 144 result = mfree(result);
df878e68
ZJS
145
146 /* Paths that contain repeated slashes */
147
d944dc95
LP
148 p = strjoina(temp, "/slashslash");
149 assert_se(symlink("///usr///", p) >= 0);
150
a5648b80 151 r = chase_symlinks(p, NULL, 0, &result, NULL);
a9fb0867 152 assert_se(r > 0);
d944dc95 153 assert_se(path_equal(result, "/usr"));
3c7b4ebf 154 assert_se(streq(result, "/usr")); /* we guarantee that we drop redundant slashes */
d944dc95 155 result = mfree(result);
b12d25a8 156
a5648b80 157 r = chase_symlinks(p, temp, 0, &result, NULL);
a9fb0867 158 assert_se(r > 0);
d944dc95 159 assert_se(path_equal(result, q));
b12d25a8 160 result = mfree(result);
d944dc95 161
2a2fe6ed
DM
162 /* Paths underneath the "root" with different UIDs while using CHASE_SAFE */
163
164 if (geteuid() == 0) {
165 p = strjoina(temp, "/user");
166 assert_se(mkdir(p, 0755) >= 0);
167 assert_se(chown(p, UID_NOBODY, GID_NOBODY) >= 0);
168
169 q = strjoina(temp, "/user/root");
170 assert_se(mkdir(q, 0755) >= 0);
171
172 p = strjoina(q, "/link");
173 assert_se(symlink("/", p) >= 0);
174
175 /* Fail when user-owned directories contain root-owned subdirectories. */
a5648b80 176 r = chase_symlinks(p, temp, CHASE_SAFE, &result, NULL);
2a2fe6ed
DM
177 assert_se(r == -ENOLINK);
178 result = mfree(result);
179
180 /* Allow this when the user-owned directories are all in the "root". */
a5648b80 181 r = chase_symlinks(p, q, CHASE_SAFE, &result, NULL);
2a2fe6ed
DM
182 assert_se(r > 0);
183 result = mfree(result);
184 }
185
df878e68
ZJS
186 /* Paths using . */
187
a5648b80 188 r = chase_symlinks("/etc/./.././", NULL, 0, &result, NULL);
a9fb0867 189 assert_se(r > 0);
d944dc95 190 assert_se(path_equal(result, "/"));
d944dc95 191 result = mfree(result);
b12d25a8 192
a5648b80 193 r = chase_symlinks("/etc/./.././", "/etc", 0, &result, NULL);
a9fb0867 194 assert_se(r > 0 && path_equal(result, "/etc"));
d944dc95 195 result = mfree(result);
b12d25a8 196
a5648b80 197 r = chase_symlinks("/../.././//../../etc", NULL, 0, &result, NULL);
95f35ccc
YW
198 assert_se(r > 0);
199 assert_se(streq(result, "/etc"));
200 result = mfree(result);
201
a5648b80 202 r = chase_symlinks("/../.././//../../test-chase.fsldajfl", NULL, CHASE_NONEXISTENT, &result, NULL);
95f35ccc
YW
203 assert_se(r == 0);
204 assert_se(streq(result, "/test-chase.fsldajfl"));
205 result = mfree(result);
206
a5648b80 207 r = chase_symlinks("/../.././//../../etc", "/", CHASE_PREFIX_ROOT, &result, NULL);
95f35ccc
YW
208 assert_se(r > 0);
209 assert_se(streq(result, "/etc"));
210 result = mfree(result);
211
a5648b80 212 r = chase_symlinks("/../.././//../../test-chase.fsldajfl", "/", CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &result, NULL);
95f35ccc
YW
213 assert_se(r == 0);
214 assert_se(streq(result, "/test-chase.fsldajfl"));
215 result = mfree(result);
216
a5648b80 217 r = chase_symlinks("/etc/machine-id/foo", NULL, 0, &result, NULL);
d944dc95 218 assert_se(r == -ENOTDIR);
b12d25a8 219 result = mfree(result);
d944dc95 220
df878e68
ZJS
221 /* Path that loops back to self */
222
d944dc95
LP
223 p = strjoina(temp, "/recursive-symlink");
224 assert_se(symlink("recursive-symlink", p) >= 0);
a5648b80 225 r = chase_symlinks(p, NULL, 0, &result, NULL);
d944dc95
LP
226 assert_se(r == -ELOOP);
227
a9fb0867
LP
228 /* Path which doesn't exist */
229
230 p = strjoina(temp, "/idontexist");
a5648b80 231 r = chase_symlinks(p, NULL, 0, &result, NULL);
a9fb0867
LP
232 assert_se(r == -ENOENT);
233
a5648b80 234 r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result, NULL);
a9fb0867
LP
235 assert_se(r == 0);
236 assert_se(path_equal(result, p));
237 result = mfree(result);
238
239 p = strjoina(temp, "/idontexist/meneither");
a5648b80 240 r = chase_symlinks(p, NULL, 0, &result, NULL);
a9fb0867
LP
241 assert_se(r == -ENOENT);
242
a5648b80 243 r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result, NULL);
a9fb0867
LP
244 assert_se(r == 0);
245 assert_se(path_equal(result, p));
246 result = mfree(result);
247
248 /* Path which doesn't exist, but contains weird stuff */
249
250 p = strjoina(temp, "/idontexist/..");
a5648b80 251 r = chase_symlinks(p, NULL, 0, &result, NULL);
a9fb0867
LP
252 assert_se(r == -ENOENT);
253
a5648b80 254 r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result, NULL);
a9fb0867
LP
255 assert_se(r == -ENOENT);
256
877777d7
CCW
257 p = strjoina(temp, "/target");
258 q = strjoina(temp, "/top");
259 assert_se(symlink(q, p) >= 0);
260 p = strjoina(temp, "/target/idontexist");
a5648b80 261 r = chase_symlinks(p, NULL, 0, &result, NULL);
877777d7
CCW
262 assert_se(r == -ENOENT);
263
f14f1806
LP
264 if (geteuid() == 0) {
265 p = strjoina(temp, "/priv1");
266 assert_se(mkdir(p, 0755) >= 0);
267
268 q = strjoina(p, "/priv2");
269 assert_se(mkdir(q, 0755) >= 0);
270
a5648b80 271 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL, NULL) >= 0);
f14f1806
LP
272
273 assert_se(chown(q, UID_NOBODY, GID_NOBODY) >= 0);
a5648b80 274 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL, NULL) >= 0);
f14f1806
LP
275
276 assert_se(chown(p, UID_NOBODY, GID_NOBODY) >= 0);
a5648b80 277 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL, NULL) >= 0);
f14f1806
LP
278
279 assert_se(chown(q, 0, 0) >= 0);
a5648b80 280 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL, NULL) == -ENOLINK);
f14f1806
LP
281
282 assert_se(rmdir(q) >= 0);
283 assert_se(symlink("/etc/passwd", q) >= 0);
a5648b80 284 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL, NULL) == -ENOLINK);
f14f1806
LP
285
286 assert_se(chown(p, 0, 0) >= 0);
a5648b80 287 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL, NULL) >= 0);
f14f1806
LP
288 }
289
1ed34d75
LP
290 p = strjoina(temp, "/machine-id-test");
291 assert_se(symlink("/usr/../etc/./machine-id", p) >= 0);
292
a5648b80
ZJS
293 r = chase_symlinks(p, NULL, 0, NULL, &pfd);
294 if (r != -ENOENT) {
1ed34d75
LP
295 _cleanup_close_ int fd = -1;
296 sd_id128_t a, b;
297
298 assert_se(pfd >= 0);
299
f2324783 300 fd = fd_reopen(pfd, O_RDONLY|O_CLOEXEC);
1ed34d75 301 assert_se(fd >= 0);
1ed34d75
LP
302 safe_close(pfd);
303
304 assert_se(id128_read_fd(fd, ID128_PLAIN, &a) >= 0);
305 assert_se(sd_id128_get_machine(&b) >= 0);
306 assert_se(sd_id128_equal(a, b));
307 }
308
1f56e4ce
FB
309 /* Test CHASE_NOFOLLOW */
310
311 p = strjoina(temp, "/target");
312 q = strjoina(temp, "/symlink");
313 assert_se(symlink(p, q) >= 0);
a5648b80
ZJS
314 r = chase_symlinks(q, NULL, CHASE_NOFOLLOW, &result, &pfd);
315 assert_se(r >= 0);
316 assert_se(pfd >= 0);
1f56e4ce
FB
317 assert_se(path_equal(result, q));
318 assert_se(fstat(pfd, &st) >= 0);
319 assert_se(S_ISLNK(st.st_mode));
320 result = mfree(result);
321
322 /* s1 -> s2 -> nonexistent */
323 q = strjoina(temp, "/s1");
324 assert_se(symlink("s2", q) >= 0);
325 p = strjoina(temp, "/s2");
326 assert_se(symlink("nonexistent", p) >= 0);
a5648b80
ZJS
327 r = chase_symlinks(q, NULL, CHASE_NOFOLLOW, &result, &pfd);
328 assert_se(r >= 0);
329 assert_se(pfd >= 0);
1f56e4ce
FB
330 assert_se(path_equal(result, q));
331 assert_se(fstat(pfd, &st) >= 0);
332 assert_se(S_ISLNK(st.st_mode));
333 result = mfree(result);
334
49eb3659
LP
335 /* Test CHASE_ONE */
336
337 p = strjoina(temp, "/start");
a5648b80 338 r = chase_symlinks(p, NULL, CHASE_STEP, &result, NULL);
49eb3659
LP
339 assert_se(r == 0);
340 p = strjoina(temp, "/top/dot/dotdota");
341 assert_se(streq(p, result));
342 result = mfree(result);
343
a5648b80 344 r = chase_symlinks(p, NULL, CHASE_STEP, &result, NULL);
49eb3659
LP
345 assert_se(r == 0);
346 p = strjoina(temp, "/top/./dotdota");
347 assert_se(streq(p, result));
348 result = mfree(result);
349
a5648b80 350 r = chase_symlinks(p, NULL, CHASE_STEP, &result, NULL);
49eb3659
LP
351 assert_se(r == 0);
352 p = strjoina(temp, "/top/../a");
353 assert_se(streq(p, result));
354 result = mfree(result);
355
a5648b80 356 r = chase_symlinks(p, NULL, CHASE_STEP, &result, NULL);
49eb3659
LP
357 assert_se(r == 0);
358 p = strjoina(temp, "/a");
359 assert_se(streq(p, result));
360 result = mfree(result);
361
a5648b80 362 r = chase_symlinks(p, NULL, CHASE_STEP, &result, NULL);
49eb3659
LP
363 assert_se(r == 0);
364 p = strjoina(temp, "/b");
365 assert_se(streq(p, result));
366 result = mfree(result);
367
a5648b80 368 r = chase_symlinks(p, NULL, CHASE_STEP, &result, NULL);
49eb3659
LP
369 assert_se(r == 0);
370 assert_se(streq("/usr", result));
371 result = mfree(result);
372
a5648b80 373 r = chase_symlinks("/usr", NULL, CHASE_STEP, &result, NULL);
49eb3659
LP
374 assert_se(r > 0);
375 assert_se(streq("/usr", result));
376 result = mfree(result);
377
6efb1257
LP
378 /* Make sure that symlinks in the "root" path are not resolved, but those below are */
379 p = strjoina("/etc/..", temp, "/self");
380 assert_se(symlink(".", p) >= 0);
381 q = strjoina(p, "/top/dot/dotdota");
382 r = chase_symlinks(q, p, 0, &result, NULL);
383 assert_se(r > 0);
384 assert_se(path_equal(path_startswith(result, p), "usr"));
385 result = mfree(result);
386
27964854 387 cleanup:
d944dc95
LP
388 assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
389}
390
c270684a 391static void test_unlink_noerrno(void) {
27964854 392 char *name;
c270684a
RC
393 int fd;
394
27964854
ZJS
395 log_info("/* %s */", __func__);
396
397 name = strjoina(arg_test_dir ?: "/tmp", "/test-close_nointr.XXXXXX");
646853bd 398 fd = mkostemp_safe(name);
c270684a
RC
399 assert_se(fd >= 0);
400 assert_se(close_nointr(fd) >= 0);
401
402 {
403 PROTECT_ERRNO;
840f606d 404 errno = 42;
c270684a 405 assert_se(unlink_noerrno(name) >= 0);
840f606d 406 assert_se(errno == 42);
c270684a 407 assert_se(unlink_noerrno(name) < 0);
840f606d 408 assert_se(errno == 42);
c270684a
RC
409 }
410}
411
412static void test_readlink_and_make_absolute(void) {
27964854
ZJS
413 const char *tempdir, *name, *name2, *name_alias;
414 _cleanup_free_ char *r1 = NULL, *r2 = NULL, *pwd = NULL;
415
416 log_info("/* %s */", __func__);
417
418 tempdir = strjoina(arg_test_dir ?: "/tmp", "/test-readlink_and_make_absolute");
419 name = strjoina(tempdir, "/original");
420 name2 = "test-readlink_and_make_absolute/original";
421 name_alias = strjoina(arg_test_dir ?: "/tmp", "/test-readlink_and_make_absolute-alias");
c270684a 422
37c1d5e9 423 assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid(), MKDIR_WARN_MODE) >= 0);
c270684a
RC
424 assert_se(touch(name) >= 0);
425
27964854
ZJS
426 if (symlink(name, name_alias) < 0) {
427 assert_se(IN_SET(errno, EINVAL, ENOSYS, ENOTTY, EPERM));
428 log_tests_skipped_errno(errno, "symlink() not possible");
429 } else {
430 assert_se(readlink_and_make_absolute(name_alias, &r1) >= 0);
431 assert_se(streq(r1, name));
432 assert_se(unlink(name_alias) >= 0);
c270684a 433
27964854 434 assert_se(safe_getcwd(&pwd) >= 0);
cd76d4c2 435
27964854
ZJS
436 assert_se(chdir(tempdir) >= 0);
437 assert_se(symlink(name2, name_alias) >= 0);
438 assert_se(readlink_and_make_absolute(name_alias, &r2) >= 0);
439 assert_se(streq(r2, name));
440 assert_se(unlink(name_alias) >= 0);
c270684a 441
27964854
ZJS
442 assert_se(chdir(pwd) >= 0);
443 }
cd76d4c2 444
c270684a
RC
445 assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
446}
447
448static void test_get_files_in_directory(void) {
449 _cleanup_strv_free_ char **l = NULL, **t = NULL;
450
27964854 451 assert_se(get_files_in_directory(arg_test_dir ?: "/tmp", &l) >= 0);
c270684a
RC
452 assert_se(get_files_in_directory(".", &t) >= 0);
453 assert_se(get_files_in_directory(".", NULL) >= 0);
454}
455
34a8f081 456static void test_var_tmp(void) {
4245eb50 457 _cleanup_free_ char *tmpdir_backup = NULL, *temp_backup = NULL, *tmp_backup = NULL;
992e8f22 458 const char *tmp_dir = NULL, *t;
34a8f081 459
27964854
ZJS
460 log_info("/* %s */", __func__);
461
992e8f22
LP
462 t = getenv("TMPDIR");
463 if (t) {
464 tmpdir_backup = strdup(t);
465 assert_se(tmpdir_backup);
466 }
34a8f081 467
4245eb50
MAP
468 t = getenv("TEMP");
469 if (t) {
470 temp_backup = strdup(t);
471 assert_se(temp_backup);
472 }
473
474 t = getenv("TMP");
475 if (t) {
476 tmp_backup = strdup(t);
477 assert_se(tmp_backup);
478 }
479
85e55d14
YW
480 assert_se(unsetenv("TMPDIR") >= 0);
481 assert_se(unsetenv("TEMP") >= 0);
482 assert_se(unsetenv("TMP") >= 0);
34a8f081 483
992e8f22
LP
484 assert_se(var_tmp_dir(&tmp_dir) >= 0);
485 assert_se(streq(tmp_dir, "/var/tmp"));
34a8f081 486
992e8f22
LP
487 assert_se(setenv("TMPDIR", "/tmp", true) >= 0);
488 assert_se(streq(getenv("TMPDIR"), "/tmp"));
34a8f081 489
992e8f22
LP
490 assert_se(var_tmp_dir(&tmp_dir) >= 0);
491 assert_se(streq(tmp_dir, "/tmp"));
34a8f081 492
992e8f22
LP
493 assert_se(setenv("TMPDIR", "/88_does_not_exist_88", true) >= 0);
494 assert_se(streq(getenv("TMPDIR"), "/88_does_not_exist_88"));
34a8f081 495
992e8f22
LP
496 assert_se(var_tmp_dir(&tmp_dir) >= 0);
497 assert_se(streq(tmp_dir, "/var/tmp"));
34a8f081 498
992e8f22
LP
499 if (tmpdir_backup) {
500 assert_se(setenv("TMPDIR", tmpdir_backup, true) >= 0);
501 assert_se(streq(getenv("TMPDIR"), tmpdir_backup));
34a8f081 502 }
4245eb50
MAP
503
504 if (temp_backup) {
505 assert_se(setenv("TEMP", temp_backup, true) >= 0);
506 assert_se(streq(getenv("TEMP"), temp_backup));
507 }
508
509 if (tmp_backup) {
510 assert_se(setenv("TMP", tmp_backup, true) >= 0);
511 assert_se(streq(getenv("TMP"), tmp_backup));
512 }
34a8f081
OW
513}
514
49bfc877 515static void test_dot_or_dot_dot(void) {
27964854
ZJS
516 log_info("/* %s */", __func__);
517
49bfc877
LP
518 assert_se(!dot_or_dot_dot(NULL));
519 assert_se(!dot_or_dot_dot(""));
520 assert_se(!dot_or_dot_dot("xxx"));
521 assert_se(dot_or_dot_dot("."));
522 assert_se(dot_or_dot_dot(".."));
523 assert_se(!dot_or_dot_dot(".foo"));
524 assert_se(!dot_or_dot_dot("..foo"));
525}
526
57a4359e
LP
527static void test_access_fd(void) {
528 _cleanup_(rmdir_and_freep) char *p = NULL;
529 _cleanup_close_ int fd = -1;
27964854 530 const char *a;
57a4359e 531
27964854
ZJS
532 log_info("/* %s */", __func__);
533
534 a = strjoina(arg_test_dir ?: "/tmp", "/access-fd.XXXXXX");
535 assert_se(mkdtemp_malloc(a, &p) >= 0);
57a4359e
LP
536
537 fd = open(p, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
538 assert_se(fd >= 0);
539
540 assert_se(access_fd(fd, R_OK) >= 0);
541 assert_se(access_fd(fd, F_OK) >= 0);
542 assert_se(access_fd(fd, W_OK) >= 0);
543
544 assert_se(fchmod(fd, 0000) >= 0);
545
546 assert_se(access_fd(fd, F_OK) >= 0);
547
548 if (geteuid() == 0) {
549 assert_se(access_fd(fd, R_OK) >= 0);
550 assert_se(access_fd(fd, W_OK) >= 0);
551 } else {
552 assert_se(access_fd(fd, R_OK) == -EACCES);
553 assert_se(access_fd(fd, W_OK) == -EACCES);
554 }
555}
556
9e3fa6e8
LP
557static void test_touch_file(void) {
558 uid_t test_uid, test_gid;
559 _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
560 struct stat st;
561 const char *a;
562 usec_t test_mtime;
9590065f 563 int r;
9e3fa6e8 564
27964854
ZJS
565 log_info("/* %s */", __func__);
566
9e3fa6e8
LP
567 test_uid = geteuid() == 0 ? 65534 : getuid();
568 test_gid = geteuid() == 0 ? 65534 : getgid();
569
570 test_mtime = usec_sub_unsigned(now(CLOCK_REALTIME), USEC_PER_WEEK);
571
27964854
ZJS
572 a = strjoina(arg_test_dir ?: "/dev/shm", "/touch-file-XXXXXX");
573 assert_se(mkdtemp_malloc(a, &p) >= 0);
9e3fa6e8
LP
574
575 a = strjoina(p, "/regular");
27964854
ZJS
576 r = touch_file(a, false, test_mtime, test_uid, test_gid, 0640);
577 if (r < 0) {
578 assert_se(IN_SET(r, -EINVAL, -ENOSYS, -ENOTTY, -EPERM));
579 log_tests_skipped_errno(errno, "touch_file() not possible");
580 return;
581 }
582
9e3fa6e8
LP
583 assert_se(lstat(a, &st) >= 0);
584 assert_se(st.st_uid == test_uid);
585 assert_se(st.st_gid == test_gid);
586 assert_se(S_ISREG(st.st_mode));
587 assert_se((st.st_mode & 0777) == 0640);
588 assert_se(timespec_load(&st.st_mtim) == test_mtime);
589
590 a = strjoina(p, "/dir");
591 assert_se(mkdir(a, 0775) >= 0);
592 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
593 assert_se(lstat(a, &st) >= 0);
594 assert_se(st.st_uid == test_uid);
595 assert_se(st.st_gid == test_gid);
596 assert_se(S_ISDIR(st.st_mode));
597 assert_se((st.st_mode & 0777) == 0640);
598 assert_se(timespec_load(&st.st_mtim) == test_mtime);
599
600 a = strjoina(p, "/fifo");
601 assert_se(mkfifo(a, 0775) >= 0);
602 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
603 assert_se(lstat(a, &st) >= 0);
604 assert_se(st.st_uid == test_uid);
605 assert_se(st.st_gid == test_gid);
606 assert_se(S_ISFIFO(st.st_mode));
607 assert_se((st.st_mode & 0777) == 0640);
608 assert_se(timespec_load(&st.st_mtim) == test_mtime);
609
610 a = strjoina(p, "/sock");
611 assert_se(mknod(a, 0775 | S_IFSOCK, 0) >= 0);
612 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
613 assert_se(lstat(a, &st) >= 0);
614 assert_se(st.st_uid == test_uid);
615 assert_se(st.st_gid == test_gid);
616 assert_se(S_ISSOCK(st.st_mode));
617 assert_se((st.st_mode & 0777) == 0640);
618 assert_se(timespec_load(&st.st_mtim) == test_mtime);
619
620 if (geteuid() == 0) {
5b5ce629
LP
621 a = strjoina(p, "/bdev");
622 r = mknod(a, 0775 | S_IFBLK, makedev(0, 0));
9590065f
YW
623 if (r < 0 && errno == EPERM && detect_container() > 0) {
624 log_notice("Running in unprivileged container? Skipping remaining tests in %s", __func__);
625 return;
626 }
627 assert_se(r >= 0);
9e3fa6e8
LP
628 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
629 assert_se(lstat(a, &st) >= 0);
630 assert_se(st.st_uid == test_uid);
631 assert_se(st.st_gid == test_gid);
5b5ce629 632 assert_se(S_ISBLK(st.st_mode));
9e3fa6e8
LP
633 assert_se((st.st_mode & 0777) == 0640);
634 assert_se(timespec_load(&st.st_mtim) == test_mtime);
635
5b5ce629
LP
636 a = strjoina(p, "/cdev");
637 assert_se(mknod(a, 0775 | S_IFCHR, makedev(0, 0)) >= 0);
9e3fa6e8
LP
638 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
639 assert_se(lstat(a, &st) >= 0);
640 assert_se(st.st_uid == test_uid);
641 assert_se(st.st_gid == test_gid);
5b5ce629 642 assert_se(S_ISCHR(st.st_mode));
9e3fa6e8
LP
643 assert_se((st.st_mode & 0777) == 0640);
644 assert_se(timespec_load(&st.st_mtim) == test_mtime);
645 }
646
647 a = strjoina(p, "/lnk");
648 assert_se(symlink("target", a) >= 0);
649 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
650 assert_se(lstat(a, &st) >= 0);
651 assert_se(st.st_uid == test_uid);
652 assert_se(st.st_gid == test_gid);
653 assert_se(S_ISLNK(st.st_mode));
9e3fa6e8
LP
654 assert_se(timespec_load(&st.st_mtim) == test_mtime);
655}
656
43767d9d
LP
657static void test_unlinkat_deallocate(void) {
658 _cleanup_free_ char *p = NULL;
659 _cleanup_close_ int fd = -1;
660 struct stat st;
661
27964854
ZJS
662 log_info("/* %s */", __func__);
663
664 assert_se(tempfn_random_child(arg_test_dir, "unlink-deallocation", &p) >= 0);
43767d9d
LP
665
666 fd = open(p, O_WRONLY|O_CLOEXEC|O_CREAT|O_EXCL, 0600);
667 assert_se(fd >= 0);
668
669 assert_se(write(fd, "hallo\n", 6) == 6);
670
671 assert_se(fstat(fd, &st) >= 0);
672 assert_se(st.st_size == 6);
673 assert_se(st.st_blocks > 0);
674 assert_se(st.st_nlink == 1);
675
053e0626 676 assert_se(unlinkat_deallocate(AT_FDCWD, p, UNLINK_ERASE) >= 0);
43767d9d
LP
677
678 assert_se(fstat(fd, &st) >= 0);
27964854
ZJS
679 assert_se(IN_SET(st.st_size, 0, 6)); /* depending on whether hole punching worked the size will be 6
680 (it worked) or 0 (we had to resort to truncation) */
43767d9d
LP
681 assert_se(st.st_blocks == 0);
682 assert_se(st.st_nlink == 0);
683}
684
11b29a96
LP
685static void test_fsync_directory_of_file(void) {
686 _cleanup_close_ int fd = -1;
687
27964854
ZJS
688 log_info("/* %s */", __func__);
689
690 fd = open_tmpfile_unlinkable(arg_test_dir, O_RDWR);
11b29a96
LP
691 assert_se(fd >= 0);
692
693 assert_se(fsync_directory_of_file(fd) >= 0);
694}
695
4a5d7761 696static void test_rename_noreplace(void) {
7158b4b3 697 static const char* const table[] = {
4a5d7761
LP
698 "/reg",
699 "/dir",
700 "/fifo",
701 "/socket",
702 "/symlink",
703 NULL
704 };
705
706 _cleanup_(rm_rf_physical_and_freep) char *z = NULL;
27964854 707 const char *j = NULL;
4a5d7761
LP
708 char **a, **b;
709
27964854
ZJS
710 log_info("/* %s */", __func__);
711
712 if (arg_test_dir)
713 j = strjoina(arg_test_dir, "/testXXXXXX");
7158b4b3 714 assert_se(mkdtemp_malloc(j, &z) >= 0);
4a5d7761
LP
715
716 j = strjoina(z, table[0]);
717 assert_se(touch(j) >= 0);
718
719 j = strjoina(z, table[1]);
720 assert_se(mkdir(j, 0777) >= 0);
721
722 j = strjoina(z, table[2]);
723 (void) mkfifo(j, 0777);
724
725 j = strjoina(z, table[3]);
726 (void) mknod(j, S_IFSOCK | 0777, 0);
727
728 j = strjoina(z, table[4]);
729 (void) symlink("foobar", j);
730
731 STRV_FOREACH(a, (char**) table) {
732 _cleanup_free_ char *x = NULL, *y = NULL;
733
734 x = strjoin(z, *a);
735 assert_se(x);
736
737 if (access(x, F_OK) < 0) {
738 assert_se(errno == ENOENT);
739 continue;
740 }
741
742 STRV_FOREACH(b, (char**) table) {
b81b9d40 743 _cleanup_free_ char *w = NULL;
4a5d7761 744
bcb1eadc 745 w = strjoin(z, *b);
b81b9d40
YW
746 assert_se(w);
747
748 if (access(w, F_OK) < 0) {
4a5d7761
LP
749 assert_se(errno == ENOENT);
750 continue;
751 }
752
bcb1eadc 753 assert_se(rename_noreplace(AT_FDCWD, x, AT_FDCWD, w) == -EEXIST);
4a5d7761
LP
754 }
755
756 y = strjoin(z, "/somethingelse");
757 assert_se(y);
758
759 assert_se(rename_noreplace(AT_FDCWD, x, AT_FDCWD, y) >= 0);
760 assert_se(rename_noreplace(AT_FDCWD, y, AT_FDCWD, x) >= 0);
761 }
762}
763
c1447be4
LP
764static void test_chmod_and_chown(void) {
765 _cleanup_(rm_rf_physical_and_freep) char *d = NULL;
766 _unused_ _cleanup_umask_ mode_t u = umask(0000);
767 struct stat st;
768 const char *p;
769
770 if (geteuid() != 0)
771 return;
772
773 log_info("/* %s */", __func__);
774
775 assert_se(mkdtemp_malloc(NULL, &d) >= 0);
776
777 p = strjoina(d, "/reg");
778 assert_se(mknod(p, S_IFREG | 0123, 0) >= 0);
779
780 assert_se(chmod_and_chown(p, S_IFREG | 0321, 1, 2) >= 0);
781 assert_se(chmod_and_chown(p, S_IFDIR | 0555, 3, 4) == -EINVAL);
782
783 assert_se(lstat(p, &st) >= 0);
784 assert_se(S_ISREG(st.st_mode));
785 assert_se((st.st_mode & 07777) == 0321);
786
787 p = strjoina(d, "/dir");
788 assert_se(mkdir(p, 0123) >= 0);
789
790 assert_se(chmod_and_chown(p, S_IFDIR | 0321, 1, 2) >= 0);
791 assert_se(chmod_and_chown(p, S_IFREG | 0555, 3, 4) == -EINVAL);
792
793 assert_se(lstat(p, &st) >= 0);
794 assert_se(S_ISDIR(st.st_mode));
795 assert_se((st.st_mode & 07777) == 0321);
796
797 p = strjoina(d, "/lnk");
798 assert_se(symlink("idontexist", p) >= 0);
799
800 assert_se(chmod_and_chown(p, S_IFLNK | 0321, 1, 2) >= 0);
801 assert_se(chmod_and_chown(p, S_IFREG | 0555, 3, 4) == -EINVAL);
802 assert_se(chmod_and_chown(p, S_IFDIR | 0555, 3, 4) == -EINVAL);
803
804 assert_se(lstat(p, &st) >= 0);
805 assert_se(S_ISLNK(st.st_mode));
806}
807
ed9c0851
LP
808static void test_path_is_encrypted_one(const char *p, int expect) {
809 int r;
810
811 r = path_is_encrypted(p);
209650b7
TM
812 if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r)) /* This might fail, if btrfs is used and we run in a
813 * container. In that case we cannot resolve the device node paths that
814 * BTRFS_IOC_DEV_INFO returns, because the device nodes are unlikely to exist in
815 * the container. But if we can't stat() them we cannot determine the dev_t of
816 * them, and thus cannot figure out if they are enrypted. Hence let's just ignore
817 * ENOENT here. Also skip the test if we lack privileges. */
f8838c6c 818 return;
ed9c0851
LP
819 assert_se(r >= 0);
820
b56a877e 821 log_info("%s encrypted: %s", p, yes_no(r));
ed9c0851
LP
822
823 assert_se(expect < 0 || ((r > 0) == (expect > 0)));
824}
825
826static void test_path_is_encrypted(void) {
933ab819
ZJS
827 int booted = sd_booted(); /* If this is run in build environments such as koji, /dev might be a
828 * reguar fs. Don't assume too much if not running under systemd. */
829
830 log_info("/* %s (sd_booted=%d)*/", __func__, booted);
ed9c0851
LP
831
832 test_path_is_encrypted_one("/home", -1);
833 test_path_is_encrypted_one("/var", -1);
834 test_path_is_encrypted_one("/", -1);
835 test_path_is_encrypted_one("/proc", false);
836 test_path_is_encrypted_one("/sys", false);
933ab819 837 test_path_is_encrypted_one("/dev", booted > 0 ? false : -1);
ed9c0851
LP
838}
839
06fed305
LP
840static void create_binary_file(const char *p, const void *data, size_t l) {
841 _cleanup_close_ int fd = -1;
842
843 fd = open(p, O_CREAT|O_WRONLY|O_EXCL|O_CLOEXEC, 0600);
844 assert_se(fd >= 0);
845 assert_se(write(fd, data, l) == (ssize_t) l);
846}
847
10981424
LP
848static void test_conservative_rename(void) {
849 _cleanup_(unlink_and_freep) char *p = NULL;
850 _cleanup_free_ char *q = NULL;
06fed305
LP
851 size_t l = 16*1024 + random_u64() % (32 * 1024); /* some randomly sized buffer 16k…48k */
852 uint8_t buffer[l+1];
853
854 random_bytes(buffer, l);
10981424
LP
855
856 assert_se(tempfn_random_child(NULL, NULL, &p) >= 0);
06fed305 857 create_binary_file(p, buffer, l);
10981424
LP
858
859 assert_se(tempfn_random_child(NULL, NULL, &q) >= 0);
860
861 /* Check that the hardlinked "copy" is detected */
862 assert_se(link(p, q) >= 0);
10195179 863 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
10981424
LP
864 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
865
866 /* Check that a manual copy is detected */
f5fbe71d 867 assert_se(copy_file(p, q, 0, MODE_INVALID, 0, 0, COPY_REFLINK) >= 0);
10195179 868 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
10981424
LP
869 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
870
871 /* Check that a manual new writeout is also detected */
06fed305 872 create_binary_file(q, buffer, l);
10195179 873 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
10981424
LP
874 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
875
876 /* Check that a minimally changed version is detected */
06fed305
LP
877 buffer[47] = ~buffer[47];
878 create_binary_file(q, buffer, l);
10195179 879 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
10981424
LP
880 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
881
882 /* Check that this really is new updated version */
06fed305 883 create_binary_file(q, buffer, l);
10195179 884 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
10981424
LP
885 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
886
887 /* Make sure we detect extended files */
06fed305
LP
888 buffer[l++] = 47;
889 create_binary_file(q, buffer, l);
10195179 890 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
10981424
LP
891 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
892
893 /* Make sure we detect truncated files */
06fed305
LP
894 l--;
895 create_binary_file(q, buffer, l);
10195179 896 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
10981424
LP
897 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
898}
899
c270684a 900int main(int argc, char *argv[]) {
27964854
ZJS
901 test_setup_logging(LOG_INFO);
902
903 arg_test_dir = argv[1];
904
f2031940 905 test_chase_symlinks();
c270684a 906 test_unlink_noerrno();
496c486f 907 test_readlink_and_make_absolute();
f2031940 908 test_get_files_in_directory();
34a8f081 909 test_var_tmp();
49bfc877 910 test_dot_or_dot_dot();
57a4359e 911 test_access_fd();
9e3fa6e8 912 test_touch_file();
43767d9d 913 test_unlinkat_deallocate();
11b29a96 914 test_fsync_directory_of_file();
4a5d7761 915 test_rename_noreplace();
c1447be4 916 test_chmod_and_chown();
ed9c0851 917 test_path_is_encrypted();
10981424 918 test_conservative_rename();
c270684a
RC
919
920 return 0;
921}