bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "FsCrypt.h" |
| 18 | |
| 19 | #include "KeyStorage.h" |
| 20 | #include "KeyUtil.h" |
| 21 | #include "Utils.h" |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 22 | #include "VoldUtil.h" |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 23 | |
| 24 | #include <algorithm> |
| 25 | #include <map> |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 26 | #include <optional> |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 27 | #include <set> |
| 28 | #include <sstream> |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include <dirent.h> |
| 33 | #include <errno.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <limits.h> |
| 36 | #include <selinux/android.h> |
| 37 | #include <sys/mount.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <unistd.h> |
| 41 | |
| 42 | #include <private/android_filesystem_config.h> |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 43 | #include <private/android_projectid_config.h> |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 44 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 45 | #include "android/os/IVold.h" |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 46 | |
| 47 | #define EMULATED_USES_SELINUX 0 |
| 48 | #define MANAGE_MISC_DIRS 0 |
| 49 | |
| 50 | #include <cutils/fs.h> |
| 51 | #include <cutils/properties.h> |
| 52 | |
| 53 | #include <fscrypt/fscrypt.h> |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 54 | #include <keyutils.h> |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 55 | #include <libdm/dm.h> |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 56 | |
| 57 | #include <android-base/file.h> |
| 58 | #include <android-base/logging.h> |
| 59 | #include <android-base/properties.h> |
| 60 | #include <android-base/stringprintf.h> |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 61 | #include <android-base/strings.h> |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 62 | #include <android-base/unique_fd.h> |
| 63 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 64 | #include "fscrypt-common.h" |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 65 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 66 | using android::base::Basename; |
| 67 | using android::base::Realpath; |
| 68 | using android::base::StartsWith; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 69 | using android::base::StringPrintf; |
| 70 | using android::fs_mgr::GetEntryForMountPoint; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 71 | using ::BuildDataPath; |
| 72 | using ::IsFilesystemSupported; |
| 73 | using ::kEmptyAuthentication; |
| 74 | using ::KeyBuffer; |
| 75 | using ::KeyGeneration; |
| 76 | using ::retrieveKey; |
| 77 | using ::retrieveOrGenerateKey; |
| 78 | using ::SetQuotaInherit; |
| 79 | using ::SetQuotaProjectId; |
| 80 | using ::writeStringToFile; |
| 81 | using namespace android::fscrypt; |
| 82 | using namespace android::dm; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 83 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 84 | // Map user ids to encryption policies |
| 85 | std::map<userid_t, EncryptionPolicy> s_de_policies; |
| 86 | std::map<userid_t, EncryptionPolicy> s_ce_policies; |
| 87 | std::string de_key_raw_ref; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 88 | |
| 89 | namespace { |
| 90 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 91 | const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder; |
| 92 | const std::string device_key_path = device_key_dir + "/key"; |
| 93 | const std::string device_key_temp = device_key_dir + "/temp"; |
| 94 | |
| 95 | const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys"; |
| 96 | const std::string user_key_temp = user_key_dir + "/temp"; |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 97 | const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs"; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 98 | |
| 99 | const std::string systemwide_volume_key_dir = |
| 100 | std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys"; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 101 | |
| 102 | // Some users are ephemeral, don't try to wipe their keys from disk |
| 103 | std::set<userid_t> s_ephemeral_users; |
| 104 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 105 | } // namespace |
| 106 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 107 | // Returns KeyGeneration suitable for key as described in EncryptionOptions |
| 108 | static KeyGeneration makeGen(const EncryptionOptions& options) { |
| 109 | return KeyGeneration{FSCRYPT_MAX_KEY_SIZE, true, options.use_hw_wrapped_key}; |
| 110 | } |
| 111 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 112 | static bool fscrypt_is_emulated() { |
| 113 | return property_get_bool("persist.sys.emulate_fbe", false); |
| 114 | } |
| 115 | |
| 116 | static const char* escape_empty(const std::string& value) { |
| 117 | return value.empty() ? "null" : value.c_str(); |
| 118 | } |
| 119 | |
| 120 | static std::string get_de_key_path(userid_t user_id) { |
| 121 | return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id); |
| 122 | } |
| 123 | |
| 124 | static std::string get_ce_key_directory_path(userid_t user_id) { |
| 125 | return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id); |
| 126 | } |
| 127 | |
| 128 | // Returns the keys newest first |
| 129 | static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) { |
| 130 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir); |
| 131 | if (!dirp) { |
| 132 | PLOG(ERROR) << "Unable to open ce key directory: " + directory_path; |
| 133 | return std::vector<std::string>(); |
| 134 | } |
| 135 | std::vector<std::string> result; |
| 136 | for (;;) { |
| 137 | errno = 0; |
| 138 | auto const entry = readdir(dirp.get()); |
| 139 | if (!entry) { |
| 140 | if (errno) { |
| 141 | PLOG(ERROR) << "Unable to read ce key directory: " + directory_path; |
| 142 | return std::vector<std::string>(); |
| 143 | } |
| 144 | break; |
| 145 | } |
| 146 | if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 147 | LOG(INFO) << "Skipping non-key " << entry->d_name; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 148 | continue; |
| 149 | } |
| 150 | result.emplace_back(directory_path + "/" + entry->d_name); |
| 151 | } |
| 152 | std::sort(result.begin(), result.end()); |
| 153 | std::reverse(result.begin(), result.end()); |
| 154 | return result; |
| 155 | } |
| 156 | |
| 157 | static std::string get_ce_key_current_path(const std::string& directory_path) { |
| 158 | return directory_path + "/current"; |
| 159 | } |
| 160 | |
| 161 | static bool get_ce_key_new_path(const std::string& directory_path, |
| 162 | const std::vector<std::string>& paths, std::string* ce_key_path) { |
| 163 | if (paths.empty()) { |
| 164 | *ce_key_path = get_ce_key_current_path(directory_path); |
| 165 | return true; |
| 166 | } |
| 167 | for (unsigned int i = 0; i < UINT_MAX; i++) { |
| 168 | auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i); |
| 169 | if (paths[0] < candidate) { |
| 170 | *ce_key_path = candidate; |
| 171 | return true; |
| 172 | } |
| 173 | } |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | // Discard all keys but the named one; rename it to canonical name. |
| 178 | // No point in acting on errors in this; ignore them. |
| 179 | static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix, |
| 180 | const std::vector<std::string>& paths) { |
| 181 | for (auto const other_path : paths) { |
| 182 | if (other_path != to_fix) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 183 | ::destroyKey(other_path); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | auto const current_path = get_ce_key_current_path(directory_path); |
| 187 | if (to_fix != current_path) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 188 | LOG(INFO) << "Renaming " << to_fix << " to " << current_path; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 189 | if (rename(to_fix.c_str(), current_path.c_str()) != 0) { |
| 190 | PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path; |
| 191 | return; |
| 192 | } |
| 193 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 194 | ::FsyncDirectory(directory_path); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static bool read_and_fixate_user_ce_key(userid_t user_id, |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 198 | const ::KeyAuthentication& auth, |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 199 | KeyBuffer* ce_key) { |
| 200 | auto const directory_path = get_ce_key_directory_path(user_id); |
| 201 | auto const paths = get_ce_key_paths(directory_path); |
| 202 | for (auto const ce_key_path : paths) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 203 | LOG(INFO) << "Trying user CE key " << ce_key_path; |
bigbiff | bbbfe17 | 2021-05-30 15:52:41 -0400 | [diff] [blame] | 204 | if (retrieveKey(ce_key_path, auth, ce_key, true)) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 205 | LOG(INFO) << "Successfully retrieved key"; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 206 | fixate_user_ce_key(directory_path, ce_key_path, paths); |
| 207 | return true; |
| 208 | } |
| 209 | } |
| 210 | LOG(ERROR) << "Failed to find working ce key for user " << user_id; |
| 211 | return false; |
| 212 | } |
| 213 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 214 | static bool IsEmmcStorage(const std::string& blk_device) { |
| 215 | // Handle symlinks. |
| 216 | std::string real_path; |
| 217 | if (!Realpath(blk_device, &real_path)) { |
| 218 | real_path = blk_device; |
| 219 | } |
| 220 | |
| 221 | // Handle logical volumes. |
| 222 | auto& dm = DeviceMapper::Instance(); |
| 223 | for (;;) { |
| 224 | auto parent = dm.GetParentBlockDeviceByPath(real_path); |
| 225 | if (!parent.has_value()) break; |
| 226 | real_path = *parent; |
| 227 | } |
| 228 | |
| 229 | // Now we should have the "real" block device. |
| 230 | LOG(INFO) << "IsEmmcStorage(): blk_device = " << blk_device << ", real_path=" << real_path; |
| 231 | return StartsWith(Basename(real_path), "mmcblk"); |
| 232 | } |
| 233 | |
| 234 | // Retrieve the options to use for encryption policies on the /data filesystem. |
| 235 | static bool get_data_file_encryption_options(EncryptionOptions* options) { |
bigbiff | 543a928 | 2021-10-02 09:50:58 -0400 | [diff] [blame] | 236 | if (fstab_default.empty()) { |
| 237 | if (!ReadDefaultFstab(&fstab_default)) { |
| 238 | PLOG(ERROR) << "Failed to open default fstab"; |
| 239 | return false; |
| 240 | } |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 241 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 242 | auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT); |
| 243 | if (entry == nullptr) { |
| 244 | LOG(ERROR) << "No mount point entry for " << DATA_MNT_POINT; |
mauronofrio matarrese | f1079ed | 2020-05-25 20:52:57 +0200 | [diff] [blame] | 245 | return false; |
| 246 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 247 | if (!ParseOptions(entry->encryption_options, options)) { |
| 248 | LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": " |
| 249 | << entry->encryption_options; |
| 250 | return false; |
| 251 | } |
bigbiff | bcd23d3 | 2021-09-22 18:22:13 -0400 | [diff] [blame] | 252 | if (options->version == 1) { |
| 253 | options->use_hw_wrapped_key = |
| 254 | GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key; |
| 255 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 256 | if ((options->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) && |
| 257 | !IsEmmcStorage(entry->blk_device)) { |
| 258 | LOG(ERROR) << "The emmc_optimized encryption flag is only allowed on eMMC storage. Remove " |
| 259 | "this flag from the device's fstab"; |
| 260 | return false; |
| 261 | } |
| 262 | return true; |
mauronofrio matarrese | f1079ed | 2020-05-25 20:52:57 +0200 | [diff] [blame] | 263 | } |
| 264 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 265 | static bool install_storage_key(const std::string& mountpoint, const EncryptionOptions& options, |
| 266 | const KeyBuffer& key, EncryptionPolicy* policy) { |
| 267 | KeyBuffer ephemeral_wrapped_key; |
| 268 | if (options.use_hw_wrapped_key) { |
| 269 | if (!exportWrappedStorageKey(key, &ephemeral_wrapped_key)) { |
| 270 | LOG(ERROR) << "Failed to get ephemeral wrapped key"; |
| 271 | return false; |
| 272 | } |
| 273 | } |
| 274 | return installKey(mountpoint, options, options.use_hw_wrapped_key ? ephemeral_wrapped_key : key, |
| 275 | policy); |
| 276 | } |
| 277 | |
| 278 | // Retrieve the options to use for encryption policies on adoptable storage. |
| 279 | static bool get_volume_file_encryption_options(EncryptionOptions* options) { |
| 280 | // If we give the empty string, libfscrypt will use the default (currently XTS) |
| 281 | auto contents_mode = android::base::GetProperty("ro.crypto.volume.contents_mode", ""); |
| 282 | // HEH as default was always a mistake. Use the libfscrypt default (CTS) |
| 283 | // for devices launching on versions above Android 10. |
| 284 | auto first_api_level = GetFirstApiLevel(); |
| 285 | constexpr uint64_t pre_gki_level = 29; |
| 286 | auto filenames_mode = |
| 287 | android::base::GetProperty("ro.crypto.volume.filenames_mode", |
| 288 | first_api_level > pre_gki_level ? "" : "aes-256-heh"); |
| 289 | auto options_string = android::base::GetProperty("ro.crypto.volume.options", |
| 290 | contents_mode + ":" + filenames_mode); |
| 291 | if (!ParseOptionsForApiLevel(first_api_level, options_string, options)) { |
| 292 | LOG(ERROR) << "Unable to parse volume encryption options: " << options_string; |
| 293 | return false; |
| 294 | } |
| 295 | if (options->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) { |
| 296 | LOG(ERROR) << "The emmc_optimized encryption flag is only allowed on eMMC storage. Remove " |
| 297 | "this flag from ro.crypto.volume.options"; |
| 298 | return false; |
| 299 | } |
| 300 | return true; |
mauronofrio | 539597d | 2020-05-25 22:23:48 +0200 | [diff] [blame] | 301 | } |
| 302 | |
bigbiff | bcd23d3 | 2021-09-22 18:22:13 -0400 | [diff] [blame] | 303 | bool is_metadata_wrapped_key_supported() { |
bigbiff | 543a928 | 2021-10-02 09:50:58 -0400 | [diff] [blame] | 304 | if (fstab_default.empty()) { |
| 305 | if (!ReadDefaultFstab(&fstab_default)) { |
| 306 | PLOG(ERROR) << "Failed to open default fstab"; |
| 307 | return false; |
| 308 | } |
bigbiff | bcd23d3 | 2021-09-22 18:22:13 -0400 | [diff] [blame] | 309 | } |
| 310 | return GetEntryForMountPoint(&fstab_default, METADATA_MNT_POINT)->fs_mgr_flags.wrapped_key; |
| 311 | } |
| 312 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 313 | static bool read_and_install_user_ce_key(userid_t user_id, |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 314 | const ::KeyAuthentication& auth) { |
| 315 | if (s_ce_policies.count(user_id) != 0) return true; |
| 316 | EncryptionOptions options; |
| 317 | if (!get_data_file_encryption_options(&options)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 318 | KeyBuffer ce_key; |
| 319 | if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 320 | EncryptionPolicy ce_policy; |
| 321 | if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false; |
| 322 | s_ce_policies[user_id] = ce_policy; |
| 323 | LOG(INFO) << "Installed ce key for user " << user_id; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 324 | return true; |
| 325 | } |
| 326 | |
| 327 | static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 328 | LOG(INFO) << "Preparing: " << dir; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 329 | if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) { |
| 330 | PLOG(ERROR) << "Failed to prepare " << dir; |
| 331 | return false; |
| 332 | } |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | static bool destroy_dir(const std::string& dir) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 337 | LOG(INFO) << "Destroying: " << dir; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 338 | if (rmdir(dir.c_str()) != 0 && errno != ENOENT) { |
| 339 | PLOG(ERROR) << "Failed to destroy " << dir; |
| 340 | return false; |
| 341 | } |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | // NB this assumes that there is only one thread listening for crypt commands, because |
| 346 | // it creates keys in a fixed location. |
| 347 | static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 348 | LOG(INFO) << "fscrypt::create_and_install_user_keys"; |
| 349 | EncryptionOptions options; |
| 350 | if (!get_data_file_encryption_options(&options)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 351 | KeyBuffer de_key, ce_key; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 352 | if (!generateStorageKey(makeGen(options), &de_key)) return false; |
| 353 | if (!generateStorageKey(makeGen(options), &ce_key)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 354 | if (create_ephemeral) { |
| 355 | // If the key should be created as ephemeral, don't store it. |
| 356 | s_ephemeral_users.insert(user_id); |
| 357 | } else { |
| 358 | auto const directory_path = get_ce_key_directory_path(user_id); |
| 359 | if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false; |
| 360 | auto const paths = get_ce_key_paths(directory_path); |
| 361 | std::string ce_key_path; |
| 362 | if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 363 | if (!::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication, |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 364 | ce_key)) |
| 365 | return false; |
| 366 | fixate_user_ce_key(directory_path, ce_key_path, paths); |
| 367 | // Write DE key second; once this is written, all is good. |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 368 | if (!::storeKeyAtomically(get_de_key_path(user_id), user_key_temp, |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 369 | kEmptyAuthentication, de_key)) |
| 370 | return false; |
| 371 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 372 | EncryptionPolicy de_policy; |
| 373 | if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false; |
| 374 | s_de_policies[user_id] = de_policy; |
| 375 | LOG(INFO) << "fscrypt::added de_policy"; |
| 376 | EncryptionPolicy ce_policy; |
| 377 | if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false; |
| 378 | s_ce_policies[user_id] = ce_policy; |
| 379 | LOG(INFO) << "Created keys for user " << user_id; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 380 | return true; |
| 381 | } |
| 382 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 383 | bool lookup_policy(const std::map<userid_t, EncryptionPolicy>& key_map, userid_t user_id, |
| 384 | EncryptionPolicy* policy) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 385 | auto refi = key_map.find(user_id); |
| 386 | if (refi == key_map.end()) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 387 | LOG(ERROR) << "Cannot find key for " << user_id; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 388 | return false; |
| 389 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 390 | *policy = refi->second; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 391 | return true; |
| 392 | } |
| 393 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 394 | static bool is_numeric(const char* name) { |
| 395 | for (const char* p = name; *p != '\0'; p++) { |
| 396 | if (!isdigit(*p)) return false; |
| 397 | } |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | static bool load_all_de_keys() { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 402 | LOG(INFO) << "fscrypt::load_all_de_keys"; |
| 403 | EncryptionOptions options; |
| 404 | if (!get_data_file_encryption_options(&options)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 405 | auto de_dir = user_key_dir + "/de"; |
| 406 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir); |
| 407 | if (!dirp) { |
| 408 | PLOG(ERROR) << "Unable to read de key directory"; |
| 409 | return false; |
| 410 | } |
| 411 | for (;;) { |
| 412 | errno = 0; |
| 413 | auto entry = readdir(dirp.get()); |
| 414 | if (!entry) { |
| 415 | if (errno) { |
| 416 | PLOG(ERROR) << "Unable to read de key directory"; |
| 417 | return false; |
| 418 | } |
| 419 | break; |
| 420 | } |
| 421 | if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 422 | LOG(INFO) << "Skipping non-de-key " << entry->d_name; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 423 | continue; |
| 424 | } |
| 425 | userid_t user_id = std::stoi(entry->d_name); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 426 | auto key_path = de_dir + "/" + entry->d_name; |
| 427 | KeyBuffer de_key; |
bigbiff | bbbfe17 | 2021-05-30 15:52:41 -0400 | [diff] [blame] | 428 | if (!retrieveKey(key_path, kEmptyAuthentication, &de_key, true)) return false; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 429 | EncryptionPolicy de_policy; |
| 430 | if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false; |
| 431 | auto ret = s_de_policies.insert({user_id, de_policy}); |
| 432 | LOG(INFO) << "fscrypt::load_all_de_keys::s_de_policies::size::" << s_de_policies.size(); |
| 433 | if (!ret.second && ret.first->second != de_policy) { |
| 434 | LOG(INFO) << "DE policy for user" << user_id << " changed"; |
| 435 | return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 436 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 437 | LOG(INFO) << "Installed de key for user " << user_id; |
| 438 | std::string user_prop = "twrp.user." + std::to_string(user_id) + ".decrypt"; |
| 439 | property_set(user_prop.c_str(), "0"); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 440 | } |
| 441 | // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the |
| 442 | // correct policy set on them, and that no rogue ones exist. |
| 443 | return true; |
| 444 | } |
| 445 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 446 | // Attempt to reinstall CE keys for users that we think are unlocked. |
| 447 | static bool try_reload_ce_keys() { |
| 448 | for (const auto& it : s_ce_policies) { |
| 449 | if (!::reloadKeyFromSessionKeyring(DATA_MNT_POINT, it.second)) { |
| 450 | LOG(ERROR) << "Failed to load CE key from session keyring for user " << it.first; |
| 451 | return false; |
| 452 | } |
| 453 | } |
| 454 | return true; |
| 455 | } |
| 456 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 457 | bool fscrypt_initialize_systemwide_keys() { |
| 458 | LOG(INFO) << "fscrypt_initialize_systemwide_keys"; |
| 459 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 460 | EncryptionOptions options; |
| 461 | if (!get_data_file_encryption_options(&options)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 462 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 463 | KeyBuffer device_key; |
| 464 | if (!retrieveOrGenerateKey(device_key_path, device_key_temp, kEmptyAuthentication, |
| 465 | makeGen(options), &device_key)) |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 466 | return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 467 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 468 | EncryptionPolicy device_policy; |
| 469 | if (!install_storage_key(DATA_MNT_POINT, options, device_key, &device_policy)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 470 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 471 | std::string options_string; |
| 472 | if (!OptionsToString(device_policy.options, &options_string)) { |
| 473 | LOG(ERROR) << "Unable to serialize options"; |
| 474 | return false; |
| 475 | } |
| 476 | std::string options_filename = std::string(DATA_MNT_POINT) + fscrypt_key_mode; |
| 477 | if (!::writeStringToFile(options_string, options_filename)) return false; |
| 478 | |
| 479 | std::string ref_filename = std::string(DATA_MNT_POINT) + fscrypt_key_ref; |
| 480 | de_key_raw_ref = device_policy.key_raw_ref; |
| 481 | if (!::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 482 | LOG(INFO) << "Wrote system DE key reference to:" << ref_filename; |
| 483 | |
| 484 | KeyBuffer per_boot_key; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 485 | if (!generateStorageKey(makeGen(options), &per_boot_key)) return false; |
| 486 | EncryptionPolicy per_boot_policy; |
| 487 | if (!install_storage_key(DATA_MNT_POINT, options, per_boot_key, &per_boot_policy)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 488 | std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 489 | if (!::writeStringToFile(per_boot_policy.key_raw_ref, per_boot_ref_filename)) |
| 490 | return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 491 | LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename; |
| 492 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 493 | if (!::FsyncDirectory(device_key_dir)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 494 | return true; |
| 495 | } |
| 496 | |
| 497 | bool fscrypt_init_user0() { |
bigbiff | 543a928 | 2021-10-02 09:50:58 -0400 | [diff] [blame] | 498 | if (fstab_default.empty()) { |
| 499 | if (!ReadDefaultFstab(&fstab_default)) { |
| 500 | PLOG(ERROR) << "Failed to open default fstab"; |
| 501 | return -1; |
| 502 | } |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 503 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 504 | LOG(INFO) << "fscrypt_init_user0"; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 505 | if (fscrypt_is_native()) { |
| 506 | if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false; |
| 507 | if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false; |
| 508 | if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 509 | if (!::pathExists(get_de_key_path(0))) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 510 | if (!create_and_install_user_keys(0, false)) return false; |
| 511 | } |
| 512 | // TODO: switch to loading only DE_0 here once framework makes |
| 513 | // explicit calls to install DE keys for secondary users |
| 514 | if (!load_all_de_keys()) return false; |
| 515 | } |
| 516 | // We can only safely prepare DE storage here, since CE keys are probably |
| 517 | // entangled with user credentials. The framework will always prepare CE |
| 518 | // storage once CE keys are installed. |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 519 | LOG(INFO) << "attempting fscrypt_prepare_user_storage"; |
| 520 | if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 521 | LOG(ERROR) << "Failed to prepare user 0 storage"; |
| 522 | return false; |
| 523 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 524 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 525 | // If this is a non-FBE device that recently left an emulated mode, |
| 526 | // restore user data directories to known-good state. |
| 527 | if (!fscrypt_is_native() && !fscrypt_is_emulated()) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 528 | LOG(INFO) << "unlocking data media"; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 529 | fscrypt_unlock_user_key(0, 0, "!", "!"); |
| 530 | } |
| 531 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 532 | // In some scenarios (e.g. userspace reboot) we might unmount userdata |
| 533 | // without doing a hard reboot. If CE keys were stored in fs keyring then |
| 534 | // they will be lost after unmount. Attempt to re-install them. |
| 535 | if (fscrypt_is_native() && ::isFsKeyringSupported()) { |
| 536 | LOG(INFO) << "reloading ce keys"; |
| 537 | if (!try_reload_ce_keys()) return false; |
| 538 | } |
| 539 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 540 | return true; |
| 541 | } |
| 542 | |
| 543 | bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 544 | LOG(INFO) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 545 | if (!fscrypt_is_native()) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 546 | return true; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 547 | } |
| 548 | // FIXME test for existence of key that is not loaded yet |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 549 | if (s_ce_policies.count(user_id) != 0) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 550 | LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id |
| 551 | << " serial " << serial; |
| 552 | // FIXME should we fail the command? |
| 553 | return true; |
| 554 | } |
| 555 | if (!create_and_install_user_keys(user_id, ephemeral)) { |
| 556 | return false; |
| 557 | } |
| 558 | return true; |
| 559 | } |
| 560 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 561 | // "Lock" all encrypted directories whose key has been removed. This is needed |
| 562 | // in the case where the keys are being put in the session keyring (rather in |
| 563 | // the newer filesystem-level keyrings), because removing a key from the session |
| 564 | // keyring doesn't affect inodes in the kernel's inode cache whose per-file key |
| 565 | // was already set up. So to remove the per-file keys and make the files |
| 566 | // "appear encrypted", these inodes must be evicted. |
| 567 | // |
| 568 | // To do this, sync() to clean all dirty inodes, then drop all reclaimable slab |
| 569 | // objects systemwide. This is overkill, but it's the best available method |
| 570 | // currently. Don't use drop_caches mode "3" because that also evicts pagecache |
| 571 | // for in-use files; all files relevant here are already closed and sync'ed. |
| 572 | static void drop_caches_if_needed() { |
| 573 | if (::isFsKeyringSupported()) { |
| 574 | return; |
| 575 | } |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 576 | sync(); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 577 | if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 578 | PLOG(ERROR) << "Failed to drop caches during key eviction"; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | static bool evict_ce_key(userid_t user_id) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 583 | bool success = true; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 584 | EncryptionPolicy policy; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 585 | // If we haven't loaded the CE key, no need to evict it. |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 586 | if (lookup_policy(s_ce_policies, user_id, &policy)) { |
| 587 | success &= ::evictKey(DATA_MNT_POINT, policy); |
| 588 | drop_caches_if_needed(); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 589 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 590 | s_ce_policies.erase(user_id); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 591 | return success; |
| 592 | } |
| 593 | |
| 594 | bool fscrypt_destroy_user_key(userid_t user_id) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 595 | LOG(INFO) << "fscrypt_destroy_user_key(" << user_id << ")"; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 596 | if (!fscrypt_is_native()) { |
| 597 | return true; |
| 598 | } |
| 599 | bool success = true; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 600 | success &= evict_ce_key(user_id); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 601 | EncryptionPolicy de_policy; |
| 602 | success &= lookup_policy(s_de_policies, user_id, &de_policy) && |
| 603 | ::evictKey(DATA_MNT_POINT, de_policy); |
| 604 | s_de_policies.erase(user_id); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 605 | auto it = s_ephemeral_users.find(user_id); |
| 606 | if (it != s_ephemeral_users.end()) { |
| 607 | s_ephemeral_users.erase(it); |
| 608 | } else { |
| 609 | for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 610 | success &= ::destroyKey(path); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 611 | } |
| 612 | auto de_key_path = get_de_key_path(user_id); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 613 | if (::pathExists(de_key_path)) { |
| 614 | success &= ::destroyKey(de_key_path); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 615 | } else { |
| 616 | LOG(INFO) << "Not present so not erasing: " << de_key_path; |
| 617 | } |
| 618 | } |
| 619 | return success; |
| 620 | } |
| 621 | |
| 622 | static bool emulated_lock(const std::string& path) { |
| 623 | if (chmod(path.c_str(), 0000) != 0) { |
| 624 | PLOG(ERROR) << "Failed to chmod " << path; |
| 625 | return false; |
| 626 | } |
| 627 | #if EMULATED_USES_SELINUX |
| 628 | if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) { |
| 629 | PLOG(WARNING) << "Failed to setfilecon " << path; |
| 630 | return false; |
| 631 | } |
| 632 | #endif |
| 633 | return true; |
| 634 | } |
| 635 | |
| 636 | static bool emulated_unlock(const std::string& path, mode_t mode) { |
| 637 | if (chmod(path.c_str(), mode) != 0) { |
| 638 | PLOG(ERROR) << "Failed to chmod " << path; |
| 639 | // FIXME temporary workaround for b/26713622 |
| 640 | if (fscrypt_is_emulated()) return false; |
| 641 | } |
| 642 | #if EMULATED_USES_SELINUX |
| 643 | if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) { |
| 644 | PLOG(WARNING) << "Failed to restorecon " << path; |
| 645 | // FIXME temporary workaround for b/26713622 |
| 646 | if (fscrypt_is_emulated()) return false; |
| 647 | } |
| 648 | #endif |
| 649 | return true; |
| 650 | } |
| 651 | |
| 652 | static bool parse_hex(const std::string& hex, std::string* result) { |
| 653 | if (hex == "!") { |
| 654 | *result = ""; |
| 655 | return true; |
| 656 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 657 | if (::HexToStr(hex, *result) != 0) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 658 | LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons |
| 659 | return false; |
| 660 | } |
| 661 | return true; |
| 662 | } |
| 663 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 664 | static std::optional<::KeyAuthentication> authentication_from_hex( |
| 665 | const std::string& token_hex, const std::string& secret_hex) { |
| 666 | std::string token, secret; |
| 667 | if (!parse_hex(token_hex, &token)) return std::optional<::KeyAuthentication>(); |
| 668 | if (!parse_hex(secret_hex, &secret)) return std::optional<::KeyAuthentication>(); |
| 669 | if (secret.empty()) { |
| 670 | return kEmptyAuthentication; |
| 671 | } else { |
| 672 | return ::KeyAuthentication(token, secret); |
| 673 | } |
| 674 | } |
| 675 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 676 | static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) { |
| 677 | return misc_path + "/vold/volume_keys/" + volume_uuid + "/default"; |
| 678 | } |
| 679 | |
| 680 | static std::string volume_secdiscardable_path(const std::string& volume_uuid) { |
| 681 | return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable"; |
| 682 | } |
| 683 | |
| 684 | static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid, |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 685 | EncryptionPolicy* policy) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 686 | auto secdiscardable_path = volume_secdiscardable_path(volume_uuid); |
| 687 | std::string secdiscardable_hash; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 688 | if (::pathExists(secdiscardable_path)) { |
| 689 | if (!readSecdiscardable(secdiscardable_path, &secdiscardable_hash)) |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 690 | return false; |
| 691 | } else { |
| 692 | if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) { |
| 693 | PLOG(ERROR) << "Creating directories for: " << secdiscardable_path; |
| 694 | return false; |
| 695 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 696 | if (!::createSecdiscardable(secdiscardable_path, &secdiscardable_hash)) |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 697 | return false; |
| 698 | } |
| 699 | auto key_path = volkey_path(misc_path, volume_uuid); |
| 700 | if (fs_mkdirs(key_path.c_str(), 0700) != 0) { |
| 701 | PLOG(ERROR) << "Creating directories for: " << key_path; |
| 702 | return false; |
| 703 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 704 | ::KeyAuthentication auth("", secdiscardable_hash); |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 705 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 706 | EncryptionOptions options; |
| 707 | if (!get_volume_file_encryption_options(&options)) return false; |
| 708 | KeyBuffer key; |
| 709 | if (!retrieveOrGenerateKey(key_path, key_path + "_tmp", auth, makeGen(options), &key)) |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 710 | return false; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 711 | if (!install_storage_key(BuildDataPath(volume_uuid), options, key, policy)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 712 | return true; |
| 713 | } |
| 714 | |
| 715 | static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) { |
| 716 | auto path = volkey_path(misc_path, volume_uuid); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 717 | if (!::pathExists(path)) return true; |
| 718 | return ::destroyKey(path); |
| 719 | } |
| 720 | |
| 721 | static bool fscrypt_rewrap_user_key(userid_t user_id, int serial, |
| 722 | const ::KeyAuthentication& retrieve_auth, |
| 723 | const ::KeyAuthentication& store_auth) { |
| 724 | if (s_ephemeral_users.count(user_id) != 0) return true; |
| 725 | auto const directory_path = get_ce_key_directory_path(user_id); |
| 726 | KeyBuffer ce_key; |
| 727 | std::string ce_key_current_path = get_ce_key_current_path(directory_path); |
bigbiff | bbbfe17 | 2021-05-30 15:52:41 -0400 | [diff] [blame] | 728 | if (retrieveKey(ce_key_current_path, retrieve_auth, &ce_key, true)) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 729 | LOG(INFO) << "Successfully retrieved key"; |
| 730 | // TODO(147732812): Remove this once Locksettingservice is fixed. |
| 731 | // Currently it calls fscrypt_clear_user_key_auth with a secret when lockscreen is |
| 732 | // changed from swipe to none or vice-versa |
bigbiff | bbbfe17 | 2021-05-30 15:52:41 -0400 | [diff] [blame] | 733 | } else if (retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key, true)) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 734 | LOG(INFO) << "Successfully retrieved key with empty auth"; |
| 735 | } else { |
| 736 | LOG(ERROR) << "Failed to retrieve key for user " << user_id; |
| 737 | return false; |
| 738 | } |
| 739 | auto const paths = get_ce_key_paths(directory_path); |
| 740 | std::string ce_key_path; |
| 741 | if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false; |
| 742 | if (!::storeKeyAtomically(ce_key_path, user_key_temp, store_auth, ce_key)) |
| 743 | return false; |
| 744 | if (!::FsyncDirectory(directory_path)) return false; |
| 745 | return true; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex, |
| 749 | const std::string& secret_hex) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 750 | LOG(INFO) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 751 | << " token_present=" << (token_hex != "!"); |
| 752 | if (!fscrypt_is_native()) return true; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 753 | auto auth = authentication_from_hex(token_hex, secret_hex); |
| 754 | if (!auth) return false; |
| 755 | return fscrypt_rewrap_user_key(user_id, serial, kEmptyAuthentication, *auth); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 756 | } |
| 757 | |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 758 | bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex, |
| 759 | const std::string& secret_hex) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 760 | LOG(INFO) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 761 | << " token_present=" << (token_hex != "!"); |
| 762 | if (!fscrypt_is_native()) return true; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 763 | auto auth = authentication_from_hex(token_hex, secret_hex); |
| 764 | if (!auth) return false; |
| 765 | return fscrypt_rewrap_user_key(user_id, serial, *auth, kEmptyAuthentication); |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 766 | } |
| 767 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 768 | bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 769 | LOG(INFO) << "fscrypt_fixate_newest_user_key_auth " << user_id; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 770 | if (!fscrypt_is_native()) return true; |
| 771 | if (s_ephemeral_users.count(user_id) != 0) return true; |
| 772 | auto const directory_path = get_ce_key_directory_path(user_id); |
| 773 | auto const paths = get_ce_key_paths(directory_path); |
| 774 | if (paths.empty()) { |
| 775 | LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id; |
| 776 | return false; |
| 777 | } |
| 778 | fixate_user_ce_key(directory_path, paths[0], paths); |
| 779 | return true; |
| 780 | } |
| 781 | |
| 782 | // TODO: rename to 'install' for consistency, and take flags to know which keys to install |
| 783 | bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex, |
| 784 | const std::string& secret_hex) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 785 | LOG(INFO) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 786 | << " token_present=" << (token_hex != "!"); |
| 787 | if (fscrypt_is_native()) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 788 | if (s_ce_policies.count(user_id) != 0) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 789 | LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id; |
| 790 | return true; |
| 791 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 792 | auto auth = authentication_from_hex(token_hex, secret_hex); |
| 793 | if (!auth) return false; |
| 794 | if (!read_and_install_user_ce_key(user_id, *auth)) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 795 | LOG(ERROR) << "Couldn't read key for " << user_id; |
| 796 | return false; |
| 797 | } |
| 798 | } else { |
| 799 | // When in emulation mode, we just use chmod. However, we also |
| 800 | // unlock directories when not in emulation mode, to bring devices |
| 801 | // back into a known-good state. |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 802 | if (!emulated_unlock(::BuildDataSystemCePath(user_id), 0771) || |
| 803 | !emulated_unlock(::BuildDataMiscCePath(user_id), 01771) || |
| 804 | !emulated_unlock(::BuildDataMediaCePath("", user_id), 0770) || |
| 805 | !emulated_unlock(::BuildDataUserCePath("", user_id), 0771)) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 806 | LOG(ERROR) << "Failed to unlock user " << user_id; |
| 807 | return false; |
| 808 | } |
| 809 | } |
| 810 | return true; |
| 811 | } |
| 812 | |
| 813 | // TODO: rename to 'evict' for consistency |
| 814 | bool fscrypt_lock_user_key(userid_t user_id) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 815 | LOG(INFO) << "fscrypt_lock_user_key " << user_id; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 816 | if (fscrypt_is_native()) { |
| 817 | return evict_ce_key(user_id); |
| 818 | } else if (fscrypt_is_emulated()) { |
| 819 | // When in emulation mode, we just use chmod |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 820 | if (!emulated_lock(::BuildDataSystemCePath(user_id)) || |
| 821 | !emulated_lock(::BuildDataMiscCePath(user_id)) || |
| 822 | !emulated_lock(::BuildDataMediaCePath("", user_id)) || |
| 823 | !emulated_lock(::BuildDataUserCePath("", user_id))) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 824 | LOG(ERROR) << "Failed to lock user " << user_id; |
| 825 | return false; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | return true; |
| 830 | } |
| 831 | |
| 832 | static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid, |
| 833 | userid_t user_id, int flags) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 834 | if (0 != ::ForkExecvp( |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 835 | std::vector<std::string>{prepare_subdirs_path, action, volume_uuid, |
| 836 | std::to_string(user_id), std::to_string(flags)})) { |
| 837 | LOG(ERROR) << "vold_prepare_subdirs failed"; |
| 838 | return false; |
| 839 | } |
| 840 | return true; |
| 841 | } |
| 842 | |
| 843 | bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial, |
| 844 | int flags) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 845 | LOG(INFO) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid) |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 846 | << ", user " << user_id << ", serial " << serial << ", flags " << flags; |
| 847 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 848 | if (flags & android::os::IVold::STORAGE_FLAG_DE) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 849 | // DE_sys key |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 850 | auto system_legacy_path = ::BuildDataSystemLegacyPath(user_id); |
| 851 | auto misc_legacy_path = ::BuildDataMiscLegacyPath(user_id); |
| 852 | auto profiles_de_path = ::BuildDataProfilesDePath(user_id); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 853 | |
| 854 | // DE_n key |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 855 | auto system_de_path = ::BuildDataSystemDePath(user_id); |
| 856 | auto misc_de_path = ::BuildDataMiscDePath(user_id); |
| 857 | auto vendor_de_path = ::BuildDataVendorDePath(user_id); |
| 858 | auto user_de_path = ::BuildDataUserDePath(volume_uuid, user_id); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 859 | |
| 860 | if (volume_uuid.empty()) { |
| 861 | if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false; |
| 862 | #if MANAGE_MISC_DIRS |
| 863 | if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM), |
| 864 | multiuser_get_uid(user_id, AID_EVERYBODY))) |
| 865 | return false; |
| 866 | #endif |
| 867 | if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false; |
| 868 | |
| 869 | if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false; |
| 870 | if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false; |
| 871 | if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false; |
| 872 | } |
| 873 | if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 874 | if (fscrypt_is_native()) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 875 | EncryptionPolicy de_policy; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 876 | if (volume_uuid.empty()) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 877 | if (!lookup_policy(s_de_policies, user_id, &de_policy)) return false; |
| 878 | if (!EnsurePolicy(de_policy, system_de_path)) return false; |
| 879 | if (!EnsurePolicy(de_policy, misc_de_path)) return false; |
| 880 | if (!EnsurePolicy(de_policy, vendor_de_path)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 881 | } else { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 882 | if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 883 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 884 | if (!EnsurePolicy(de_policy, user_de_path)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 885 | } |
| 886 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 887 | |
| 888 | if (flags & android::os::IVold::STORAGE_FLAG_CE) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 889 | // CE_n key |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 890 | auto system_ce_path = ::BuildDataSystemCePath(user_id); |
| 891 | auto misc_ce_path = ::BuildDataMiscCePath(user_id); |
| 892 | auto vendor_ce_path = ::BuildDataVendorCePath(user_id); |
| 893 | auto media_ce_path = ::BuildDataMediaCePath(volume_uuid, user_id); |
| 894 | auto user_ce_path = ::BuildDataUserCePath(volume_uuid, user_id); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 895 | |
| 896 | if (volume_uuid.empty()) { |
| 897 | if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false; |
| 898 | if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false; |
| 899 | if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false; |
| 900 | } |
| 901 | if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 902 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 903 | if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false; |
| 904 | |
| 905 | if (fscrypt_is_native()) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 906 | EncryptionPolicy ce_policy; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 907 | if (volume_uuid.empty()) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 908 | if (!lookup_policy(s_ce_policies, user_id, &ce_policy)) return false; |
| 909 | if (!EnsurePolicy(ce_policy, system_ce_path)) return false; |
| 910 | if (!EnsurePolicy(ce_policy, misc_ce_path)) return false; |
| 911 | if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 912 | } else { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 913 | if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 914 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 915 | if (!EnsurePolicy(ce_policy, media_ce_path)) return false; |
| 916 | if (!EnsurePolicy(ce_policy, user_ce_path)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | if (volume_uuid.empty()) { |
| 920 | // Now that credentials have been installed, we can run restorecon |
| 921 | // over these paths |
| 922 | // NOTE: these paths need to be kept in sync with libselinux |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 923 | ::RestoreconRecursive(system_ce_path); |
| 924 | ::RestoreconRecursive(vendor_ce_path); |
| 925 | ::RestoreconRecursive(misc_ce_path); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false; |
| 929 | |
| 930 | return true; |
| 931 | } |
| 932 | |
| 933 | bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 934 | LOG(INFO) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid) |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 935 | << ", user " << user_id << ", flags " << flags; |
| 936 | bool res = true; |
| 937 | |
| 938 | res &= prepare_subdirs("destroy", volume_uuid, user_id, flags); |
| 939 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 940 | if (flags & android::os::IVold::STORAGE_FLAG_CE) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 941 | // CE_n key |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 942 | auto system_ce_path = ::BuildDataSystemCePath(user_id); |
| 943 | auto misc_ce_path = ::BuildDataMiscCePath(user_id); |
| 944 | auto vendor_ce_path = ::BuildDataVendorCePath(user_id); |
| 945 | auto media_ce_path = ::BuildDataMediaCePath(volume_uuid, user_id); |
| 946 | auto user_ce_path = ::BuildDataUserCePath(volume_uuid, user_id); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 947 | |
| 948 | res &= destroy_dir(media_ce_path); |
| 949 | res &= destroy_dir(user_ce_path); |
| 950 | if (volume_uuid.empty()) { |
| 951 | res &= destroy_dir(system_ce_path); |
| 952 | res &= destroy_dir(misc_ce_path); |
| 953 | res &= destroy_dir(vendor_ce_path); |
| 954 | } else { |
| 955 | if (fscrypt_is_native()) { |
| 956 | res &= destroy_volkey(misc_ce_path, volume_uuid); |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 961 | if (flags & android::os::IVold::STORAGE_FLAG_DE) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 962 | // DE_sys key |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 963 | auto system_legacy_path = ::BuildDataSystemLegacyPath(user_id); |
| 964 | auto misc_legacy_path = ::BuildDataMiscLegacyPath(user_id); |
| 965 | auto profiles_de_path = ::BuildDataProfilesDePath(user_id); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 966 | |
| 967 | // DE_n key |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 968 | auto system_de_path = ::BuildDataSystemDePath(user_id); |
| 969 | auto misc_de_path = ::BuildDataMiscDePath(user_id); |
| 970 | auto vendor_de_path = ::BuildDataVendorDePath(user_id); |
| 971 | auto user_de_path = ::BuildDataUserDePath(volume_uuid, user_id); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 972 | |
| 973 | res &= destroy_dir(user_de_path); |
| 974 | if (volume_uuid.empty()) { |
| 975 | res &= destroy_dir(system_legacy_path); |
| 976 | #if MANAGE_MISC_DIRS |
| 977 | res &= destroy_dir(misc_legacy_path); |
| 978 | #endif |
| 979 | res &= destroy_dir(profiles_de_path); |
| 980 | res &= destroy_dir(system_de_path); |
| 981 | res &= destroy_dir(misc_de_path); |
| 982 | res &= destroy_dir(vendor_de_path); |
| 983 | } else { |
| 984 | if (fscrypt_is_native()) { |
| 985 | res &= destroy_volkey(misc_de_path, volume_uuid); |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | return res; |
| 991 | } |
| 992 | |
| 993 | static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) { |
| 994 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir); |
| 995 | if (!dirp) { |
| 996 | PLOG(ERROR) << "Unable to open directory: " + directory_path; |
| 997 | return false; |
| 998 | } |
| 999 | bool res = true; |
| 1000 | for (;;) { |
| 1001 | errno = 0; |
| 1002 | auto const entry = readdir(dirp.get()); |
| 1003 | if (!entry) { |
| 1004 | if (errno) { |
| 1005 | PLOG(ERROR) << "Unable to read directory: " + directory_path; |
| 1006 | return false; |
| 1007 | } |
| 1008 | break; |
| 1009 | } |
| 1010 | if (entry->d_type != DT_DIR || entry->d_name[0] == '.') { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 1011 | LOG(INFO) << "Skipping non-user " << entry->d_name; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 1012 | continue; |
| 1013 | } |
| 1014 | res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid); |
| 1015 | } |
| 1016 | return res; |
| 1017 | } |
| 1018 | |
| 1019 | bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) { |
| 1020 | bool res = true; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 1021 | LOG(INFO) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 1022 | auto secdiscardable_path = volume_secdiscardable_path(volume_uuid); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 1023 | res &= ::runSecdiscardSingle(secdiscardable_path); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 1024 | res &= destroy_volume_keys("/data/misc_ce", volume_uuid); |
| 1025 | res &= destroy_volume_keys("/data/misc_de", volume_uuid); |
| 1026 | return res; |
| 1027 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 1028 | |
| 1029 | bool lookup_key_ref(const std::map<userid_t, android::fscrypt::EncryptionPolicy>& key_map, userid_t user_id, |
| 1030 | std::string* raw_ref) { |
| 1031 | auto refi = key_map.find(user_id); |
| 1032 | if (refi == key_map.end()) { |
| 1033 | LOG(ERROR) << "Cannot find key for " << user_id; |
| 1034 | return false; |
| 1035 | } |
| 1036 | *raw_ref = refi->second.key_raw_ref; |
| 1037 | return true; |
| 1038 | } |