Repository navigation
fix(fs): a chrooted task resolves and reports paths against its own root #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -815,10 +815,36 @@ dword_t sys_getcwd(addr_t buf_addr, dword_t size) { | |
| struct fd *wd = current->fs->pwd; | ||
| char pwd[MAX_PATH + 1]; | ||
| int err = generic_getpath(wd, pwd); | ||
| char root[MAX_PATH + 1] = ""; | ||
| if (err >= 0 && current->fs->root != NULL) | ||
| err = generic_getpath(current->fs->root, root); | ||
| unlock(¤t->fs->lock); | ||
| if (err < 0) | ||
| return err; | ||
|
|
||
| // What a task sees is relative to its own root, the way it is for a | ||
| // chrooted process on Linux: `generic_getpath` answers in the machine's | ||
| // terms, and a task rooted at a subtree would otherwise be told its working | ||
| // directory is a path it cannot name — `/jail` for what is its own `/`. | ||
| // | ||
| // Two things this must not do. `generic_getpath` answers "/" for a task | ||
| // rooted at the machine's own — it repairs an empty result to that — so | ||
| // stripping unconditionally would take the leading slash off every path a | ||
| // normal task reports and turn `/tmp` into `tmp`. And the prefix has to end | ||
| // on a component boundary, or a cwd of `/jail-old` under a root of `/jail` | ||
| // would be reported as `-old`. | ||
| // | ||
| // A cwd outside the root is left as it is: it cannot be spelled from inside, | ||
| // but a machine-absolute path is at least true, and there is no reachable | ||
| // way to produce one — `fs_chdir` only stores what this task resolved. | ||
| size_t root_len = strlen(root); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Correctness | 🟠 High 🧩 Analysis
🤖 Prompt for AI agents |
||
| if (root_len > 1 && strncmp(pwd, root, root_len) == 0 && | ||
| (pwd[root_len] == '/' || pwd[root_len] == '\0')) { | ||
| memmove(pwd, pwd + root_len, strlen(pwd) - root_len + 1); | ||
| if (pwd[0] == '\0') | ||
| strcpy(pwd, "/"); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if (strlen(pwd) + 1 > size) | ||
| return _ERANGE; | ||
| size = strlen(pwd) + 1; | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.