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/fscrypt.h" |
| 18 | |
| 19 | #include <array> |
| 20 | |
| 21 | #include <asm/ioctl.h> |
| 22 | #include <dirent.h> |
| 23 | #include <errno.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <linux/fs.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/syscall.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include <android-base/file.h> |
| 33 | #include <android-base/logging.h> |
| 34 | #include <cutils/properties.h> |
| 35 | #include <logwrap/logwrap.h> |
| 36 | #include <utils/misc.h> |
| 37 | |
| 38 | #include "fscrypt_policy.h" |
| 39 | |
| 40 | static int encryption_mode = FS_ENCRYPTION_MODE_PRIVATE; |
| 41 | |
| 42 | bool fscrypt_is_native() { |
| 43 | char value[PROPERTY_VALUE_MAX]; |
| 44 | property_get("ro.crypto.type", value, "none"); |
| 45 | return !strcmp(value, "file"); |
| 46 | } |
| 47 | |
| 48 | static void log_ls(const char* dirname) { |
| 49 | std::array<const char*, 3> argv = {"ls", "-laZ", dirname}; |
| 50 | int status = 0; |
| 51 | auto res = |
| 52 | android_fork_execvp(argv.size(), const_cast<char**>(argv.data()), &status, false, true); |
| 53 | if (res != 0) { |
| 54 | PLOG(ERROR) << argv[0] << " " << argv[1] << " " << argv[2] << "failed"; |
| 55 | return; |
| 56 | } |
| 57 | if (!WIFEXITED(status)) { |
| 58 | LOG(ERROR) << argv[0] << " " << argv[1] << " " << argv[2] |
| 59 | << " did not exit normally, status: " << status; |
| 60 | return; |
| 61 | } |
| 62 | if (WEXITSTATUS(status) != 0) { |
| 63 | LOG(ERROR) << argv[0] << " " << argv[1] << " " << argv[2] |
| 64 | << " returned failure: " << WEXITSTATUS(status); |
| 65 | return; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | extern "C" void policy_to_hex(const uint8_t* policy, char* hex) { |
| 70 | for (size_t i = 0, j = 0; i < FS_KEY_DESCRIPTOR_SIZE; i++) { |
| 71 | hex[j++] = HEX_LOOKUP[(policy[i] & 0xF0) >> 4]; |
| 72 | hex[j++] = HEX_LOOKUP[policy[i] & 0x0F]; |
| 73 | } |
| 74 | hex[FS_KEY_DESCRIPTOR_SIZE_HEX - 1] = '\0'; |
| 75 | } |
| 76 | |
| 77 | static bool is_dir_empty(const char *dirname, bool *is_empty) |
| 78 | { |
| 79 | int n = 0; |
| 80 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(dirname), closedir); |
| 81 | if (!dirp) { |
| 82 | PLOG(ERROR) << "Unable to read directory: " << dirname; |
| 83 | return false; |
| 84 | } |
| 85 | for (;;) { |
| 86 | errno = 0; |
| 87 | auto entry = readdir(dirp.get()); |
| 88 | if (!entry) { |
| 89 | if (errno) { |
| 90 | PLOG(ERROR) << "Unable to read directory: " << dirname; |
| 91 | return false; |
| 92 | } |
| 93 | break; |
| 94 | } |
| 95 | if (strcmp(entry->d_name, "lost+found") != 0) { // Skip lost+found |
| 96 | ++n; |
| 97 | if (n > 2) { |
| 98 | *is_empty = false; |
| 99 | return true; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | *is_empty = true; |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | static uint8_t fscrypt_get_policy_flags(int filenames_encryption_mode) { |
| 108 | if (filenames_encryption_mode == FS_ENCRYPTION_MODE_AES_256_CTS) { |
| 109 | // Use legacy padding with our original filenames encryption mode. |
| 110 | return FS_POLICY_FLAGS_PAD_4; |
| 111 | } else if (filenames_encryption_mode == FS_ENCRYPTION_MODE_ADIANTUM) { |
| 112 | // Use DIRECT_KEY for Adiantum, since it's much more efficient but just |
| 113 | // as secure since Android doesn't reuse the same master key for |
| 114 | // multiple encryption modes |
| 115 | return (FS_POLICY_FLAGS_PAD_16 | FS_POLICY_FLAG_DIRECT_KEY); |
| 116 | } |
| 117 | // With a new mode we can use the better padding flag without breaking existing devices: pad |
| 118 | // filenames with zeroes to the next 16-byte boundary. This is more secure (helps hide the |
| 119 | // length of filenames) and makes the inputs evenly divisible into blocks which is more |
| 120 | // efficient for encryption and decryption. |
| 121 | return FS_POLICY_FLAGS_PAD_16; |
| 122 | } |
| 123 | |
| 124 | static bool fscrypt_policy_set(const char *directory, uint8_t *policy, |
| 125 | size_t policy_length, |
| 126 | int contents_encryption_mode, |
| 127 | int filenames_encryption_mode) { |
| 128 | if (policy_length != FS_KEY_DESCRIPTOR_SIZE) { |
| 129 | LOG(ERROR) << "Policy wrong length: " << policy_length; |
| 130 | return false; |
| 131 | } |
| 132 | char policy_hex[FS_KEY_DESCRIPTOR_SIZE_HEX]; |
| 133 | policy_to_hex(policy, policy_hex); |
| 134 | |
| 135 | int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
| 136 | if (fd == -1) { |
| 137 | PLOG(ERROR) << "Failed to open directory " << directory; |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | fscrypt_policy fp; |
| 142 | fp.version = 0; |
| 143 | fp.contents_encryption_mode = contents_encryption_mode; |
| 144 | fp.filenames_encryption_mode = filenames_encryption_mode; |
| 145 | fp.flags = fscrypt_get_policy_flags(filenames_encryption_mode); |
| 146 | memcpy(fp.master_key_descriptor, policy, FS_KEY_DESCRIPTOR_SIZE); |
| 147 | if (ioctl(fd, FS_IOC_SET_ENCRYPTION_POLICY, &fp)) { |
| 148 | PLOG(ERROR) << "Failed to set encryption policy for " << directory << " to " << policy_hex |
| 149 | << " modes " << contents_encryption_mode << "/" << filenames_encryption_mode; |
| 150 | close(fd); |
| 151 | return false; |
| 152 | } |
| 153 | close(fd); |
| 154 | |
| 155 | LOG(INFO) << "Policy for " << directory << " set to " << policy_hex |
| 156 | << " modes " << contents_encryption_mode << "/" << filenames_encryption_mode; |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | static bool fscrypt_policy_get(const char *directory, uint8_t *policy, |
| 161 | size_t policy_length, |
| 162 | int contents_encryption_mode, |
| 163 | int filenames_encryption_mode) { |
| 164 | if (policy_length != FS_KEY_DESCRIPTOR_SIZE) { |
| 165 | LOG(ERROR) << "Policy wrong length: " << policy_length; |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
| 170 | if (fd == -1) { |
| 171 | PLOG(ERROR) << "Failed to open directory " << directory; |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | fscrypt_policy fp; |
| 176 | memset(&fp, 0, sizeof(fscrypt_policy)); |
| 177 | if (ioctl(fd, FS_IOC_GET_ENCRYPTION_POLICY, &fp) != 0) { |
| 178 | PLOG(ERROR) << "Failed to get encryption policy for " << directory; |
| 179 | close(fd); |
| 180 | log_ls(directory); |
| 181 | return false; |
| 182 | } |
| 183 | close(fd); |
| 184 | |
| 185 | if ((fp.version != 0) |
| 186 | || (fp.contents_encryption_mode != contents_encryption_mode) |
| 187 | || (fp.filenames_encryption_mode != filenames_encryption_mode) |
| 188 | || (fp.flags != |
| 189 | fscrypt_get_policy_flags(filenames_encryption_mode))) { |
| 190 | LOG(ERROR) << "Failed to find matching encryption policy for " << directory; |
| 191 | return false; |
| 192 | } |
| 193 | memcpy(policy, fp.master_key_descriptor, FS_KEY_DESCRIPTOR_SIZE); |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | static bool fscrypt_policy_check(const char *directory, uint8_t *policy, |
| 199 | size_t policy_length, |
| 200 | int contents_encryption_mode, |
| 201 | int filenames_encryption_mode) { |
| 202 | if (policy_length != FS_KEY_DESCRIPTOR_SIZE) { |
| 203 | LOG(ERROR) << "Policy wrong length: " << policy_length; |
| 204 | return false; |
| 205 | } |
| 206 | uint8_t existing_policy[FS_KEY_DESCRIPTOR_SIZE]; |
| 207 | if (!fscrypt_policy_get(directory, existing_policy, FS_KEY_DESCRIPTOR_SIZE, |
| 208 | contents_encryption_mode, |
| 209 | filenames_encryption_mode)) return false; |
| 210 | char existing_policy_hex[FS_KEY_DESCRIPTOR_SIZE_HEX]; |
| 211 | |
| 212 | policy_to_hex(existing_policy, existing_policy_hex); |
| 213 | |
| 214 | if (memcmp(policy, existing_policy, FS_KEY_DESCRIPTOR_SIZE) != 0) { |
| 215 | char policy_hex[FS_KEY_DESCRIPTOR_SIZE_HEX]; |
| 216 | policy_to_hex(policy, policy_hex); |
| 217 | LOG(ERROR) << "Found policy " << existing_policy_hex << " at " << directory |
| 218 | << " which doesn't match expected value " << policy_hex; |
| 219 | log_ls(directory); |
| 220 | return false; |
| 221 | } |
| 222 | LOG(INFO) << "Found policy " << existing_policy_hex << " at " << directory |
| 223 | << " which matches expected value"; |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | int fscrypt_policy_ensure(const char *directory, uint8_t *policy, |
| 228 | size_t policy_length, |
| 229 | const char *contents_encryption_mode, |
| 230 | const char *filenames_encryption_mode) { |
| 231 | int contents_mode = 0; |
| 232 | int filenames_mode = 0; |
| 233 | |
| 234 | if (!strcmp(contents_encryption_mode, "software") || |
| 235 | !strcmp(contents_encryption_mode, "aes-256-xts")) { |
| 236 | contents_mode = FS_ENCRYPTION_MODE_AES_256_XTS; |
| 237 | } else if (!strcmp(contents_encryption_mode, "adiantum")) { |
| 238 | contents_mode = FS_ENCRYPTION_MODE_ADIANTUM; |
| 239 | } else if (!strcmp(contents_encryption_mode, "ice")) { |
| 240 | contents_mode = FS_ENCRYPTION_MODE_PRIVATE; |
| 241 | } else { |
| 242 | LOG(ERROR) << "Invalid file contents encryption mode: " |
| 243 | << contents_encryption_mode; |
| 244 | return -1; |
| 245 | } |
| 246 | |
| 247 | if (!strcmp(filenames_encryption_mode, "aes-256-cts")) { |
| 248 | filenames_mode = FS_ENCRYPTION_MODE_AES_256_CTS; |
| 249 | } else if (!strcmp(filenames_encryption_mode, "aes-256-heh")) { |
| 250 | filenames_mode = FS_ENCRYPTION_MODE_AES_256_HEH; |
| 251 | } else if (!strcmp(filenames_encryption_mode, "adiantum")) { |
| 252 | filenames_mode = FS_ENCRYPTION_MODE_ADIANTUM; |
| 253 | } else { |
| 254 | LOG(ERROR) << "Invalid file names encryption mode: " |
| 255 | << filenames_encryption_mode; |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | bool is_empty; |
| 260 | if (!is_dir_empty(directory, &is_empty)) return -1; |
| 261 | if (is_empty) { |
| 262 | if (!fscrypt_policy_set(directory, policy, policy_length, |
| 263 | contents_mode, filenames_mode)) return -1; |
| 264 | } else { |
| 265 | if (!fscrypt_policy_check(directory, policy, policy_length, |
| 266 | contents_mode, filenames_mode)) return -1; |
| 267 | } |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | extern "C" bool fscrypt_set_mode() { |
| 272 | const char* mode_file = "/data/unencrypted/mode"; |
| 273 | struct stat st; |
| 274 | if (stat(mode_file, &st) != 0 || st.st_size <= 0) { |
| 275 | printf("Invalid encryption mode file %s\n", mode_file); |
| 276 | return false; |
| 277 | } |
| 278 | size_t mode_size = st.st_size; |
| 279 | char contents_encryption_mode[mode_size + 1]; |
| 280 | memset((void*)contents_encryption_mode, 0, mode_size + 1); |
| 281 | int fd = open(mode_file, O_RDONLY); |
| 282 | if (fd < 0) { |
| 283 | printf("error opening '%s': %s\n", mode_file, strerror(errno)); |
| 284 | return false; |
| 285 | } |
| 286 | if (read(fd, contents_encryption_mode, mode_size) != mode_size) { |
| 287 | printf("read error on '%s': %s\n", mode_file, strerror(errno)); |
| 288 | close(fd); |
| 289 | return false; |
| 290 | } |
| 291 | close(fd); |
| 292 | |
| 293 | std::string contents_encryption_mode_string = std::string(contents_encryption_mode); |
| 294 | int pos = contents_encryption_mode_string.find(":"); |
| 295 | PLOG(ERROR) << "contents_encryption_mode_string: " << contents_encryption_mode_string.substr(0, pos); |
| 296 | |
| 297 | // if (!strcmp(contents_encryption_mode, "software")) { |
| 298 | if (contents_encryption_mode_string.substr(0, pos) == "software") { |
| 299 | encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS; |
| 300 | // } else if (!strcmp(contents_encryption_mode, "ice")) { |
| 301 | } else if (contents_encryption_mode_string.substr(0, pos) == "ice") { |
| 302 | encryption_mode = FS_ENCRYPTION_MODE_PRIVATE; |
| 303 | } else { |
| 304 | printf("Invalid encryption mode '%s'\n", contents_encryption_mode); |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | printf("set encryption mode to %i\n", encryption_mode); |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | extern "C" void fscrypt_policy_fill_default_struct(fscrypt_encryption_policy *fep) { |
| 313 | fep->version = 0; |
| 314 | fep->contents_encryption_mode = encryption_mode; |
| 315 | fep->filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS; |
| 316 | fep->flags = 0; |
| 317 | memset((void*)&fep->master_key_descriptor[0], 0, FS_KEY_DESCRIPTOR_SIZE); |
| 318 | } |
| 319 | |
| 320 | extern "C" bool fscrypt_policy_set_struct(const char *directory, const fscrypt_encryption_policy *fep) { |
| 321 | int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
| 322 | if (fd == -1) { |
| 323 | printf("failed to open %s\n", directory); |
| 324 | PLOG(ERROR) << "Failed to open directory " << directory; |
| 325 | return false; |
| 326 | } |
| 327 | if (ioctl(fd, FS_IOC_SET_ENCRYPTION_POLICY, fep)) { |
| 328 | printf("failed to set policy for '%s'\n", directory); |
| 329 | PLOG(ERROR) << "Failed to set encryption policy for " << directory; |
| 330 | close(fd); |
| 331 | return false; |
| 332 | } |
| 333 | close(fd); |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | extern "C" bool fscrypt_policy_get_struct(const char *directory, fscrypt_encryption_policy *fep) { |
| 338 | int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
| 339 | if (fd == -1) { |
| 340 | printf("Failed to open '%s'\n", directory); |
| 341 | PLOG(ERROR) << "Failed to open directory " << directory; |
| 342 | return false; |
| 343 | } |
| 344 | memset(fep, 0, sizeof(fscrypt_encryption_policy)); |
| 345 | if (ioctl(fd, FS_IOC_GET_ENCRYPTION_POLICY, fep) != 0) { |
| 346 | PLOG(ERROR) << "Failed to get encryption policy for " << directory; |
| 347 | close(fd); |
| 348 | return false; |
| 349 | } |
| 350 | printf("fscrypt_policy_get_struct::fep->version::%d\n", fep->version); |
| 351 | close(fd); |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | extern "C" bool fscrypt_policy_set(const char *directory, uint8_t *policy, |
| 356 | size_t policy_length, int contents_encryption_mode) { |
| 357 | if (contents_encryption_mode == 0) |
| 358 | contents_encryption_mode = encryption_mode; |
| 359 | if (policy_length != FS_KEY_DESCRIPTOR_SIZE) { |
| 360 | printf("policy wrong length\n"); |
| 361 | LOG(ERROR) << "Policy wrong length: " << policy_length; |
| 362 | return false; |
| 363 | } |
| 364 | int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
| 365 | if (fd == -1) { |
| 366 | printf("failed to open %s\n", directory); |
| 367 | PLOG(ERROR) << "Failed to open directory " << directory; |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | fscrypt_encryption_policy fep; |
| 372 | fep.version = 0; |
| 373 | fep.contents_encryption_mode = contents_encryption_mode; |
| 374 | fep.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS; |
| 375 | fep.flags = 0; |
| 376 | memcpy(fep.master_key_descriptor, policy, FS_KEY_DESCRIPTOR_SIZE); |
| 377 | if (ioctl(fd, FS_IOC_SET_ENCRYPTION_POLICY, &fep)) { |
| 378 | printf("failed to set policy for '%s' '%s'\n", directory, policy); |
| 379 | PLOG(ERROR) << "Failed to set encryption policy for " << directory; |
| 380 | close(fd); |
| 381 | return false; |
| 382 | } |
| 383 | close(fd); |
| 384 | |
| 385 | char policy_hex[FS_KEY_DESCRIPTOR_SIZE_HEX]; |
| 386 | policy_to_hex(policy, policy_hex); |
| 387 | LOG(INFO) << "Policy for " << directory << " set to " << policy_hex; |
| 388 | return true; |
| 389 | } |