blob: 02ed507a757c8a7df7aa5ffbd8fc9e2b4cf01d45 [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>
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 Cherrycb0f9bb2017-09-12 15:58:47 -070030#include "util.h"
31
Tom Cherryde6bd502018-02-13 16:50:08 -080032#if defined(__ANDROID__)
Tom Cherry40acb372018-08-01 13:41:12 -070033#include <android/api-level.h>
Tom Cherryde6bd502018-02-13 16:50:08 -080034#include "property_service.h"
Vic Yang92c236e2019-05-28 15:58:35 -070035#include "selabel.h"
Tom Cherryde6bd502018-02-13 16:50:08 -080036#include "selinux.h"
37#else
38#include "host_init_stubs.h"
39#endif
Tom Cherry32228482018-01-18 16:14:25 -080040
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070041using android::base::GetExecutablePath;
42using android::base::Join;
43using android::base::Socketpair;
44using android::base::Split;
45using android::base::StartsWith;
46using android::base::unique_fd;
47
48namespace android {
49namespace init {
50
Tom Cherryac7428b2017-10-02 16:59:02 -070051const std::string kInitContext = "u:r:init:s0";
52const std::string kVendorContext = "u:r:vendor_init:s0";
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070053
Tom Cherrydc375862018-02-28 10:39:01 -080054const char* const paths_and_secontexts[2][2] = {
55 {"/vendor", kVendorContext.c_str()},
56 {"/odm", kVendorContext.c_str()},
57};
58
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070059namespace {
60
61constexpr size_t kBufferSize = 4096;
62
63Result<std::string> ReadMessage(int socket) {
64 char buffer[kBufferSize] = {};
65 auto result = TEMP_FAILURE_RETRY(recv(socket, buffer, sizeof(buffer), 0));
Luis Hector Chavez72353592018-09-21 12:27:02 -070066 if (result == 0) {
67 return Error();
68 } else if (result < 0) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070069 return ErrnoError();
70 }
71 return std::string(buffer, result);
72}
73
74template <typename T>
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070075Result<void> SendMessage(int socket, const T& message) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070076 std::string message_string;
77 if (!message.SerializeToString(&message_string)) {
78 return Error() << "Unable to serialize message";
79 }
80
81 if (message_string.size() > kBufferSize) {
82 return Error() << "Serialized message too long to send";
83 }
84
85 if (auto result =
86 TEMP_FAILURE_RETRY(send(socket, message_string.c_str(), message_string.size(), 0));
87 result != static_cast<long>(message_string.size())) {
88 return ErrnoError() << "send() failed to send message contents";
89 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070090 return {};
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070091}
92
Tom Cherry32228482018-01-18 16:14:25 -080093std::vector<std::pair<std::string, std::string>> properties_to_set;
94
95uint32_t SubcontextPropertySet(const std::string& name, const std::string& value) {
96 properties_to_set.emplace_back(name, value);
Tom Cherryde6bd502018-02-13 16:50:08 -080097 return 0;
Tom Cherry32228482018-01-18 16:14:25 -080098}
99
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700100class SubcontextProcess {
101 public:
102 SubcontextProcess(const KeywordFunctionMap* function_map, std::string context, int init_fd)
103 : function_map_(function_map), context_(std::move(context)), init_fd_(init_fd){};
104 void MainLoop();
105
106 private:
107 void RunCommand(const SubcontextCommand::ExecuteCommand& execute_command,
Tom Cherryc49719f2018-01-10 11:04:34 -0800108 SubcontextReply* reply) const;
109 void ExpandArgs(const SubcontextCommand::ExpandArgsCommand& expand_args_command,
110 SubcontextReply* reply) const;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700111
112 const KeywordFunctionMap* function_map_;
113 const std::string context_;
114 const int init_fd_;
115};
116
117void SubcontextProcess::RunCommand(const SubcontextCommand::ExecuteCommand& execute_command,
Tom Cherryc49719f2018-01-10 11:04:34 -0800118 SubcontextReply* reply) const {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700119 // Need to use ArraySplice instead of this code.
120 auto args = std::vector<std::string>();
121 for (const auto& string : execute_command.args()) {
122 args.emplace_back(string);
123 }
124
125 auto map_result = function_map_->FindFunction(args);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700126 Result<void> result;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700127 if (!map_result) {
128 result = Error() << "Cannot find command: " << map_result.error();
129 } else {
130 result = RunBuiltinFunction(map_result->second, args, context_);
131 }
132
Tom Cherry32228482018-01-18 16:14:25 -0800133 for (const auto& [name, value] : properties_to_set) {
134 auto property = reply->add_properties_to_set();
135 property->set_name(name);
136 property->set_value(value);
137 }
138
139 properties_to_set.clear();
140
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700141 if (result) {
Tom Cherryc49719f2018-01-10 11:04:34 -0800142 reply->set_success(true);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700143 } else {
Tom Cherryc49719f2018-01-10 11:04:34 -0800144 auto* failure = reply->mutable_failure();
Jiyong Park8fd64c82019-05-31 03:43:34 +0900145 failure->set_error_string(result.error().message());
146 failure->set_error_errno(result.error().code());
Tom Cherryc49719f2018-01-10 11:04:34 -0800147 }
148}
149
150void SubcontextProcess::ExpandArgs(const SubcontextCommand::ExpandArgsCommand& expand_args_command,
151 SubcontextReply* reply) const {
152 for (const auto& arg : expand_args_command.args()) {
153 auto expanded_prop = std::string{};
154 if (!expand_props(arg, &expanded_prop)) {
155 auto* failure = reply->mutable_failure();
156 failure->set_error_string("Failed to expand '" + arg + "'");
157 failure->set_error_errno(0);
158 return;
159 } else {
160 auto* expand_args_reply = reply->mutable_expand_args_reply();
161 expand_args_reply->add_expanded_args(expanded_prop);
162 }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700163 }
164}
165
166void SubcontextProcess::MainLoop() {
167 pollfd ufd[1];
168 ufd[0].events = POLLIN;
169 ufd[0].fd = init_fd_;
170
171 while (true) {
172 ufd[0].revents = 0;
173 int nr = TEMP_FAILURE_RETRY(poll(ufd, arraysize(ufd), -1));
174 if (nr == 0) continue;
175 if (nr < 0) {
176 PLOG(FATAL) << "poll() of subcontext socket failed, continuing";
177 }
178
179 auto init_message = ReadMessage(init_fd_);
180 if (!init_message) {
Jiyong Park8fd64c82019-05-31 03:43:34 +0900181 if (init_message.error().code() == 0) {
Luis Hector Chavez72353592018-09-21 12:27:02 -0700182 // If the init file descriptor was closed, let's exit quietly. If
183 // this was accidental, init will restart us. If init died, this
184 // avoids calling abort(3) unnecessarily.
185 return;
186 }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700187 LOG(FATAL) << "Could not read message from init: " << init_message.error();
188 }
189
190 auto subcontext_command = SubcontextCommand();
191 if (!subcontext_command.ParseFromString(*init_message)) {
192 LOG(FATAL) << "Unable to parse message from init";
193 }
194
195 auto reply = SubcontextReply();
196 switch (subcontext_command.command_case()) {
197 case SubcontextCommand::kExecuteCommand: {
Tom Cherryc49719f2018-01-10 11:04:34 -0800198 RunCommand(subcontext_command.execute_command(), &reply);
199 break;
200 }
201 case SubcontextCommand::kExpandArgsCommand: {
202 ExpandArgs(subcontext_command.expand_args_command(), &reply);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700203 break;
204 }
205 default:
206 LOG(FATAL) << "Unknown message type from init: "
207 << subcontext_command.command_case();
208 }
209
210 if (auto result = SendMessage(init_fd_, reply); !result) {
211 LOG(FATAL) << "Failed to send message to init: " << result.error();
212 }
213 }
214}
215
216} // namespace
217
218int SubcontextMain(int argc, char** argv, const KeywordFunctionMap* function_map) {
219 if (argc < 4) LOG(FATAL) << "Fewer than 4 args specified to subcontext (" << argc << ")";
220
221 auto context = std::string(argv[2]);
222 auto init_fd = std::atoi(argv[3]);
223
Tom Cherry0d1452e2017-10-19 14:39:35 -0700224 SelabelInitialize();
Tom Cherry32228482018-01-18 16:14:25 -0800225
226 property_set = SubcontextPropertySet;
227
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700228 auto subcontext_process = SubcontextProcess(function_map, context, init_fd);
229 subcontext_process.MainLoop();
230 return 0;
231}
232
233void Subcontext::Fork() {
234 unique_fd subcontext_socket;
235 if (!Socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, &socket_, &subcontext_socket)) {
236 LOG(FATAL) << "Could not create socket pair to communicate to subcontext";
237 return;
238 }
239
240 auto result = fork();
241
242 if (result == -1) {
243 LOG(FATAL) << "Could not fork subcontext";
244 } else if (result == 0) {
245 socket_.reset();
246
247 // We explicitly do not use O_CLOEXEC here, such that we can reference this FD by number
248 // in the subcontext process after we exec.
249 int child_fd = dup(subcontext_socket);
250 if (child_fd < 0) {
251 PLOG(FATAL) << "Could not dup child_fd";
252 }
253
254 if (setexeccon(context_.c_str()) < 0) {
255 PLOG(FATAL) << "Could not set execcon for '" << context_ << "'";
256 }
257
258 auto init_path = GetExecutablePath();
259 auto child_fd_string = std::to_string(child_fd);
260 const char* args[] = {init_path.c_str(), "subcontext", context_.c_str(),
261 child_fd_string.c_str(), nullptr};
262 execv(init_path.data(), const_cast<char**>(args));
263
264 PLOG(FATAL) << "Could not execv subcontext init";
265 } else {
266 subcontext_socket.reset();
267 pid_ = result;
268 LOG(INFO) << "Forked subcontext for '" << context_ << "' with pid " << pid_;
269 }
270}
271
272void Subcontext::Restart() {
273 LOG(ERROR) << "Restarting subcontext '" << context_ << "'";
274 if (pid_) {
275 kill(pid_, SIGKILL);
276 }
277 pid_ = 0;
278 socket_.reset();
279 Fork();
280}
281
Tom Cherryc49719f2018-01-10 11:04:34 -0800282Result<SubcontextReply> Subcontext::TransmitMessage(const SubcontextCommand& subcontext_command) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700283 if (auto result = SendMessage(socket_, subcontext_command); !result) {
284 Restart();
285 return ErrnoError() << "Failed to send message to subcontext";
286 }
287
288 auto subcontext_message = ReadMessage(socket_);
289 if (!subcontext_message) {
290 Restart();
291 return Error() << "Failed to receive result from subcontext: " << subcontext_message.error();
292 }
293
Tom Cherryc49719f2018-01-10 11:04:34 -0800294 auto subcontext_reply = SubcontextReply{};
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700295 if (!subcontext_reply.ParseFromString(*subcontext_message)) {
296 Restart();
297 return Error() << "Unable to parse message from subcontext";
298 }
Tom Cherryc49719f2018-01-10 11:04:34 -0800299 return subcontext_reply;
300}
301
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700302Result<void> Subcontext::Execute(const std::vector<std::string>& args) {
Tom Cherryc49719f2018-01-10 11:04:34 -0800303 auto subcontext_command = SubcontextCommand();
304 std::copy(
305 args.begin(), args.end(),
306 RepeatedPtrFieldBackInserter(subcontext_command.mutable_execute_command()->mutable_args()));
307
308 auto subcontext_reply = TransmitMessage(subcontext_command);
309 if (!subcontext_reply) {
310 return subcontext_reply.error();
311 }
312
Tom Cherry32228482018-01-18 16:14:25 -0800313 for (const auto& property : subcontext_reply->properties_to_set()) {
314 ucred cr = {.pid = pid_, .uid = 0, .gid = 0};
Tom Cherry69d47aa2018-03-01 11:00:57 -0800315 std::string error;
316 if (HandlePropertySet(property.name(), property.value(), context_, cr, &error) != 0) {
317 LOG(ERROR) << "Subcontext init could not set '" << property.name() << "' to '"
318 << property.value() << "': " << error;
319 }
Tom Cherry32228482018-01-18 16:14:25 -0800320 }
321
322 if (subcontext_reply->reply_case() == SubcontextReply::kFailure) {
323 auto& failure = subcontext_reply->failure();
324 return ResultError(failure.error_string(), failure.error_errno());
325 }
326
Tom Cherryc49719f2018-01-10 11:04:34 -0800327 if (subcontext_reply->reply_case() != SubcontextReply::kSuccess) {
328 return Error() << "Unexpected message type from subcontext: "
329 << subcontext_reply->reply_case();
330 }
331
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700332 return {};
Tom Cherryc49719f2018-01-10 11:04:34 -0800333}
334
335Result<std::vector<std::string>> Subcontext::ExpandArgs(const std::vector<std::string>& args) {
336 auto subcontext_command = SubcontextCommand{};
337 std::copy(args.begin(), args.end(),
338 RepeatedPtrFieldBackInserter(
339 subcontext_command.mutable_expand_args_command()->mutable_args()));
340
341 auto subcontext_reply = TransmitMessage(subcontext_command);
342 if (!subcontext_reply) {
343 return subcontext_reply.error();
344 }
345
Tom Cherry32228482018-01-18 16:14:25 -0800346 if (subcontext_reply->reply_case() == SubcontextReply::kFailure) {
347 auto& failure = subcontext_reply->failure();
348 return ResultError(failure.error_string(), failure.error_errno());
349 }
350
Tom Cherryc49719f2018-01-10 11:04:34 -0800351 if (subcontext_reply->reply_case() != SubcontextReply::kExpandArgsReply) {
352 return Error() << "Unexpected message type from subcontext: "
353 << subcontext_reply->reply_case();
354 }
355
356 auto& reply = subcontext_reply->expand_args_reply();
357 auto expanded_args = std::vector<std::string>{};
358 for (const auto& string : reply.expanded_args()) {
359 expanded_args.emplace_back(string);
360 }
361 return expanded_args;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700362}
363
364static std::vector<Subcontext> subcontexts;
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -0700365static bool shutting_down;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700366
367std::vector<Subcontext>* InitializeSubcontexts() {
Tom Cherry40acb372018-08-01 13:41:12 -0700368 if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) {
Tom Cherrya1dbeb82018-04-11 15:50:00 -0700369 for (const auto& [path_prefix, secontext] : paths_and_secontexts) {
370 subcontexts.emplace_back(path_prefix, secontext);
371 }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700372 }
373 return &subcontexts;
374}
375
376bool SubcontextChildReap(pid_t pid) {
377 for (auto& subcontext : subcontexts) {
378 if (subcontext.pid() == pid) {
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -0700379 if (!shutting_down) {
380 subcontext.Restart();
381 }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700382 return true;
383 }
384 }
385 return false;
386}
387
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -0700388void SubcontextTerminate() {
389 shutting_down = true;
390 for (auto& subcontext : subcontexts) {
391 kill(subcontext.pid(), SIGTERM);
392 }
393}
394
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700395} // namespace init
396} // namespace android