OSDN Git Service

Merge "simpleperf: fix x86 register dump on x86_64."
[android-x86/system-extras.git] / ext4_utils / ext4_crypt.cpp
1 /*
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
17 #include "ext4_utils/ext4_crypt.h"
18
19 #include <asm/ioctl.h>
20 #include <dirent.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <sys/syscall.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <android-base/file.h>
30 #include <android-base/logging.h>
31 #include <cutils/properties.h>
32
33 #define XATTR_NAME_ENCRYPTION_POLICY "encryption.policy"
34 #define EXT4_KEYREF_DELIMITER ((char)'.')
35
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
40
41 struct ext4_encryption_policy {
42     char version;
43     char contents_encryption_mode;
44     char filenames_encryption_mode;
45     char flags;
46     char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
47 } __attribute__((__packed__));
48
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
52
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)
56
57 #define HEX_LOOKUP "0123456789abcdef"
58
59 bool e4crypt_is_native() {
60     char value[PROPERTY_VALUE_MAX];
61     property_get("ro.crypto.type", value, "none");
62     return !strcmp(value, "file");
63 }
64
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];
69     }
70     hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX - 1] = '\0';
71 }
72
73 static bool is_dir_empty(const char *dirname, bool *is_empty)
74 {
75     int n = 0;
76     auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(dirname), closedir);
77     if (!dirp) {
78         PLOG(ERROR) << "Unable to read directory: " << dirname;
79         return false;
80     }
81     for (;;) {
82         errno = 0;
83         auto entry = readdir(dirp.get());
84         if (!entry) {
85             if (errno) {
86                 PLOG(ERROR) << "Unable to read directory: " << dirname;
87                 return false;
88             }
89             break;
90         }
91         if (strcmp(entry->d_name, "lost+found") != 0) { // Skip lost+found
92             ++n;
93             if (n > 2) {
94                 *is_empty = false;
95                 return true;
96             }
97         }
98     }
99     *is_empty = true;
100     return true;
101 }
102
103 static bool e4crypt_policy_set(const char *directory, const char *policy,
104                                size_t policy_length, int contents_encryption_mode) {
105     if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
106         LOG(ERROR) << "Policy wrong length: " << policy_length;
107         return false;
108     }
109     int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
110     if (fd == -1) {
111         PLOG(ERROR) << "Failed to open directory " << directory;
112         return false;
113     }
114
115     ext4_encryption_policy eep;
116     eep.version = 0;
117     eep.contents_encryption_mode = contents_encryption_mode;
118     eep.filenames_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_CTS;
119     eep.flags = 0;
120     memcpy(eep.master_key_descriptor, policy, EXT4_KEY_DESCRIPTOR_SIZE);
121     if (ioctl(fd, EXT4_IOC_SET_ENCRYPTION_POLICY, &eep)) {
122         PLOG(ERROR) << "Failed to set encryption policy for " << directory;
123         close(fd);
124         return false;
125     }
126     close(fd);
127
128     char policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
129     policy_to_hex(policy, policy_hex);
130     LOG(INFO) << "Policy for " << directory << " set to " << policy_hex;
131     return true;
132 }
133
134 static bool e4crypt_policy_get(const char *directory, char *policy,
135                                size_t policy_length, int contents_encryption_mode) {
136     if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
137         LOG(ERROR) << "Policy wrong length: " << policy_length;
138         return false;
139     }
140
141     int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
142     if (fd == -1) {
143         PLOG(ERROR) << "Failed to open directory " << directory;
144         return false;
145     }
146
147     ext4_encryption_policy eep;
148     memset(&eep, 0, sizeof(ext4_encryption_policy));
149     if (ioctl(fd, EXT4_IOC_GET_ENCRYPTION_POLICY, &eep) != 0) {
150         PLOG(ERROR) << "Failed to get encryption policy for " << directory;
151         close(fd);
152         return false;
153     }
154     close(fd);
155
156     if ((eep.version != 0)
157             || (eep.contents_encryption_mode != contents_encryption_mode)
158             || (eep.filenames_encryption_mode != EXT4_ENCRYPTION_MODE_AES_256_CTS)
159             || (eep.flags != 0)) {
160         LOG(ERROR) << "Failed to find matching encryption policy for " << directory;
161         return false;
162     }
163     memcpy(policy, eep.master_key_descriptor, EXT4_KEY_DESCRIPTOR_SIZE);
164
165     return true;
166 }
167
168 static bool e4crypt_policy_check(const char *directory, const char *policy,
169                                  size_t policy_length, int contents_encryption_mode) {
170     if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
171         LOG(ERROR) << "Policy wrong length: " << policy_length;
172         return false;
173     }
174     char existing_policy[EXT4_KEY_DESCRIPTOR_SIZE];
175     if (!e4crypt_policy_get(directory, existing_policy, EXT4_KEY_DESCRIPTOR_SIZE,
176                             contents_encryption_mode)) return false;
177     char existing_policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
178
179     policy_to_hex(existing_policy, existing_policy_hex);
180
181     if (memcmp(policy, existing_policy, EXT4_KEY_DESCRIPTOR_SIZE) != 0) {
182         char policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
183         policy_to_hex(policy, policy_hex);
184         LOG(ERROR) << "Found policy " << existing_policy_hex << " at " << directory
185                    << " which doesn't match expected value " << policy_hex;
186         return false;
187     }
188     LOG(INFO) << "Found policy " << existing_policy_hex << " at " << directory
189               << " which matches expected value";
190     return true;
191 }
192
193 int e4crypt_policy_ensure(const char *directory, const char *policy,
194                           size_t policy_length, const char* contents_encryption_mode) {
195     int mode = 0;
196     if (!strcmp(contents_encryption_mode, "software")) {
197         mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
198     } else if (!strcmp(contents_encryption_mode, "ice")) {
199         mode = EXT4_ENCRYPTION_MODE_PRIVATE;
200     } else {
201         LOG(ERROR) << "Invalid encryption mode";
202         return -1;
203     }
204
205     bool is_empty;
206     if (!is_dir_empty(directory, &is_empty)) return -1;
207     if (is_empty) {
208         if (!e4crypt_policy_set(directory, policy, policy_length,
209                                 mode)) return -1;
210     } else {
211         if (!e4crypt_policy_check(directory, policy, policy_length,
212                                   mode)) return -1;
213     }
214     return 0;
215 }