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