Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: using -EBADF as a dirfd argument #1187

Closed
cyphar opened this issue Oct 17, 2024 · 1 comment · Fixed by #1189
Closed

fs: using -EBADF as a dirfd argument #1187

cyphar opened this issue Oct 17, 2024 · 1 comment · Fixed by #1189

Comments

@cyphar
Copy link

cyphar commented Oct 17, 2024

Some programs want to ensure that no paths are resolved relative to the current directory, and so use -EBADF as a file descriptor argument so that an error is returned if the path is not absolute. This is a fairly common pattern, but unfortunately rustix does not allow it:

thread 'utils::fd::tests::metadata_badfd' panicked at /home/cyphar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rustix-0.38.37/src/backend/linux_raw/conv.rs:165:5:
assertion failed: fd == crate::fs::CWD.as_raw_fd() || fd >= 0
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The one-line patch for this is fairly straightforward (only disallow -1 since that's an invalid BorrowedFd value) but I wanted to make sure that there wasn't a nicer solution (maybe we should just define a new CWD-like constant akin to no_fd so users can just re-use that).

sunfishcode added a commit that referenced this issue Oct 17, 2024
Add a `rustix::fs::ABS` constant, which corresponds to the undocumented
but commonly used convention of using `-EBADF` as the file descriptor in
`openat` and similar calls. This makes them fail if the path is not
absolute.

Fixes #1187.
@sunfishcode
Copy link
Member

sunfishcode commented Oct 17, 2024

Interesting, I hadn't heard of that convention before. I think defining a new CWD-like constant sounds good. I've now posted #1189 with a patch for this.

sunfishcode added a commit that referenced this issue Oct 18, 2024
Add a `rustix::fs::ABS` constant, which corresponds to the undocumented
but commonly used convention of using `-EBADF` as the file descriptor in
`openat` and similar calls. This makes them fail if the path is not
absolute.

Fixes #1187.
sunfishcode added a commit that referenced this issue Oct 27, 2024
Add a `rustix::fs::ABS` constant, which corresponds to the undocumented
but commonly used convention of using `-EBADF` as the file descriptor in
`openat` and similar calls. This makes them fail if the path is not
absolute.

Fixes #1187.
sunfishcode added a commit that referenced this issue Oct 27, 2024
Add a `rustix::fs::ABS` constant, which corresponds to the undocumented
but commonly used convention of using `-EBADF` as the file descriptor in
`openat` and similar calls. This makes them fail if the path is not
absolute.

Fixes #1187.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants