-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Refactored docs for std::fs::set_permissions_nofollow + fix BSD-based systems to use fchmodat with AT_SYMLINK_NOFOLLOW flag
#160170
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
base: main
Are you sure you want to change the base?
Changes from all commits
69de9ae
a793546
c1de9c9
b89b539
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2036,35 +2036,76 @@ pub fn set_perm(p: &CStr, perm: FilePermissions) -> io::Result<()> { | |
| } | ||
|
|
||
| pub fn set_perm_nofollow(p: &CStr, perm: FilePermissions) -> io::Result<()> { | ||
| // ESP-IDF and Horizon do not support O_NOFOLLOW, so we skip setting it. | ||
| // Their filesystems do not have symbolic links, so no special handling is required. | ||
| cfg_select! { | ||
| // wasm32-wasip1 targets do not support fchmodat, so we fall down to | ||
| // open + fchmod | ||
| target_os = "wasi" => { | ||
| use crate::fs::OpenOptions; | ||
| use crate::fs::Permissions; | ||
| use crate::os::wasi::ffi::OsStrExt; | ||
| use crate::os::wasi::fs::OpenOptionsExt; | ||
| #[inline] | ||
| /// Helper function for fallback open with `O_NOFOLLOW` + `fchmod` behavior | ||
| fn open_and_set_permissions(p: &CStr, perm: FilePermissions) -> io::Result<()> { | ||
| use crate::fs::{OpenOptions, Permissions}; | ||
|
|
||
| let mut options = OpenOptions::new(); | ||
| options.custom_flags(libc::O_NOFOLLOW); | ||
| let mut options = OpenOptions::new(); | ||
|
|
||
| let bytes = p.to_bytes(); | ||
| let os_str = OsStr::from_bytes(bytes); | ||
| options.open(Path::new(os_str))?.set_permissions(Permissions::from_inner(perm)) | ||
| // ESP-IDF and Horizon do not support O_NOFOLLOW, so we skip setting it. | ||
| // Their filesystems do not have symbolic links, so no special handling is required. | ||
| #[cfg(not(any(target_os = "espidf", target_os = "horizon")))] | ||
| { | ||
| #[cfg(not(target_os = "wasi"))] | ||
| use crate::os::unix::fs::OpenOptionsExt; | ||
| #[cfg(target_os = "wasi")] | ||
| use crate::os::wasi::fs::OpenOptionsExt; | ||
| options.read(true).custom_flags(libc::O_NOFOLLOW); | ||
| } | ||
| all(target_os = "linux", not(any(target_os = "espidf", target_os = "horizon"))) => { | ||
| cvt_r(|| unsafe { | ||
| libc::fchmodat(libc::AT_FDCWD, p.as_ptr(), perm.mode, libc::AT_SYMLINK_NOFOLLOW) | ||
| }) | ||
| .map(|_| ()) | ||
| }, | ||
| _ => { | ||
| cvt_r(|| unsafe { | ||
| libc::fchmodat(libc::AT_FDCWD, p.as_ptr(), perm.mode, 0) | ||
| }) | ||
| .map(|_| ()) | ||
|
|
||
| #[cfg(not(target_os = "wasi"))] | ||
| use crate::os::unix::ffi::OsStrExt; | ||
| #[cfg(target_os = "wasi")] | ||
| use crate::os::wasi::ffi::OsStrExt; | ||
|
|
||
| let os_str = OsStr::from_bytes(p.to_bytes()); | ||
| options.open(Path::new(os_str))?.set_permissions(Permissions::from_inner(perm)) | ||
| } | ||
|
|
||
| let mut _res: Result<(), core::io::Error> = Err(crate::io::ErrorKind::Unsupported.into()); | ||
|
|
||
| // These platforms support `fchmodat`, so utilize this syscall over `open` + `fchmod` | ||
| #[cfg(any( | ||
| target_os = "linux", | ||
| target_os = "macos", | ||
| target_os = "freebsd", | ||
| target_os = "openbsd", | ||
| target_os = "netbsd", | ||
| target_os = "dragonfly", | ||
| target_os = "android", | ||
| target_os = "qnx" | ||
| ))] | ||
| { | ||
| _res = cvt_r(|| unsafe { | ||
| libc::fchmodat(libc::AT_FDCWD, p.as_ptr(), perm.mode, libc::AT_SYMLINK_NOFOLLOW) | ||
| }) | ||
| .map(|_| ()); | ||
| } | ||
|
|
||
| // If fchmodat fails with `ErrorKind::Unsupported` fallback to using open + fchmod. This is just in case | ||
| // for older systems like Ubuntu 20.04 where fchmodat fails with EOPNOTSUPP on both regular files and | ||
| // symlinks when AT_SYMLINK_NOFOLLOW is passed in. | ||
| match _res { | ||
| Ok(_) => Ok(()), | ||
| Err(err) => { | ||
| if err.kind() == crate::io::ErrorKind::Unsupported { | ||
| match open_and_set_permissions(p, perm) { | ||
| Ok(_) => return Ok(()), | ||
| Err(e) => { | ||
| if e.kind() == crate::io::ErrorKind::FilesystemLoop { | ||
| // When O_NOFOLLOW flag is enabled, if the trailing component of | ||
| // a path is a symbolic link, open should fail with ELOOP error. | ||
| // For consistency with other Linux distributions, we return | ||
| // `ErrorKind::Unsupported`. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this comment. Which "other distributions"? Do different Linuxes behave different here, despite all using the same kernel? Which system returns "unsupported" to indicate a symlink?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See Rachel's comment earlier. Ubuntu 20.04 throws Maybe consistency was a bad choice of word here, but I thought it was better to keep the error message to what fchmodat would fail with (
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't that just mean that Ubuntu 20 has an old kernel that does not yet support
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
And the consistency thing is bad wording on my end. I might've even wrote that comment before getting rid of the I'll make changes to the comment in an amended commit later today.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I assume not just later Ubuntu, but all recent Linux distros? Ubuntu uses a fairly standard kernel AFAIK? The man page just says so it is a bit odd that we get "unsupported" at all... |
||
| return Err(err); | ||
| } | ||
| return Err(e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Err(err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
View changes since the review