Skip to content

fix Wasi rights system to work with wasi-libc#6463

Merged
pchickey merged 12 commits intomainfrom
pch/wasi_read_write_rights
May 30, 2023
Merged

fix Wasi rights system to work with wasi-libc#6463
pchickey merged 12 commits intomainfrom
pch/wasi_read_write_rights

Conversation

@pchickey
Copy link
Contributor

@pchickey pchickey commented May 26, 2023

This is an upstreaming of #6462

#6265 introduced a regression with programs using wasi-libc, reported at WebAssembly/wasi-libc#415.

Wasi-libc read the rights of the base directory (using fd_fdstat_get) and used those to mask the rights requested to path_open. In 6265, I changed the behavior of fdstat_get to always report and empty set of rights. This means that Wasi-libc will always pass an empty set of rights to path_open, which is a problem because the FD_READ and FD_WRITE rights are how path_open determines if a descriptor is to be opened for reading, writing, or both.

The fix is as follows:

  • directories always return the full set of rights in fd_fdstat_get.
  • legacy implementation records the access mode that a file is opened with, and use that to set the FD_READ and FD_WRITE bits in fs_rights_base for a file's fd_fdstat_get. The preview 2 implementation already did this.
  • A test demonstrates the behavior of the fdstat rights bits, and that opening for reading, writing, or reading and writing behaves correctly when calling fd_read and fd_write

Preview 2's wit definition was unintentionally (per @sunfishcode) missing a way for filesystem.{read,write,append}-via-stream to return an error-code when a descriptor cannot be used to create such a stream. I added this to the wit definition in order to make the preview 2 behavior match the legacy implementation.

@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:wasm labels May 26, 2023
@pchickey pchickey force-pushed the pch/wasi_read_write_rights branch from 827cf5e to 5b3c019 Compare May 26, 2023 17:47
@pchickey pchickey marked this pull request as ready for review May 26, 2023 17:50
@pchickey pchickey requested a review from a team as a code owner May 26, 2023 17:50
@pchickey pchickey requested review from itsrainy and removed request for a team May 26, 2023 17:50
@pchickey pchickey requested a review from a team as a code owner May 26, 2023 17:54
@pchickey pchickey enabled auto-merge May 26, 2023 17:58
@pchickey pchickey removed the request for review from itsrainy May 26, 2023 17:58
@github-actions github-actions bot added the wasi Issues pertaining to WASI label May 26, 2023
@github-actions
Copy link

Subscribe to Label Action

cc @kubkon

Details This issue or pull request has been labeled: "cranelift", "cranelift:wasm", "wasi"

Thus the following users have been cc'd because of the following labels:

  • kubkon: wasi

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@pchickey pchickey force-pushed the pch/wasi_read_write_rights branch 2 times, most recently from e7831fe to 0bd2695 Compare May 26, 2023 20:33
@pchickey pchickey added this pull request to the merge queue May 30, 2023
@pchickey pchickey removed this pull request from the merge queue due to a manual request May 30, 2023
Pat Hickey added 11 commits May 30, 2023 15:18
implementations such as wasi-libc use these as a mask for any rights
they request for a new fd when invoking path_open
…am creation

which will mean editing the wit.
…n error-code

in order to fail appropriately when a file is not open for reading or
writing.
I appreciate his work on this stuff in the past but he hasnt been around
for a few years now
since file perms is now more accurately the file's access mode, a file
open for writing was having problems here.

in reality i dont think it makes sense to restrict this based on the
perms. if a file is opened, you should be able to stat it.

this fixes the host adapter's path_link test, which was broken by prior
changes.  additionally, this fix lets the path_open_dirfd_not_dir test
pass.
in order to work with wasi-testsuite, it needs to be possible to
path_open(dirfd, ".", ...) with the same rights reported in the
fdstat of that dirfd. When we report the Rights::all() set, both
FD_READ and FD_WRITE are set in the base rights, which results in
unix rejecting an openat2(dirfd, ".", O_RDWR) with EISDIR.

By not having the FD_READ and FD_WRITE rights present in the base
rights, the open syscall defaults to O_RDONLY, which is the only
access mode allowed for opening directories.
@pchickey pchickey force-pushed the pch/wasi_read_write_rights branch from eb3a07f to e063785 Compare May 30, 2023 22:21
@pchickey pchickey added this pull request to the merge queue May 30, 2023
Merged via the queue into main with commit bffdbce May 30, 2023
@pchickey pchickey deleted the pch/wasi_read_write_rights branch May 30, 2023 23:34
dhil pushed a commit to dhil/wasmtime that referenced this pull request Jun 5, 2023
* wasi-common: need FileEntry to track the mode with which a file was opened

* add a test for read, write access modes in path_open

* a directory needs to report full set of rights in fdstat

implementations such as wasi-libc use these as a mask for any rights
they request for a new fd when invoking path_open

* preview2: mask file perms. and we really need to enforce them on stream creation

which will mean editing the wit.

* filesystem.wit: make {read, write, append}-via-stream fallible with an error-code

in order to fail appropriately when a file is not open for reading or
writing.

* filesystem.wit: update comments

* preview2 impls: fix error handling for {read,write,append}_via_stream

* wasi-common: normalize wrong access mode error on windows to badf

* remove kubkob from labeler

I appreciate his work on this stuff in the past but he hasnt been around
for a few years now

* preview 2 filesystem: allow stat on any opened fd

since file perms is now more accurately the file's access mode, a file
open for writing was having problems here.

in reality i dont think it makes sense to restrict this based on the
perms. if a file is opened, you should be able to stat it.

this fixes the host adapter's path_link test, which was broken by prior
changes.  additionally, this fix lets the path_open_dirfd_not_dir test
pass.

* fix the directory base & inheriting rights

in order to work with wasi-testsuite, it needs to be possible to
path_open(dirfd, ".", ...) with the same rights reported in the
fdstat of that dirfd. When we report the Rights::all() set, both
FD_READ and FD_WRITE are set in the base rights, which results in
unix rejecting an openat2(dirfd, ".", O_RDWR) with EISDIR.

By not having the FD_READ and FD_WRITE rights present in the base
rights, the open syscall defaults to O_RDONLY, which is the only
access mode allowed for opening directories.

* path_open of a directory with read and write succeeds on windows
@squeek502
Copy link

The bug that this fixed has regressed in certain ways. Reproduction here: WebAssembly/wasi-libc#415 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:wasm cranelift Issues related to the Cranelift code generator wasi Issues pertaining to WASI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants