Allow WASI preopen file descriptors to be closed. (#5828)

Early on in WASI, we weren't sure whether we should allow preopens to be
closed, so conservatively, we disallowed them. Among other things, this
protected assumptions in wasi-libc that it can hold onto preopen file
descriptors and rely on them always being open.

However now, I think it makes sense to relax this restriction. wasi-libc
itself doesn't expose the preopen file descriptors, so users shouldn't
ever be closing them naively, unless they have wild closes. And
toolchains other than wasi-libc may want to close preopens as a way to
drop priveleges once the main file handles are opened.
This commit is contained in:
Dan Gohman
2023-02-24 13:06:38 -08:00
committed by GitHub
parent fb2cbec34a
commit 67e2e57b02
4 changed files with 54 additions and 41 deletions

View File

@@ -218,21 +218,12 @@ impl DirFdStat {
pub(crate) trait TableDirExt {
fn get_dir(&self, fd: u32) -> Result<Arc<DirEntry>, Error>;
fn is_preopen(&self, fd: u32) -> bool;
}
impl TableDirExt for crate::table::Table {
fn get_dir(&self, fd: u32) -> Result<Arc<DirEntry>, Error> {
self.get(fd)
}
fn is_preopen(&self, fd: u32) -> bool {
if self.is::<DirEntry>(fd) {
let dir_entry: Arc<DirEntry> = self.get(fd).unwrap();
dir_entry.preopen_path.is_some()
} else {
false
}
}
}
#[derive(Debug, Clone)]

View File

@@ -145,12 +145,6 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
if table.is::<FileEntry>(fd) {
let _ = table.delete::<FileEntry>(fd);
} else if table.is::<DirEntry>(fd) {
// We cannot close preopened directories
let dir_entry: Arc<DirEntry> = table.get(fd).unwrap();
if dir_entry.preopen_path().is_some() {
return Err(Error::not_supported().context("cannot close propened directory"));
}
drop(dir_entry);
let _ = table.delete::<DirEntry>(fd);
} else {
return Err(Error::badf().context("key does not refer to file or directory"));
@@ -533,9 +527,6 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
if !table.contains_key(from) {
return Err(Error::badf());
}
if table.is_preopen(from) || table.is_preopen(to) {
return Err(Error::not_supported().context("cannot renumber a preopen"));
}
table.renumber(from, to)
}