Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package build |
| 16 | |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 17 | import ( |
| 18 | "bytes" |
| 19 | "os" |
| 20 | "os/exec" |
| 21 | "os/user" |
Diego Wilson | 10e564a | 2020-04-15 17:42:59 +0000 | [diff] [blame] | 22 | "path/filepath" |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 23 | "strings" |
| 24 | "sync" |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 27 | type Sandbox struct { |
| 28 | Enabled bool |
| 29 | DisableWhenUsingGoma bool |
Dan Willemsen | 25e6f09 | 2019-04-09 10:22:43 -0700 | [diff] [blame] | 30 | |
| 31 | AllowBuildBrokenUsesNetwork bool |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | var ( |
| 35 | noSandbox = Sandbox{} |
| 36 | basicSandbox = Sandbox{ |
| 37 | Enabled: true, |
| 38 | } |
| 39 | |
| 40 | dumpvarsSandbox = basicSandbox |
| 41 | katiSandbox = basicSandbox |
| 42 | soongSandbox = basicSandbox |
| 43 | ninjaSandbox = Sandbox{ |
| 44 | Enabled: true, |
| 45 | DisableWhenUsingGoma: true, |
Dan Willemsen | 25e6f09 | 2019-04-09 10:22:43 -0700 | [diff] [blame] | 46 | |
| 47 | AllowBuildBrokenUsesNetwork: true, |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 48 | } |
| 49 | ) |
| 50 | |
Taylor Santiago | ca30e08 | 2024-07-30 13:48:43 -0700 | [diff] [blame^] | 51 | const ( |
| 52 | nsjailPath = "prebuilts/build-tools/linux-x86/bin/nsjail" |
| 53 | abfsSrcDir = "/src" |
| 54 | abfsOutDir = "/src/out" |
| 55 | ) |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 56 | |
| 57 | var sandboxConfig struct { |
| 58 | once sync.Once |
| 59 | |
| 60 | working bool |
| 61 | group string |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 62 | srcDir string |
| 63 | outDir string |
| 64 | distDir string |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 67 | func (c *Cmd) sandboxSupported() bool { |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 68 | if !c.Sandbox.Enabled { |
| 69 | return false |
| 70 | } |
| 71 | |
| 72 | // Goma is incompatible with PID namespaces and Mount namespaces. b/122767582 |
| 73 | if c.Sandbox.DisableWhenUsingGoma && c.config.UseGoma() { |
| 74 | return false |
| 75 | } |
| 76 | |
| 77 | sandboxConfig.once.Do(func() { |
| 78 | sandboxConfig.group = "nogroup" |
| 79 | if _, err := user.LookupGroup(sandboxConfig.group); err != nil { |
| 80 | sandboxConfig.group = "nobody" |
| 81 | } |
| 82 | |
Diego Wilson | 10e564a | 2020-04-15 17:42:59 +0000 | [diff] [blame] | 83 | // These directories will be bind mounted |
| 84 | // so we need full non-symlink paths |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 85 | sandboxConfig.srcDir = absPath(c.ctx, ".") |
Diego Wilson | 10e564a | 2020-04-15 17:42:59 +0000 | [diff] [blame] | 86 | if derefPath, err := filepath.EvalSymlinks(sandboxConfig.srcDir); err == nil { |
| 87 | sandboxConfig.srcDir = absPath(c.ctx, derefPath) |
| 88 | } |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 89 | sandboxConfig.outDir = absPath(c.ctx, c.config.OutDir()) |
Diego Wilson | 10e564a | 2020-04-15 17:42:59 +0000 | [diff] [blame] | 90 | if derefPath, err := filepath.EvalSymlinks(sandboxConfig.outDir); err == nil { |
| 91 | sandboxConfig.outDir = absPath(c.ctx, derefPath) |
| 92 | } |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 93 | sandboxConfig.distDir = absPath(c.ctx, c.config.DistDir()) |
Diego Wilson | 10e564a | 2020-04-15 17:42:59 +0000 | [diff] [blame] | 94 | if derefPath, err := filepath.EvalSymlinks(sandboxConfig.distDir); err == nil { |
| 95 | sandboxConfig.distDir = absPath(c.ctx, derefPath) |
| 96 | } |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 97 | |
Diego Wilson | a5d9653 | 2020-04-06 22:02:38 +0000 | [diff] [blame] | 98 | sandboxArgs := []string{ |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 99 | "-H", "android-build", |
| 100 | "-e", |
| 101 | "-u", "nobody", |
| 102 | "-g", sandboxConfig.group, |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 103 | "-R", "/", |
Spandan Das | 0506361 | 2021-06-25 01:39:04 +0000 | [diff] [blame] | 104 | // Mount tmp before srcDir |
| 105 | // srcDir is /tmp/.* in integration tests, which is a child dir of /tmp |
| 106 | // nsjail throws an error if a child dir is mounted before its parent |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 107 | "-B", "/tmp", |
Spandan Das | 2d99704 | 2022-11-04 20:58:18 +0000 | [diff] [blame] | 108 | c.config.sandboxConfig.SrcDirMountFlag(), sandboxConfig.srcDir, |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 109 | "-B", sandboxConfig.outDir, |
Diego Wilson | a5d9653 | 2020-04-06 22:02:38 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | if _, err := os.Stat(sandboxConfig.distDir); !os.IsNotExist(err) { |
| 113 | //Mount dist dir as read-write if it already exists |
| 114 | sandboxArgs = append(sandboxArgs, "-B", |
| 115 | sandboxConfig.distDir) |
| 116 | } |
| 117 | |
| 118 | sandboxArgs = append(sandboxArgs, |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 119 | "--disable_clone_newcgroup", |
| 120 | "--", |
| 121 | "/bin/bash", "-c", `if [ $(hostname) == "android-build" ]; then echo "Android" "Success"; else echo Failure; fi`) |
Diego Wilson | a5d9653 | 2020-04-06 22:02:38 +0000 | [diff] [blame] | 122 | |
| 123 | cmd := exec.CommandContext(c.ctx.Context, nsjailPath, sandboxArgs...) |
| 124 | |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 125 | cmd.Env = c.config.Environment().Environ() |
| 126 | |
| 127 | c.ctx.Verboseln(cmd.Args) |
| 128 | data, err := cmd.CombinedOutput() |
| 129 | if err == nil && bytes.Contains(data, []byte("Android Success")) { |
| 130 | sandboxConfig.working = true |
| 131 | return |
| 132 | } |
| 133 | |
Dan Willemsen | 1871d88 | 2020-03-02 20:36:04 +0000 | [diff] [blame] | 134 | c.ctx.Println("Build sandboxing disabled due to nsjail error.") |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 135 | |
| 136 | for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") { |
| 137 | c.ctx.Verboseln(line) |
| 138 | } |
| 139 | |
| 140 | if err == nil { |
| 141 | c.ctx.Verboseln("nsjail exited successfully, but without the correct output") |
| 142 | } else if e, ok := err.(*exec.ExitError); ok { |
| 143 | c.ctx.Verbosef("nsjail failed with %v", e.ProcessState.String()) |
| 144 | } else { |
| 145 | c.ctx.Verbosef("nsjail failed with %v", err) |
| 146 | } |
| 147 | }) |
| 148 | |
| 149 | return sandboxConfig.working |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Taylor Santiago | ca30e08 | 2024-07-30 13:48:43 -0700 | [diff] [blame^] | 152 | func (c *Cmd) srcDirArg() string { |
| 153 | if !c.config.UseABFS() { |
| 154 | return sandboxConfig.srcDir |
| 155 | } |
| 156 | |
| 157 | return sandboxConfig.srcDir + ":" + abfsSrcDir |
| 158 | } |
| 159 | |
| 160 | func (c *Cmd) outDirArg() string { |
| 161 | if !c.config.UseABFS() { |
| 162 | return sandboxConfig.outDir |
| 163 | } |
| 164 | |
| 165 | return sandboxConfig.outDir + ":" + abfsOutDir |
| 166 | } |
| 167 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 168 | func (c *Cmd) wrapSandbox() { |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 169 | wd, _ := os.Getwd() |
| 170 | |
| 171 | sandboxArgs := []string{ |
| 172 | // The executable to run |
| 173 | "-x", c.Path, |
| 174 | |
| 175 | // Set the hostname to something consistent |
| 176 | "-H", "android-build", |
| 177 | |
| 178 | // Use the current working dir |
| 179 | "--cwd", wd, |
| 180 | |
| 181 | // No time limit |
| 182 | "-t", "0", |
| 183 | |
| 184 | // Keep all environment variables, we already filter them out |
| 185 | // in soong_ui |
| 186 | "-e", |
| 187 | |
Dan Willemsen | 3a4dbd6 | 2019-01-16 23:02:24 -0800 | [diff] [blame] | 188 | // Mount /proc read-write, necessary to run a nested nsjail or minijail0 |
| 189 | "--proc_rw", |
| 190 | |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 191 | // Use a consistent user & group. |
| 192 | // Note that these are mapped back to the real UID/GID when |
| 193 | // doing filesystem operations, so they're rather arbitrary. |
| 194 | "-u", "nobody", |
| 195 | "-g", sandboxConfig.group, |
| 196 | |
| 197 | // Set high values, as nsjail uses low defaults. |
| 198 | "--rlimit_as", "soft", |
| 199 | "--rlimit_core", "soft", |
| 200 | "--rlimit_cpu", "soft", |
| 201 | "--rlimit_fsize", "soft", |
| 202 | "--rlimit_nofile", "soft", |
| 203 | |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 204 | // For now, just map everything. Make most things readonly. |
| 205 | "-R", "/", |
| 206 | |
Dan Willemsen | 1612e26 | 2020-05-01 16:26:56 -0700 | [diff] [blame] | 207 | // Mount a writable tmp dir |
| 208 | "-B", "/tmp", |
| 209 | |
Spandan Das | a3639e6 | 2021-05-25 19:14:02 +0000 | [diff] [blame] | 210 | // Mount source |
Taylor Santiago | ca30e08 | 2024-07-30 13:48:43 -0700 | [diff] [blame^] | 211 | c.config.sandboxConfig.SrcDirMountFlag(), c.srcDirArg(), |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 212 | |
| 213 | //Mount out dir as read-write |
Taylor Santiago | ca30e08 | 2024-07-30 13:48:43 -0700 | [diff] [blame^] | 214 | "-B", c.outDirArg(), |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 215 | |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 216 | // Disable newcgroup for now, since it may require newer kernels |
| 217 | // TODO: try out cgroups |
| 218 | "--disable_clone_newcgroup", |
| 219 | |
| 220 | // Only log important warnings / errors |
| 221 | "-q", |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 222 | } |
Taylor Santiago | 3c16e61 | 2024-05-30 14:41:31 -0700 | [diff] [blame] | 223 | if c.config.UseABFS() { |
| 224 | sandboxArgs = append(sandboxArgs, "-B", "{ABFS_DIR}") |
| 225 | } |
Dan Willemsen | 25e6f09 | 2019-04-09 10:22:43 -0700 | [diff] [blame] | 226 | |
Spandan Das | a3639e6 | 2021-05-25 19:14:02 +0000 | [diff] [blame] | 227 | // Mount srcDir RW allowlists as Read-Write |
| 228 | if len(c.config.sandboxConfig.SrcDirRWAllowlist()) > 0 && !c.config.sandboxConfig.SrcDirIsRO() { |
| 229 | errMsg := `Product source tree has been set as ReadWrite, RW allowlist not necessary. |
| 230 | To recover, either |
| 231 | 1. Unset BUILD_BROKEN_SRC_DIR_IS_WRITABLE #or |
| 232 | 2. Unset BUILD_BROKEN_SRC_DIR_RW_ALLOWLIST` |
| 233 | c.ctx.Fatalln(errMsg) |
| 234 | } |
| 235 | for _, srcDirChild := range c.config.sandboxConfig.SrcDirRWAllowlist() { |
| 236 | sandboxArgs = append(sandboxArgs, "-B", srcDirChild) |
| 237 | } |
| 238 | |
Diego Wilson | a5d9653 | 2020-04-06 22:02:38 +0000 | [diff] [blame] | 239 | if _, err := os.Stat(sandboxConfig.distDir); !os.IsNotExist(err) { |
| 240 | //Mount dist dir as read-write if it already exists |
| 241 | sandboxArgs = append(sandboxArgs, "-B", sandboxConfig.distDir) |
| 242 | } |
| 243 | |
Dan Willemsen | 25e6f09 | 2019-04-09 10:22:43 -0700 | [diff] [blame] | 244 | if c.Sandbox.AllowBuildBrokenUsesNetwork && c.config.BuildBrokenUsesNetwork() { |
| 245 | c.ctx.Printf("AllowBuildBrokenUsesNetwork: %v", c.Sandbox.AllowBuildBrokenUsesNetwork) |
| 246 | c.ctx.Printf("BuildBrokenUsesNetwork: %v", c.config.BuildBrokenUsesNetwork()) |
| 247 | sandboxArgs = append(sandboxArgs, "-N") |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 248 | } else if dlv, _ := c.config.Environment().Get("SOONG_DELVE"); dlv != "" { |
| 249 | // The debugger is enabled and soong_build will pause until a remote delve process connects, allow |
| 250 | // network connections. |
| 251 | sandboxArgs = append(sandboxArgs, "-N") |
Dan Willemsen | 25e6f09 | 2019-04-09 10:22:43 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // Stop nsjail from parsing arguments |
| 255 | sandboxArgs = append(sandboxArgs, "--") |
| 256 | |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 257 | c.Args = append(sandboxArgs, c.Args[1:]...) |
| 258 | c.Path = nsjailPath |
| 259 | |
| 260 | env := Environment(c.Env) |
| 261 | if _, hasUser := env.Get("USER"); hasUser { |
| 262 | env.Set("USER", "nobody") |
| 263 | } |
| 264 | c.Env = []string(env) |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 265 | } |