Skip to content

wasi: return NotPermitted for stream open permission failures#13816

Merged
alexcrichton merged 2 commits into
bytecodealliance:mainfrom
SebTardif:fix/wasi-stream-not-permitted
Jul 6, 2026
Merged

wasi: return NotPermitted for stream open permission failures#13816
alexcrichton merged 2 commits into
bytecodealliance:mainfrom
SebTardif:fix/wasi-stream-not-permitted

Conversation

@SebTardif

@SebTardif SebTardif commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Summary

read_via_stream, write_via_stream, and append_via_stream returned ErrorCode::BadDescriptor when the descriptor lacked the required read/write permission. The non-stream read / write paths in the same module already return NotPermitted for the same condition.

Why this change is needed

Permission denial should use the permission-denied error code. BadDescriptor maps to a different condition (invalid/wrong kind of FD). Guests that branch on not-permitted vs bad-descriptor currently get the wrong signal when opening a stream view on a file opened without the needed access.

Change

Three sites in crates/wasi/src/p2/host/filesystem.rs:

- Err(types::ErrorCode::BadDescriptor)?;
+ Err(types::ErrorCode::NotPermitted)?;

aligned with:

// async fn read / write
if !f.perms.contains(FilePerms::READ/WRITE) {
    return Err(ErrorCode::NotPermitted.into());
}

Testing

  • cargo test -p wasmtime-wasi --test all --features p2 p2_file_stream_not_permitted passes (sync + async harness).
  • Guest program p2_file_stream_not_permitted covers:
    • read-only preopen (FilePerms::READ): write_via_stream / append_via_stream return ErrorCode::NotPermitted; read_via_stream still works.
    • write-only preopen (FilePerms::WRITE): read_via_stream returns ErrorCode::NotPermitted; write_via_stream still works.

read_via_stream, write_via_stream, and append_via_stream returned
BadDescriptor when the descriptor lacked read/write perms. The non-stream
read/write paths already return NotPermitted for the same condition.

Use NotPermitted for consistency with those siblings and the WASI
permission-denied mapping used elsewhere in this module.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
@SebTardif SebTardif requested a review from a team as a code owner July 4, 2026 13:10
@SebTardif SebTardif requested review from dicej and removed request for a team July 4, 2026 13:10
@github-actions github-actions Bot added the wasi Issues pertaining to WASI label Jul 4, 2026
@alexcrichton

Copy link
Copy Markdown
Member

Thanks! Could you add a test for this as well?

@alexcrichton alexcrichton requested review from alexcrichton and removed request for dicej July 6, 2026 03:04
Add a p2 guest program and sync/async harness coverage so
read_via_stream / write_via_stream / append_via_stream reject the
missing access mode with ErrorCode::NotPermitted, matching the
non-stream read/write paths.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
@SebTardif SebTardif requested a review from a team as a code owner July 6, 2026 20:35
@SebTardif

Copy link
Copy Markdown
Contributor Author

@alexcrichton Added a focused test in cbf77768a5:

  • Guest: crates/test-programs/src/bin/p2_file_stream_not_permitted.rs
  • Harness: p2_file_stream_not_permitted in both crates/wasi/tests/all/p2/sync.rs and async_.rs

It asserts ErrorCode::NotPermitted for write_via_stream / append_via_stream under a read-only preopen and for read_via_stream under a write-only preopen (while the allowed stream path still succeeds).

Verified locally with:

cargo test -p wasmtime-wasi --test all --features p2 p2_file_stream_not_permitted
# 2 passed (sync + async)

@alexcrichton alexcrichton added this pull request to the merge queue Jul 6, 2026
Merged via the queue into bytecodealliance:main with commit e59c8cf Jul 6, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasi Issues pertaining to WASI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants