1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <sys/xattr.h>
4 #include <unistd.h>
5 
6 #include "alloc-util.h"
7 #include "chase-symlinks.h"
8 #include "copy.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "fs-util.h"
12 #include "hexdecoct.h"
13 #include "log.h"
14 #include "macro.h"
15 #include "mkdir.h"
16 #include "path-util.h"
17 #include "rm-rf.h"
18 #include "string-util.h"
19 #include "strv.h"
20 #include "tests.h"
21 #include "tmpfile-util.h"
22 #include "user-util.h"
23 #include "util.h"
24 #include "xattr-util.h"
25 
TEST(copy_file)26 TEST(copy_file) {
27         _cleanup_free_ char *buf = NULL;
28         char fn[] = "/tmp/test-copy_file.XXXXXX";
29         char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
30         size_t sz = 0;
31         int fd;
32 
33         fd = mkostemp_safe(fn);
34         assert_se(fd >= 0);
35         close(fd);
36 
37         fd = mkostemp_safe(fn_copy);
38         assert_se(fd >= 0);
39         close(fd);
40 
41         assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0);
42 
43         assert_se(copy_file(fn, fn_copy, 0, 0644, 0, 0, COPY_REFLINK) == 0);
44 
45         assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
46         assert_se(streq(buf, "foo bar bar bar foo\n"));
47         assert_se(sz == 20);
48 
49         unlink(fn);
50         unlink(fn_copy);
51 }
52 
TEST(copy_file_fd)53 TEST(copy_file_fd) {
54         char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
55         char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
56         _cleanup_close_ int in_fd = -1, out_fd = -1;
57         const char *text = "boohoo\nfoo\n\tbar\n";
58         char buf[64] = {};
59 
60         in_fd = mkostemp_safe(in_fn);
61         assert_se(in_fd >= 0);
62         out_fd = mkostemp_safe(out_fn);
63         assert_se(out_fd >= 0);
64 
65         assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
66         assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, COPY_REFLINK) < 0);
67         assert_se(copy_file_fd(in_fn, out_fd, COPY_REFLINK) >= 0);
68         assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
69 
70         assert_se(read(out_fd, buf, sizeof buf) == (ssize_t) strlen(text));
71         assert_se(streq(buf, text));
72 
73         unlink(in_fn);
74         unlink(out_fn);
75 }
76 
TEST(copy_tree)77 TEST(copy_tree) {
78         char original_dir[] = "/tmp/test-copy_tree/";
79         char copy_dir[] = "/tmp/test-copy_tree-copy/";
80         char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
81         char **symlinks = STRV_MAKE("link", "file",
82                                     "link2", "dir1/file");
83         char **hardlinks = STRV_MAKE("hlink", "file",
84                                      "hlink2", "dir1/file");
85         const char *unixsockp;
86         struct stat st;
87         int xattr_worked = -1; /* xattr support is optional in temporary directories, hence use it if we can,
88                                 * but don't fail if we can't */
89 
90         (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
91         (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
92 
93         STRV_FOREACH(p, files) {
94                 _cleanup_free_ char *f, *c;
95                 int k;
96 
97                 assert_se(f = path_join(original_dir, *p));
98 
99                 assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755) == 0);
100 
101                 assert_se(base64mem(*p, strlen(*p), &c) >= 0);
102 
103                 k = setxattr(f, "user.testxattr", c, strlen(c), 0);
104                 assert_se(xattr_worked < 0 || ((k >= 0) == !!xattr_worked));
105                 xattr_worked = k >= 0;
106         }
107 
108         STRV_FOREACH_PAIR(ll, p, symlinks) {
109                 _cleanup_free_ char *f, *l;
110 
111                 assert_se(f = path_join(original_dir, *p));
112                 assert_se(l = path_join(original_dir, *ll));
113 
114                 assert_se(mkdir_parents(l, 0755) >= 0);
115                 assert_se(symlink(f, l) == 0);
116         }
117 
118         STRV_FOREACH_PAIR(ll, p, hardlinks) {
119                 _cleanup_free_ char *f, *l;
120 
121                 assert_se(f = path_join(original_dir, *p));
122                 assert_se(l = path_join(original_dir, *ll));
123 
124                 assert_se(mkdir_parents(l, 0755) >= 0);
125                 assert_se(link(f, l) == 0);
126         }
127 
128         unixsockp = strjoina(original_dir, "unixsock");
129         assert_se(mknod(unixsockp, S_IFSOCK|0644, 0) >= 0);
130 
131         assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_MERGE|COPY_HARDLINKS) == 0);
132 
133         STRV_FOREACH(p, files) {
134                 _cleanup_free_ char *buf, *f, *c = NULL;
135                 size_t sz;
136                 int k;
137 
138                 assert_se(f = path_join(copy_dir, *p));
139 
140                 assert_se(access(f, F_OK) == 0);
141                 assert_se(read_full_file(f, &buf, &sz) == 0);
142                 assert_se(streq(buf, "file\n"));
143 
144                 k = lgetxattr_malloc(f, "user.testxattr", &c);
145                 assert_se(xattr_worked < 0 || ((k >= 0) == !!xattr_worked));
146 
147                 if (k >= 0) {
148                         _cleanup_free_ char *d = NULL;
149 
150                         assert_se(base64mem(*p, strlen(*p), &d) >= 0);
151                         assert_se(streq(d, c));
152                 }
153         }
154 
155         STRV_FOREACH_PAIR(ll, p, symlinks) {
156                 _cleanup_free_ char *target, *f, *l;
157 
158                 assert_se(f = strjoin(original_dir, *p));
159                 assert_se(l = strjoin(copy_dir, *ll));
160 
161                 assert_se(chase_symlinks(l, NULL, 0, &target, NULL) == 1);
162                 assert_se(path_equal(f, target));
163         }
164 
165         STRV_FOREACH_PAIR(ll, p, hardlinks) {
166                 _cleanup_free_ char *f, *l;
167                 struct stat a, b;
168 
169                 assert_se(f = strjoin(copy_dir, *p));
170                 assert_se(l = strjoin(copy_dir, *ll));
171 
172                 assert_se(lstat(f, &a) >= 0);
173                 assert_se(lstat(l, &b) >= 0);
174 
175                 assert_se(a.st_ino == b.st_ino);
176                 assert_se(a.st_dev == b.st_dev);
177         }
178 
179         unixsockp = strjoina(copy_dir, "unixsock");
180         assert_se(stat(unixsockp, &st) >= 0);
181         assert_se(S_ISSOCK(st.st_mode));
182 
183         assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0);
184         assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0);
185 
186         (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
187         (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
188 }
189 
TEST(copy_bytes)190 TEST(copy_bytes) {
191         _cleanup_close_pair_ int pipefd[2] = {-1, -1};
192         _cleanup_close_ int infd = -1;
193         int r, r2;
194         char buf[1024], buf2[1024];
195 
196         infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC);
197         if (infd < 0)
198                 infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC);
199         assert_se(infd >= 0);
200 
201         assert_se(pipe2(pipefd, O_CLOEXEC) == 0);
202 
203         r = copy_bytes(infd, pipefd[1], UINT64_MAX, 0);
204         assert_se(r == 0);
205 
206         r = read(pipefd[0], buf, sizeof(buf));
207         assert_se(r >= 0);
208 
209         assert_se(lseek(infd, 0, SEEK_SET) == 0);
210         r2 = read(infd, buf2, sizeof(buf2));
211         assert_se(r == r2);
212 
213         assert_se(strneq(buf, buf2, r));
214 
215         /* test copy_bytes with invalid descriptors */
216         r = copy_bytes(pipefd[0], pipefd[0], 1, 0);
217         assert_se(r == -EBADF);
218 
219         r = copy_bytes(pipefd[1], pipefd[1], 1, 0);
220         assert_se(r == -EBADF);
221 
222         r = copy_bytes(pipefd[1], infd, 1, 0);
223         assert_se(r == -EBADF);
224 }
225 
test_copy_bytes_regular_file_one(const char * src,bool try_reflink,uint64_t max_bytes)226 static void test_copy_bytes_regular_file_one(const char *src, bool try_reflink, uint64_t max_bytes) {
227         char fn2[] = "/tmp/test-copy-file-XXXXXX";
228         char fn3[] = "/tmp/test-copy-file-XXXXXX";
229         _cleanup_close_ int fd = -1, fd2 = -1, fd3 = -1;
230         int r;
231         struct stat buf, buf2, buf3;
232 
233         log_info("%s try_reflink=%s max_bytes=%" PRIu64, __func__, yes_no(try_reflink), max_bytes);
234 
235         fd = open(src, O_RDONLY | O_CLOEXEC | O_NOCTTY);
236         assert_se(fd >= 0);
237 
238         fd2 = mkostemp_safe(fn2);
239         assert_se(fd2 >= 0);
240 
241         fd3 = mkostemp_safe(fn3);
242         assert_se(fd3 >= 0);
243 
244         r = copy_bytes(fd, fd2, max_bytes, try_reflink ? COPY_REFLINK : 0);
245         if (max_bytes == UINT64_MAX)
246                 assert_se(r == 0);
247         else
248                 assert_se(IN_SET(r, 0, 1));
249 
250         assert_se(fstat(fd, &buf) == 0);
251         assert_se(fstat(fd2, &buf2) == 0);
252         assert_se((uint64_t) buf2.st_size == MIN((uint64_t) buf.st_size, max_bytes));
253 
254         if (max_bytes < UINT64_MAX)
255                 /* Make sure the file is now higher than max_bytes */
256                 assert_se(ftruncate(fd2, max_bytes + 1) == 0);
257 
258         assert_se(lseek(fd2, 0, SEEK_SET) == 0);
259 
260         r = copy_bytes(fd2, fd3, max_bytes, try_reflink ? COPY_REFLINK : 0);
261         if (max_bytes == UINT64_MAX)
262                 assert_se(r == 0);
263         else
264                 /* We cannot distinguish between the input being exactly max_bytes
265                  * or longer than max_bytes (without trying to read one more byte,
266                  * or calling stat, or FION_READ, etc, and we don't want to do any
267                  * of that). So we expect "truncation" since we know that file we
268                  * are copying is exactly max_bytes bytes. */
269                 assert_se(r == 1);
270 
271         assert_se(fstat(fd3, &buf3) == 0);
272 
273         if (max_bytes == UINT64_MAX)
274                 assert_se(buf3.st_size == buf2.st_size);
275         else
276                 assert_se((uint64_t) buf3.st_size == max_bytes);
277 
278         unlink(fn2);
279         unlink(fn3);
280 }
281 
TEST(copy_bytes_regular_file)282 TEST(copy_bytes_regular_file) {
283         test_copy_bytes_regular_file_one(saved_argv[0], false, UINT64_MAX);
284         test_copy_bytes_regular_file_one(saved_argv[0], true, UINT64_MAX);
285         test_copy_bytes_regular_file_one(saved_argv[0], false, 1000); /* smaller than copy buffer size */
286         test_copy_bytes_regular_file_one(saved_argv[0], true, 1000);
287         test_copy_bytes_regular_file_one(saved_argv[0], false, 32000); /* larger than copy buffer size */
288         test_copy_bytes_regular_file_one(saved_argv[0], true, 32000);
289 }
290 
TEST(copy_atomic)291 TEST(copy_atomic) {
292         _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
293         const char *q;
294         int r;
295 
296         assert_se(mkdtemp_malloc(NULL, &p) >= 0);
297 
298         q = strjoina(p, "/fstab");
299 
300         r = copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REFLINK);
301         if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r))
302                 return;
303 
304         assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REFLINK) == -EEXIST);
305 
306         assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REPLACE) >= 0);
307 }
308 
TEST(copy_proc)309 TEST(copy_proc) {
310         _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
311         _cleanup_free_ char *f = NULL, *a = NULL, *b = NULL;
312 
313         /* Check if copying data from /proc/ works correctly, i.e. let's see if https://lwn.net/Articles/846403/ is a problem for us */
314 
315         assert_se(mkdtemp_malloc(NULL, &p) >= 0);
316         assert_se(f = path_join(p, "version"));
317         assert_se(copy_file("/proc/version", f, 0, MODE_INVALID, 0, 0, 0) >= 0);
318 
319         assert_se(read_one_line_file("/proc/version", &a) >= 0);
320         assert_se(read_one_line_file(f, &b) >= 0);
321         assert_se(streq(a, b));
322         assert_se(!isempty(a));
323 }
324 
TEST_RET(copy_holes)325 TEST_RET(copy_holes) {
326         char fn[] = "/var/tmp/test-copy-hole-fd-XXXXXX";
327         char fn_copy[] = "/var/tmp/test-copy-hole-fd-XXXXXX";
328         struct stat stat;
329         off_t blksz;
330         int r, fd, fd_copy;
331         char *buf;
332 
333         fd = mkostemp_safe(fn);
334         assert_se(fd >= 0);
335 
336         fd_copy = mkostemp_safe(fn_copy);
337         assert_se(fd_copy >= 0);
338 
339         r = RET_NERRNO(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 1));
340         if (ERRNO_IS_NOT_SUPPORTED(r))
341                 return log_tests_skipped("Filesystem doesn't support hole punching");
342         assert_se(r >= 0);
343 
344         assert_se(fstat(fd, &stat) >= 0);
345         blksz = stat.st_blksize;
346         buf = alloca_safe(blksz);
347         memset(buf, 1, blksz);
348 
349         /* We need to make sure to create hole in multiples of the block size, otherwise filesystems (btrfs)
350          * might silently truncate/extend the holes. */
351 
352         assert_se(lseek(fd, blksz, SEEK_CUR) >= 0);
353         assert_se(write(fd, buf, blksz) >= 0);
354         assert_se(lseek(fd, 0, SEEK_END) == 2 * blksz);
355         /* Only ftruncate() can create holes at the end of a file. */
356         assert_se(ftruncate(fd, 3 * blksz) >= 0);
357         assert_se(lseek(fd, 0, SEEK_SET) >= 0);
358 
359         assert_se(copy_bytes(fd, fd_copy, UINT64_MAX, COPY_HOLES) >= 0);
360 
361         /* Test that the hole starts at the beginning of the file. */
362         assert_se(lseek(fd_copy, 0, SEEK_HOLE) == 0);
363         /* Test that the hole has the expected size. */
364         assert_se(lseek(fd_copy, 0, SEEK_DATA) == blksz);
365         assert_se(lseek(fd_copy, blksz, SEEK_HOLE) == 2 * blksz);
366         assert_se(lseek(fd_copy, 2 * blksz, SEEK_DATA) < 0 && errno == ENXIO);
367 
368         /* Test that the copied file has the correct size. */
369         assert_se(fstat(fd_copy, &stat) >= 0);
370         assert_se(stat.st_size == 3 * blksz);
371 
372         close(fd);
373         close(fd_copy);
374 
375         unlink(fn);
376         unlink(fn_copy);
377 
378         return 0;
379 }
380 
381 DEFINE_TEST_MAIN(LOG_DEBUG);
382