OSDN Git Service

Incremental Ext4 Bug: prevent out of bound boundary allocations
[android-x86/system-extras.git] / ext4_utils / ext4_crypt.cpp
1 /*
2  * Copyright (c) 2015 Google, Inc.
3  */
4
5 #include "ext4_crypt.h"
6
7 #include <dirent.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <unistd.h>
11
12 #include <fcntl.h>
13 #include <asm/ioctl.h>
14 #include <sys/syscall.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17
18 #include <android-base/logging.h>
19
20 #define XATTR_NAME_ENCRYPTION_POLICY "encryption.policy"
21 #define EXT4_KEYREF_DELIMITER ((char)'.')
22
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
27
28 struct ext4_encryption_policy {
29     char version;
30     char contents_encryption_mode;
31     char filenames_encryption_mode;
32     char flags;
33     char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
34 } __attribute__((__packed__));
35
36 #define EXT4_ENCRYPTION_MODE_AES_256_XTS    1
37 #define EXT4_ENCRYPTION_MODE_AES_256_CTS    4
38
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)
42
43 #define HEX_LOOKUP "0123456789abcdef"
44
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];
49     }
50     hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX - 1] = '\0';
51 }
52
53 static bool is_dir_empty(const char *dirname, bool *is_empty)
54 {
55     int n = 0;
56     auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(dirname), closedir);
57     if (!dirp) {
58         PLOG(ERROR) << "Unable to read directory: " << dirname;
59         return false;
60     }
61     for (;;) {
62         errno = 0;
63         auto entry = readdir(dirp.get());
64         if (!entry) {
65             if (errno) {
66                 PLOG(ERROR) << "Unable to read directory: " << dirname;
67                 return false;
68             }
69             break;
70         }
71         if (strcmp(entry->d_name, "lost+found") != 0) { // Skip lost+found
72             ++n;
73             if (n > 2) {
74                 *is_empty = false;
75                 return true;
76             }
77         }
78     }
79     *is_empty = true;
80     return true;
81 }
82
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;
86         return false;
87     }
88     int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
89     if (fd == -1) {
90         PLOG(ERROR) << "Failed to open directory " << directory;
91         return false;
92     }
93
94     ext4_encryption_policy eep;
95     eep.version = 0;
96     eep.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
97     eep.filenames_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_CTS;
98     eep.flags = 0;
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;
102         close(fd);
103         return false;
104     }
105     close(fd);
106
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;
110     return true;
111 }
112
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;
116         return false;
117     }
118
119     int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
120     if (fd == -1) {
121         PLOG(ERROR) << "Failed to open directory " << directory;
122         return false;
123     }
124
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;
129         close(fd);
130         return -1;
131     }
132     close(fd);
133
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;
139         return false;
140     }
141     memcpy(policy, eep.master_key_descriptor, EXT4_KEY_DESCRIPTOR_SIZE);
142
143     return true;
144 }
145
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;
149         return false;
150     }
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];
154
155     policy_to_hex(existing_policy, existing_policy_hex);
156
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;
162         return false;
163     }
164     LOG(INFO) << "Found policy " << existing_policy_hex << " at " << directory
165               << " which matches expected value";
166     return true;
167 }
168
169 int e4crypt_policy_ensure(const char *directory, const char *policy, size_t policy_length) {
170     bool is_empty;
171     if (!is_dir_empty(directory, &is_empty)) return -1;
172     if (is_empty) {
173         if (!e4crypt_policy_set(directory, policy, policy_length)) return -1;
174     } else {
175         if (!e4crypt_policy_check(directory, policy, policy_length)) return -1;
176     }
177     return 0;
178 }