Debian Codegraph @

xattrs.c

raw symbols SCIP
1 /*
2 * Extended Attribute support for rsync.
3 * Written by Jay Fenlason, vaguely based on the ACLs patch.
4 *
5 * Copyright (C) 2004 Red Hat, Inc.
6 * Copyright (C) 2006-2022 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22 #include "rsync.h"
23 #include "ifuncs.h"
24 #include "inums.h"
25 #include "lib/sysxattrs.h"
26
27 #ifdef SUPPORT_XATTRS
28
29 extern int dry_run;
30 extern int am_root;
31 extern int am_sender;
32 extern int am_generator;
33 extern int read_only;
34 extern int list_only;
35 extern int preserve_xattrs;
36 extern int preserve_links;
37 extern int preserve_devices;
38 extern int preserve_specials;
39 extern int checksum_seed;
40 extern int saw_xattr_filter;
41
42 extern struct name_num_item *xattr_sum_nni;
43 extern int xattr_sum_len;
44
45 #define RSYNC_XAL_INITIAL 5
46 #define RSYNC_XAL_LIST_INITIAL 100
47
48 #define MAX_XATTR_DIGEST_LEN MD5_DIGEST_LEN
49 #define MAX_FULL_DATUM 32
50 #define MAX_XATTR_LIST_BYTES ((size_t)512 * 1024 * 1024)
51 #define MAX_XATTR_VALUE_BYTES ((size_t)128 * 1024 * 1024)
52
53 #define HAS_PREFIX(str, prfx) (*(str) == *(prfx) && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
54
55 #define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
56
57 #define XSTATE_ABBREV 1
58 #define XSTATE_DONE 2
59 #define XSTATE_TODO 3
60
61 #define USER_PREFIX "user."
62 #define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
63 #define SYSTEM_PREFIX "system."
64 #define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
65
66 #ifdef HAVE_LINUX_XATTRS
67 #define MIGHT_NEED_RPRE (am_root <= 0)
68 #define RSYNC_PREFIX USER_PREFIX "rsync."
69 #else
70 #define MIGHT_NEED_RPRE am_root
71 #define RSYNC_PREFIX "rsync."
72 #endif
73 #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
74
75 #define XSTAT_SUFFIX "stat"
76 #define XSTAT_ATTR RSYNC_PREFIX "%" XSTAT_SUFFIX
77 #define XACC_ACL_SUFFIX "aacl"
78 #define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
79 #define XDEF_ACL_SUFFIX "dacl"
80 #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
81
82 typedef struct {
83 char *datum, *name;
84 size_t datum_len, name_len;
85 int num;
86 } rsync_xa;
87
88 struct _rsync_xa_list;
89
90 typedef struct _rsync_xa_list_ref {
91 struct _rsync_xa_list_ref *next;
92 int ndx;
93 } rsync_xa_list_ref;
94
95 typedef struct _rsync_xa_list {
96 int ndx;
97 int64 key;
98 item_list xa_items;
99 } rsync_xa_list;
100
101 static size_t namebuf_len = 0;
102 static char *namebuf = NULL;
103
104 static const rsync_xa_list empty_xa_list = {
105 .xa_items = EMPTY_ITEM_LIST,
106 };
107 static const item_list empty_xattr = EMPTY_ITEM_LIST;
108 static item_list rsync_xal_l = EMPTY_ITEM_LIST;
109 static struct hashtable *rsync_xal_h = NULL;
110
111 static size_t prior_xattr_count = (size_t)-1;
112
113 /* ------------------------------------------------------------------------- */
114
115 static void rsync_xal_free(item_list *xalp)
116 {
117 size_t i;
118 rsync_xa *rxas = xalp->items;
119
120 if (!xalp->malloced)
121 return;
122
123 for (i = 0; i < xalp->count; i++) {
124 free(rxas[i].datum);
125 /*free(rxas[i].name);*/
126 }
127 free(xalp->items);
128 }
129
130 void free_xattr(stat_x *sxp)
131 {
132 if (!sxp->xattr)
133 return;
134 rsync_xal_free(sxp->xattr);
135 free(sxp->xattr);
136 sxp->xattr = NULL;
137 }
138
139 static int rsync_xal_compare_names(const void *x1, const void *x2)
140 {
141 const rsync_xa *xa1 = x1;
142 const rsync_xa *xa2 = x2;
143 return strcmp(xa1->name, xa2->name);
144 }
145
146 /* List the names of fname's xattrs into the global namebuf. When fd >= 0 the
147 * list is read from that held fd (sys_flistxattr) so a parent-symlink race
148 * can't redirect it; otherwise it falls back to the path (sys_llistxattr). */
149 static ssize_t get_xattr_names(const char *fname, int fd)
150 {
151 ssize_t list_len;
152 int64 arg;
153
154 if (!namebuf) {
155 namebuf_len = 1024;
156 namebuf = new_array(char, namebuf_len);
157 }
158
159 while (1) {
160 /* The length returned includes all the '\0' terminators. */
161 list_len = fd >= 0 ? sys_flistxattr(fd, namebuf, namebuf_len)
162 : sys_llistxattr(fname, namebuf, namebuf_len);
163 if (list_len >= 0) {
164 if ((size_t)list_len <= namebuf_len)
165 break;
166 } else if (errno == ENOTSUP)
167 return 0;
168 else if (errno != ERANGE) {
169 arg = namebuf_len;
170 got_error:
171 rsyserr(FERROR_XFER, errno,
172 "get_xattr_names: llistxattr(%s,%s) failed",
173 full_fname(fname), big_num(arg));
174 return -1;
175 }
176 list_len = fd >= 0 ? sys_flistxattr(fd, NULL, 0)
177 : sys_llistxattr(fname, NULL, 0);
178 if (list_len < 0) {
179 arg = 0;
180 goto got_error;
181 }
182 if (namebuf_len)
183 free(namebuf);
184 namebuf_len = list_len + 1024;
185 namebuf = new_array(char, namebuf_len);
186 }
187
188 return list_len;
189 }
190
191 /* On entry, the *len_ptr parameter contains the size of the extra space we
192 * should allocate when we create a buffer for the data. On exit, it contains
193 * the length of the datum. */
194 /* Read xattr `name`'s value. When fd >= 0 the read goes through that held fd
195 * (sys_fgetxattr) so a parent-symlink race can't redirect it; otherwise it falls
196 * back to the path (sys_lgetxattr), matching get_xattr_names(). */
197 static char *get_xattr_data(const char *fname, int fd, const char *name, size_t *len_ptr, int no_missing_error)
198 {
199 size_t datum_len = fd >= 0 ? sys_fgetxattr(fd, name, NULL, 0)
200 : sys_lgetxattr(fname, name, NULL, 0);
201 size_t extra_len = *len_ptr;
202 char *ptr;
203
204 *len_ptr = datum_len;
205
206 if (datum_len == (size_t)-1) {
207 if (errno == ENOTSUP || no_missing_error)
208 return NULL;
209 rsyserr(FERROR_XFER, errno,
210 "get_xattr_data: lgetxattr(%s,\"%s\",0) failed",
211 full_fname(fname), name);
212 return NULL;
213 }
214
215 if (!datum_len && !extra_len)
216 extra_len = 1; /* request non-zero amount of memory */
217 if (SIZE_MAX - datum_len < extra_len)
218 overflow_exit("get_xattr_data");
219 ptr = new_array(char, datum_len + extra_len);
220
221 if (datum_len) {
222 size_t len = fd >= 0 ? sys_fgetxattr(fd, name, ptr, datum_len)
223 : sys_lgetxattr(fname, name, ptr, datum_len);
224 if (len != datum_len) {
225 if (len == (size_t)-1) {
226 rsyserr(FERROR_XFER, errno,
227 "get_xattr_data: lgetxattr(%s,\"%s\",%ld) failed",
228 full_fname(fname), name, (long)datum_len);
229 } else {
230 rprintf(FERROR_XFER,
231 "get_xattr_data: lgetxattr(%s,\"%s\",%ld) returned %ld\n",
232 full_fname(fname), name,
233 (long)datum_len, (long)len);
234 }
235 free(ptr);
236 return NULL;
237 }
238 }
239
240 return ptr;
241 }
242
243 static int rsync_xal_get(const char *fname, int fd, item_list *xalp)
244 {
245 ssize_t list_len, name_len;
246 size_t datum_len, name_offset;
247 char *name, *ptr;
248 #ifdef HAVE_LINUX_XATTRS
249 int user_only = am_sender ? 0 : !am_root;
250 #endif
251 rsync_xa *rxa;
252 int count;
253
254 /* This puts the name list into the "namebuf" buffer. */
255 if ((list_len = get_xattr_names(fname, fd)) < 0)
256 return -1;
257
258 for (name = namebuf; list_len > 0; name += name_len) {
259 name_len = strlen(name) + 1;
260 list_len -= name_len;
261
262 if (saw_xattr_filter) {
263 if (name_is_excluded(name, NAME_IS_XATTR, ALL_FILTERS))
264 continue;
265 }
266 #ifdef HAVE_LINUX_XATTRS
267 /* Choose between ignoring the system namespace or (non-root) ignoring any non-user namespace. */
268 else if (user_only ? !HAS_PREFIX(name, USER_PREFIX) : HAS_PREFIX(name, SYSTEM_PREFIX))
269 continue;
270 #endif
271
272 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
273 if (name_len > RPRE_LEN && name[RPRE_LEN] == '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
274 if ((am_sender && preserve_xattrs < 2)
275 || (am_root < 0
276 && (strcmp(name+RPRE_LEN+1, XSTAT_SUFFIX) == 0
277 || strcmp(name+RPRE_LEN+1, XACC_ACL_SUFFIX) == 0
278 || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0)))
279 continue;
280 }
281
282 datum_len = name_len; /* Pass extra size to get_xattr_data() */
283 if (!(ptr = get_xattr_data(fname, fd, name, &datum_len, 0)))
284 return -1;
285
286 if (datum_len > MAX_FULL_DATUM) {
287 /* For large datums, we store a flag and a checksum. */
288 name_offset = 1 + MAX_XATTR_DIGEST_LEN;
289 sum_init(xattr_sum_nni, checksum_seed);
290 sum_update(ptr, datum_len);
291 free(ptr);
292
293 ptr = new_array(char, name_offset + name_len);
294 *ptr = XSTATE_ABBREV;
295 sum_end(ptr + 1);
296 } else
297 name_offset = datum_len;
298
299 rxa = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
300 rxa->name = ptr + name_offset;
301 memcpy(rxa->name, name, name_len);
302 rxa->datum = ptr;
303 rxa->name_len = name_len;
304 rxa->datum_len = datum_len;
305 }
306 count = xalp->count;
307 rxa = xalp->items;
308 if (count > 1)
309 qsort(rxa, count, sizeof (rsync_xa), rsync_xal_compare_names);
310 /* Guard count==0: rxa is then xalp->items (possibly NULL) and the
311 * "rxa += count-1" init would form NULL-1 (undefined). */
312 if (count) {
313 for (rxa += count-1; count; count--, rxa--)
314 rxa->num = count;
315 }
316 return 0;
317 }
318
319 /* Read the xattr(s) for this filename. */
320 /* Collect fname's xattrs into sxp. fd >= 0 reads them through that held fd
321 * (race-safe); fd < 0 uses the path. */
322 int get_xattr(const char *fname, int fd, stat_x *sxp)
323 {
324 sxp->xattr = new(item_list);
325 *sxp->xattr = empty_xattr;
326
327 if (S_ISREG(sxp->st.st_mode) || S_ISDIR(sxp->st.st_mode)) {
328 /* Everyone supports this. */
329 } else if (S_ISLNK(sxp->st.st_mode)) {
330 #ifndef NO_SYMLINK_XATTRS
331 if (!preserve_links)
332 #endif
333 return 0;
334 } else if (IS_SPECIAL(sxp->st.st_mode)) {
335 #ifndef NO_SPECIAL_XATTRS
336 if (!preserve_specials)
337 #endif
338 return 0;
339 } else if (IS_DEVICE(sxp->st.st_mode)) {
340 #ifndef NO_DEVICE_XATTRS
341 if (!preserve_devices)
342 #endif
343 return 0;
344 } else if (IS_MISSING_FILE(sxp->st))
345 return 0;
346
347 if (rsync_xal_get(fname, fd, sxp->xattr) < 0) {
348 free_xattr(sxp);
349 return -1;
350 }
351 return 0;
352 }
353
354 /* Copy xattrs from source to dest. source_fd/dest_fd, when >= 0, are held
355 * O_NOFOLLOW-opened fds so the source read (fgetxattr) and the dest set
356 * (fsetxattr) can't be redirected by a parent-symlink race; a < 0 fd keeps the
357 * path-based l-variant (non-hardened receivers, or no held fd available). */
358 int copy_xattrs(const char *source, int source_fd, const char *dest, int dest_fd)
359 {
360 ssize_t list_len, name_len;
361 size_t datum_len;
362 char *name, *ptr;
363 #ifdef HAVE_LINUX_XATTRS
364 int user_only = am_sender ? 0 : am_root <= 0;
365 #endif
366
367 /* This puts the name list into the "namebuf" buffer. */
368 if ((list_len = get_xattr_names(source, source_fd)) < 0)
369 return -1;
370
371 for (name = namebuf; list_len > 0; name += name_len) {
372 name_len = strlen(name) + 1;
373 list_len -= name_len;
374
375 if (saw_xattr_filter) {
376 if (name_is_excluded(name, NAME_IS_XATTR, ALL_FILTERS))
377 continue;
378 }
379 #ifdef HAVE_LINUX_XATTRS
380 /* Choose between ignoring the system namespace or (non-root) ignoring any non-user namespace. */
381 else if (user_only ? !HAS_PREFIX(name, USER_PREFIX) : HAS_PREFIX(name, SYSTEM_PREFIX))
382 continue;
383 #endif
384
385 datum_len = 0;
386 if (!(ptr = get_xattr_data(source, source_fd, name, &datum_len, 0)))
387 return -1;
388 if ((dest_fd >= 0 ? sys_fsetxattr(dest_fd, name, ptr, datum_len)
389 : sys_lsetxattr(dest, name, ptr, datum_len)) < 0) {
390 int save_errno = errno ? errno : EINVAL;
391 rsyserr(FERROR_XFER, errno,
392 "copy_xattrs: %ssetxattr(%s,\"%s\") failed",
393 dest_fd >= 0 ? "f" : "l", full_fname(dest), name);
394 errno = save_errno;
395 free(ptr);
396 return -1;
397 }
398 free(ptr);
399 }
400
401 return 0;
402 }
403
404 static int64 xattr_lookup_hash(const item_list *xalp)
405 {
406 const rsync_xa *rxas = xalp->items;
407 size_t i;
408 /* Accumulate unsigned: the summed hash values routinely overflow a
409 * signed int64 (UB), and we only care about the resulting bit pattern. */
410 uint64_t key = (uint64_t)hashlittle2(&xalp->count, sizeof xalp->count);
411
412 for (i = 0; i < xalp->count; i++) {
413 key += (uint64_t)hashlittle2(rxas[i].name, rxas[i].name_len);
414 if (rxas[i].datum_len > MAX_FULL_DATUM)
415 key += (uint64_t)hashlittle2(rxas[i].datum, xattr_sum_len);
416 else
417 key += (uint64_t)hashlittle2(rxas[i].datum, rxas[i].datum_len);
418 }
419
420 return (int64)key;
421 }
422
423 static int find_matching_xattr(const item_list *xalp)
424 {
425 const struct ht_int64_node *node;
426 const rsync_xa_list_ref *ref;
427 int64 key;
428
429 if (rsync_xal_h == NULL)
430 return -1;
431
432 key = xattr_lookup_hash(xalp);
433
434 node = hashtable_find(rsync_xal_h, key, NULL);
435 if (node == NULL)
436 return -1;
437
438 if (node->data == NULL)
439 return -1;
440
441 for (ref = node->data; ref != NULL; ref = ref->next) {
442 const rsync_xa_list *ptr = rsync_xal_l.items;
443 const rsync_xa *rxas1;
444 const rsync_xa *rxas2 = xalp->items;
445 size_t j;
446
447 ptr += ref->ndx;
448 rxas1 = ptr->xa_items.items;
449
450 /* Wrong number of elements? */
451 if (ptr->xa_items.count != xalp->count)
452 continue;
453 /* any elements different? */
454 for (j = 0; j < xalp->count; j++) {
455 if (rxas1[j].name_len != rxas2[j].name_len
456 || rxas1[j].datum_len != rxas2[j].datum_len
457 || strcmp(rxas1[j].name, rxas2[j].name))
458 break;
459 if (rxas1[j].datum_len > MAX_FULL_DATUM) {
460 if (memcmp(rxas1[j].datum + 1,
461 rxas2[j].datum + 1,
462 xattr_sum_len) != 0)
463 break;
464 } else {
465 if (memcmp(rxas1[j].datum, rxas2[j].datum,
466 rxas2[j].datum_len))
467 break;
468 }
469 }
470 /* no differences found. This is The One! */
471 if (j == xalp->count)
472 return ref->ndx;
473 }
474
475 return -1;
476 }
477
478 /* Store *xalp on the end of rsync_xal_l */
479 static int rsync_xal_store(item_list *xalp)
480 {
481 struct ht_int64_node *node;
482 int ndx = rsync_xal_l.count; /* pre-incremented count */
483 rsync_xa_list *new_list = EXPAND_ITEM_LIST(&rsync_xal_l, rsync_xa_list, RSYNC_XAL_LIST_INITIAL);
484 rsync_xa_list_ref *new_ref;
485 /* Since the following call starts a new list, we know it will hold the
486 * entire initial-count, not just enough space for one new item. */
487 *new_list = empty_xa_list;
488 (void)EXPAND_ITEM_LIST(&new_list->xa_items, rsync_xa, xalp->count);
489 /* xalp->items is NULL for an empty list; memcpy(dst, NULL, 0) is UB. */
490 if (xalp->count)
491 memcpy(new_list->xa_items.items, xalp->items, xalp->count * sizeof (rsync_xa));
492 new_list->xa_items.count = xalp->count;
493 xalp->count = 0;
494
495 new_list->ndx = ndx;
496 new_list->key = xattr_lookup_hash(&new_list->xa_items);
497
498 if (rsync_xal_h == NULL)
499 rsync_xal_h = hashtable_create(512, HT_KEY64);
500
501 new_ref = new0(rsync_xa_list_ref);
502 new_ref->ndx = ndx;
503
504 node = hashtable_find(rsync_xal_h, new_list->key, new_ref);
505 if (node->data != (void*)new_ref) {
506 rsync_xa_list_ref *ref = node->data;
507
508 while (ref != NULL) {
509 if (ref->next != NULL) {
510 ref = ref->next;
511 continue;
512 }
513
514 ref->next = new_ref;
515 break;
516 }
517 }
518
519 return ndx;
520 }
521
522 /* Send the make_xattr()-generated xattr list for this flist entry. */
523 int send_xattr(int f, stat_x *sxp)
524 {
525 int ndx = find_matching_xattr(sxp->xattr);
526
527 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
528 write_varint(f, ndx + 1);
529
530 if (ndx < 0) {
531 rsync_xa *rxa;
532 int count = sxp->xattr->count;
533 write_varint(f, count);
534 for (rxa = sxp->xattr->items; count--; rxa++) {
535 size_t name_len = rxa->name_len;
536 const char *name = rxa->name;
537 /* Strip the rsync prefix from disguised namespaces. */
538 if (name_len > RPRE_LEN
539 #ifdef HAVE_LINUX_XATTRS
540 && am_root < 0
541 #endif
542 && name[RPRE_LEN] != '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
543 name += RPRE_LEN;
544 name_len -= RPRE_LEN;
545 }
546 #ifndef HAVE_LINUX_XATTRS
547 else {
548 /* Put everything else in the user namespace. */
549 name_len += UPRE_LEN;
550 }
551 #endif
552 write_varint(f, name_len);
553 write_varint(f, rxa->datum_len);
554 #ifndef HAVE_LINUX_XATTRS
555 if (name_len > rxa->name_len) {
556 write_buf(f, USER_PREFIX, UPRE_LEN);
557 name_len -= UPRE_LEN;
558 }
559 #endif
560 write_buf(f, name, name_len);
561 if (rxa->datum_len > MAX_FULL_DATUM)
562 write_buf(f, rxa->datum + 1, xattr_sum_len);
563 else
564 write_bigbuf(f, rxa->datum, rxa->datum_len);
565 }
566 ndx = rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
567 }
568
569 return ndx;
570 }
571
572 /* Return a flag indicating if we need to change a file's xattrs. If
573 * "find_all" is specified, also mark any abbreviated xattrs that we
574 * need so that send_xattr_request() can tell the sender about them. */
575 int xattr_diff(struct file_struct *file, stat_x *sxp, int find_all)
576 {
577 const rsync_xa_list *glst = rsync_xal_l.items;
578 const item_list *lst;
579 rsync_xa *snd_rxa, *rec_rxa;
580 int snd_cnt, rec_cnt;
581 int cmp, same, xattrs_equal = 1;
582
583 if (sxp && XATTR_READY(*sxp)) {
584 rec_rxa = sxp->xattr->items;
585 rec_cnt = sxp->xattr->count;
586 } else {
587 rec_rxa = NULL;
588 rec_cnt = 0;
589 }
590
591 if (F_XATTR(file) >= 0) {
592 glst += F_XATTR(file);
593 lst = &glst->xa_items;
594 } else
595 lst = &empty_xattr;
596
597 snd_rxa = lst->items;
598 snd_cnt = lst->count;
599
600 /* If the count of the sender's xattrs is different from our
601 * (receiver's) xattrs, the lists are not the same. */
602 if (snd_cnt != rec_cnt) {
603 if (!find_all)
604 return 1;
605 xattrs_equal = 0;
606 }
607
608 while (snd_cnt) {
609 cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
610 if (cmp > 0)
611 same = 0;
612 else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
613 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
614 && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
615 xattr_sum_len) == 0;
616 /* Flag unrequested items that we need. */
617 if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
618 snd_rxa->datum[0] = XSTATE_TODO;
619 } else {
620 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
621 && memcmp(snd_rxa->datum, rec_rxa->datum,
622 snd_rxa->datum_len) == 0;
623 }
624 if (!same) {
625 if (!find_all)
626 return 1;
627 xattrs_equal = 0;
628 }
629
630 if (cmp <= 0) {
631 snd_rxa++;
632 snd_cnt--;
633 }
634 if (cmp >= 0) {
635 rec_rxa++;
636 rec_cnt--;
637 }
638 }
639
640 if (rec_cnt)
641 xattrs_equal = 0;
642
643 return !xattrs_equal;
644 }
645
646 /* When called by the generator (with a NULL fname), this tells the sender
647 * all the abbreviated xattr values we need. When called by the sender
648 * (with a non-NULL fname), we send all the extra xattr data it needs.
649 * The generator may also call with f_out < 0 to just change all the
650 * XSTATE_ABBREV states into XSTATE_DONE. */
651 void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
652 {
653 const rsync_xa_list *glst = rsync_xal_l.items;
654 const item_list *lst;
655 int cnt, prior_req = 0;
656 rsync_xa *rxa;
657
658 glst += F_XATTR(file);
659 lst = &glst->xa_items;
660
661 for (rxa = lst->items, cnt = lst->count; cnt--; rxa++) {
662 if (rxa->datum_len <= MAX_FULL_DATUM)
663 continue;
664 switch (rxa->datum[0]) {
665 case XSTATE_ABBREV:
666 /* Items left abbreviated matched the sender's checksum, so
667 * the receiver will cache the local data for future use. */
668 if (am_generator)
669 rxa->datum[0] = XSTATE_DONE;
670 continue;
671 case XSTATE_TODO:
672 assert(f_out >= 0);
673 break;
674 default:
675 continue;
676 }
677
678 /* Flag that we handled this abbreviated item. */
679 rxa->datum[0] = XSTATE_DONE;
680
681 write_varint(f_out, rxa->num - prior_req);
682 prior_req = rxa->num;
683
684 if (fname) {
685 size_t len = 0;
686 char *ptr;
687
688 /* Re-read the long datum. */
689 if (!(ptr = get_xattr_data(fname, -1, rxa->name, &len, 0))) {
690 rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
691 write_varint(f_out, 0);
692 continue;
693 }
694
695 write_varint(f_out, len); /* length might have changed! */
696 write_bigbuf(f_out, ptr, len);
697 free(ptr);
698 }
699 }
700
701 if (f_out >= 0)
702 write_byte(f_out, 0); /* end the list */
703 }
704
705 /* When called by the sender, read the request from the generator and mark
706 * any needed xattrs with a flag that lets us know they need to be sent to
707 * the receiver. When called by the receiver, reads the sent data and
708 * stores it in place of its checksum. */
709 int recv_xattr_request(struct file_struct *file, int f_in)
710 {
711 const rsync_xa_list *glst = rsync_xal_l.items;
712 const item_list *lst;
713 char *old_datum, *name;
714 rsync_xa *rxa;
715 int rel_pos, cnt, num, got_xattr_data = 0;
716
717 if (F_XATTR(file) < 0) {
718 rprintf(FERROR, "recv_xattr_request: internal data error!\n");
719 exit_cleanup(RERR_PROTOCOL);
720 }
721 glst += F_XATTR(file);
722 lst = &glst->xa_items;
723
724 cnt = lst->count;
725 rxa = lst->items;
726 num = 0;
727 while ((rel_pos = read_varint(f_in)) != 0) {
728 /* Detect signed overflow before the accumulating add. A hostile
729 * peer could otherwise wrap 'num' to land on an arbitrary value. */
730 if ((rel_pos > 0 && num > INT_MAX - rel_pos)
731 || (rel_pos < 0 && num < INT_MIN - rel_pos)) {
732 rprintf(FERROR, "xattr rel_pos accumulation overflow [%s]\n", who_am_i());
733 exit_cleanup(RERR_PROTOCOL);
734 }
735 num += rel_pos;
736 if (am_sender) {
737 /* The sender-related num values are only in order on the sender.
738 * We use that order here to scan forward or backward as needed. */
739 if (rel_pos < 0) {
740 while (cnt < (int)lst->count && rxa->num > num) {
741 rxa--;
742 cnt++;
743 }
744 } else {
745 while (cnt > 1 && rxa->num < num) {
746 rxa++;
747 cnt--;
748 }
749 }
750 } else {
751 int j;
752 /* The receiving side has no known num order, so we just scan
753 * forward (w/wrap) and hope that the next value is near by. */
754 for (j = lst->count; j > 1 && rxa->num != num; j--) {
755 if (--cnt)
756 rxa++;
757 else {
758 cnt = lst->count;
759 rxa = lst->items;
760 }
761 }
762 }
763 if (!cnt || rxa->num != num) {
764 rprintf(FERROR, "[%s] could not find xattr #%d for %s\n",
765 who_am_i(), num, f_name(file, NULL));
766 exit_cleanup(RERR_PROTOCOL);
767 }
768 if (!XATTR_ABBREV(*rxa) || rxa->datum[0] != XSTATE_ABBREV) {
769 rprintf(FERROR, "[%s] internal abbrev error on %s (%s, len=%ld)!\n",
770 who_am_i(), f_name(file, NULL), rxa->name, (long)rxa->datum_len);
771 exit_cleanup(RERR_PROTOCOL);
772 }
773
774 if (am_sender) {
775 rxa->datum[0] = XSTATE_TODO;
776 continue;
777 }
778
779 old_datum = rxa->datum;
780 rxa->datum_len = read_varint_size(f_in, MAX_WIRE_XATTR_DATALEN, "xattr datum_len");
781 if (rxa->datum_len > MAX_XATTR_VALUE_BYTES) {
782 rprintf(FERROR, "xattr datum_len exceeds per-value limit [%s]\n", who_am_i());
783 exit_cleanup(RERR_PROTOCOL);
784 }
785
786 if (SIZE_MAX - rxa->name_len < rxa->datum_len)
787 overflow_exit("recv_xattr_request");
788 rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
789 name = rxa->datum + rxa->datum_len;
790 memcpy(name, rxa->name, rxa->name_len);
791 rxa->name = name;
792 free(old_datum);
793 read_buf(f_in, rxa->datum, rxa->datum_len);
794 got_xattr_data = 1;
795 }
796
797 return got_xattr_data;
798 }
799
800 /* ------------------------------------------------------------------------- */
801
802 /* receive and build the rsync_xattr_lists */
803 void receive_xattr(int f, struct file_struct *file)
804 {
805 static item_list temp_xattr = EMPTY_ITEM_LIST;
806 int count, num;
807 #ifdef HAVE_LINUX_XATTRS
808 int need_sort = 0;
809 #else
810 int need_sort = 1;
811 #endif
812 int ndx = read_varint(f);
813 size_t total_xattr_bytes = 0;
814
815 if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
816 rprintf(FERROR, "receive_xattr: xa index %d out of"
817 " range for %s\n", ndx, f_name(file, NULL));
818 exit_cleanup(RERR_STREAMIO);
819 }
820
821 if (ndx != 0) {
822 F_XATTR(file) = ndx - 1;
823 return;
824 }
825
826 count = read_varint_bounded(f, 0, MAX_WIRE_XATTR_COUNT, "xattr count");
827 if (count != 0) {
828 (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
829 temp_xattr.count = 0;
830 }
831
832 for (num = 1; num <= count; num++) {
833 char *ptr, *name;
834 rsync_xa *rxa;
835 size_t name_len = read_varint_size(f, MAX_WIRE_XATTR_NAMELEN, "xattr name_len");
836 size_t datum_len = read_varint_size(f, MAX_WIRE_XATTR_DATALEN, "xattr datum_len");
837 size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + (size_t)xattr_sum_len : datum_len;
838 size_t extra_len = MIGHT_NEED_RPRE ? RPRE_LEN : 0;
839 if (datum_len > MAX_XATTR_VALUE_BYTES) {
840 rprintf(FERROR, "xattr datum_len exceeds per-value limit [%s]\n", who_am_i());
841 exit_cleanup(RERR_PROTOCOL);
842 }
843 if (SIZE_MAX - total_xattr_bytes < name_len
844 || SIZE_MAX - total_xattr_bytes - name_len < datum_len) {
845 overflow_exit("receive_xattr");
846 }
847 total_xattr_bytes += name_len + datum_len;
848 if (total_xattr_bytes > MAX_XATTR_LIST_BYTES) {
849 rprintf(FERROR, "xattr list exceeds per-file limit [%s]\n", who_am_i());
850 exit_cleanup(RERR_PROTOCOL);
851 }
852 if (SIZE_MAX - dget_len < extra_len || SIZE_MAX - dget_len - extra_len < name_len)
853 overflow_exit("receive_xattr");
854 ptr = new_array(char, dget_len + extra_len + name_len);
855 name = ptr + dget_len + extra_len;
856 read_buf(f, name, name_len);
857 if (name_len < 1 || name[name_len-1] != '\0') {
858 rprintf(FERROR, "Invalid xattr name received (missing trailing \\0).\n");
859 exit_cleanup(RERR_FILEIO);
860 }
861 if (dget_len == datum_len)
862 read_buf(f, ptr, dget_len);
863 else {
864 *ptr = XSTATE_ABBREV;
865 read_buf(f, ptr + 1, xattr_sum_len);
866 }
867
868 if (saw_xattr_filter) {
869 if (name_is_excluded(name, NAME_IS_XATTR, ALL_FILTERS)) {
870 free(ptr);
871 continue;
872 }
873 }
874 #ifdef HAVE_LINUX_XATTRS
875 /* Non-root can only save the user namespace. */
876 if (am_root <= 0 && !HAS_PREFIX(name, USER_PREFIX)) {
877 if (!am_root && !saw_xattr_filter) {
878 free(ptr);
879 continue;
880 }
881 name -= RPRE_LEN;
882 name_len += RPRE_LEN;
883 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
884 need_sort = 1;
885 }
886 #else
887 /* This OS only has a user namespace, so we either
888 * strip the user prefix, or we put a non-user
889 * namespace inside our rsync hierarchy. */
890 if (HAS_PREFIX(name, USER_PREFIX)) {
891 name += UPRE_LEN;
892 name_len -= UPRE_LEN;
893 } else if (am_root) {
894 name -= RPRE_LEN;
895 name_len += RPRE_LEN;
896 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
897 } else {
898 free(ptr);
899 continue;
900 }
901 #endif
902 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
903 if (preserve_xattrs < 2 && name_len > RPRE_LEN
904 && name[RPRE_LEN] == '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
905 free(ptr);
906 continue;
907 }
908
909 rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
910 rxa->name = name;
911 rxa->datum = ptr;
912 rxa->name_len = name_len;
913 rxa->datum_len = datum_len;
914 rxa->num = num;
915 }
916
917 if (need_sort && temp_xattr.count > 1)
918 qsort(temp_xattr.items, temp_xattr.count, sizeof (rsync_xa), rsync_xal_compare_names);
919
920 ndx = rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
921
922 F_XATTR(file) = ndx;
923 }
924
925 /* Turn the xattr data in stat_x into cached xattr data, setting the index
926 * values in the file struct. */
927 void cache_tmp_xattr(struct file_struct *file, stat_x *sxp)
928 {
929 int ndx;
930
931 if (!sxp->xattr)
932 return;
933
934 if (prior_xattr_count == (size_t)-1)
935 prior_xattr_count = rsync_xal_l.count;
936 ndx = find_matching_xattr(sxp->xattr);
937 if (ndx < 0)
938 ndx = rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
939
940 F_XATTR(file) = ndx;
941 }
942
943 void uncache_tmp_xattrs(void)
944 {
945 if (prior_xattr_count != (size_t)-1) {
946 rsync_xa_list *xa_list_item = rsync_xal_l.items;
947 rsync_xa_list *xa_list_start = xa_list_item + prior_xattr_count;
948 xa_list_item += rsync_xal_l.count;
949 rsync_xal_l.count = prior_xattr_count;
950 while (xa_list_item-- > xa_list_start) {
951 struct ht_int64_node *node;
952 rsync_xa_list_ref *ref;
953
954 rsync_xal_free(&xa_list_item->xa_items);
955
956 if (rsync_xal_h == NULL)
957 continue;
958
959 node = hashtable_find(rsync_xal_h, xa_list_item->key, NULL);
960 if (node == NULL)
961 continue;
962
963 if (node->data == NULL)
964 continue;
965
966 ref = node->data;
967 if (xa_list_item->ndx == ref->ndx) {
968 /* xa_list_item is the first in the list. */
969 node->data = ref->next;
970 free(ref);
971 continue;
972 }
973
974 while (1) {
975 rsync_xa_list_ref *next = ref->next;
976 if (next == NULL)
977 break;
978 if (xa_list_item->ndx == next->ndx) {
979 ref->next = next->next;
980 free(next);
981 break;
982 }
983 ref = next;
984 }
985 }
986 prior_xattr_count = (size_t)-1;
987 }
988 }
989
990 /* Apply the wanted xattr set to fname. When fd >= 0, the list/set/remove ops
991 * that target fname go through that held fd (sys_f*xattr) so a parent-symlink
992 * race can't redirect them; the abbreviated-value compare still reads the basis
993 * via the fnamecmp path. fd < 0 keeps the path-based behavior. */
994 static int rsync_xal_set(const char *fname, item_list *xalp,
995 const char *fnamecmp, stat_x *sxp, int fd)
996 {
997 rsync_xa *rxas = xalp->items;
998 ssize_t list_len;
999 size_t i, len;
1000 char *name, *ptr, sum[MAX_XATTR_DIGEST_LEN];
1001 #ifdef HAVE_LINUX_XATTRS
1002 int user_only = am_root <= 0;
1003 #endif
1004 size_t name_len;
1005 int ret = 0;
1006
1007 /* This puts the current name list into the "namebuf" buffer. */
1008 if ((list_len = get_xattr_names(fname, fd)) < 0)
1009 return -1;
1010
1011 for (i = 0; i < xalp->count; i++) {
1012 name = rxas[i].name;
1013
1014 if (XATTR_ABBREV(rxas[i])) {
1015 /* See if the fnamecmp version is identical. */
1016 len = name_len = rxas[i].name_len;
1017 if ((ptr = get_xattr_data(fnamecmp, -1, name, &len, 1)) == NULL) {
1018 still_abbrev:
1019 if (am_generator)
1020 continue;
1021 rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
1022 rxas[i].name, full_fname(fname));
1023 ret = -1;
1024 continue;
1025 }
1026 if (len != rxas[i].datum_len) {
1027 free(ptr);
1028 goto still_abbrev;
1029 }
1030
1031 sum_init(xattr_sum_nni, checksum_seed);
1032 sum_update(ptr, len);
1033 sum_end(sum);
1034 if (memcmp(sum, rxas[i].datum + 1, xattr_sum_len) != 0) {
1035 free(ptr);
1036 goto still_abbrev;
1037 }
1038
1039 if (fname == fnamecmp)
1040 ; /* Value is already set when identical */
1041 else if ((fd >= 0 ? sys_fsetxattr(fd, name, ptr, len)
1042 : sys_lsetxattr(fname, name, ptr, len)) < 0) {
1043 rsyserr(FERROR_XFER, errno,
1044 "rsync_xal_set: %ssetxattr(%s,\"%s\") failed",
1045 fd >= 0 ? "f" : "l", full_fname(fname), name);
1046 ret = -1;
1047 } else /* make sure caller sets mtime */
1048 sxp->st.st_mtime = (time_t)-1;
1049
1050 if (am_generator) { /* generator items stay abbreviated */
1051 free(ptr);
1052 continue;
1053 }
1054
1055 memcpy(ptr + len, name, name_len);
1056 free(rxas[i].datum);
1057
1058 rxas[i].name = ptr + len;
1059 rxas[i].datum = ptr;
1060 continue;
1061 }
1062
1063 if ((fd >= 0 ? sys_fsetxattr(fd, name, rxas[i].datum, rxas[i].datum_len)
1064 : sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len)) < 0) {
1065 rsyserr(FERROR_XFER, errno,
1066 "rsync_xal_set: %ssetxattr(%s,\"%s\") failed",
1067 fd >= 0 ? "f" : "l", full_fname(fname), name);
1068 ret = -1;
1069 } else /* make sure caller sets mtime */
1070 sxp->st.st_mtime = (time_t)-1;
1071 }
1072
1073 /* Remove any extraneous names. */
1074 for (name = namebuf; list_len > 0; name += name_len) {
1075 name_len = strlen(name) + 1;
1076 list_len -= name_len;
1077
1078 if (saw_xattr_filter) {
1079 if (name_is_excluded(name, NAME_IS_XATTR, ALL_FILTERS))
1080 continue;
1081 }
1082 #ifdef HAVE_LINUX_XATTRS
1083 /* Choose between ignoring the system namespace or (non-root) ignoring any non-user namespace. */
1084 else if (user_only ? !HAS_PREFIX(name, USER_PREFIX) : HAS_PREFIX(name, SYSTEM_PREFIX))
1085 continue;
1086 #endif
1087 if (am_root < 0 && name_len > RPRE_LEN && name[RPRE_LEN] == '%' && strcmp(name, XSTAT_ATTR) == 0)
1088 continue;
1089
1090 for (i = 0; i < xalp->count; i++) {
1091 if (strcmp(name, rxas[i].name) == 0)
1092 break;
1093 }
1094 if (i == xalp->count) {
1095 if ((fd >= 0 ? sys_fremovexattr(fd, name)
1096 : sys_lremovexattr(fname, name)) < 0) {
1097 rsyserr(FERROR_XFER, errno,
1098 "rsync_xal_set: %sremovexattr(%s,\"%s\") failed",
1099 fd >= 0 ? "f" : "l", full_fname(fname), name);
1100 ret = -1;
1101 } else /* make sure caller sets mtime */
1102 sxp->st.st_mtime = (time_t)-1;
1103 }
1104 }
1105
1106 return ret;
1107 }
1108
1109 /* Set extended attributes on indicated filename. When fd >= 0 it is a held,
1110 * O_NOFOLLOW-opened fd for fname (a regular file or directory) and the xattr
1111 * ops go through it (fsetxattr/fremovexattr/flistxattr) so a parent-symlink
1112 * race can't redirect them; fd < 0 keeps the path-based behavior (symlinks,
1113 * special files, non-hardened receivers, platforms without the f-variants). */
1114 int set_xattr(const char *fname, const struct file_struct *file, const char *fnamecmp, stat_x *sxp, int fd)
1115 {
1116 rsync_xa_list *glst = rsync_xal_l.items;
1117 item_list *lst;
1118 int ndx, added_write_perm = 0;
1119
1120 if (dry_run)
1121 return 1; /* FIXME: --dry-run needs to compute this value */
1122
1123 if (read_only || list_only) {
1124 errno = EROFS;
1125 return -1;
1126 }
1127
1128 #ifdef NO_SPECIAL_XATTRS
1129 if (IS_SPECIAL(sxp->st.st_mode)) {
1130 errno = ENOTSUP;
1131 return -1;
1132 }
1133 #endif
1134 #ifdef NO_DEVICE_XATTRS
1135 if (IS_DEVICE(sxp->st.st_mode)) {
1136 errno = ENOTSUP;
1137 return -1;
1138 }
1139 #endif
1140 #ifdef NO_SYMLINK_XATTRS
1141 if (S_ISLNK(sxp->st.st_mode)) {
1142 errno = ENOTSUP;
1143 return -1;
1144 }
1145 #endif
1146
1147 /* If the target file lacks write permission, we try to add it
1148 * temporarily so we can change the extended attributes. With a held fd
1149 * the perm change goes through fchmod() on the pinned inode rather than
1150 * re-resolving the path. */
1151 if (!am_root
1152 #ifdef SUPPORT_LINKS
1153 && !S_ISLNK(sxp->st.st_mode)
1154 #endif
1155 && access(fname, W_OK) < 0
1156 && (fd >= 0 ? fchmod(fd, (sxp->st.st_mode & CHMOD_BITS) | S_IWUSR)
1157 : do_chmod_at(fname, (sxp->st.st_mode & CHMOD_BITS) | S_IWUSR)) == 0)
1158 added_write_perm = 1;
1159
1160 ndx = F_XATTR(file);
1161 if (ndx < 0) {
1162 /* No cached xattr list for this file (e.g. the cache_tmp_xattr()
1163 * path on --backup); match the guards at the other F_XATTR()
1164 * consumers rather than indexing rsync_xal_l.items[-1]. */
1165 if (added_write_perm) {
1166 if (fd >= 0)
1167 fchmod(fd, sxp->st.st_mode);
1168 else
1169 do_chmod_at(fname, sxp->st.st_mode);
1170 }
1171 return 0;
1172 }
1173 glst += ndx;
1174 lst = &glst->xa_items;
1175 int return_value = rsync_xal_set(fname, lst, fnamecmp, sxp, fd);
1176 if (added_write_perm) { /* remove the temporary write permission */
1177 if (fd >= 0)
1178 fchmod(fd, sxp->st.st_mode);
1179 else
1180 do_chmod_at(fname, sxp->st.st_mode);
1181 }
1182 return return_value;
1183 }
1184
1185 #ifdef SUPPORT_ACLS
1186 char *get_xattr_acl(const char *fname, int fd, int is_access_acl, size_t *len_p)
1187 {
1188 const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
1189 *len_p = 0; /* no extra data alloc needed from get_xattr_data() */
1190 return get_xattr_data(fname, fd, name, len_p, 1);
1191 }
1192
1193 /* Store/delete a --fake-super ACL-as-xattr. When fd >= 0 (a held O_NOFOLLOW fd
1194 * from set_file_attrs) use the f-variant so a parent-symlink race can't redirect
1195 * the write; otherwise fall back to the l-variant on the path. */
1196 int set_xattr_acl(int fd, const char *fname, int is_access_acl, const char *buf, size_t buf_len)
1197 {
1198 const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
1199 if ((fd >= 0 ? sys_fsetxattr(fd, name, buf, buf_len)
1200 : sys_lsetxattr(fname, name, buf, buf_len)) < 0) {
1201 rsyserr(FERROR_XFER, errno,
1202 "set_xattr_acl: %ssetxattr(%s,\"%s\") failed",
1203 fd >= 0 ? "f" : "l", full_fname(fname), name);
1204 return -1;
1205 }
1206 return 0;
1207 }
1208
1209 int del_def_xattr_acl(int fd, const char *fname)
1210 {
1211 return fd >= 0 ? sys_fremovexattr(fd, XDEF_ACL_ATTR)
1212 : sys_lremovexattr(fname, XDEF_ACL_ATTR);
1213 }
1214 #endif
1215
1216 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
1217 {
1218 unsigned int mode;
1219 int rdev_major, rdev_minor, uid, gid, len;
1220 char buf[256];
1221
1222 if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
1223 return -1;
1224
1225 if (xst)
1226 *xst = *fst;
1227 else
1228 xst = fst;
1229 if (fname) {
1230 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
1231 } else {
1232 fname = "fd";
1233 len = sys_fgetxattr(fd, XSTAT_ATTR, buf, sizeof buf - 1);
1234 }
1235 if (len >= (int)sizeof buf) {
1236 len = -1;
1237 errno = ERANGE;
1238 }
1239 if (len < 0) {
1240 if (errno == ENOTSUP || errno == ENOATTR)
1241 return -1;
1242 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
1243 xst->st_uid = 0;
1244 xst->st_gid = 0;
1245 return 0;
1246 }
1247 rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
1248 XSTAT_ATTR, full_fname(fname));
1249 return -1;
1250 }
1251 buf[len] = '\0';
1252
1253 if (sscanf(buf, "%o %d,%d %d:%d",
1254 &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
1255 rprintf(FERROR, "Corrupt %s xattr attached to %s: \"%s\"\n",
1256 XSTAT_ATTR, full_fname(fname), buf);
1257 exit_cleanup(RERR_FILEIO);
1258 }
1259
1260 xst->st_mode = from_wire_mode(mode);
1261 xst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
1262 xst->st_uid = uid;
1263 xst->st_gid = gid;
1264
1265 return 0;
1266 }
1267
1268 /* Store the fake-super %stat metadata on fname. When fd >= 0 it is a held,
1269 * O_NOFOLLOW-opened fd for fname (regular file or directory): the re-stat, the
1270 * permission reset and the xattr write/remove go through it so a parent-symlink
1271 * race can't redirect them. fd < 0 keeps the path-based behavior (symlinks,
1272 * devices/specials, non-hardened receivers, platforms without the f-variants). */
1273 int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode, int fd)
1274 {
1275 STRUCT_STAT fst, xst;
1276 dev_t rdev;
1277 mode_t mode, fmode;
1278
1279 if (dry_run)
1280 return 0;
1281
1282 if (read_only || list_only) {
1283 rsyserr(FERROR_XFER, EROFS, "failed to write xattr %s for %s",
1284 XSTAT_ATTR, full_fname(fname));
1285 return -1;
1286 }
1287
1288 if (fd >= 0) {
1289 if (do_fstat(fd, &fst) < 0) {
1290 rsyserr(FERROR_XFER, errno, "failed to re-stat %s",
1291 full_fname(fname));
1292 return -1;
1293 }
1294 if (get_stat_xattr(NULL, fd, &fst, &xst) < 0) {
1295 xst = fst; /* keep xst fully defined; st_mode=0 means "no stat xattr" */
1296 xst.st_mode = 0;
1297 }
1298 } else if (x_lstat(fname, &fst, &xst) < 0) {
1299 rsyserr(FERROR_XFER, errno, "failed to re-stat %s",
1300 full_fname(fname));
1301 return -1;
1302 }
1303
1304 fst.st_mode &= (_S_IFMT | CHMOD_BITS);
1305 fmode = new_mode & (_S_IFMT | CHMOD_BITS);
1306
1307 if (IS_DEVICE(fmode)) {
1308 uint32 *devp = F_RDEV_P(file);
1309 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1310 } else
1311 rdev = 0;
1312
1313 /* Dump the special permissions and enable full owner access. */
1314 mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
1315 | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
1316 if (fst.st_mode != mode) {
1317 if (fd >= 0)
1318 fchmod(fd, mode);
1319 else
1320 do_chmod_at(fname, mode);
1321 }
1322 if (!IS_DEVICE(fst.st_mode))
1323 fst.st_rdev = 0; /* just in case */
1324
1325 if (mode == fmode && fst.st_rdev == rdev
1326 && fst.st_uid == F_OWNER(file) && fst.st_gid == F_GROUP(file)) {
1327 /* xst.st_mode will be 0 if there's no current stat xattr */
1328 if (xst.st_mode && (fd >= 0 ? sys_fremovexattr(fd, XSTAT_ATTR)
1329 : sys_lremovexattr(fname, XSTAT_ATTR)) < 0) {
1330 rsyserr(FERROR_XFER, errno,
1331 "delete of stat xattr failed for %s",
1332 full_fname(fname));
1333 return -1;
1334 }
1335 return 0;
1336 }
1337
1338 if (xst.st_mode != fmode || xst.st_rdev != rdev
1339 || xst.st_uid != F_OWNER(file) || xst.st_gid != F_GROUP(file)) {
1340 char buf[256];
1341 int len = snprintf(buf, sizeof buf, "%o %u,%u %u:%u",
1342 to_wire_mode(fmode),
1343 (int)major(rdev), (int)minor(rdev),
1344 F_OWNER(file), F_GROUP(file));
1345 if ((fd >= 0 ? sys_fsetxattr(fd, XSTAT_ATTR, buf, len)
1346 : sys_lsetxattr(fname, XSTAT_ATTR, buf, len)) < 0) {
1347 if (errno == EPERM && S_ISLNK(fst.st_mode))
1348 return 0;
1349 rsyserr(FERROR_XFER, errno,
1350 "failed to write xattr %s for %s",
1351 XSTAT_ATTR, full_fname(fname));
1352 return -1;
1353 }
1354 }
1355
1356 return 0;
1357 }
1358
1359 int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1360 {
1361 /* Use the *_at variants so that on a daemon-no-chroot deployment
1362 * the metadata read goes through a secure parent dirfd instead
1363 * of bare path resolution. The *_at wrappers fall through to
1364 * plain do_stat outside the daemon-no-chroot context, so this
1365 * change is transparent for non-daemon use. */
1366 int ret = do_stat_at(fname, fst);
1367 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1368 xst->st_mode = 0;
1369 return ret;
1370 }
1371
1372 int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1373 {
1374 int ret = do_lstat_at(fname, fst);
1375 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1376 xst->st_mode = 0;
1377 return ret;
1378 }
1379
1380 int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
1381 {
1382 int ret = do_fstat(fd, fst);
1383 if ((ret < 0 || get_stat_xattr(NULL, fd, fst, xst) < 0) && xst)
1384 xst->st_mode = 0;
1385 return ret;
1386 }
1387
1388 #endif /* SUPPORT_XATTRS */