Skip to content

Don't apply a mask on the flags in PermissionsExt. #256

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

Merged
merged 2 commits into from
Jun 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cap-primitives/src/rustix/fs/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io;

#[cfg(any(target_os = "android", target_os = "linux"))]
#[cold]
pub(crate) fn invalid_flags() -> io::Error {
rustix::io::Errno::INVAL.into()
6 changes: 2 additions & 4 deletions cap-primitives/src/rustix/fs/permissions_ext.rs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ impl PermissionsExt {
pub(crate) fn from_std(std: fs::Permissions) -> Self {
use std::os::unix::fs::PermissionsExt;
Self {
mode: std.mode() as RawMode & 0o7777,
mode: std.mode() as RawMode,
}
}

@@ -26,9 +26,7 @@ impl PermissionsExt {
pub(crate) const fn from_raw_mode(mode: RawMode) -> Permissions {
Permissions {
readonly: Self::readonly(mode),
ext: Self {
mode: mode & 0o7777,
},
ext: Self { mode },
}
}

8 changes: 4 additions & 4 deletions cap-primitives/src/rustix/fs/set_permissions_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::fs::{errors, open, OpenOptions, Permissions};
use crate::fs::{open, OpenOptions, Permissions};
use rustix::fs::{fchmod, Mode};
use rustix::io::Errno;
use std::convert::TryInto;
@@ -43,8 +43,8 @@ pub(crate) fn set_permissions_impl(
}

pub(crate) fn set_file_permissions(file: &fs::File, perm: fs::Permissions) -> io::Result<()> {
#[allow(clippy::useless_conversion)]
let mode =
Mode::from_bits(perm.mode().try_into().unwrap()).ok_or_else(errors::invalid_flags)?;
// Use `from_bits_truncate` for compatibility with std, which allows
// non-permission bits to propagate through.
let mode = Mode::from_bits_truncate(perm.mode().try_into().unwrap());
Ok(fchmod(file, mode)?)
}
6 changes: 4 additions & 2 deletions cap-primitives/src/rustix/linux/fs/set_permissions_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::procfs::set_permissions_through_proc_self_fd;
use crate::fs::{errors, open, OpenOptions, Permissions};
use crate::fs::{open, OpenOptions, Permissions};
use rustix::fs::{fchmod, Mode, RawMode};
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
@@ -48,6 +48,8 @@ pub(crate) fn set_permissions_impl(
}

fn set_file_permissions(file: &fs::File, perm: fs::Permissions) -> io::Result<()> {
let mode = Mode::from_bits(perm.mode() as RawMode).ok_or_else(errors::invalid_flags)?;
// Use `from_bits_truncate` for compatibility with std, which allows
// non-permission bits to propagate through.
let mode = Mode::from_bits_truncate(perm.mode() as RawMode);
Ok(fchmod(file, mode)?)
}
4 changes: 1 addition & 3 deletions tests/fs_additional.rs
Original file line number Diff line number Diff line change
@@ -931,9 +931,7 @@ fn check_metadata(std: &std::fs::Metadata, cap: &cap_std::fs::Metadata) {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
// The standard library returns file format bits with `mode()`, whereas
// cap-std currently doesn't.
assert_eq!(std.permissions().mode() & 0o7777, cap.permissions().mode());
assert_eq!(std.permissions().mode(), cap.permissions().mode());
}

// If the standard library supports file modified/accessed/created times,