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 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 17 | #include <array> |
| 18 | |
| 19 | #include <asm/ioctl.h> |
| 20 | #include <dirent.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <linux/fs.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/syscall.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <android-base/file.h> |
| 31 | #include <android-base/logging.h> |
| 32 | #include <cutils/properties.h> |
| 33 | #include <logwrap/logwrap.h> |
| 34 | #include <utils/misc.h> |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 35 | #include <fscrypt/fscrypt.h> |
bigbiff | a84acc7 | 2021-12-23 19:58:22 -0500 | [diff] [blame] | 36 | #include "KeyUtil.h" |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 37 | |
| 38 | #include "fscrypt_policy.h" |
| 39 | |
| 40 | static int encryption_mode = FS_ENCRYPTION_MODE_PRIVATE; |
| 41 | |
| 42 | bool fscrypt_is_native() { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 43 | LOG(ERROR) << "fscrypt_is_native::ro.crypto.type"; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 44 | char value[PROPERTY_VALUE_MAX]; |
| 45 | property_get("ro.crypto.type", value, "none"); |
| 46 | return !strcmp(value, "file"); |
| 47 | } |
| 48 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 49 | extern "C" void bytes_to_hex(const uint8_t *bytes, size_t num_bytes, char *hex) { |
| 50 | for (size_t i = 0; i < num_bytes; i++) { |
| 51 | sprintf(&hex[2 * i], "%02x", bytes[i]); |
| 52 | } |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static bool is_dir_empty(const char *dirname, bool *is_empty) |
| 56 | { |
| 57 | int n = 0; |
| 58 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(dirname), closedir); |
| 59 | if (!dirp) { |
| 60 | PLOG(ERROR) << "Unable to read directory: " << dirname; |
| 61 | return false; |
| 62 | } |
| 63 | for (;;) { |
| 64 | errno = 0; |
| 65 | auto entry = readdir(dirp.get()); |
| 66 | if (!entry) { |
| 67 | if (errno) { |
| 68 | PLOG(ERROR) << "Unable to read directory: " << dirname; |
| 69 | return false; |
| 70 | } |
| 71 | break; |
| 72 | } |
| 73 | if (strcmp(entry->d_name, "lost+found") != 0) { // Skip lost+found |
| 74 | ++n; |
| 75 | if (n > 2) { |
| 76 | *is_empty = false; |
| 77 | return true; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | *is_empty = true; |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | static uint8_t fscrypt_get_policy_flags(int filenames_encryption_mode) { |
| 86 | if (filenames_encryption_mode == FS_ENCRYPTION_MODE_AES_256_CTS) { |
| 87 | // Use legacy padding with our original filenames encryption mode. |
| 88 | return FS_POLICY_FLAGS_PAD_4; |
| 89 | } else if (filenames_encryption_mode == FS_ENCRYPTION_MODE_ADIANTUM) { |
| 90 | // Use DIRECT_KEY for Adiantum, since it's much more efficient but just |
| 91 | // as secure since Android doesn't reuse the same master key for |
| 92 | // multiple encryption modes |
| 93 | return (FS_POLICY_FLAGS_PAD_16 | FS_POLICY_FLAG_DIRECT_KEY); |
| 94 | } |
| 95 | // With a new mode we can use the better padding flag without breaking existing devices: pad |
| 96 | // filenames with zeroes to the next 16-byte boundary. This is more secure (helps hide the |
| 97 | // length of filenames) and makes the inputs evenly divisible into blocks which is more |
| 98 | // efficient for encryption and decryption. |
| 99 | return FS_POLICY_FLAGS_PAD_16; |
| 100 | } |
| 101 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 102 | extern "C" bool fscrypt_set_mode() { |
| 103 | const char* mode_file = "/data/unencrypted/mode"; |
| 104 | struct stat st; |
| 105 | if (stat(mode_file, &st) != 0 || st.st_size <= 0) { |
| 106 | printf("Invalid encryption mode file %s\n", mode_file); |
| 107 | return false; |
| 108 | } |
| 109 | size_t mode_size = st.st_size; |
| 110 | char contents_encryption_mode[mode_size + 1]; |
| 111 | memset((void*)contents_encryption_mode, 0, mode_size + 1); |
| 112 | int fd = open(mode_file, O_RDONLY); |
| 113 | if (fd < 0) { |
| 114 | printf("error opening '%s': %s\n", mode_file, strerror(errno)); |
| 115 | return false; |
| 116 | } |
| 117 | if (read(fd, contents_encryption_mode, mode_size) != mode_size) { |
| 118 | printf("read error on '%s': %s\n", mode_file, strerror(errno)); |
| 119 | close(fd); |
| 120 | return false; |
| 121 | } |
| 122 | close(fd); |
| 123 | |
| 124 | std::string contents_encryption_mode_string = std::string(contents_encryption_mode); |
| 125 | int pos = contents_encryption_mode_string.find(":"); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 126 | LOG(INFO) << "contents_encryption_mode_string: " << contents_encryption_mode_string.substr(0, pos); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 127 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 128 | if (contents_encryption_mode_string.substr(0, pos) == "software") { |
| 129 | encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 130 | } else if (contents_encryption_mode_string.substr(0, pos) == "ice") { |
| 131 | encryption_mode = FS_ENCRYPTION_MODE_PRIVATE; |
| 132 | } else { |
| 133 | printf("Invalid encryption mode '%s'\n", contents_encryption_mode); |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | printf("set encryption mode to %i\n", encryption_mode); |
| 138 | return true; |
| 139 | } |
| 140 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 141 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 142 | extern "C" bool fscrypt_policy_set_struct(const char *directory, const struct fscrypt_policy_v1 *fep) { |
| 143 | #else |
| 144 | extern "C" bool fscrypt_policy_set_struct(const char *directory, const struct fscrypt_policy_v2 *fep) { |
| 145 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 146 | int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
| 147 | if (fd == -1) { |
bigbiff | a84acc7 | 2021-12-23 19:58:22 -0500 | [diff] [blame] | 148 | printf("failed to open %s\n", directory); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 149 | PLOG(ERROR) << "Failed to open directory " << directory; |
| 150 | return false; |
| 151 | } |
bigbiff | a84acc7 | 2021-12-23 19:58:22 -0500 | [diff] [blame] | 152 | if (isFsKeyringSupported()) { |
| 153 | if (ioctl(fd, FS_IOC_SET_ENCRYPTION_POLICY, fep)) { |
| 154 | PLOG(ERROR) << "Failed to set encryption policy for " << directory; |
| 155 | close(fd); |
| 156 | return false; |
| 157 | } |
| 158 | } else { |
| 159 | if (ioctl(fd, FS_IOC_SET_ENCRYPTION_POLICY, fep)) { |
| 160 | PLOG(ERROR) << "Failed to set encryption policy for " << directory; |
| 161 | close(fd); |
| 162 | return false; |
| 163 | } |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 164 | } |
| 165 | close(fd); |
| 166 | return true; |
| 167 | } |
| 168 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 169 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 170 | extern "C" bool fscrypt_policy_get_struct(const char *directory, struct fscrypt_policy_v1 *fep) { |
| 171 | #else |
| 172 | extern "C" bool fscrypt_policy_get_struct(const char *directory, struct fscrypt_policy_v2 *fep) { |
| 173 | #endif |
| 174 | int fd = open(directory, O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_CLOEXEC); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 175 | if (fd == -1) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 176 | PLOG(ERROR) << "Failed to open directory " << directory; |
| 177 | return false; |
| 178 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 179 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 180 | memset(fep, 0, sizeof(fscrypt_policy_v1)); |
| 181 | #else |
| 182 | memset(fep, 0, sizeof(fscrypt_policy_v2)); |
| 183 | #endif |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 184 | struct fscrypt_get_policy_ex_arg ex_policy = {0}; |
bigbiff | a84acc7 | 2021-12-23 19:58:22 -0500 | [diff] [blame] | 185 | |
| 186 | if (isFsKeyringSupported()) { |
| 187 | ex_policy.policy_size = sizeof(ex_policy.policy); |
| 188 | if (ioctl(fd, FS_IOC_GET_ENCRYPTION_POLICY_EX, &ex_policy) != 0) { |
| 189 | PLOG(ERROR) << "Failed to get encryption policy for " << directory; |
| 190 | close(fd); |
| 191 | return false; |
| 192 | } |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame] | 193 | #ifdef USE_FSCRYPT_POLICY_V1 |
bigbiff | a84acc7 | 2021-12-23 19:58:22 -0500 | [diff] [blame] | 194 | memcpy(fep, &ex_policy.policy.v1, sizeof(ex_policy.policy.v1)); |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame] | 195 | #else |
bigbiff | a84acc7 | 2021-12-23 19:58:22 -0500 | [diff] [blame] | 196 | memcpy(fep, &ex_policy.policy.v2, sizeof(ex_policy.policy.v2)); |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame] | 197 | #endif |
bigbiff | a84acc7 | 2021-12-23 19:58:22 -0500 | [diff] [blame] | 198 | } else { |
| 199 | if (ioctl(fd, FS_IOC_GET_ENCRYPTION_POLICY, &ex_policy.policy.v1) != 0) { |
| 200 | PLOG(ERROR) << "Failed to get encryption policy for " << directory; |
| 201 | close(fd); |
| 202 | return false; |
| 203 | } |
| 204 | memcpy(fep, &ex_policy.policy.v1, sizeof(ex_policy.policy.v1)); |
| 205 | } |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 206 | close(fd); |
| 207 | return true; |
| 208 | } |