brew/Library/Homebrew/sandbox.rb

242 lines
6.5 KiB
Ruby
Raw Normal View History

2020-11-23 17:31:17 +01:00
# typed: true
# frozen_string_literal: true
require "erb"
require "io/console"
require "pty"
require "tempfile"
2020-08-19 07:02:01 +02:00
# Helper class for running a sub-process inside of a sandboxed environment.
#
# @api private
class Sandbox
2020-10-20 12:03:48 +02:00
extend T::Sig
SANDBOX_EXEC = "/usr/bin/sandbox-exec"
2020-08-19 07:02:01 +02:00
private_constant :SANDBOX_EXEC
2020-10-20 12:03:48 +02:00
sig { returns(T::Boolean) }
def self.available?
OS.mac? && File.executable?(SANDBOX_EXEC)
end
2020-10-20 12:03:48 +02:00
sig { void }
2015-04-13 18:05:15 +08:00
def initialize
@profile = SandboxProfile.new
2015-04-13 18:05:15 +08:00
end
def record_log(file)
@logfile = file
end
2015-04-13 18:05:15 +08:00
def add_rule(rule)
@profile.add_rule(rule)
end
def allow_write(path, options = {})
add_rule allow: true, operation: "file-write*", filter: path_filter(path, options[:type])
2015-04-13 18:05:15 +08:00
end
def deny_write(path, options = {})
add_rule allow: false, operation: "file-write*", filter: path_filter(path, options[:type])
2015-04-13 18:05:15 +08:00
end
def allow_write_path(path)
allow_write path, type: :subpath
2015-04-13 18:05:15 +08:00
end
def deny_write_path(path)
deny_write path, type: :subpath
2015-04-13 18:05:15 +08:00
end
def allow_write_temp_and_cache
allow_write_path "/private/tmp"
allow_write_path "/private/var/tmp"
allow_write "^/private/var/folders/[^/]+/[^/]+/[C,T]/", type: :regex
2015-04-13 18:05:15 +08:00
allow_write_path HOMEBREW_TEMP
allow_write_path HOMEBREW_CACHE
end
2018-07-01 23:35:29 +02:00
def allow_cvs
2020-11-23 17:31:17 +01:00
allow_write_path "#{Dir.home(ENV.fetch("USER"))}/.cvspass"
2018-07-01 23:35:29 +02:00
end
def allow_fossil
2020-11-23 17:31:17 +01:00
allow_write_path "#{Dir.home(ENV.fetch("USER"))}/.fossil"
allow_write_path "#{Dir.home(ENV.fetch("USER"))}/.fossil-journal"
2018-07-01 23:35:29 +02:00
end
2015-04-13 18:05:15 +08:00
def allow_write_cellar(formula)
allow_write_path formula.rack
allow_write_path formula.etc
allow_write_path formula.var
end
# Xcode projects expect access to certain cache/archive dirs.
def allow_write_xcode
2020-11-23 17:31:17 +01:00
allow_write_path "#{Dir.home(ENV.fetch("USER"))}/Library/Developer"
end
2015-04-13 18:05:15 +08:00
def allow_write_log(formula)
2015-04-25 22:07:06 -04:00
allow_write_path formula.logs
end
def deny_write_homebrew_repository
deny_write HOMEBREW_BREW_FILE
if HOMEBREW_PREFIX.to_s == HOMEBREW_REPOSITORY.to_s
deny_write_path HOMEBREW_LIBRARY
deny_write_path HOMEBREW_REPOSITORY/".git"
else
deny_write_path HOMEBREW_REPOSITORY
end
end
def exec(*args)
seatbelt = Tempfile.new(["homebrew", ".sb"], HOMEBREW_TEMP)
seatbelt.write(@profile.dump)
seatbelt.close
@start = Time.now
2020-11-23 17:31:17 +01:00
begin
command = [SANDBOX_EXEC, "-f", seatbelt.path, *args]
2021-08-24 14:46:00 +01:00
# Start sandbox in a pseudoterminal to prevent access of the parent terminal.
T.unsafe(PTY).spawn(*command) do |r, w, pid|
2021-09-07 09:44:58 -07:00
# Set the PTY's window size to match the parent terminal.
# Some formula tests are sensitive to the terminal size and fail if this is not set.
winch = proc do |_sig|
w.winsize = if $stdout.tty?
# We can only use IO#winsize if the IO object is a TTY.
$stdout.winsize
else
# Otherwise, default to tput, if available.
# This relies on ncurses rather than the system's ioctl.
[Utils.popen_read("tput", "lines").to_i, Utils.popen_read("tput", "cols").to_i]
2021-09-01 16:06:07 +01:00
end
2021-09-07 09:44:58 -07:00
end
2021-08-25 19:56:12 +01:00
2021-09-07 09:44:58 -07:00
write_to_pty = proc do
begin
# Update the window size whenever the parent terminal's window size changes.
old_winch = trap(:WINCH, &winch)
winch.call(nil)
stdin_thread = Thread.new { IO.copy_stream($stdin, w) }
2021-08-25 19:56:12 +01:00
r.each_char { |c| print(c) }
2021-08-25 19:56:12 +01:00
Process.wait(pid)
ensure
stdin_thread&.kill
trap(:WINCH, old_winch)
end
2021-09-07 09:44:58 -07:00
end
if $stdin.tty?
$stdin.raw(&write_to_pty)
else
write_to_pty.call
end
end
raise ErrorDuringExecution.new(command, status: $CHILD_STATUS) unless $CHILD_STATUS.success?
2020-11-23 17:31:17 +01:00
rescue
@failed = true
raise
ensure
seatbelt.unlink
sleep 0.1 # wait for a bit to let syslog catch up the latest events.
2021-08-13 13:49:52 +01:00
syslog_args = [
"-F", "$((Time)(local)) $(Sender)[$(PID)]: $(Message)",
"-k", "Time", "ge", @start.to_i.to_s,
"-k", "Message", "S", "deny",
"-k", "Sender", "kernel",
"-o",
"-k", "Time", "ge", @start.to_i.to_s,
"-k", "Message", "S", "deny",
"-k", "Sender", "sandboxd"
2020-11-23 17:31:17 +01:00
]
logs = Utils.popen_read("syslog", *syslog_args)
# These messages are confusing and non-fatal, so don't report them.
logs = logs.lines.reject { |l| l.match(/^.*Python\(\d+\) deny file-write.*pyc$/) }.join
unless logs.empty?
if @logfile
File.open(@logfile, "w") do |log|
log.write logs
log.write "\nWe use time to filter sandbox log. Therefore, unrelated logs may be recorded.\n"
end
2018-03-07 16:14:55 +00:00
end
2020-11-23 17:31:17 +01:00
if @failed && Homebrew::EnvConfig.verbose?
ohai "Sandbox Log", logs
2020-11-23 17:31:17 +01:00
$stdout.flush # without it, brew test-bot would fail to catch the log
end
end
end
end
private
def expand_realpath(path)
raise unless path.absolute?
2018-09-17 02:45:00 +02:00
path.exist? ? path.realpath : expand_realpath(path.parent)/path.basename
end
2015-04-13 18:05:15 +08:00
def path_filter(path, type)
case type
when :regex then "regex \#\"#{path}\""
when :subpath then "subpath \"#{expand_realpath(Pathname.new(path))}\""
when :literal, nil then "literal \"#{expand_realpath(Pathname.new(path))}\""
end
end
2020-08-19 07:02:01 +02:00
# Configuration profile for a sandbox.
class SandboxProfile
2020-10-20 12:03:48 +02:00
extend T::Sig
SEATBELT_ERB = <<~ERB
(version 1)
(debug deny) ; log all denied operations to /var/log/system.log
<%= rules.join("\n") %>
(allow file-write*
(literal "/dev/ptmx")
(literal "/dev/dtracehelper")
(literal "/dev/null")
(literal "/dev/random")
(literal "/dev/zero")
(regex #"^/dev/fd/[0-9]+$")
(regex #"^/dev/tty[a-z0-9]*$")
)
(deny file-write*) ; deny non-allowlist file write operations
(allow process-exec
(literal "/bin/ps")
(with no-sandbox)
) ; allow certain processes running without sandbox
(allow default) ; allow everything else
2018-07-11 15:17:40 +02:00
ERB
attr_reader :rules
2020-10-20 12:03:48 +02:00
sig { void }
def initialize
@rules = []
end
def add_rule(rule)
s = +"("
2017-09-24 19:24:46 +01:00
s << (rule[:allow] ? "allow" : "deny")
s << " #{rule[:operation]}"
s << " (#{rule[:filter]})" if rule[:filter]
s << " (with #{rule[:modifier]})" if rule[:modifier]
s << ")"
@rules << s.freeze
end
def dump
ERB.new(SEATBELT_ERB).result(binding)
end
end
2020-08-19 07:02:01 +02:00
private_constant :SandboxProfile
end