+--- old/t_unsafe.c
++++ new/t_unsafe.c
+@@ -23,7 +23,11 @@
+
+ #include "rsync.h"
+
+-int dry_run, read_only, list_only, verbose;
++int dry_run = 0;
++int am_root = 0;
++int read_only = 0;
++int list_only = 0;
++int verbose = 0;
+ int preserve_perms = 0;
+
+ int
+--- old/tls.c
++++ new/tls.c
+@@ -39,6 +39,7 @@
+
+ /* These are to make syscall.o shut up. */
+ int dry_run = 0;
++int am_root = 0;
+ int read_only = 1;
+ int list_only = 0;
+ int preserve_perms = 0;
+--- old/trimslash.c
++++ new/trimslash.c
+@@ -22,6 +22,7 @@
+
+ /* These are to make syscall.o shut up. */
+ int dry_run = 0;
++int am_root = 0;
+ int read_only = 1;
+ int list_only = 0;
+ int preserve_perms = 0;
+--- old/xattrs.c
++++ new/xattrs.c
+@@ -53,11 +53,16 @@ extern int checksum_seed;
+ #define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
+
+ #ifdef HAVE_LINUX_XATTRS
+-#define RPRE_LEN 0
++#define MIGHT_NEED_RPRE (am_root < 0)
++#define RSYNC_PREFIX USER_PREFIX "rsync."
+ #else
++#define MIGHT_NEED_RPRE am_root
+ #define RSYNC_PREFIX "rsync."
+-#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
+ #endif
++#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
++
++#define XSTAT_ATTR RSYNC_PREFIX "%stat"
++#define XSTAT_LEN ((int)sizeof XSTAT_ATTR - 1)
+
+ typedef struct {
+ char *datum, *name;
+@@ -218,6 +223,10 @@ static int rsync_xal_get(const char *fna
+ continue;
+ #endif
+
++ if (am_root < 0 && name_len == XSTAT_LEN + 1
++ && name[RPRE_LEN] == '%' && strcmp(name, XSTAT_ATTR) == 0)
++ continue;
++
+ datum_len = name_len; /* Pass extra size to get_xattr_data() */
+ if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
+ return -1;
+@@ -236,6 +245,14 @@ static int rsync_xal_get(const char *fna
+ } else
+ name_offset = datum_len;
+
++#ifdef HAVE_LINUX_XATTRS
++ if (am_root < 0 && name_len > RPRE_LEN
++ && HAS_PREFIX(name, RSYNC_PREFIX)) {
++ name += RPRE_LEN;
++ name_len -= RPRE_LEN;
++ }
++#endif
++
+ rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
+ rxas->name = ptr + name_offset;
+ memcpy(rxas->name, name, name_len);
+@@ -576,13 +593,9 @@ void receive_xattr(struct file_struct *f
+ size_t name_len = read_abbrevint(f);
+ size_t datum_len = read_abbrevint(f);
+ size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
+-#ifdef HAVE_LINUX_XATTRS
+- size_t extra_len = 0;
+-#else
+- size_t extra_len = am_root ? RPRE_LEN : 0;
++ size_t extra_len = MIGHT_NEED_RPRE ? RPRE_LEN : 0;
+ if (dget_len + extra_len < dget_len)
+ out_of_memory("receive_xattr"); /* overflow */
+-#endif
+ if (dget_len + extra_len + name_len < dget_len)
+ out_of_memory("receive_xattr"); /* overflow */
+ ptr = new_array(char, dget_len + extra_len + name_len);
+@@ -598,9 +611,14 @@ void receive_xattr(struct file_struct *f
+ }
+ #ifdef HAVE_LINUX_XATTRS
+ /* Non-root can only save the user namespace. */
+- if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
+- free(ptr);
+- continue;
++ if (am_root <= 0 && !HAS_PREFIX(name, USER_PREFIX)) {
++ if (!am_root) {
++ free(ptr);
++ continue;
++ }
++ name -= RPRE_LEN;
++ name_len += RPRE_LEN;
++ memcpy(name, RSYNC_PREFIX, RPRE_LEN);
+ }
+ #else
+ /* This OS only has a user namespace, so we either
+@@ -618,6 +636,11 @@ void receive_xattr(struct file_struct *f
+ continue;
+ }