Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 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 | |
Justin Yun | dc5a327 | 2018-11-21 17:37:57 +0900 | [diff] [blame] | 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 | }; |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 44 | const auto kVndkVersionPropertyName = "ro.vndk.version"s; |
| 45 | const auto kVendorTopDir = "/vendor/"s; |
| 46 | const auto kLowerdirOption = "lowerdir="s; |
| 47 | |
Justin Yun | dc5a327 | 2018-11-21 17:37:57 +0900 | [diff] [blame] | 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; |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 56 | |
Justin Yun | dc5a327 | 2018-11-21 17:37:57 +0900 | [diff] [blame] | 57 | // Vendor overlay root for current vendor version found! |
| 58 | LINFO << "vendor overlay root: " << overlay_top; |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 59 | |
Justin Yun | dc5a327 | 2018-11-21 17:37:57 +0900 | [diff] [blame] | 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); |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 66 | } |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 67 | } |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 68 | return vendor_overlay_dirs; |
| 69 | } |
| 70 | |
Justin Yun | dc5a327 | 2018-11-21 17:37:57 +0900 | [diff] [blame] | 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; |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 74 | LINFO << "vendor overlay mount on " << vendor_mount_point; |
| 75 | |
Justin Yun | ad55005 | 2018-11-02 17:45:24 +0900 | [diff] [blame] | 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 | } |
Justin Yun | dc5a327 | 2018-11-21 17:37:57 +0900 | [diff] [blame] | 81 | const auto source_directory = overlay_top + "/" + mount_dir; |
Justin Yun | ad55005 | 2018-11-02 17:45:24 +0900 | [diff] [blame] | 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 << ")"; |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 86 | return false; |
| 87 | } |
| 88 | |
Mark Salyzyn | 6a11694 | 2018-11-05 08:53:30 -0800 | [diff] [blame] | 89 | auto options = kLowerdirOption + source_directory + ":" + vendor_mount_point; |
| 90 | if (fs_mgr_overlayfs_valid() == OverlayfsValidResult::kOverrideCredsRequired) { |
| 91 | options += ",override_creds=off"; |
| 92 | } |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 93 | auto report = "__mount(source=overlay,target="s + vendor_mount_point + ",type=overlay," + |
| 94 | options + ")="; |
Yi-Yo Chiang | 7cbed5a | 2021-05-18 17:11:35 +0800 | [diff] [blame] | 95 | auto ret = mount("overlay", vendor_mount_point.c_str(), "overlay", MS_RDONLY | MS_NOATIME, |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 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. |
| 113 | bool fs_mgr_vendor_overlay_mount_all() { |
Justin Yun | dc5a327 | 2018-11-21 17:37:57 +0900 | [diff] [blame] | 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()) { |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 118 | LINFO << "vendor overlay: vndk version not defined"; |
| 119 | return false; |
| 120 | } |
Justin Yun | dc5a327 | 2018-11-21 17:37:57 +0900 | [diff] [blame] | 121 | |
| 122 | const auto vendor_overlay_dirs = fs_mgr_get_vendor_overlay_dirs(vndk_version); |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 123 | if (vendor_overlay_dirs.empty()) return true; |
Mark Salyzyn | 6a11694 | 2018-11-05 08:53:30 -0800 | [diff] [blame] | 124 | if (fs_mgr_overlayfs_valid() == OverlayfsValidResult::kNotSupported) { |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 125 | LINFO << "vendor overlay: kernel does not support overlayfs"; |
| 126 | return false; |
| 127 | } |
| 128 | |
Justin Yun | dc5a327 | 2018-11-21 17:37:57 +0900 | [diff] [blame] | 129 | // Mount each directory in /(system|product)/vendor_overlay/<ver> on /vendor |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 130 | auto ret = true; |
| 131 | for (const auto& vendor_overlay_dir : vendor_overlay_dirs) { |
Justin Yun | dc5a327 | 2018-11-21 17:37:57 +0900 | [diff] [blame] | 132 | if (!fs_mgr_vendor_overlay_mount(vendor_overlay_dir)) { |
Justin Yun | 6bab0a9 | 2018-10-29 18:31:48 +0900 | [diff] [blame] | 133 | ret = false; |
| 134 | } |
| 135 | } |
| 136 | return ret; |
| 137 | } |