Skip to content

mv: cross-device move drops subdirectory ACLs and modes, widening access #15784

Closed

Description

@sylvestre
============================================================================
ANT-2026-FOLY3LY1  [medium]  CWE-281  authz
mv: cross-device move drops subdirectory ACLs and modes, widening access
Location: src/uu/mv/src/mv.rs:1297  in copy_dir_contents_recursive()
Commit:   7bc1eb6d1d6fa2e1dc2c9d2277cf576c05f61e47
============================================================================

When uutils mv moves a directory tree to another filesystem, every directory below the operand loses its POSIX ACLs (access and default) and other extended attributes. A local user kept out of a subdirectory by an entry such as user:www-data:--- can therefore read it after the move. mv prints nothing, exits 0 and deletes the source, so the original ACLs no longer exist anywhere, whereas GNU mv keeps them. The user who gains access plays no part in the move and cannot cause it. It only takes root or the tree's owner moving a parent of the restricted directory across filesystems, for example /srv to a new disk or a project tree into or out of a tmpfs.

When rename(2) fails with EXDEV, rename_dir_fallback() in src/uu/mv/src/mv.rs preserves xattrs on the operand directory, and the tree walk preserves them on regular files and symlinks. The subdirectory branch of copy_dir_contents_recursive() (mv.rs:1297) only does mkdir, chown and the recursion, and never calls into fsxattr. Because nothing is attempted, nothing fails or gets reported. The same omission also drops each subdirectory's mode, so a 0700 subdirectory with no ACL arrives as 0777 & ~umask (0755 under the default umask). That is a broader exposure and should be fixed in the same change, with the chmod applied after the ACL so the mask bits are not handed to the owning group. The attached reproducer mounts two tmpfs and shows www-data denied on site/private/notes.txt before moving site and reading it afterwards with uutils mv, while GNU mv keeps the deny entry. This is not a regression. The current code dates from e9b24b4 ("mv: improve the hardlink support (#8296)", July 2025, first released in 0.2.0), and the cross-device fallback never copied subdirectory xattrs before that either.

Details

When mv moves a directory tree to another filesystem, the directories below the operand arrive without their extended
attributes, POSIX ACLs included. Both filesystems below support ACLs:

root# mkdir -p /mnt/a/site/private; echo 'internal only' > /mnt/a/site/private/notes.txt
root# setfacl -m user:www-data:--- /mnt/a/site/private   # user::rwx user:www-data:--- group::r-x mask::r-x other::r-x
root# mv /mnt/a/site /mnt/b/site; echo $?                # no message
0
root# getfacl -c /mnt/b/site/private
user::rwx
group::r-x
other::r-x

site/private has the same owner and the same mode bits (0755) as before; the only thing missing is the ACL, and www-data,
who got "Permission denied" on notes.txt before the move, reads it afterwards. GNU mv 9.7 moves the same tree with the ACL
intact. uutils mv keeps the ACL when private itself is the operand, and it keeps the ACLs of regular files anywhere in the
tree; only directories below the operand lose theirs.

rename_with_fallback() (src/uu/mv/src/mv.rs:939) calls rename_dir_fallback() when rename(2) fails with EXDEV on a
directory. The fallback creates objects in three places, and two of them copy xattrs:

  • the operand directory, in rename_dir_fallback(): fsxattr::retrieve_xattrs_fd() at mv.rs:1127 before the copy,
    fsxattr::apply_xattrs_fd_ignore_unsupported() at mv.rs:1160 after it;
  • regular files, in copy_file_with_hardlinks_helper(): fsxattr::copy_xattrs_ignore_unsupported() at mv.rs:1389
    (symlinks likewise, at mv.rs:1054);
  • subdirectories, in copy_dir_contents_recursive(): nothing.
