Skip to content

Commit

Permalink
Document the behavior of some rights-related functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
marmistrz committed Jan 17, 2020
1 parent 448faed commit 919190e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
6 changes: 2 additions & 4 deletions crates/wasi-common/src/hostcalls_impl/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,8 @@ pub(crate) unsafe fn path_open(
let fd = hostcalls_impl::path_open(resolved, read, write, oflags, fs_flags)?;

let mut fe = FdEntry::from(fd)?;
// We need to manually deny the rights which are not explicitly requested.
// This should not be needed, but currently determine_type_and_access_rights,
// which is used by FdEntry::from, may grant extra rights while inferring it
// from the open mode.
// We need to manually deny the rights which are not explicitly requested
// because FdEntry::from will assign maximal consistent rights.
fe.rights_base &= fs_rights_base;
fe.rights_inheriting &= fs_rights_inheriting;
let guest_fd = wasi_ctx.insert_fd_entry(fe)?;
Expand Down
3 changes: 3 additions & 0 deletions crates/wasi-common/src/old/snapshot_0/fdentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ pub(crate) struct FdEntry {
}

impl FdEntry {
/// Create an FdEntry with *maximal* possible rights from a given `File`.
/// If this is not desired, the rights of the resulting `FdEntry` should
/// be manually restricted.
pub(crate) fn from(file: fs::File) -> Result<Self> {
unsafe { determine_type_and_access_rights(&file) }.map(
|(file_type, rights_base, rights_inheriting)| Self {
Expand Down
5 changes: 5 additions & 0 deletions crates/wasi-common/src/sys/unix/fdentry_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub(crate) fn descriptor_as_oshandle<'lifetime>(
})))
}

/// Returns the set of all possible rights that are both relevant for the file
/// type and consistent with the open mode.
///
/// This function is unsafe because it operates on a raw file descriptor.
pub(crate) unsafe fn determine_type_and_access_rights<Fd: AsRawFd>(
fd: &Fd,
Expand All @@ -48,6 +51,8 @@ pub(crate) unsafe fn determine_type_and_access_rights<Fd: AsRawFd>(
Ok((file_type, rights_base, rights_inheriting))
}

/// Returns the set of all possible rights that are relevant for file type.
///
/// This function is unsafe because it operates on a raw file descriptor.
pub(crate) unsafe fn determine_type_rights<Fd: AsRawFd>(
fd: &Fd,
Expand Down
9 changes: 7 additions & 2 deletions crates/wasi-common/src/sys/windows/fdentry_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ pub(crate) fn descriptor_as_oshandle<'lifetime>(
})))
}

/// This function is unsafe because it operates on a raw file handle.
/// Returns the set of all possible rights that are both relevant for the file
/// type and consistent with the open mode.
///
/// This function is unsafe because it operates on a raw file descriptor.
pub(crate) unsafe fn determine_type_and_access_rights<Handle: AsRawHandle>(
handle: &Handle,
) -> Result<(
Expand Down Expand Up @@ -85,7 +88,9 @@ pub(crate) unsafe fn determine_type_and_access_rights<Handle: AsRawHandle>(
Ok((file_type, rights_base, rights_inheriting))
}

/// This function is unsafe because it operates on a raw file handle.
/// Returns the set of all possible rights that are relevant for file type.
///
/// This function is unsafe because it operates on a raw file descriptor.
pub(crate) unsafe fn determine_type_rights<Handle: AsRawHandle>(
handle: &Handle,
) -> Result<(
Expand Down

0 comments on commit 919190e

Please sign in to comment.