Roman Sorokin | daa3fc0 | 2016-10-26 16:24:26 +0200 | [diff] [blame] | 1 | // Copyright 2016 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 "authpolicy/process_executor.h" |
| 6 | |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 7 | #include <stdlib.h> |
Lutz Justen | f44987e | 2017-04-03 11:26:23 +0200 | [diff] [blame] | 8 | #include <algorithm> |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 9 | #include <utility> |
Roman Sorokin | daa3fc0 | 2016-10-26 16:24:26 +0200 | [diff] [blame] | 10 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 11 | #include <base/check.h> |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 12 | #include <base/files/file_path.h> |
Lutz Justen | 54b2da9 | 2017-01-19 23:50:19 +0100 | [diff] [blame] | 13 | #include <base/files/file_util.h> |
Lutz Justen | df64d03 | 2017-02-09 14:49:15 +0100 | [diff] [blame] | 14 | #include <base/strings/string_split.h> |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 15 | #include <base/strings/stringprintf.h> |
| 16 | #include <libminijail.h> |
Lutz Justen | 841a8d8 | 2017-03-13 17:25:57 -0700 | [diff] [blame] | 17 | #include <scoped_minijail.h> |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 18 | |
Lutz Justen | 63436ab | 2017-06-12 13:37:21 +0200 | [diff] [blame] | 19 | #include "authpolicy/anonymizer.h" |
Lutz Justen | 350cdde | 2018-01-19 09:42:11 +0100 | [diff] [blame] | 20 | #include "authpolicy/log_colors.h" |
Lutz Justen | d7b00fd | 2017-02-02 09:46:41 +0100 | [diff] [blame] | 21 | #include "authpolicy/platform_helper.h" |
Lutz Justen | 1589f9d | 2017-05-18 09:50:41 +0200 | [diff] [blame] | 22 | #include "authpolicy/samba_helper.h" |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 23 | |
Lutz Justen | f44987e | 2017-04-03 11:26:23 +0200 | [diff] [blame] | 24 | namespace authpolicy { |
Lutz Justen | 1b1519d | 2017-05-05 14:12:21 +0200 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | // Prevent some environment variables from being wiped since they're used by |
| 28 | // tests. crbug.com/718182. |
| 29 | struct EnvVarDef { |
| 30 | const char* name_equals; // "name=" |
| 31 | size_t size; // strlen("name=") |
| 32 | }; |
| 33 | |
| 34 | // Note: strlen doesn't work with constexpr. |
| 35 | #define DEFINE_ENV_VAR(name) \ |
| 36 | { name "=", sizeof(name "=") - 1 } |
| 37 | |
| 38 | constexpr EnvVarDef kWhitelistedEnvVars[]{ |
Tom Hughes | 8bda4e6 | 2020-08-24 18:02:53 -0700 | [diff] [blame] | 39 | DEFINE_ENV_VAR("ASAN_OPTIONS"), DEFINE_ENV_VAR("LSAN_OPTIONS"), |
| 40 | DEFINE_ENV_VAR("MSAN_OPTIONS"), DEFINE_ENV_VAR("TSAN_OPTIONS"), |
Manoj Gupta | d93e771 | 2018-08-24 09:39:08 -0700 | [diff] [blame] | 41 | DEFINE_ENV_VAR("UBSAN_OPTIONS"), |
Lutz Justen | 1b1519d | 2017-05-05 14:12:21 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | #undef DEFINE_ENV_VAR |
| 45 | |
| 46 | } // namespace |
Lutz Justen | df64d03 | 2017-02-09 14:49:15 +0100 | [diff] [blame] | 47 | |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 48 | ProcessExecutor::ProcessExecutor(std::vector<std::string> args) |
Lutz Justen | 841a8d8 | 2017-03-13 17:25:57 -0700 | [diff] [blame] | 49 | : args_(std::move(args)) {} |
Roman Sorokin | daa3fc0 | 2016-10-26 16:24:26 +0200 | [diff] [blame] | 50 | |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 51 | void ProcessExecutor::SetInputFile(int fd) { |
Lutz Justen | 875dd42 | 2016-11-10 16:02:00 +0100 | [diff] [blame] | 52 | input_fd_ = fd; |
Roman Sorokin | daa3fc0 | 2016-10-26 16:24:26 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 55 | void ProcessExecutor::SetInputString(const std::string& input_str) { |
| 56 | input_str_ = input_str; |
| 57 | } |
| 58 | |
| 59 | void ProcessExecutor::SetEnv(const std::string& key, const std::string& value) { |
| 60 | env_map_[key] = value; |
| 61 | } |
| 62 | |
| 63 | void ProcessExecutor::SetSeccompFilter(const std::string& policy_file) { |
Lutz Justen | 841a8d8 | 2017-03-13 17:25:57 -0700 | [diff] [blame] | 64 | seccomp_policy_file_ = policy_file; |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Lutz Justen | 1589f9d | 2017-05-18 09:50:41 +0200 | [diff] [blame] | 67 | void ProcessExecutor::LogSeccompFilterFailures(bool enabled) { |
| 68 | log_seccomp_failures_ = enabled; |
Lutz Justen | 4b19b79 | 2017-01-13 17:02:44 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Lutz Justen | 1589f9d | 2017-05-18 09:50:41 +0200 | [diff] [blame] | 71 | void ProcessExecutor::SetNoNewPrivs(bool enabled) { |
| 72 | no_new_privs_ = enabled; |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Lutz Justen | 1589f9d | 2017-05-18 09:50:41 +0200 | [diff] [blame] | 75 | void ProcessExecutor::KeepSupplementaryGroups(bool enabled) { |
| 76 | keep_supplementary_flags_ = enabled; |
| 77 | } |
| 78 | |
| 79 | void ProcessExecutor::LogCommand(bool enabled) { |
| 80 | log_command_ = enabled; |
| 81 | } |
| 82 | |
| 83 | void ProcessExecutor::LogOutput(bool enabled) { |
| 84 | log_output_ = enabled; |
| 85 | } |
| 86 | |
| 87 | void ProcessExecutor::LogOutputOnError(bool enabled) { |
| 88 | log_output_on_error_ = enabled; |
Lutz Justen | 2eb6395 | 2017-01-03 19:58:02 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Lutz Justen | 63436ab | 2017-06-12 13:37:21 +0200 | [diff] [blame] | 91 | void ProcessExecutor::SetAnonymizer(Anonymizer* anonymizer) { |
| 92 | anonymizer_ = anonymizer; |
| 93 | } |
| 94 | |
Roman Sorokin | daa3fc0 | 2016-10-26 16:24:26 +0200 | [diff] [blame] | 95 | bool ProcessExecutor::Execute() { |
Lutz Justen | 875dd42 | 2016-11-10 16:02:00 +0100 | [diff] [blame] | 96 | ResetOutput(); |
Lutz Justen | f44987e | 2017-04-03 11:26:23 +0200 | [diff] [blame] | 97 | if (args_.empty() || args_[0].empty()) |
| 98 | return true; |
Lutz Justen | 875dd42 | 2016-11-10 16:02:00 +0100 | [diff] [blame] | 99 | |
Lutz Justen | 63436ab | 2017-06-12 13:37:21 +0200 | [diff] [blame] | 100 | bool need_anonymizer = log_command_ || log_output_ || log_output_on_error_; |
| 101 | CHECK(anonymizer_ || !need_anonymizer) << "Logs must be anonymized"; |
| 102 | |
Lutz Justen | 875dd42 | 2016-11-10 16:02:00 +0100 | [diff] [blame] | 103 | if (!base::FilePath(args_[0]).IsAbsolute()) { |
| 104 | LOG(ERROR) << "Command must be specified by absolute path."; |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 105 | exit_code_ = kExitCodeInternalError; |
Lutz Justen | 875dd42 | 2016-11-10 16:02:00 +0100 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | |
Lutz Justen | 1589f9d | 2017-05-18 09:50:41 +0200 | [diff] [blame] | 109 | if (log_command_ && LOG_IS_ON(INFO)) { |
Lutz Justen | 875dd42 | 2016-11-10 16:02:00 +0100 | [diff] [blame] | 110 | std::string cmd = args_[0]; |
| 111 | for (size_t n = 1; n < args_.size(); ++n) |
| 112 | cmd += base::StringPrintf(" '%s'", args_[n].c_str()); |
Lutz Justen | 350cdde | 2018-01-19 09:42:11 +0100 | [diff] [blame] | 113 | LOG(INFO) << kColorCommand << "Executing " << anonymizer_->Process(cmd) |
| 114 | << kColorReset; |
Roman Sorokin | daa3fc0 | 2016-10-26 16:24:26 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 117 | // Convert args to array of pointers. Must be nullptr terminated. |
| 118 | std::vector<char*> args_ptr; |
| 119 | for (const auto& arg : args_) |
| 120 | args_ptr.push_back(const_cast<char*>(arg.c_str())); |
| 121 | args_ptr.push_back(nullptr); |
| 122 | |
| 123 | // Save old environment and set ours. Note that clearenv() doesn't actually |
| 124 | // delete any pointers, so we can just keep the old pointers. |
| 125 | std::vector<char*> old_environ; |
| 126 | for (char** env = environ; env != nullptr && *env != nullptr; ++env) |
| 127 | old_environ.push_back(*env); |
| 128 | clearenv(); |
Lutz Justen | 1b1519d | 2017-05-05 14:12:21 +0200 | [diff] [blame] | 129 | |
| 130 | // Store strings in list because putenv requires pointers to stay alive. |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 131 | std::vector<std::string> env_list; |
| 132 | for (const auto& env : env_map_) { |
| 133 | env_list.push_back(env.first + "=" + env.second); |
| 134 | putenv(const_cast<char*>(env_list.back().c_str())); |
| 135 | } |
| 136 | |
Lutz Justen | 1b1519d | 2017-05-05 14:12:21 +0200 | [diff] [blame] | 137 | // Add back whitelisted env vars. Note that |whitelisted_var.name_equals| is |
| 138 | // name= and |env| is name=value. A linear search seems fine, but consider |
| 139 | // using a map if kWhitelistedEnvVars grows. |
| 140 | for (char* env : old_environ) { |
| 141 | for (const EnvVarDef& whitelisted_var : kWhitelistedEnvVars) { |
| 142 | if (strncmp(env, whitelisted_var.name_equals, whitelisted_var.size) == 0) |
| 143 | putenv(env); |
| 144 | } |
| 145 | } |
| 146 | |
Lutz Justen | 841a8d8 | 2017-03-13 17:25:57 -0700 | [diff] [blame] | 147 | // Prepare minijail. |
| 148 | ScopedMinijail jail(minijail_new()); |
| 149 | if (log_seccomp_failures_) |
| 150 | minijail_log_seccomp_filter_failures(jail.get()); |
| 151 | if (!seccomp_policy_file_.empty()) { |
| 152 | minijail_parse_seccomp_filters(jail.get(), seccomp_policy_file_.c_str()); |
| 153 | minijail_use_seccomp_filter(jail.get()); |
| 154 | } |
| 155 | if (no_new_privs_) |
| 156 | minijail_no_new_privs(jail.get()); |
| 157 | if (keep_supplementary_flags_) |
| 158 | minijail_keep_supplementary_gids(jail.get()); |
| 159 | |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 160 | // Execute the command. |
| 161 | pid_t pid = -1; |
| 162 | int child_stdin = -1, child_stdout = -1, child_stderr = -1; |
Lutz Justen | 4b00822 | 2017-10-13 17:50:20 +0200 | [diff] [blame] | 163 | minijail_run_pid_pipes(jail.get(), args_ptr[0], args_ptr.data(), &pid, |
| 164 | &child_stdin, &child_stdout, &child_stderr); |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 165 | |
Lutz Justen | 54b2da9 | 2017-01-19 23:50:19 +0100 | [diff] [blame] | 166 | // Make sure the pipes never block. |
| 167 | if (!base::SetNonBlocking(child_stdin)) |
| 168 | LOG(WARNING) << "Failed to set stdin non-blocking"; |
| 169 | if (!base::SetNonBlocking(child_stdout)) |
| 170 | LOG(WARNING) << "Failed to set stdout non-blocking"; |
| 171 | if (!base::SetNonBlocking(child_stderr)) |
| 172 | LOG(WARNING) << "Failed to set stderr non-blocking"; |
| 173 | |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 174 | // Restore the environment. |
| 175 | clearenv(); |
| 176 | for (char* env : old_environ) |
| 177 | putenv(env); |
| 178 | |
Pavol Marko | 6e32303 | 2019-09-16 10:24:44 +0200 | [diff] [blame] | 179 | if (perform_pipe_io_after_process_exit_for_testing_) |
| 180 | exit_code_ = minijail_wait(jail.get()); |
| 181 | |
Lutz Justen | 54b2da9 | 2017-01-19 23:50:19 +0100 | [diff] [blame] | 182 | // Write to child_stdin and read from child_stdout and child_stderr while |
| 183 | // there is still data to read/write. |
Lutz Justen | 4b00822 | 2017-10-13 17:50:20 +0200 | [diff] [blame] | 184 | bool io_success = |
| 185 | PerformPipeIo(child_stdin, child_stdout, child_stderr, input_fd_, |
| 186 | input_str_, &out_data_, &err_data_); |
Roman Sorokin | daa3fc0 | 2016-10-26 16:24:26 +0200 | [diff] [blame] | 187 | |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 188 | // Wait for the process to exit. |
Pavol Marko | 6e32303 | 2019-09-16 10:24:44 +0200 | [diff] [blame] | 189 | if (!perform_pipe_io_after_process_exit_for_testing_) |
| 190 | exit_code_ = minijail_wait(jail.get()); |
Lutz Justen | 841a8d8 | 2017-03-13 17:25:57 -0700 | [diff] [blame] | 191 | jail.reset(); |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 192 | |
Lutz Justen | 4b19b79 | 2017-01-13 17:02:44 +0100 | [diff] [blame] | 193 | // Print out a useful error message for seccomp failures. |
| 194 | if (exit_code_ == MINIJAIL_ERR_JAIL) |
| 195 | LOG(ERROR) << "Seccomp filter blocked a system call"; |
| 196 | |
Lutz Justen | 54b2da9 | 2017-01-19 23:50:19 +0100 | [diff] [blame] | 197 | // Always exit AFTER minijail_wait! If we do it before, the exit code is never |
| 198 | // queried and the process is left dangling. |
| 199 | if (!io_success) { |
| 200 | LOG(ERROR) << "IO failed"; |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 201 | exit_code_ = kExitCodeInternalError; |
Roman Sorokin | daa3fc0 | 2016-10-26 16:24:26 +0200 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
Lutz Justen | ac94e9b | 2017-07-13 15:55:40 +0200 | [diff] [blame] | 205 | output_logged_ = false; |
| 206 | if (log_output_ || (log_output_on_error_ && exit_code_ != 0)) |
| 207 | LogOutputOnce(); |
Lutz Justen | 350cdde | 2018-01-19 09:42:11 +0100 | [diff] [blame] | 208 | LOG_IF(INFO, log_command_) |
| 209 | << kColorCommand << "Exit code: " << exit_code_ << kColorReset; |
Lutz Justen | f44987e | 2017-04-03 11:26:23 +0200 | [diff] [blame] | 210 | |
Lutz Justen | 6d7b900 | 2016-12-06 21:52:57 +0100 | [diff] [blame] | 211 | return exit_code_ == 0; |
Roman Sorokin | daa3fc0 | 2016-10-26 16:24:26 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Lutz Justen | ac94e9b | 2017-07-13 15:55:40 +0200 | [diff] [blame] | 214 | void ProcessExecutor::LogOutputOnce() { |
| 215 | if (output_logged_ || args_.empty() || !(log_output_on_error_ || log_output_)) |
| 216 | return; |
Lutz Justen | 350cdde | 2018-01-19 09:42:11 +0100 | [diff] [blame] | 217 | LogLongString(kColorCommandStdout, args_[0] + " stdout: ", out_data_, |
| 218 | anonymizer_); |
| 219 | LogLongString(kColorCommandStderr, args_[0] + " stderr: ", err_data_, |
| 220 | anonymizer_); |
Lutz Justen | ac94e9b | 2017-07-13 15:55:40 +0200 | [diff] [blame] | 221 | output_logged_ = true; |
| 222 | } |
| 223 | |
Pavol Marko | 6e32303 | 2019-09-16 10:24:44 +0200 | [diff] [blame] | 224 | void ProcessExecutor::SetPerformPipeIoAfterProcessExitForTesting( |
| 225 | bool perform_pipe_io_after_process_exit_for_testing) { |
| 226 | perform_pipe_io_after_process_exit_for_testing_ = |
| 227 | perform_pipe_io_after_process_exit_for_testing; |
| 228 | } |
| 229 | |
Lutz Justen | 875dd42 | 2016-11-10 16:02:00 +0100 | [diff] [blame] | 230 | void ProcessExecutor::ResetOutput() { |
| 231 | exit_code_ = 0; |
| 232 | out_data_.clear(); |
| 233 | err_data_.clear(); |
| 234 | } |
| 235 | |
Roman Sorokin | daa3fc0 | 2016-10-26 16:24:26 +0200 | [diff] [blame] | 236 | } // namespace authpolicy |