Repository navigation
fix(security): do not follow container-planted links in logs, build output or cache #269
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9bad17b
fix(security): do not follow container-planted links in logs, build o…
loks0n 5f08891
fix(security): upload build output and cache from a private copy
loks0n 1392190
refactor: drop RegularFile::stat and the extra build-output error
loks0n 9031162
fix: remove upload snapshots, cover the Docker paths end to end
loks0n a8a8a2f
test: cover the build-cache path end to end
loks0n 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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| <?php | ||
|
|
||
| namespace OpenRuntimes\Executor; | ||
|
|
||
| /** | ||
| * A file in a directory that a runtime or build container can write: its logs, logging, | ||
| * build output and cache mounts. | ||
| * | ||
| * The container controls what sits at the path, so it is untrusted. A symlink there made the | ||
| * executor read its own files (its environment, with OPR_EXECUTOR_SECRET) and hand them back | ||
| * as logs or build output. Only a regular file that is still the same file once opened is used. | ||
| */ | ||
| final class RegularFile | ||
| { | ||
| /** | ||
| * Copy $path to $destination, a new file outside the container's reach, from the handle that | ||
| * passed the checks, and return the bytes copied. Null when $path is not a regular file. | ||
| * | ||
| * Uploading $path by name would reopen it, and a container still running can swap a symlink | ||
| * in for that open and back again. The copy is what gets uploaded instead. | ||
| */ | ||
| public static function copy(string $path, string $destination): ?int | ||
| { | ||
| $source = self::open($path); | ||
| if ($source === null) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| // 'x' fails if anything already sits at $destination, a symlink included. | ||
| $target = @\fopen($destination, 'xb'); | ||
| if ($target === false) { | ||
| throw new \Exception('Failed to create ' . $destination); | ||
| } | ||
|
|
||
| try { | ||
| $copied = \stream_copy_to_stream($source, $target); | ||
| if ($copied === false) { | ||
| throw new \Exception('Failed to copy ' . $path); | ||
| } | ||
|
|
||
| return $copied; | ||
| } finally { | ||
| \fclose($target); | ||
| } | ||
| } finally { | ||
| \fclose($source); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Up to $length bytes of $path from $offset (all of it when $length is null), or '' when | ||
| * $path is not a regular file. | ||
| */ | ||
| public static function read(string $path, int $offset = 0, ?int $length = null): string | ||
| { | ||
| $handle = self::open($path); | ||
| if ($handle === null) { | ||
| return ''; | ||
| } | ||
|
|
||
| try { | ||
| return (string) \stream_get_contents($handle, $length, $offset); | ||
| } finally { | ||
| \fclose($handle); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The open handle when $path is a regular file that opens as itself, otherwise null: | ||
| * a symlink, FIFO, device, directory or missing path. | ||
| * | ||
| * @return resource|null | ||
| */ | ||
| private static function open(string $path) | ||
| { | ||
| $link = @\lstat($path); | ||
| // lstat describes the entry itself, so a symlink is refused rather than followed. | ||
| if ($link === false || ($link['mode'] & 0170000) !== 0100000) { | ||
| return null; | ||
| } | ||
|
|
||
| // 'n' opens with O_NONBLOCK: a FIFO swapped in after the lstat would otherwise block | ||
| // this open forever with no writer. It opens at once instead, and the inode check refuses it. | ||
| $handle = @\fopen($path, 'rbn'); | ||
| if ($handle === false) { | ||
| return null; | ||
| } | ||
|
|
||
| // Swapped for a symlink between lstat and fopen, the open followed it to another inode. | ||
| $opened = \fstat($handle); | ||
| if ($opened === false || $opened['dev'] !== $link['dev'] || $opened['ino'] !== $link['ino']) { | ||
| \fclose($handle); | ||
| return null; | ||
| } | ||
|
|
||
| return $handle; | ||
| } | ||
| } |
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
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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| const fs = require('fs'); | ||
|
|
||
| // Replaces this execution's log files with symlinks to /proc/self/environ. The runtime keeps | ||
| // writing to the files it already opened, but the executor reads them by path afterwards. | ||
| module.exports = async (context) => { | ||
| context.log('planted'); | ||
|
|
||
| for (const file of fs.readdirSync('/mnt/logs')) { | ||
| fs.rmSync(`/mnt/logs/${file}`); | ||
| fs.symlinkSync('/proc/self/environ', `/mnt/logs/${file}`); | ||
| } | ||
|
|
||
| return context.res.send('OK'); | ||
| } |
Oops, something went wrong.
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.