1 /*
2 * Copyright (C) 2018 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 <dirent.h>
18 #include <selinux/selinux.h>
19 #include <sys/mount.h>
20 #include <unistd.h>
21
22 #include <memory>
23 #include <string>
24 #include <vector>
25
26 #include <android-base/logging.h>
27 #include <android-base/properties.h>
28 #include <fs_mgr_overlayfs.h>
29 #include <fs_mgr_vendor_overlay.h>
30 #include <fstab/fstab.h>
31
32 #include "fs_mgr_priv.h"
33
34 using namespace std::literals;
35
36 namespace {
37
38 // The order of the list means the priority to show the files in the directory.
39 // The last one has the highest priority.
40 const std::vector<const std::string> kVendorOverlaySourceDirs = {
41 "/system/vendor_overlay/",
42 "/product/vendor_overlay/",
43 };
44 const auto kVndkVersionPropertyName = "ro.vndk.version"s;
45 const auto kVendorTopDir = "/vendor/"s;
46 const auto kLowerdirOption = "lowerdir="s;
47
fs_mgr_get_vendor_overlay_dirs(const std::string & vndk_version)48 std::vector<std::pair<std::string, std::string>> fs_mgr_get_vendor_overlay_dirs(
49 const std::string& vndk_version) {
50 std::vector<std::pair<std::string, std::string>> vendor_overlay_dirs;
51 for (const auto& vendor_overlay_source : kVendorOverlaySourceDirs) {
52 const auto overlay_top = vendor_overlay_source + vndk_version;
53 std::unique_ptr<DIR, decltype(&closedir)> vendor_overlay_top(opendir(overlay_top.c_str()),
54 closedir);
55 if (!vendor_overlay_top) continue;
56
57 // Vendor overlay root for current vendor version found!
58 LINFO << "vendor overlay root: " << overlay_top;
59
60 struct dirent* dp;
61 while ((dp = readdir(vendor_overlay_top.get())) != nullptr) {
62 if (dp->d_type != DT_DIR || dp->d_name[0] == '.') {
63 continue;
64 }
65 vendor_overlay_dirs.emplace_back(overlay_top, dp->d_name);
66 }
67 }
68 return vendor_overlay_dirs;
69 }
70
fs_mgr_vendor_overlay_mount(const std::pair<std::string,std::string> & mount_point)71 bool fs_mgr_vendor_overlay_mount(const std::pair<std::string, std::string>& mount_point) {
72 const auto [overlay_top, mount_dir] = mount_point;
73 const auto vendor_mount_point = kVendorTopDir + mount_dir;
74 LINFO << "vendor overlay mount on " << vendor_mount_point;
75
76 const auto target_context = fs_mgr_get_context(vendor_mount_point);
77 if (target_context.empty()) {
78 PERROR << " failed: cannot find the target vendor mount point";
79 return false;
80 }
81 const auto source_directory = overlay_top + "/" + mount_dir;
82 const auto source_context = fs_mgr_get_context(source_directory);
83 if (target_context != source_context) {
84 LERROR << " failed: source and target contexts do not match (source:" << source_context
85 << ", target:" << target_context << ")";
86 return false;
87 }
88
89 auto options = kLowerdirOption + source_directory + ":" + vendor_mount_point;
90 if (fs_mgr_overlayfs_valid() == OverlayfsValidResult::kOverrideCredsRequired) {
91 options += ",override_creds=off";
92 }
93 auto report = "__mount(source=overlay,target="s + vendor_mount_point + ",type=overlay," +
94 options + ")=";
95 auto ret = mount("overlay", vendor_mount_point.c_str(), "overlay", MS_RDONLY | MS_RELATIME,
96 options.c_str());
97 if (ret) {
98 PERROR << report << ret;
99 return false;
100 } else {
101 LINFO << report << ret;
102 return true;
103 }
104 }
105
106 } // namespace
107
108 // Since the vendor overlay requires to know the version of the vendor partition,
109 // it is not possible to mount vendor overlay at the first stage that cannot
110 // initialize properties.
111 // To read the properties, vendor overlay must be mounted at the second stage, right
112 // after "property_load_boot_defaults()" is called.
fs_mgr_vendor_overlay_mount_all()113 bool fs_mgr_vendor_overlay_mount_all() {
114 // To read the property, it must be called at the second init stage after the default
115 // properties are loaded.
116 static const auto vndk_version = android::base::GetProperty(kVndkVersionPropertyName, "");
117 if (vndk_version.empty()) {
118 LINFO << "vendor overlay: vndk version not defined";
119 return false;
120 }
121
122 const auto vendor_overlay_dirs = fs_mgr_get_vendor_overlay_dirs(vndk_version);
123 if (vendor_overlay_dirs.empty()) return true;
124 if (fs_mgr_overlayfs_valid() == OverlayfsValidResult::kNotSupported) {
125 LINFO << "vendor overlay: kernel does not support overlayfs";
126 return false;
127 }
128
129 // Mount each directory in /(system|product)/vendor_overlay/<ver> on /vendor
130 auto ret = true;
131 for (const auto& vendor_overlay_dir : vendor_overlay_dirs) {
132 if (!fs_mgr_vendor_overlay_mount(vendor_overlay_dir)) {
133 ret = false;
134 }
135 }
136 return ret;
137 }
138