Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [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 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 17 | #include "Ext4Crypt.h" |
| 18 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 19 | #include "KeyStorage.h" |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 20 | #include "Utils.h" |
| 21 | |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 22 | #include <iomanip> |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 23 | #include <map> |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 24 | #include <set> |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <sstream> |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 27 | |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 29 | #include <errno.h> |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 30 | #include <dirent.h> |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 31 | #include <sys/mount.h> |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 32 | #include <sys/types.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <fcntl.h> |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 35 | #include <cutils/properties.h> |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 36 | #include <openssl/sha.h> |
Jeff Sharkey | 7a9dd95 | 2016-01-12 16:52:16 -0700 | [diff] [blame] | 37 | #include <selinux/android.h> |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 38 | |
Paul Crowley | 480fcd2 | 2015-08-24 14:53:28 +0100 | [diff] [blame] | 39 | #include <private/android_filesystem_config.h> |
| 40 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 41 | #include "unencrypted_properties.h" |
| 42 | #include "key_control.h" |
| 43 | #include "cryptfs.h" |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 44 | #include "ext4_crypt_init_extensions.h" |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 45 | |
| 46 | #define LOG_TAG "Ext4Crypt" |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 47 | |
Jeff Sharkey | 7a9dd95 | 2016-01-12 16:52:16 -0700 | [diff] [blame] | 48 | #define EMULATED_USES_SELINUX 0 |
| 49 | |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 50 | #include <cutils/fs.h> |
| 51 | #include <cutils/log.h> |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 52 | #include <cutils/klog.h> |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 53 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 54 | #include <android-base/file.h> |
Elliott Hughes | 6bf0547 | 2015-12-04 17:55:33 -0800 | [diff] [blame] | 55 | #include <android-base/logging.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 56 | #include <android-base/stringprintf.h> |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 57 | |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 58 | using android::base::StringPrintf; |
| 59 | |
Jeff Sharkey | fc505c3 | 2015-12-07 17:27:01 -0700 | [diff] [blame] | 60 | static bool e4crypt_is_native() { |
| 61 | char value[PROPERTY_VALUE_MAX]; |
| 62 | property_get("ro.crypto.type", value, "none"); |
| 63 | return !strcmp(value, "file"); |
| 64 | } |
| 65 | |
| 66 | static bool e4crypt_is_emulated() { |
| 67 | return property_get_bool("persist.sys.emulate_fbe", false); |
| 68 | } |
Jeff Sharkey | c79fb89 | 2015-11-12 20:18:02 -0800 | [diff] [blame] | 69 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 70 | namespace { |
| 71 | // Key length in bits |
| 72 | const int key_length = 128; |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 73 | static_assert(key_length % 8 == 0, |
| 74 | "Key length must be multiple of 8 bits"); |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 75 | |
Paul Lawrence | 86c942a | 2015-05-06 13:53:43 -0700 | [diff] [blame] | 76 | // How long do we store passwords for? |
| 77 | const int password_max_age_seconds = 60; |
| 78 | |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 79 | const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys"; |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 80 | const std::string user_key_temp = user_key_dir + "/temp"; |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 81 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 82 | // How is device encrypted |
| 83 | struct keys { |
| 84 | std::string master_key; |
| 85 | std::string password; |
Paul Lawrence | 86c942a | 2015-05-06 13:53:43 -0700 | [diff] [blame] | 86 | time_t expiry_time; |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 87 | }; |
| 88 | std::map<std::string, keys> s_key_store; |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 89 | // Some users are ephemeral, don't try to wipe their keys from disk |
| 90 | std::set<userid_t> s_ephemeral_users; |
| 91 | // Map user ids to key references |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 92 | std::map<userid_t, std::string> s_de_key_raw_refs; |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 93 | std::map<userid_t, std::string> s_ce_key_raw_refs; |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 94 | |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 95 | // ext4enc:TODO get this const from somewhere good |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 96 | const int EXT4_KEY_DESCRIPTOR_SIZE = 8; |
| 97 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 98 | // ext4enc:TODO Include structure from somewhere sensible |
| 99 | // MUST be in sync with ext4_crypto.c in kernel |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 100 | const int EXT4_MAX_KEY_SIZE = 64; |
| 101 | const int EXT4_ENCRYPTION_MODE_AES_256_XTS = 1; |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 102 | struct ext4_encryption_key { |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 103 | uint32_t mode; |
| 104 | char raw[EXT4_MAX_KEY_SIZE]; |
| 105 | uint32_t size; |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | namespace tag { |
| 109 | const char* magic = "magic"; |
| 110 | const char* major_version = "major_version"; |
| 111 | const char* minor_version = "minor_version"; |
| 112 | const char* flags = "flags"; |
| 113 | const char* crypt_type = "crypt_type"; |
| 114 | const char* failed_decrypt_count = "failed_decrypt_count"; |
| 115 | const char* crypto_type_name = "crypto_type_name"; |
| 116 | const char* master_key = "master_key"; |
| 117 | const char* salt = "salt"; |
| 118 | const char* kdf_type = "kdf_type"; |
| 119 | const char* N_factor = "N_factor"; |
| 120 | const char* r_factor = "r_factor"; |
| 121 | const char* p_factor = "p_factor"; |
| 122 | const char* keymaster_blob = "keymaster_blob"; |
| 123 | const char* scrypted_intermediate_key = "scrypted_intermediate_key"; |
| 124 | } |
| 125 | } |
| 126 | |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 127 | static bool install_key(const std::string &key, std::string &raw_ref); |
Paul Crowley | f25a35a | 2015-05-06 13:38:53 +0100 | [diff] [blame] | 128 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 129 | static int put_crypt_ftr_and_key(const crypt_mnt_ftr& crypt_ftr, |
| 130 | UnencryptedProperties& props) |
| 131 | { |
| 132 | SLOGI("Putting crypt footer"); |
| 133 | |
| 134 | bool success = props.Set<int>(tag::magic, crypt_ftr.magic) |
| 135 | && props.Set<int>(tag::major_version, crypt_ftr.major_version) |
| 136 | && props.Set<int>(tag::minor_version, crypt_ftr.minor_version) |
| 137 | && props.Set<int>(tag::flags, crypt_ftr.flags) |
| 138 | && props.Set<int>(tag::crypt_type, crypt_ftr.crypt_type) |
| 139 | && props.Set<int>(tag::failed_decrypt_count, |
| 140 | crypt_ftr.failed_decrypt_count) |
| 141 | && props.Set<std::string>(tag::crypto_type_name, |
| 142 | std::string(reinterpret_cast<const char*>(crypt_ftr.crypto_type_name))) |
| 143 | && props.Set<std::string>(tag::master_key, |
| 144 | std::string((const char*) crypt_ftr.master_key, |
| 145 | crypt_ftr.keysize)) |
| 146 | && props.Set<std::string>(tag::salt, |
| 147 | std::string((const char*) crypt_ftr.salt, |
| 148 | SALT_LEN)) |
| 149 | && props.Set<int>(tag::kdf_type, crypt_ftr.kdf_type) |
| 150 | && props.Set<int>(tag::N_factor, crypt_ftr.N_factor) |
| 151 | && props.Set<int>(tag::r_factor, crypt_ftr.r_factor) |
| 152 | && props.Set<int>(tag::p_factor, crypt_ftr.p_factor) |
| 153 | && props.Set<std::string>(tag::keymaster_blob, |
| 154 | std::string((const char*) crypt_ftr.keymaster_blob, |
| 155 | crypt_ftr.keymaster_blob_size)) |
| 156 | && props.Set<std::string>(tag::scrypted_intermediate_key, |
| 157 | std::string((const char*) crypt_ftr.scrypted_intermediate_key, |
| 158 | SCRYPT_LEN)); |
| 159 | return success ? 0 : -1; |
| 160 | } |
| 161 | |
| 162 | static int get_crypt_ftr_and_key(crypt_mnt_ftr& crypt_ftr, |
| 163 | const UnencryptedProperties& props) |
| 164 | { |
| 165 | memset(&crypt_ftr, 0, sizeof(crypt_ftr)); |
| 166 | crypt_ftr.magic = props.Get<int>(tag::magic); |
| 167 | crypt_ftr.major_version = props.Get<int>(tag::major_version); |
| 168 | crypt_ftr.minor_version = props.Get<int>(tag::minor_version); |
Paul Lawrence | 0d9cd9e | 2015-05-05 15:58:27 -0700 | [diff] [blame] | 169 | crypt_ftr.ftr_size = sizeof(crypt_ftr); |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 170 | crypt_ftr.flags = props.Get<int>(tag::flags); |
| 171 | crypt_ftr.crypt_type = props.Get<int>(tag::crypt_type); |
| 172 | crypt_ftr.failed_decrypt_count = props.Get<int>(tag::failed_decrypt_count); |
| 173 | std::string crypto_type_name = props.Get<std::string>(tag::crypto_type_name); |
| 174 | strlcpy(reinterpret_cast<char*>(crypt_ftr.crypto_type_name), |
| 175 | crypto_type_name.c_str(), |
| 176 | sizeof(crypt_ftr.crypto_type_name)); |
| 177 | std::string master_key = props.Get<std::string>(tag::master_key); |
| 178 | crypt_ftr.keysize = master_key.size(); |
| 179 | if (crypt_ftr.keysize > sizeof(crypt_ftr.master_key)) { |
| 180 | SLOGE("Master key size too long"); |
| 181 | return -1; |
| 182 | } |
| 183 | memcpy(crypt_ftr.master_key, &master_key[0], crypt_ftr.keysize); |
| 184 | std::string salt = props.Get<std::string>(tag::salt); |
| 185 | if (salt.size() != SALT_LEN) { |
| 186 | SLOGE("Salt wrong length"); |
| 187 | return -1; |
| 188 | } |
| 189 | memcpy(crypt_ftr.salt, &salt[0], SALT_LEN); |
| 190 | crypt_ftr.kdf_type = props.Get<int>(tag::kdf_type); |
| 191 | crypt_ftr.N_factor = props.Get<int>(tag::N_factor); |
| 192 | crypt_ftr.r_factor = props.Get<int>(tag::r_factor); |
| 193 | crypt_ftr.p_factor = props.Get<int>(tag::p_factor); |
| 194 | std::string keymaster_blob = props.Get<std::string>(tag::keymaster_blob); |
| 195 | crypt_ftr.keymaster_blob_size = keymaster_blob.size(); |
| 196 | if (crypt_ftr.keymaster_blob_size > sizeof(crypt_ftr.keymaster_blob)) { |
| 197 | SLOGE("Keymaster blob too long"); |
| 198 | return -1; |
| 199 | } |
| 200 | memcpy(crypt_ftr.keymaster_blob, &keymaster_blob[0], |
| 201 | crypt_ftr.keymaster_blob_size); |
| 202 | std::string scrypted_intermediate_key = props.Get<std::string>(tag::scrypted_intermediate_key); |
| 203 | if (scrypted_intermediate_key.size() != SCRYPT_LEN) { |
| 204 | SLOGE("scrypted intermediate key wrong length"); |
| 205 | return -1; |
| 206 | } |
| 207 | memcpy(crypt_ftr.scrypted_intermediate_key, &scrypted_intermediate_key[0], |
| 208 | SCRYPT_LEN); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static UnencryptedProperties GetProps(const char* path) |
| 214 | { |
| 215 | return UnencryptedProperties(path); |
| 216 | } |
| 217 | |
| 218 | static UnencryptedProperties GetAltProps(const char* path) |
| 219 | { |
| 220 | return UnencryptedProperties((std::string() + path + "/tmp_mnt").c_str()); |
| 221 | } |
| 222 | |
| 223 | static UnencryptedProperties GetPropsOrAltProps(const char* path) |
| 224 | { |
| 225 | UnencryptedProperties props = GetProps(path); |
| 226 | if (props.OK()) { |
| 227 | return props; |
| 228 | } |
| 229 | return GetAltProps(path); |
| 230 | } |
| 231 | |
| 232 | int e4crypt_enable(const char* path) |
| 233 | { |
| 234 | // Already enabled? |
| 235 | if (s_key_store.find(path) != s_key_store.end()) { |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | // Not an encryptable device? |
| 240 | UnencryptedProperties key_props = GetProps(path).GetChild(properties::key); |
| 241 | if (!key_props.OK()) { |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | if (key_props.Get<std::string>(tag::master_key).empty()) { |
| 246 | crypt_mnt_ftr ftr; |
| 247 | if (cryptfs_create_default_ftr(&ftr, key_length)) { |
| 248 | SLOGE("Failed to create crypto footer"); |
| 249 | return -1; |
| 250 | } |
| 251 | |
Paul Lawrence | 0d9cd9e | 2015-05-05 15:58:27 -0700 | [diff] [blame] | 252 | // Scrub fields not used by ext4enc |
| 253 | ftr.persist_data_offset[0] = 0; |
| 254 | ftr.persist_data_offset[1] = 0; |
| 255 | ftr.persist_data_size = 0; |
| 256 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 257 | if (put_crypt_ftr_and_key(ftr, key_props)) { |
| 258 | SLOGE("Failed to write crypto footer"); |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | crypt_mnt_ftr ftr2; |
| 263 | if (get_crypt_ftr_and_key(ftr2, key_props)) { |
| 264 | SLOGE("Failed to read crypto footer back"); |
| 265 | return -1; |
| 266 | } |
| 267 | |
| 268 | if (memcmp(&ftr, &ftr2, sizeof(ftr)) != 0) { |
| 269 | SLOGE("Crypto footer not correctly written"); |
Paul Lawrence | 0d9cd9e | 2015-05-05 15:58:27 -0700 | [diff] [blame] | 270 | return -1; |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | if (!UnencryptedProperties(path).Remove(properties::ref)) { |
| 275 | SLOGE("Failed to remove key ref"); |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | return e4crypt_check_passwd(path, ""); |
| 280 | } |
| 281 | |
| 282 | int e4crypt_change_password(const char* path, int crypt_type, |
| 283 | const char* password) |
| 284 | { |
| 285 | SLOGI("e4crypt_change_password"); |
Paul Lawrence | a56d313 | 2015-05-04 15:48:24 -0700 | [diff] [blame] | 286 | auto key_props = GetProps(path).GetChild(properties::key); |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 287 | |
| 288 | crypt_mnt_ftr ftr; |
| 289 | if (get_crypt_ftr_and_key(ftr, key_props)) { |
| 290 | SLOGE("Failed to read crypto footer back"); |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | auto mki = s_key_store.find(path); |
| 295 | if (mki == s_key_store.end()) { |
| 296 | SLOGE("No stored master key - can't change password"); |
| 297 | return -1; |
| 298 | } |
| 299 | |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 300 | const unsigned char* master_key_bytes |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 301 | = reinterpret_cast<const unsigned char*>(&mki->second.master_key[0]); |
| 302 | |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 303 | if (cryptfs_set_password(&ftr, password, master_key_bytes)) { |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 304 | SLOGE("Failed to set password"); |
| 305 | return -1; |
| 306 | } |
| 307 | |
| 308 | ftr.crypt_type = crypt_type; |
| 309 | |
| 310 | if (put_crypt_ftr_and_key(ftr, key_props)) { |
| 311 | SLOGE("Failed to write crypto footer"); |
| 312 | return -1; |
| 313 | } |
| 314 | |
| 315 | if (!UnencryptedProperties(path).Set(properties::is_default, |
| 316 | crypt_type == CRYPT_TYPE_DEFAULT)) { |
| 317 | SLOGE("Failed to update default flag"); |
| 318 | return -1; |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | int e4crypt_crypto_complete(const char* path) |
| 325 | { |
| 326 | SLOGI("ext4 crypto complete called on %s", path); |
Paul Lawrence | a56d313 | 2015-05-04 15:48:24 -0700 | [diff] [blame] | 327 | auto key_props = GetPropsOrAltProps(path).GetChild(properties::key); |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 328 | if (key_props.Get<std::string>(tag::master_key).empty()) { |
| 329 | SLOGI("No master key, so not ext4enc"); |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
Paul Crowley | 9336348 | 2015-07-07 15:17:22 +0100 | [diff] [blame] | 336 | // Get raw keyref - used to make keyname and to pass to ioctl |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 337 | static std::string generate_key_ref(const char* key, int length) |
| 338 | { |
| 339 | SHA512_CTX c; |
| 340 | |
| 341 | SHA512_Init(&c); |
| 342 | SHA512_Update(&c, key, length); |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 343 | unsigned char key_ref1[SHA512_DIGEST_LENGTH]; |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 344 | SHA512_Final(key_ref1, &c); |
| 345 | |
| 346 | SHA512_Init(&c); |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 347 | SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH); |
| 348 | unsigned char key_ref2[SHA512_DIGEST_LENGTH]; |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 349 | SHA512_Final(key_ref2, &c); |
| 350 | |
| 351 | return std::string((char*)key_ref2, EXT4_KEY_DESCRIPTOR_SIZE); |
| 352 | } |
| 353 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 354 | int e4crypt_check_passwd(const char* path, const char* password) |
| 355 | { |
| 356 | SLOGI("e4crypt_check_password"); |
Paul Lawrence | a56d313 | 2015-05-04 15:48:24 -0700 | [diff] [blame] | 357 | auto props = GetPropsOrAltProps(path); |
| 358 | auto key_props = props.GetChild(properties::key); |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 359 | |
| 360 | crypt_mnt_ftr ftr; |
| 361 | if (get_crypt_ftr_and_key(ftr, key_props)) { |
| 362 | SLOGE("Failed to read crypto footer back"); |
| 363 | return -1; |
| 364 | } |
| 365 | |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 366 | unsigned char master_key_bytes[key_length / 8]; |
| 367 | if (cryptfs_get_master_key (&ftr, password, master_key_bytes)){ |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 368 | SLOGI("Incorrect password"); |
Paul Lawrence | c78c71b | 2015-04-14 15:26:29 -0700 | [diff] [blame] | 369 | ftr.failed_decrypt_count++; |
| 370 | if (put_crypt_ftr_and_key(ftr, key_props)) { |
| 371 | SLOGW("Failed to update failed_decrypt_count"); |
| 372 | } |
| 373 | return ftr.failed_decrypt_count; |
| 374 | } |
| 375 | |
| 376 | if (ftr.failed_decrypt_count) { |
| 377 | ftr.failed_decrypt_count = 0; |
| 378 | if (put_crypt_ftr_and_key(ftr, key_props)) { |
| 379 | SLOGW("Failed to reset failed_decrypt_count"); |
| 380 | } |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 381 | } |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 382 | std::string master_key(reinterpret_cast<char*>(master_key_bytes), |
| 383 | sizeof(master_key_bytes)); |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 384 | |
Paul Lawrence | 86c942a | 2015-05-06 13:53:43 -0700 | [diff] [blame] | 385 | struct timespec now; |
| 386 | clock_gettime(CLOCK_BOOTTIME, &now); |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 387 | s_key_store[path] = keys{master_key, password, |
Paul Lawrence | 86c942a | 2015-05-06 13:53:43 -0700 | [diff] [blame] | 388 | now.tv_sec + password_max_age_seconds}; |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 389 | std::string raw_ref; |
| 390 | if (!install_key(master_key, raw_ref)) { |
Paul Crowley | f25a35a | 2015-05-06 13:38:53 +0100 | [diff] [blame] | 391 | return -1; |
| 392 | } |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 393 | SLOGD("Installed master key"); |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 394 | |
Paul Crowley | f25a35a | 2015-05-06 13:38:53 +0100 | [diff] [blame] | 395 | // Save reference to key so we can set policy later |
| 396 | if (!props.Set(properties::ref, raw_ref)) { |
| 397 | SLOGE("Cannot save key reference"); |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
Paul Crowley | 9336348 | 2015-07-07 15:17:22 +0100 | [diff] [blame] | 404 | static ext4_encryption_key fill_key(const std::string &key) |
Paul Crowley | f25a35a | 2015-05-06 13:38:53 +0100 | [diff] [blame] | 405 | { |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 406 | // ext4enc:TODO Currently raw key is required to be of length |
| 407 | // sizeof(ext4_key.raw) == EXT4_MAX_KEY_SIZE, so zero pad to |
| 408 | // this length. Change when kernel bug is fixed. |
| 409 | ext4_encryption_key ext4_key = {EXT4_ENCRYPTION_MODE_AES_256_XTS, |
| 410 | {0}, |
| 411 | sizeof(ext4_key.raw)}; |
| 412 | memset(ext4_key.raw, 0, sizeof(ext4_key.raw)); |
| 413 | static_assert(key_length / 8 <= sizeof(ext4_key.raw), |
| 414 | "Key too long!"); |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 415 | memcpy(ext4_key.raw, &key[0], key.size()); |
Paul Crowley | 9336348 | 2015-07-07 15:17:22 +0100 | [diff] [blame] | 416 | return ext4_key; |
| 417 | } |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 418 | |
Paul Crowley | 9336348 | 2015-07-07 15:17:22 +0100 | [diff] [blame] | 419 | static std::string keyname(const std::string &raw_ref) |
| 420 | { |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 421 | std::ostringstream o; |
Paul Crowley | 9336348 | 2015-07-07 15:17:22 +0100 | [diff] [blame] | 422 | o << "ext4:"; |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 423 | for (auto i = raw_ref.begin(); i != raw_ref.end(); ++i) { |
| 424 | o << std::hex << std::setw(2) << std::setfill('0') << (int)*i; |
| 425 | } |
Paul Crowley | 9336348 | 2015-07-07 15:17:22 +0100 | [diff] [blame] | 426 | return o.str(); |
| 427 | } |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 428 | |
Paul Crowley | 9336348 | 2015-07-07 15:17:22 +0100 | [diff] [blame] | 429 | // Get the keyring we store all keys in |
| 430 | static key_serial_t e4crypt_keyring() |
| 431 | { |
| 432 | return keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "e4crypt", 0); |
| 433 | } |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 434 | |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 435 | // Install password into global keyring |
| 436 | // Return raw key reference for use in policy |
| 437 | static bool install_key(const std::string &key, std::string &raw_ref) |
Paul Crowley | 9336348 | 2015-07-07 15:17:22 +0100 | [diff] [blame] | 438 | { |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 439 | if (key.size() != key_length/8) { |
| 440 | LOG(ERROR) << "Wrong size key " << key.size(); |
| 441 | return false; |
| 442 | } |
| 443 | auto ext4_key = fill_key(key); |
| 444 | raw_ref = generate_key_ref(ext4_key.raw, ext4_key.size); |
| 445 | auto ref = keyname(raw_ref); |
Paul Crowley | 9336348 | 2015-07-07 15:17:22 +0100 | [diff] [blame] | 446 | key_serial_t device_keyring = e4crypt_keyring(); |
Paul Lawrence | fd7db73 | 2015-04-10 07:48:51 -0700 | [diff] [blame] | 447 | key_serial_t key_id = add_key("logon", ref.c_str(), |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 448 | (void*)&ext4_key, sizeof(ext4_key), |
| 449 | device_keyring); |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 450 | if (key_id == -1) { |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 451 | PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring; |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 452 | return false; |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 453 | } |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 454 | LOG(INFO) << "Added key " << key_id << " (" << ref << ") to keyring " |
| 455 | << device_keyring << " in process " << getpid(); |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 456 | return true; |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | int e4crypt_restart(const char* path) |
| 460 | { |
| 461 | SLOGI("e4crypt_restart"); |
| 462 | |
| 463 | int rc = 0; |
| 464 | |
| 465 | SLOGI("ext4 restart called on %s", path); |
| 466 | property_set("vold.decrypt", "trigger_reset_main"); |
| 467 | SLOGI("Just asked init to shut down class main"); |
| 468 | sleep(2); |
| 469 | |
| 470 | std::string tmp_path = std::string() + path + "/tmp_mnt"; |
| 471 | |
Paul Lawrence | 2f32cda | 2015-05-05 14:28:25 -0700 | [diff] [blame] | 472 | rc = wait_and_unmount(tmp_path.c_str(), true); |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 473 | if (rc) { |
| 474 | SLOGE("umount %s failed with rc %d, msg %s", |
| 475 | tmp_path.c_str(), rc, strerror(errno)); |
| 476 | return rc; |
| 477 | } |
| 478 | |
Paul Lawrence | 2f32cda | 2015-05-05 14:28:25 -0700 | [diff] [blame] | 479 | rc = wait_and_unmount(path, true); |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 480 | if (rc) { |
| 481 | SLOGE("umount %s failed with rc %d, msg %s", |
| 482 | path, rc, strerror(errno)); |
| 483 | return rc; |
| 484 | } |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 489 | int e4crypt_get_password_type(const char* path) |
| 490 | { |
| 491 | SLOGI("e4crypt_get_password_type"); |
| 492 | return GetPropsOrAltProps(path).GetChild(properties::key) |
| 493 | .Get<int>(tag::crypt_type, CRYPT_TYPE_DEFAULT); |
| 494 | } |
Paul Lawrence | 368d794 | 2015-04-15 14:12:00 -0700 | [diff] [blame] | 495 | |
Paul Lawrence | 86c942a | 2015-05-06 13:53:43 -0700 | [diff] [blame] | 496 | const char* e4crypt_get_password(const char* path) |
| 497 | { |
| 498 | SLOGI("e4crypt_get_password"); |
| 499 | |
| 500 | auto i = s_key_store.find(path); |
| 501 | if (i == s_key_store.end()) { |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | struct timespec now; |
| 506 | clock_gettime(CLOCK_BOOTTIME, &now); |
| 507 | if (i->second.expiry_time < now.tv_sec) { |
| 508 | e4crypt_clear_password(path); |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | return i->second.password.c_str(); |
| 513 | } |
| 514 | |
| 515 | void e4crypt_clear_password(const char* path) |
| 516 | { |
| 517 | SLOGI("e4crypt_clear_password"); |
| 518 | |
| 519 | auto i = s_key_store.find(path); |
| 520 | if (i == s_key_store.end()) { |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | memset(&i->second.password[0], 0, i->second.password.size()); |
| 525 | i->second.password = std::string(); |
| 526 | } |
| 527 | |
Paul Lawrence | 368d794 | 2015-04-15 14:12:00 -0700 | [diff] [blame] | 528 | int e4crypt_get_field(const char* path, const char* fieldname, |
| 529 | char* value, size_t len) |
| 530 | { |
| 531 | auto v = GetPropsOrAltProps(path).GetChild(properties::props) |
| 532 | .Get<std::string>(fieldname); |
| 533 | |
| 534 | if (v == "") { |
| 535 | return CRYPTO_GETFIELD_ERROR_NO_FIELD; |
| 536 | } |
| 537 | |
| 538 | if (v.length() >= len) { |
| 539 | return CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL; |
| 540 | } |
| 541 | |
| 542 | strlcpy(value, v.c_str(), len); |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | int e4crypt_set_field(const char* path, const char* fieldname, |
| 547 | const char* value) |
| 548 | { |
| 549 | return GetPropsOrAltProps(path).GetChild(properties::props) |
| 550 | .Set(fieldname, std::string(value)) ? 0 : -1; |
| 551 | } |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 552 | |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 553 | static std::string get_de_key_path(userid_t user_id) { |
| 554 | return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id); |
| 555 | } |
| 556 | |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 557 | static std::string get_ce_key_path(userid_t user_id) { |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 558 | return StringPrintf("%s/ce/%d/current", user_key_dir.c_str(), user_id); |
Paul Crowley | b33e887 | 2015-05-19 12:34:09 +0100 | [diff] [blame] | 559 | } |
| 560 | |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 561 | static bool read_and_install_key(const std::string &key_path, std::string &raw_ref) |
| 562 | { |
| 563 | std::string key; |
| 564 | if (!android::vold::retrieveKey(key_path, key)) return false; |
| 565 | if (!install_key(key, raw_ref)) return false; |
| 566 | return true; |
Lenka Trochtova | 395039f | 2015-11-25 10:13:03 +0100 | [diff] [blame] | 567 | } |
| 568 | |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 569 | static bool read_and_install_user_ce_key(userid_t user_id) |
Paul Crowley | b33e887 | 2015-05-19 12:34:09 +0100 | [diff] [blame] | 570 | { |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 571 | if (s_ce_key_raw_refs.count(user_id) != 0) return true; |
| 572 | const auto key_path = get_ce_key_path(user_id); |
| 573 | std::string raw_ref; |
| 574 | if (!read_and_install_key(key_path, raw_ref)) return false; |
| 575 | s_ce_key_raw_refs[user_id] = raw_ref; |
| 576 | LOG(DEBUG) << "Installed ce key for user " << user_id; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 577 | return true; |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 580 | static bool prepare_dir(const std::string &dir, mode_t mode, uid_t uid, gid_t gid) { |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 581 | LOG(DEBUG) << "Preparing: " << dir; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 582 | if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) { |
| 583 | PLOG(ERROR) << "Failed to prepare " << dir; |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 584 | return false; |
| 585 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 586 | return true; |
| 587 | } |
| 588 | |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 589 | static bool random_key(std::string &key) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 590 | if (android::vold::ReadRandomBytes(key_length / 8, key) != 0) { |
| 591 | // TODO status_t plays badly with PLOG, fix it. |
| 592 | LOG(ERROR) << "Random read failed"; |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 593 | return false; |
| 594 | } |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 595 | return true; |
| 596 | } |
| 597 | |
| 598 | static bool path_exists(const std::string &path) { |
| 599 | return access(path.c_str(), F_OK) == 0; |
| 600 | } |
| 601 | |
| 602 | // NB this assumes that there is only one thread listening for crypt commands, because |
| 603 | // it creates keys in a fixed location. |
| 604 | static bool store_key(const std::string &key_path, const std::string &key) { |
| 605 | if (path_exists(key_path)) { |
| 606 | LOG(ERROR) << "Already exists, cannot create key at: " << key_path; |
| 607 | return false; |
| 608 | } |
| 609 | if (path_exists(user_key_temp)) { |
| 610 | android::vold::destroyKey(user_key_temp); |
| 611 | } |
| 612 | if (!android::vold::storeKey(user_key_temp, key)) return false; |
| 613 | if (rename(user_key_temp.c_str(), key_path.c_str()) != 0) { |
| 614 | PLOG(ERROR) << "Unable to move new key to location: " << key_path; |
| 615 | return false; |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 616 | } |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 617 | LOG(DEBUG) << "Created key " << key_path; |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 618 | return true; |
| 619 | } |
| 620 | |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 621 | static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) { |
| 622 | std::string de_key, ce_key; |
| 623 | if (!random_key(de_key)) return false; |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 624 | if (!random_key(ce_key)) return false; |
| 625 | if (create_ephemeral) { |
| 626 | // If the key should be created as ephemeral, don't store it. |
| 627 | s_ephemeral_users.insert(user_id); |
| 628 | } else { |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 629 | if (!store_key(get_de_key_path(user_id), de_key)) return false; |
| 630 | if (!prepare_dir(user_key_dir + "/ce/" + std::to_string(user_id), |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 631 | 0700, AID_ROOT, AID_ROOT)) return false; |
| 632 | if (!store_key(get_ce_key_path(user_id), ce_key)) return false; |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 633 | } |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 634 | std::string de_raw_ref; |
| 635 | if (!install_key(de_key, de_raw_ref)) return false; |
| 636 | s_de_key_raw_refs[user_id] = de_raw_ref; |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 637 | std::string ce_raw_ref; |
| 638 | if (!install_key(ce_key, ce_raw_ref)) return false; |
| 639 | s_ce_key_raw_refs[user_id] = ce_raw_ref; |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 640 | LOG(DEBUG) << "Created keys for user " << user_id; |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 641 | return true; |
| 642 | } |
| 643 | |
| 644 | static bool lookup_key_ref(const std::map<userid_t, std::string> &key_map, |
| 645 | userid_t user_id, std::string &raw_ref) { |
| 646 | auto refi = key_map.find(user_id); |
| 647 | if (refi == key_map.end()) { |
| 648 | LOG(ERROR) << "Cannot find key for " << user_id; |
| 649 | return false; |
| 650 | } |
| 651 | raw_ref = refi->second; |
| 652 | return true; |
| 653 | } |
| 654 | |
| 655 | static bool set_policy(const std::string &raw_ref, const std::string& path) { |
| 656 | if (do_policy_set(path.c_str(), raw_ref.data(), raw_ref.size()) != 0) { |
| 657 | LOG(ERROR) << "Failed to set policy on: " << path; |
| 658 | return false; |
| 659 | } |
| 660 | return true; |
Paul Crowley | 95376d6 | 2015-05-06 15:04:43 +0100 | [diff] [blame] | 661 | } |
Paul Crowley | b33e887 | 2015-05-19 12:34:09 +0100 | [diff] [blame] | 662 | |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 663 | static bool is_numeric(const char *name) { |
| 664 | for (const char *p = name; *p != '\0'; p++) { |
| 665 | if (!isdigit(*p)) |
| 666 | return false; |
| 667 | } |
| 668 | return true; |
| 669 | } |
| 670 | |
| 671 | static bool load_all_de_keys() { |
| 672 | auto de_dir = user_key_dir + "/de"; |
| 673 | auto dirp = std::unique_ptr<DIR, int(*)(DIR*)>(opendir(de_dir.c_str()), closedir); |
| 674 | if (!dirp) { |
| 675 | PLOG(ERROR) << "Unable to read de key directory"; |
| 676 | return false; |
| 677 | } |
| 678 | for (;;) { |
| 679 | errno = 0; |
| 680 | auto entry = readdir(dirp.get()); |
| 681 | if (!entry) { |
| 682 | if (errno) { |
| 683 | PLOG(ERROR) << "Unable to read de key directory"; |
| 684 | return false; |
| 685 | } |
| 686 | break; |
| 687 | } |
| 688 | if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) { |
| 689 | LOG(DEBUG) << "Skipping non-de-key " << entry->d_name; |
| 690 | continue; |
| 691 | } |
| 692 | userid_t user_id = atoi(entry->d_name); |
| 693 | if (s_de_key_raw_refs.count(user_id) == 0) { |
| 694 | std::string raw_ref; |
| 695 | if (!read_and_install_key(de_dir + "/" + entry->d_name, raw_ref)) return false; |
| 696 | s_de_key_raw_refs[user_id] = raw_ref; |
| 697 | LOG(DEBUG) << "Installed de key for user " << user_id; |
| 698 | } |
| 699 | } |
| 700 | // ext4enc:TODO: go through all DE directories, ensure that all user dirs have the |
| 701 | // correct policy set on them, and that no rogue ones exist. |
| 702 | return true; |
| 703 | } |
| 704 | |
Paul Crowley | 8fb12fd | 2016-02-01 14:28:12 +0000 | [diff] [blame] | 705 | int e4crypt_init_user0() { |
| 706 | LOG(DEBUG) << "e4crypt_init_user0"; |
| 707 | if (e4crypt_is_native()) { |
| 708 | if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return -1; |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 709 | if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return -1; |
| 710 | if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return -1; |
| 711 | auto de_path = get_de_key_path(0); |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 712 | auto ce_path = get_ce_key_path(0); |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 713 | if (!path_exists(de_path) || !path_exists(ce_path)) { |
| 714 | if (path_exists(de_path)) { |
| 715 | android::vold::destroyKey(de_path); // Ignore failure |
| 716 | } |
| 717 | if (path_exists(ce_path)) { |
| 718 | android::vold::destroyKey(ce_path); // Ignore failure |
| 719 | } |
| 720 | if (!create_and_install_user_keys(0, false)) return -1; |
Paul Crowley | 8fb12fd | 2016-02-01 14:28:12 +0000 | [diff] [blame] | 721 | } |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 722 | if (!load_all_de_keys()) return -1; |
Paul Crowley | 8fb12fd | 2016-02-01 14:28:12 +0000 | [diff] [blame] | 723 | } |
| 724 | // Ignore failures. FIXME this is horrid |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 725 | // FIXME: we need an idempotent policy-setting call, which simply verifies the |
| 726 | // policy is already set on a second run, even if the directory is nonempty. |
| 727 | // Then we need to call it all the time. |
Paul Crowley | 8fb12fd | 2016-02-01 14:28:12 +0000 | [diff] [blame] | 728 | e4crypt_prepare_user_storage(nullptr, 0, 0, false); |
| 729 | return 0; |
| 730 | } |
| 731 | |
Paul Crowley | 27cbce9 | 2015-12-10 14:51:30 +0000 | [diff] [blame] | 732 | int e4crypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) { |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 733 | LOG(DEBUG) << "e4crypt_vold_create_user_key for " << user_id << " serial " << serial; |
Paul Crowley | ea62e26 | 2016-01-28 12:23:53 +0000 | [diff] [blame] | 734 | if (!e4crypt_is_native()) { |
| 735 | return 0; |
| 736 | } |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 737 | // FIXME test for existence of key that is not loaded yet |
| 738 | if (s_ce_key_raw_refs.count(user_id) != 0) { |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 739 | LOG(ERROR) << "Already exists, can't e4crypt_vold_create_user_key for " |
| 740 | << user_id << " serial " << serial; |
| 741 | // FIXME should we fail the command? |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 742 | return 0; |
| 743 | } |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 744 | if (!create_and_install_user_keys(user_id, ephemeral)) { |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 745 | return -1; |
| 746 | } |
| 747 | // TODO: create second key for user_de data |
| 748 | return 0; |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 749 | } |
| 750 | |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 751 | static bool evict_key(const std::string &raw_ref) { |
| 752 | auto ref = keyname(raw_ref); |
Paul Crowley | 9336348 | 2015-07-07 15:17:22 +0100 | [diff] [blame] | 753 | auto key_serial = keyctl_search(e4crypt_keyring(), "logon", ref.c_str(), 0); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 754 | if (keyctl_revoke(key_serial) != 0) { |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 755 | PLOG(ERROR) << "Failed to revoke key with serial " << key_serial << " ref " << ref; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 756 | return false; |
Paul Crowley | 9336348 | 2015-07-07 15:17:22 +0100 | [diff] [blame] | 757 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 758 | LOG(DEBUG) << "Revoked key with serial " << key_serial << " ref " << ref; |
| 759 | return true; |
| 760 | } |
| 761 | |
| 762 | int e4crypt_destroy_user_key(userid_t user_id) { |
| 763 | LOG(DEBUG) << "e4crypt_destroy_user_key(" << user_id << ")"; |
Paul Crowley | ea62e26 | 2016-01-28 12:23:53 +0000 | [diff] [blame] | 764 | if (!e4crypt_is_native()) { |
| 765 | return 0; |
| 766 | } |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 767 | bool success = true; |
| 768 | std::string raw_ref; |
| 769 | success &= lookup_key_ref(s_ce_key_raw_refs, user_id, raw_ref) && evict_key(raw_ref); |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 770 | success &= lookup_key_ref(s_de_key_raw_refs, user_id, raw_ref) && evict_key(raw_ref); |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 771 | auto it = s_ephemeral_users.find(user_id); |
| 772 | if (it != s_ephemeral_users.end()) { |
| 773 | s_ephemeral_users.erase(it); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 774 | } else { |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 775 | success &= android::vold::destroyKey(get_ce_key_path(user_id)); |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 776 | success &= android::vold::destroyKey(get_de_key_path(user_id)); |
Lenka Trochtova | 395039f | 2015-11-25 10:13:03 +0100 | [diff] [blame] | 777 | } |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 778 | return success ? 0 : -1; |
Paul Crowley | b33e887 | 2015-05-19 12:34:09 +0100 | [diff] [blame] | 779 | } |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 780 | |
Jeff Sharkey | 7a9dd95 | 2016-01-12 16:52:16 -0700 | [diff] [blame] | 781 | static int emulated_lock(const std::string& path) { |
| 782 | if (chmod(path.c_str(), 0000) != 0) { |
| 783 | PLOG(ERROR) << "Failed to chmod " << path; |
| 784 | return -1; |
| 785 | } |
| 786 | #if EMULATED_USES_SELINUX |
| 787 | if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) { |
| 788 | PLOG(WARNING) << "Failed to setfilecon " << path; |
| 789 | return -1; |
| 790 | } |
| 791 | #endif |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | static int emulated_unlock(const std::string& path, mode_t mode) { |
| 796 | if (chmod(path.c_str(), mode) != 0) { |
| 797 | PLOG(ERROR) << "Failed to chmod " << path; |
Paul Crowley | a042cb5 | 2016-01-21 17:24:49 +0000 | [diff] [blame] | 798 | // FIXME temporary workaround for b/26713622 |
| 799 | if (e4crypt_is_emulated()) return -1; |
Jeff Sharkey | 7a9dd95 | 2016-01-12 16:52:16 -0700 | [diff] [blame] | 800 | } |
| 801 | #if EMULATED_USES_SELINUX |
| 802 | if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) { |
| 803 | PLOG(WARNING) << "Failed to restorecon " << path; |
Paul Crowley | a042cb5 | 2016-01-21 17:24:49 +0000 | [diff] [blame] | 804 | // FIXME temporary workaround for b/26713622 |
| 805 | if (e4crypt_is_emulated()) return -1; |
Jeff Sharkey | 7a9dd95 | 2016-01-12 16:52:16 -0700 | [diff] [blame] | 806 | } |
| 807 | #endif |
| 808 | return 0; |
| 809 | } |
| 810 | |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 811 | int e4crypt_unlock_user_key(userid_t user_id, int serial, const char* token) { |
| 812 | LOG(DEBUG) << "e4crypt_unlock_user_key " << user_id << " " << (token != nullptr); |
Jeff Sharkey | fc505c3 | 2015-12-07 17:27:01 -0700 | [diff] [blame] | 813 | if (e4crypt_is_native()) { |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 814 | if (!read_and_install_user_ce_key(user_id)) { |
Paul Crowley | 8fb12fd | 2016-02-01 14:28:12 +0000 | [diff] [blame] | 815 | LOG(ERROR) << "Couldn't read key for " << user_id; |
| 816 | return -1; |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 817 | } |
Jeff Sharkey | fc505c3 | 2015-12-07 17:27:01 -0700 | [diff] [blame] | 818 | } else { |
| 819 | // When in emulation mode, we just use chmod. However, we also |
| 820 | // unlock directories when not in emulation mode, to bring devices |
| 821 | // back into a known-good state. |
Jeff Sharkey | 7a9dd95 | 2016-01-12 16:52:16 -0700 | [diff] [blame] | 822 | if (emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) || |
| 823 | emulated_unlock(android::vold::BuildDataMediaPath(nullptr, user_id), 0770) || |
| 824 | emulated_unlock(android::vold::BuildDataUserPath(nullptr, user_id), 0771)) { |
| 825 | LOG(ERROR) << "Failed to unlock user " << user_id; |
Jeff Sharkey | fc505c3 | 2015-12-07 17:27:01 -0700 | [diff] [blame] | 826 | return -1; |
| 827 | } |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 828 | } |
| 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | int e4crypt_lock_user_key(userid_t user_id) { |
Jeff Sharkey | fc505c3 | 2015-12-07 17:27:01 -0700 | [diff] [blame] | 833 | if (e4crypt_is_native()) { |
| 834 | // TODO: remove from kernel keyring |
| 835 | } else if (e4crypt_is_emulated()) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 836 | // When in emulation mode, we just use chmod |
Jeff Sharkey | 7a9dd95 | 2016-01-12 16:52:16 -0700 | [diff] [blame] | 837 | if (emulated_lock(android::vold::BuildDataSystemCePath(user_id)) || |
| 838 | emulated_lock(android::vold::BuildDataMediaPath(nullptr, user_id)) || |
| 839 | emulated_lock(android::vold::BuildDataUserPath(nullptr, user_id))) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 840 | PLOG(ERROR) << "Failed to lock user " << user_id; |
| 841 | return -1; |
| 842 | } |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 843 | } |
Jeff Sharkey | fc505c3 | 2015-12-07 17:27:01 -0700 | [diff] [blame] | 844 | |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 845 | return 0; |
| 846 | } |
| 847 | |
Lenka Trochtova | 9ad4369 | 2015-12-11 13:27:26 +0100 | [diff] [blame] | 848 | int e4crypt_prepare_user_storage(const char* volume_uuid, |
| 849 | userid_t user_id, |
| 850 | int serial, |
| 851 | bool ephemeral) { |
Paul Crowley | 285956f | 2016-01-20 13:12:38 +0000 | [diff] [blame] | 852 | if (volume_uuid) { |
| 853 | LOG(DEBUG) << "e4crypt_prepare_user_storage " << volume_uuid << " " << user_id; |
| 854 | } else { |
| 855 | LOG(DEBUG) << "e4crypt_prepare_user_storage, null volume " << user_id; |
| 856 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 857 | auto system_ce_path = android::vold::BuildDataSystemCePath(user_id); |
| 858 | auto media_ce_path = android::vold::BuildDataMediaPath(volume_uuid, user_id); |
| 859 | auto user_ce_path = android::vold::BuildDataUserPath(volume_uuid, user_id); |
| 860 | auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id); |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 861 | |
Paul Crowley | 8fb12fd | 2016-02-01 14:28:12 +0000 | [diff] [blame] | 862 | // FIXME: should this be 0770 or 0700? |
| 863 | if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return -1; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 864 | if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return -1; |
| 865 | if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return -1; |
| 866 | if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return -1; |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 867 | |
| 868 | if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) { |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 869 | std::string ce_raw_ref, de_raw_ref; |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 870 | if (!lookup_key_ref(s_ce_key_raw_refs, user_id, ce_raw_ref)) return -1; |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 871 | if (!lookup_key_ref(s_de_key_raw_refs, user_id, de_raw_ref)) return -1; |
Paul Crowley | b1f3d24 | 2016-01-28 10:09:46 +0000 | [diff] [blame] | 872 | if (!set_policy(ce_raw_ref, system_ce_path)) return -1; |
| 873 | if (!set_policy(ce_raw_ref, media_ce_path)) return -1; |
| 874 | if (!set_policy(ce_raw_ref, user_ce_path)) return -1; |
Paul Crowley | b92f83c | 2016-02-01 14:10:43 +0000 | [diff] [blame^] | 875 | if (!set_policy(de_raw_ref, user_de_path)) return -1; |
| 876 | // FIXME I thought there were more DE directories than this |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | return 0; |
| 880 | } |