blob: f3dd53826988eb451b063e8e387feaa9c38a55b6 [file] [log] [blame]
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001/*
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>
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070021#include <unistd.h>
22
23#include <android-base/file.h>
24#include <android-base/logging.h>
Tom Cherry1ab3dfc2019-04-22 17:46:37 -070025#include <android-base/properties.h>
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070026#include <android-base/strings.h>
27#include <selinux/android.h>
28
29#include "action.h"
Tom Cherryd52a5b32019-07-22 16:05:36 -070030#include "builtins.h"
Tom Cherry1ab3dfc2019-04-22 17:46:37 -070031#include "proto_utils.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070032#include "util.h"
33
Tom Cherrya2f91362020-02-20 10:50:00 -080034#ifdef INIT_FULL_SOURCES
Tom Cherry40acb372018-08-01 13:41:12 -070035#include <android/api-level.h>
Tom Cherryde6bd502018-02-13 16:50:08 -080036#include "property_service.h"
Vic Yang92c236e2019-05-28 15:58:35 -070037#include "selabel.h"
Tom Cherryde6bd502018-02-13 16:50:08 -080038#include "selinux.h"
39#else
40#include "host_init_stubs.h"
41#endif
Tom Cherry32228482018-01-18 16:14:25 -080042
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070043using android::base::GetExecutablePath;
44using android::base::Join;
45using android::base::Socketpair;
46using android::base::Split;
47using android::base::StartsWith;
48using android::base::unique_fd;
49
50namespace android {
51namespace init {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070052namespace {
53
Tom Cherry18278d22019-11-12 16:21:20 -080054std::string shutdown_command;
Tom Cherrye3e77d32020-04-28 13:55:19 -070055static bool subcontext_terminated_by_shutdown;
56static std::unique_ptr<Subcontext> subcontext;
Tom Cherry18278d22019-11-12 16:21:20 -080057
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070058class SubcontextProcess {
59 public:
Tom Cherryd52a5b32019-07-22 16:05:36 -070060 SubcontextProcess(const BuiltinFunctionMap* function_map, std::string context, int init_fd)
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070061 : function_map_(function_map), context_(std::move(context)), init_fd_(init_fd){};
62 void MainLoop();
63
64 private:
65 void RunCommand(const SubcontextCommand::ExecuteCommand& execute_command,
Tom Cherryc49719f2018-01-10 11:04:34 -080066 SubcontextReply* reply) const;
67 void ExpandArgs(const SubcontextCommand::ExpandArgsCommand& expand_args_command,
68 SubcontextReply* reply) const;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070069
Tom Cherryd52a5b32019-07-22 16:05:36 -070070 const BuiltinFunctionMap* function_map_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070071 const std::string context_;
72 const int init_fd_;
73};
74
75void SubcontextProcess::RunCommand(const SubcontextCommand::ExecuteCommand& execute_command,
Tom Cherryc49719f2018-01-10 11:04:34 -080076 SubcontextReply* reply) const {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070077 // Need to use ArraySplice instead of this code.
78 auto args = std::vector<std::string>();
79 for (const auto& string : execute_command.args()) {
80 args.emplace_back(string);
81 }
82
Tom Cherryd52a5b32019-07-22 16:05:36 -070083 auto map_result = function_map_->Find(args);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070084 Result<void> result;
Bernie Innocenticecebbb2020-02-06 03:49:33 +090085 if (!map_result.ok()) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070086 result = Error() << "Cannot find command: " << map_result.error();
87 } else {
Tom Cherryd52a5b32019-07-22 16:05:36 -070088 result = RunBuiltinFunction(map_result->function, args, context_);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070089 }
90
Bernie Innocenticecebbb2020-02-06 03:49:33 +090091 if (result.ok()) {
Tom Cherryc49719f2018-01-10 11:04:34 -080092 reply->set_success(true);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070093 } else {
Tom Cherryc49719f2018-01-10 11:04:34 -080094 auto* failure = reply->mutable_failure();
Jiyong Park8fd64c82019-05-31 03:43:34 +090095 failure->set_error_string(result.error().message());
96 failure->set_error_errno(result.error().code());
Tom Cherryc49719f2018-01-10 11:04:34 -080097 }
98}
99
100void SubcontextProcess::ExpandArgs(const SubcontextCommand::ExpandArgsCommand& expand_args_command,
101 SubcontextReply* reply) const {
102 for (const auto& arg : expand_args_command.args()) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700103 auto expanded_arg = ExpandProps(arg);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900104 if (!expanded_arg.ok()) {
Tom Cherryc49719f2018-01-10 11:04:34 -0800105 auto* failure = reply->mutable_failure();
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700106 failure->set_error_string(expanded_arg.error().message());
Tom Cherryc49719f2018-01-10 11:04:34 -0800107 failure->set_error_errno(0);
108 return;
109 } else {
110 auto* expand_args_reply = reply->mutable_expand_args_reply();
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700111 expand_args_reply->add_expanded_args(*expanded_arg);
Tom Cherryc49719f2018-01-10 11:04:34 -0800112 }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700113 }
114}
115
116void SubcontextProcess::MainLoop() {
117 pollfd ufd[1];
118 ufd[0].events = POLLIN;
119 ufd[0].fd = init_fd_;
120
121 while (true) {
122 ufd[0].revents = 0;
123 int nr = TEMP_FAILURE_RETRY(poll(ufd, arraysize(ufd), -1));
124 if (nr == 0) continue;
125 if (nr < 0) {
126 PLOG(FATAL) << "poll() of subcontext socket failed, continuing";
127 }
128
129 auto init_message = ReadMessage(init_fd_);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900130 if (!init_message.ok()) {
Jiyong Park8fd64c82019-05-31 03:43:34 +0900131 if (init_message.error().code() == 0) {
Luis Hector Chavez72353592018-09-21 12:27:02 -0700132 // If the init file descriptor was closed, let's exit quietly. If
133 // this was accidental, init will restart us. If init died, this
134 // avoids calling abort(3) unnecessarily.
135 return;
136 }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700137 LOG(FATAL) << "Could not read message from init: " << init_message.error();
138 }
139
140 auto subcontext_command = SubcontextCommand();
141 if (!subcontext_command.ParseFromString(*init_message)) {
142 LOG(FATAL) << "Unable to parse message from init";
143 }
144
145 auto reply = SubcontextReply();
146 switch (subcontext_command.command_case()) {
147 case SubcontextCommand::kExecuteCommand: {
Tom Cherryc49719f2018-01-10 11:04:34 -0800148 RunCommand(subcontext_command.execute_command(), &reply);
149 break;
150 }
151 case SubcontextCommand::kExpandArgsCommand: {
152 ExpandArgs(subcontext_command.expand_args_command(), &reply);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700153 break;
154 }
155 default:
156 LOG(FATAL) << "Unknown message type from init: "
157 << subcontext_command.command_case();
158 }
159
Tom Cherry18278d22019-11-12 16:21:20 -0800160 if (!shutdown_command.empty()) {
161 reply.set_trigger_shutdown(shutdown_command);
162 shutdown_command.clear();
163 }
164
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900165 if (auto result = SendMessage(init_fd_, reply); !result.ok()) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700166 LOG(FATAL) << "Failed to send message to init: " << result.error();
167 }
168 }
169}
170
171} // namespace
172
Tom Cherryd52a5b32019-07-22 16:05:36 -0700173int SubcontextMain(int argc, char** argv, const BuiltinFunctionMap* function_map) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700174 if (argc < 4) LOG(FATAL) << "Fewer than 4 args specified to subcontext (" << argc << ")";
175
176 auto context = std::string(argv[2]);
177 auto init_fd = std::atoi(argv[3]);
178
Tom Cherry0d1452e2017-10-19 14:39:35 -0700179 SelabelInitialize();
Tom Cherry32228482018-01-18 16:14:25 -0800180
Tom Cherry18278d22019-11-12 16:21:20 -0800181 trigger_shutdown = [](const std::string& command) { shutdown_command = command; };
182
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700183 auto subcontext_process = SubcontextProcess(function_map, context, init_fd);
184 subcontext_process.MainLoop();
185 return 0;
186}
187
188void Subcontext::Fork() {
189 unique_fd subcontext_socket;
190 if (!Socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, &socket_, &subcontext_socket)) {
191 LOG(FATAL) << "Could not create socket pair to communicate to subcontext";
192 return;
193 }
194
195 auto result = fork();
196
197 if (result == -1) {
198 LOG(FATAL) << "Could not fork subcontext";
199 } else if (result == 0) {
200 socket_.reset();
201
202 // We explicitly do not use O_CLOEXEC here, such that we can reference this FD by number
203 // in the subcontext process after we exec.
Tom Cherry247ffbf2019-07-08 15:09:36 -0700204 int child_fd = dup(subcontext_socket); // NOLINT(android-cloexec-dup)
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700205 if (child_fd < 0) {
206 PLOG(FATAL) << "Could not dup child_fd";
207 }
208
Tom Cherry1c005f32019-11-20 15:51:36 -0800209 // We don't switch contexts if we're running the unit tests. We don't use std::optional,
210 // since we still need a real context string to pass to the builtin functions.
211 if (context_ != kTestContext) {
212 if (setexeccon(context_.c_str()) < 0) {
213 PLOG(FATAL) << "Could not set execcon for '" << context_ << "'";
214 }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700215 }
216
217 auto init_path = GetExecutablePath();
218 auto child_fd_string = std::to_string(child_fd);
219 const char* args[] = {init_path.c_str(), "subcontext", context_.c_str(),
220 child_fd_string.c_str(), nullptr};
221 execv(init_path.data(), const_cast<char**>(args));
222
223 PLOG(FATAL) << "Could not execv subcontext init";
224 } else {
225 subcontext_socket.reset();
226 pid_ = result;
227 LOG(INFO) << "Forked subcontext for '" << context_ << "' with pid " << pid_;
228 }
229}
230
231void Subcontext::Restart() {
232 LOG(ERROR) << "Restarting subcontext '" << context_ << "'";
233 if (pid_) {
234 kill(pid_, SIGKILL);
235 }
236 pid_ = 0;
237 socket_.reset();
238 Fork();
239}
240
Tom Cherry14c24722019-09-18 13:47:19 -0700241bool Subcontext::PathMatchesSubcontext(const std::string& path) {
242 for (const auto& prefix : path_prefixes_) {
243 if (StartsWith(path, prefix)) {
244 return true;
245 }
246 }
247 return false;
248}
249
Tom Cherryc49719f2018-01-10 11:04:34 -0800250Result<SubcontextReply> Subcontext::TransmitMessage(const SubcontextCommand& subcontext_command) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900251 if (auto result = SendMessage(socket_, subcontext_command); !result.ok()) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700252 Restart();
253 return ErrnoError() << "Failed to send message to subcontext";
254 }
255
256 auto subcontext_message = ReadMessage(socket_);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900257 if (!subcontext_message.ok()) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700258 Restart();
259 return Error() << "Failed to receive result from subcontext: " << subcontext_message.error();
260 }
261
Tom Cherryc49719f2018-01-10 11:04:34 -0800262 auto subcontext_reply = SubcontextReply{};
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700263 if (!subcontext_reply.ParseFromString(*subcontext_message)) {
264 Restart();
265 return Error() << "Unable to parse message from subcontext";
266 }
Tom Cherry18278d22019-11-12 16:21:20 -0800267
268 if (subcontext_reply.has_trigger_shutdown()) {
269 trigger_shutdown(subcontext_reply.trigger_shutdown());
270 }
271
Tom Cherryc49719f2018-01-10 11:04:34 -0800272 return subcontext_reply;
273}
274
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700275Result<void> Subcontext::Execute(const std::vector<std::string>& args) {
Tom Cherryc49719f2018-01-10 11:04:34 -0800276 auto subcontext_command = SubcontextCommand();
277 std::copy(
278 args.begin(), args.end(),
279 RepeatedPtrFieldBackInserter(subcontext_command.mutable_execute_command()->mutable_args()));
280
281 auto subcontext_reply = TransmitMessage(subcontext_command);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900282 if (!subcontext_reply.ok()) {
Tom Cherryc49719f2018-01-10 11:04:34 -0800283 return subcontext_reply.error();
284 }
285
Tom Cherry32228482018-01-18 16:14:25 -0800286 if (subcontext_reply->reply_case() == SubcontextReply::kFailure) {
287 auto& failure = subcontext_reply->failure();
288 return ResultError(failure.error_string(), failure.error_errno());
289 }
290
Tom Cherryc49719f2018-01-10 11:04:34 -0800291 if (subcontext_reply->reply_case() != SubcontextReply::kSuccess) {
292 return Error() << "Unexpected message type from subcontext: "
293 << subcontext_reply->reply_case();
294 }
295
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700296 return {};
Tom Cherryc49719f2018-01-10 11:04:34 -0800297}
298
299Result<std::vector<std::string>> Subcontext::ExpandArgs(const std::vector<std::string>& args) {
300 auto subcontext_command = SubcontextCommand{};
301 std::copy(args.begin(), args.end(),
302 RepeatedPtrFieldBackInserter(
303 subcontext_command.mutable_expand_args_command()->mutable_args()));
304
305 auto subcontext_reply = TransmitMessage(subcontext_command);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900306 if (!subcontext_reply.ok()) {
Tom Cherryc49719f2018-01-10 11:04:34 -0800307 return subcontext_reply.error();
308 }
309
Tom Cherry32228482018-01-18 16:14:25 -0800310 if (subcontext_reply->reply_case() == SubcontextReply::kFailure) {
311 auto& failure = subcontext_reply->failure();
312 return ResultError(failure.error_string(), failure.error_errno());
313 }
314
Tom Cherryc49719f2018-01-10 11:04:34 -0800315 if (subcontext_reply->reply_case() != SubcontextReply::kExpandArgsReply) {
316 return Error() << "Unexpected message type from subcontext: "
317 << subcontext_reply->reply_case();
318 }
319
320 auto& reply = subcontext_reply->expand_args_reply();
321 auto expanded_args = std::vector<std::string>{};
322 for (const auto& string : reply.expanded_args()) {
323 expanded_args.emplace_back(string);
324 }
325 return expanded_args;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700326}
327
Tom Cherrye3e77d32020-04-28 13:55:19 -0700328void InitializeSubcontext() {
Tom Cherry40acb372018-08-01 13:41:12 -0700329 if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) {
Tom Cherrye3e77d32020-04-28 13:55:19 -0700330 subcontext.reset(
331 new Subcontext(std::vector<std::string>{"/vendor", "/odm"}, kVendorContext));
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700332 }
Tom Cherrye3e77d32020-04-28 13:55:19 -0700333}
334
335Subcontext* GetSubcontext() {
336 return subcontext.get();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700337}
338
339bool SubcontextChildReap(pid_t pid) {
Tom Cherrye3e77d32020-04-28 13:55:19 -0700340 if (subcontext->pid() == pid) {
341 if (!subcontext_terminated_by_shutdown) {
342 subcontext->Restart();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700343 }
Tom Cherrye3e77d32020-04-28 13:55:19 -0700344 return true;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700345 }
346 return false;
347}
348
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -0700349void SubcontextTerminate() {
Tom Cherrye3e77d32020-04-28 13:55:19 -0700350 subcontext_terminated_by_shutdown = true;
351 kill(subcontext->pid(), SIGTERM);
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -0700352}
353
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700354} // namespace init
355} // namespace android