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 | |
Alan Stokes | be3db7b | 2020-02-07 09:29:38 +0000 | [diff] [blame] | 57 | static bool prepare_dir_for_user(struct selabel_handle* sehandle, mode_t mode, uid_t uid, gid_t gid, |
| 58 | const std::string& path, uid_t user_id) { |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 59 | auto clearfscreatecon = android::base::make_scope_guard([] { setfscreatecon(nullptr); }); |
| 60 | auto secontext = std::unique_ptr<char, void (*)(char*)>(nullptr, freecon); |
Alan Stokes | be3db7b | 2020-02-07 09:29:38 +0000 | [diff] [blame] | 61 | if (sehandle) { |
| 62 | char* tmp_secontext; |
| 63 | |
| 64 | if (selabel_lookup(sehandle, &tmp_secontext, path.c_str(), S_IFDIR) == 0) { |
| 65 | secontext.reset(tmp_secontext); |
| 66 | |
| 67 | if (user_id != (uid_t)-1) { |
| 68 | if (selinux_android_context_with_level(secontext.get(), &tmp_secontext, user_id, |
| 69 | (uid_t)-1) != 0) { |
| 70 | PLOG(ERROR) << "Unable to create context with level for: " << path; |
| 71 | return false; |
| 72 | } |
| 73 | secontext.reset(tmp_secontext); // Free the context |
| 74 | } |
| 75 | } |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 76 | } |
Alan Stokes | be3db7b | 2020-02-07 09:29:38 +0000 | [diff] [blame] | 77 | |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 78 | 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] | 79 | << gid << " context " << (secontext ? secontext.get() : "null") |
| 80 | << " on path: " << path; |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 81 | if (secontext) { |
| 82 | if (setfscreatecon(secontext.get()) != 0) { |
Alan Stokes | be3db7b | 2020-02-07 09:29:38 +0000 | [diff] [blame] | 83 | PLOG(ERROR) << "Unable to setfscreatecon for: " << path; |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | if (fs_prepare_dir(path.c_str(), mode, uid, gid) != 0) { |
| 88 | return false; |
| 89 | } |
| 90 | if (secontext) { |
| 91 | char* tmp_oldsecontext = nullptr; |
| 92 | if (lgetfilecon(path.c_str(), &tmp_oldsecontext) < 0) { |
| 93 | PLOG(ERROR) << "Unable to read secontext for: " << path; |
| 94 | return false; |
| 95 | } |
| 96 | auto oldsecontext = std::unique_ptr<char, void (*)(char*)>(tmp_oldsecontext, freecon); |
| 97 | if (strcmp(secontext.get(), oldsecontext.get()) != 0) { |
| 98 | LOG(INFO) << "Relabelling from " << ((char*)oldsecontext.get()) << " to " |
| 99 | << ((char*)secontext.get()) << ": " << path; |
| 100 | if (lsetfilecon(path.c_str(), secontext.get()) != 0) { |
| 101 | PLOG(ERROR) << "Relabelling failed for: " << path; |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | |
Alan Stokes | be3db7b | 2020-02-07 09:29:38 +0000 | [diff] [blame] | 109 | static bool prepare_dir(struct selabel_handle* sehandle, mode_t mode, uid_t uid, gid_t gid, |
| 110 | const std::string& path) { |
| 111 | return prepare_dir_for_user(sehandle, mode, uid, gid, path, (uid_t)-1); |
| 112 | } |
| 113 | |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 114 | static bool rmrf_contents(const std::string& path) { |
| 115 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(path.c_str()), closedir); |
| 116 | if (!dirp) { |
| 117 | PLOG(ERROR) << "Unable to open directory: " << path; |
| 118 | return false; |
| 119 | } |
| 120 | bool res = true; |
| 121 | for (;;) { |
| 122 | errno = 0; |
| 123 | auto const entry = readdir(dirp.get()); |
| 124 | if (!entry) { |
| 125 | if (errno) { |
| 126 | PLOG(ERROR) << "readdir failed on: " << path; |
| 127 | return false; |
| 128 | } |
| 129 | return res; |
| 130 | } |
| 131 | if (entry->d_name[0] == '.') continue; |
| 132 | auto subdir = path + "/" + entry->d_name; |
| 133 | if (0 != |
| 134 | android::vold::ForkExecvp(std::vector<std::string>{"/system/bin/rm", "-rf", subdir})) { |
| 135 | LOG(ERROR) << "rm -rf failed on " << subdir; |
| 136 | res = false; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
Oli Lan | 9445721 | 2019-11-19 18:09:34 +0000 | [diff] [blame] | 141 | static bool prepare_apex_subdirs(struct selabel_handle* sehandle, const std::string& path) { |
Oli Lan | e1b3f5c | 2020-01-17 11:01:38 +0000 | [diff] [blame] | 142 | if (!prepare_dir(sehandle, 0711, 0, 0, path + "/apexdata")) return false; |
Oli Lan | 9445721 | 2019-11-19 18:09:34 +0000 | [diff] [blame] | 143 | |
| 144 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/apex"), closedir); |
| 145 | if (!dirp) { |
| 146 | PLOG(ERROR) << "Unable to open apex directory"; |
| 147 | return false; |
| 148 | } |
| 149 | struct dirent* entry; |
| 150 | while ((entry = readdir(dirp.get())) != nullptr) { |
| 151 | if (entry->d_type != DT_DIR) continue; |
| 152 | |
| 153 | const char* name = entry->d_name; |
| 154 | // skip any starting with "." |
| 155 | if (name[0] == '.') continue; |
| 156 | |
| 157 | if (strchr(name, '@') != NULL) continue; |
| 158 | |
Oli Lan | e1b3f5c | 2020-01-17 11:01:38 +0000 | [diff] [blame] | 159 | if (!prepare_dir(sehandle, 0771, AID_ROOT, AID_SYSTEM, path + "/apexdata/" + name)) { |
Oli Lan | 9445721 | 2019-11-19 18:09:34 +0000 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 166 | static bool prepare_subdirs(const std::string& volume_uuid, int user_id, int flags) { |
| 167 | struct selabel_handle* sehandle = selinux_android_file_context_handle(); |
| 168 | |
Alan Stokes | be3db7b | 2020-02-07 09:29:38 +0000 | [diff] [blame] | 169 | const uid_t user_for_level = |
| 170 | (flags & android::os::IVold::STORAGE_FLAG_LEVEL_FROM_USER) ? user_id : -1; |
| 171 | |
| 172 | if (flags & android::os::IVold::STORAGE_FLAG_DE) { |
| 173 | auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id); |
| 174 | if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM, user_de_path, |
| 175 | user_for_level)) { |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | if (volume_uuid.empty()) { |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 180 | auto misc_de_path = android::vold::BuildDataMiscDePath(user_id); |
| 181 | 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] | 182 | 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] | 183 | if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/rollback")) return false; |
Oli Lan | 9445721 | 2019-11-19 18:09:34 +0000 | [diff] [blame] | 184 | // TODO: Return false if this returns false once sure this should succeed. |
Oli Lan | ac003c4 | 2019-12-02 18:27:24 +0000 | [diff] [blame] | 185 | prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/apexrollback"); |
Oli Lan | 9445721 | 2019-11-19 18:09:34 +0000 | [diff] [blame] | 186 | prepare_apex_subdirs(sehandle, misc_de_path); |
Andreas Huber | 71cd43f | 2018-01-22 11:25:29 -0800 | [diff] [blame] | 187 | |
Alan Stokes | be3db7b | 2020-02-07 09:29:38 +0000 | [diff] [blame] | 188 | auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id); |
| 189 | if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM, profiles_de_path, |
| 190 | user_for_level)) { |
| 191 | return false; |
| 192 | } |
| 193 | |
Andreas Huber | 71cd43f | 2018-01-22 11:25:29 -0800 | [diff] [blame] | 194 | auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id); |
| 195 | if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, vendor_de_path + "/fpdata")) { |
| 196 | return false; |
| 197 | } |
Kevin Chyn | cdd4228 | 2018-11-20 19:09:15 +0000 | [diff] [blame] | 198 | auto facedata_path = vendor_de_path + "/facedata"; |
| 199 | if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, facedata_path)) { |
| 200 | return false; |
| 201 | } |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 202 | } |
Alan Stokes | be3db7b | 2020-02-07 09:29:38 +0000 | [diff] [blame] | 203 | } |
| 204 | if (flags & android::os::IVold::STORAGE_FLAG_CE) { |
| 205 | auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id); |
| 206 | if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM, user_ce_path, |
| 207 | user_for_level)) { |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | if (volume_uuid.empty()) { |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 212 | auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id); |
| 213 | 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] | 214 | 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] | 215 | if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/rollback")) return false; |
Alan Stokes | be3db7b | 2020-02-07 09:29:38 +0000 | [diff] [blame] | 216 | |
Oli Lan | 9445721 | 2019-11-19 18:09:34 +0000 | [diff] [blame] | 217 | // TODO: Return false if this returns false once sure this should succeed. |
Oli Lan | ac003c4 | 2019-12-02 18:27:24 +0000 | [diff] [blame] | 218 | prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/apexrollback"); |
Oli Lan | 9445721 | 2019-11-19 18:09:34 +0000 | [diff] [blame] | 219 | prepare_apex_subdirs(sehandle, misc_ce_path); |
Annie Meng | 66176c5 | 2019-01-16 21:32:27 +0000 | [diff] [blame] | 220 | |
| 221 | auto system_ce_path = android::vold::BuildDataSystemCePath(user_id); |
| 222 | if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, system_ce_path + "/backup")) { |
| 223 | return false; |
| 224 | } |
| 225 | if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, |
| 226 | system_ce_path + "/backup_stage")) { |
| 227 | return false; |
| 228 | } |
Paul Crowley | b409ade | 2019-04-23 17:04:35 -0700 | [diff] [blame] | 229 | auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id); |
| 230 | auto facedata_path = vendor_ce_path + "/facedata"; |
| 231 | if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, facedata_path)) { |
| 232 | return false; |
| 233 | } |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | static bool destroy_subdirs(const std::string& volume_uuid, int user_id, int flags) { |
| 240 | bool res = true; |
| 241 | if (volume_uuid.empty()) { |
| 242 | if (flags & android::os::IVold::STORAGE_FLAG_CE) { |
| 243 | auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id); |
| 244 | res &= rmrf_contents(misc_ce_path); |
Andreas Huber | 71cd43f | 2018-01-22 11:25:29 -0800 | [diff] [blame] | 245 | |
| 246 | auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id); |
| 247 | res &= rmrf_contents(vendor_ce_path); |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 248 | } |
| 249 | if (flags & android::os::IVold::STORAGE_FLAG_DE) { |
| 250 | auto misc_de_path = android::vold::BuildDataMiscDePath(user_id); |
| 251 | res &= rmrf_contents(misc_de_path); |
Andreas Huber | 71cd43f | 2018-01-22 11:25:29 -0800 | [diff] [blame] | 252 | |
| 253 | auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id); |
| 254 | res &= rmrf_contents(vendor_de_path); |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | return res; |
| 258 | } |
| 259 | |
| 260 | int main(int argc, const char* const argv[]) { |
| 261 | android::base::InitLogging(const_cast<char**>(argv)); |
| 262 | std::vector<std::string> args(argv + 1, argv + argc); |
| 263 | |
| 264 | if (args.size() != 4 || !valid_uuid(args[1]) || !small_int(args[2]) || !small_int(args[3])) { |
| 265 | usage(argv[0]); |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | auto volume_uuid = args[1]; |
| 270 | int user_id = stoi(args[2]); |
| 271 | int flags = stoi(args[3]); |
| 272 | if (args[0] == "prepare") { |
| 273 | if (!prepare_subdirs(volume_uuid, user_id, flags)) return -1; |
| 274 | } else if (args[0] == "destroy") { |
| 275 | if (!destroy_subdirs(volume_uuid, user_id, flags)) return -1; |
| 276 | } else { |
| 277 | usage(argv[0]); |
| 278 | return -1; |
| 279 | } |
| 280 | return 0; |
| 281 | } |