blob: 9ca3ed4fd06921149815444f3e402e82e2f18715 [file] [log] [blame]
bigbiff7ba75002020-04-11 20:47:09 -04001/*
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
bigbiff7ba75002020-04-11 20:47:09 -040017#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>
bigbiffa957f072021-03-07 18:20:29 -050035#include <fscrypt/fscrypt.h>
bigbiff7ba75002020-04-11 20:47:09 -040036
37#include "fscrypt_policy.h"
38
39static int encryption_mode = FS_ENCRYPTION_MODE_PRIVATE;
40
41bool fscrypt_is_native() {
bigbiffa957f072021-03-07 18:20:29 -050042 LOG(ERROR) << "fscrypt_is_native::ro.crypto.type";
bigbiff7ba75002020-04-11 20:47:09 -040043 char value[PROPERTY_VALUE_MAX];
44 property_get("ro.crypto.type", value, "none");
45 return !strcmp(value, "file");
46}
47
bigbiffa957f072021-03-07 18:20:29 -050048extern "C" void bytes_to_hex(const uint8_t *bytes, size_t num_bytes, char *hex) {
49 for (size_t i = 0; i < num_bytes; i++) {
50 sprintf(&hex[2 * i], "%02x", bytes[i]);
51 }
bigbiff7ba75002020-04-11 20:47:09 -040052}
53
54static bool is_dir_empty(const char *dirname, bool *is_empty)
55{
56 int n = 0;
57 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(dirname), closedir);
58 if (!dirp) {
59 PLOG(ERROR) << "Unable to read directory: " << dirname;
60 return false;
61 }
62 for (;;) {
63 errno = 0;
64 auto entry = readdir(dirp.get());
65 if (!entry) {
66 if (errno) {
67 PLOG(ERROR) << "Unable to read directory: " << dirname;
68 return false;
69 }
70 break;
71 }
72 if (strcmp(entry->d_name, "lost+found") != 0) { // Skip lost+found
73 ++n;
74 if (n > 2) {
75 *is_empty = false;
76 return true;
77 }
78 }
79 }
80 *is_empty = true;
81 return true;
82}
83
84static uint8_t fscrypt_get_policy_flags(int filenames_encryption_mode) {
85 if (filenames_encryption_mode == FS_ENCRYPTION_MODE_AES_256_CTS) {
86 // Use legacy padding with our original filenames encryption mode.
87 return FS_POLICY_FLAGS_PAD_4;
88 } else if (filenames_encryption_mode == FS_ENCRYPTION_MODE_ADIANTUM) {
89 // Use DIRECT_KEY for Adiantum, since it's much more efficient but just
90 // as secure since Android doesn't reuse the same master key for
91 // multiple encryption modes
92 return (FS_POLICY_FLAGS_PAD_16 | FS_POLICY_FLAG_DIRECT_KEY);
93 }
94 // With a new mode we can use the better padding flag without breaking existing devices: pad
95 // filenames with zeroes to the next 16-byte boundary. This is more secure (helps hide the
96 // length of filenames) and makes the inputs evenly divisible into blocks which is more
97 // efficient for encryption and decryption.
98 return FS_POLICY_FLAGS_PAD_16;
99}
100
bigbiff7ba75002020-04-11 20:47:09 -0400101extern "C" bool fscrypt_set_mode() {
102 const char* mode_file = "/data/unencrypted/mode";
103 struct stat st;
104 if (stat(mode_file, &st) != 0 || st.st_size <= 0) {
105 printf("Invalid encryption mode file %s\n", mode_file);
106 return false;
107 }
108 size_t mode_size = st.st_size;
109 char contents_encryption_mode[mode_size + 1];
110 memset((void*)contents_encryption_mode, 0, mode_size + 1);
111 int fd = open(mode_file, O_RDONLY);
112 if (fd < 0) {
113 printf("error opening '%s': %s\n", mode_file, strerror(errno));
114 return false;
115 }
116 if (read(fd, contents_encryption_mode, mode_size) != mode_size) {
117 printf("read error on '%s': %s\n", mode_file, strerror(errno));
118 close(fd);
119 return false;
120 }
121 close(fd);
122
123 std::string contents_encryption_mode_string = std::string(contents_encryption_mode);
124 int pos = contents_encryption_mode_string.find(":");
bigbiffa957f072021-03-07 18:20:29 -0500125 LOG(INFO) << "contents_encryption_mode_string: " << contents_encryption_mode_string.substr(0, pos);
bigbiff7ba75002020-04-11 20:47:09 -0400126
bigbiff7ba75002020-04-11 20:47:09 -0400127 if (contents_encryption_mode_string.substr(0, pos) == "software") {
128 encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
bigbiff7ba75002020-04-11 20:47:09 -0400129 } else if (contents_encryption_mode_string.substr(0, pos) == "ice") {
130 encryption_mode = FS_ENCRYPTION_MODE_PRIVATE;
131 } else {
132 printf("Invalid encryption mode '%s'\n", contents_encryption_mode);
133 return false;
134 }
135
136 printf("set encryption mode to %i\n", encryption_mode);
137 return true;
138}
139
bigbiffa957f072021-03-07 18:20:29 -0500140#ifdef USE_FSCRYPT_POLICY_V1
141extern "C" bool fscrypt_policy_set_struct(const char *directory, const struct fscrypt_policy_v1 *fep) {
142#else
143extern "C" bool fscrypt_policy_set_struct(const char *directory, const struct fscrypt_policy_v2 *fep) {
144#endif
bigbiff7ba75002020-04-11 20:47:09 -0400145 int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
146 if (fd == -1) {
147 printf("failed to open %s\n", directory);
148 PLOG(ERROR) << "Failed to open directory " << directory;
149 return false;
150 }
151 if (ioctl(fd, FS_IOC_SET_ENCRYPTION_POLICY, fep)) {
bigbiff7ba75002020-04-11 20:47:09 -0400152 PLOG(ERROR) << "Failed to set encryption policy for " << directory;
153 close(fd);
154 return false;
155 }
156 close(fd);
157 return true;
158}
159
bigbiffa957f072021-03-07 18:20:29 -0500160#ifdef USE_FSCRYPT_POLICY_V1
161extern "C" bool fscrypt_policy_get_struct(const char *directory, struct fscrypt_policy_v1 *fep) {
162#else
163extern "C" bool fscrypt_policy_get_struct(const char *directory, struct fscrypt_policy_v2 *fep) {
164#endif
165 int fd = open(directory, O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
bigbiff7ba75002020-04-11 20:47:09 -0400166 if (fd == -1) {
bigbiff7ba75002020-04-11 20:47:09 -0400167 PLOG(ERROR) << "Failed to open directory " << directory;
168 return false;
169 }
bigbiffa957f072021-03-07 18:20:29 -0500170#ifdef USE_FSCRYPT_POLICY_V1
171 memset(fep, 0, sizeof(fscrypt_policy_v1));
172#else
173 memset(fep, 0, sizeof(fscrypt_policy_v2));
174#endif
175
176 struct fscrypt_get_policy_ex_arg ex_policy = {0};
177 ex_policy.policy_size = sizeof(ex_policy.policy);
178 if (ioctl(fd, FS_IOC_GET_ENCRYPTION_POLICY_EX, &ex_policy) != 0) {
bigbiff7ba75002020-04-11 20:47:09 -0400179 PLOG(ERROR) << "Failed to get encryption policy for " << directory;
180 close(fd);
181 return false;
182 }
bigbiff2e344ab2021-05-07 10:41:55 -0400183#ifdef USE_FSCRYPT_POLICY_V1
184 memcpy(fep, &ex_policy.policy.v1, sizeof(ex_policy.policy.v1));
185#else
bigbiffa957f072021-03-07 18:20:29 -0500186 memcpy(fep, &ex_policy.policy.v2, sizeof(ex_policy.policy.v2));
bigbiff2e344ab2021-05-07 10:41:55 -0400187#endif
bigbiff7ba75002020-04-11 20:47:09 -0400188 close(fd);
189 return true;
190}