|
| 1 | +# typed: strict |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "fileutils" |
| 5 | +require "env_config" |
| 6 | + |
| 7 | +module OS |
| 8 | + module Linux |
| 9 | + module Sandbox |
| 10 | + extend T::Helpers |
| 11 | + |
| 12 | + requires_ancestor { ::Sandbox } |
| 13 | + |
| 14 | + BUBBLEWRAP = "bwrap" |
| 15 | + TIOCSCTTY = 0x540E |
| 16 | + READ_ONLY_PATHS = T.let(%w[ |
| 17 | + /bin |
| 18 | + /etc |
| 19 | + /lib |
| 20 | + /lib64 |
| 21 | + /opt |
| 22 | + /sbin |
| 23 | + /usr |
| 24 | + ].freeze, T::Array[String]) |
| 25 | + private_constant :BUBBLEWRAP, :TIOCSCTTY, :READ_ONLY_PATHS |
| 26 | + |
| 27 | + sig { void } |
| 28 | + def allow_write_temp_and_cache |
| 29 | + allow_write_path "/tmp" |
| 30 | + allow_write_path "/var/tmp" |
| 31 | + allow_write_path HOMEBREW_TEMP |
| 32 | + allow_write_path HOMEBREW_CACHE |
| 33 | + end |
| 34 | + |
| 35 | + sig { void } |
| 36 | + def allow_cvs |
| 37 | + cvspass = ::Pathname.new("#{Dir.home(ENV.fetch("USER"))}/.cvspass") |
| 38 | + allow_write path: cvspass, type: :literal if cvspass.exist? |
| 39 | + end |
| 40 | + |
| 41 | + sig { void } |
| 42 | + def allow_fossil |
| 43 | + [".fossil", ".fossil-journal"].each do |file| |
| 44 | + fossil_file = ::Pathname.new("#{Dir.home(ENV.fetch("USER"))}/#{file}") |
| 45 | + allow_write path: fossil_file, type: :literal if fossil_file.exist? |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + module ClassMethods |
| 50 | + extend T::Helpers |
| 51 | + |
| 52 | + requires_ancestor { T.class_of(::Sandbox) } |
| 53 | + |
| 54 | + sig { returns(T.nilable(::Pathname)) } |
| 55 | + def bubblewrap_executable |
| 56 | + which(BUBBLEWRAP, ORIGINAL_PATHS) || which(BUBBLEWRAP) |
| 57 | + end |
| 58 | + |
| 59 | + sig { void } |
| 60 | + def ensure_bubblewrap_installed! |
| 61 | + return if ENV["HOMEBREW_INSTALLING_BUBBLEWRAP"] |
| 62 | + return if bubblewrap_executable |
| 63 | + return unless CoreTap.instance.installed? |
| 64 | + |
| 65 | + require "formula" |
| 66 | + with_env(HOMEBREW_INSTALLING_BUBBLEWRAP: "1") do |
| 67 | + ::Formula["bubblewrap"].ensure_installed!(reason: "Linux sandboxing") |
| 68 | + end |
| 69 | + rescue FormulaUnavailableError |
| 70 | + nil |
| 71 | + end |
| 72 | + |
| 73 | + sig { returns(T::Boolean) } |
| 74 | + def available? |
| 75 | + return false unless Homebrew::EnvConfig.sandbox_linux? |
| 76 | + |
| 77 | + ensure_bubblewrap_installed! |
| 78 | + |
| 79 | + return false unless (bubblewrap = bubblewrap_executable) |
| 80 | + return false if bubblewrap.stat.setuid? |
| 81 | + |
| 82 | + system( |
| 83 | + bubblewrap.to_s, |
| 84 | + "--unshare-user", |
| 85 | + "--unshare-ipc", |
| 86 | + "--unshare-pid", |
| 87 | + "--unshare-uts", |
| 88 | + "--unshare-cgroup-try", |
| 89 | + "--ro-bind", "/", "/", |
| 90 | + "--proc", "/proc", |
| 91 | + "--dev", "/dev", |
| 92 | + "true", |
| 93 | + out: File::NULL, |
| 94 | + err: File::NULL |
| 95 | + ) == true |
| 96 | + end |
| 97 | + |
| 98 | + sig { returns(Integer) } |
| 99 | + def terminal_ioctl_request |
| 100 | + TIOCSCTTY |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + private |
| 105 | + |
| 106 | + sig { params(args: T::Array[T.any(String, ::Pathname)], tmpdir: String).returns(T::Array[T.any(String, ::Pathname)]) } |
| 107 | + def sandbox_command(args, tmpdir) |
| 108 | + bubblewrap = T.must(which(BUBBLEWRAP, ORIGINAL_PATHS) || which(BUBBLEWRAP)) |
| 109 | + [bubblewrap, *bubblewrap_args(tmpdir), "--", *args] |
| 110 | + end |
| 111 | + |
| 112 | + sig { params(_tmpdir: String).returns(T::Array[String]) } |
| 113 | + def bubblewrap_args(_tmpdir) |
| 114 | + args = T.let([ |
| 115 | + "--unshare-user", |
| 116 | + "--unshare-ipc", |
| 117 | + "--unshare-pid", |
| 118 | + "--unshare-uts", |
| 119 | + "--unshare-cgroup-try", |
| 120 | + "--die-with-parent", |
| 121 | + "--new-session", |
| 122 | + "--dev", "/dev", |
| 123 | + "--proc", "/proc", |
| 124 | + "--dir", "/var", |
| 125 | + "--chdir", "/" |
| 126 | + ], T::Array[String]) |
| 127 | + args << "--unshare-net" if deny_all_network? |
| 128 | + |
| 129 | + read_only_paths.each do |path| |
| 130 | + args += ["--ro-bind", path, path] |
| 131 | + end |
| 132 | + |
| 133 | + writable_paths.each do |path, type| |
| 134 | + prepare_writable_path(path, type) |
| 135 | + args += ["--bind", path, path] |
| 136 | + end |
| 137 | + |
| 138 | + denied_write_paths.each do |path| |
| 139 | + next unless File.exist?(path) |
| 140 | + |
| 141 | + args += ["--ro-bind", path, path] |
| 142 | + end |
| 143 | + |
| 144 | + args |
| 145 | + end |
| 146 | + |
| 147 | + sig { returns(T::Boolean) } |
| 148 | + def deny_all_network? |
| 149 | + profile.rules.any? do |rule| |
| 150 | + !rule.allow && rule.operation == "network*" && rule.filter.nil? |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + sig { returns(T::Array[String]) } |
| 155 | + def read_only_paths |
| 156 | + (READ_ONLY_PATHS + [HOMEBREW_PREFIX.to_s, HOMEBREW_REPOSITORY.to_s]) |
| 157 | + .select { |path| File.exist?(path) } |
| 158 | + .uniq |
| 159 | + end |
| 160 | + |
| 161 | + sig { returns(T::Hash[String, Symbol]) } |
| 162 | + def writable_paths |
| 163 | + profile.rules.each_with_object({}) do |rule, paths| |
| 164 | + next if !rule.allow || !rule.operation.start_with?("file-write") |
| 165 | + next unless (filter = rule.filter) |
| 166 | + |
| 167 | + case filter.type |
| 168 | + when :literal, :subpath |
| 169 | + paths[filter.path] ||= filter.type |
| 170 | + when :regex |
| 171 | + raise ArgumentError, "Linux sandbox does not support regex path filters: #{filter.path}" |
| 172 | + else |
| 173 | + raise ArgumentError, "Invalid path filter type: #{filter.type}" |
| 174 | + end |
| 175 | + end |
| 176 | + end |
| 177 | + |
| 178 | + sig { returns(T::Array[String]) } |
| 179 | + def denied_write_paths |
| 180 | + profile.rules.filter_map do |rule| |
| 181 | + next if rule.allow || !rule.operation.start_with?("file-write") |
| 182 | + |
| 183 | + filter = rule.filter |
| 184 | + filter.path if filter && [:literal, :subpath].include?(filter.type) |
| 185 | + end.uniq |
| 186 | + end |
| 187 | + |
| 188 | + sig { params(path: String, type: Symbol).void } |
| 189 | + def prepare_writable_path(path, type) |
| 190 | + pathname = ::Pathname.new(path) |
| 191 | + return if pathname.exist? |
| 192 | + |
| 193 | + if type == :literal |
| 194 | + FileUtils.mkdir_p(pathname.dirname) |
| 195 | + FileUtils.touch(pathname) |
| 196 | + else |
| 197 | + FileUtils.mkdir_p(pathname) |
| 198 | + end |
| 199 | + end |
| 200 | + end |
| 201 | + end |
| 202 | +end |
| 203 | + |
| 204 | +Sandbox.prepend(OS::Linux::Sandbox) |
| 205 | +Sandbox.singleton_class.prepend(OS::Linux::Sandbox::ClassMethods) |
0 commit comments