blob: c4986e57c73810e11749291ef6b9fec8eb6e9cbc [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>
bigbiffa84acc72021-12-23 19:58:22 -050036#include "KeyUtil.h"
bigbiff7ba75002020-04-11 20:47:09 -040037
38#include "fscrypt_policy.h"
39
40static int encryption_mode = FS_ENCRYPTION_MODE_PRIVATE;
41
42bool fscrypt_is_native() {
bigbiffa957f072021-03-07 18:20:29 -050043 LOG(ERROR) << "fscrypt_is_native::ro.crypto.type";
bigbiff7ba75002020-04-11 20:47:09 -040044 char value[PROPERTY_VALUE_MAX];
45 property_get("ro.crypto.type", value, "none");
46 return !strcmp(value, "file");
47}
48
bigbiffa957f072021-03-07 18:20:29 -050049extern "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 }
bigbiff7ba75002020-04-11 20:47:09 -040053}
54
55static 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
85static 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
bigbiff7ba75002020-04-11 20:47:09 -0400102extern "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(":");
bigbiffa957f072021-03-07 18:20:29 -0500126 LOG(INFO) << "contents_encryption_mode_string: " << contents_encryption_mode_string.substr(0, pos);
bigbiff7ba75002020-04-11 20:47:09 -0400127
bigbiff7ba75002020-04-11 20:47:09 -0400128 if (contents_encryption_mode_string.substr(0, pos) == "software") {
129 encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
bigbiff7ba75002020-04-11 20:47:09 -0400130 } 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
bigbiffa957f072021-03-07 18:20:29 -0500141#ifdef USE_FSCRYPT_POLICY_V1
142extern "C" bool fscrypt_policy_set_struct(const char *directory, const struct fscrypt_policy_v1 *fep) {
143#else
144extern "C" bool fscrypt_policy_set_struct(const char *directory, const struct fscrypt_policy_v2 *fep) {
145#endif
bigbiff7ba75002020-04-11 20:47:09 -0400146 int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
147 if (fd == -1) {
bigbiffa84acc72021-12-23 19:58:22 -0500148 printf("failed to open %s\n", directory);
bigbiff7ba75002020-04-11 20:47:09 -0400149 PLOG(ERROR) << "Failed to open directory " << directory;
150 return false;
151 }
bigbiffa84acc72021-12-23 19:58:22 -0500152 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 }
bigbiff7ba75002020-04-11 20:47:09 -0400164 }
165 close(fd);
166 return true;
167}
168
bigbiffa957f072021-03-07 18:20:29 -0500169#ifdef USE_FSCRYPT_POLICY_V1
170extern "C" bool fscrypt_policy_get_struct(const char *directory, struct fscrypt_policy_v1 *fep) {
171#else
172extern "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);
bigbiff7ba75002020-04-11 20:47:09 -0400175 if (fd == -1) {
bigbiff7ba75002020-04-11 20:47:09 -0400176 PLOG(ERROR) << "Failed to open directory " << directory;
177 return false;
178 }
bigbiffa957f072021-03-07 18:20:29 -0500179#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
bigbiffa957f072021-03-07 18:20:29 -0500184 struct fscrypt_get_policy_ex_arg ex_policy = {0};
bigbiffa84acc72021-12-23 19:58:22 -0500185
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 }
bigbiff2e344ab2021-05-07 10:41:55 -0400193#ifdef USE_FSCRYPT_POLICY_V1
bigbiffa84acc72021-12-23 19:58:22 -0500194 memcpy(fep, &ex_policy.policy.v1, sizeof(ex_policy.policy.v1));
bigbiff2e344ab2021-05-07 10:41:55 -0400195#else
bigbiffa84acc72021-12-23 19:58:22 -0500196 memcpy(fep, &ex_policy.policy.v2, sizeof(ex_policy.policy.v2));
bigbiff2e344ab2021-05-07 10:41:55 -0400197#endif
bigbiffa84acc72021-12-23 19:58:22 -0500198 } 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 }
bigbiff7ba75002020-04-11 20:47:09 -0400206 close(fd);
207 return true;
208}