Skip to content

Commit

Permalink
liberate wasi-common from Rights (#6265)
Browse files Browse the repository at this point in the history
* tests: remove all use of rights for anything besides path_open read | write

* wasi-common and friends: delete all Caps from FileEntry and DirEntry

the sole thing rights are used to determine is whether a path_open
is opening for reading and writing.
  • Loading branch information
Pat Hickey authored Apr 27, 2023
1 parent edae6c0 commit 98501e4
Show file tree
Hide file tree
Showing 22 changed files with 136 additions and 814 deletions.
25 changes: 2 additions & 23 deletions crates/test-programs/wasi-tests/src/bin/directory_seek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@ unsafe fn test_directory_seek(dir_fd: wasi::Fd) {
wasi::path_create_directory(dir_fd, "dir").expect("failed to make directory");

// Open the directory and attempt to request rights for seeking.
let fd = wasi::path_open(
dir_fd,
0,
"dir",
wasi::OFLAGS_DIRECTORY,
wasi::RIGHTS_FD_SEEK,
0,
0,
)
.expect("failed to open file");
let fd = wasi::path_open(dir_fd, 0, "dir", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
.expect("failed to open file");
assert!(
fd > libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
Expand All @@ -27,19 +19,6 @@ unsafe fn test_directory_seek(dir_fd: wasi::Fd) {
wasi::ERRNO_BADF
);

// Check if we obtained the right to seek.
let fdstat = wasi::fd_fdstat_get(fd).expect("failed to fdstat");
assert_eq!(
fdstat.fs_filetype,
wasi::FILETYPE_DIRECTORY,
"expected the scratch directory to be a directory",
);
assert_eq!(
(fdstat.fs_rights_base & wasi::RIGHTS_FD_SEEK),
0,
"directory does NOT have the seek right",
);

// Clean up.
wasi::fd_close(fd).expect("failed to close fd");
wasi::path_remove_directory(dir_fd, "dir").expect("failed to remove dir");
Expand Down
7 changes: 1 addition & 6 deletions crates/test-programs/wasi-tests/src/bin/fd_advise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ unsafe fn test_fd_advise(dir_fd: wasi::Fd) {
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
| wasi::RIGHTS_FD_ADVISE
| wasi::RIGHTS_FD_FILESTAT_GET
| wasi::RIGHTS_FD_FILESTAT_SET_SIZE
| wasi::RIGHTS_FD_ALLOCATE,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
)
Expand Down
6 changes: 1 addition & 5 deletions crates/test-programs/wasi-tests/src/bin/fd_filestat_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ unsafe fn test_fd_filestat_set(dir_fd: wasi::Fd) {
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
| wasi::RIGHTS_FD_FILESTAT_GET
| wasi::RIGHTS_FD_FILESTAT_SET_SIZE
| wasi::RIGHTS_FD_FILESTAT_SET_TIMES,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
)
Expand Down
6 changes: 1 addition & 5 deletions crates/test-programs/wasi-tests/src/bin/fd_flags_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ unsafe fn test_fd_fdstat_set_flags(dir_fd: wasi::Fd) {
0,
FILE_NAME,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
| wasi::RIGHTS_FD_SEEK
| wasi::RIGHTS_FD_TELL
| wasi::RIGHTS_FD_FDSTAT_SET_FLAGS,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
wasi::FDFLAGS_APPEND,
)
Expand Down
22 changes: 4 additions & 18 deletions crates/test-programs/wasi-tests/src/bin/fd_readdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
| wasi::RIGHTS_FD_READDIR
| wasi::RIGHTS_FD_FILESTAT_GET,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
)
Expand All @@ -119,16 +116,8 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
wasi::fd_close(file_fd).expect("closing a file");

wasi::path_create_directory(dir_fd, "nested").expect("create a directory");
let nested_fd = wasi::path_open(
dir_fd,
0,
"nested",
0,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_READDIR | wasi::RIGHTS_FD_FILESTAT_GET,
0,
0,
)
.expect("failed to open nested directory");
let nested_fd =
wasi::path_open(dir_fd, 0, "nested", 0, 0, 0, 0).expect("failed to open nested directory");
assert!(
nested_fd > file_fd,
"nested directory file descriptor range check",
Expand Down Expand Up @@ -190,10 +179,7 @@ unsafe fn test_fd_readdir_lots(dir_fd: wasi::Fd) {
0,
&format!("file.{}", count),
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
| wasi::RIGHTS_FD_READDIR
| wasi::RIGHTS_FD_FILESTAT_GET,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
)
Expand Down
5 changes: 1 addition & 4 deletions crates/test-programs/wasi-tests/src/bin/file_allocate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ unsafe fn test_file_allocate(dir_fd: wasi::Fd) {
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
| wasi::RIGHTS_FD_ALLOCATE
| wasi::RIGHTS_FD_FILESTAT_GET,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ unsafe fn test_file_pread_pwrite(dir_fd: wasi::Fd) {
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_SEEK | wasi::RIGHTS_FD_WRITE,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/wasi-tests/src/bin/file_seek_tell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ unsafe fn test_file_seek_tell(dir_fd: wasi::Fd) {
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE | wasi::RIGHTS_FD_SEEK | wasi::RIGHTS_FD_TELL,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
)
Expand Down
23 changes: 3 additions & 20 deletions crates/test-programs/wasi-tests/src/bin/path_filestat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ use std::{env, process};
use wasi_tests::{assert_errno, open_scratch_directory, TESTCONFIG};

unsafe fn test_path_filestat(dir_fd: wasi::Fd) {
let mut fdstat = wasi::fd_fdstat_get(dir_fd).expect("fd_fdstat_get");
assert_ne!(
fdstat.fs_rights_base & wasi::RIGHTS_PATH_FILESTAT_GET,
0,
"the scratch directory should have RIGHT_PATH_FILESTAT_GET as base right",
);

let fdflags = if TESTCONFIG.support_fdflags_sync() {
wasi::FDFLAGS_APPEND | wasi::FDFLAGS_SYNC
} else {
Expand All @@ -21,7 +14,7 @@ unsafe fn test_path_filestat(dir_fd: wasi::Fd) {
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE | wasi::RIGHTS_PATH_FILESTAT_GET,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
// Pass some flags for later retrieval
fdflags,
Expand All @@ -32,17 +25,7 @@ unsafe fn test_path_filestat(dir_fd: wasi::Fd) {
"file descriptor range check",
);

fdstat = wasi::fd_fdstat_get(file_fd).expect("fd_fdstat_get");
assert_eq!(
fdstat.fs_rights_base & wasi::RIGHTS_PATH_FILESTAT_GET,
0,
"files shouldn't have rights for path_* syscalls even if manually given",
);
assert_eq!(
fdstat.fs_rights_inheriting & wasi::RIGHTS_PATH_FILESTAT_GET,
0,
"files shouldn't have rights for path_* syscalls even if manually given",
);
let fdstat = wasi::fd_fdstat_get(file_fd).expect("fd_fdstat_get");
assert_eq!(
fdstat.fs_flags & wasi::FDFLAGS_APPEND,
wasi::FDFLAGS_APPEND,
Expand All @@ -63,7 +46,7 @@ unsafe fn test_path_filestat(dir_fd: wasi::Fd) {
0,
"file",
0,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE | wasi::RIGHTS_PATH_FILESTAT_GET,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
wasi::FDFLAGS_SYNC,
)
Expand Down
11 changes: 2 additions & 9 deletions crates/test-programs/wasi-tests/src/bin/path_link.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
use std::{env, process};
use wasi_tests::{assert_errno, create_file, open_scratch_directory, TESTCONFIG};

const TEST_RIGHTS: wasi::Rights = wasi::RIGHTS_FD_READ
| wasi::RIGHTS_PATH_LINK_SOURCE
| wasi::RIGHTS_PATH_LINK_TARGET
| wasi::RIGHTS_FD_FILESTAT_GET
| wasi::RIGHTS_PATH_OPEN
| wasi::RIGHTS_PATH_UNLINK_FILE;

unsafe fn create_or_open(dir_fd: wasi::Fd, name: &str, flags: wasi::Oflags) -> wasi::Fd {
let file_fd = wasi::path_open(dir_fd, 0, name, flags, TEST_RIGHTS, TEST_RIGHTS, 0)
let file_fd = wasi::path_open(dir_fd, 0, name, flags, 0, 0, 0)
.unwrap_or_else(|_| panic!("opening '{}'", name));
assert!(
file_fd > libc::STDERR_FILENO as wasi::Fd,
Expand All @@ -19,7 +12,7 @@ unsafe fn create_or_open(dir_fd: wasi::Fd, name: &str, flags: wasi::Oflags) -> w
}

unsafe fn open_link(dir_fd: wasi::Fd, name: &str) -> wasi::Fd {
let file_fd = wasi::path_open(dir_fd, 0, name, 0, TEST_RIGHTS, TEST_RIGHTS, 0)
let file_fd = wasi::path_open(dir_fd, 0, name, 0, 0, 0, 0)
.unwrap_or_else(|_| panic!("opening a link '{}'", name));
assert!(
file_fd > libc::STDERR_FILENO as wasi::Fd,
Expand Down

This file was deleted.

14 changes: 3 additions & 11 deletions crates/test-programs/wasi-tests/src/bin/poll_oneoff_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,8 @@ unsafe fn test_fd_readwrite_valid_fd(dir_fd: wasi::Fd) {
wasi::fd_close(nonempty_file).expect("close");

// Now open the file for reading
let readable_fd = wasi::path_open(
dir_fd,
0,
"readable_file",
0,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_POLL_FD_READWRITE,
0,
0,
)
.expect("opening a readable file");
let readable_fd = wasi::path_open(dir_fd, 0, "readable_file", 0, wasi::RIGHTS_FD_READ, 0, 0)
.expect("opening a readable file");

assert!(
readable_fd > libc::STDERR_FILENO as wasi::Fd,
Expand All @@ -218,7 +210,7 @@ unsafe fn test_fd_readwrite_valid_fd(dir_fd: wasi::Fd) {
0,
"writable_file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_WRITE | wasi::RIGHTS_POLL_FD_READWRITE,
wasi::RIGHTS_FD_WRITE,
0,
0,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let mut file = std::fs::File::open("").unwrap();
let mut buffer = Vec::new();
std::io::Read::read_to_end(&mut file, &mut buffer).unwrap();
}
9 changes: 1 addition & 8 deletions crates/test-programs/wasi-tests/src/bin/symlink_filestat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@ use std::{env, process};
use wasi_tests::open_scratch_directory;

unsafe fn test_path_filestat(dir_fd: wasi::Fd) {
let fdstat = wasi::fd_fdstat_get(dir_fd).expect("fd_fdstat_get");
assert_ne!(
fdstat.fs_rights_base & wasi::RIGHTS_PATH_FILESTAT_GET,
0,
"the scratch directory should have RIGHT_PATH_FILESTAT_GET as base right",
);

// Create a file in the scratch directory.
let file_fd = wasi::path_open(
dir_fd,
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE | wasi::RIGHTS_PATH_FILESTAT_GET,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
)
Expand Down
22 changes: 2 additions & 20 deletions crates/test-programs/wasi-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ pub fn open_scratch_directory(path: &str) -> Result<wasi::Fd, String> {
}
dst.set_len(stat.u.dir.pr_name_len);
if dst == path.as_bytes() {
let (base, inherit) = fd_get_rights(i);
return Ok(
wasi::path_open(i, 0, ".", wasi::OFLAGS_DIRECTORY, base, inherit, 0)
.expect("failed to open dir"),
);
return Ok(wasi::path_open(i, 0, ".", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
.expect("failed to open dir"));
}
}

Expand All @@ -49,21 +46,6 @@ pub unsafe fn create_file(dir_fd: wasi::Fd, filename: &str) {
wasi::fd_close(file_fd).expect("closing a file");
}

// Returns: (rights_base, rights_inheriting)
pub unsafe fn fd_get_rights(fd: wasi::Fd) -> (wasi::Rights, wasi::Rights) {
let fdstat = wasi::fd_fdstat_get(fd).expect("fd_fdstat_get failed");
(fdstat.fs_rights_base, fdstat.fs_rights_inheriting)
}

pub unsafe fn drop_rights(fd: wasi::Fd, drop_base: wasi::Rights, drop_inheriting: wasi::Rights) {
let (current_base, current_inheriting) = fd_get_rights(fd);

let new_base = current_base & !drop_base;
let new_inheriting = current_inheriting & !drop_inheriting;

wasi::fd_fdstat_set_rights(fd, new_base, new_inheriting).expect("dropping fd rights");
}

#[macro_export]
macro_rules! assert_errno {
($s:expr, windows => $i:expr, $( $rest:tt )+) => {
Expand Down
Loading

0 comments on commit 98501e4

Please sign in to comment.