From d30a8faa42b36651194de4dc8dfebac99718db28 Mon Sep 17 00:00:00 2001 From: Eric Borisch Date: Mon, 4 Jun 2018 22:13:43 -0500 Subject: [PATCH] Check if root before operations. No overall change to the code path when root; when not root, ensure created files are initially user writable if we need to do XATTRs or HFS compression. Mode is fixed later to the desired final mode in this case. Net of one extra syscall per file to fix the mode at the end only when all of these are true: * Not root * Final mode is not owner-writable * XATTRs or HFS compression are needed. These changes make it unexpectedly pass the xattr test on freebsd. --- libarchive/archive_write_disk_posix.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/libarchive/archive_write_disk_posix.c b/libarchive/archive_write_disk_posix.c index 101fce927..003e17d77 100644 --- a/libarchive/archive_write_disk_posix.c +++ b/libarchive/archive_write_disk_posix.c @@ -1712,7 +1712,8 @@ _archive_write_disk_finish_entry(struct archive *_a) * which may be the state after set_mode(). Perform * set_xattrs() first based on these constraints. */ - if (a->user_uid && (a->todo & TODO_XATTR)) { + if (a->user_uid != 0 && + (a->todo & TODO_XATTR)) { int r2 = set_xattrs(a); if (r2 < ret) ret = r2; } @@ -1732,7 +1733,8 @@ _archive_write_disk_finish_entry(struct archive *_a) * since they're implicitly removed by other file changes. * We do this last only when root. */ - if (!a->user_uid && (a->todo & TODO_XATTR)) { + if (a->user_uid == 0 && + (a->todo & TODO_XATTR)) { int r2 = set_xattrs(a); if (r2 < ret) ret = r2; } @@ -2237,9 +2239,12 @@ create_filesystem_object(struct archive_write_disk *a) */ mode = final_mode & 0777 & ~a->user_umask; - if (a->todo & (TODO_HFS_COMPRESSION | - TODO_XATTR)) { - /* Always create writable such that [f]setxattr() works */ + /* + * Always create writable such that [f]setxattr() works if we're not + * root. + */ + if (a->user_uid != 0 && + a->todo & (TODO_HFS_COMPRESSION | TODO_XATTR)) { mode |= 0200; } -- 2.47.2