coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

df, stat: mount points with a newline are ignored (escape not decoded)

Open sylvestre opened this issue 1 month ago • 1 comments

============================================================================
ANT-2026-6LP5L261  [NOT SECURITY: ordinary bug]
df, stat: mount points with a newline are ignored (escape not decoded)
Location: src/uucore/src/lib/features/fsext/mod.rs:181  in replace_special_chars()
Commit:   7bc1eb6d1d6fa2e1dc2c9d2277cf576c05f61e47
============================================================================

Not a security issue. This is a real, reproducible bug that is NOT a security issue: by your project's own policy nobody gains anything from it that they did not already have. It is not part of the vulnerability report, is in none of its counts, needs no embargo and no advisory, and can be filed and fixed in the open like any bug.

When a mount point's name contains a newline, uutils df leaves that file system out of its listing (including df -a and the --total sum), and df PATH and stat -c %m PATH report the parent file system for any path below it. Nothing is printed on stderr and the exit status is 0, so the answer is silently wrong. GNU coreutils 9.7 handles the same mount correctly.

The cause is replace_special_chars() in src/uucore/src/lib/features/fsext/mod.rs. The kernel escapes space, tab, newline and backslash in the mount point field of /proc/self/mountinfo and /proc/mounts as \040, \011, \012 and \134. The function decodes three of these and leaves \012 as four literal characters, so MountInfo.mount_dir names a path that does not exist. df drops the entry because statfs() on that path fails with ENOENT. The prefix match in df FILE and in stat's find_mount_point() never matches the real path, which contains an actual newline, so the enclosing mount wins. The attached script mounts a 1 MiB tmpfs on usb stick and on usb<LF>stick. The space-named mount is reported correctly. For the newline-named one, df -a omits it, df DIR prints the root overlay and its 123638648 KiB, and stat %m prints /. The partial decoder came in with 96c17312eadf ("df: fix display of special characters", first released in 0.0.29). Before that commit no escapes were decoded at all, so newline-named mounts have always been missed.

This is an ordinary bug, not a security issue: only root's own mount was shown putting a newline-named mount point into the host's mount table (a mount an unprivileged user makes in their own namespace never reaches it), and the result is wrong df / stat %m output, with nothing read, written or deleted and no script decision shown to change.

Details

The kernel writes four characters of a mount point name as backslash plus three octal digits in /proc/self/mountinfo and /proc/mounts: space \040, tab \011, newline \012, backslash \134 (checked on Linux 6.1 by mounting on such names; other control characters are written raw). uucore's parser undoes three of the four:

