HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/tar-util.c
Git blob: 8d6306a20d6dbbd4054199b130c7fa24d02d2fd7
Lines 1–1000 of 1,682
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "errno-util.h"
#include "log.h"
#include "tar-util.h"
#if HAVE_LIBARCHIVE
#include <sys/mount.h>
#include <sys/sysmacros.h>
#include "acl-util.h"
#include "alloc-util.h"
#include "chase.h"
#include "chattr-util.h"
#include "fd-util.h"
#include "fs-util.h"
#include "iovec-util.h"
#include "libarchive-util.h"
#include "mountpoint-util.h"
#include "nsresource.h"
#include "nulstr-util.h"
#include "path-util.h"
#include "recurse-dir.h"
#include "rm-rf.h"
#include "sha256.h"
#include "stat-util.h"
#include "string-util.h"
#include "tmpfile-util.h"
#include "user-util.h"
#include "xattr-util.h"
#define DEPTH_MAX 128U
/* We are a bit conservative with the flags we save/restore in tar files */
#define CHATTR_TAR_FL \
(FS_NOATIME_FL | \
FS_NOCOW_FL | \
FS_PROJINHERIT_FL | \
FS_NODUMP_FL | \
FS_SYNC_FL | \
FS_DIRSYNC_FL)
typedef struct XAttr {
char *name;
struct iovec data;
} XAttr;
typedef struct OpenInode {
int fd;
char *path;
/* File properties to apply when we are done with the inode, i.e. right before closing it */
mode_t filetype;
mode_t mode;
struct timespec mtime;
uid_t uid;
gid_t gid;
unsigned fflags;
XAttr *xattr;
size_t n_xattr;
acl_t acl_access, acl_default;
} OpenInode;
static void xattr_done(XAttr *xa) {
assert(xa);
free(xa->name);
iovec_done(&xa->data);
}
static DEFINE_ARRAY_FREE_FUNC(xattr_free_array, XAttr, xattr_done);
static void open_inode_done(OpenInode *of) {
assert(of);
if (of->path) {
/* Only close the stored fd if the path field is set. We'll set the path to NULL for the root
* inode, and we don't want the fd for that closed, as it's owned by the caller. */
of->fd = safe_close(of->fd);
of->path = mfree(of->path);
}
xattr_free_array(of->xattr, of->n_xattr);
of->xattr = NULL;
of->n_xattr = 0;
#if HAVE_ACL
acl_freep(&of->acl_access);
acl_freep(&of->acl_default);
#endif
}
static DEFINE_ARRAY_FREE_FUNC(open_inode_free_array, OpenInode, open_inode_done);
static int open_inode_apply_acl(OpenInode *of) {
int r = 0;
assert(of);
assert(of->fd >= 0);
if (!inode_type_can_acl(of->filetype))
return 0;
if (of->acl_access) {
#if HAVE_ACL
if (sym_acl_set_fd(of->fd, of->acl_access) < 0)
RET_GATHER(r, log_error_errno(errno, "Failed to adjust ACLs of '%s': %m", of->path));
#else
log_debug("The archive entry '%s' has ACLs, but ACL support is disabled.", of->path);
#endif
}
if (of->filetype == S_IFDIR && of->acl_default) {
#if HAVE_ACL
/* There's no API to set default ACLs by fd, hence go by /proc/self/fd/ path */
if (sym_acl_set_file(FORMAT_PROC_FD_PATH(of->fd), ACL_TYPE_DEFAULT, of->acl_default) < 0)
RET_GATHER(r, log_error_errno(errno, "Failed to adjust default ACLs of '%s': %m", of->path));
#else
log_debug("The archive entry '%s' has default ACLs, but ACL support is disabled.", of->path);
#endif
}
return r;
}
static int open_inode_finalize(OpenInode *of) {
int r = 0;
assert(of);
if (of->fd >= 0) {
int k;
/* We adjust the UID/GID right before the mode, since doing this might affect the mode (drops
* suid/sgid bits).
*
* We adjust the mode only when leaving a dir, because if we are unprivileged we might lose
* the ability to enter it once we do this. */
if (uid_is_valid(of->uid) || gid_is_valid(of->gid) || of->mode != MODE_INVALID) {
k = fchmod_and_chown_with_fallback(of->fd, /* path= */ NULL, of->mode, of->uid, of->gid);
if (k < 0)
RET_GATHER(r, log_error_errno(k, "Failed to adjust ownership/mode of '%s': %m", of->path));
}
k = open_inode_apply_acl(of);
if (k < 0)
RET_GATHER(r, log_error_errno(k, "Failed to adjust ACL of '%s': %m", of->path));
if ((of->fflags & ~CHATTR_EARLY_FL) != 0 && inode_type_can_chattr(of->filetype)) {
k = chattr_full(of->fd,
/* path= */ NULL,
/* value= */ of->fflags,
/* mask= */ of->fflags & ~CHATTR_EARLY_FL,
/* ret_previous= */ NULL,
/* ret_final= */ NULL,
CHATTR_FALLBACK_BITWISE);
if (ERRNO_IS_NEG_NOT_SUPPORTED(k))
log_warning_errno(k, "Failed to apply chattr of '%s', ignoring: %m", of->path);
else if (k < 0)
RET_GATHER(r, log_error_errno(k, "Failed to adjust chattr of '%s': %m", of->path));
}
/* We also adjust the mtime only after leaving a dir, since it might otherwise change again
* because we make modifications inside it */
if (of->mtime.tv_nsec != UTIME_OMIT) {
k = futimens_opath(of->fd, (const struct timespec[2]) {
{ .tv_nsec = UTIME_OMIT },
of->mtime,
});
if (k < 0)
RET_GATHER(r, log_error_errno(k, "Failed to adjust mtime of '%s': %m", of->path));
}
/* Setting certain xattrs might cause us to lose access to the inode, hence set this last */
FOREACH_ARRAY(i, of->xattr, of->n_xattr) {
k = xsetxattr_full(
of->fd,
/* path= */ NULL,
AT_EMPTY_PATH,
i->name,
i->data.iov_base,
i->data.iov_len,
/* xattr_flags= */ 0);
if (k < 0)
RET_GATHER(r, log_error_errno(k, "Failed to set xattr '%s' of '%s': %m", i->name, of->path));
}
}
open_inode_done(of); /* free this item even on failure */
return r;
}
static int open_inode_finalize_many(OpenInode **array, size_t *n) {
int r = 0;
assert(array);
assert(n);
assert(*array || *n == 0);
/* Go backwards, so that we adjust innermost first */
for (size_t i = *n; i > 0; i--)
RET_GATHER(r, open_inode_finalize(*array + i - 1));
*array = mfree(*array);
*n = 0;
return r;
}
static const struct {
int libarchive;
acl_tag_t libacl;
} acl_tag_map[] = {
{ ARCHIVE_ENTRY_ACL_USER, ACL_USER },
{ ARCHIVE_ENTRY_ACL_GROUP, ACL_GROUP },
{ ARCHIVE_ENTRY_ACL_USER_OBJ, ACL_USER_OBJ },
{ ARCHIVE_ENTRY_ACL_GROUP_OBJ, ACL_GROUP_OBJ },
{ ARCHIVE_ENTRY_ACL_MASK, ACL_MASK },
{ ARCHIVE_ENTRY_ACL_OTHER, ACL_OTHER },
};
static acl_tag_t libarchive_acl_tag_to_libacl_tag(int libarchive_tag) {
FOREACH_ELEMENT(t, acl_tag_map)
if (t->libarchive == libarchive_tag)
return t->libacl;
return ACL_UNDEFINED_TAG;
}
#if HAVE_ACL
#define _ARCHIVE_ENTRY_ACL_UNDEFINED 0
static int libacl_tag_to_libarchive_acl_tag(acl_tag_t libacl_tag) {
FOREACH_ELEMENT(t, acl_tag_map)
if (t->libacl == libacl_tag)
return t->libarchive;
return _ARCHIVE_ENTRY_ACL_UNDEFINED;
}
#endif
static int archive_unpack_regular(
struct archive *a,
struct archive_entry *entry,
int parent_fd,
const char *filename,
const char *path,
unsigned fflags) {
int r;
assert(a);
assert(entry);
assert(parent_fd >= 0);
assert(filename);
assert(path);
_cleanup_free_ char *tmp = NULL;
_cleanup_close_ int fd = open_tmpfile_linkable_at(parent_fd, filename, O_CLOEXEC|O_WRONLY, &tmp);
if (fd < 0)
return log_error_errno(fd, "Failed to create regular file '%s': %m", path);
CLEANUP_TMPFILE_AT(parent_fd, tmp);
if ((fflags & CHATTR_EARLY_FL) != 0) {
r = chattr_full(fd,
/* path= */ NULL,
/* value= */ fflags,
/* mask= */ fflags & CHATTR_EARLY_FL,
/* ret_previous= */ NULL,
/* ret_final= */ NULL,
CHATTR_FALLBACK_BITWISE);
if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
log_warning_errno(r, "Failed to apply chattr of '%s', ignoring: %m", path);
else if (r < 0)
return log_error_errno(r, "Failed to adjust chattr of '%s': %m", path);
}
r = sym_archive_read_data_into_fd(a, fd);
if (r != ARCHIVE_OK)
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
"Failed to unpack regular file '%s': %s", path, sym_archive_error_string(a));
/* If this is a sparse file, then libarchive's archive_read_data_into_fd() won't insert the final
* hole. We need to manually truncate. */
off_t l = lseek(fd, 0, SEEK_CUR);
if (l < 0)
return log_error_errno(errno, "Failed to determine current file position in '%s': %m", path);
if (ftruncate(fd, l) < 0)
return log_error_errno(errno, "Failed to truncate regular file '%s' to %" PRIu64 ": %m", path, (uint64_t) l);
r = link_tmpfile_at(fd, parent_fd, tmp, filename, LINK_TMPFILE_REPLACE);
if (r < 0)
return log_error_errno(r, "Failed to install regular file '%s': %m", path);
tmp = mfree(tmp); /* disarm CLEANUP_TMPFILE_AT() */
return TAKE_FD(fd);
}
static int overlayfs_fsetfattr(
const char *path, /* purely decorative, for log purposes */
int fd,
const char *name, /* xattr key name */
const char *value /* xattr value */) {
int r;
assert(fd >= 0);
assert(path);
assert(name);
assert(value);
/* overlayfs knows magic {user|trusted}.overlay.* xattrs for whiteouts and opaque directories. The
* 'user.overlay.*' ones are only checked if overlayfs is mounted with "userxattr". We only set that
* one because we want to operate unprivileged. Ideally, we'd set both here, to maximize the chance
* that things work both in privileged and unprivileged scenarios, but unfortunately this has the
* effect that the privileged ones are ignored (and visible in the overlayfs mount). */
_cleanup_free_ char *n = strjoin("user.overlay.", name);
if (!n)
return log_oom();
r = xsetxattr(fd, /* path= */ NULL, AT_EMPTY_PATH, n, value);
if (r < 0)
return log_error_errno(r, "Failed to set '%s' xattr on file '%s': %m", n, path);
return 0;
}
static int archive_unpack_whiteout(
struct archive *a,
struct archive_entry *entry,
int parent_fd,
const char *parent_path, /* Full path of 'parent_fd', purely decorative for log purposes */
const char *filename, /* Just the filename we are supposed to whiteout */
const char *path /* Full path of the whiteout file, purely decorative for log purposes */) {
int r;
assert(a);
assert(entry);
assert(parent_fd >= 0);
assert(parent_path);
assert(filename);
assert(path);
_cleanup_free_ char *tmp = NULL;
_cleanup_close_ int fd = open_tmpfile_linkable_at(parent_fd, filename, O_CLOEXEC|O_WRONLY, &tmp);
if (fd < 0)
return log_error_errno(fd, "Failed to create whiteout file for '%s': %m", path);
CLEANUP_TMPFILE_AT(parent_fd, tmp);
r = overlayfs_fsetfattr(path, fd, "whiteout", "y");
if (r < 0)
return r;
/* As per https://docs.kernel.org/filesystems/overlayfs.html also mark the parent */
r = overlayfs_fsetfattr(parent_path, parent_fd, "opaque", "x");
if (r < 0)
return r;
r = link_tmpfile_at(fd, parent_fd, tmp, filename, LINK_TMPFILE_REPLACE);
if (r < 0)
return log_error_errno(r, "Failed to install regular file '%s': %m", path);
tmp = mfree(tmp); /* disarm CLEANUP_TMPFILE_AT */
return 0; /* we do not return an fd here, because this kills an inode, and doesn't synthesize one */
}
static int archive_unpack_opaque(
struct archive *a,
struct archive_entry *entry,
int parent_fd,
const char *parent_path) {
assert(a);
assert(entry);
assert(parent_fd >= 0);
assert(parent_path);
/* we do not return an fd here either */
return overlayfs_fsetfattr(parent_path, parent_fd, "opaque", "y");
}
static int archive_unpack_directory(
struct archive *a,
struct archive_entry *entry,
int parent_fd,
const char *filename,
const char *path,
unsigned fflags) {
int r;
assert(a);
assert(entry);
assert(parent_fd >= 0);
assert(filename);
assert(path);
/* For the other inode types we operate in an atomic replace fashion, but not for the directories,
* they are more of a "shared" concept, and we try to reuse existing inodes. Note that we create the
* dir inode in mode 0700, so that we can fully access it (but others cannot). We'll adjust the modes
* right before closing the inode. */
_cleanup_close_ int fd = open_mkdir_at(parent_fd, filename, O_CLOEXEC, 0700);
if (fd < 0)
return log_error_errno(fd, "Failed to create directory '%s': %m", path);
if ((fflags & CHATTR_EARLY_FL) != 0) {
r = chattr_full(fd,
/* path= */ NULL,
/* value= */ fflags,
/* mask= */ fflags & CHATTR_EARLY_FL,
/* ret_previous= */ NULL,
/* ret_final= */ NULL,
CHATTR_FALLBACK_BITWISE);
if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
log_warning_errno(r, "Failed to apply chattr of '%s', ignoring: %m", path);
else if (r < 0)
return log_error_errno(r, "Failed to adjust chattr of '%s': %m", path);
}
return TAKE_FD(fd);
}
static int archive_unpack_symlink(
struct archive *a,
struct archive_entry *entry,
int parent_fd,
const char *filename,
const char *path) {
int r;
assert(a);
assert(entry);
assert(parent_fd >= 0);
assert(filename);
assert(path);
const char *target = sym_archive_entry_symlink(entry);
if (!target)
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Failed to get symlink target for '%s': %m", path);
r = symlinkat_atomic_full(target, parent_fd, filename, /* flags= */ 0);
if (r < 0)
return log_error_errno(r, "Failed to create symlink '%s' → '%s': %m", path, target);
_cleanup_close_ int fd = openat(parent_fd, filename, O_CLOEXEC|O_PATH|O_NOFOLLOW);
if (fd < 0)
return log_error_errno(errno, "Failed to open symlink '%s' we just created: %m", path);
r = fd_verify_symlink(fd);
if (r < 0)
return log_error_errno(r, "Symlink '%s' we just created is not a symlink: %m", path);
return TAKE_FD(fd);
}
static int archive_unpack_special_inode(
struct archive *a,
struct archive_entry *entry,
int parent_fd,
const char *filename,
const char *path,
mode_t filetype) {
int r;
assert(a);
assert(entry);
assert(parent_fd >= 0);
assert(filename);
assert(path);
dev_t major = 0, minor = 0;
if (IN_SET(filetype, S_IFCHR, S_IFBLK)) {
major = sym_archive_entry_rdevmajor(entry);
minor = sym_archive_entry_rdevminor(entry);
}
r = mknodat_atomic(parent_fd, filename, filetype | 0000, makedev(major, minor));
if (r < 0)
return log_error_errno(r, "Failed to create special node '%s': %m", path);
_cleanup_close_ int fd = openat(parent_fd, filename, O_CLOEXEC|O_PATH|O_NOFOLLOW);
if (fd < 0)
return log_error_errno(errno, "Failed to open special node '%s' we just created: %m", path);
struct stat st;
if (fstat(fd, &st) < 0)
return log_error_errno(errno, "Failed to fstat() '%s': %m", path);
if (((st.st_mode ^ filetype) & S_IFMT) != 0)
return log_error_errno(SYNTHETIC_ERRNO(ENODEV),
"Special node '%s' we just created is of a wrong type: %m", path);
return TAKE_FD(fd);
}
static int archive_entry_pathname_safe(struct archive_entry *entry, const char **ret) {
/* libarchive prefixes all paths with "./", let's chop that off. Note that we'll return a path of
* NULL for the root inode here! */
assert(entry);
assert(ret);
const char *p = sym_archive_entry_pathname(entry);
if (!p)
return -EBADMSG;
const char *e = startswith(p, "./") ?: p;
if (isempty(e))
*ret = NULL;
else if (path_is_safe(e))
*ret = e;
else
return -EBADMSG;
return 0;
}
static int archive_entry_read_acl(
struct archive_entry *entry,
acl_type_t ntype,
acl_t *acl,
TarFlags flags) {
int r;
assert(entry);
assert(acl);
int type;
if (ntype == ACL_TYPE_ACCESS)
type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
else if (ntype == ACL_TYPE_DEFAULT)
type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
else
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Unexpected ACL type");
int c = sym_archive_entry_acl_reset(entry, type);
if (c == 0)
return 0;
assert(c > 0);
#if HAVE_ACL
r = dlopen_libacl(LOG_DEBUG);
if (r < 0)
return 0;
_cleanup_(acl_freep) acl_t a = NULL;
a = sym_acl_init(c);
if (!a)
return log_oom();
#endif
for (;;) {
int rtype, permset, tag, qual;
const char *name;
r = sym_archive_entry_acl_next(
entry,
type,
&rtype,
&permset,
&tag,
&qual,
&name);
if (r == ARCHIVE_EOF)
break;
if (r != ARCHIVE_OK)
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
"Unexpected error while iterating through ACLs.");
assert(rtype == type);
acl_tag_t ntag = libarchive_acl_tag_to_libacl_tag(tag);
if (ntag == ACL_UNDEFINED_TAG)
continue;
#if HAVE_ACL
acl_entry_t e;
if (IN_SET(ntag, ACL_USER, ACL_GROUP)) {
id_t id = qual;
/* Suppress ACL entries for invalid UIDs/GIDS */
if (!uid_is_valid(id))
continue;
/* Suppress ACL entries for UIDs/GIDs to squash */
if (FLAGS_SET(flags, TAR_SQUASH_UIDS_ABOVE_64K) && id >= NSRESOURCE_UIDS_64K)
continue;
if (sym_acl_create_entry(&a, &e) < 0)
return log_error_errno(errno, "Failed to create ACL entry: %m");
if (sym_acl_set_tag_type(e, ntag) < 0)
return log_error_errno(errno, "Failed to set ACL entry tag: %m");
if (sym_acl_set_qualifier(e, &id) < 0)
return log_error_errno(errno, "Failed to set ACL entry qualifier: %m");
} else {
if (sym_acl_create_entry(&a, &e) < 0)
return log_error_errno(errno, "Failed to create ACL entry: %m");
if (sym_acl_set_tag_type(e, ntag) < 0)
return log_error_errno(errno, "Failed to set ACL entry tag: %m");
}
acl_permset_t p;
if (sym_acl_get_permset(e, &p) < 0)
return log_error_errno(errno, "Failed to get ACL entry permission set: %m");
r = acl_set_perm(p, ACL_READ, permset & ARCHIVE_ENTRY_ACL_READ);
if (r < 0)
return log_error_errno(r, "Failed to set ACL entry read bit: %m");
r = acl_set_perm(p, ACL_WRITE, permset & ARCHIVE_ENTRY_ACL_WRITE);
if (r < 0)
return log_error_errno(r, "Failed to set ACL entry write bit: %m");
r = acl_set_perm(p, ACL_EXECUTE, permset & ARCHIVE_ENTRY_ACL_EXECUTE);
if (r < 0)
return log_error_errno(r, "Failed to set ACL entry execute bit: %m");
if (sym_acl_set_permset(e, p) < 0)
return log_error_errno(errno, "Failed to set ACL entry permission set: %m");
#else
*acl = POINTER_MAX; /* Indicate the entry has valid ACLs. */
return 0;
#endif
}
#if HAVE_ACL
if (*acl)
sym_acl_free(*acl);
*acl = TAKE_PTR(a);
#else
*acl = NULL; /* Indicate the entry has no ACL. */
#endif
return 0;
}
static uid_t maybe_squash_uid(uid_t uid, TarFlags flags) {
if (FLAGS_SET(flags, TAR_SQUASH_UIDS_ABOVE_64K) &&
uid_is_valid(uid) &&
uid >= NSRESOURCE_UIDS_64K)
return UID_NOBODY;
return uid;
}
static uid_t maybe_squash_gid(uid_t gid, TarFlags flags) {
if (FLAGS_SET(flags, TAR_SQUASH_UIDS_ABOVE_64K) &&
gid_is_valid(gid) &&
gid >= NSRESOURCE_UIDS_64K)
return GID_NOBODY;
return gid;
}
static int archive_entry_read_stat(
struct archive_entry *entry,
mode_t *filetype,
mode_t *mode,
struct timespec *mtime,
uid_t *uid,
gid_t *gid,
unsigned *fflags,
acl_t *acl_access,
acl_t *acl_default,
XAttr **xa,
size_t *n_xa,
TarFlags flags) {
int r;
assert(entry);
assert(xa);
assert(n_xa);
/* Fills in all fields that are present in the archive entry. Doesn't change the fields if the entry
* doesn't contain the relevant data */
if (filetype)
*filetype = sym_archive_entry_filetype(entry);
if (mode)
*mode = sym_archive_entry_mode(entry);
if (mtime && sym_archive_entry_mtime_is_set(entry))
*mtime = (struct timespec) {
sym_archive_entry_mtime(entry),
sym_archive_entry_mtime_nsec(entry),
};
if (uid && sym_archive_entry_uid_is_set(entry))
*uid = maybe_squash_uid(sym_archive_entry_uid(entry), flags);
if (gid && sym_archive_entry_gid_is_set(entry))
*gid = maybe_squash_gid(sym_archive_entry_gid(entry), flags);
if (fflags) {
unsigned long fs = 0, fc = 0;
sym_archive_entry_fflags(entry, &fs, &fc);
*fflags = (fs & ~fc) & CHATTR_TAR_FL;
}
(void) sym_archive_entry_xattr_reset(entry);
for (;;) {
const char *name = NULL;
struct iovec data;
r = sym_archive_entry_xattr_next(entry, &name, (const void**) &data.iov_base, &data.iov_len);
if (r != ARCHIVE_OK)
break;
assert(name);
if (xattr_is_acl(name))
continue;
if (!FLAGS_SET(flags, TAR_SELINUX) && xattr_is_selinux(name))
continue;
bool duplicate = false;
FOREACH_ARRAY(i, *xa, *n_xa)
if (streq(i->name, name)) {
duplicate = true;
break;
}
if (duplicate)
continue;
_cleanup_free_ char *n = strdup(name);
if (!n)
return log_oom();
_cleanup_(iovec_done) struct iovec iovec_copy = {};
if (!iovec_memdup(&data, &iovec_copy))
return log_oom();
if (!GREEDY_REALLOC(*xa, *n_xa+1))
return log_oom();
(*xa)[(*n_xa)++] = (XAttr) {
.name = TAKE_PTR(n),
.data = TAKE_STRUCT(iovec_copy),
};
}
if (acl_access) {
r = archive_entry_read_acl(entry, ACL_TYPE_ACCESS, acl_access, flags);
if (r < 0)
return r;
}
if (acl_default) {
r = archive_entry_read_acl(entry, ACL_TYPE_DEFAULT, acl_default, flags);
if (r < 0)
return r;
}
return 0;
}
int tar_x(int input_fd, int tree_fd, TarFlags flags) {
int ar, r;
assert(input_fd >= 0);
assert(tree_fd >= 0);
r = dlopen_libarchive(LOG_DEBUG);
if (r < 0)
return r;
_cleanup_(archive_read_freep) struct archive *a = NULL;
a = sym_archive_read_new();
if (!a)
return log_oom();
ar = sym_archive_read_support_format_tar(a);
if (ar != ARCHIVE_OK)
return log_error_errno(
SYNTHETIC_ERRNO(ENOTRECOVERABLE),
"Failed to enable tar unpacking: %s", sym_archive_error_string(a));
ar = sym_archive_read_support_format_cpio(a);
if (ar != ARCHIVE_OK)
return log_error_errno(
SYNTHETIC_ERRNO(ENOTRECOVERABLE),
"Failed to enable cpio unpacking: %s", sym_archive_error_string(a));
ar = sym_archive_read_open_fd(a, input_fd, 64 * 1024);
if (ar != ARCHIVE_OK)
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
"Failed to initialize archive context: %s", sym_archive_error_string(a));
OpenInode *open_inodes = NULL;
if (!GREEDY_REALLOC(open_inodes, 2)) /* the minimal case is a single file in an archive, which would
* mean two inodes, the root dir inode, and he regular file
* inode, hence start with 2 here */
return log_oom();
size_t n_open_inodes = 0;
CLEANUP_ARRAY(open_inodes, n_open_inodes, open_inode_free_array);
/* Fill in the root inode. (Note: we leave the .path field as NULL to mark it as root inode.) */
open_inodes[0] = (OpenInode) {
.fd = tree_fd,
.filetype = S_IFDIR,
.mode = MODE_INVALID,
.mtime = { .tv_nsec = UTIME_OMIT },
.uid = UID_INVALID,
.gid = GID_INVALID,
};
n_open_inodes = 1;
for (;;) {
struct archive_entry *entry = NULL;
ar = sym_archive_read_next_header(a, &entry);
if (ar == ARCHIVE_EOF)
break;
if (!IN_SET(ar, ARCHIVE_OK, ARCHIVE_WARN))
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Failed to parse archive: %s",
sym_archive_error_string(a));
const char *p = NULL;
r = archive_entry_pathname_safe(entry, &p);
if (r < 0)
return log_error_errno(r, "Invalid path name in entry, refusing.");
if (ar == ARCHIVE_WARN)
log_warning("Non-critical error found while parsing '%s' from the archive, ignoring: %s",
p ?: ".", sym_archive_error_string(a));
if (!p) {
/* This is the root inode */
r = archive_entry_read_stat(
entry,
&open_inodes[0].filetype,
&open_inodes[0].mode,
&open_inodes[0].mtime,
&open_inodes[0].uid,
&open_inodes[0].gid,
&open_inodes[0].fflags,
&open_inodes[0].acl_access,
&open_inodes[0].acl_default,
&open_inodes[0].xattr,
&open_inodes[0].n_xattr,
flags);
if (r < 0)
return r;
if (open_inodes[0].filetype != S_IFDIR)
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
"Archives root inode is not a directory, refusing.");
continue;
}
/* Find common prefix with path elements we were looking at so far. */
const char *rest = p;
size_t i;
for (i = 1; i < n_open_inodes; i++) {
const char *e = path_startswith(p, open_inodes[i].path);
if (isempty(e))
break;
rest = e;
}
/* Finalize all inodes we won't need anymore now (go backwards, i.e. close inner fds first) */
while (n_open_inodes > i) {
n_open_inodes--;
r = open_inode_finalize(open_inodes + n_open_inodes);
if (r < 0)
return r;
}
/* And now create all remaining components */
for (;;) {
const char *element;
r = path_find_first_component(&rest, /* accept_dot_dot= */ false, &element);
if (r < 0)
return log_error_errno(r, "Failed to extract next element from path: %m");
if (r == 0)
break;
/* Safety check, before we add another level to our stack */
if (n_open_inodes >= DEPTH_MAX)
return log_error_errno(
SYNTHETIC_ERRNO(E2BIG),
"Archive's directory tree nested too deeply, refusing to descend more than %u levels.", DEPTH_MAX);
_cleanup_free_ char *e = strndup(element, r);
if (!e)
return log_oom();
const char *parent_path = NULL;
int parent_fd = -EBADF;
assert(n_open_inodes > 0);
parent_fd = open_inodes[n_open_inodes-1].fd;
parent_path = open_inodes[n_open_inodes-1].path;
_cleanup_free_ char *j = parent_path ? path_join(parent_path, e) : strdup(e);
if (!j)
return log_oom();
if (!GREEDY_REALLOC(open_inodes, n_open_inodes+1))
return log_oom();
_cleanup_close_ int fd = -EBADF;
mode_t filetype = MODE_INVALID;
mode_t mode = MODE_INVALID;
uid_t uid = UID_INVALID;
gid_t gid = GID_INVALID;
struct timespec mtime = { .tv_nsec = UTIME_OMIT };
unsigned fflags = 0;
#if HAVE_ACL
_cleanup_(acl_freep)
#endif
acl_t acl_access = NULL, acl_default = NULL;
XAttr *xa = NULL;
size_t n_xa = 0;
CLEANUP_ARRAY(xa, n_xa, xattr_free_array);
if (isempty(rest)) {
/* This is the final node in the path, create it */
if (sym_archive_entry_hardlink_is_set(entry)) {
/* If this is a hardlink, act on it */
const char *h = sym_archive_entry_hardlink(entry);
if (!h)
return log_error_errno(
SYNTHETIC_ERRNO(EBADMSG),
"No hardlink target in hardlink entry, refusing.");
/* libarchive prefixes all paths with "./", let's chop that off */
const char *target = startswith(h, "./") ?: h;
if (!path_is_safe(target))
return log_error_errno(
SYNTHETIC_ERRNO(EBADMSG),
"Invalid hardlink path name '%s' in entry, refusing.", target);
_cleanup_close_ int target_fd = -EBADF;
r = chaseat(tree_fd, tree_fd, target,
CHASE_PROHIBIT_SYMLINKS|CHASE_NOFOLLOW,
/* ret_path= */ NULL, &target_fd);
if (r < 0)
return log_error_errno(
r,
"Failed to find inode '%s' which shall be hardlinked as '%s': %m", target, j);
struct stat verify_st;
if (fstat(target_fd, &verify_st) < 0)
return log_error_errno(errno, "Failed to stat inode '%s': %m", target);
/* Refuse hardlinking directories early. */
if (!inode_type_can_hardlink(verify_st.st_mode))
return log_error_errno(SYNTHETIC_ERRNO(EBADF),
"Refusing to hardlink inode '%s' of type '%s': %m",
target, inode_type_to_string(verify_st.st_mode));
if (linkat(target_fd, "", parent_fd, e, AT_EMPTY_PATH) < 0) {
if (errno != ENOENT)
return log_error_errno(
errno,
"Failed to hardlink inode '%s' as '%s': %m", target, j);
/* To be able to link by inode fd we might have needed
* CAP_DAC_READ_SEARCH which we lacked. Let's retry with the
* parent. Yes, glibc/kernel report this as ENOENT. Kinda
* annoying. */
_cleanup_close_ int target_parent_fd = -EBADF;
_cleanup_free_ char *target_filename = NULL;
r = chaseat(tree_fd, tree_fd, target,
CHASE_PROHIBIT_SYMLINKS|CHASE_PARENT|CHASE_EXTRACT_FILENAME|CHASE_NOFOLLOW,
&target_filename, &target_parent_fd);
if (r < 0)
return log_error_errno(r, "Failed to find inode '%s' which shall be hardlinked as '%s': %m",
target, j);
if (linkat(target_parent_fd, target_filename, parent_fd, e, /* flags= */ 0) < 0)
return log_error_errno(errno, "Failed to hardlink inode '%s' as '%s': %m",
target, j);
}
continue;
}
r = archive_entry_read_stat(
entry,
&filetype,
&mode,
&mtime,
&uid,
&gid,
&fflags,
&acl_access,
&acl_default,
&xa,
&n_xa,
flags);