diff --git a/Cargo.toml b/Cargo.toml index df2020dc..9b594bdc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cap-std-workspace" -version = "0.25.2" +version = "0.26.1" description = "Capability-based version of the Rust standard library" authors = [ "Dan Gohman ", @@ -17,11 +17,12 @@ exclude = ["/.*"] [dev-dependencies] async-std = { version = "1.10.0", features = ["attributes"] } anyhow = "1.0.37" -cap-async-std = { path = "cap-async-std", version = "^0.25.0" } -cap-fs-ext = { path = "cap-fs-ext", version = "^0.25.0" } -cap-directories = { path = "cap-directories", version = "^0.25.0" } -cap-std = { path = "cap-std", version = "^0.25.0" } -cap-tempfile = { path = "cap-tempfile", version = "^0.25.0" } +cap-async-std = { path = "cap-async-std", version = "^0.26.0-patch0" } +cap-fs-ext = { path = "cap-fs-ext", version = "^0.26.0-patch0" } +cap-directories = { path = "cap-directories", version = "^0.26.0-patch0" } +cap-std = { path = "cap-std", version = "^0.26.0-patch0" } +cap-tempfile = { path = "cap-tempfile", version = "^0.26.0-patch0" } +cap-rand = { path = "cap-rand", version = "^0.26.0-patch0" } rand = "0.8.1" tempfile = "3.1.0" camino = "1.0.5" diff --git a/cap-async-std/Cargo.toml b/cap-async-std/Cargo.toml index de741783..f4c8183e 100644 --- a/cap-async-std/Cargo.toml +++ b/cap-async-std/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cap-async-std" -version = "0.25.2" +version = "0.26.1" description = "Capability-based version of async-std" authors = [ "Dan Gohman ", @@ -16,7 +16,7 @@ edition = "2018" arf-strings = { version = "0.6.7", optional = true } # Enable "unstable" for `spawn_blocking`. async-std = { version = "1.10.0", features = ["attributes", "unstable"] } -cap-primitives = { path = "../cap-primitives", version = "^0.25.0" } +cap-primitives = { path = "../cap-primitives", version = "^0.26.0-patch0" } io-lifetimes = { version = "0.7.0", default-features = false, features = ["async-std"] } ipnet = "2.3.0" io-extras = { version = "0.15.0", features = ["use_async_std"] } diff --git a/cap-directories/Cargo.toml b/cap-directories/Cargo.toml index ef0cc133..ad187f0f 100644 --- a/cap-directories/Cargo.toml +++ b/cap-directories/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cap-directories" -version = "0.25.2" +version = "0.26.1" description = "Capability-based standard directories for config, cache and other data" authors = [ "Dan Gohman ", @@ -13,7 +13,7 @@ repository = "https://github.com/bytecodealliance/cap-std" edition = "2018" [dependencies] -cap-std = { path = "../cap-std", version = "^0.25.0" } +cap-std = { path = "../cap-std", version = "^0.26.0-patch0" } directories-next = "2.0.0" [target.'cfg(not(windows))'.dependencies] diff --git a/cap-fs-ext/Cargo.toml b/cap-fs-ext/Cargo.toml index fed8501c..52e44672 100644 --- a/cap-fs-ext/Cargo.toml +++ b/cap-fs-ext/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cap-fs-ext" -version = "0.25.2" +version = "0.26.1" description = "Extension traits for `Dir`, `File`, etc." authors = [ "Dan Gohman ", @@ -14,9 +14,9 @@ edition = "2018" [dependencies] arf-strings = { version = "0.6.7", optional = true } -cap-async-std = { path = "../cap-async-std", optional = true, version = "^0.25.0" } -cap-std = { path = "../cap-std", optional = true, version = "^0.25.0" } -cap-primitives = { path = "../cap-primitives", version = "^0.25.0" } +cap-async-std = { path = "../cap-async-std", optional = true, version = "^0.26.0-patch0" } +cap-std = { path = "../cap-std", optional = true, version = "^0.26.0-patch0" } +cap-primitives = { path = "../cap-primitives", version = "^0.26.0-patch0" } io-lifetimes = { version = "0.7.0", default-features = false } # Enable "unstable" for `spawn_blocking`. async-std = { version = "1.10.0", features = ["attributes", "unstable"], optional = true } diff --git a/cap-fs-ext/src/open_options_maybe_dir_ext.rs b/cap-fs-ext/src/open_options_maybe_dir_ext.rs index 52b687f8..a286adf0 100644 --- a/cap-fs-ext/src/open_options_maybe_dir_ext.rs +++ b/cap-fs-ext/src/open_options_maybe_dir_ext.rs @@ -1,11 +1,13 @@ /// Extension trait for `cap_primitives::fs::OpenOptions` which adds -/// `maybe_dir`, a function for controlling whether an open should -/// attempt to succeed on a directory. On Posix-ish platforms, opening -/// a directory always succeeds, but on Windows, opening a directory -/// needs this option. +/// `maybe_dir`, a function for controlling whether an open should attempt to +/// succeed on a directory. On Posix-ish platforms, opening a directory always +/// succeeds, but on Windows, opening a directory needs this option. pub trait OpenOptionsMaybeDirExt { - /// Sets the option for disabling an error that might be generated - /// by the opened object being a directory. + /// Sets the option for disabling an error that might be generated by the + /// opened object being a directory. + /// + /// On some platforms, this may prevent the directory from being deleted + /// or renamed while the handle is open. fn maybe_dir(&mut self, maybe_dir: bool) -> &mut Self; } diff --git a/cap-primitives/Cargo.toml b/cap-primitives/Cargo.toml index 3b452fb2..c142da61 100644 --- a/cap-primitives/Cargo.toml +++ b/cap-primitives/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cap-primitives" -version = "0.25.2" +version = "0.26.1" description = "Capability-based primitives" authors = [ "Dan Gohman ", diff --git a/cap-primitives/src/rustix/fs/dir_utils.rs b/cap-primitives/src/rustix/fs/dir_utils.rs index bb18ee02..e706b199 100644 --- a/cap-primitives/src/rustix/fs/dir_utils.rs +++ b/cap-primitives/src/rustix/fs/dir_utils.rs @@ -6,7 +6,7 @@ use std::ops::Deref; #[cfg(unix)] use std::os::unix::{ffi::OsStrExt, fs::OpenOptionsExt}; #[cfg(target_os = "wasi")] -use std::os::wasi::ffi::OsStrExt; +use std::os::wasi::{ffi::OsStrExt, fs::OpenOptionsExt}; use std::path::Path; #[cfg(racy_asserts)] use std::{ffi::OsString, os::unix::ffi::OsStringExt, path::PathBuf}; @@ -100,15 +100,15 @@ pub(crate) fn canonicalize_options() -> OpenOptions { /// This function is not sandboxed and may trivially access any path that the /// host process has access to. pub(crate) fn open_ambient_dir_impl(path: &Path, _: AmbientAuthority) -> io::Result { - // This is for `std::fs`, so we don't have `dir_required`, so set - // `O_DIRECTORY` manually. - let flags = OFlags::DIRECTORY | target_o_path(); - let mut options = fs::OpenOptions::new(); options.read(true); #[cfg(not(target_os = "wasi"))] - options.custom_flags(flags.bits() as i32); + // This is for `std::fs`, so we don't have `dir_required`, so set + // `O_DIRECTORY` manually. + options.custom_flags((OFlags::DIRECTORY | target_o_path()).bits() as i32); + #[cfg(target_os = "wasi")] + options.directory(true); options.open(&path) } diff --git a/cap-primitives/src/windows/fs/oflags.rs b/cap-primitives/src/windows/fs/oflags.rs index f2867e0a..8f5022a5 100644 --- a/cap-primitives/src/windows/fs/oflags.rs +++ b/cap-primitives/src/windows/fs/oflags.rs @@ -2,7 +2,7 @@ use crate::fs::{FollowSymlinks, OpenOptions}; use std::fs; use std::os::windows::fs::OpenOptionsExt; use windows_sys::Win32::Storage::FileSystem::{ - FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT, + FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT, FILE_SHARE_DELETE, }; /// Translate the given `cap_std` into `std` options. Also return a bool @@ -24,8 +24,15 @@ pub(in super::super) fn open_options_to_std(opts: &OpenOptions) -> (fs::OpenOpti opts.ext.custom_flags | FILE_FLAG_OPEN_REPARSE_POINT } }; + let mut share_mode = opts.ext.share_mode; if opts.maybe_dir { custom_flags |= FILE_FLAG_BACKUP_SEMANTICS; + + // Only allow `FILE_SHARE_READ` and `FILE_SHARE_WRITE`; this mirrors + // the values in `dir_options()` and is done to prevent directories + // from being deleted or renamed underneath cap-std's sandboxed path + // lookups on Windows. + share_mode &= !FILE_SHARE_DELETE; } let mut std_opts = fs::OpenOptions::new(); std_opts @@ -35,7 +42,7 @@ pub(in super::super) fn open_options_to_std(opts: &OpenOptions) -> (fs::OpenOpti .truncate(trunc) .create(opts.create) .create_new(opts.create_new) - .share_mode(opts.ext.share_mode) + .share_mode(share_mode) .custom_flags(custom_flags) .attributes(opts.ext.attributes); diff --git a/cap-rand/Cargo.toml b/cap-rand/Cargo.toml index 156da113..72509314 100644 --- a/cap-rand/Cargo.toml +++ b/cap-rand/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cap-rand" -version = "0.25.2" +version = "0.26.1" description = "Capability-based random number generators" authors = [ "Dan Gohman ", diff --git a/cap-rand/src/lib.rs b/cap-rand/src/lib.rs index db4e6b56..deded589 100644 --- a/cap-rand/src/lib.rs +++ b/cap-rand/src/lib.rs @@ -14,7 +14,8 @@ //! interface, it provides a `from_entropy` function which directly reads //! from the operating system entropy source. To preserve the //! capability-based interface, avoid using `rand::SeedableRng`'s -//! `from_entropy` function on any of the types that implement that trait. +//! `from_entropy` function on any of the types that implement that trait; use +//! [`std_rng_from_entropy`] instead. //! //! [`OsRng`]: crate::rngs::OsRng //! [`CapRng`]: crate::rngs::CapRng @@ -166,6 +167,19 @@ pub fn thread_rng(_: AmbientAuthority) -> rngs::CapRng { } } +/// Retrieve the standard random number generator, seeded by the system. +/// +/// This corresponds to [`rand::rngs::StdRng::from_entropy`]. +/// +/// # Ambient Authority +/// +/// This function makes use of ambient authority to access the platform entropy +/// source. +#[inline] +pub fn std_rng_from_entropy(_: AmbientAuthority) -> rngs::StdRng { + rand::rngs::StdRng::from_entropy() +} + /// Generates a random value using the thread-local random number generator. /// /// This corresponds to [`rand::random`]. diff --git a/cap-std/Cargo.toml b/cap-std/Cargo.toml index b805fa98..cac7b023 100644 --- a/cap-std/Cargo.toml +++ b/cap-std/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cap-std" -version = "0.25.2" +version = "0.26.1" description = "Capability-based version of the Rust standard library" authors = [ "Dan Gohman ", @@ -18,7 +18,7 @@ rustdoc-args = ["--cfg=doc_cfg"] [dependencies] arf-strings = { version = "0.6.7", optional = true } -cap-primitives = { path = "../cap-primitives", version = "^0.25.0" } +cap-primitives = { path = "../cap-primitives", version = "^0.26.0-patch0" } ipnet = "2.3.0" io-extras = "0.15.0" io-lifetimes = { version = "0.7.0", default-features = false } diff --git a/cap-tempfile/Cargo.toml b/cap-tempfile/Cargo.toml index 68538e3f..0ac44a21 100644 --- a/cap-tempfile/Cargo.toml +++ b/cap-tempfile/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cap-tempfile" -version = "0.25.2" +version = "0.26.1" description = "Capability-based temporary directories" authors = [ "Dan Gohman ", @@ -13,7 +13,7 @@ repository = "https://github.com/bytecodealliance/cap-std" edition = "2018" [dependencies] -cap-std = { path = "../cap-std", version = "^0.25.0" } +cap-std = { path = "../cap-std", version = "^0.26.0-patch0" } uuid = { version = "1.0.0", features = ["v4"] } camino = { version = "1.0.5", optional = true } diff --git a/cap-time-ext/Cargo.toml b/cap-time-ext/Cargo.toml index 8fa4df96..8c640e91 100644 --- a/cap-time-ext/Cargo.toml +++ b/cap-time-ext/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cap-time-ext" -version = "0.25.2" +version = "0.26.1" description = "Extension traits for `SystemClock` and `MonotonicClock`" authors = [ "Dan Gohman ", @@ -13,8 +13,8 @@ repository = "https://github.com/bytecodealliance/cap-std" edition = "2018" [dependencies] -cap-primitives = { path = "../cap-primitives", version = "^0.25.0" } -cap-std = { path = "../cap-std", optional = true, version = "^0.25.0" } +cap-primitives = { path = "../cap-primitives", version = "^0.26.0-patch0" } +cap-std = { path = "../cap-std", optional = true, version = "^0.26.0-patch0" } [target.'cfg(not(windows))'.dependencies] rustix = { version = "0.35.6", features = ["time"] } diff --git a/tests/rand.rs b/tests/rand.rs new file mode 100644 index 00000000..db00423a --- /dev/null +++ b/tests/rand.rs @@ -0,0 +1,5 @@ +#[test] +fn test_std_rng_from_entropy() { + let rng = cap_rand::std_rng_from_entropy(cap_rand::ambient_authority()); + assert_eq!(rng.clone(), rng); +}