blob: c2a21d45cc4f08230b821c5aa2d6c1294b88ba12 [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"
35#include "selinux.h"
36#else
37#include "host_init_stubs.h"
38#endif
Tom Cherry32228482018-01-18 16:14:25 -080039
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070040using android::base::GetExecutablePath;
41using android::base::Join;
42using android::base::Socketpair;
43using android::base::Split;
44using android::base::StartsWith;
45using android::base::unique_fd;
46
47namespace android {
48namespace init {
49
Tom Cherryac7428b2017-10-02 16:59:02 -070050const std::string kInitContext = "u:r:init:s0";
51const std::string kVendorContext = "u:r:vendor_init:s0";
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070052
Tom Cherrydc375862018-02-28 10:39:01 -080053const char* const paths_and_secontexts[2][2] = {
54 {"/vendor", kVendorContext.c_str()},
55 {"/odm", kVendorContext.c_str()},
56};
57
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070058namespace {
59
60constexpr size_t kBufferSize = 4096;
61
62Result<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
71template <typename T>
72Result<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 Cherry32228482018-01-18 16:14:25 -080090std::vector<std::pair<std::string, std::string>> properties_to_set;
91
92uint32_t SubcontextPropertySet(const std::string& name, const std::string& value) {
93 properties_to_set.emplace_back(name, value);
Tom Cherryde6bd502018-02-13 16:50:08 -080094 return 0;
Tom Cherry32228482018-01-18 16:14:25 -080095}
96
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070097class 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 Cherryc49719f2018-01-10 11:04:34 -0800105 SubcontextReply* reply) const;
106 void ExpandArgs(const SubcontextCommand::ExpandArgsCommand& expand_args_command,
107 SubcontextReply* reply) const;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700108
109 const KeywordFunctionMap* function_map_;
110 const std::string context_;
111 const int init_fd_;
112};
113
114void SubcontextProcess::RunCommand(const SubcontextCommand::ExecuteCommand& execute_command,
Tom Cherryc49719f2018-01-10 11:04:34 -0800115 SubcontextReply* reply) const {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700116 // 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 Cherry32228482018-01-18 16:14:25 -0800130 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 Cherrycb0f9bb2017-09-12 15:58:47 -0700138 if (result) {
Tom Cherryc49719f2018-01-10 11:04:34 -0800139 reply->set_success(true);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700140 } else {
Tom Cherryc49719f2018-01-10 11:04:34 -0800141 auto* failure = reply->mutable_failure();
142 failure->set_error_string(result.error_string());
143 failure->set_error_errno(result.error_errno());
144 }
145}
146
147void 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 Cherrycb0f9bb2017-09-12 15:58:47 -0700160 }
161}
162
163void 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 Cherryc49719f2018-01-10 11:04:34 -0800189 RunCommand(subcontext_command.execute_command(), &reply);
190 break;
191 }
192 case SubcontextCommand::kExpandArgsCommand: {
193 ExpandArgs(subcontext_command.expand_args_command(), &reply);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700194 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
209int 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 Cherry0d1452e2017-10-19 14:39:35 -0700215 SelabelInitialize();
Tom Cherry32228482018-01-18 16:14:25 -0800216
217 property_set = SubcontextPropertySet;
218
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700219 auto subcontext_process = SubcontextProcess(function_map, context, init_fd);
220 subcontext_process.MainLoop();
221 return 0;
222}
223
224void 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
263void 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 Cherryc49719f2018-01-10 11:04:34 -0800273Result<SubcontextReply> Subcontext::TransmitMessage(const SubcontextCommand& subcontext_command) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700274 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 Cherryc49719f2018-01-10 11:04:34 -0800285 auto subcontext_reply = SubcontextReply{};
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700286 if (!subcontext_reply.ParseFromString(*subcontext_message)) {
287 Restart();
288 return Error() << "Unable to parse message from subcontext";
289 }
Tom Cherryc49719f2018-01-10 11:04:34 -0800290 return subcontext_reply;
291}
292
293Result<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 Cherry32228482018-01-18 16:14:25 -0800304 for (const auto& property : subcontext_reply->properties_to_set()) {
305 ucred cr = {.pid = pid_, .uid = 0, .gid = 0};
Tom Cherry69d47aa2018-03-01 11:00:57 -0800306 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 Cherry32228482018-01-18 16:14:25 -0800311 }
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 Cherryc49719f2018-01-10 11:04:34 -0800318 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
326Result<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 Cherry32228482018-01-18 16:14:25 -0800337 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 Cherryc49719f2018-01-10 11:04:34 -0800342 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 Cherrycb0f9bb2017-09-12 15:58:47 -0700353}
354
355static std::vector<Subcontext> subcontexts;
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -0700356static bool shutting_down;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700357
358std::vector<Subcontext>* InitializeSubcontexts() {
Tom Cherry40acb372018-08-01 13:41:12 -0700359 if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) {
Tom Cherrya1dbeb82018-04-11 15:50:00 -0700360 for (const auto& [path_prefix, secontext] : paths_and_secontexts) {
361 subcontexts.emplace_back(path_prefix, secontext);
362 }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700363 }
364 return &subcontexts;
365}
366
367bool SubcontextChildReap(pid_t pid) {
368 for (auto& subcontext : subcontexts) {
369 if (subcontext.pid() == pid) {
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -0700370 if (!shutting_down) {
371 subcontext.Restart();
372 }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700373 return true;
374 }
375 }
376 return false;
377}
378
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -0700379void SubcontextTerminate() {
380 shutting_down = true;
381 for (auto& subcontext : subcontexts) {
382 kill(subcontext.pid(), SIGTERM);
383 }
384}
385
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700386} // namespace init
387} // namespace android