| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/fsverity.h> |
| 4 |
#include <sys/file.h> |
| 5 |
#include <sys/ioctl.h> |
| 6 |
#include <sys/xattr.h> |
| 7 |
#include <unistd.h> |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "argv-util.h" |
| 11 |
#include "chase.h" |
| 12 |
#include "copy.h" |
| 13 |
#include "errno-util.h" |
| 14 |
#include "fd-util.h" |
| 15 |
#include "fileio.h" |
| 16 |
#include "fs-util.h" |
| 17 |
#include "hashmap.h" |
| 18 |
#include "hexdecoct.h" |
| 19 |
#include "io-util.h" |
| 20 |
#include "log.h" |
| 21 |
#include "mkdir.h" |
| 22 |
#include "path-util.h" |
| 23 |
#include "rm-rf.h" |
| 24 |
#include "stat-util.h" |
| 25 |
#include "string-util.h" |
| 26 |
#include "strv.h" |
| 27 |
#include "tests.h" |
| 28 |
#include "tmpfile-util.h" |
| 29 |
#include "xattr-util.h" |
| 30 |
|
| 31 |
TEST(copy_file) { |
| 32 |
_cleanup_free_ char *buf = NULL; |
| 33 |
_cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-copy_file.XXXXXX"; |
| 34 |
_cleanup_(unlink_tempfilep) char fn_copy[] = "/tmp/test-copy_file.XXXXXX"; |
| 35 |
size_t sz = 0; |
| 36 |
int fd; |
| 37 |
|
| 38 |
fd = mkostemp_safe(fn); |
| 39 |
assert_se(fd >= 0); |
| 40 |
close(fd); |
| 41 |
|
| 42 |
fd = mkostemp_safe(fn_copy); |
| 43 |
assert_se(fd >= 0); |
| 44 |
close(fd); |
| 45 |
|
| 46 |
assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0); |
| 47 |
|
| 48 |
assert_se(copy_file(fn, fn_copy, 0, 0644, COPY_REFLINK) == 0); |
| 49 |
|
| 50 |
assert_se(read_full_file(fn_copy, &buf, &sz) == 0); |
| 51 |
ASSERT_STREQ(buf, "foo bar bar bar foo\n"); |
| 52 |
assert_se(sz == 20); |
| 53 |
} |
| 54 |
|
| 55 |
static bool read_file_at_and_streq(int dir_fd, const char *path, const char *expected) { |
| 56 |
_cleanup_free_ char *buf = NULL; |
| 57 |
|
| 58 |
assert_se(read_full_file_at(dir_fd, path, &buf, NULL) == 0); |
| 59 |
return streq(buf, expected); |
| 60 |
} |
| 61 |
|
| 62 |
TEST(copy_tree_replace_file) { |
| 63 |
_cleanup_free_ char *src = NULL, *dst = NULL; |
| 64 |
|
| 65 |
assert_se(tempfn_random("/tmp/test-copy_file.XXXXXX", NULL, &src) >= 0); |
| 66 |
assert_se(tempfn_random("/tmp/test-copy_file.XXXXXX", NULL, &dst) >= 0); |
| 67 |
|
| 68 |
assert_se(write_string_file(src, "bar bar", WRITE_STRING_FILE_CREATE) == 0); |
| 69 |
assert_se(write_string_file(dst, "foo foo foo", WRITE_STRING_FILE_CREATE) == 0); |
| 70 |
|
| 71 |
/* The file exists- now overwrite original contents, and test the COPY_REPLACE flag. */ |
| 72 |
|
| 73 |
assert_se(copy_tree(src, dst, UID_INVALID, GID_INVALID, COPY_REFLINK, NULL, NULL) == -EEXIST); |
| 74 |
|
| 75 |
assert_se(read_file_at_and_streq(AT_FDCWD, dst, "foo foo foo\n")); |
| 76 |
|
| 77 |
assert_se(copy_tree(src, dst, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE, NULL, NULL) == 0); |
| 78 |
|
| 79 |
assert_se(read_file_at_and_streq(AT_FDCWD, dst, "bar bar\n")); |
| 80 |
} |
| 81 |
|
| 82 |
TEST(copy_tree_replace_dirs) { |
| 83 |
_cleanup_(rm_rf_physical_and_freep) char *srcp = NULL, *dstp = NULL; |
| 84 |
_cleanup_close_ int src = -EBADF, dst = -EBADF; |
| 85 |
|
| 86 |
/* Create the random source/destination directories */ |
| 87 |
assert_se((src = mkdtemp_open(NULL, 0, &srcp)) >= 0); |
| 88 |
assert_se((dst = mkdtemp_open(NULL, 0, &dstp)) >= 0); |
| 89 |
|
| 90 |
/* Populate some data to differentiate the files. */ |
| 91 |
assert_se(write_string_file_at(src, "foo", "src file 1", WRITE_STRING_FILE_CREATE) >= 0); |
| 92 |
assert_se(write_string_file_at(src, "bar", "src file 2", WRITE_STRING_FILE_CREATE) == 0); |
| 93 |
|
| 94 |
assert_se(write_string_file_at(dst, "foo", "dest file 1", WRITE_STRING_FILE_CREATE) == 0); |
| 95 |
assert_se(write_string_file_at(dst, "bar", "dest file 2", WRITE_STRING_FILE_CREATE) == 0); |
| 96 |
|
| 97 |
/* Copying without COPY_REPLACE should fail because the destination file already exists. */ |
| 98 |
assert_se(copy_tree_at(src, ".", dst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK, NULL, NULL) == -EEXIST); |
| 99 |
|
| 100 |
assert_se(read_file_at_and_streq(src, "foo", "src file 1\n")); |
| 101 |
assert_se(read_file_at_and_streq(src, "bar", "src file 2\n")); |
| 102 |
assert_se(read_file_at_and_streq(dst, "foo", "dest file 1\n")); |
| 103 |
assert_se(read_file_at_and_streq(dst, "bar", "dest file 2\n")); |
| 104 |
|
| 105 |
assert_se(copy_tree_at(src, ".", dst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE|COPY_MERGE, NULL, NULL) == 0); |
| 106 |
|
| 107 |
assert_se(read_file_at_and_streq(src, "foo", "src file 1\n")); |
| 108 |
assert_se(read_file_at_and_streq(src, "bar", "src file 2\n")); |
| 109 |
assert_se(read_file_at_and_streq(dst, "foo", "src file 1\n")); |
| 110 |
assert_se(read_file_at_and_streq(dst, "bar", "src file 2\n")); |
| 111 |
} |
| 112 |
|
| 113 |
TEST(copy_file_fd) { |
| 114 |
_cleanup_(unlink_tempfilep) char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; |
| 115 |
_cleanup_(unlink_tempfilep) char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; |
| 116 |
_cleanup_close_ int in_fd = -EBADF, out_fd = -EBADF; |
| 117 |
const char *text = "boohoo\nfoo\n\tbar\n"; |
| 118 |
char buf[64] = {}; |
| 119 |
|
| 120 |
in_fd = mkostemp_safe(in_fn); |
| 121 |
assert_se(in_fd >= 0); |
| 122 |
out_fd = mkostemp_safe(out_fn); |
| 123 |
assert_se(out_fd >= 0); |
| 124 |
|
| 125 |
assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0); |
| 126 |
assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, COPY_REFLINK) < 0); |
| 127 |
assert_se(copy_file_fd(in_fn, out_fd, COPY_REFLINK) >= 0); |
| 128 |
assert_se(lseek(out_fd, 0, SEEK_SET) == 0); |
| 129 |
|
| 130 |
assert_se(read(out_fd, buf, sizeof buf) == (ssize_t) strlen(text)); |
| 131 |
ASSERT_STREQ(buf, text); |
| 132 |
} |
| 133 |
|
| 134 |
TEST(copy_tree) { |
| 135 |
_cleanup_hashmap_free_ Hashmap *denylist = NULL; |
| 136 |
_cleanup_free_ char *cp = NULL; |
| 137 |
char original_dir[] = "/tmp/test-copy_tree/"; |
| 138 |
char copy_dir[] = "/tmp/test-copy_tree-copy/"; |
| 139 |
char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file"); |
| 140 |
char **symlinks = STRV_MAKE("link", "file", |
| 141 |
"link2", "dir1/file"); |
| 142 |
char **hardlinks = STRV_MAKE("hlink", "file", |
| 143 |
"hlink2", "dir1/file"); |
| 144 |
const char *unixsockp, *ignorep; |
| 145 |
struct stat st; |
| 146 |
int xattr_worked = -1; /* xattr support is optional in temporary directories, hence use it if we can, |
| 147 |
* but don't fail if we can't */ |
| 148 |
|
| 149 |
(void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
| 150 |
(void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
| 151 |
|
| 152 |
STRV_FOREACH(p, files) { |
| 153 |
_cleanup_free_ char *f = NULL, *c = NULL; |
| 154 |
int k; |
| 155 |
|
| 156 |
assert_se(f = path_join(original_dir, *p)); |
| 157 |
|
| 158 |
assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755) == 0); |
| 159 |
|
| 160 |
assert_se(base64mem(*p, strlen(*p), &c) >= 0); |
| 161 |
|
| 162 |
k = setxattr(f, "user.testxattr", c, strlen(c), 0); |
| 163 |
assert_se(xattr_worked < 0 || ((k >= 0) == !!xattr_worked)); |
| 164 |
xattr_worked = k >= 0; |
| 165 |
} |
| 166 |
|
| 167 |
STRV_FOREACH_PAIR(ll, p, symlinks) { |
| 168 |
_cleanup_free_ char *f = NULL, *l = NULL; |
| 169 |
|
| 170 |
assert_se(f = path_join(original_dir, *p)); |
| 171 |
assert_se(l = path_join(original_dir, *ll)); |
| 172 |
|
| 173 |
assert_se(mkdir_parents(l, 0755) >= 0); |
| 174 |
assert_se(symlink(f, l) == 0); |
| 175 |
} |
| 176 |
|
| 177 |
STRV_FOREACH_PAIR(ll, p, hardlinks) { |
| 178 |
_cleanup_free_ char *f = NULL, *l = NULL; |
| 179 |
|
| 180 |
assert_se(f = path_join(original_dir, *p)); |
| 181 |
assert_se(l = path_join(original_dir, *ll)); |
| 182 |
|
| 183 |
assert_se(mkdir_parents(l, 0755) >= 0); |
| 184 |
assert_se(link(f, l) == 0); |
| 185 |
} |
| 186 |
|
| 187 |
unixsockp = strjoina(original_dir, "unixsock"); |
| 188 |
assert_se(mknod(unixsockp, S_IFSOCK|0644, 0) >= 0); |
| 189 |
|
| 190 |
ignorep = strjoina(original_dir, "ignore/file"); |
| 191 |
assert_se(write_string_file(ignorep, "ignore", WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755) == 0); |
| 192 |
assert_se(RET_NERRNO(stat(ignorep, &st)) >= 0); |
| 193 |
assert_se(cp = memdup(&st, sizeof(st))); |
| 194 |
assert_se(hashmap_ensure_put(&denylist, &inode_hash_ops, cp, INT_TO_PTR(DENY_INODE)) >= 0); |
| 195 |
TAKE_PTR(cp); |
| 196 |
|
| 197 |
assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_MERGE|COPY_HARDLINKS, denylist, NULL) == 0); |
| 198 |
|
| 199 |
STRV_FOREACH(p, files) { |
| 200 |
_cleanup_free_ char *buf = NULL, *f = NULL, *c = NULL; |
| 201 |
size_t sz; |
| 202 |
int k; |
| 203 |
|
| 204 |
assert_se(f = path_join(copy_dir, *p)); |
| 205 |
|
| 206 |
assert_se(access(f, F_OK) == 0); |
| 207 |
assert_se(read_full_file(f, &buf, &sz) == 0); |
| 208 |
ASSERT_STREQ(buf, "file\n"); |
| 209 |
|
| 210 |
k = lgetxattr_malloc(f, "user.testxattr", &c, /* ret_size= */ NULL); |
| 211 |
assert_se(xattr_worked < 0 || ((k >= 0) == !!xattr_worked)); |
| 212 |
|
| 213 |
if (k >= 0) { |
| 214 |
_cleanup_free_ char *d = NULL; |
| 215 |
|
| 216 |
assert_se(base64mem(*p, strlen(*p), &d) >= 0); |
| 217 |
ASSERT_STREQ(d, c); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
STRV_FOREACH_PAIR(ll, p, symlinks) { |
| 222 |
_cleanup_free_ char *target = NULL, *f = NULL, *l = NULL; |
| 223 |
|
| 224 |
assert_se(f = strjoin(original_dir, *p)); |
| 225 |
assert_se(l = strjoin(copy_dir, *ll)); |
| 226 |
|
| 227 |
assert_se(chase(l, NULL, 0, &target, NULL) == 1); |
| 228 |
assert_se(path_equal(f, target)); |
| 229 |
} |
| 230 |
|
| 231 |
STRV_FOREACH_PAIR(ll, p, hardlinks) { |
| 232 |
_cleanup_free_ char *f = NULL, *l = NULL; |
| 233 |
struct stat a, b; |
| 234 |
|
| 235 |
assert_se(f = strjoin(copy_dir, *p)); |
| 236 |
assert_se(l = strjoin(copy_dir, *ll)); |
| 237 |
|
| 238 |
assert_se(lstat(f, &a) >= 0); |
| 239 |
assert_se(lstat(l, &b) >= 0); |
| 240 |
|
| 241 |
assert_se(a.st_ino == b.st_ino); |
| 242 |
assert_se(a.st_dev == b.st_dev); |
| 243 |
} |
| 244 |
|
| 245 |
unixsockp = strjoina(copy_dir, "unixsock"); |
| 246 |
assert_se(stat(unixsockp, &st) >= 0); |
| 247 |
assert_se(S_ISSOCK(st.st_mode)); |
| 248 |
|
| 249 |
assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK, denylist, NULL) < 0); |
| 250 |
assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK, denylist, NULL) < 0); |
| 251 |
|
| 252 |
ignorep = strjoina(copy_dir, "ignore/file"); |
| 253 |
assert_se(RET_NERRNO(access(ignorep, F_OK)) == -ENOENT); |
| 254 |
|
| 255 |
(void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
| 256 |
(void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
| 257 |
} |
| 258 |
|
| 259 |
TEST(copy_tree_at_symlink) { |
| 260 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 261 |
_cleanup_close_ int tfd = -EBADF, fd = -EBADF; |
| 262 |
_cleanup_free_ char *p = NULL, *q = NULL; |
| 263 |
const char *expect = "hgoehogefoobar"; |
| 264 |
|
| 265 |
tfd = mkdtemp_open(NULL, O_PATH, &t); |
| 266 |
assert_se(tfd >= 0); |
| 267 |
|
| 268 |
assert_se(symlinkat(expect, tfd, "from") >= 0); |
| 269 |
|
| 270 |
assert_se(copy_tree_at(tfd, "from", tfd, "to_1", UID_INVALID, GID_INVALID, 0, NULL, NULL) >= 0); |
| 271 |
assert_se(readlinkat_malloc(tfd, "to_1", &p) >= 0); |
| 272 |
ASSERT_STREQ(p, expect); |
| 273 |
p = mfree(p); |
| 274 |
|
| 275 |
assert_se(q = path_join(t, "from")); |
| 276 |
assert_se(copy_tree_at(AT_FDCWD, q, tfd, "to_2", UID_INVALID, GID_INVALID, 0, NULL, NULL) >= 0); |
| 277 |
assert_se(readlinkat_malloc(tfd, "to_2", &p) >= 0); |
| 278 |
ASSERT_STREQ(p, expect); |
| 279 |
p = mfree(p); |
| 280 |
q = mfree(q); |
| 281 |
|
| 282 |
fd = openat(tfd, "from", O_CLOEXEC | O_PATH | O_NOFOLLOW); |
| 283 |
assert_se(fd >= 0); |
| 284 |
assert_se(copy_tree_at(fd, NULL, tfd, "to_3", UID_INVALID, GID_INVALID, 0, NULL, NULL) >= 0); |
| 285 |
assert_se(readlinkat_malloc(tfd, "to_3", &p) >= 0); |
| 286 |
ASSERT_STREQ(p, expect); |
| 287 |
p = mfree(p); |
| 288 |
|
| 289 |
assert_se(copy_tree_at(fd, "", tfd, "to_4", UID_INVALID, GID_INVALID, 0, NULL, NULL) >= 0); |
| 290 |
assert_se(readlinkat_malloc(tfd, "to_4", &p) >= 0); |
| 291 |
ASSERT_STREQ(p, expect); |
| 292 |
p = mfree(p); |
| 293 |
fd = safe_close(fd); |
| 294 |
} |
| 295 |
|
| 296 |
TEST_RET(copy_bytes) { |
| 297 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 298 |
_cleanup_close_ int infd = -EBADF; |
| 299 |
int r; |
| 300 |
char buf[1024], buf2[1024]; |
| 301 |
|
| 302 |
infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC); |
| 303 |
if (infd < 0) |
| 304 |
infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC); |
| 305 |
if (infd < 0) |
| 306 |
return log_tests_skipped_errno(errno, "Could not open /usr/lib/os-release or /etc/os-release: %m"); |
| 307 |
|
| 308 |
assert_se(pipe2(pipefd, O_CLOEXEC) == 0); |
| 309 |
|
| 310 |
r = copy_bytes(infd, pipefd[1], UINT64_MAX, 0); |
| 311 |
assert_se(r == 0); |
| 312 |
|
| 313 |
ssize_t n = read(pipefd[0], buf, sizeof(buf)); |
| 314 |
assert_se(n >= 0); |
| 315 |
|
| 316 |
assert_se(lseek(infd, 0, SEEK_SET) == 0); |
| 317 |
ssize_t n2 = read(infd, buf2, sizeof(buf2)); |
| 318 |
assert_se(n == n2); |
| 319 |
|
| 320 |
assert_se(strneq(buf, buf2, n)); |
| 321 |
|
| 322 |
/* test copy_bytes with invalid descriptors */ |
| 323 |
r = copy_bytes(pipefd[0], pipefd[0], 1, 0); |
| 324 |
assert_se(r == -EBADF); |
| 325 |
|
| 326 |
r = copy_bytes(pipefd[1], pipefd[1], 1, 0); |
| 327 |
assert_se(r == -EBADF); |
| 328 |
|
| 329 |
r = copy_bytes(pipefd[1], infd, 1, 0); |
| 330 |
assert_se(r == -EBADF); |
| 331 |
|
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
static void test_copy_bytes_regular_file_one(const char *src, bool try_reflink, uint64_t max_bytes) { |
| 336 |
_cleanup_(unlink_tempfilep) char fn2[] = "/tmp/test-copy-file-XXXXXX"; |
| 337 |
_cleanup_(unlink_tempfilep) char fn3[] = "/tmp/test-copy-file-XXXXXX"; |
| 338 |
_cleanup_close_ int fd = -EBADF, fd2 = -EBADF, fd3 = -EBADF; |
| 339 |
int r; |
| 340 |
struct stat buf, buf2, buf3; |
| 341 |
|
| 342 |
log_info("%s try_reflink=%s max_bytes=%" PRIu64, __func__, yes_no(try_reflink), max_bytes); |
| 343 |
|
| 344 |
fd = open(src, O_CLOEXEC | O_PATH); |
| 345 |
assert_se(fd >= 0); |
| 346 |
|
| 347 |
fd2 = mkostemp_safe(fn2); |
| 348 |
assert_se(fd2 >= 0); |
| 349 |
|
| 350 |
fd3 = mkostemp_safe(fn3); |
| 351 |
assert_se(fd3 >= 0); |
| 352 |
|
| 353 |
r = copy_bytes(fd, fd2, max_bytes, try_reflink ? COPY_REFLINK : 0); |
| 354 |
if (max_bytes == UINT64_MAX) |
| 355 |
assert_se(r == 0); |
| 356 |
else |
| 357 |
assert_se(IN_SET(r, 0, 1)); |
| 358 |
|
| 359 |
assert_se(fstat(fd, &buf) == 0); |
| 360 |
assert_se(fstat(fd2, &buf2) == 0); |
| 361 |
assert_se((uint64_t) buf2.st_size == MIN((uint64_t) buf.st_size, max_bytes)); |
| 362 |
|
| 363 |
if (max_bytes < UINT64_MAX) |
| 364 |
/* Make sure the file is now higher than max_bytes */ |
| 365 |
assert_se(ftruncate(fd2, max_bytes + 1) == 0); |
| 366 |
|
| 367 |
r = copy_bytes(fd2, fd3, max_bytes, COPY_SEEK0_SOURCE | (try_reflink ? COPY_REFLINK : 0)); |
| 368 |
if (max_bytes == UINT64_MAX) |
| 369 |
assert_se(r == 0); |
| 370 |
else |
| 371 |
/* We cannot distinguish between the input being exactly max_bytes |
| 372 |
* or longer than max_bytes (without trying to read one more byte, |
| 373 |
* or calling stat, or FION_READ, etc, and we don't want to do any |
| 374 |
* of that). So we expect "truncation" since we know that file we |
| 375 |
* are copying is exactly max_bytes bytes. */ |
| 376 |
assert_se(r == 1); |
| 377 |
|
| 378 |
assert_se(fstat(fd3, &buf3) == 0); |
| 379 |
|
| 380 |
if (max_bytes == UINT64_MAX) |
| 381 |
assert_se(buf3.st_size == buf2.st_size); |
| 382 |
else |
| 383 |
assert_se((uint64_t) buf3.st_size == max_bytes); |
| 384 |
} |
| 385 |
|
| 386 |
TEST(copy_bytes_regular_file) { |
| 387 |
test_copy_bytes_regular_file_one(saved_argv[0], false, UINT64_MAX); |
| 388 |
test_copy_bytes_regular_file_one(saved_argv[0], true, UINT64_MAX); |
| 389 |
test_copy_bytes_regular_file_one(saved_argv[0], false, 1000); /* smaller than copy buffer size */ |
| 390 |
test_copy_bytes_regular_file_one(saved_argv[0], true, 1000); |
| 391 |
test_copy_bytes_regular_file_one(saved_argv[0], false, 32000); /* larger than copy buffer size */ |
| 392 |
test_copy_bytes_regular_file_one(saved_argv[0], true, 32000); |
| 393 |
} |
| 394 |
|
| 395 |
TEST(copy_atomic) { |
| 396 |
_cleanup_(rm_rf_physical_and_freep) char *p = NULL; |
| 397 |
const char *q; |
| 398 |
int r; |
| 399 |
|
| 400 |
assert_se(mkdtemp_malloc(NULL, &p) >= 0); |
| 401 |
|
| 402 |
q = strjoina(p, "/fstab"); |
| 403 |
|
| 404 |
r = copy_file_atomic("/etc/fstab", q, 0644, COPY_REFLINK); |
| 405 |
if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r)) |
| 406 |
return; |
| 407 |
|
| 408 |
assert_se(copy_file_atomic("/etc/fstab", q, 0644, COPY_REFLINK) == -EEXIST); |
| 409 |
|
| 410 |
assert_se(copy_file_atomic("/etc/fstab", q, 0644, COPY_REPLACE) >= 0); |
| 411 |
} |
| 412 |
|
| 413 |
TEST(copy_proc) { |
| 414 |
_cleanup_(rm_rf_physical_and_freep) char *p = NULL; |
| 415 |
_cleanup_free_ char *f = NULL, *a = NULL, *b = NULL; |
| 416 |
|
| 417 |
/* Check if copying data from /proc/ works correctly, i.e. let's see if https://lwn.net/Articles/846403/ is a problem for us */ |
| 418 |
|
| 419 |
assert_se(mkdtemp_malloc(NULL, &p) >= 0); |
| 420 |
assert_se(f = path_join(p, "version")); |
| 421 |
assert_se(copy_file("/proc/version", f, 0, MODE_INVALID, 0) >= 0); |
| 422 |
|
| 423 |
assert_se(read_one_line_file("/proc/version", &a) >= 0); |
| 424 |
assert_se(read_one_line_file(f, &b) >= 0); |
| 425 |
ASSERT_STREQ(a, b); |
| 426 |
assert_se(!isempty(a)); |
| 427 |
} |
| 428 |
|
| 429 |
TEST_RET(copy_holes) { |
| 430 |
_cleanup_(unlink_tempfilep) char fn[] = "/var/tmp/test-copy-hole-fd-XXXXXX"; |
| 431 |
_cleanup_(unlink_tempfilep) char fn_copy[] = "/var/tmp/test-copy-hole-fd-XXXXXX"; |
| 432 |
struct stat stat; |
| 433 |
off_t blksz; |
| 434 |
int r, fd, fd_copy; |
| 435 |
char *buf; |
| 436 |
|
| 437 |
fd = mkostemp_safe(fn); |
| 438 |
assert_se(fd >= 0); |
| 439 |
|
| 440 |
fd_copy = mkostemp_safe(fn_copy); |
| 441 |
assert_se(fd_copy >= 0); |
| 442 |
|
| 443 |
r = RET_NERRNO(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 1)); |
| 444 |
if (ERRNO_IS_NOT_SUPPORTED(r)) |
| 445 |
return log_tests_skipped("Filesystem doesn't support hole punching"); |
| 446 |
assert_se(r >= 0); |
| 447 |
|
| 448 |
ASSERT_OK_ERRNO(fstat(fd, &stat)); |
| 449 |
blksz = stat.st_blksize; |
| 450 |
buf = alloca_safe(blksz); |
| 451 |
memset(buf, 1, blksz); |
| 452 |
|
| 453 |
/* We need to make sure to create hole in multiples of the block size, otherwise filesystems (btrfs) |
| 454 |
* might silently truncate/extend the holes. */ |
| 455 |
|
| 456 |
assert_se(lseek(fd, blksz, SEEK_CUR) >= 0); |
| 457 |
assert_se(write(fd, buf, blksz) >= 0); |
| 458 |
assert_se(lseek(fd, 0, SEEK_END) == 2 * blksz); |
| 459 |
/* Only ftruncate() can create holes at the end of a file. */ |
| 460 |
assert_se(ftruncate(fd, 3 * blksz) >= 0); |
| 461 |
|
| 462 |
assert_se(copy_bytes(fd, fd_copy, UINT64_MAX, COPY_SEEK0_SOURCE|COPY_HOLES) >= 0); |
| 463 |
|
| 464 |
/* Test that the hole starts at the beginning of the file. */ |
| 465 |
assert_se(lseek(fd_copy, 0, SEEK_HOLE) == 0); |
| 466 |
/* Test that the hole has the expected size. */ |
| 467 |
assert_se(lseek(fd_copy, 0, SEEK_DATA) == blksz); |
| 468 |
assert_se(lseek(fd_copy, blksz, SEEK_HOLE) == 2 * blksz); |
| 469 |
assert_se(lseek(fd_copy, 2 * blksz, SEEK_DATA) < 0 && errno == ENXIO); |
| 470 |
|
| 471 |
/* Test that the copied file has the correct size. */ |
| 472 |
ASSERT_OK_ERRNO(fstat(fd_copy, &stat)); |
| 473 |
assert_se(stat.st_size == 3 * blksz); |
| 474 |
|
| 475 |
close(fd); |
| 476 |
close(fd_copy); |
| 477 |
|
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
TEST_RET(copy_holes_with_gaps) { |
| 482 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 483 |
_cleanup_close_ int tfd = -EBADF, fd = -EBADF, fd_copy = -EBADF; |
| 484 |
struct stat st; |
| 485 |
off_t blksz; |
| 486 |
char *buf; |
| 487 |
int r; |
| 488 |
|
| 489 |
assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0); |
| 490 |
assert_se((fd = openat(tfd, "src", O_CREAT | O_RDWR, 0600)) >= 0); |
| 491 |
assert_se((fd_copy = openat(tfd, "dst", O_CREAT | O_WRONLY, 0600)) >= 0); |
| 492 |
|
| 493 |
ASSERT_OK_ERRNO(fstat(fd, &st)); |
| 494 |
blksz = st.st_blksize; |
| 495 |
buf = alloca_safe(blksz); |
| 496 |
memset(buf, 1, blksz); |
| 497 |
|
| 498 |
/* Create a file with: |
| 499 |
* - hole of 1 block |
| 500 |
* - data of 2 block |
| 501 |
* - hole of 2 blocks |
| 502 |
* - data of 1 block |
| 503 |
* |
| 504 |
* Since sparse files are based on blocks and not bytes, we need to make |
| 505 |
* sure that the holes are aligned to the block size. |
| 506 |
*/ |
| 507 |
|
| 508 |
r = RET_NERRNO(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, blksz)); |
| 509 |
if (ERRNO_IS_NOT_SUPPORTED(r)) |
| 510 |
return log_tests_skipped("Filesystem doesn't support hole punching"); |
| 511 |
|
| 512 |
assert_se(lseek(fd, blksz, SEEK_CUR) >= 0); |
| 513 |
assert_se(loop_write(fd, buf, blksz) >= 0); |
| 514 |
assert_se(loop_write(fd, buf, blksz) >= 0); |
| 515 |
assert_se(lseek(fd, 2 * blksz, SEEK_CUR) >= 0); |
| 516 |
assert_se(loop_write(fd, buf, blksz) >= 0); |
| 517 |
assert_se(lseek(fd, 0, SEEK_SET) >= 0); |
| 518 |
assert_se(fsync(fd) >= 0); |
| 519 |
|
| 520 |
/* Copy to the start of the second hole */ |
| 521 |
assert_se(copy_bytes(fd, fd_copy, 3 * blksz, COPY_HOLES) >= 0); |
| 522 |
ASSERT_OK_ERRNO(fstat(fd_copy, &st)); |
| 523 |
assert_se(st.st_size == 3 * blksz); |
| 524 |
|
| 525 |
/* Copy to the middle of the second hole */ |
| 526 |
assert_se(ftruncate(fd_copy, 0) >= 0); |
| 527 |
assert_se(copy_bytes(fd, fd_copy, 4 * blksz, COPY_SEEK0_SOURCE|COPY_SEEK0_TARGET|COPY_HOLES) >= 0); |
| 528 |
ASSERT_OK_ERRNO(fstat(fd_copy, &st)); |
| 529 |
assert_se(st.st_size == 4 * blksz); |
| 530 |
|
| 531 |
/* Copy to the end of the second hole */ |
| 532 |
assert_se(ftruncate(fd_copy, 0) >= 0); |
| 533 |
assert_se(copy_bytes(fd, fd_copy, 5 * blksz, COPY_SEEK0_SOURCE|COPY_SEEK0_TARGET|COPY_HOLES) >= 0); |
| 534 |
ASSERT_OK_ERRNO(fstat(fd_copy, &st)); |
| 535 |
assert_se(st.st_size == 5 * blksz); |
| 536 |
|
| 537 |
/* Copy everything */ |
| 538 |
assert_se(ftruncate(fd_copy, 0) >= 0); |
| 539 |
assert_se(copy_bytes(fd, fd_copy, UINT64_MAX, COPY_SEEK0_SOURCE|COPY_SEEK0_TARGET|COPY_HOLES) >= 0); |
| 540 |
ASSERT_OK_ERRNO(fstat(fd_copy, &st)); |
| 541 |
assert_se(st.st_size == 6 * blksz); |
| 542 |
|
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
TEST(copy_lock) { |
| 547 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 548 |
_cleanup_close_ int tfd = -EBADF, fd = -EBADF; |
| 549 |
|
| 550 |
assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0); |
| 551 |
assert_se(mkdirat(tfd, "abc", 0755) >= 0); |
| 552 |
assert_se(write_string_file_at(tfd, "abc/def", "abc", WRITE_STRING_FILE_CREATE) >= 0); |
| 553 |
|
| 554 |
assert_se((fd = copy_directory_at(tfd, "abc", tfd, "qed", UID_INVALID, GID_INVALID, COPY_LOCK_BSD)) >= 0); |
| 555 |
assert_se(faccessat(tfd, "qed", F_OK, 0) >= 0); |
| 556 |
assert_se(faccessat(tfd, "qed/def", F_OK, 0) >= 0); |
| 557 |
assert_se(xopenat_lock(tfd, "qed", 0, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN); |
| 558 |
fd = safe_close(fd); |
| 559 |
|
| 560 |
assert_se((fd = copy_file_at(tfd, "abc/def", tfd, "poi", 0, 0644, COPY_LOCK_BSD))); |
| 561 |
assert_se(read_file_at_and_streq(tfd, "poi", "abc\n")); |
| 562 |
assert_se(xopenat_lock(tfd, "poi", 0, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN); |
| 563 |
fd = safe_close(fd); |
| 564 |
} |
| 565 |
|
| 566 |
TEST(copy_verify_linked) { |
| 567 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 568 |
_cleanup_close_ int tfd = -EBADF, fd_1 = -EBADF, fd_2 = -EBADF; |
| 569 |
|
| 570 |
tfd = mkdtemp_open(NULL, O_PATH, &t); |
| 571 |
assert_se(tfd >= 0); |
| 572 |
|
| 573 |
assert_se(write_string_file_at(tfd, "hoge", "bar bar", WRITE_STRING_FILE_CREATE) >= 0); |
| 574 |
|
| 575 |
fd_1 = openat(tfd, "hoge", O_CLOEXEC | O_NOCTTY | O_RDONLY); |
| 576 |
assert_se(fd_1 >= 0); |
| 577 |
fd_2 = openat(tfd, "hoge", O_CLOEXEC | O_NOCTTY | O_RDONLY); |
| 578 |
assert_se(fd_2 >= 0); |
| 579 |
assert_se(unlinkat(tfd, "hoge", 0) >= 0); |
| 580 |
|
| 581 |
assert_se(copy_file_at(fd_1, NULL, tfd, "to_1", 0, 0644, 0) >= 0); |
| 582 |
assert_se(read_file_at_and_streq(tfd, "to_1", "bar bar\n")); |
| 583 |
|
| 584 |
assert_se(copy_file_at(fd_2, NULL, tfd, "to_2", O_EXCL, 0644, COPY_VERIFY_LINKED) == -EIDRM); |
| 585 |
assert_se(faccessat(tfd, "to_2", F_OK, AT_SYMLINK_NOFOLLOW) < 0 && errno == ENOENT); |
| 586 |
} |
| 587 |
|
| 588 |
static bool enable_fsverity(int dirfd, const char *name, int algo, const char *salt, size_t salt_size) { |
| 589 |
_cleanup_close_ int fd = -EBADF; |
| 590 |
ASSERT_OK_ERRNO(fd = openat(dirfd, name, O_RDONLY | O_CLOEXEC | O_NOCTTY)); |
| 591 |
|
| 592 |
struct fsverity_enable_arg enable_arg = { |
| 593 |
.version = 1, |
| 594 |
.hash_algorithm = algo, |
| 595 |
.block_size = 4096, |
| 596 |
.salt_size = salt_size, |
| 597 |
.salt_ptr = (uintptr_t) salt |
| 598 |
}; |
| 599 |
|
| 600 |
if (ioctl(fd, FS_IOC_ENABLE_VERITY, &enable_arg) == 0) |
| 601 |
return true; |
| 602 |
|
| 603 |
/* We might get this if we're on a filesystem without support. */ |
| 604 |
ASSERT_TRUE(IN_SET(errno, EOPNOTSUPP, ENOTTY)); |
| 605 |
return false; |
| 606 |
} |
| 607 |
|
| 608 |
static struct fsverity_digest* alloc_fsverity_digest(size_t digest_size) { |
| 609 |
struct fsverity_digest *digest = malloc(sizeof (struct fsverity_digest) + digest_size); |
| 610 |
ASSERT_NOT_NULL(digest); |
| 611 |
digest->digest_size = digest_size; |
| 612 |
return digest; |
| 613 |
} |
| 614 |
|
| 615 |
static bool measure_fsverity(int dirfd, const char *name, struct fsverity_digest *digest) { |
| 616 |
_cleanup_close_ int fd = -EBADF; |
| 617 |
ASSERT_OK_ERRNO(fd = openat(dirfd, name, O_RDONLY | O_CLOEXEC | O_NOCTTY)); |
| 618 |
if (ioctl(fd, FS_IOC_MEASURE_VERITY, digest) == 0) |
| 619 |
return true; |
| 620 |
|
| 621 |
/* Make sure that the error is caused by the file simply not having fs-verity. */ |
| 622 |
ASSERT_TRUE(errno == ENODATA); |
| 623 |
return false; |
| 624 |
} |
| 625 |
|
| 626 |
static void assert_no_fsverity(int dirfd, const char *name) { |
| 627 |
_cleanup_free_ struct fsverity_digest *digest = alloc_fsverity_digest(0); |
| 628 |
ASSERT_FALSE(measure_fsverity(dirfd, name, digest)); |
| 629 |
} |
| 630 |
|
| 631 |
static void assert_fsverity_eq(int dirfd_a, int dirfd_b, const char *name) { |
| 632 |
_cleanup_free_ struct fsverity_digest *digest_a = alloc_fsverity_digest(64); |
| 633 |
_cleanup_free_ struct fsverity_digest *digest_b = alloc_fsverity_digest(64); |
| 634 |
|
| 635 |
bool enabled_a = measure_fsverity(dirfd_a, name, digest_a); |
| 636 |
bool enabled_b = measure_fsverity(dirfd_b, name, digest_b); |
| 637 |
ASSERT_EQ(enabled_a, enabled_b); |
| 638 |
if (enabled_a) { |
| 639 |
ASSERT_EQ(digest_a->digest_algorithm, digest_b->digest_algorithm); |
| 640 |
ASSERT_EQ(digest_a->digest_size, digest_b->digest_size); |
| 641 |
ASSERT_EQ(memcmp(digest_a->digest, digest_b->digest, digest_a->digest_size), 0); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
TEST_RET(copy_with_verity) { |
| 646 |
_cleanup_(rm_rf_physical_and_freep) char *srcp = NULL, *dstp = NULL, *badsrcp = NULL, *baddstp = NULL; |
| 647 |
const char *files[] = { "disabled", "simple", "bigsha", "salty" }; |
| 648 |
_cleanup_close_ int src = -EBADF, dst = -EBADF, badsrc = -EBADF, baddst = -EBADF; |
| 649 |
|
| 650 |
/* We're more likely to hit a filesystem with fs-verity enabled on /var/tmp than on /tmp (tmpfs) */ |
| 651 |
ASSERT_OK(src = mkdtemp_open("/var/tmp/test-copy_file-src.XXXXXX", 0, &srcp)); |
| 652 |
ASSERT_OK(dst = mkdtemp_open("/var/tmp/test-copy_file-dst.XXXXXX", 0, &dstp)); |
| 653 |
|
| 654 |
/* Populate some data to differentiate the files. */ |
| 655 |
FOREACH_ELEMENT(file, files) |
| 656 |
ASSERT_OK(write_string_file_at(src, *file, "src file", WRITE_STRING_FILE_CREATE)); |
| 657 |
|
| 658 |
/* Enable on some file using a range of options */ |
| 659 |
if (!enable_fsverity(src, "simple", FS_VERITY_HASH_ALG_SHA256, NULL, 0)) |
| 660 |
return log_tests_skipped_errno(errno, "/var/tmp: fs-verity is not supported here"); |
| 661 |
ASSERT_TRUE(enable_fsverity(src, "bigsha", FS_VERITY_HASH_ALG_SHA512, NULL, 0)); |
| 662 |
ASSERT_TRUE(enable_fsverity(src, "salty", FS_VERITY_HASH_ALG_SHA512, "edamame", 8)); |
| 663 |
|
| 664 |
/* Copy without fs-verity enabled and make sure nothing is set on the destination */ |
| 665 |
ASSERT_OK(copy_tree_at(src, ".", dst, ".", UID_INVALID, GID_INVALID, COPY_REPLACE|COPY_MERGE, NULL, NULL)); |
| 666 |
FOREACH_ELEMENT(file, files) |
| 667 |
assert_no_fsverity(dst, *file); |
| 668 |
|
| 669 |
/* Copy *with* fs-verity enabled and make sure it works properly */ |
| 670 |
int r = copy_tree_at(src, ".", dst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE|COPY_MERGE|COPY_PRESERVE_FS_VERITY, NULL, NULL); |
| 671 |
if (r == -ESOCKTNOSUPPORT) |
| 672 |
/* This can happen on some versions of btrfs, for example */ |
| 673 |
return log_tests_skipped_errno(errno, "/var/tmp: fs-verity supported, but not reading metadata"); |
| 674 |
ASSERT_OK(r); |
| 675 |
FOREACH_ELEMENT(file, files) |
| 676 |
assert_fsverity_eq(src, dst, *file); |
| 677 |
|
| 678 |
/* Now try to create files where we know fs-verity doesn't work: tmpfs */ |
| 679 |
ASSERT_OK(badsrc = mkdtemp_open("/tmp/test-copy_file-src.XXXXXX", 0, &badsrcp)); |
| 680 |
ASSERT_OK(baddst = mkdtemp_open("/tmp/test-copy_file-src.XXXXXX", 0, &baddstp)); |
| 681 |
|
| 682 |
/* Populate the source, same as before */ |
| 683 |
FOREACH_ELEMENT(file, files) |
| 684 |
ASSERT_OK(write_string_file_at(badsrc, *file, "src file", WRITE_STRING_FILE_CREATE)); |
| 685 |
|
| 686 |
/* Ensure the attempting to enable fs-verity here will fail */ |
| 687 |
if (enable_fsverity(badsrc, "simple", FS_VERITY_HASH_ALG_SHA256, NULL, 0)) |
| 688 |
return log_tests_skipped_errno(errno, "/tmp: fs-verity *is* unexpectedly supported here"); |
| 689 |
|
| 690 |
/* Copy from our non-verity filesystem into dst, requesting verity and making sure we notice that |
| 691 |
* we failed to read verity from the source. */ |
| 692 |
ASSERT_ERROR(copy_tree_at(badsrc, ".", dst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE|COPY_MERGE|COPY_PRESERVE_FS_VERITY, NULL, NULL), ESOCKTNOSUPPORT); |
| 693 |
|
| 694 |
/* Copy from our verity filesystem into our baddst, requesting verity and making sure we notice that |
| 695 |
* we failed to set verity on the destination. */ |
| 696 |
ASSERT_ERROR(copy_tree_at(src, ".", baddst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE|COPY_MERGE|COPY_PRESERVE_FS_VERITY, NULL, NULL), ESOCKTNOSUPPORT); |
| 697 |
|
| 698 |
/* Of course this should fail too... */ |
| 699 |
ASSERT_ERROR(copy_tree_at(badsrc, ".", baddst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE|COPY_MERGE|COPY_PRESERVE_FS_VERITY, NULL, NULL), ESOCKTNOSUPPORT); |
| 700 |
|
| 701 |
return 0; |
| 702 |
} |
| 703 |
|
| 704 |
DEFINE_TEST_MAIN(LOG_DEBUG); |