2 * Copyright (c) 2015 Google, Inc.
5 #include "ext4_crypt.h"
13 #include <asm/ioctl.h>
14 #include <sys/syscall.h>
16 #include <sys/types.h>
18 #include <android-base/logging.h>
20 #define XATTR_NAME_ENCRYPTION_POLICY "encryption.policy"
21 #define EXT4_KEYREF_DELIMITER ((char)'.')
23 // ext4enc:TODO Include structure from somewhere sensible
24 // MUST be in sync with ext4_crypto.c in kernel
25 #define EXT4_KEY_DESCRIPTOR_SIZE 8
26 #define EXT4_KEY_DESCRIPTOR_SIZE_HEX 17
28 struct ext4_encryption_policy {
30 char contents_encryption_mode;
31 char filenames_encryption_mode;
33 char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
34 } __attribute__((__packed__));
36 #define EXT4_ENCRYPTION_MODE_AES_256_XTS 1
37 #define EXT4_ENCRYPTION_MODE_AES_256_CTS 4
39 // ext4enc:TODO Get value from somewhere sensible
40 #define EXT4_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct ext4_encryption_policy)
41 #define EXT4_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct ext4_encryption_policy)
43 #define HEX_LOOKUP "0123456789abcdef"
45 static void policy_to_hex(const char* policy, char* hex) {
46 for (size_t i = 0, j = 0; i < EXT4_KEY_DESCRIPTOR_SIZE; i++) {
47 hex[j++] = HEX_LOOKUP[(policy[i] & 0xF0) >> 4];
48 hex[j++] = HEX_LOOKUP[policy[i] & 0x0F];
50 hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX - 1] = '\0';
53 static bool is_dir_empty(const char *dirname, bool *is_empty)
56 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(dirname), closedir);
58 PLOG(ERROR) << "Unable to read directory: " << dirname;
63 auto entry = readdir(dirp.get());
66 PLOG(ERROR) << "Unable to read directory: " << dirname;
71 if (strcmp(entry->d_name, "lost+found") != 0) { // Skip lost+found
83 static bool e4crypt_policy_set(const char *directory, const char *policy, size_t policy_length) {
84 if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
85 LOG(ERROR) << "Policy wrong length: " << policy_length;
88 int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
90 PLOG(ERROR) << "Failed to open directory " << directory;
94 ext4_encryption_policy eep;
96 eep.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
97 eep.filenames_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_CTS;
99 memcpy(eep.master_key_descriptor, policy, EXT4_KEY_DESCRIPTOR_SIZE);
100 if (ioctl(fd, EXT4_IOC_SET_ENCRYPTION_POLICY, &eep)) {
101 PLOG(ERROR) << "Failed to set encryption policy for " << directory;
107 char policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
108 policy_to_hex(policy, policy_hex);
109 LOG(INFO) << "Policy for " << directory << " set to " << policy_hex;
113 static bool e4crypt_policy_get(const char *directory, char *policy, size_t policy_length) {
114 if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
115 LOG(ERROR) << "Policy wrong length: " << policy_length;
119 int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
121 PLOG(ERROR) << "Failed to open directory " << directory;
125 ext4_encryption_policy eep;
126 memset(&eep, 0, sizeof(ext4_encryption_policy));
127 if (ioctl(fd, EXT4_IOC_GET_ENCRYPTION_POLICY, &eep) != 0) {
128 PLOG(ERROR) << "Failed to get encryption policy for " << directory;
134 if ((eep.version != 0)
135 || (eep.contents_encryption_mode != EXT4_ENCRYPTION_MODE_AES_256_XTS)
136 || (eep.filenames_encryption_mode != EXT4_ENCRYPTION_MODE_AES_256_CTS)
137 || (eep.flags != 0)) {
138 LOG(ERROR) << "Failed to find matching encryption policy for " << directory;
141 memcpy(policy, eep.master_key_descriptor, EXT4_KEY_DESCRIPTOR_SIZE);
146 static bool e4crypt_policy_check(const char *directory, const char *policy, size_t policy_length) {
147 if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
148 LOG(ERROR) << "Policy wrong length: " << policy_length;
151 char existing_policy[EXT4_KEY_DESCRIPTOR_SIZE];
152 if (!e4crypt_policy_get(directory, existing_policy, EXT4_KEY_DESCRIPTOR_SIZE)) return false;
153 char existing_policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
155 policy_to_hex(existing_policy, existing_policy_hex);
157 if (memcmp(policy, existing_policy, EXT4_KEY_DESCRIPTOR_SIZE) != 0) {
158 char policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
159 policy_to_hex(policy, policy_hex);
160 LOG(ERROR) << "Found policy " << existing_policy_hex << " at " << directory
161 << " which doesn't match expected value " << policy_hex;
164 LOG(INFO) << "Found policy " << existing_policy_hex << " at " << directory
165 << " which matches expected value";
169 int e4crypt_policy_ensure(const char *directory, const char *policy, size_t policy_length) {
171 if (!is_dir_empty(directory, &is_empty)) return -1;
173 if (!e4crypt_policy_set(directory, policy, policy_length)) return -1;
175 if (!e4crypt_policy_check(directory, policy, policy_length)) return -1;