OSDN Git Service

Merge d83a851b136575e235a5727aedfec7d5f2a86ce3 on remote branch
[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_crypt.h"
18
19 #include <dirent.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <fcntl.h>
25 #include <asm/ioctl.h>
26 #include <sys/syscall.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29
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, size_t policy_length) {
104     if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
105         LOG(ERROR) << "Policy wrong length: " << policy_length;
106         return false;
107     }
108     int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
109     if (fd == -1) {
110         PLOG(ERROR) << "Failed to open directory " << directory;
111         return false;
112     }
113
114     ext4_encryption_policy eep;
115     eep.version = 0;
116     eep.contents_encryption_mode = EXT4_ENCRYPTION_MODE_PRIVATE;
117     eep.filenames_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_CTS;
118     eep.flags = 0;
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;
122         close(fd);
123         return false;
124     }
125     close(fd);
126
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;
130     return true;
131 }
132
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;
136         return false;
137     }
138
139     int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
140     if (fd == -1) {
141         PLOG(ERROR) << "Failed to open directory " << directory;
142         return false;
143     }
144
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;
149         close(fd);
150         return -1;
151     }
152     close(fd);
153
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;
159         return false;
160     }
161     memcpy(policy, eep.master_key_descriptor, EXT4_KEY_DESCRIPTOR_SIZE);
162
163     return true;
164 }
165
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;
169         return false;
170     }
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];
174
175     policy_to_hex(existing_policy, existing_policy_hex);
176
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;
182         return false;
183     }
184     LOG(INFO) << "Found policy " << existing_policy_hex << " at " << directory
185               << " which matches expected value";
186     return true;
187 }
188
189 int e4crypt_policy_ensure(const char *directory, const char *policy, size_t policy_length) {
190     bool is_empty;
191     if (!is_dir_empty(directory, &is_empty)) return -1;
192     if (is_empty) {
193         if (!e4crypt_policy_set(directory, policy, policy_length)) return -1;
194     } else {
195         if (!e4crypt_policy_check(directory, policy, policy_length)) return -1;
196     }
197     return 0;
198 }