// src/uu/mv/src/mv.rs:1297
} else if from_path.is_dir() {
    // Recursively copy subdirectory (only real directories, not symlinks)
    create_dir_fail_closed(&to_path)?;

    // Preserve ownership (uid/gid) of the subdirectory
    #[cfg(unix)]
    {
        let _ = preserve_ownership(&from_path, &to_path);
    }

    print_verbose(&from_path, &to_path);

    copy_dir_contents_recursive(&from_path, &to_path, …)?;
} else {

For site/private this branch does mkdir, chown and the recursion, and never calls into fsxattr. Nothing is attempted,
so nothing can fail and nothing is reported. When the recursion has returned, rename_dir_fallback() deletes the source tree
(fs::remove_dir_all(from), mv.rs:1166), and the ACLs exist nowhere any more.

Everything stored in xattrs of a subdirectory goes the same way. In a tree moved with the binary of this commit:

  • access ACL: a subdirectory root:users, mode 0700 plus user:backup:r-x, arrives without the user:backup entry;
  • default ACL: a subdirectory with default:group::---, default:other::--- arrives without it, and a file created in it
    afterwards is 0644 instead of 0600;
  • other namespaces: a trusted.foo attribute on a subdirectory is gone.

The same missing step also discards the subdirectory's mode: the directory keeps the 0777 & ~umask it was created with,
so with the default umask a 0700 subdirectory arrives as 0755. That exposure is broader than the ACL loss (it needs no ACL
at all, and opens the directory to every local user), and it belongs in the same change; the example above is chosen so
that it plays no part (0755 before and after). The two interact,
which matters for the fix: while a directory has an ACL, the group bits of its st_mode hold the ACL mask, not the owning
group's permission. The root:users directory above has group::--- and mask::r-x, so its st_mode is 0750. Restoring
that mode on a copy that has no ACL gives root:users 0750, and a member of users reads a directory that was closed to
the group.

This is not a regression: the cross-device fallback has never copied the xattrs of directories below the operand. The
subdirectory branch in its present form was written in e9b24b4bc2fd ("mv: improve the hardlink support (#8296)",
2025-07-03), first released in 0.2.0.

Impact

A local user whom an ACL keeps out of a directory gets in once somebody moves a parent of that directory to another
filesystem (/home or /srv to a new disk, a project tree into or out of a tmpfs). Concretely: user:NAME:--- and
group:NAME:--- entries on subdirectories stop applying, and files created later in a moved subdirectory no longer get the
restrictions of its default ACL. Named-user and named-group grants are lost as well, which takes access away instead of
giving it. The user who gains access does nothing during the move and cannot cause it; they only read afterwards. The
preconditions are an ACL on a directory at least one level below the operand and a move that crosses filesystems. No option
is involved, the invoker can be root or the unprivileged owner of the tree, mv prints nothing and exits 0, and the source
with the original ACLs is deleted. The same move also turns a subdirectory that relies on its mode alone (0700, no ACL)
into 0755 under the default umask, a broader exposure than the ACL case.

Reproducing

repro is a single Python script (standard library only, no companion files), run as root in the built image. It
uses the prebuilt target/debug/coreutils and GNU mv from /usr/bin as the control.

Setup: it mounts two tmpfs in a fresh temporary directory, so that rename(2) between them fails with EXDEV and mv
has to copy the tree, and both sides store POSIX ACLs. The umask is the default 022, so the directories mv creates get
the same 0755 as the source and only the ACL can differ. The mounts are removed at the end, also if a step fails.

Then, once with GNU mv (CONTROL) and once with uutils mv (OBSERVED):

  1. as root (the tree's owner), create site/private/notes.txt on the first filesystem and put user:www-data:--- on
    site/private;
  2. read the ACL of site/private and let www-data (with its own group only) cat the file: denied;
  3. as root, move site (the parent of site/private) to the second filesystem, the ordinary administrative action;
  4. read the ACL of the moved site/private and let www-data cat the file again.

www-data plays the user the ACL was meant to keep out: it does nothing during the move, it only reads before and
after.

Each run prints the mv exit status and stderr, and for site/private before and after the move its ACL as
getfacl -cpE shows it and what www-data's cat returned. Expected output:

CONTROL  /usr/bin/mv site -> other filesystem: exit 0 stderr ''
CONTROL    site/private before: user::rwx,user:www-data:---,group::r-x,mask::r-x,other::r-x   www-data cat notes.txt: exit 1 ''
CONTROL    site/private after:  user::rwx,user:www-data:---,group::r-x,mask::r-x,other::r-x   www-data cat notes.txt: exit 1 ''
OBSERVED /src/target/debug/coreutils mv site -> other filesystem: exit 0 stderr ''
OBSERVED   site/private before: user::rwx,user:www-data:---,group::r-x,mask::r-x,other::r-x   www-data cat notes.txt: exit 1 ''
OBSERVED   site/private after:  user::rwx,group::r-x,other::r-x   www-data cat notes.txt: exit 0 'internal only\n'
BUG

The last line is BUG when, with uutils mv, site/private had the user:www-data:--- entry and www-data was denied
before the move, and after it the entry is gone and www-data reads the content root wrote, while with GNU mv this did
not happen. Otherwise it is OK.

Suggested fix

The attached patch adds the missing step to src/uu/mv/src/mv.rs:

  • in the subdirectory branch of copy_dir_contents_recursive(), after the recursive call has populated the new directory
    (the retrieve-before/apply-after order rename_dir_fallback() uses for the operand), it opens source and destination
    with uucore::safe_copy::open_source(…, true) (O_NOFOLLOW) and calls fsxattr::copy_xattrs_fd_ignore_unsupported(),
    reporting refused attributes through show_xattr_failures() like the file path does: a warning, and the move still
    succeeds;
  • in the fifo branch of copy_file_with_hardlinks_helper(), which had the same omission inside the tree walk, it adds the
    path-based fsxattr::copy_xattrs_ignore_unsupported() call the symlink branch already uses. The top-level
    rename_fifo_fallback(), which preserves neither ownership nor xattrs, is left alone.

It also adds test_mv_cross_device_subdir_acl_preserved to tests/by-util/test_mv.rs, next to
test_mv_cross_device_dir_xattr_preserved: ACLs on two nested subdirectories and a fifo, the tree moved to /dev/shm,
getfacl output compared. With the patch, the moved tree keeps the access ACLs, default ACLs and
trusted.* attributes of its subdirectories, for root and for an unprivileged owner, and the reproducer prints OK.

The patch does not restore subdirectory modes (the 0700 → 0755 loss described above), which should land in the same change.
The chmod belongs after the xattr step, in the order rename_file_fallback() uses for files (xattrs at mv.rs:1462,
mode at mv.rs:1480): then it sets the mask of an ACL that is already in place, and does not hand the mask bits to the
owning group. Because the ACL is applied only once the subdirectory is filled, its contents sit in a 0777 & ~umask
directory for the duration of a long copy; creating each directory 0700, filling it, copying the xattrs and only then
setting the mode closes that window as well.

Running the reproducer

ANT-2026-FOLY3LY1/repro, next to this file. It was written by an automated agent and our check ran it as root in a privileged container: run it INSIDE A THROWAWAY VM, with the image oss-scan-7bc1eb6d1d6f built by bash build/build-image.sh, from the archive's top directory:

docker run --rm --network=none --privileged -u 0 -w /src -v "$PWD/ANT-2026-FOLY3LY1/repro:/tmp/repro:ro" oss-scan-7bc1eb6d1d6f /tmp/repro

It prints its evidence; the last line is BUG if the issue triggered, OK otherwise.

Activity

  1. added a commit that references this issue on Nov 9, 2026
    51ee816
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions