Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "subcontext.h" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <poll.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include <android-base/file.h> |
| 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/strings.h> |
| 27 | #include <selinux/android.h> |
| 28 | |
| 29 | #include "action.h" |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 30 | #include "util.h" |
| 31 | |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 32 | #if defined(__ANDROID__) |
Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame^] | 33 | #include <android/api-level.h> |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 34 | #include "property_service.h" |
| 35 | #include "selinux.h" |
| 36 | #else |
| 37 | #include "host_init_stubs.h" |
| 38 | #endif |
Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 39 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 40 | using android::base::GetExecutablePath; |
| 41 | using android::base::Join; |
| 42 | using android::base::Socketpair; |
| 43 | using android::base::Split; |
| 44 | using android::base::StartsWith; |
| 45 | using android::base::unique_fd; |
| 46 | |
| 47 | namespace android { |
| 48 | namespace init { |
| 49 | |
Tom Cherry | ac7428b | 2017-10-02 16:59:02 -0700 | [diff] [blame] | 50 | const std::string kInitContext = "u:r:init:s0"; |
| 51 | const std::string kVendorContext = "u:r:vendor_init:s0"; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 52 | |
Tom Cherry | dc37586 | 2018-02-28 10:39:01 -0800 | [diff] [blame] | 53 | const char* const paths_and_secontexts[2][2] = { |
| 54 | {"/vendor", kVendorContext.c_str()}, |
| 55 | {"/odm", kVendorContext.c_str()}, |
| 56 | }; |
| 57 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 58 | namespace { |
| 59 | |
| 60 | constexpr size_t kBufferSize = 4096; |
| 61 | |
| 62 | Result<std::string> ReadMessage(int socket) { |
| 63 | char buffer[kBufferSize] = {}; |
| 64 | auto result = TEMP_FAILURE_RETRY(recv(socket, buffer, sizeof(buffer), 0)); |
| 65 | if (result <= 0) { |
| 66 | return ErrnoError(); |
| 67 | } |
| 68 | return std::string(buffer, result); |
| 69 | } |
| 70 | |
| 71 | template <typename T> |
| 72 | Result<Success> SendMessage(int socket, const T& message) { |
| 73 | std::string message_string; |
| 74 | if (!message.SerializeToString(&message_string)) { |
| 75 | return Error() << "Unable to serialize message"; |
| 76 | } |
| 77 | |
| 78 | if (message_string.size() > kBufferSize) { |
| 79 | return Error() << "Serialized message too long to send"; |
| 80 | } |
| 81 | |
| 82 | if (auto result = |
| 83 | TEMP_FAILURE_RETRY(send(socket, message_string.c_str(), message_string.size(), 0)); |
| 84 | result != static_cast<long>(message_string.size())) { |
| 85 | return ErrnoError() << "send() failed to send message contents"; |
| 86 | } |
| 87 | return Success(); |
| 88 | } |
| 89 | |
Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 90 | std::vector<std::pair<std::string, std::string>> properties_to_set; |
| 91 | |
| 92 | uint32_t SubcontextPropertySet(const std::string& name, const std::string& value) { |
| 93 | properties_to_set.emplace_back(name, value); |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 94 | return 0; |
Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 97 | class SubcontextProcess { |
| 98 | public: |
| 99 | SubcontextProcess(const KeywordFunctionMap* function_map, std::string context, int init_fd) |
| 100 | : function_map_(function_map), context_(std::move(context)), init_fd_(init_fd){}; |
| 101 | void MainLoop(); |
| 102 | |
| 103 | private: |
| 104 | void RunCommand(const SubcontextCommand::ExecuteCommand& execute_command, |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 105 | SubcontextReply* reply) const; |
| 106 | void ExpandArgs(const SubcontextCommand::ExpandArgsCommand& expand_args_command, |
| 107 | SubcontextReply* reply) const; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 108 | |
| 109 | const KeywordFunctionMap* function_map_; |
| 110 | const std::string context_; |
| 111 | const int init_fd_; |
| 112 | }; |
| 113 | |
| 114 | void SubcontextProcess::RunCommand(const SubcontextCommand::ExecuteCommand& execute_command, |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 115 | SubcontextReply* reply) const { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 116 | // Need to use ArraySplice instead of this code. |
| 117 | auto args = std::vector<std::string>(); |
| 118 | for (const auto& string : execute_command.args()) { |
| 119 | args.emplace_back(string); |
| 120 | } |
| 121 | |
| 122 | auto map_result = function_map_->FindFunction(args); |
| 123 | Result<Success> result; |
| 124 | if (!map_result) { |
| 125 | result = Error() << "Cannot find command: " << map_result.error(); |
| 126 | } else { |
| 127 | result = RunBuiltinFunction(map_result->second, args, context_); |
| 128 | } |
| 129 | |
Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 130 | for (const auto& [name, value] : properties_to_set) { |
| 131 | auto property = reply->add_properties_to_set(); |
| 132 | property->set_name(name); |
| 133 | property->set_value(value); |
| 134 | } |
| 135 | |
| 136 | properties_to_set.clear(); |
| 137 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 138 | if (result) { |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 139 | reply->set_success(true); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 140 | } else { |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 141 | auto* failure = reply->mutable_failure(); |
| 142 | failure->set_error_string(result.error_string()); |
| 143 | failure->set_error_errno(result.error_errno()); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void SubcontextProcess::ExpandArgs(const SubcontextCommand::ExpandArgsCommand& expand_args_command, |
| 148 | SubcontextReply* reply) const { |
| 149 | for (const auto& arg : expand_args_command.args()) { |
| 150 | auto expanded_prop = std::string{}; |
| 151 | if (!expand_props(arg, &expanded_prop)) { |
| 152 | auto* failure = reply->mutable_failure(); |
| 153 | failure->set_error_string("Failed to expand '" + arg + "'"); |
| 154 | failure->set_error_errno(0); |
| 155 | return; |
| 156 | } else { |
| 157 | auto* expand_args_reply = reply->mutable_expand_args_reply(); |
| 158 | expand_args_reply->add_expanded_args(expanded_prop); |
| 159 | } |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | void SubcontextProcess::MainLoop() { |
| 164 | pollfd ufd[1]; |
| 165 | ufd[0].events = POLLIN; |
| 166 | ufd[0].fd = init_fd_; |
| 167 | |
| 168 | while (true) { |
| 169 | ufd[0].revents = 0; |
| 170 | int nr = TEMP_FAILURE_RETRY(poll(ufd, arraysize(ufd), -1)); |
| 171 | if (nr == 0) continue; |
| 172 | if (nr < 0) { |
| 173 | PLOG(FATAL) << "poll() of subcontext socket failed, continuing"; |
| 174 | } |
| 175 | |
| 176 | auto init_message = ReadMessage(init_fd_); |
| 177 | if (!init_message) { |
| 178 | LOG(FATAL) << "Could not read message from init: " << init_message.error(); |
| 179 | } |
| 180 | |
| 181 | auto subcontext_command = SubcontextCommand(); |
| 182 | if (!subcontext_command.ParseFromString(*init_message)) { |
| 183 | LOG(FATAL) << "Unable to parse message from init"; |
| 184 | } |
| 185 | |
| 186 | auto reply = SubcontextReply(); |
| 187 | switch (subcontext_command.command_case()) { |
| 188 | case SubcontextCommand::kExecuteCommand: { |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 189 | RunCommand(subcontext_command.execute_command(), &reply); |
| 190 | break; |
| 191 | } |
| 192 | case SubcontextCommand::kExpandArgsCommand: { |
| 193 | ExpandArgs(subcontext_command.expand_args_command(), &reply); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 194 | break; |
| 195 | } |
| 196 | default: |
| 197 | LOG(FATAL) << "Unknown message type from init: " |
| 198 | << subcontext_command.command_case(); |
| 199 | } |
| 200 | |
| 201 | if (auto result = SendMessage(init_fd_, reply); !result) { |
| 202 | LOG(FATAL) << "Failed to send message to init: " << result.error(); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | } // namespace |
| 208 | |
| 209 | int SubcontextMain(int argc, char** argv, const KeywordFunctionMap* function_map) { |
| 210 | if (argc < 4) LOG(FATAL) << "Fewer than 4 args specified to subcontext (" << argc << ")"; |
| 211 | |
| 212 | auto context = std::string(argv[2]); |
| 213 | auto init_fd = std::atoi(argv[3]); |
| 214 | |
Tom Cherry | 0d1452e | 2017-10-19 14:39:35 -0700 | [diff] [blame] | 215 | SelabelInitialize(); |
Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 216 | |
| 217 | property_set = SubcontextPropertySet; |
| 218 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 219 | auto subcontext_process = SubcontextProcess(function_map, context, init_fd); |
| 220 | subcontext_process.MainLoop(); |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | void Subcontext::Fork() { |
| 225 | unique_fd subcontext_socket; |
| 226 | if (!Socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, &socket_, &subcontext_socket)) { |
| 227 | LOG(FATAL) << "Could not create socket pair to communicate to subcontext"; |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | auto result = fork(); |
| 232 | |
| 233 | if (result == -1) { |
| 234 | LOG(FATAL) << "Could not fork subcontext"; |
| 235 | } else if (result == 0) { |
| 236 | socket_.reset(); |
| 237 | |
| 238 | // We explicitly do not use O_CLOEXEC here, such that we can reference this FD by number |
| 239 | // in the subcontext process after we exec. |
| 240 | int child_fd = dup(subcontext_socket); |
| 241 | if (child_fd < 0) { |
| 242 | PLOG(FATAL) << "Could not dup child_fd"; |
| 243 | } |
| 244 | |
| 245 | if (setexeccon(context_.c_str()) < 0) { |
| 246 | PLOG(FATAL) << "Could not set execcon for '" << context_ << "'"; |
| 247 | } |
| 248 | |
| 249 | auto init_path = GetExecutablePath(); |
| 250 | auto child_fd_string = std::to_string(child_fd); |
| 251 | const char* args[] = {init_path.c_str(), "subcontext", context_.c_str(), |
| 252 | child_fd_string.c_str(), nullptr}; |
| 253 | execv(init_path.data(), const_cast<char**>(args)); |
| 254 | |
| 255 | PLOG(FATAL) << "Could not execv subcontext init"; |
| 256 | } else { |
| 257 | subcontext_socket.reset(); |
| 258 | pid_ = result; |
| 259 | LOG(INFO) << "Forked subcontext for '" << context_ << "' with pid " << pid_; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void Subcontext::Restart() { |
| 264 | LOG(ERROR) << "Restarting subcontext '" << context_ << "'"; |
| 265 | if (pid_) { |
| 266 | kill(pid_, SIGKILL); |
| 267 | } |
| 268 | pid_ = 0; |
| 269 | socket_.reset(); |
| 270 | Fork(); |
| 271 | } |
| 272 | |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 273 | Result<SubcontextReply> Subcontext::TransmitMessage(const SubcontextCommand& subcontext_command) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 274 | if (auto result = SendMessage(socket_, subcontext_command); !result) { |
| 275 | Restart(); |
| 276 | return ErrnoError() << "Failed to send message to subcontext"; |
| 277 | } |
| 278 | |
| 279 | auto subcontext_message = ReadMessage(socket_); |
| 280 | if (!subcontext_message) { |
| 281 | Restart(); |
| 282 | return Error() << "Failed to receive result from subcontext: " << subcontext_message.error(); |
| 283 | } |
| 284 | |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 285 | auto subcontext_reply = SubcontextReply{}; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 286 | if (!subcontext_reply.ParseFromString(*subcontext_message)) { |
| 287 | Restart(); |
| 288 | return Error() << "Unable to parse message from subcontext"; |
| 289 | } |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 290 | return subcontext_reply; |
| 291 | } |
| 292 | |
| 293 | Result<Success> Subcontext::Execute(const std::vector<std::string>& args) { |
| 294 | auto subcontext_command = SubcontextCommand(); |
| 295 | std::copy( |
| 296 | args.begin(), args.end(), |
| 297 | RepeatedPtrFieldBackInserter(subcontext_command.mutable_execute_command()->mutable_args())); |
| 298 | |
| 299 | auto subcontext_reply = TransmitMessage(subcontext_command); |
| 300 | if (!subcontext_reply) { |
| 301 | return subcontext_reply.error(); |
| 302 | } |
| 303 | |
Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 304 | for (const auto& property : subcontext_reply->properties_to_set()) { |
| 305 | ucred cr = {.pid = pid_, .uid = 0, .gid = 0}; |
Tom Cherry | 69d47aa | 2018-03-01 11:00:57 -0800 | [diff] [blame] | 306 | std::string error; |
| 307 | if (HandlePropertySet(property.name(), property.value(), context_, cr, &error) != 0) { |
| 308 | LOG(ERROR) << "Subcontext init could not set '" << property.name() << "' to '" |
| 309 | << property.value() << "': " << error; |
| 310 | } |
Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | if (subcontext_reply->reply_case() == SubcontextReply::kFailure) { |
| 314 | auto& failure = subcontext_reply->failure(); |
| 315 | return ResultError(failure.error_string(), failure.error_errno()); |
| 316 | } |
| 317 | |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 318 | if (subcontext_reply->reply_case() != SubcontextReply::kSuccess) { |
| 319 | return Error() << "Unexpected message type from subcontext: " |
| 320 | << subcontext_reply->reply_case(); |
| 321 | } |
| 322 | |
| 323 | return Success(); |
| 324 | } |
| 325 | |
| 326 | Result<std::vector<std::string>> Subcontext::ExpandArgs(const std::vector<std::string>& args) { |
| 327 | auto subcontext_command = SubcontextCommand{}; |
| 328 | std::copy(args.begin(), args.end(), |
| 329 | RepeatedPtrFieldBackInserter( |
| 330 | subcontext_command.mutable_expand_args_command()->mutable_args())); |
| 331 | |
| 332 | auto subcontext_reply = TransmitMessage(subcontext_command); |
| 333 | if (!subcontext_reply) { |
| 334 | return subcontext_reply.error(); |
| 335 | } |
| 336 | |
Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 337 | if (subcontext_reply->reply_case() == SubcontextReply::kFailure) { |
| 338 | auto& failure = subcontext_reply->failure(); |
| 339 | return ResultError(failure.error_string(), failure.error_errno()); |
| 340 | } |
| 341 | |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 342 | if (subcontext_reply->reply_case() != SubcontextReply::kExpandArgsReply) { |
| 343 | return Error() << "Unexpected message type from subcontext: " |
| 344 | << subcontext_reply->reply_case(); |
| 345 | } |
| 346 | |
| 347 | auto& reply = subcontext_reply->expand_args_reply(); |
| 348 | auto expanded_args = std::vector<std::string>{}; |
| 349 | for (const auto& string : reply.expanded_args()) { |
| 350 | expanded_args.emplace_back(string); |
| 351 | } |
| 352 | return expanded_args; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static std::vector<Subcontext> subcontexts; |
Luis Hector Chavez | 92c49bc | 2018-07-27 11:19:25 -0700 | [diff] [blame] | 356 | static bool shutting_down; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 357 | |
| 358 | std::vector<Subcontext>* InitializeSubcontexts() { |
Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame^] | 359 | if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) { |
Tom Cherry | a1dbeb8 | 2018-04-11 15:50:00 -0700 | [diff] [blame] | 360 | for (const auto& [path_prefix, secontext] : paths_and_secontexts) { |
| 361 | subcontexts.emplace_back(path_prefix, secontext); |
| 362 | } |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 363 | } |
| 364 | return &subcontexts; |
| 365 | } |
| 366 | |
| 367 | bool SubcontextChildReap(pid_t pid) { |
| 368 | for (auto& subcontext : subcontexts) { |
| 369 | if (subcontext.pid() == pid) { |
Luis Hector Chavez | 92c49bc | 2018-07-27 11:19:25 -0700 | [diff] [blame] | 370 | if (!shutting_down) { |
| 371 | subcontext.Restart(); |
| 372 | } |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 373 | return true; |
| 374 | } |
| 375 | } |
| 376 | return false; |
| 377 | } |
| 378 | |
Luis Hector Chavez | 92c49bc | 2018-07-27 11:19:25 -0700 | [diff] [blame] | 379 | void SubcontextTerminate() { |
| 380 | shutting_down = true; |
| 381 | for (auto& subcontext : subcontexts) { |
| 382 | kill(subcontext.pid(), SIGTERM); |
| 383 | } |
| 384 | } |
| 385 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 386 | } // namespace init |
| 387 | } // namespace android |