uutils/coreutils · error · io::Error

invalid file descriptor

Error message

invalid file descriptor

What it means

Error "invalid file descriptor" thrown in uutils/coreutils.

Source

Thrown at src/uucore/src/lib/features/safe_traversal.rs:467

                source: err,
            }
            .into());
        }
        Ok(())
    }

    /// Open a file for writing relative to this directory
    /// Creates the file if it doesn't exist, truncates if it does
    pub fn open_file_at(&self, name: &OsStr) -> io::Result<fs::File> {
        let name_cstr =
            CString::new(name.as_bytes()).map_err(|_| SafeTraversalError::PathContainsNull)?;
        // O_NOFOLLOW: `openat` anchors the *directory*, not the final component,
        // so without it a symlink planted at `name` is still followed and its
        // target truncated. Callers reach here right after unlinking `name`,
        // which is precisely the window an attacker races.
        let flags = OFlag::O_CREAT
            | OFlag::O_WRONLY
            | OFlag::O_TRUNC
            | OFlag::O_CLOEXEC
            | OFlag::O_NOFOLLOW;
        let mode = Mode::from_bits_truncate(0o666); // Default file permissions

        let fd: OwnedFd = openat(self.fd.as_fd(), name_cstr.as_c_str(), flags, mode)
            .map_err(|e| io::Error::from_raw_os_error(e as i32))?;

        Ok(fs::File::from(fd))
    }

    /// Create a DirFd from an existing file descriptor (takes ownership)
    pub fn from_raw_fd(fd: RawFd) -> io::Result<Self> {
        if fd < 0 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                translate!("safe-traversal-error-invalid-fd"),
            ));
        }

View on GitHub (pinned to 85295bbf78)

Solutions

  1. Ensure the file descriptor is still open when used; do not close it before traversal completes.
  2. Open the directory with the correct flags so the descriptor is valid for *at syscalls.

When it happens

Trigger: Occurs during safe directory traversal when a file descriptor becomes invalid (EBADF), e.g. closed concurrently.

Common situations: Traversal racing with another thread/process closing descriptors, or use-after-close in directory walking.


AI-assisted analysis of uutils/coreutils@85295bbf78 (2026-08-19). Data as JSON: /api/errors/b671bd1eac833829. Report an issue: GitHub.