Skip to content

Commit e0a979f

Browse files
authored
Merge pull request #22440 from jamessawle/sandbox-chdir-before-exec
sandbox: chdir into tmpdir before exec to avoid getcwd EPERM
2 parents 896f2f0 + 9b2953a commit e0a979f

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

Library/Homebrew/sandbox.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ def run(*args)
375375

376376
ensure_child_tty_available
377377

378+
# Move into a non-denied directory before `exec` so subsequent
379+
# `getcwd(3)` calls (which walk every parent) never cross a
380+
# `deny_read_home` path inherited from the caller's CWD.
381+
Dir.chdir(tmpdir)
382+
378383
worker.close_on_exec = true
379384
exec(*command, in: worker, out: worker, err: worker) # And map everything to the PTY.
380385
else

Library/Homebrew/test/sandbox_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@
4343
.and output(/foo/).to_stdout
4444
end
4545

46+
it "does not raise getcwd EPERM when the parent CWD is sandbox-denied" do
47+
mktmpdir do |denied|
48+
sandbox.deny_read_path(denied)
49+
Dir.chdir(denied) do
50+
expect { sandbox.run "/bin/pwd" }.not_to raise_error
51+
end
52+
end
53+
end
54+
4655
it "ignores bogus Python error" do
4756
ENV["HOMEBREW_VERBOSE"] = "1"
4857

0 commit comments

Comments
 (0)