From c463a1ffca0b6c88e2c06690e6792e295fc29a12 Mon Sep 17 00:00:00 2001 From: Zen Dodd Date: Sat, 5 Dec 2026 00:18:13 +1000 Subject: [PATCH 1/3] receiver: preserve file mode while writing to devices --- receiver.c | 30 ++++------------ testsuite/tests/metadata/write-devices.py | 42 +++++++++++++++++++++++ 2 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 testsuite/tests/metadata/write-devices.py diff --git a/receiver.c b/receiver.c index 188631a40..fa0841594 100644 --- a/receiver.c +++ b/receiver.c @@ -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; @@ -688,7 +689,8 @@ 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)); } @@ -718,7 +720,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) @@ -863,7 +865,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(); @@ -1350,32 +1351,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); diff --git a/testsuite/tests/metadata/write-devices.py b/testsuite/tests/metadata/write-devices.py new file mode 100644 index 000000000..77c885c4d --- /dev/null +++ b/testsuite/tests/metadata/write-devices.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +import os +import stat + +from harness import metadata +from harness.environment import SCRATCHDIR +from harness.results import test_fail, test_skipped +from harness.rsync import run_rsync + + +metadata(features={'device_nodes'}, transports={'pipe'}, + mutates={'filesystem', 'process'}, tags={'metadata', 'regression'}) + +if os.geteuid() != 0: + test_skipped('device tests need root', capability='device_nodes') + +null_stat = os.stat('/dev/null') +if not stat.S_ISCHR(null_stat.st_mode): + test_skipped('no character device is available', capability='device_nodes') + +base = SCRATCHDIR / 'write-devices' +source = base / 'source' +dest = base / 'dest' +source.mkdir(parents=True) +dest.mkdir() +(source / 'a-device').write_bytes(b'device data\n') +(source / 'b-regular').write_bytes(b'ordinary data\n') + +try: + os.mknod(dest / 'a-device', stat.S_IFCHR | 0o600, null_stat.st_rdev) +except OSError as error: + test_skipped(f'cannot create a device node: {error}', capability='device_nodes') + +result = run_rsync('-rt', '--write-devices', '--ignore-times', f'{source}/', f'{dest}/', + capture_output=True, check=False) +if result.returncode != 0: + test_fail(f'writing to the device failed: {result.stderr}') +if not stat.S_ISCHR((dest / 'a-device').stat().st_mode): + test_fail('the destination device was replaced') +if (dest / 'b-regular').read_bytes() != b'ordinary data\n': + test_fail('the following regular file was not copied') From 046b359d29184a546df1cdae5f7006c9540b19ec Mon Sep 17 00:00:00 2001 From: Zen Dodd Date: Sat, 5 Dec 2026 00:18:13 +1000 Subject: [PATCH 2/3] syscall: share removal logic and return entry leaf names --- generator.c | 73 ++++++++++++++++------------------ receiver.c | 9 ++--- rsync.c | 14 +++---- syscall.c | 110 +++++++++++++--------------------------------------- util1.c | 13 +++---- 5 files changed, 76 insertions(+), 143 deletions(-) diff --git a/generator.c b/generator.c index cb7d1a535..3cc775232 100644 --- a/generator.c +++ b/generator.c @@ -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); } @@ -1744,20 +1742,19 @@ 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); @@ -1765,11 +1762,10 @@ static void gen_entry_set_times(const char *fname, struct file_struct *file, STR 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); } @@ -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. @@ -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); } @@ -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); } @@ -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 diff --git a/receiver.c b/receiver.c index fa0841594..b68228bcb 100644 --- a/receiver.c +++ b/receiver.c @@ -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, @@ -1150,6 +1150,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()) @@ -1161,11 +1162,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) -- diff --git a/rsync.c b/rsync.c index 043e124bc..230ad3bbd 100644 --- a/rsync.c +++ b/rsync.c @@ -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", @@ -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 diff --git a/syscall.c b/syscall.c index a2a0a043c..94a37216f 100644 --- a/syscall.c +++ b/syscall.c @@ -1240,26 +1240,18 @@ int do_unlink(const char *path) return unlink(path); } -/* - Symlink-race-safe variant of do_unlink() for receiver-side use. See - the comment on do_chmod_at() for the threat model. unlink() resolves - parent components, so a parent-symlink swap can delete an outside - file under the daemon's authority. Defence: open the parent of path - under secure_relative_open() and use unlinkat() (flags=0) against - that dirfd. - - Falls through to do_unlink() for the same dry-run / non-daemon / - chrooted / no-parent / absolute-path cases as the other wrappers. -*/ -int do_unlink_at(const char *path) +/* Resolve the parent identically for file and directory removal. The leaf + * operation differs only by AT_REMOVEDIR; both retain the same path-policy + * fallbacks and close the owned parent descriptor after unlinkat(). */ +static int do_remove_at(const char *path, int remove_dir) { #ifdef AT_FDCWD - extern int am_daemon, am_chrooted; char dirpath[MAXPATHLEN]; const char *bname; const char *slash; int dfd, ret, e; size_t dlen; + int flags = remove_dir ? AT_REMOVEDIR : 0; if (dry_run) return 0; RETURN_ERROR_IF_RO_OR_LO; @@ -1268,11 +1260,11 @@ int do_unlink_at(const char *path) #if defined O_NOFOLLOW && defined O_DIRECTORY if (operator_path_resolve) { if (symlink_optout_allowed()) - return unlink(path); + return remove_dir ? rmdir(path) : unlink(path); dfd = owner_walk_parent(path, &bname); if (dfd < 0) return -1; - ret = unlinkat(dfd, bname, 0); + ret = unlinkat(dfd, bname, flags); e = errno; close(dfd); errno = e; @@ -1281,14 +1273,14 @@ int do_unlink_at(const char *path) #endif if (!secure_relpath_active()) - return unlink(path); + return remove_dir ? rmdir(path) : unlink(path); if (!path || !*path || *path == '/') - return unlink(path); + return remove_dir ? rmdir(path) : unlink(path); slash = strrchr(path, '/'); if (!slash) - return unlink(path); + return remove_dir ? rmdir(path) : unlink(path); dlen = slash - path; if (dlen >= sizeof dirpath) { @@ -1303,16 +1295,23 @@ int do_unlink_at(const char *path) if (dfd < 0) return -1; - ret = unlinkat(dfd, bname, 0); + ret = unlinkat(dfd, bname, flags); e = errno; close(dfd); errno = e; return ret; #else - return do_unlink(path); + if (dry_run) return 0; + RETURN_ERROR_IF_RO_OR_LO; + return remove_dir ? rmdir(path) : unlink(path); #endif } +int do_unlink_at(const char *path) +{ + return do_remove_at(path, 0); +} + #ifdef SUPPORT_LINKS int do_symlink(const char *lnk, const char *path) { @@ -1959,64 +1958,7 @@ int do_rmdir(const char *pathname) */ int do_rmdir_at(const char *pathname) { -#ifdef AT_FDCWD - extern int am_daemon, am_chrooted; - char dirpath[MAXPATHLEN]; - const char *bname; - const char *slash; - int dfd, ret, e; - size_t dlen; - - if (dry_run) return 0; - RETURN_ERROR_IF_RO_OR_LO; - RETURN_ERROR_IF_NULL(pathname); - -#if defined O_NOFOLLOW && defined O_DIRECTORY - if (operator_path_resolve) { - if (symlink_optout_allowed()) - return do_rmdir(pathname); - dfd = owner_walk_parent(pathname, &bname); - if (dfd < 0) - return -1; - ret = unlinkat(dfd, bname, AT_REMOVEDIR); - e = errno; - close(dfd); - errno = e; - return ret; - } -#endif - - if (!secure_relpath_active()) - return rmdir(pathname); - - if (!pathname || !*pathname || *pathname == '/') - return rmdir(pathname); - - slash = strrchr(pathname, '/'); - if (!slash) - return rmdir(pathname); - - dlen = slash - pathname; - if (dlen >= sizeof dirpath) { - errno = ENAMETOOLONG; - return -1; - } - memcpy(dirpath, pathname, dlen); - dirpath[dlen] = '\0'; - bname = slash + 1; - - dfd = secure_relative_dirfd(NULL, dirpath); - if (dfd < 0) - return -1; - - ret = unlinkat(dfd, bname, AT_REMOVEDIR); - e = errno; - close(dfd); - errno = e; - return ret; -#else - return do_rmdir(pathname); -#endif + return do_remove_at(pathname, 1); } int do_open(const char *pathname, int flags, mode_t mode) @@ -4440,11 +4382,12 @@ int open_sender_root_entry(int root_fd, const char *relpath, int flags) * is opened once and cached. * * file->basename is NOT assumed to equal `path`'s leaf (a temp file has a - * different basename), so the caller derives the leaf from `path`. */ -int held_dfd_for(const char *path, const struct file_struct *file) + * different basename). On success, optionally return the leaf of `path`. */ +int held_dfd_for(const char *path, const struct file_struct *file, const char **leaf) { const char *slash, *dn; size_t plen; + int fd; if (!path || *path == '/') return -1; @@ -4453,7 +4396,10 @@ int held_dfd_for(const char *path, const struct file_struct *file) plen = slash ? (size_t)(slash - path) : 0; if (strlen(dn) != plen || memcmp(path, dn, plen) != 0) return -1; - return get_dir_fd(file ? file->dirname : NULL); + fd = get_dir_fd(file ? file->dirname : NULL); + if (fd >= 0 && leaf) + *leaf = slash ? slash + 1 : path; + return fd; } /* No symlink means the receiver's checked directory also satisfies an @@ -4465,7 +4411,7 @@ int held_plain_dfd_for(const char *path, const struct file_struct *file) if (am_daemon || confine_root || strlen(path) >= MAXPATHLEN - 1) return -1; - fd = held_dfd_for(path, file); + fd = held_dfd_for(path, file, NULL); if (fd >= 0 && dpc_plain_depth == dpc_depth) return fd; #else diff --git a/util1.c b/util1.c index 9a5580f58..e007f72b1 100644 --- a/util1.c +++ b/util1.c @@ -735,14 +735,13 @@ int robust_rename(const char *from, const char *to, const char *partialptr, while (tries--) { /* tmp -> final usually live in the entry's own dir: rename via the * held dir fd when both do, else the full-path wrapper. */ - int ofd = held_dfd_for(from, file); - int nfd = held_dfd_for(to, file); + const char *oleaf, *nleaf; + int ofd = held_dfd_for(from, file, &oleaf); + int nfd = held_dfd_for(to, file, &nleaf); int rr; - if (ofd >= 0 && nfd >= 0) { - const char *os = strrchr(from, '/'); - const char *ns = strrchr(to, '/'); - rr = do_rename_atfd(ofd, os ? os + 1 : from, nfd, ns ? ns + 1 : to); - } else + if (ofd >= 0 && nfd >= 0) + rr = do_rename_atfd(ofd, oleaf, nfd, nleaf); + else rr = do_rename_at(from, to); if (rr == 0) return 0; From 024ca47b1a24431131a9921d47d496dad5c14fac Mon Sep 17 00:00:00 2001 From: Zen Dodd Date: Sat, 5 Dec 2026 00:50:41 +1000 Subject: [PATCH 3/3] testsuite: probe scratch device access before write-devices --- receiver.c | 2 ++ testsuite/profiles/openbsd.json | 1 + testsuite/tests/metadata/write-devices.py | 22 ++++++++++++++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/receiver.c b/receiver.c index b68228bcb..66e7937c2 100644 --- a/receiver.c +++ b/receiver.c @@ -694,6 +694,8 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r, 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)) diff --git a/testsuite/profiles/openbsd.json b/testsuite/profiles/openbsd.json index 066ce0eb2..11164424c 100644 --- a/testsuite/profiles/openbsd.json +++ b/testsuite/profiles/openbsd.json @@ -19,6 +19,7 @@ "high_fd": "no descriptors above FD_SETSIZE", "interpose": "no supported interposition fixture", "preallocate": "no preallocation support", + "scratch_device_io": "scratch filesystem can deny opening device nodes", "search_only": "no race-safe search-only directory descriptors", "setpriv": "no setpriv launcher", "simd": "no SIMD test build", diff --git a/testsuite/tests/metadata/write-devices.py b/testsuite/tests/metadata/write-devices.py index 77c885c4d..04f465dc6 100644 --- a/testsuite/tests/metadata/write-devices.py +++ b/testsuite/tests/metadata/write-devices.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import errno import os import stat @@ -9,7 +10,7 @@ from harness.rsync import run_rsync -metadata(features={'device_nodes'}, transports={'pipe'}, +metadata(features={'device_nodes', 'scratch_device_io'}, transports={'pipe'}, mutates={'filesystem', 'process'}, tags={'metadata', 'regression'}) if os.geteuid() != 0: @@ -27,16 +28,29 @@ (source / 'a-device').write_bytes(b'device data\n') (source / 'b-regular').write_bytes(b'ordinary data\n') +device = dest / 'a-device' try: - os.mknod(dest / 'a-device', stat.S_IFCHR | 0o600, null_stat.st_rdev) + os.mknod(device, stat.S_IFCHR | 0o600, null_stat.st_rdev) except OSError as error: test_skipped(f'cannot create a device node: {error}', capability='device_nodes') +# Some scratch mounts allow mknod but deny opening the resulting device. +try: + fd = os.open(device, os.O_RDWR) +except OSError as error: + if error.errno not in (errno.EACCES, errno.ENXIO, errno.EPERM): + raise + test_skipped(f'cannot open a scratch character device: {error}', capability='scratch_device_io') +os.close(fd) +before = device.stat() + result = run_rsync('-rt', '--write-devices', '--ignore-times', f'{source}/', f'{dest}/', capture_output=True, check=False) if result.returncode != 0: test_fail(f'writing to the device failed: {result.stderr}') -if not stat.S_ISCHR((dest / 'a-device').stat().st_mode): - test_fail('the destination device was replaced') +after = device.stat() +if (not stat.S_ISCHR(after.st_mode) + or (after.st_dev, after.st_ino, after.st_rdev) != (before.st_dev, before.st_ino, before.st_rdev)): + test_fail('the destination device was replaced or changed') if (dest / 'b-regular').read_bytes() != b'ordinary data\n': test_fail('the following regular file was not copied')