Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "debugd/src/probe_tool.h" |
| 6 | |
Clark Chung | fb2997b | 2019-07-10 12:21:33 +0800 | [diff] [blame] | 7 | #include <fcntl.h> |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 8 | |
Chung-Sheng Wu | 0d82b44 | 2020-10-13 11:37:24 +0800 | [diff] [blame] | 9 | #include <array> |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <string> |
| 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 15 | #include <base/files/file_path.h> |
Clark Chung | fb2997b | 2019-07-10 12:21:33 +0800 | [diff] [blame] | 16 | #include <base/files/file_util.h> |
| 17 | #include <base/files/scoped_file.h> |
| 18 | #include <base/files/scoped_temp_dir.h> |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 19 | #include <base/json/json_reader.h> |
Chung-Sheng Wu | a0e3e24 | 2020-10-15 12:38:04 +0800 | [diff] [blame] | 20 | #include <base/json/json_writer.h> |
Clark Chung | fb2997b | 2019-07-10 12:21:33 +0800 | [diff] [blame] | 21 | #include <base/logging.h> |
Chung-Sheng Wu | eb6d53a | 2020-11-24 14:26:08 +0800 | [diff] [blame] | 22 | #include <base/optional.h> |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 23 | #include <base/strings/string_number_conversions.h> |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 24 | #include <base/strings/stringprintf.h> |
Clark Chung | fb2997b | 2019-07-10 12:21:33 +0800 | [diff] [blame] | 25 | #include <base/strings/string_split.h> |
| 26 | #include <base/values.h> |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 27 | #include <brillo/errors/error_codes.h> |
| 28 | #include <build/build_config.h> |
| 29 | #include <build/buildflag.h> |
| 30 | #include <chromeos/dbus/service_constants.h> |
| 31 | #include <vboot/crossystem.h> |
| 32 | |
Clark Chung | fb2997b | 2019-07-10 12:21:33 +0800 | [diff] [blame] | 33 | #include "debugd/src/error_utils.h" |
| 34 | #include "debugd/src/sandboxed_process.h" |
| 35 | |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 36 | namespace debugd { |
| 37 | |
| 38 | namespace { |
| 39 | constexpr char kErrorPath[] = "org.chromium.debugd.RunProbeFunctionError"; |
| 40 | constexpr char kSandboxInfoDir[] = "/etc/runtime_probe/sandbox"; |
Chung-Sheng Wu | 1e4d5d6 | 2020-11-05 14:53:39 +0800 | [diff] [blame] | 41 | constexpr char kSandboxArgs[] = "/etc/runtime_probe/sandbox/args.json"; |
Chung-Sheng Wu | 0d82b44 | 2020-10-13 11:37:24 +0800 | [diff] [blame] | 42 | constexpr std::array<const char*, 3> kBinaryAndArgs{"/usr/bin/runtime_probe", |
| 43 | "--helper", "--"}; |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 44 | constexpr char kRunAs[] = "runtime_probe"; |
| 45 | |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 46 | bool CreateNonblockingPipe(base::ScopedFD* read_fd, base::ScopedFD* write_fd) { |
Clark Chung | fb2997b | 2019-07-10 12:21:33 +0800 | [diff] [blame] | 47 | int pipe_fd[2]; |
| 48 | int ret = pipe2(pipe_fd, O_CLOEXEC | O_NONBLOCK); |
| 49 | if (ret != 0) { |
Chung-Sheng Wu | a0e3e24 | 2020-10-15 12:38:04 +0800 | [diff] [blame] | 50 | PLOG(ERROR) << "Cannot create a pipe"; |
Clark Chung | fb2997b | 2019-07-10 12:21:33 +0800 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | read_fd->reset(pipe_fd[0]); |
| 54 | write_fd->reset(pipe_fd[1]); |
| 55 | return true; |
| 56 | } |
| 57 | |
Chung-Sheng Wu | a0e3e24 | 2020-10-15 12:38:04 +0800 | [diff] [blame] | 58 | std::string GetStringFromValue(const base::Value& value) { |
| 59 | std::string json; |
| 60 | base::JSONWriter::WriteWithOptions( |
| 61 | value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
| 62 | return json; |
| 63 | } |
| 64 | |
Chung-Sheng Wu | 1e4d5d6 | 2020-11-05 14:53:39 +0800 | [diff] [blame] | 65 | bool AppendSandboxArgs(brillo::ErrorPtr* error, |
Chung-Sheng Wu | eb6d53a | 2020-11-24 14:26:08 +0800 | [diff] [blame] | 66 | const std::string& function_name, |
Chung-Sheng Wu | 1e4d5d6 | 2020-11-05 14:53:39 +0800 | [diff] [blame] | 67 | std::vector<std::string>* parsed_args) { |
| 68 | std::string minijail_args_str; |
| 69 | if (!base::ReadFileToString(base::FilePath(kSandboxArgs), |
| 70 | &minijail_args_str)) { |
| 71 | DEBUGD_ADD_ERROR_FMT(error, kErrorPath, |
| 72 | "Failed to read minijail arguments from: %s", |
| 73 | kSandboxArgs); |
| 74 | return false; |
| 75 | } |
| 76 | auto minijail_args_dict = base::JSONReader::Read(minijail_args_str); |
| 77 | if (!minijail_args_dict || !minijail_args_dict->is_dict()) { |
| 78 | DEBUGD_ADD_ERROR_FMT(error, kErrorPath, |
| 79 | "Minijail arguments are not stored in dict. Expected " |
| 80 | "dict but got: %s", |
| 81 | minijail_args_str.c_str()); |
| 82 | return false; |
| 83 | } |
Chung-Sheng Wu | eb6d53a | 2020-11-24 14:26:08 +0800 | [diff] [blame] | 84 | const auto* minijail_args = minijail_args_dict->FindListKey(function_name); |
Chung-Sheng Wu | 1e4d5d6 | 2020-11-05 14:53:39 +0800 | [diff] [blame] | 85 | if (!minijail_args) { |
| 86 | DEBUGD_ADD_ERROR_FMT(error, kErrorPath, |
| 87 | "Arguments of \"%s\" is not found in minijail " |
| 88 | "arguments file: %s", |
Chung-Sheng Wu | eb6d53a | 2020-11-24 14:26:08 +0800 | [diff] [blame] | 89 | function_name.c_str(), kSandboxArgs); |
Chung-Sheng Wu | 1e4d5d6 | 2020-11-05 14:53:39 +0800 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | DVLOG(1) << "Minijail arguments: " << (*minijail_args); |
| 93 | for (const auto& arg : minijail_args->GetList()) { |
| 94 | if (!arg.is_string()) { |
| 95 | auto arg_str = GetStringFromValue(arg); |
| 96 | DEBUGD_ADD_ERROR_FMT( |
| 97 | error, kErrorPath, |
| 98 | "Failed to parse minijail arguments. Expected string but got: %s", |
| 99 | arg_str.c_str()); |
| 100 | return false; |
| 101 | } |
| 102 | parsed_args->push_back(arg.GetString()); |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |
Chung-Sheng Wu | eb6d53a | 2020-11-24 14:26:08 +0800 | [diff] [blame] | 107 | base::Optional<std::string> GetFunctionNameFromProbeStatement( |
| 108 | brillo::ErrorPtr* error, const std::string& probe_statement) { |
| 109 | // The name of the probe function is the only key in the dictionary. |
| 110 | auto probe_statement_dict = base::JSONReader::Read(probe_statement); |
| 111 | if (!probe_statement_dict || !probe_statement_dict->is_dict()) { |
| 112 | DEBUGD_ADD_ERROR_FMT( |
| 113 | error, kErrorPath, |
| 114 | "Failed to parse probe statement. Expected json but got: %s", |
| 115 | probe_statement.c_str()); |
| 116 | return base::nullopt; |
| 117 | } |
| 118 | if (probe_statement_dict->DictSize() != 1) { |
| 119 | DEBUGD_ADD_ERROR_FMT( |
| 120 | error, kErrorPath, |
| 121 | "Expected only one probe function in probe statement but got: %zu", |
| 122 | probe_statement_dict->DictSize()); |
| 123 | return base::nullopt; |
| 124 | } |
| 125 | auto it = probe_statement_dict->DictItems().begin(); |
| 126 | const auto& function_name = it->first; |
| 127 | return function_name; |
| 128 | } |
| 129 | |
Chung-Sheng Wu | a0e3e24 | 2020-10-15 12:38:04 +0800 | [diff] [blame] | 130 | std::unique_ptr<brillo::Process> CreateSandboxedProcess( |
Chung-Sheng Wu | eb6d53a | 2020-11-24 14:26:08 +0800 | [diff] [blame] | 131 | brillo::ErrorPtr* error, const std::string& probe_statement) { |
| 132 | const auto function_name = |
| 133 | GetFunctionNameFromProbeStatement(error, probe_statement); |
| 134 | if (!function_name) |
| 135 | return nullptr; |
| 136 | |
Chung-Sheng Wu | a0e3e24 | 2020-10-15 12:38:04 +0800 | [diff] [blame] | 137 | auto sandboxed_process = std::make_unique<SandboxedProcess>(); |
Chung-Sheng Wu | a0e3e24 | 2020-10-15 12:38:04 +0800 | [diff] [blame] | 138 | // The following is the general minijail set up for runtime_probe in debugd |
| 139 | // /dev/log needs to be bind mounted before any possible tmpfs mount on run |
| 140 | // See: |
| 141 | // minijail0 manpage (`man 1 minijail0` in cros\_sdk) |
Tom Hughes | 18251f9 | 2020-12-07 13:38:14 -0800 | [diff] [blame] | 142 | // https://chromium.googlesource.com/chromiumos/docs/+/HEAD/sandboxing.md |
Chung-Sheng Wu | a0e3e24 | 2020-10-15 12:38:04 +0800 | [diff] [blame] | 143 | std::vector<std::string> parsed_args{ |
| 144 | "-G", // Inherit all the supplementary groups |
| 145 | "-P", "/mnt/empty", // Set /mnt/empty as the root fs using pivot_root |
| 146 | "-b", "/", // Bind mount rootfs |
| 147 | "-b", "/proc", // Bind mount /proc |
| 148 | "-b", "/dev/log", // Enable logging |
| 149 | "-t", // Mount a tmpfs on /tmp |
| 150 | "-r", // Remount /proc readonly |
| 151 | "-d" // Mount /dev with a minimal set of nodes. |
| 152 | }; |
Chung-Sheng Wu | eb6d53a | 2020-11-24 14:26:08 +0800 | [diff] [blame] | 153 | if (!AppendSandboxArgs(error, *function_name, &parsed_args)) |
Chung-Sheng Wu | 1e4d5d6 | 2020-11-05 14:53:39 +0800 | [diff] [blame] | 154 | return nullptr; |
Chung-Sheng Wu | a0e3e24 | 2020-10-15 12:38:04 +0800 | [diff] [blame] | 155 | |
| 156 | sandboxed_process->SandboxAs(kRunAs, kRunAs); |
Chung-Sheng Wu | 1e4d5d6 | 2020-11-05 14:53:39 +0800 | [diff] [blame] | 157 | const auto seccomp_path = base::FilePath{kSandboxInfoDir}.Append( |
Chung-Sheng Wu | eb6d53a | 2020-11-24 14:26:08 +0800 | [diff] [blame] | 158 | base::StringPrintf("%s-seccomp.policy", function_name->c_str())); |
Chung-Sheng Wu | 1e4d5d6 | 2020-11-05 14:53:39 +0800 | [diff] [blame] | 159 | if (!base::PathExists(seccomp_path)) { |
| 160 | DEBUGD_ADD_ERROR_FMT(error, kErrorPath, |
| 161 | "Seccomp policy file of \"%s\" is not found at: %s", |
Chung-Sheng Wu | eb6d53a | 2020-11-24 14:26:08 +0800 | [diff] [blame] | 162 | function_name->c_str(), seccomp_path.value().c_str()); |
Chung-Sheng Wu | 1e4d5d6 | 2020-11-05 14:53:39 +0800 | [diff] [blame] | 163 | return nullptr; |
| 164 | } |
Chung-Sheng Wu | a0e3e24 | 2020-10-15 12:38:04 +0800 | [diff] [blame] | 165 | sandboxed_process->SetSeccompFilterPolicyFile(seccomp_path.MaybeAsASCII()); |
Chung-Sheng Wu | eb6d53a | 2020-11-24 14:26:08 +0800 | [diff] [blame] | 166 | DVLOG(1) << "Sandbox for " << (*function_name) << " is ready"; |
Chung-Sheng Wu | a0e3e24 | 2020-10-15 12:38:04 +0800 | [diff] [blame] | 167 | if (!sandboxed_process->Init(parsed_args)) { |
| 168 | DEBUGD_ADD_ERROR(error, kErrorPath, |
| 169 | "Sandboxed process initialization failure"); |
| 170 | return nullptr; |
| 171 | } |
| 172 | return sandboxed_process; |
| 173 | } |
| 174 | |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 175 | } // namespace |
| 176 | |
Clark Chung | fb2997b | 2019-07-10 12:21:33 +0800 | [diff] [blame] | 177 | bool ProbeTool::EvaluateProbeFunction( |
| 178 | brillo::ErrorPtr* error, |
Clark Chung | fb2997b | 2019-07-10 12:21:33 +0800 | [diff] [blame] | 179 | const std::string& probe_statement, |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 180 | brillo::dbus_utils::FileDescriptor* outfd) { |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 181 | // Details of sandboxing for probing should be centralized in a single |
| 182 | // directory. Sandboxing is mandatory when we don't allow debug features. |
Chung-Sheng Wu | eb6d53a | 2020-11-24 14:26:08 +0800 | [diff] [blame] | 183 | auto process = CreateSandboxedProcess(error, probe_statement); |
Chung-Sheng Wu | a0e3e24 | 2020-10-15 12:38:04 +0800 | [diff] [blame] | 184 | if (process == nullptr) |
| 185 | return false; |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 186 | |
Clark Chung | fb2997b | 2019-07-10 12:21:33 +0800 | [diff] [blame] | 187 | base::ScopedFD read_fd, write_fd; |
| 188 | if (!CreateNonblockingPipe(&read_fd, &write_fd)) { |
Chung-Sheng Wu | a0e3e24 | 2020-10-15 12:38:04 +0800 | [diff] [blame] | 189 | DEBUGD_ADD_ERROR(error, kErrorPath, "Cannot create a pipe"); |
Clark Chung | fb2997b | 2019-07-10 12:21:33 +0800 | [diff] [blame] | 190 | return false; |
| 191 | } |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 192 | |
Chung-Sheng Wu | 0d82b44 | 2020-10-13 11:37:24 +0800 | [diff] [blame] | 193 | for (auto arg : kBinaryAndArgs) { |
| 194 | process->AddArg(arg); |
| 195 | } |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 196 | process->AddArg(probe_statement); |
Clark Chung | fb2997b | 2019-07-10 12:21:33 +0800 | [diff] [blame] | 197 | process->BindFd(write_fd.get(), STDOUT_FILENO); |
| 198 | process->Start(); |
| 199 | process->Release(); |
| 200 | *outfd = std::move(read_fd); |
Chun-Ta Lin | 32b8a51 | 2019-03-25 18:53:40 +0800 | [diff] [blame] | 201 | return true; |
| 202 | } |
| 203 | |
| 204 | } // namespace debugd |