2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "ext4_crypt.h"
25 #include <asm/ioctl.h>
26 #include <sys/syscall.h>
28 #include <sys/types.h>
30 #include <android-base/logging.h>
31 #include <cutils/properties.h>
33 #define XATTR_NAME_ENCRYPTION_POLICY "encryption.policy"
34 #define EXT4_KEYREF_DELIMITER ((char)'.')
36 // ext4enc:TODO Include structure from somewhere sensible
37 // MUST be in sync with ext4_crypto.c in kernel
38 #define EXT4_KEY_DESCRIPTOR_SIZE 8
39 #define EXT4_KEY_DESCRIPTOR_SIZE_HEX 17
41 struct ext4_encryption_policy {
43 char contents_encryption_mode;
44 char filenames_encryption_mode;
46 char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
47 } __attribute__((__packed__));
49 #define EXT4_ENCRYPTION_MODE_AES_256_XTS 1
50 #define EXT4_ENCRYPTION_MODE_AES_256_CTS 4
51 #define EXT4_ENCRYPTION_MODE_PRIVATE 127
53 // ext4enc:TODO Get value from somewhere sensible
54 #define EXT4_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct ext4_encryption_policy)
55 #define EXT4_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct ext4_encryption_policy)
57 #define HEX_LOOKUP "0123456789abcdef"
59 bool e4crypt_is_native() {
60 char value[PROPERTY_VALUE_MAX];
61 property_get("ro.crypto.type", value, "none");
62 return !strcmp(value, "file");
65 static void policy_to_hex(const char* policy, char* hex) {
66 for (size_t i = 0, j = 0; i < EXT4_KEY_DESCRIPTOR_SIZE; i++) {
67 hex[j++] = HEX_LOOKUP[(policy[i] & 0xF0) >> 4];
68 hex[j++] = HEX_LOOKUP[policy[i] & 0x0F];
70 hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX - 1] = '\0';
73 static bool is_dir_empty(const char *dirname, bool *is_empty)
76 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(dirname), closedir);
78 PLOG(ERROR) << "Unable to read directory: " << dirname;
83 auto entry = readdir(dirp.get());
86 PLOG(ERROR) << "Unable to read directory: " << dirname;
91 if (strcmp(entry->d_name, "lost+found") != 0) { // Skip lost+found
103 static bool e4crypt_policy_set(const char *directory, const char *policy, size_t policy_length) {
104 if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
105 LOG(ERROR) << "Policy wrong length: " << policy_length;
108 int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
110 PLOG(ERROR) << "Failed to open directory " << directory;
114 ext4_encryption_policy eep;
116 eep.contents_encryption_mode = EXT4_ENCRYPTION_MODE_PRIVATE;
117 eep.filenames_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_CTS;
119 memcpy(eep.master_key_descriptor, policy, EXT4_KEY_DESCRIPTOR_SIZE);
120 if (ioctl(fd, EXT4_IOC_SET_ENCRYPTION_POLICY, &eep)) {
121 PLOG(ERROR) << "Failed to set encryption policy for " << directory;
127 char policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
128 policy_to_hex(policy, policy_hex);
129 LOG(INFO) << "Policy for " << directory << " set to " << policy_hex;
133 static bool e4crypt_policy_get(const char *directory, char *policy, size_t policy_length) {
134 if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
135 LOG(ERROR) << "Policy wrong length: " << policy_length;
139 int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
141 PLOG(ERROR) << "Failed to open directory " << directory;
145 ext4_encryption_policy eep;
146 memset(&eep, 0, sizeof(ext4_encryption_policy));
147 if (ioctl(fd, EXT4_IOC_GET_ENCRYPTION_POLICY, &eep) != 0) {
148 PLOG(ERROR) << "Failed to get encryption policy for " << directory;
154 if ((eep.version != 0)
155 || (eep.contents_encryption_mode != EXT4_ENCRYPTION_MODE_PRIVATE)
156 || (eep.filenames_encryption_mode != EXT4_ENCRYPTION_MODE_AES_256_CTS)
157 || (eep.flags != 0)) {
158 LOG(ERROR) << "Failed to find matching encryption policy for " << directory;
161 memcpy(policy, eep.master_key_descriptor, EXT4_KEY_DESCRIPTOR_SIZE);
166 static bool e4crypt_policy_check(const char *directory, const char *policy, size_t policy_length) {
167 if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
168 LOG(ERROR) << "Policy wrong length: " << policy_length;
171 char existing_policy[EXT4_KEY_DESCRIPTOR_SIZE];
172 if (!e4crypt_policy_get(directory, existing_policy, EXT4_KEY_DESCRIPTOR_SIZE)) return false;
173 char existing_policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
175 policy_to_hex(existing_policy, existing_policy_hex);
177 if (memcmp(policy, existing_policy, EXT4_KEY_DESCRIPTOR_SIZE) != 0) {
178 char policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
179 policy_to_hex(policy, policy_hex);
180 LOG(ERROR) << "Found policy " << existing_policy_hex << " at " << directory
181 << " which doesn't match expected value " << policy_hex;
184 LOG(INFO) << "Found policy " << existing_policy_hex << " at " << directory
185 << " which matches expected value";
189 int e4crypt_policy_ensure(const char *directory, const char *policy, size_t policy_length) {
191 if (!is_dir_empty(directory, &is_empty)) return -1;
193 if (!e4crypt_policy_set(directory, policy, policy_length)) return -1;
195 if (!e4crypt_policy_check(directory, policy, policy_length)) return -1;