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" |
Taylor Santiago | ca30e08 | 2024-07-30 13:48:43 -0700 | [diff] [blame] | 54 | ) |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 55 | |
| 56 | var sandboxConfig struct { |
| 57 | once sync.Once |
| 58 | |
| 59 | working bool |
| 60 | group string |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 61 | srcDir string |
| 62 | outDir string |
| 63 | distDir string |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 66 | func (c *Cmd) sandboxSupported() bool { |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 67 | if !c.Sandbox.Enabled { |
| 68 | return false |
| 69 | } |
| 70 | |
| 71 | // Goma is incompatible with PID namespaces and Mount namespaces. b/122767582 |
| 72 | if c.Sandbox.DisableWhenUsingGoma && c.config.UseGoma() { |
| 73 | return false |
| 74 | } |
| 75 | |
| 76 | sandboxConfig.once.Do(func() { |
| 77 | sandboxConfig.group = "nogroup" |
| 78 | if _, err := user.LookupGroup(sandboxConfig.group); err != nil { |
| 79 | sandboxConfig.group = "nobody" |
| 80 | } |
| 81 | |
Diego Wilson | 10e564a | 2020-04-15 17:42:59 +0000 | [diff] [blame] | 82 | // These directories will be bind mounted |
| 83 | // so we need full non-symlink paths |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 84 | sandboxConfig.srcDir = absPath(c.ctx, ".") |
Diego Wilson | 10e564a | 2020-04-15 17:42:59 +0000 | [diff] [blame] | 85 | if derefPath, err := filepath.EvalSymlinks(sandboxConfig.srcDir); err == nil { |
| 86 | sandboxConfig.srcDir = absPath(c.ctx, derefPath) |
| 87 | } |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 88 | sandboxConfig.outDir = absPath(c.ctx, c.config.OutDir()) |
Diego Wilson | 10e564a | 2020-04-15 17:42:59 +0000 | [diff] [blame] | 89 | if derefPath, err := filepath.EvalSymlinks(sandboxConfig.outDir); err == nil { |
| 90 | sandboxConfig.outDir = absPath(c.ctx, derefPath) |
| 91 | } |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 92 | sandboxConfig.distDir = absPath(c.ctx, c.config.DistDir()) |
Diego Wilson | 10e564a | 2020-04-15 17:42:59 +0000 | [diff] [blame] | 93 | if derefPath, err := filepath.EvalSymlinks(sandboxConfig.distDir); err == nil { |
| 94 | sandboxConfig.distDir = absPath(c.ctx, derefPath) |
| 95 | } |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 96 | |
Diego Wilson | a5d9653 | 2020-04-06 22:02:38 +0000 | [diff] [blame] | 97 | sandboxArgs := []string{ |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 98 | "-H", "android-build", |
| 99 | "-e", |
| 100 | "-u", "nobody", |
| 101 | "-g", sandboxConfig.group, |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 102 | "-R", "/", |
Spandan Das | 0506361 | 2021-06-25 01:39:04 +0000 | [diff] [blame] | 103 | // Mount tmp before srcDir |
| 104 | // srcDir is /tmp/.* in integration tests, which is a child dir of /tmp |
| 105 | // 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] | 106 | "-B", "/tmp", |
Spandan Das | 2d99704 | 2022-11-04 20:58:18 +0000 | [diff] [blame] | 107 | c.config.sandboxConfig.SrcDirMountFlag(), sandboxConfig.srcDir, |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 108 | "-B", sandboxConfig.outDir, |
Diego Wilson | a5d9653 | 2020-04-06 22:02:38 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | if _, err := os.Stat(sandboxConfig.distDir); !os.IsNotExist(err) { |
| 112 | //Mount dist dir as read-write if it already exists |
| 113 | sandboxArgs = append(sandboxArgs, "-B", |
| 114 | sandboxConfig.distDir) |
| 115 | } |
| 116 | |
| 117 | sandboxArgs = append(sandboxArgs, |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 118 | "--disable_clone_newcgroup", |
| 119 | "--", |
| 120 | "/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] | 121 | |
| 122 | cmd := exec.CommandContext(c.ctx.Context, nsjailPath, sandboxArgs...) |
| 123 | |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 124 | cmd.Env = c.config.Environment().Environ() |
| 125 | |
| 126 | c.ctx.Verboseln(cmd.Args) |
| 127 | data, err := cmd.CombinedOutput() |
| 128 | if err == nil && bytes.Contains(data, []byte("Android Success")) { |
| 129 | sandboxConfig.working = true |
| 130 | return |
| 131 | } |
| 132 | |
Dan Willemsen | 1871d88 | 2020-03-02 20:36:04 +0000 | [diff] [blame] | 133 | c.ctx.Println("Build sandboxing disabled due to nsjail error.") |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 134 | |
| 135 | for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") { |
| 136 | c.ctx.Verboseln(line) |
| 137 | } |
| 138 | |
| 139 | if err == nil { |
| 140 | c.ctx.Verboseln("nsjail exited successfully, but without the correct output") |
| 141 | } else if e, ok := err.(*exec.ExitError); ok { |
| 142 | c.ctx.Verbosef("nsjail failed with %v", e.ProcessState.String()) |
| 143 | } else { |
| 144 | c.ctx.Verbosef("nsjail failed with %v", err) |
| 145 | } |
| 146 | }) |
| 147 | |
| 148 | return sandboxConfig.working |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Taylor Santiago | ca30e08 | 2024-07-30 13:48:43 -0700 | [diff] [blame] | 151 | func (c *Cmd) srcDirArg() string { |
| 152 | if !c.config.UseABFS() { |
| 153 | return sandboxConfig.srcDir |
| 154 | } |
| 155 | |
| 156 | return sandboxConfig.srcDir + ":" + abfsSrcDir |
| 157 | } |
| 158 | |
| 159 | func (c *Cmd) outDirArg() string { |
| 160 | if !c.config.UseABFS() { |
| 161 | return sandboxConfig.outDir |
| 162 | } |
| 163 | |
Taylor Santiago | 2611851 | 2024-08-05 19:52:41 -0700 | [diff] [blame] | 164 | return sandboxConfig.outDir + ":" + filepath.Join(abfsSrcDir, sandboxConfig.outDir) |
| 165 | } |
| 166 | |
| 167 | // When configured to use ABFS, we need to allow the creation of the /src |
| 168 | // directory. Therefore, we cannot mount the root "/" directory as read-only. |
| 169 | // Instead, we individually mount the children of "/" as RO. |
| 170 | func (c *Cmd) readMountArgs() []string { |
| 171 | if !c.config.UseABFS() { |
| 172 | // For now, just map everything. Make most things readonly. |
| 173 | return []string{"-R", "/"} |
| 174 | } |
| 175 | |
| 176 | entries, err := os.ReadDir("/") |
| 177 | if err != nil { |
| 178 | // If we can't read "/", just use the default non-ABFS behavior. |
| 179 | return []string{"-R", "/"} |
| 180 | } |
| 181 | |
| 182 | args := make([]string, 0, 2*len(entries)) |
| 183 | for _, ent := range entries { |
| 184 | args = append(args, "-R", "/"+ent.Name()) |
| 185 | } |
| 186 | |
| 187 | return args |
Taylor Santiago | ca30e08 | 2024-07-30 13:48:43 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 190 | func (c *Cmd) wrapSandbox() { |
PODISHETTY KUMAR (xWF) | 9543d19 | 2024-09-02 03:54:36 +0000 | [diff] [blame] | 191 | wd, _ := os.Getwd() |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 192 | |
Taylor Santiago | 2611851 | 2024-08-05 19:52:41 -0700 | [diff] [blame] | 193 | var sandboxArgs []string |
| 194 | sandboxArgs = append(sandboxArgs, |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 195 | // The executable to run |
| 196 | "-x", c.Path, |
| 197 | |
| 198 | // Set the hostname to something consistent |
| 199 | "-H", "android-build", |
| 200 | |
| 201 | // Use the current working dir |
| 202 | "--cwd", wd, |
| 203 | |
| 204 | // No time limit |
| 205 | "-t", "0", |
| 206 | |
| 207 | // Keep all environment variables, we already filter them out |
| 208 | // in soong_ui |
| 209 | "-e", |
| 210 | |
Dan Willemsen | 3a4dbd6 | 2019-01-16 23:02:24 -0800 | [diff] [blame] | 211 | // Mount /proc read-write, necessary to run a nested nsjail or minijail0 |
| 212 | "--proc_rw", |
| 213 | |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 214 | // Use a consistent user & group. |
| 215 | // Note that these are mapped back to the real UID/GID when |
| 216 | // doing filesystem operations, so they're rather arbitrary. |
| 217 | "-u", "nobody", |
| 218 | "-g", sandboxConfig.group, |
| 219 | |
| 220 | // Set high values, as nsjail uses low defaults. |
| 221 | "--rlimit_as", "soft", |
| 222 | "--rlimit_core", "soft", |
| 223 | "--rlimit_cpu", "soft", |
| 224 | "--rlimit_fsize", "soft", |
| 225 | "--rlimit_nofile", "soft", |
Taylor Santiago | 2611851 | 2024-08-05 19:52:41 -0700 | [diff] [blame] | 226 | ) |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 227 | |
Taylor Santiago | 2611851 | 2024-08-05 19:52:41 -0700 | [diff] [blame] | 228 | sandboxArgs = append(sandboxArgs, |
PODISHETTY KUMAR (xWF) | 9543d19 | 2024-09-02 03:54:36 +0000 | [diff] [blame] | 229 | c.readMountArgs()... |
Taylor Santiago | 2611851 | 2024-08-05 19:52:41 -0700 | [diff] [blame] | 230 | ) |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 231 | |
Taylor Santiago | 2611851 | 2024-08-05 19:52:41 -0700 | [diff] [blame] | 232 | sandboxArgs = append(sandboxArgs, |
Dan Willemsen | 1612e26 | 2020-05-01 16:26:56 -0700 | [diff] [blame] | 233 | // Mount a writable tmp dir |
| 234 | "-B", "/tmp", |
| 235 | |
Spandan Das | a3639e6 | 2021-05-25 19:14:02 +0000 | [diff] [blame] | 236 | // Mount source |
Taylor Santiago | ca30e08 | 2024-07-30 13:48:43 -0700 | [diff] [blame] | 237 | c.config.sandboxConfig.SrcDirMountFlag(), c.srcDirArg(), |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 238 | |
| 239 | //Mount out dir as read-write |
Taylor Santiago | ca30e08 | 2024-07-30 13:48:43 -0700 | [diff] [blame] | 240 | "-B", c.outDirArg(), |
Diego Wilson | a22240b | 2020-04-02 18:11:28 +0000 | [diff] [blame] | 241 | |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 242 | // Disable newcgroup for now, since it may require newer kernels |
| 243 | // TODO: try out cgroups |
| 244 | "--disable_clone_newcgroup", |
| 245 | |
| 246 | // Only log important warnings / errors |
| 247 | "-q", |
Taylor Santiago | 2611851 | 2024-08-05 19:52:41 -0700 | [diff] [blame] | 248 | ) |
Taylor Santiago | 3c16e61 | 2024-05-30 14:41:31 -0700 | [diff] [blame] | 249 | if c.config.UseABFS() { |
| 250 | sandboxArgs = append(sandboxArgs, "-B", "{ABFS_DIR}") |
| 251 | } |
Dan Willemsen | 25e6f09 | 2019-04-09 10:22:43 -0700 | [diff] [blame] | 252 | |
Spandan Das | a3639e6 | 2021-05-25 19:14:02 +0000 | [diff] [blame] | 253 | // Mount srcDir RW allowlists as Read-Write |
| 254 | if len(c.config.sandboxConfig.SrcDirRWAllowlist()) > 0 && !c.config.sandboxConfig.SrcDirIsRO() { |
| 255 | errMsg := `Product source tree has been set as ReadWrite, RW allowlist not necessary. |
| 256 | To recover, either |
| 257 | 1. Unset BUILD_BROKEN_SRC_DIR_IS_WRITABLE #or |
| 258 | 2. Unset BUILD_BROKEN_SRC_DIR_RW_ALLOWLIST` |
| 259 | c.ctx.Fatalln(errMsg) |
| 260 | } |
| 261 | for _, srcDirChild := range c.config.sandboxConfig.SrcDirRWAllowlist() { |
| 262 | sandboxArgs = append(sandboxArgs, "-B", srcDirChild) |
| 263 | } |
| 264 | |
Diego Wilson | a5d9653 | 2020-04-06 22:02:38 +0000 | [diff] [blame] | 265 | if _, err := os.Stat(sandboxConfig.distDir); !os.IsNotExist(err) { |
| 266 | //Mount dist dir as read-write if it already exists |
| 267 | sandboxArgs = append(sandboxArgs, "-B", sandboxConfig.distDir) |
| 268 | } |
| 269 | |
Dan Willemsen | 25e6f09 | 2019-04-09 10:22:43 -0700 | [diff] [blame] | 270 | if c.Sandbox.AllowBuildBrokenUsesNetwork && c.config.BuildBrokenUsesNetwork() { |
| 271 | c.ctx.Printf("AllowBuildBrokenUsesNetwork: %v", c.Sandbox.AllowBuildBrokenUsesNetwork) |
| 272 | c.ctx.Printf("BuildBrokenUsesNetwork: %v", c.config.BuildBrokenUsesNetwork()) |
| 273 | sandboxArgs = append(sandboxArgs, "-N") |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 274 | } else if dlv, _ := c.config.Environment().Get("SOONG_DELVE"); dlv != "" { |
| 275 | // The debugger is enabled and soong_build will pause until a remote delve process connects, allow |
| 276 | // network connections. |
| 277 | sandboxArgs = append(sandboxArgs, "-N") |
Dan Willemsen | 25e6f09 | 2019-04-09 10:22:43 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // Stop nsjail from parsing arguments |
| 281 | sandboxArgs = append(sandboxArgs, "--") |
| 282 | |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 283 | c.Args = append(sandboxArgs, c.Args[1:]...) |
| 284 | c.Path = nsjailPath |
| 285 | |
| 286 | env := Environment(c.Env) |
| 287 | if _, hasUser := env.Get("USER"); hasUser { |
| 288 | env.Set("USER", "nobody") |
| 289 | } |
| 290 | c.Env = []string(env) |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 291 | } |