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 
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)
55 
56 #define HEX_LOOKUP "0123456789abcdef"
57 
e4crypt_is_native()58 bool e4crypt_is_native() {
59     char value[PROPERTY_VALUE_MAX];
60     property_get("ro.crypto.type", value, "none");
61     return !strcmp(value, "file");
62 }
63 
policy_to_hex(const char * policy,char * hex)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];
68     }
69     hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX - 1] = '\0';
70 }
71 
is_dir_empty(const char * dirname,bool * is_empty)72 static bool is_dir_empty(const char *dirname, bool *is_empty)
73 {
74     int n = 0;
75     auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(dirname), closedir);
76     if (!dirp) {
77         PLOG(ERROR) << "Unable to read directory: " << dirname;
78         return false;
79     }
80     for (;;) {
81         errno = 0;
82         auto entry = readdir(dirp.get());
83         if (!entry) {
84             if (errno) {
85                 PLOG(ERROR) << "Unable to read directory: " << dirname;
86                 return false;
87             }
88             break;
89         }
90         if (strcmp(entry->d_name, "lost+found") != 0) { // Skip lost+found
91             ++n;
92             if (n > 2) {
93                 *is_empty = false;
94                 return true;
95             }
96         }
97     }
98     *is_empty = true;
99     return true;
100 }
101 
e4crypt_policy_set(const char * directory,const char * policy,size_t policy_length)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;
105         return false;
106     }
107     int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
108     if (fd == -1) {
109         PLOG(ERROR) << "Failed to open directory " << directory;
110         return false;
111     }
112 
113     ext4_encryption_policy eep;
114     eep.version = 0;
115     eep.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
116     eep.filenames_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_CTS;
117     eep.flags = 0;
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;
121         close(fd);
122         return false;
123     }
124     close(fd);
125 
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;
129     return true;
130 }
131 
e4crypt_policy_get(const char * directory,char * policy,size_t policy_length)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;
135         return false;
136     }
137 
138     int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
139     if (fd == -1) {
140         PLOG(ERROR) << "Failed to open directory " << directory;
141         return false;
142     }
143 
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;
148         close(fd);
149         return -1;
150     }
151     close(fd);
152 
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;
158         return false;
159     }
160     memcpy(policy, eep.master_key_descriptor, EXT4_KEY_DESCRIPTOR_SIZE);
161 
162     return true;
163 }
164 
e4crypt_policy_check(const char * directory,const char * policy,size_t policy_length)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;
168         return false;
169     }
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];
173 
174     policy_to_hex(existing_policy, existing_policy_hex);
175 
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;
181         return false;
182     }
183     LOG(INFO) << "Found policy " << existing_policy_hex << " at " << directory
184               << " which matches expected value";
185     return true;
186 }
187 
e4crypt_policy_ensure(const char * directory,const char * policy,size_t policy_length)188 int e4crypt_policy_ensure(const char *directory, const char *policy, size_t policy_length) {
189     bool is_empty;
190     if (!is_dir_empty(directory, &is_empty)) return -1;
191     if (is_empty) {
192         if (!e4crypt_policy_set(directory, policy, policy_length)) return -1;
193     } else {
194         if (!e4crypt_policy_check(directory, policy, policy_length)) return -1;
195     }
196     return 0;
197 }
198