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
52 // ext4enc:TODO Get value from somewhere sensible
53 #define EXT4_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct ext4_encryption_policy)
54 #define EXT4_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct ext4_encryption_policy)
56 #define HEX_LOOKUP "0123456789abcdef"
58 bool e4crypt_is_native() {
59 char value[PROPERTY_VALUE_MAX];
60 property_get("ro.crypto.type", value, "none");
61 return !strcmp(value, "file");
64 static void policy_to_hex(const char* policy, char* hex) {
65 for (size_t i = 0, j = 0; i < EXT4_KEY_DESCRIPTOR_SIZE; i++) {
66 hex[j++] = HEX_LOOKUP[(policy[i] & 0xF0) >> 4];
67 hex[j++] = HEX_LOOKUP[policy[i] & 0x0F];
69 hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX - 1] = '\0';
72 static bool is_dir_empty(const char *dirname, bool *is_empty)
75 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(dirname), closedir);
77 PLOG(ERROR) << "Unable to read directory: " << dirname;
82 auto entry = readdir(dirp.get());
85 PLOG(ERROR) << "Unable to read directory: " << dirname;
90 if (strcmp(entry->d_name, "lost+found") != 0) { // Skip lost+found
102 static bool e4crypt_policy_set(const char *directory, const char *policy, size_t policy_length) {
103 if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
104 LOG(ERROR) << "Policy wrong length: " << policy_length;
107 int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
109 PLOG(ERROR) << "Failed to open directory " << directory;
113 ext4_encryption_policy eep;
115 eep.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
116 eep.filenames_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_CTS;
118 memcpy(eep.master_key_descriptor, policy, EXT4_KEY_DESCRIPTOR_SIZE);
119 if (ioctl(fd, EXT4_IOC_SET_ENCRYPTION_POLICY, &eep)) {
120 PLOG(ERROR) << "Failed to set encryption policy for " << directory;
126 char policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
127 policy_to_hex(policy, policy_hex);
128 LOG(INFO) << "Policy for " << directory << " set to " << policy_hex;
132 static bool e4crypt_policy_get(const char *directory, char *policy, size_t policy_length) {
133 if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
134 LOG(ERROR) << "Policy wrong length: " << policy_length;
138 int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
140 PLOG(ERROR) << "Failed to open directory " << directory;
144 ext4_encryption_policy eep;
145 memset(&eep, 0, sizeof(ext4_encryption_policy));
146 if (ioctl(fd, EXT4_IOC_GET_ENCRYPTION_POLICY, &eep) != 0) {
147 PLOG(ERROR) << "Failed to get encryption policy for " << directory;
153 if ((eep.version != 0)
154 || (eep.contents_encryption_mode != EXT4_ENCRYPTION_MODE_AES_256_XTS)
155 || (eep.filenames_encryption_mode != EXT4_ENCRYPTION_MODE_AES_256_CTS)
156 || (eep.flags != 0)) {
157 LOG(ERROR) << "Failed to find matching encryption policy for " << directory;
160 memcpy(policy, eep.master_key_descriptor, EXT4_KEY_DESCRIPTOR_SIZE);
165 static bool e4crypt_policy_check(const char *directory, const char *policy, size_t policy_length) {
166 if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
167 LOG(ERROR) << "Policy wrong length: " << policy_length;
170 char existing_policy[EXT4_KEY_DESCRIPTOR_SIZE];
171 if (!e4crypt_policy_get(directory, existing_policy, EXT4_KEY_DESCRIPTOR_SIZE)) return false;
172 char existing_policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
174 policy_to_hex(existing_policy, existing_policy_hex);
176 if (memcmp(policy, existing_policy, EXT4_KEY_DESCRIPTOR_SIZE) != 0) {
177 char policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
178 policy_to_hex(policy, policy_hex);
179 LOG(ERROR) << "Found policy " << existing_policy_hex << " at " << directory
180 << " which doesn't match expected value " << policy_hex;
183 LOG(INFO) << "Found policy " << existing_policy_hex << " at " << directory
184 << " which matches expected value";
188 int e4crypt_policy_ensure(const char *directory, const char *policy, size_t policy_length) {
190 if (!is_dir_empty(directory, &is_empty)) return -1;
192 if (!e4crypt_policy_set(directory, policy, policy_length)) return -1;
194 if (!e4crypt_policy_check(directory, policy, policy_length)) return -1;