Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | /* |
| 18 | * Tool to create a directory with the right SELinux context applied, or |
| 19 | * apply the context if it's absent. Also fixes mode, uid, gid. |
| 20 | */ |
| 21 | |
| 22 | #include <iostream> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <dirent.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | |
| 32 | #include <android-base/logging.h> |
| 33 | #include <android-base/scopeguard.h> |
| 34 | |
| 35 | #include <cutils/fs.h> |
| 36 | #include <selinux/android.h> |
| 37 | |
| 38 | #include "Utils.h" |
| 39 | #include "android/os/IVold.h" |
| 40 | |
Andreas Huber | 71cd43f | 2018-01-22 11:25:29 -0800 | [diff] [blame] | 41 | #include <private/android_filesystem_config.h> |
| 42 | |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 43 | static void usage(const char* progname) { |
| 44 | std::cerr << "Usage: " << progname << " [ prepare | destroy ] <volume_uuid> <user_id> <flags>" |
| 45 | << std::endl; |
| 46 | exit(-1); |
| 47 | } |
| 48 | |
| 49 | static bool small_int(const std::string& s) { |
| 50 | return !s.empty() && s.size() < 7 && s.find_first_not_of("0123456789") == std::string::npos; |
| 51 | } |
| 52 | |
| 53 | static bool valid_uuid(const std::string& s) { |
| 54 | return s.size() < 40 && s.find_first_not_of("0123456789abcdefABCDEF-_") == std::string::npos; |
| 55 | } |
| 56 | |
| 57 | static bool prepare_dir(struct selabel_handle* sehandle, mode_t mode, uid_t uid, gid_t gid, |
| 58 | const std::string& path) { |
| 59 | auto clearfscreatecon = android::base::make_scope_guard([] { setfscreatecon(nullptr); }); |
| 60 | auto secontext = std::unique_ptr<char, void (*)(char*)>(nullptr, freecon); |
| 61 | char* tmp_secontext; |
| 62 | if (sehandle && selabel_lookup(sehandle, &tmp_secontext, path.c_str(), S_IFDIR) == 0) { |
| 63 | secontext.reset(tmp_secontext); |
| 64 | } |
| 65 | LOG(DEBUG) << "Setting up mode " << std::oct << mode << std::dec << " uid " << uid << " gid " |
Roman Kiryanov | f101236 | 2018-07-26 13:41:14 -0700 | [diff] [blame] | 66 | << gid << " context " << (secontext ? secontext.get() : "null") |
| 67 | << " on path: " << path; |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 68 | if (secontext) { |
| 69 | if (setfscreatecon(secontext.get()) != 0) { |
| 70 | PLOG(ERROR) << "Unable to read setfscreatecon for: " << path; |
| 71 | return false; |
| 72 | } |
| 73 | } |
| 74 | if (fs_prepare_dir(path.c_str(), mode, uid, gid) != 0) { |
| 75 | return false; |
| 76 | } |
| 77 | if (secontext) { |
| 78 | char* tmp_oldsecontext = nullptr; |
| 79 | if (lgetfilecon(path.c_str(), &tmp_oldsecontext) < 0) { |
| 80 | PLOG(ERROR) << "Unable to read secontext for: " << path; |
| 81 | return false; |
| 82 | } |
| 83 | auto oldsecontext = std::unique_ptr<char, void (*)(char*)>(tmp_oldsecontext, freecon); |
| 84 | if (strcmp(secontext.get(), oldsecontext.get()) != 0) { |
| 85 | LOG(INFO) << "Relabelling from " << ((char*)oldsecontext.get()) << " to " |
| 86 | << ((char*)secontext.get()) << ": " << path; |
| 87 | if (lsetfilecon(path.c_str(), secontext.get()) != 0) { |
| 88 | PLOG(ERROR) << "Relabelling failed for: " << path; |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | static bool rmrf_contents(const std::string& path) { |
| 97 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(path.c_str()), closedir); |
| 98 | if (!dirp) { |
| 99 | PLOG(ERROR) << "Unable to open directory: " << path; |
| 100 | return false; |
| 101 | } |
| 102 | bool res = true; |
| 103 | for (;;) { |
| 104 | errno = 0; |
| 105 | auto const entry = readdir(dirp.get()); |
| 106 | if (!entry) { |
| 107 | if (errno) { |
| 108 | PLOG(ERROR) << "readdir failed on: " << path; |
| 109 | return false; |
| 110 | } |
| 111 | return res; |
| 112 | } |
| 113 | if (entry->d_name[0] == '.') continue; |
| 114 | auto subdir = path + "/" + entry->d_name; |
| 115 | if (0 != |
| 116 | android::vold::ForkExecvp(std::vector<std::string>{"/system/bin/rm", "-rf", subdir})) { |
| 117 | LOG(ERROR) << "rm -rf failed on " << subdir; |
| 118 | res = false; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static bool prepare_subdirs(const std::string& volume_uuid, int user_id, int flags) { |
| 124 | struct selabel_handle* sehandle = selinux_android_file_context_handle(); |
| 125 | |
| 126 | if (volume_uuid.empty()) { |
| 127 | if (flags & android::os::IVold::STORAGE_FLAG_DE) { |
| 128 | auto misc_de_path = android::vold::BuildDataMiscDePath(user_id); |
| 129 | if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/vold")) return false; |
Jin Qian | f396144 | 2017-10-19 14:44:37 -0700 | [diff] [blame] | 130 | if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/storaged")) return false; |
Narayan Kamath | a232fd7 | 2019-01-14 10:03:07 +0000 | [diff] [blame] | 131 | if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/rollback")) return false; |
Andreas Huber | 71cd43f | 2018-01-22 11:25:29 -0800 | [diff] [blame] | 132 | |
| 133 | auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id); |
| 134 | if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, vendor_de_path + "/fpdata")) { |
| 135 | return false; |
| 136 | } |
Kevin Chyn | cdd4228 | 2018-11-20 19:09:15 +0000 | [diff] [blame] | 137 | auto facedata_path = vendor_de_path + "/facedata"; |
| 138 | if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, facedata_path)) { |
| 139 | return false; |
| 140 | } |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 141 | } |
| 142 | if (flags & android::os::IVold::STORAGE_FLAG_CE) { |
| 143 | auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id); |
| 144 | if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/vold")) return false; |
Jin Qian | f396144 | 2017-10-19 14:44:37 -0700 | [diff] [blame] | 145 | if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/storaged")) return false; |
Narayan Kamath | a232fd7 | 2019-01-14 10:03:07 +0000 | [diff] [blame] | 146 | if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/rollback")) return false; |
Annie Meng | 66176c5 | 2019-01-16 21:32:27 +0000 | [diff] [blame] | 147 | |
| 148 | auto system_ce_path = android::vold::BuildDataSystemCePath(user_id); |
| 149 | if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, system_ce_path + "/backup")) { |
| 150 | return false; |
| 151 | } |
| 152 | if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, |
| 153 | system_ce_path + "/backup_stage")) { |
| 154 | return false; |
| 155 | } |
Paul Crowley | b409ade | 2019-04-23 17:04:35 -0700 | [diff] [blame] | 156 | auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id); |
| 157 | auto facedata_path = vendor_ce_path + "/facedata"; |
| 158 | if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, facedata_path)) { |
| 159 | return false; |
| 160 | } |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | static bool destroy_subdirs(const std::string& volume_uuid, int user_id, int flags) { |
| 167 | bool res = true; |
| 168 | if (volume_uuid.empty()) { |
| 169 | if (flags & android::os::IVold::STORAGE_FLAG_CE) { |
| 170 | auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id); |
| 171 | res &= rmrf_contents(misc_ce_path); |
Andreas Huber | 71cd43f | 2018-01-22 11:25:29 -0800 | [diff] [blame] | 172 | |
| 173 | auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id); |
| 174 | res &= rmrf_contents(vendor_ce_path); |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 175 | } |
| 176 | if (flags & android::os::IVold::STORAGE_FLAG_DE) { |
| 177 | auto misc_de_path = android::vold::BuildDataMiscDePath(user_id); |
| 178 | res &= rmrf_contents(misc_de_path); |
Andreas Huber | 71cd43f | 2018-01-22 11:25:29 -0800 | [diff] [blame] | 179 | |
| 180 | auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id); |
| 181 | res &= rmrf_contents(vendor_de_path); |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | return res; |
| 185 | } |
| 186 | |
| 187 | int main(int argc, const char* const argv[]) { |
| 188 | android::base::InitLogging(const_cast<char**>(argv)); |
| 189 | std::vector<std::string> args(argv + 1, argv + argc); |
| 190 | |
| 191 | if (args.size() != 4 || !valid_uuid(args[1]) || !small_int(args[2]) || !small_int(args[3])) { |
| 192 | usage(argv[0]); |
| 193 | return -1; |
| 194 | } |
| 195 | |
| 196 | auto volume_uuid = args[1]; |
| 197 | int user_id = stoi(args[2]); |
| 198 | int flags = stoi(args[3]); |
| 199 | if (args[0] == "prepare") { |
| 200 | if (!prepare_subdirs(volume_uuid, user_id, flags)) return -1; |
| 201 | } else if (args[0] == "destroy") { |
| 202 | if (!destroy_subdirs(volume_uuid, user_id, flags)) return -1; |
| 203 | } else { |
| 204 | usage(argv[0]); |
| 205 | return -1; |
| 206 | } |
| 207 | return 0; |
| 208 | } |