Debian Codegraph @

backup.c

raw symbols SCIP
1 /*
2 * Backup handling code.
3 *
4 * Copyright (C) 1999 Andrew Tridgell
5 * Copyright (C) 2003-2022 Wayne Davison
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, visit the http://fsf.org website.
19 */
20
21 #include "rsync.h"
22 #include "ifuncs.h"
23
24 extern int am_root;
25 extern int preserve_acls;
26 extern int preserve_xattrs;
27 extern int preserve_devices;
28 extern int preserve_specials;
29 extern int preserve_links;
30 extern int safe_symlinks;
31 extern int backup_dir_len;
32 extern unsigned int backup_dir_remainder;
33 extern int operator_path_resolve;
34 extern char backup_dir_buf[MAXPATHLEN];
35 extern char *backup_suffix;
36 extern char *backup_dir;
37
38 /* Pin a backup SOURCE leaf with a confined O_NOFOLLOW fd (via the operator
39 * owner-walk resolver, like set_file_attrs's op_leaf_fd) so the ACL/xattr the
40 * backup caches off it are read through the held fd -- a parent-symlink race
41 * can't redirect the read out of the module. Returns -1 for a non-hardened
42 * receiver (caller path-reads) or for a raced/absent leaf on a hardened one
43 * (caller skips the cache rather than read through a flippable path; use
44 * backup_metadata_hardened() to tell the two -1 cases apart). */
45 int backup_metadata_hardened(void)
46 {
47 return secure_relpath_active() && !symlink_optout_allowed();
48 }
49
50 int backup_source_fd(const char *path)
51 {
52 #if defined AT_FDCWD && defined O_NOFOLLOW
53 if (backup_metadata_hardened() && path && *path) {
54 int save = operator_path_resolve, fd;
55 operator_path_resolve = 1;
56 fd = do_open_at(path, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_CLOEXEC, 0);
57 operator_path_resolve = save;
58 return fd;
59 }
60 #endif
61 return -1;
62 }
63
64 /* Returns -1 on error, 0 on missing dir, and 1 on present dir. */
65 static int validate_backup_dir(void)
66 {
67 STRUCT_STAT st;
68
69 if (do_lstat_at(backup_dir_buf, &st) < 0) {
70 if (errno == ENOENT)
71 return 0;
72 rsyserr(FERROR, errno, "backup lstat %s failed", backup_dir_buf);
73 return -1;
74 }
75 if (!S_ISDIR(st.st_mode)) {
76 int flags = get_del_for_flag(st.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
77 if (delete_item(backup_dir_buf, st.st_mode, flags) == 0)
78 return 0;
79 return -1;
80 }
81 return 1;
82 }
83
84 /* Create a backup path from the given fname, putting the result into
85 * backup_dir_buf. Any new directories (compared to the prior backup
86 * path) are ensured to exist as directories, replacing anything else
87 * that may be in the way (e.g. a symlink). */
88 static BOOL copy_valid_path(const char *fname)
89 {
90 const char *f;
91 int val;
92 BOOL ret = True;
93 stat_x sx;
94 char *b, *rel = backup_dir_buf + backup_dir_len, *name = rel;
95
96 for (f = fname, b = rel; *f && *f == *b; f++, b++) {
97 if (*b == '/')
98 name = b + 1;
99 }
100
101 if (stringjoin(rel, backup_dir_remainder, fname, backup_suffix, NULL) >= backup_dir_remainder) {
102 rprintf(FERROR, "backup filename too long\n");
103 *name = '\0';
104 return False;
105 }
106
107 for ( ; ; name = b + 1) {
108 if ((b = strchr(name, '/')) == NULL)
109 return True;
110 *b = '\0';
111
112 val = validate_backup_dir();
113 if (val == 0)
114 break;
115 if (val < 0) {
116 *name = '\0';
117 return False;
118 }
119
120 *b = '/';
121 }
122
123 init_stat_x(&sx);
124
125 for ( ; b; name = b + 1, b = strchr(name, '/')) {
126 *b = '\0';
127
128 while (do_mkdir_at(backup_dir_buf, ACCESSPERMS) < 0) {
129 if (errno == EEXIST) {
130 val = validate_backup_dir();
131 if (val > 0)
132 break;
133 if (val == 0)
134 continue;
135 } else
136 rsyserr(FERROR, errno, "backup mkdir %s failed", backup_dir_buf);
137 *name = '\0';
138 ret = False;
139 goto cleanup;
140 }
141
142 /* Try to transfer the directory settings of the actual dir
143 * that the files are coming from. */
144 if (x_stat(rel, &sx.st, NULL) < 0)
145 rsyserr(FERROR, errno, "backup stat %s failed", full_fname(rel));
146 else {
147 struct file_struct *file;
148 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
149 continue;
150 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
151 { /* read the source dir's ACL/xattr through a confined fd */
152 int bfd = backup_source_fd(rel);
153 if (!backup_metadata_hardened() || bfd >= 0) {
154 # ifdef SUPPORT_ACLS
155 if (preserve_acls && !S_ISLNK(file->mode)) {
156 get_acl_fdat(bfd, -1, NULL, rel, &sx);
157 cache_tmp_acl(file, &sx);
158 free_acl(&sx);
159 }
160 # endif
161 # ifdef SUPPORT_XATTRS
162 if (preserve_xattrs) {
163 get_xattr(rel, bfd, &sx);
164 cache_tmp_xattr(file, &sx);
165 free_xattr(&sx);
166 }
167 # endif
168 }
169 if (bfd >= 0)
170 close(bfd);
171 }
172 #endif
173 set_file_attrs(backup_dir_buf, file, NULL, NULL, 0);
174 unmake_file(file);
175 }
176
177 *b = '/';
178 }
179
180 cleanup:
181
182 #ifdef SUPPORT_ACLS
183 uncache_tmp_acls();
184 #endif
185 #ifdef SUPPORT_XATTRS
186 uncache_tmp_xattrs();
187 #endif
188
189 return ret;
190 }
191
192 /* Make a complete pathname for backup file and verify any new path elements. */
193 char *get_backup_name(const char *fname)
194 {
195 if (backup_dir) {
196 static int initialized = 0;
197 if (!initialized) {
198 char dirbuf[MAXPATHLEN];
199 int ret;
200 if (strlcpy(dirbuf, backup_dir_buf, sizeof dirbuf) >= sizeof dirbuf) {
201 errno = ENAMETOOLONG;
202 return NULL;
203 }
204 if (backup_dir_len > 1)
205 dirbuf[backup_dir_len-1] = '\0';
206 ret = make_path(dirbuf, 0);
207 if (ret < 0)
208 return NULL;
209 initialized = 1;
210 }
211 /* copy fname into backup_dir_buf while validating the dirs. */
212 if (copy_valid_path(fname))
213 return backup_dir_buf;
214 /* copy_valid_path() has printed an error message. */
215 return NULL;
216 }
217
218 if (stringjoin(backup_dir_buf, MAXPATHLEN, fname, backup_suffix, NULL) < MAXPATHLEN)
219 return backup_dir_buf;
220
221 rprintf(FERROR, "backup filename too long\n");
222 return NULL;
223 }
224
225 /* Has same return codes as make_backup(). */
226 static inline int link_or_rename(const char *from, const char *to,
227 BOOL prefer_rename, STRUCT_STAT *stp)
228 {
229 #ifdef SUPPORT_HARD_LINKS
230 if (!prefer_rename) {
231 #ifndef CAN_HARDLINK_SYMLINK
232 if (S_ISLNK(stp->st_mode))
233 return 0; /* Use copy code. */
234 #endif
235 #ifndef CAN_HARDLINK_SPECIAL
236 if (IS_SPECIAL(stp->st_mode) || IS_DEVICE(stp->st_mode))
237 return 0; /* Use copy code. */
238 #endif
239 if (do_link_at(from, to) == 0) {
240 if (DEBUG_GTE(BACKUP, 1))
241 rprintf(FINFO, "make_backup: HLINK %s successful.\n", from);
242 return 2;
243 }
244 /* We prefer to rename a regular file rather than copy it. */
245 if (!S_ISREG(stp->st_mode) || errno == EEXIST || errno == EISDIR)
246 return 0;
247 }
248 #endif
249 if (do_rename_at(from, to) == 0) {
250 if (stp->st_nlink > 1 && !S_ISDIR(stp->st_mode)) {
251 /* If someone has hard-linked the file into the backup
252 * dir, rename() might return success but do nothing! */
253 robust_unlink(from); /* Just in case... */
254 }
255 if (DEBUG_GTE(BACKUP, 1))
256 rprintf(FINFO, "make_backup: RENAME %s successful.\n", from);
257 return 1;
258 }
259 return 0;
260 }
261
262 /* Hard-link, rename, or copy an item to the backup name. Returns 0 for
263 * failure, 1 if item was moved, 2 if item was duplicated or hard linked
264 * into backup area, or 3 if item doesn't exist or isn't a regular file. */
265 static int make_backup_inner(const char *fname, BOOL prefer_rename)
266 {
267 stat_x sx;
268 struct file_struct *file;
269 int save_preserve_xattrs;
270 char *buf;
271 int ret = 0;
272
273 init_stat_x(&sx);
274 /* Return success if no file to keep. */
275 if (x_lstat(fname, &sx.st, NULL) < 0)
276 return 3;
277
278 if (!(buf = get_backup_name(fname)))
279 return 0;
280
281 #ifdef SUPPORT_LINKS
282 /* Honor --safe-links BEFORE the hard-link / rename fast path. When
283 * CAN_HARDLINK_SYMLINK is defined, link_or_rename() would otherwise
284 * hard-link an escaping symlink (e.g. ../../etc/passwd) into the backup
285 * area and "goto success", skipping the safe_symlinks check in the
286 * copy-fallback path below -- silently preserving an unsafe link that
287 * --safe-links was meant to drop. Match the copy path: don't back up an
288 * unsafe symlink. */
289 if (preserve_links && S_ISLNK(sx.st.st_mode) && safe_symlinks) {
290 char lnkbuf[MAXPATHLEN];
291 int llen = do_readlink(fname, lnkbuf, MAXPATHLEN - 1);
292 /* A failed readlink means we can't verify the target, so fail
293 * closed: skip the backup rather than let the hard-link fast path
294 * preserve a possibly-unsafe symlink unchecked. */
295 if (llen <= 0) {
296 if (INFO_GTE(SYMSAFE, 1))
297 rprintf(FINFO, "not backing up symlink with unreadable target \"%s\"\n", fname);
298 ret = 2;
299 goto success;
300 }
301 lnkbuf[llen] = '\0';
302 if (unsafe_symlink(lnkbuf, fname)) {
303 if (INFO_GTE(SYMSAFE, 1)) {
304 rprintf(FINFO, "not backing up unsafe symlink \"%s\" -> \"%s\"\n",
305 fname, lnkbuf);
306 }
307 ret = 2;
308 goto success;
309 }
310 }
311 #endif
312
313 /* Try a hard-link or a rename first. Using rename is not atomic, but
314 * is more efficient than forcing a copy for larger files when no hard-
315 * linking is possible. */
316 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
317 goto success;
318 if (errno == EEXIST || errno == EISDIR) {
319 STRUCT_STAT bakst;
320 if (do_lstat_at(buf, &bakst) == 0) {
321 int flags = get_del_for_flag(bakst.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
322 if (delete_item(buf, bakst.st_mode, flags) != 0)
323 return 0;
324 }
325 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
326 goto success;
327 }
328
329 /* Fall back to making a copy. */
330 if (!(file = make_file(fname, NULL, &sx.st, 0, NO_FILTERS)))
331 return 3; /* the file could have disappeared */
332
333 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
334 { /* read the source file's ACL/xattr through a confined fd */
335 int bfd = backup_source_fd(fname);
336 if (!backup_metadata_hardened() || bfd >= 0) {
337 # ifdef SUPPORT_ACLS
338 if (preserve_acls && !S_ISLNK(file->mode)) {
339 get_acl_fdat(bfd, -1, NULL, fname, &sx);
340 cache_tmp_acl(file, &sx);
341 free_acl(&sx);
342 }
343 # endif
344 # ifdef SUPPORT_XATTRS
345 if (preserve_xattrs) {
346 get_xattr(fname, bfd, &sx);
347 cache_tmp_xattr(file, &sx);
348 free_xattr(&sx);
349 }
350 # endif
351 }
352 if (bfd >= 0)
353 close(bfd);
354 }
355 #endif
356
357 /* Check to see if this is a device file, or link */
358 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
359 || (preserve_specials && IS_SPECIAL(file->mode))) {
360 if (do_mknod_at(buf, file->mode, sx.st.st_rdev) < 0)
361 rsyserr(FERROR, errno, "mknod %s failed", full_fname(buf));
362 else if (DEBUG_GTE(BACKUP, 1))
363 rprintf(FINFO, "make_backup: DEVICE %s successful.\n", fname);
364 ret = 2;
365 }
366
367 #ifdef SUPPORT_LINKS
368 if (!ret && preserve_links && S_ISLNK(file->mode)) {
369 const char *sl = F_SYMLINK(file);
370 if (safe_symlinks && unsafe_symlink(sl, fname)) {
371 if (INFO_GTE(SYMSAFE, 1)) {
372 rprintf(FINFO, "not backing up unsafe symlink \"%s\" -> \"%s\"\n",
373 fname, sl);
374 }
375 ret = 2;
376 } else {
377 if (do_symlink_at(sl, buf) < 0)
378 rsyserr(FERROR, errno, "link %s -> \"%s\"", full_fname(buf), sl);
379 else if (DEBUG_GTE(BACKUP, 1))
380 rprintf(FINFO, "make_backup: SYMLINK %s successful.\n", fname);
381 ret = 2;
382 }
383 }
384 #endif
385
386 if (!ret && !S_ISREG(file->mode)) {
387 if (INFO_GTE(NONREG, 1))
388 rprintf(FINFO, "make_bak: skipping non-regular file %s\n", fname);
389 unmake_file(file);
390 #ifdef SUPPORT_ACLS
391 uncache_tmp_acls();
392 #endif
393 #ifdef SUPPORT_XATTRS
394 uncache_tmp_xattrs();
395 #endif
396 return 3;
397 }
398
399 /* Copy to backup tree if a file. */
400 if (!ret) {
401 if (copy_file(fname, buf, -1, file->mode) < 0) {
402 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
403 full_fname(fname), buf);
404 unmake_file(file);
405 #ifdef SUPPORT_ACLS
406 uncache_tmp_acls();
407 #endif
408 #ifdef SUPPORT_XATTRS
409 uncache_tmp_xattrs();
410 #endif
411 return 0;
412 }
413 if (DEBUG_GTE(BACKUP, 1))
414 rprintf(FINFO, "make_backup: COPY %s successful.\n", fname);
415 ret = 2;
416 }
417
418 save_preserve_xattrs = preserve_xattrs;
419 preserve_xattrs = 0;
420 set_file_attrs(buf, file, NULL, fname, ATTRS_ACCURATE_TIME);
421 preserve_xattrs = save_preserve_xattrs;
422
423 unmake_file(file);
424 #ifdef SUPPORT_ACLS
425 uncache_tmp_acls();
426 #endif
427 #ifdef SUPPORT_XATTRS
428 uncache_tmp_xattrs();
429 #endif
430
431 success:
432 if (INFO_GTE(BACKUP, 1))
433 rprintf(FINFO, "backed up %s to %s\n", fname, buf);
434 return ret;
435 }
436
437 int make_backup(const char *fname, BOOL prefer_rename)
438 {
439 int ret;
440 /* The --backup-dir is an operator-supplied path: resolve it (and the
441 * tail/rename beneath it) with the ownership walk so a foreign-owned
442 * symlink component is refused while the operator's own is followed --
443 * absolute and relative alike. --insecure-links / "insecure links ="
444 * restores legacy following. */
445 operator_path_resolve = 1;
446 ret = make_backup_inner(fname, prefer_rename);
447 operator_path_resolve = 0;
448 return ret;
449 }