Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/main/tools/linux-sandbox-pid1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,19 @@ static int CreateTarget(const char* path, bool is_directory) {
}

struct stat sb;
// If the path already exists...

if (stat(path, &sb) == 0) {
// Use lstat() instead of stat() to avoid following symlinks.
// If a parent component under sandbox_root is a pre-seeded symlink,
// stat() would follow it and subsequent mkdir()/link() calls would
// operate outside sandbox_root. Using lstat() detects and rejects
// symlinks, preventing writes to arbitrary host paths.
// See https://github.com/bazelbuild/bazel/issues/28515

if (lstat(path, &sb) == 0) {
if (S_ISLNK(sb.st_mode)) {
// Reject symlinks: following them could escape the sandbox root.
errno = ELOOP;
return -1;
}
if (is_directory && S_ISDIR(sb.st_mode)) {
// and it's a directory and supposed to be a directory, we're done here.
return 0;
Expand All @@ -144,7 +154,7 @@ static int CreateTarget(const char* path, bool is_directory) {
return -1;
}
} else {
// If stat failed because of any error other than "the path does not exist",
// If lstat failed because of any error other than "the path does not exist",
// this is an error.
if (errno != ENOENT) {
return -1;
Expand Down