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