Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 33 additions & 40 deletions generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1694,22 +1694,20 @@ static int gen_entry_stat(const char *fname, struct file_struct *file,
STRUCT_STAT *stp, int follow_dirlinks)
{
int dfd;
const char *leaf;
/* link_stat_at folds in no fake-super xattr, so only use it when
* am_root >= 0 (where link_stat's get_stat_xattr is a no-op anyway). */
if (am_root >= 0 && (dfd = held_dfd_for(fname, file)) >= 0) {
const char *slash = strrchr(fname, '/');
return link_stat_at(dfd, slash ? slash + 1 : fname, stp, follow_dirlinks);
}
if (am_root >= 0 && (dfd = held_dfd_for(fname, file, &leaf)) >= 0)
return link_stat_at(dfd, leaf, stp, follow_dirlinks);
return link_stat(fname, stp, follow_dirlinks);
}

static int gen_entry_mkdir(char *fname, struct file_struct *file, mode_t mode)
{
int dfd = held_dfd_for(fname, file);
if (dfd >= 0) {
const char *slash = strrchr(fname, '/');
return do_mkdir_atfd(dfd, slash ? slash + 1 : fname, mode);
}
const char *leaf;
int dfd = held_dfd_for(fname, file, &leaf);
if (dfd >= 0)
return do_mkdir_atfd(dfd, leaf, mode);
return do_mkdir_at(fname, mode);
}

Expand Down Expand Up @@ -1744,32 +1742,30 @@ static BOOL can_try_fresh_basis_link(struct file_struct *file)

static int gen_entry_chmod(const char *fname, struct file_struct *file, mode_t mode)
{
int dfd = held_dfd_for(fname, file);
if (dfd >= 0) {
const char *slash = strrchr(fname, '/');
return do_chmod_atfd(dfd, slash ? slash + 1 : fname, mode);
}
const char *leaf;
int dfd = held_dfd_for(fname, file, &leaf);
if (dfd >= 0)
return do_chmod_atfd(dfd, leaf, mode);
return do_chmod_at(fname, mode);
}

static void gen_entry_set_times(const char *fname, struct file_struct *file, STRUCT_STAT *stp)
{
int dfd = held_dfd_for(fname, file);
const char *leaf;
int dfd = held_dfd_for(fname, file, &leaf);
if (dfd >= 0) {
const char *slash = strrchr(fname, '/');
if (set_times_at(dfd, slash ? slash + 1 : fname, stp) != -2)
if (set_times_at(dfd, leaf, stp) != -2)
return; /* handled (success or error) by the at-on-dfd tier */
}
set_times(fname, stp);
}

static int gen_entry_symlink(const char *slnk, const char *path, struct file_struct *file)
{
int dfd = held_dfd_for(path, file);
if (dfd >= 0) {
const char *slash = strrchr(path, '/');
return do_symlink_atfd(slnk, dfd, slash ? slash + 1 : path);
}
const char *leaf;
int dfd = held_dfd_for(path, file, &leaf);
if (dfd >= 0)
return do_symlink_atfd(slnk, dfd, leaf);
return do_symlink_at(slnk, path);
}

