fscrypt.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. #include "fscrypt/fscrypt.h"
  17. #include <array>
  18. #include <asm/ioctl.h>
  19. #include <dirent.h>
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #include <linux/fs.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <sys/syscall.h>
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #include <android-base/file.h>
  29. #include <android-base/logging.h>
  30. #include <cutils/properties.h>
  31. #include <logwrap/logwrap.h>
  32. #include <utils/misc.h>
  33. #define FS_KEY_DESCRIPTOR_SIZE_HEX (2 * FS_KEY_DESCRIPTOR_SIZE + 1)
  34. /* modes not supported by upstream kernel, so not in <linux/fs.h> */
  35. #define FS_ENCRYPTION_MODE_AES_256_HEH 126
  36. #define FS_ENCRYPTION_MODE_PRIVATE 127
  37. /* new definition, not yet in Bionic's <linux/fs.h> */
  38. #ifndef FS_ENCRYPTION_MODE_ADIANTUM
  39. #define FS_ENCRYPTION_MODE_ADIANTUM 9
  40. #endif
  41. /* new definition, not yet in Bionic's <linux/fs.h> */
  42. #ifndef FS_POLICY_FLAG_DIRECT_KEY
  43. #define FS_POLICY_FLAG_DIRECT_KEY 0x4
  44. #endif
  45. #define HEX_LOOKUP "0123456789abcdef"
  46. bool fscrypt_is_native() {
  47. char value[PROPERTY_VALUE_MAX];
  48. property_get("ro.crypto.type", value, "none");
  49. return !strcmp(value, "file");
  50. }
  51. static void log_ls(const char* dirname) {
  52. std::array<const char*, 3> argv = {"ls", "-laZ", dirname};
  53. int status = 0;
  54. auto res =
  55. android_fork_execvp(argv.size(), const_cast<char**>(argv.data()), &status, false, true);
  56. if (res != 0) {
  57. PLOG(ERROR) << argv[0] << " " << argv[1] << " " << argv[2] << "failed";
  58. return;
  59. }
  60. if (!WIFEXITED(status)) {
  61. LOG(ERROR) << argv[0] << " " << argv[1] << " " << argv[2]
  62. << " did not exit normally, status: " << status;
  63. return;
  64. }
  65. if (WEXITSTATUS(status) != 0) {
  66. LOG(ERROR) << argv[0] << " " << argv[1] << " " << argv[2]
  67. << " returned failure: " << WEXITSTATUS(status);
  68. return;
  69. }
  70. }
  71. static void policy_to_hex(const char* policy, char* hex) {
  72. for (size_t i = 0, j = 0; i < FS_KEY_DESCRIPTOR_SIZE; i++) {
  73. hex[j++] = HEX_LOOKUP[(policy[i] & 0xF0) >> 4];
  74. hex[j++] = HEX_LOOKUP[policy[i] & 0x0F];
  75. }
  76. hex[FS_KEY_DESCRIPTOR_SIZE_HEX - 1] = '\0';
  77. }
  78. static bool is_dir_empty(const char *dirname, bool *is_empty)
  79. {
  80. int n = 0;
  81. auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(dirname), closedir);
  82. if (!dirp) {
  83. PLOG(ERROR) << "Unable to read directory: " << dirname;
  84. return false;
  85. }
  86. for (;;) {
  87. errno = 0;
  88. auto entry = readdir(dirp.get());
  89. if (!entry) {
  90. if (errno) {
  91. PLOG(ERROR) << "Unable to read directory: " << dirname;
  92. return false;
  93. }
  94. break;
  95. }
  96. if (strcmp(entry->d_name, "lost+found") != 0) { // Skip lost+found
  97. ++n;
  98. if (n > 2) {
  99. *is_empty = false;
  100. return true;
  101. }
  102. }
  103. }
  104. *is_empty = true;
  105. return true;
  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. static bool fscrypt_policy_set(const char *directory, const char *policy,
  124. size_t policy_length,
  125. int contents_encryption_mode,
  126. int filenames_encryption_mode) {
  127. if (policy_length != FS_KEY_DESCRIPTOR_SIZE) {
  128. LOG(ERROR) << "Policy wrong length: " << policy_length;
  129. return false;
  130. }
  131. char policy_hex[FS_KEY_DESCRIPTOR_SIZE_HEX];
  132. policy_to_hex(policy, policy_hex);
  133. int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
  134. if (fd == -1) {
  135. PLOG(ERROR) << "Failed to open directory " << directory;
  136. return false;
  137. }
  138. fscrypt_policy fp;
  139. fp.version = 0;
  140. fp.contents_encryption_mode = contents_encryption_mode;
  141. fp.filenames_encryption_mode = filenames_encryption_mode;
  142. fp.flags = fscrypt_get_policy_flags(filenames_encryption_mode);
  143. memcpy(fp.master_key_descriptor, policy, FS_KEY_DESCRIPTOR_SIZE);
  144. if (ioctl(fd, FS_IOC_SET_ENCRYPTION_POLICY, &fp)) {
  145. PLOG(ERROR) << "Failed to set encryption policy for " << directory << " to " << policy_hex
  146. << " modes " << contents_encryption_mode << "/" << filenames_encryption_mode;
  147. close(fd);
  148. return false;
  149. }
  150. close(fd);
  151. LOG(INFO) << "Policy for " << directory << " set to " << policy_hex
  152. << " modes " << contents_encryption_mode << "/" << filenames_encryption_mode;
  153. return true;
  154. }
  155. static bool fscrypt_policy_get(const char *directory, char *policy,
  156. size_t policy_length,
  157. int contents_encryption_mode,
  158. int filenames_encryption_mode) {
  159. if (policy_length != FS_KEY_DESCRIPTOR_SIZE) {
  160. LOG(ERROR) << "Policy wrong length: " << policy_length;
  161. return false;
  162. }
  163. int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
  164. if (fd == -1) {
  165. PLOG(ERROR) << "Failed to open directory " << directory;
  166. return false;
  167. }
  168. fscrypt_policy fp;
  169. memset(&fp, 0, sizeof(fscrypt_policy));
  170. if (ioctl(fd, FS_IOC_GET_ENCRYPTION_POLICY, &fp) != 0) {
  171. PLOG(ERROR) << "Failed to get encryption policy for " << directory;
  172. close(fd);
  173. log_ls(directory);
  174. return false;
  175. }
  176. close(fd);
  177. if ((fp.version != 0)
  178. || (fp.contents_encryption_mode != contents_encryption_mode)
  179. || (fp.filenames_encryption_mode != filenames_encryption_mode)
  180. || (fp.flags !=
  181. fscrypt_get_policy_flags(filenames_encryption_mode))) {
  182. LOG(ERROR) << "Failed to find matching encryption policy for " << directory;
  183. return false;
  184. }
  185. memcpy(policy, fp.master_key_descriptor, FS_KEY_DESCRIPTOR_SIZE);
  186. return true;
  187. }
  188. static bool fscrypt_policy_check(const char *directory, const char *policy,
  189. size_t policy_length,
  190. int contents_encryption_mode,
  191. int filenames_encryption_mode) {
  192. if (policy_length != FS_KEY_DESCRIPTOR_SIZE) {
  193. LOG(ERROR) << "Policy wrong length: " << policy_length;
  194. return false;
  195. }
  196. char existing_policy[FS_KEY_DESCRIPTOR_SIZE];
  197. if (!fscrypt_policy_get(directory, existing_policy, FS_KEY_DESCRIPTOR_SIZE,
  198. contents_encryption_mode,
  199. filenames_encryption_mode)) return false;
  200. char existing_policy_hex[FS_KEY_DESCRIPTOR_SIZE_HEX];
  201. policy_to_hex(existing_policy, existing_policy_hex);
  202. if (memcmp(policy, existing_policy, FS_KEY_DESCRIPTOR_SIZE) != 0) {
  203. char policy_hex[FS_KEY_DESCRIPTOR_SIZE_HEX];
  204. policy_to_hex(policy, policy_hex);
  205. LOG(ERROR) << "Found policy " << existing_policy_hex << " at " << directory
  206. << " which doesn't match expected value " << policy_hex;
  207. log_ls(directory);
  208. return false;
  209. }
  210. LOG(INFO) << "Found policy " << existing_policy_hex << " at " << directory
  211. << " which matches expected value";
  212. return true;
  213. }
  214. int fscrypt_policy_ensure(const char *directory, const char *policy,
  215. size_t policy_length,
  216. const char *contents_encryption_mode,
  217. const char *filenames_encryption_mode) {
  218. int contents_mode = 0;
  219. int filenames_mode = 0;
  220. if (!strcmp(contents_encryption_mode, "software") ||
  221. !strcmp(contents_encryption_mode, "aes-256-xts")) {
  222. contents_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
  223. } else if (!strcmp(contents_encryption_mode, "adiantum")) {
  224. contents_mode = FS_ENCRYPTION_MODE_ADIANTUM;
  225. } else if (!strcmp(contents_encryption_mode, "ice")) {
  226. contents_mode = FS_ENCRYPTION_MODE_PRIVATE;
  227. } else {
  228. LOG(ERROR) << "Invalid file contents encryption mode: "
  229. << contents_encryption_mode;
  230. return -1;
  231. }
  232. if (!strcmp(filenames_encryption_mode, "aes-256-cts")) {
  233. filenames_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
  234. } else if (!strcmp(filenames_encryption_mode, "aes-256-heh")) {
  235. filenames_mode = FS_ENCRYPTION_MODE_AES_256_HEH;
  236. } else if (!strcmp(filenames_encryption_mode, "adiantum")) {
  237. filenames_mode = FS_ENCRYPTION_MODE_ADIANTUM;
  238. } else {
  239. LOG(ERROR) << "Invalid file names encryption mode: "
  240. << filenames_encryption_mode;
  241. return -1;
  242. }
  243. bool is_empty;
  244. if (!is_dir_empty(directory, &is_empty)) return -1;
  245. if (is_empty) {
  246. if (!fscrypt_policy_set(directory, policy, policy_length,
  247. contents_mode, filenames_mode)) return -1;
  248. } else {
  249. if (!fscrypt_policy_check(directory, policy, policy_length,
  250. contents_mode, filenames_mode)) return -1;
  251. }
  252. return 0;
  253. }