Prashant Malani | b3241da | 2020-12-09 10:06:18 -0800 | [diff] [blame^] | 1 | // Copyright 2020 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/ectool_util.h" |
| 6 | |
| 7 | #include <base/files/file_util.h> |
| 8 | |
| 9 | #include "debugd/src/error_utils.h" |
| 10 | #include "debugd/src/process_with_output.h" |
| 11 | |
| 12 | namespace { |
| 13 | |
| 14 | constexpr char kErrorPath[] = "org.chromium.debugd.ECToolError"; |
| 15 | constexpr char kEctoolBinary[] = "/usr/sbin/ectool"; |
| 16 | |
| 17 | } // namespace |
| 18 | |
| 19 | namespace debugd { |
| 20 | |
| 21 | // Runs ectool with the provided |ectool_args| in a sandboxed process. Returns |
| 22 | // true on success. |
| 23 | bool RunEctoolWithArgs(brillo::ErrorPtr* error, |
| 24 | const base::FilePath& seccomp_policy_path, |
| 25 | const std::vector<std::string> ectool_args, |
| 26 | const std::string& user, |
| 27 | std::string* output) { |
| 28 | if (!base::PathExists(seccomp_policy_path)) { |
| 29 | DEBUGD_ADD_ERROR(error, kErrorPath, |
| 30 | "Sandbox info is missing for this architecture."); |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | // Minijail setup for ectool. |
| 35 | std::vector<std::string> parsed_args{"-c", "cap_sys_rawio=e", "-b", |
| 36 | "/dev/cros_ec"}; |
| 37 | |
| 38 | ProcessWithOutput process; |
| 39 | process.SandboxAs(user, user); |
| 40 | process.SetSeccompFilterPolicyFile(seccomp_policy_path.MaybeAsASCII()); |
| 41 | process.InheritUsergroups(); |
| 42 | if (!process.Init(parsed_args)) { |
| 43 | DEBUGD_ADD_ERROR(error, kErrorPath, "Process initialization failure."); |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | process.AddArg(kEctoolBinary); |
| 48 | for (const auto& arg : ectool_args) |
| 49 | process.AddArg(arg); |
| 50 | if (process.Run() != EXIT_SUCCESS) { |
| 51 | DEBUGD_ADD_ERROR(error, kErrorPath, "Failed to run process."); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | if (!process.GetOutput(output)) { |
| 56 | DEBUGD_ADD_ERROR(error, kErrorPath, "Failed to get output from process."); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | } // namespace debugd |