// src/uucore/src/lib/features/fsext/mod.rs:172
fn replace_special_chars(s: &[u8]) -> Vec<u8> {
    …
    s.replace(r"\040", " ")
        .replace(r"\011", "	")
        .replace(r"\134", r"\")
}

\012 is missing. With a tmpfs mounted on /tmp/mount-newline/media/usb<LF>stick the table line is

433 465 0:56 / /tmp/mount-newline/media/usb\012stick rw,relatime - tmpfs newline-fs rw,size=1024k,inode64

and MountInfo::new() (fsext/mod.rs:209, and :217 for the /etc/mtab fallback) stores mount_dir = /tmp/mount-newline/media/usb\012stick with a literal backslash, 0, 1, 2. No such path exists. Both consumers of read_fs_list() then give a wrong answer with nothing on stderr and exit status 0:

uutils GNU coreutils 9.7
df, df -a no newline-fs row row present, mount point shown as …/usb?stick
df --total -t tmpfs total lacks the 1024 blocks total includes them
df --output=source,fstype,size DIR overlay overlay 123638648 (the root fs) newline-fs tmpfs 1024
stat -c %m DIR / /tmp/mount-newline/media/usb<LF>stick

The same mount on usb stick (\040, which is decoded) is reported correctly by all four commands.

How each one goes wrong:

  • Listing. get_all_filesystems() converts every MountInfo with platform::filesystem_from_mount(&mounts, m, None).ok() inside a filter_map (src/uu/df/src/df.rs:365-369). That ends in fs_usage() (src/uu/df/src/platform/unix.rs:27-34), which calls statfs(mount_dir); strace shows statfs("/tmp/…/usb\\012stick") = -1 ENOENT. The None becomes an Err, .ok() discards it, and the entry is dropped before the -a filter on the next line is consulted.
  • df FILE. mount_info_from_path() canonicalizes the operand and keeps the entries with path.starts_with(&mi.mount_dir), longest first (src/uu/df/src/filesystem.rs:104-105). The operand contains a real newline, mount_dir contains \012, so the entry for the file's own file system never matches and the enclosing mount wins. Source, type, sizes and mount point printed are the parent's.
  • stat -c %m FILE. populate_mount_list() collects the same mount_dir values (src/uu/stat/src/stat.rs:991) and find_mount_point() does the same prefix test (stat.rs:1106-1110), with the same result.

The undecoded path is not only a dead name. When a directory literally named usb\012stick (backslash, 0, 1, 2) is created next to the real mount point, statfs() succeeds on it: df -a then prints a newline-fs tmpfs row whose sizes are those of the parent file system (123638648 blocks instead of 1024), and df FILE / stat -c %m FILE for a plain file inside that directory report newline-fs / that directory as its mount point, where GNU reports overlay / /.

The partial decoder was introduced in 96c17312eadf ("df: fix display of special characters", 2025-01-16), first released in 0.0.29. Before that commit the mount point was not decoded at all, so a newline-named mount was already missed (together with space, tab and backslash names); that commit fixed three of the four escapes and left \012 out.

Impact

When a mount point's name contains a newline, that file system is missing from df, df -a and the df --total sum, and df PATH (including --output=fstype,source,target) and stat -c %m PATH report the parent file system for every path below it: its source, type, sizes and mount point. There is no message on stderr and the exit status is 0, so a user or script asking "which file system is this on" or "how much space is free here" gets the wrong answer without any sign of it. With the literal-name directory the reverse also holds: an ordinary directory is reported as a separate mount with the newline mount's source name and the parent's sizes.

Nothing is read, written or deleted by df or stat as a result; the consequence is limited to wrong output. The device number stat reports (%d) and every file operation are unaffected; only the mount-table lookups in df and stat %m go wrong, and no other utility reads the mount table. A mount point name containing a newline is unusual, which is what makes the case easy to overlook and also what limits how often anyone meets it; whoever does have such a mount gets the wrong answers every time.

Reproducing

repro is a single Python script with no companions. It needs root and permission to mount, which the privileged container provides. It runs the prebuilt /src/target/debug/coreutils and builds nothing. It finishes in well under a second.

Setup. The script creates a fresh directory /tmp/mount-newline-XXXXXXXX/ containing two directories, usb stick and usb<LF>stick, and mounts a 1 MiB tmpfs on each. The sources are space-fs and newline-fs. The only unusual input is the mount point name with a newline in it; root's mount creates both mounts. The kernel writes the names in the mount table as usb\040stick and usb\012stick, and the script prints both mount table lines. The two names differ in one character, so the space mount is the control: uutils decodes \040.

Ground truth. While the file systems are mounted, the script reads statvfs() (size in KiB) of both directories and st_dev of usb<LF>stick. These values come from the kernel, not from GNU.

Trigger. The script asks uutils (with LC_ALL=C) what an administrator would ask about each directory:

  • df -a --output=source: is the file system listed?
  • df --output=source,size DIR: which file system is DIR on, and how big is it?
  • stat --printf=%m DIR: what is DIR's mount point? The script then reads st_dev of the path uutils returned.

Both file systems are unmounted and the directories removed before any verdict is made.

Expected output. Three CONTROL lines for usb stick show df -a listing space-fs, df DIR reporting space-fs 1024 KiB, which matches statvfs, and %m naming the directory itself. Three OBSERVED lines for usb<LF>stick show:

  • df -a not listing newline-fs;
  • df DIR reporting the parent file system (here overlay 123638648 KiB), while statvfs says 1024 KiB;
  • %m returning /, whose st_dev differs from the directory's.

The last line is BUG when the control is correct on all three counts and the newline mount is wrong on all three. Otherwise it is OK. With either attached patch applied, the newline mount behaves like the control and the script prints OK.

Suggested fix

Two patches are attached. Both change src/uucore/src/lib/features/fsext/mod.rs (patch 1 also uucore's manifest and Cargo.lock), and with either one applied to a clean checkout and rebuilt, the reproducer prints OK. Patch 2 is the smaller change and the one to take.

Attached patch 2 keeps the existing bstr replace() chain in replace_special_chars() and adds the two escapes the kernel writes that were missing: \012 (newline) and \043 (#, escaped in the device name). \134 stays last, and a comment now says why: a literal \012 in a name arrives as \134012, and decoding the backslash first would turn it into a newline. MountInfo::new() now also decodes the source field (raw[after_fields + 1] at line 206, raw[0] for mtab at line 214) and the mount root (raw[3]), which were kept escaped before: a tmpfs mounted with source src#0 was listed by uutils df as src\0430, where GNU df shows src#0. New unit tests cover a newline and a literal \012 (\134012) in the mount point for mountinfo and mtab, and the decoding of root and device name.

Attached patch 1 fixes the same thing by rewriting replace_special_chars() as a single-pass decoder of any backslash followed by three octal digits whose value fits in a byte, the exact inverse of the kernel's encoding. It needs no list of escapes, and a decoded backslash can never be re-read as the start of another escape. It applies the decoder to the same three fields. Since the function no longer uses bstr, the patch also drops that optional dependency from src/uucore/Cargo.toml (and from the fsext feature), and from Cargo.lock. Its tests add non-escape cases (\08d, \400, a trailing backslash) to those of patch 2. On what the kernel actually writes it behaves the same as patch 2, with more churn.

One follow-up belongs in the same change, and neither patch contains it. Once the newline is decoded, df writes it byte for byte into the Mounted on column: with either patch, a mount point named a<LF>fake-fs 100 0 100 0% makes df print a second line that starts with fake-fs 100 0 100 0%. GNU df prints ? for control characters in that column (…/a?fake-fs 100 0 100 0% ). df's text cells should do the same (Cell::from_string and Cell::from_os_string, src/uu/df/src/table.rs:260 and :269); on its own, the decode fix turns a missing row into a forged-looking one.

Running the reproducer

bugs-not-security/ANT-2026-6LP5L261/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/bugs-not-security/ANT-2026-6LP5L261/repro:/tmp/repro:ro" oss-scan-7bc1eb6d1d6f /tmp/repro

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

sylvestre avatar Oct 25 '26 12:10 sylvestre

Hi! I'd like to work on this one. Plan: add the missing \012 -> newline case to replace_special_chars(), keeping \134 last so a decoded backslash can't form a new escape, plus a unit test for each of the four escapes. Does that sound right, or would you prefer a general octal decoder?

devmaiter avatar Nov 12 '26 23:11 devmaiter