blob: 3a783434a5e35e7bcd777b04e2be3b2f78af16b6 [file] [log] [blame]
David Anderson491e4da2020-12-08 00:21:20 -08001/*
2 * Copyright (C) 2020 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 "snapuserd_transition.h"
18
19#include <sys/mman.h>
20#include <sys/socket.h>
21#include <sys/syscall.h>
22#include <sys/xattr.h>
23#include <unistd.h>
24
25#include <filesystem>
26#include <string>
David Anderson9fd88622021-03-05 14:10:55 -080027#include <string_view>
Kelvin Zhangcb3cfc12023-12-11 11:14:48 -080028#include <thread>
David Anderson491e4da2020-12-08 00:21:20 -080029
30#include <android-base/file.h>
31#include <android-base/logging.h>
32#include <android-base/parseint.h>
Akilesh Kailash4ffe8a32022-06-02 08:00:39 +000033#include <android-base/stringprintf.h>
David Anderson491e4da2020-12-08 00:21:20 -080034#include <android-base/strings.h>
35#include <android-base/unique_fd.h>
36#include <cutils/sockets.h>
Akilesh Kailashfd5562b2022-01-25 07:05:31 +000037#include <fs_avb/fs_avb.h>
David Anderson491e4da2020-12-08 00:21:20 -080038#include <libsnapshot/snapshot.h>
David Anderson491e4da2020-12-08 00:21:20 -080039#include <private/android_filesystem_config.h>
David Anderson9fd88622021-03-05 14:10:55 -080040#include <procinfo/process_map.h>
David Anderson491e4da2020-12-08 00:21:20 -080041#include <selinux/android.h>
Akilesh Kailash36aeeb32021-07-26 06:59:18 +000042#include <snapuserd/snapuserd_client.h>
David Anderson491e4da2020-12-08 00:21:20 -080043
44#include "block_dev_initializer.h"
Akilesh Kailash4ffe8a32022-06-02 08:00:39 +000045#include "lmkd_service.h"
David Anderson491e4da2020-12-08 00:21:20 -080046#include "service_utils.h"
47#include "util.h"
48
49namespace android {
50namespace init {
51
52using namespace std::string_literals;
53
54using android::base::unique_fd;
55using android::snapshot::SnapshotManager;
56using android::snapshot::SnapuserdClient;
57
58static constexpr char kSnapuserdPath[] = "/system/bin/snapuserd";
59static constexpr char kSnapuserdFirstStagePidVar[] = "FIRST_STAGE_SNAPUSERD_PID";
60static constexpr char kSnapuserdFirstStageFdVar[] = "FIRST_STAGE_SNAPUSERD_FD";
David Anderson0e5ad5a2021-07-21 21:53:28 -070061static constexpr char kSnapuserdFirstStageInfoVar[] = "FIRST_STAGE_SNAPUSERD_INFO";
David Anderson491e4da2020-12-08 00:21:20 -080062static constexpr char kSnapuserdLabel[] = "u:object_r:snapuserd_exec:s0";
63static constexpr char kSnapuserdSocketLabel[] = "u:object_r:snapuserd_socket:s0";
64
Akilesh Kailash3b874452021-10-03 09:41:13 +000065void LaunchFirstStageSnapuserd(SnapshotDriver driver) {
David Anderson491e4da2020-12-08 00:21:20 -080066 SocketDescriptor socket_desc;
67 socket_desc.name = android::snapshot::kSnapuserdSocket;
68 socket_desc.type = SOCK_STREAM;
69 socket_desc.perm = 0660;
70 socket_desc.uid = AID_SYSTEM;
71 socket_desc.gid = AID_SYSTEM;
72
73 // We specify a label here even though it technically is not needed. During
74 // first_stage_mount there is no sepolicy loaded. Once sepolicy is loaded,
75 // we bypass the socket entirely.
76 auto socket = socket_desc.Create(kSnapuserdSocketLabel);
77 if (!socket.ok()) {
78 LOG(FATAL) << "Could not create snapuserd socket: " << socket.error();
79 }
80
81 pid_t pid = fork();
82 if (pid < 0) {
83 PLOG(FATAL) << "Cannot launch snapuserd; fork failed";
84 }
85 if (pid == 0) {
86 socket->Publish();
Akilesh Kailash3b874452021-10-03 09:41:13 +000087
88 if (driver == SnapshotDriver::DM_USER) {
89 char arg0[] = "/system/bin/snapuserd";
90 char arg1[] = "-user_snapshot";
91 char* const argv[] = {arg0, arg1, nullptr};
92 if (execv(arg0, argv) < 0) {
93 PLOG(FATAL) << "Cannot launch snapuserd; execv failed";
94 }
95 _exit(127);
96 } else {
97 char arg0[] = "/system/bin/snapuserd";
98 char* const argv[] = {arg0, nullptr};
99 if (execv(arg0, argv) < 0) {
100 PLOG(FATAL) << "Cannot launch snapuserd; execv failed";
101 }
102 _exit(127);
David Anderson491e4da2020-12-08 00:21:20 -0800103 }
David Anderson491e4da2020-12-08 00:21:20 -0800104 }
105
David Anderson0e5ad5a2021-07-21 21:53:28 -0700106 auto client = SnapuserdClient::Connect(android::snapshot::kSnapuserdSocket, 10s);
107 if (!client) {
108 LOG(FATAL) << "Could not connect to first-stage snapuserd";
109 }
110 if (client->SupportsSecondStageSocketHandoff()) {
111 setenv(kSnapuserdFirstStageInfoVar, "socket", 1);
112 }
113
David Anderson491e4da2020-12-08 00:21:20 -0800114 setenv(kSnapuserdFirstStagePidVar, std::to_string(pid).c_str(), 1);
115
Akilesh Kailash5140f3a2023-01-02 21:11:13 -0800116 if (!client->RemoveTransitionedDaemonIndicator()) {
117 LOG(ERROR) << "RemoveTransitionedDaemonIndicator failed";
118 }
119
David Anderson491e4da2020-12-08 00:21:20 -0800120 LOG(INFO) << "Relaunched snapuserd with pid: " << pid;
121}
122
123std::optional<pid_t> GetSnapuserdFirstStagePid() {
124 const char* pid_str = getenv(kSnapuserdFirstStagePidVar);
125 if (!pid_str) {
126 return {};
127 }
128
129 int pid = 0;
130 if (!android::base::ParseInt(pid_str, &pid)) {
131 LOG(FATAL) << "Could not parse pid in environment, " << kSnapuserdFirstStagePidVar << "="
132 << pid_str;
133 }
134 return {pid};
135}
136
137static void RelabelLink(const std::string& link) {
138 selinux_android_restorecon(link.c_str(), 0);
139
140 std::string path;
141 if (android::base::Readlink(link, &path)) {
142 selinux_android_restorecon(path.c_str(), 0);
143 }
144}
145
146static void RelabelDeviceMapper() {
147 selinux_android_restorecon("/dev/device-mapper", 0);
148
149 std::error_code ec;
150 for (auto& iter : std::filesystem::directory_iterator("/dev/block", ec)) {
151 const auto& path = iter.path();
152 if (android::base::StartsWith(path.string(), "/dev/block/dm-")) {
153 selinux_android_restorecon(path.string().c_str(), 0);
154 }
155 }
156}
157
158static std::optional<int> GetRamdiskSnapuserdFd() {
159 const char* fd_str = getenv(kSnapuserdFirstStageFdVar);
160 if (!fd_str) {
161 return {};
162 }
163
164 int fd;
165 if (!android::base::ParseInt(fd_str, &fd)) {
166 LOG(FATAL) << "Could not parse fd in environment, " << kSnapuserdFirstStageFdVar << "="
167 << fd_str;
168 }
169 return {fd};
170}
171
172void RestoreconRamdiskSnapuserd(int fd) {
173 if (fsetxattr(fd, XATTR_NAME_SELINUX, kSnapuserdLabel, strlen(kSnapuserdLabel) + 1, 0) < 0) {
174 PLOG(FATAL) << "fsetxattr snapuserd failed";
175 }
176}
177
178SnapuserdSelinuxHelper::SnapuserdSelinuxHelper(std::unique_ptr<SnapshotManager>&& sm, pid_t old_pid)
179 : sm_(std::move(sm)), old_pid_(old_pid) {
180 // Only dm-user device names change during transitions, so the other
181 // devices are expected to be present.
182 sm_->SetUeventRegenCallback([this](const std::string& device) -> bool {
183 if (android::base::StartsWith(device, "/dev/dm-user/")) {
184 return block_dev_init_.InitDmUser(android::base::Basename(device));
185 }
186 return true;
187 });
188}
189
David Anderson9fd88622021-03-05 14:10:55 -0800190static void LockAllSystemPages() {
191 bool ok = true;
192 auto callback = [&](const android::procinfo::MapInfo& map) -> void {
193 if (!ok || android::base::StartsWith(map.name, "/dev/") ||
194 !android::base::StartsWith(map.name, "/")) {
195 return;
196 }
197 auto start = reinterpret_cast<const void*>(map.start);
198 auto len = map.end - map.start;
199 if (!len) {
200 return;
201 }
202 if (mlock(start, len) < 0) {
203 LOG(ERROR) << "mlock failed, " << start << " for " << len << " bytes.";
204 ok = false;
205 }
206 };
207
208 if (!android::procinfo::ReadProcessMaps(getpid(), callback) || !ok) {
209 LOG(FATAL) << "Could not process /proc/" << getpid() << "/maps file for init, "
210 << "falling back to mlockall().";
211 if (mlockall(MCL_CURRENT) < 0) {
212 LOG(FATAL) << "mlockall failed";
213 }
214 }
215}
216
David Anderson491e4da2020-12-08 00:21:20 -0800217void SnapuserdSelinuxHelper::StartTransition() {
218 LOG(INFO) << "Starting SELinux transition of snapuserd";
219
220 // The restorecon path reads from /system etc, so make sure any reads have
221 // been cached before proceeding.
222 auto handle = selinux_android_file_context_handle();
223 if (!handle) {
224 LOG(FATAL) << "Could not create SELinux file context handle";
225 }
226 selinux_android_set_sehandle(handle);
227
228 // We cannot access /system after the transition, so make sure init is
229 // pinned in memory.
David Anderson9fd88622021-03-05 14:10:55 -0800230 LockAllSystemPages();
David Anderson491e4da2020-12-08 00:21:20 -0800231
232 argv_.emplace_back("snapuserd");
233 argv_.emplace_back("-no_socket");
Akilesh Kailash035e5572022-07-15 18:40:07 +0000234 if (!sm_->PrepareSnapuserdArgsForSelinux(&argv_)) {
David Anderson491e4da2020-12-08 00:21:20 -0800235 LOG(FATAL) << "Could not perform selinux transition";
236 }
David Anderson491e4da2020-12-08 00:21:20 -0800237}
238
239void SnapuserdSelinuxHelper::FinishTransition() {
240 RelabelLink("/dev/block/by-name/super");
241 RelabelDeviceMapper();
242
243 selinux_android_restorecon("/dev/null", 0);
244 selinux_android_restorecon("/dev/urandom", 0);
245 selinux_android_restorecon("/dev/kmsg", 0);
246 selinux_android_restorecon("/dev/dm-user", SELINUX_ANDROID_RESTORECON_RECURSE);
247
248 RelaunchFirstStageSnapuserd();
249
250 if (munlockall() < 0) {
251 PLOG(ERROR) << "munlockall failed";
252 }
253}
254
Akilesh Kailashfd5562b2022-01-25 07:05:31 +0000255/*
256 * Before starting init second stage, we will wait
257 * for snapuserd daemon to be up and running; bionic libc
258 * may read /system/etc/selinux/plat_property_contexts file
259 * before invoking main() function. This will happen if
260 * init initializes property during second stage. Any access
261 * to /system without snapuserd daemon will lead to a deadlock.
262 *
263 * Thus, we do a simple probe by reading system partition. This
264 * read will eventually be serviced by daemon confirming that
265 * daemon is up and running. Furthermore, we are still in the kernel
266 * domain and sepolicy has not been enforced yet. Thus, access
267 * to these device mapper block devices are ok even though
268 * we may see audit logs.
269 */
270bool SnapuserdSelinuxHelper::TestSnapuserdIsReady() {
Akilesh Kailash5140f3a2023-01-02 21:11:13 -0800271 // Wait for the daemon to be fully up. Daemon will write to path
272 // /metadata/ota/daemon-alive-indicator only when all the threads
273 // are ready and attached to dm-user.
274 //
275 // This check will fail for GRF devices with vendor on Android S.
276 // snapuserd binary from Android S won't be able to communicate
277 // and hence, we will fallback and issue I/O to verify
278 // the presence of daemon.
279 auto client = std::make_unique<SnapuserdClient>();
280 if (!client->IsTransitionedDaemonReady()) {
281 LOG(ERROR) << "IsTransitionedDaemonReady failed";
282 }
283
Akilesh Kailashfd5562b2022-01-25 07:05:31 +0000284 std::string dev = "/dev/block/mapper/system"s + fs_mgr_get_slot_suffix();
285 android::base::unique_fd fd(open(dev.c_str(), O_RDONLY | O_DIRECT));
286 if (fd < 0) {
287 PLOG(ERROR) << "open " << dev << " failed";
288 return false;
289 }
290
291 void* addr;
292 ssize_t page_size = getpagesize();
293 if (posix_memalign(&addr, page_size, page_size) < 0) {
294 PLOG(ERROR) << "posix_memalign with page size " << page_size;
295 return false;
296 }
297
298 std::unique_ptr<void, decltype(&::free)> buffer(addr, ::free);
299
300 int iter = 0;
301 while (iter < 10) {
302 ssize_t n = TEMP_FAILURE_RETRY(pread(fd.get(), buffer.get(), page_size, 0));
303 if (n < 0) {
304 // Wait for sometime before retry
305 std::this_thread::sleep_for(100ms);
306 } else if (n == page_size) {
307 return true;
308 } else {
309 LOG(ERROR) << "pread returned: " << n << " from: " << dev << " expected: " << page_size;
310 }
311
312 iter += 1;
313 }
314
315 return false;
316}
317
David Anderson491e4da2020-12-08 00:21:20 -0800318void SnapuserdSelinuxHelper::RelaunchFirstStageSnapuserd() {
Akilesh Kailash035e5572022-07-15 18:40:07 +0000319 if (!sm_->DetachFirstStageSnapuserdForSelinux()) {
320 LOG(FATAL) << "Could not perform selinux transition";
321 }
322
323 KillFirstStageSnapuserd(old_pid_);
324
David Anderson491e4da2020-12-08 00:21:20 -0800325 auto fd = GetRamdiskSnapuserdFd();
326 if (!fd) {
327 LOG(FATAL) << "Environment variable " << kSnapuserdFirstStageFdVar << " was not set!";
328 }
329 unsetenv(kSnapuserdFirstStageFdVar);
330
331 RestoreconRamdiskSnapuserd(fd.value());
332
333 pid_t pid = fork();
334 if (pid < 0) {
335 PLOG(FATAL) << "Fork to relaunch snapuserd failed";
336 }
337 if (pid > 0) {
338 // We don't need the descriptor anymore, and it should be closed to
339 // avoid leaking into subprocesses.
340 close(fd.value());
341
342 setenv(kSnapuserdFirstStagePidVar, std::to_string(pid).c_str(), 1);
343
344 LOG(INFO) << "Relaunched snapuserd with pid: " << pid;
Akilesh Kailashfd5562b2022-01-25 07:05:31 +0000345
Akilesh Kailash4ffe8a32022-06-02 08:00:39 +0000346 // Since daemon is not started as a service, we have
347 // to explicitly set the OOM score to default which is unkillable
348 std::string oom_str = std::to_string(DEFAULT_OOM_SCORE_ADJUST);
349 std::string oom_file = android::base::StringPrintf("/proc/%d/oom_score_adj", pid);
350 if (!android::base::WriteStringToFile(oom_str, oom_file)) {
351 PLOG(ERROR) << "couldn't write oom_score_adj to snapuserd daemon with pid: " << pid;
352 }
353
Akilesh Kailashfd5562b2022-01-25 07:05:31 +0000354 if (!TestSnapuserdIsReady()) {
355 PLOG(FATAL) << "snapuserd daemon failed to launch";
356 } else {
357 LOG(INFO) << "snapuserd daemon is up and running";
358 }
359
David Anderson491e4da2020-12-08 00:21:20 -0800360 return;
361 }
362
363 // Make sure the descriptor is gone after we exec.
364 if (fcntl(fd.value(), F_SETFD, FD_CLOEXEC) < 0) {
365 PLOG(FATAL) << "fcntl FD_CLOEXEC failed for snapuserd fd";
366 }
367
368 std::vector<char*> argv;
369 for (auto& arg : argv_) {
370 argv.emplace_back(arg.data());
371 }
372 argv.emplace_back(nullptr);
373
374 int rv = syscall(SYS_execveat, fd.value(), "", reinterpret_cast<char* const*>(argv.data()),
375 nullptr, AT_EMPTY_PATH);
376 if (rv < 0) {
377 PLOG(FATAL) << "Failed to execveat() snapuserd";
378 }
379}
380
381std::unique_ptr<SnapuserdSelinuxHelper> SnapuserdSelinuxHelper::CreateIfNeeded() {
382 if (IsRecoveryMode()) {
383 return nullptr;
384 }
385
386 auto old_pid = GetSnapuserdFirstStagePid();
387 if (!old_pid) {
388 return nullptr;
389 }
390
391 auto sm = SnapshotManager::NewForFirstStageMount();
392 if (!sm) {
393 LOG(FATAL) << "Unable to create SnapshotManager";
394 }
395 return std::make_unique<SnapuserdSelinuxHelper>(std::move(sm), old_pid.value());
396}
397
398void KillFirstStageSnapuserd(pid_t pid) {
399 if (kill(pid, SIGTERM) < 0 && errno != ESRCH) {
400 LOG(ERROR) << "Kill snapuserd pid failed: " << pid;
401 } else {
402 LOG(INFO) << "Sent SIGTERM to snapuserd process " << pid;
403 }
404}
405
406void CleanupSnapuserdSocket() {
407 auto socket_path = ANDROID_SOCKET_DIR "/"s + android::snapshot::kSnapuserdSocket;
408 if (access(socket_path.c_str(), F_OK) != 0) {
409 return;
410 }
411
412 // Tell the daemon to stop accepting connections and to gracefully exit
413 // once all outstanding handlers have terminated.
414 if (auto client = SnapuserdClient::Connect(android::snapshot::kSnapuserdSocket, 3s)) {
415 client->DetachSnapuserd();
416 }
417
418 // Unlink the socket so we can create it again in second-stage.
419 if (unlink(socket_path.c_str()) < 0) {
420 PLOG(FATAL) << "unlink " << socket_path << " failed";
421 }
422}
423
424void SaveRamdiskPathToSnapuserd() {
425 int fd = open(kSnapuserdPath, O_PATH);
426 if (fd < 0) {
427 PLOG(FATAL) << "Unable to open snapuserd: " << kSnapuserdPath;
428 }
429
430 auto value = std::to_string(fd);
431 if (setenv(kSnapuserdFirstStageFdVar, value.c_str(), 1) < 0) {
432 PLOG(FATAL) << "setenv failed: " << kSnapuserdFirstStageFdVar << "=" << value;
433 }
434}
435
436bool IsFirstStageSnapuserdRunning() {
437 return GetSnapuserdFirstStagePid().has_value();
438}
439
David Anderson0e5ad5a2021-07-21 21:53:28 -0700440std::vector<std::string> GetSnapuserdFirstStageInfo() {
441 const char* pid_str = getenv(kSnapuserdFirstStageInfoVar);
442 if (!pid_str) {
443 return {};
444 }
445 return android::base::Split(pid_str, ",");
446}
447
David Anderson491e4da2020-12-08 00:21:20 -0800448} // namespace init
449} // namespace android