Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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. |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 14 | |
Philipp Wollermann | 1572344e | 2015-06-29 13:59:45 +0000 | [diff] [blame] | 15 | package com.google.devtools.build.lib.sandbox; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 16 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.devtools.build.lib.actions.ExecutionStrategy; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.devtools.build.lib.actions.SpawnActionContext; |
ulfjack | 19befaf | 2017-07-24 11:09:40 +0200 | [diff] [blame] | 19 | import com.google.devtools.build.lib.exec.AbstractSpawnStrategy; |
| 20 | import com.google.devtools.build.lib.exec.SpawnRunner; |
Philipp Wollermann | e219a24 | 2016-08-18 14:39:37 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.runtime.CommandEnvironment; |
Philipp Wollermann | 95b16a8 | 2016-09-30 10:32:36 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import com.google.devtools.build.lib.vfs.Path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import java.io.IOException; |
ruperts | da00941 | 2017-12-22 16:59:38 -0800 | [diff] [blame] | 25 | import java.time.Duration; |
jmmv | bd923e7 | 2018-04-10 14:01:34 -0700 | [diff] [blame] | 26 | import javax.annotation.Nullable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 28 | /** Strategy that uses sandboxing to execute a process. */ |
ulfjack | a63da8e | 2017-07-12 16:51:20 +0200 | [diff] [blame] | 29 | // TODO(ulfjack): This class only exists for this annotation. Find a better way to handle this! |
Yue Gan | 6b88e15 | 2016-04-12 14:10:27 +0000 | [diff] [blame] | 30 | @ExecutionStrategy( |
philwo | 0c613be | 2017-04-21 16:39:30 +0200 | [diff] [blame] | 31 | name = {"sandboxed", "linux-sandbox"}, |
Yue Gan | 6b88e15 | 2016-04-12 14:10:27 +0000 | [diff] [blame] | 32 | contextType = SpawnActionContext.class |
| 33 | ) |
ulfjack | 19befaf | 2017-07-24 11:09:40 +0200 | [diff] [blame] | 34 | public final class LinuxSandboxedStrategy extends AbstractSpawnStrategy { |
tomlu | 1a19b62 | 2018-01-11 15:17:28 -0800 | [diff] [blame] | 35 | LinuxSandboxedStrategy(Path execRoot, SpawnRunner spawnRunner) { |
| 36 | super(execRoot, spawnRunner); |
philwo | db5e06a | 2017-05-12 23:41:47 +0200 | [diff] [blame] | 37 | } |
| 38 | |
ulfjack | 19befaf | 2017-07-24 11:09:40 +0200 | [diff] [blame] | 39 | @Override |
| 40 | public String toString() { |
philwo | d3de5cc | 2018-04-16 06:40:19 -0700 | [diff] [blame] | 41 | return "linux-sandbox"; |
ulfjack | 19befaf | 2017-07-24 11:09:40 +0200 | [diff] [blame] | 42 | } |
| 43 | |
ruperts | da00941 | 2017-12-22 16:59:38 -0800 | [diff] [blame] | 44 | /** |
ruperts | da00941 | 2017-12-22 16:59:38 -0800 | [diff] [blame] | 45 | * Creates a sandboxed spawn runner that uses the {@code linux-sandbox} tool. |
| 46 | * |
| 47 | * @param cmdEnv the command environment to use |
| 48 | * @param sandboxBase path to the sandbox base directory |
Philipp Wollermann | 23e1c5d | 2018-03-23 07:39:27 -0700 | [diff] [blame] | 49 | * @param timeoutKillDelay additional grace period before killing timing out commands |
jmmv | bd923e7 | 2018-04-10 14:01:34 -0700 | [diff] [blame] | 50 | * @param sandboxfsProcess instance of the sandboxfs process to use; may be null for none, in |
| 51 | * which case the runner uses a symlinked sandbox |
jmmv | c2ba4a0 | 2019-03-18 14:51:45 -0700 | [diff] [blame^] | 52 | * @param sandboxfsMapSymlinkTargets map the targets of symlinks within the sandbox if true |
ruperts | da00941 | 2017-12-22 16:59:38 -0800 | [diff] [blame] | 53 | */ |
| 54 | static LinuxSandboxedSpawnRunner create( |
jmmv | c2ba4a0 | 2019-03-18 14:51:45 -0700 | [diff] [blame^] | 55 | CommandEnvironment cmdEnv, |
| 56 | Path sandboxBase, |
| 57 | Duration timeoutKillDelay, |
| 58 | @Nullable SandboxfsProcess sandboxfsProcess, |
| 59 | boolean sandboxfsMapSymlinkTargets) |
| 60 | throws IOException { |
philwo | db5e06a | 2017-05-12 23:41:47 +0200 | [diff] [blame] | 61 | Path inaccessibleHelperFile = sandboxBase.getRelative("inaccessibleHelperFile"); |
| 62 | FileSystemUtils.touchFile(inaccessibleHelperFile); |
| 63 | inaccessibleHelperFile.setReadable(false); |
| 64 | inaccessibleHelperFile.setWritable(false); |
| 65 | inaccessibleHelperFile.setExecutable(false); |
| 66 | |
| 67 | Path inaccessibleHelperDir = sandboxBase.getRelative("inaccessibleHelperDir"); |
| 68 | inaccessibleHelperDir.createDirectory(); |
| 69 | inaccessibleHelperDir.setReadable(false); |
| 70 | inaccessibleHelperDir.setWritable(false); |
| 71 | inaccessibleHelperDir.setExecutable(false); |
| 72 | |
ulfjack | 19befaf | 2017-07-24 11:09:40 +0200 | [diff] [blame] | 73 | return new LinuxSandboxedSpawnRunner( |
philwo | db5e06a | 2017-05-12 23:41:47 +0200 | [diff] [blame] | 74 | cmdEnv, |
philwo | db5e06a | 2017-05-12 23:41:47 +0200 | [diff] [blame] | 75 | sandboxBase, |
philwo | db5e06a | 2017-05-12 23:41:47 +0200 | [diff] [blame] | 76 | inaccessibleHelperFile, |
philwo | 7f08537 | 2017-07-18 16:31:34 +0200 | [diff] [blame] | 77 | inaccessibleHelperDir, |
jmmv | bd923e7 | 2018-04-10 14:01:34 -0700 | [diff] [blame] | 78 | timeoutKillDelay, |
jmmv | c2ba4a0 | 2019-03-18 14:51:45 -0700 | [diff] [blame^] | 79 | sandboxfsProcess, |
| 80 | sandboxfsMapSymlinkTargets); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 81 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | } |