[libstd]: reworked CacheHash means its WASI compatible now#5784
Closed
kubkon wants to merge 14 commits intoziglang:masterfrom
Closed
[libstd]: reworked CacheHash means its WASI compatible now#5784kubkon wants to merge 14 commits intoziglang:masterfrom
kubkon wants to merge 14 commits intoziglang:masterfrom
Conversation
Member
Author
`std.os.realpathat` is similar to `std.os.realpath`, however, it accepts a pair `fd_t, []const u8` of args and thus works out the realpath of a relative path wrt to some opened file descriptor. If the input pathname argument turns out to be an absolute path, this function reverts to calling `realpath` on that pathname completely ignoring the input file descriptor. This behaviour is standard in POSIX and IMHO a good rule of thumb to follow. If the input file descriptor was obtained using `std.fs.cwd()` call, this function reverts to `std.os.getcwd()` to obtain the file descriptor's path. `std.fs.Dir.realpath` integrates `std.os.realpathat` with `std.fs.Dir` but with dynamic memory allocator.
This commit adds null-terminated and WTF16 versions of `std.fs.Dir.realpath` and `std.os.realpathat`. Alloc version has been renamed to `std.fs.Dir.realpathAlloc` to be compatible with the naming convention used across Zig's `libstd`.
Co-authored-by: Joachim Schmidt <joachim.schmidt557@outlook.com>
On non-capability-oriented hosts (i.e., excluding WASI), use `Dir.realpathAlloc` to resolve cached path. Previously, we relied on `std.fs.path.resolve` to do the path resolution which could yield erroneous results since `std.fs.path.resolve` would not actually issue any syscalls, and hence, would not correctly resolve any symlinks.
This commit adds `Dir.realpathWasi`, and `os.realpathatWasi`. The latter resolves the path however upto only the `Dir` it is relative to ensuring the sandboxing rules are observed. In fact, it proceeds in two steps: 1. Try opening the path as-is, and check if that works OK. Then, this will imply the path was valid under WASI's capability-oriented security model, and no path traversal attack was attempted. 2. Since we've verified we're OK wrt sandboxing rules, we can analyze the path component-by-component working out the canonicalized path in the process.
This commit fixes `std.os.realpathatW` to properly canonicalize `pathname` if contains '.' or '..'.
Member
Author
|
After discussions with @andrewrk and further thought, due to the fact that querying the OS for a path to a resource is generally not a universal feature (for instance, it turns out FreeBSD for one doesn't support any standard mechanisms for getting those, at least to my knowledge: source), this approach to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR builds upon #5701.
This PR reworks
CacheHashto make it WASI compatible. The summary of changes:On non-capability-oriented hosts (i.e., excluding WASI), use
Dir.realpathAllocto resolve cached path. Previously, we relied onstd.fs.path.resolveto do the path resolution which could yield erroneous results sincestd.fs.path.resolvewould not actually issue any syscalls, and hence, would not correctly resolve any symlinks. UsingDir.realpathAllocwhich now supports WASI (more on this below), meansCacheHashnow supports WASI.Add
Dir.realpathWasi, andos.realpathatWasi. The latter resolves the path however upto only theDirit is relativeto ensuring the sandboxing rules are observed. In fact, it proceeds in two steps:
This PR closes #5437.
EDIT: This PR also fixes
os.realpathatWto correctly handle relativepathnamewith relative components such as.or...