Skip to content
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

sandbox: start sandbox in a pseudoterminal #11914

Merged
merged 3 commits into from
Aug 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion Library/Homebrew/sandbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# frozen_string_literal: true

require "erb"
require "io/console"
require "pty"
require "tempfile"

# Helper class for running a sub-process inside of a sandboxed environment.
Expand Down Expand Up @@ -95,12 +97,29 @@ def exec(*args)
seatbelt.close
@start = Time.now

$stdin.raw! if $stdin.tty?
stdin_thread = T.let(nil, T.nilable(Thread))

begin
T.unsafe(self).safe_system SANDBOX_EXEC, "-f", seatbelt.path, *args
command = [SANDBOX_EXEC, "-f", seatbelt.path, *args]
# Start sandbox in a pseudoterminal to prevent access of the parent terminal.
T.unsafe(PTY).spawn(*command) do |r, w, pid|
MikeMcQuaid marked this conversation as resolved.
Show resolved Hide resolved
if $stdout.tty?
w.winsize = $stdout.winsize
trap(:WINCH) { w.winsize = $stdout.winsize }
end
stdin_thread = Thread.new { IO.copy_stream($stdin, w) }
r.each_char { |c| print(c) }
Process.wait(pid)
end
raise ErrorDuringExecution.new(command, status: $CHILD_STATUS) unless $CHILD_STATUS.success?
rescue
@failed = true
raise
ensure
stdin_thread&.kill
$stdin.cooked! if $stdin.tty?

seatbelt.unlink
sleep 0.1 # wait for a bit to let syslog catch up the latest events.
syslog_args = [
Expand Down