Expand Down Expand Up @@ -1804,10 +1800,10 @@ static int no_atfd_mknod_primitive(mode_t mode)
static int gen_entry_mknod(const char *path, struct file_struct *file, mode_t mode, dev_t rdev)
{
int dfd;
const char *leaf;
/* Let the path wrapper select the socket backend for this parent. */
if (!S_ISSOCK(mode) && (dfd = held_dfd_for(path, file)) >= 0) {
const char *slash = strrchr(path, '/');
int ret = do_mknod_atfd(dfd, slash ? slash + 1 : path, mode, rdev);
if (!S_ISSOCK(mode) && (dfd = held_dfd_for(path, file, &leaf)) >= 0) {
int ret = do_mknod_atfd(dfd, leaf, mode, rdev);
/* Fall through to the unconfined path-based create only where this
* build compiled no fd-relative primitive for this kind of node --
* SECURITY.md's rule for a platform that cannot be secure at all.
Expand All @@ -1823,11 +1819,10 @@ static int gen_entry_mknod(const char *path, struct file_struct *file, mode_t mo

static int gen_entry_unlink(const char *path, struct file_struct *file)
{
int dfd = held_dfd_for(path, file);
if (dfd >= 0) {
const char *slash = strrchr(path, '/');
return do_unlink_atfd(dfd, slash ? slash + 1 : path, 0);
}
const char *leaf;
int dfd = held_dfd_for(path, file, &leaf);
if (dfd >= 0)
return do_unlink_atfd(dfd, leaf, 0);
return do_unlink_at(path);
}

Expand All @@ -1836,13 +1831,11 @@ static int gen_entry_unlink(const char *path, struct file_struct *file)
* single renameat() within it, else fall back to the full-path wrapper. */
static int gen_entry_rename(const char *opath, const char *npath, struct file_struct *file)
{
int odfd = held_dfd_for(opath, file);
int ndfd = held_dfd_for(npath, file);
if (odfd >= 0 && ndfd >= 0) {
const char *os = strrchr(opath, '/');
const char *ns = strrchr(npath, '/');
return do_rename_atfd(odfd, os ? os + 1 : opath, ndfd, ns ? ns + 1 : npath);
}
const char *oleaf, *nleaf;
int odfd = held_dfd_for(opath, file, &oleaf);
int ndfd = held_dfd_for(npath, file, &nleaf);
if (odfd >= 0 && ndfd >= 0)
return do_rename_atfd(odfd, oleaf, ndfd, nleaf);
return do_rename_at(opath, npath);
}

Expand All @@ -1855,11 +1848,11 @@ static int gen_entry_rename(const char *opath, const char *npath, struct file_st
* set_file_attrs' held-fd handling). */
static int gen_entry_copy_xattrs(const char *src, const char *fname, struct file_struct *file)
{
int dfd = held_dfd_for(fname, file);
const char *leaf;
int dfd = held_dfd_for(fname, file, &leaf);
int xfd = -1, sfd = -1, ret;
if (dfd >= 0) {
const char *slash = strrchr(fname, '/');
xfd = openat(dfd, slash ? slash + 1 : fname,
xfd = openat(dfd, leaf,
O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_NOCTTY | O_CLOEXEC);
if (xfd < 0) {
/* We hold a confined parent dirfd but couldn't pin the
Expand Down
41 changes: 13 additions & 28 deletions receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
* (it may legitimately point outside the tree); the deep-entry-dir fallback,
* when the held-dirfd cache declines, gets the strict transfer-path one. */
if (secure_relpath_active()) {
int dfd = held_dfd_for(fnametmp, file);
int dfd = held_dfd_for(fnametmp, file, NULL);
if (dfd >= 0) {
char *slash = strrchr(fnametmp, '/');
fd = do_mkstemp_atfd(dfd, slash ? slash + 1 : fnametmp,
Expand Down Expand Up @@ -489,7 +489,8 @@ int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
}

static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
const char *fname, int fd, struct file_struct *file, int inplace_sizing)
const char *fname, int fd, struct file_struct *file, int inplace_sizing,
int output_is_device)
{
static char file_sum1[MAX_DIGEST_LEN];
struct map_struct *mapbuf;
Expand Down Expand Up @@ -688,10 +689,13 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
/* inplace: New data could be shorter than old data.
* preallocate_files: total_size could have been an overestimate.
* Cut off any extra preallocated zeros from dest file. */
if ((inplace_sizing || preallocated_len > offset) && fd != -1 && !IS_DEVICE(file->mode)) {
if ((inplace_sizing || preallocated_len > offset) && fd != -1
&& !output_is_device && !IS_DEVICE(file->mode)) {
if (do_ftruncate(fd, offset) < 0)
rsyserr(FERROR_XFER, errno, "ftruncate failed on %s", full_fname(fname));
}
#else
(void)output_is_device;
#endif

if (INFO_GTE(PROGRESS, 1))
Expand All @@ -718,7 +722,7 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,

static void discard_receive_data(int f_in, struct file_struct *file)
{
receive_data(f_in, NULL, -1, 0, NULL, -1, file, 0);
receive_data(f_in, NULL, -1, 0, NULL, -1, file, 0, 0);
}

static void record_placement(int *log_fd, const char *name)
Expand Down Expand Up @@ -863,7 +867,6 @@ int recv_files(int f_in, int f_out, char *local_name, int placement_fd)
const char *parent_dirname = "";
#endif
int ndx, recv_ok, one_inplace, write_to_device;
mode_t write_devices_saved_mode = 0;

reset_basis_parent_cache();

Expand Down Expand Up @@ -1149,6 +1152,7 @@ int recv_files(int f_in, int f_out, char *local_name, int placement_fd)
basis_has_stat = True;
} else {
int bdfd;
const char *basis_leaf;
BOOL backup_basis = fnamecmp_type == FNAMECMP_BACKUP && inplace && make_backups > 0;

if (backup_basis && !begin_backup_read())
Expand All @@ -1160,11 +1164,9 @@ int recv_files(int f_in, int f_out, char *local_name, int placement_fd)
/* The relative partial path contains peer-derived directory
* components. It is not an operator-trusted path as a whole. */
fd1 = secure_relative_open(NULL, fnamecmp, O_RDONLY, 0);
} else if (!basedir && (bdfd = held_dfd_for(fnamecmp, file)) >= 0) {
const char *slash;
} else if (!basedir && (bdfd = held_dfd_for(fnamecmp, file, &basis_leaf)) >= 0) {
assert(fnamecmp != NULL); /* set on every path above */
slash = strrchr(fnamecmp, '/');
fd1 = do_open_atfd(bdfd, slash ? slash + 1 : fnamecmp, O_RDONLY, 0);
fd1 = do_open_atfd(bdfd, basis_leaf, O_RDONLY, 0);
} else {
/* An operator-supplied basis -- a --partial-dir, or an
* alt-dest basedir (--copy-dest/--compare-dest/--link-dest) --
Expand Down Expand Up @@ -1350,32 +1352,15 @@ int recv_files(int f_in, int f_out, char *local_name, int placement_fd)
else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
rprintf(FINFO, "%s\n", fname);

/* --write-devices writes a regular source file's content into an
* existing destination device. Flip file->mode to S_IFBLK just for
* receive_data()'s ftruncate gate (!IS_DEVICE), then restore it right
* after -- the file_struct was built as a regular file with no
* DEV_EXTRA_CNT, so leaving it S_IFBLK would make set_stat_xattr's
* F_RDEV_P() read past the allocation. Kept tight here (after the
* pre-transfer log and the fd2 bail-out) so no continue skips the
* restore and dest_mode() above ran on the real mode. */
if (write_to_device) {
write_devices_saved_mode = file->mode;
file->mode = S_IFBLK | (file->mode & ACCESSPERMS);
}

/* recv file data */
recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size, fname, fd2, file, inplace || one_inplace);
recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size, fname, fd2, file,
inplace || one_inplace, write_to_device);
if (stream_source && recv_ok <= 0) {
rprintf(FERROR, "FIFO transfer of %s failed verification and cannot be retried\n",
full_fname(fname));
exit_cleanup(RERR_FILEIO);
}

if (write_to_device) {
file->mode = write_devices_saved_mode;
write_devices_saved_mode = 0;
}

log_item(log_code, file, iflags, NULL);
if (want_progress_now)
instant_progress(fname);
Expand Down
14 changes: 5 additions & 9 deletions rsync.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,15 +527,15 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,

if (!sxp) {
int sret, sdfd;
const char *stat_leaf;
if (dry_run)
return 1;
/* Stat through the entry's held dir fd (like gen_entry_stat) so we
* don't re-walk the full path here; link_stat_at folds in no
* fake-super xattr, so only when am_root >= 0. */
if (am_root >= 0 && (sdfd = held_dfd_for(fname, file)) >= 0) {
const char *sl = strrchr(fname, '/');
sret = link_stat_at(sdfd, sl ? sl + 1 : fname, &sx2.st, 0);
} else
if (am_root >= 0 && (sdfd = held_dfd_for(fname, file, &stat_leaf)) >= 0)
sret = link_stat_at(sdfd, stat_leaf, &sx2.st, 0);
else
sret = link_stat(fname, &sx2.st, 0);
if (sret < 0) {
rsyserr(FERROR_XFER, errno, "stat %s failed",
Expand All @@ -552,11 +552,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
* issue single-component *at() calls against it instead of re-resolving
* the full path each time. -1 => fall back to the full-path wrappers
* (cross-tree path such as --temp-dir/--backup-dir, or gated off). */
dfd = held_dfd_for(fname, file);
if (dfd >= 0) {
const char *slash = strrchr(fname, '/');
leaf = slash ? slash + 1 : fname;
}
dfd = held_dfd_for(fname, file, &leaf);

#if defined SUPPORT_XATTRS || defined SUPPORT_ACLS
/* Pin a regular-file/dir/fifo entry via the held dir fd with O_NOFOLLOW so
Expand Down
Loading