blob: 95cbfb742082752fbf1eb150d01e6b0a419d00d3 [file] [log] [blame]
Paul Lawrence731a7a22015-04-28 22:14:15 +00001#include "Ext4Crypt.h"
2
Paul Lawrencefd7db732015-04-10 07:48:51 -07003#include <iomanip>
Paul Lawrence731a7a22015-04-28 22:14:15 +00004#include <map>
Paul Lawrencefd7db732015-04-10 07:48:51 -07005#include <fstream>
6#include <string>
7#include <sstream>
Paul Lawrence731a7a22015-04-28 22:14:15 +00008
9#include <errno.h>
Paul Crowley95376d62015-05-06 15:04:43 +010010#include <dirent.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000011#include <sys/mount.h>
Paul Crowley95376d62015-05-06 15:04:43 +010012#include <sys/types.h>
13#include <sys/stat.h>
14#include <fcntl.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000015#include <cutils/properties.h>
Paul Lawrencefd7db732015-04-10 07:48:51 -070016#include <openssl/sha.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000017
18#include "unencrypted_properties.h"
19#include "key_control.h"
20#include "cryptfs.h"
Paul Crowley95376d62015-05-06 15:04:43 +010021#include "ext4_crypt_init_extensions.h"
Paul Lawrence731a7a22015-04-28 22:14:15 +000022
23#define LOG_TAG "Ext4Crypt"
24#include "cutils/log.h"
25#include <cutils/klog.h>
Paul Crowley95376d62015-05-06 15:04:43 +010026#include <base/file.h>
27#include <base/stringprintf.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000028
29namespace {
30 // Key length in bits
31 const int key_length = 128;
Paul Lawrencefd7db732015-04-10 07:48:51 -070032 static_assert(key_length % 8 == 0,
33 "Key length must be multiple of 8 bits");
Paul Lawrence731a7a22015-04-28 22:14:15 +000034
Paul Lawrence86c942a2015-05-06 13:53:43 -070035 // How long do we store passwords for?
36 const int password_max_age_seconds = 60;
37
Paul Lawrence731a7a22015-04-28 22:14:15 +000038 // How is device encrypted
39 struct keys {
40 std::string master_key;
41 std::string password;
Paul Lawrence86c942a2015-05-06 13:53:43 -070042 time_t expiry_time;
Paul Lawrence731a7a22015-04-28 22:14:15 +000043 };
44 std::map<std::string, keys> s_key_store;
45
Paul Lawrencefd7db732015-04-10 07:48:51 -070046 // ext4enc:TODO get these consts from somewhere good
47 const int SHA512_LENGTH = 64;
48 const int EXT4_KEY_DESCRIPTOR_SIZE = 8;
49
Paul Lawrence731a7a22015-04-28 22:14:15 +000050 // ext4enc:TODO Include structure from somewhere sensible
51 // MUST be in sync with ext4_crypto.c in kernel
Paul Lawrencefd7db732015-04-10 07:48:51 -070052 const int EXT4_MAX_KEY_SIZE = 64;
53 const int EXT4_ENCRYPTION_MODE_AES_256_XTS = 1;
Paul Lawrence731a7a22015-04-28 22:14:15 +000054 struct ext4_encryption_key {
Paul Lawrencefd7db732015-04-10 07:48:51 -070055 uint32_t mode;
56 char raw[EXT4_MAX_KEY_SIZE];
57 uint32_t size;
Paul Lawrence731a7a22015-04-28 22:14:15 +000058 };
59
60 namespace tag {
61 const char* magic = "magic";
62 const char* major_version = "major_version";
63 const char* minor_version = "minor_version";
64 const char* flags = "flags";
65 const char* crypt_type = "crypt_type";
66 const char* failed_decrypt_count = "failed_decrypt_count";
67 const char* crypto_type_name = "crypto_type_name";
68 const char* master_key = "master_key";
69 const char* salt = "salt";
70 const char* kdf_type = "kdf_type";
71 const char* N_factor = "N_factor";
72 const char* r_factor = "r_factor";
73 const char* p_factor = "p_factor";
74 const char* keymaster_blob = "keymaster_blob";
75 const char* scrypted_intermediate_key = "scrypted_intermediate_key";
76 }
77}
78
Paul Crowley95376d62015-05-06 15:04:43 +010079static std::string e4crypt_install_key(const std::string &key);
Paul Crowleyf25a35a2015-05-06 13:38:53 +010080
Paul Lawrence731a7a22015-04-28 22:14:15 +000081static int put_crypt_ftr_and_key(const crypt_mnt_ftr& crypt_ftr,
82 UnencryptedProperties& props)
83{
84 SLOGI("Putting crypt footer");
85
86 bool success = props.Set<int>(tag::magic, crypt_ftr.magic)
87 && props.Set<int>(tag::major_version, crypt_ftr.major_version)
88 && props.Set<int>(tag::minor_version, crypt_ftr.minor_version)
89 && props.Set<int>(tag::flags, crypt_ftr.flags)
90 && props.Set<int>(tag::crypt_type, crypt_ftr.crypt_type)
91 && props.Set<int>(tag::failed_decrypt_count,
92 crypt_ftr.failed_decrypt_count)
93 && props.Set<std::string>(tag::crypto_type_name,
94 std::string(reinterpret_cast<const char*>(crypt_ftr.crypto_type_name)))
95 && props.Set<std::string>(tag::master_key,
96 std::string((const char*) crypt_ftr.master_key,
97 crypt_ftr.keysize))
98 && props.Set<std::string>(tag::salt,
99 std::string((const char*) crypt_ftr.salt,
100 SALT_LEN))
101 && props.Set<int>(tag::kdf_type, crypt_ftr.kdf_type)
102 && props.Set<int>(tag::N_factor, crypt_ftr.N_factor)
103 && props.Set<int>(tag::r_factor, crypt_ftr.r_factor)
104 && props.Set<int>(tag::p_factor, crypt_ftr.p_factor)
105 && props.Set<std::string>(tag::keymaster_blob,
106 std::string((const char*) crypt_ftr.keymaster_blob,
107 crypt_ftr.keymaster_blob_size))
108 && props.Set<std::string>(tag::scrypted_intermediate_key,
109 std::string((const char*) crypt_ftr.scrypted_intermediate_key,
110 SCRYPT_LEN));
111 return success ? 0 : -1;
112}
113
114static int get_crypt_ftr_and_key(crypt_mnt_ftr& crypt_ftr,
115 const UnencryptedProperties& props)
116{
117 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
118 crypt_ftr.magic = props.Get<int>(tag::magic);
119 crypt_ftr.major_version = props.Get<int>(tag::major_version);
120 crypt_ftr.minor_version = props.Get<int>(tag::minor_version);
Paul Lawrence0d9cd9e2015-05-05 15:58:27 -0700121 crypt_ftr.ftr_size = sizeof(crypt_ftr);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000122 crypt_ftr.flags = props.Get<int>(tag::flags);
123 crypt_ftr.crypt_type = props.Get<int>(tag::crypt_type);
124 crypt_ftr.failed_decrypt_count = props.Get<int>(tag::failed_decrypt_count);
125 std::string crypto_type_name = props.Get<std::string>(tag::crypto_type_name);
126 strlcpy(reinterpret_cast<char*>(crypt_ftr.crypto_type_name),
127 crypto_type_name.c_str(),
128 sizeof(crypt_ftr.crypto_type_name));
129 std::string master_key = props.Get<std::string>(tag::master_key);
130 crypt_ftr.keysize = master_key.size();
131 if (crypt_ftr.keysize > sizeof(crypt_ftr.master_key)) {
132 SLOGE("Master key size too long");
133 return -1;
134 }
135 memcpy(crypt_ftr.master_key, &master_key[0], crypt_ftr.keysize);
136 std::string salt = props.Get<std::string>(tag::salt);
137 if (salt.size() != SALT_LEN) {
138 SLOGE("Salt wrong length");
139 return -1;
140 }
141 memcpy(crypt_ftr.salt, &salt[0], SALT_LEN);
142 crypt_ftr.kdf_type = props.Get<int>(tag::kdf_type);
143 crypt_ftr.N_factor = props.Get<int>(tag::N_factor);
144 crypt_ftr.r_factor = props.Get<int>(tag::r_factor);
145 crypt_ftr.p_factor = props.Get<int>(tag::p_factor);
146 std::string keymaster_blob = props.Get<std::string>(tag::keymaster_blob);
147 crypt_ftr.keymaster_blob_size = keymaster_blob.size();
148 if (crypt_ftr.keymaster_blob_size > sizeof(crypt_ftr.keymaster_blob)) {
149 SLOGE("Keymaster blob too long");
150 return -1;
151 }
152 memcpy(crypt_ftr.keymaster_blob, &keymaster_blob[0],
153 crypt_ftr.keymaster_blob_size);
154 std::string scrypted_intermediate_key = props.Get<std::string>(tag::scrypted_intermediate_key);
155 if (scrypted_intermediate_key.size() != SCRYPT_LEN) {
156 SLOGE("scrypted intermediate key wrong length");
157 return -1;
158 }
159 memcpy(crypt_ftr.scrypted_intermediate_key, &scrypted_intermediate_key[0],
160 SCRYPT_LEN);
161
162 return 0;
163}
164
165static UnencryptedProperties GetProps(const char* path)
166{
167 return UnencryptedProperties(path);
168}
169
170static UnencryptedProperties GetAltProps(const char* path)
171{
172 return UnencryptedProperties((std::string() + path + "/tmp_mnt").c_str());
173}
174
175static UnencryptedProperties GetPropsOrAltProps(const char* path)
176{
177 UnencryptedProperties props = GetProps(path);
178 if (props.OK()) {
179 return props;
180 }
181 return GetAltProps(path);
182}
183
184int e4crypt_enable(const char* path)
185{
186 // Already enabled?
187 if (s_key_store.find(path) != s_key_store.end()) {
188 return 0;
189 }
190
191 // Not an encryptable device?
192 UnencryptedProperties key_props = GetProps(path).GetChild(properties::key);
193 if (!key_props.OK()) {
194 return 0;
195 }
196
197 if (key_props.Get<std::string>(tag::master_key).empty()) {
198 crypt_mnt_ftr ftr;
199 if (cryptfs_create_default_ftr(&ftr, key_length)) {
200 SLOGE("Failed to create crypto footer");
201 return -1;
202 }
203
Paul Lawrence0d9cd9e2015-05-05 15:58:27 -0700204 // Scrub fields not used by ext4enc
205 ftr.persist_data_offset[0] = 0;
206 ftr.persist_data_offset[1] = 0;
207 ftr.persist_data_size = 0;
208
Paul Lawrence731a7a22015-04-28 22:14:15 +0000209 if (put_crypt_ftr_and_key(ftr, key_props)) {
210 SLOGE("Failed to write crypto footer");
211 return -1;
212 }
213
214 crypt_mnt_ftr ftr2;
215 if (get_crypt_ftr_and_key(ftr2, key_props)) {
216 SLOGE("Failed to read crypto footer back");
217 return -1;
218 }
219
220 if (memcmp(&ftr, &ftr2, sizeof(ftr)) != 0) {
221 SLOGE("Crypto footer not correctly written");
Paul Lawrence0d9cd9e2015-05-05 15:58:27 -0700222 return -1;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000223 }
224 }
225
226 if (!UnencryptedProperties(path).Remove(properties::ref)) {
227 SLOGE("Failed to remove key ref");
228 return -1;
229 }
230
231 return e4crypt_check_passwd(path, "");
232}
233
234int e4crypt_change_password(const char* path, int crypt_type,
235 const char* password)
236{
237 SLOGI("e4crypt_change_password");
Paul Lawrencea56d3132015-05-04 15:48:24 -0700238 auto key_props = GetProps(path).GetChild(properties::key);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000239
240 crypt_mnt_ftr ftr;
241 if (get_crypt_ftr_and_key(ftr, key_props)) {
242 SLOGE("Failed to read crypto footer back");
243 return -1;
244 }
245
246 auto mki = s_key_store.find(path);
247 if (mki == s_key_store.end()) {
248 SLOGE("No stored master key - can't change password");
249 return -1;
250 }
251
Paul Crowley95376d62015-05-06 15:04:43 +0100252 const unsigned char* master_key_bytes
Paul Lawrence731a7a22015-04-28 22:14:15 +0000253 = reinterpret_cast<const unsigned char*>(&mki->second.master_key[0]);
254
Paul Crowley95376d62015-05-06 15:04:43 +0100255 if (cryptfs_set_password(&ftr, password, master_key_bytes)) {
Paul Lawrence731a7a22015-04-28 22:14:15 +0000256 SLOGE("Failed to set password");
257 return -1;
258 }
259
260 ftr.crypt_type = crypt_type;
261
262 if (put_crypt_ftr_and_key(ftr, key_props)) {
263 SLOGE("Failed to write crypto footer");
264 return -1;
265 }
266
267 if (!UnencryptedProperties(path).Set(properties::is_default,
268 crypt_type == CRYPT_TYPE_DEFAULT)) {
269 SLOGE("Failed to update default flag");
270 return -1;
271 }
272
273 return 0;
274}
275
276int e4crypt_crypto_complete(const char* path)
277{
278 SLOGI("ext4 crypto complete called on %s", path);
Paul Lawrencea56d3132015-05-04 15:48:24 -0700279 auto key_props = GetPropsOrAltProps(path).GetChild(properties::key);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000280 if (key_props.Get<std::string>(tag::master_key).empty()) {
281 SLOGI("No master key, so not ext4enc");
282 return -1;
283 }
284
285 return 0;
286}
287
Paul Crowley93363482015-07-07 15:17:22 +0100288// Get raw keyref - used to make keyname and to pass to ioctl
Paul Lawrencefd7db732015-04-10 07:48:51 -0700289static std::string generate_key_ref(const char* key, int length)
290{
291 SHA512_CTX c;
292
293 SHA512_Init(&c);
294 SHA512_Update(&c, key, length);
295 unsigned char key_ref1[SHA512_LENGTH];
296 SHA512_Final(key_ref1, &c);
297
298 SHA512_Init(&c);
299 SHA512_Update(&c, key_ref1, SHA512_LENGTH);
300 unsigned char key_ref2[SHA512_LENGTH];
301 SHA512_Final(key_ref2, &c);
302
303 return std::string((char*)key_ref2, EXT4_KEY_DESCRIPTOR_SIZE);
304}
305
Paul Lawrence731a7a22015-04-28 22:14:15 +0000306int e4crypt_check_passwd(const char* path, const char* password)
307{
308 SLOGI("e4crypt_check_password");
Paul Lawrencea56d3132015-05-04 15:48:24 -0700309 auto props = GetPropsOrAltProps(path);
310 auto key_props = props.GetChild(properties::key);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000311
312 crypt_mnt_ftr ftr;
313 if (get_crypt_ftr_and_key(ftr, key_props)) {
314 SLOGE("Failed to read crypto footer back");
315 return -1;
316 }
317
Paul Crowley95376d62015-05-06 15:04:43 +0100318 unsigned char master_key_bytes[key_length / 8];
319 if (cryptfs_get_master_key (&ftr, password, master_key_bytes)){
Paul Lawrence731a7a22015-04-28 22:14:15 +0000320 SLOGI("Incorrect password");
Paul Lawrencec78c71b2015-04-14 15:26:29 -0700321 ftr.failed_decrypt_count++;
322 if (put_crypt_ftr_and_key(ftr, key_props)) {
323 SLOGW("Failed to update failed_decrypt_count");
324 }
325 return ftr.failed_decrypt_count;
326 }
327
328 if (ftr.failed_decrypt_count) {
329 ftr.failed_decrypt_count = 0;
330 if (put_crypt_ftr_and_key(ftr, key_props)) {
331 SLOGW("Failed to reset failed_decrypt_count");
332 }
Paul Lawrence731a7a22015-04-28 22:14:15 +0000333 }
Paul Crowley95376d62015-05-06 15:04:43 +0100334 std::string master_key(reinterpret_cast<char*>(master_key_bytes),
335 sizeof(master_key_bytes));
Paul Lawrence731a7a22015-04-28 22:14:15 +0000336
Paul Lawrence86c942a2015-05-06 13:53:43 -0700337 struct timespec now;
338 clock_gettime(CLOCK_BOOTTIME, &now);
Paul Crowley95376d62015-05-06 15:04:43 +0100339 s_key_store[path] = keys{master_key, password,
Paul Lawrence86c942a2015-05-06 13:53:43 -0700340 now.tv_sec + password_max_age_seconds};
Paul Crowleyf25a35a2015-05-06 13:38:53 +0100341 auto raw_ref = e4crypt_install_key(master_key);
342 if (raw_ref.empty()) {
343 return -1;
344 }
Paul Lawrence731a7a22015-04-28 22:14:15 +0000345
Paul Crowleyf25a35a2015-05-06 13:38:53 +0100346 // Save reference to key so we can set policy later
347 if (!props.Set(properties::ref, raw_ref)) {
348 SLOGE("Cannot save key reference");
349 return -1;
350 }
351
352 return 0;
353}
354
Paul Crowley93363482015-07-07 15:17:22 +0100355static ext4_encryption_key fill_key(const std::string &key)
Paul Crowleyf25a35a2015-05-06 13:38:53 +0100356{
Paul Lawrencefd7db732015-04-10 07:48:51 -0700357 // ext4enc:TODO Currently raw key is required to be of length
358 // sizeof(ext4_key.raw) == EXT4_MAX_KEY_SIZE, so zero pad to
359 // this length. Change when kernel bug is fixed.
360 ext4_encryption_key ext4_key = {EXT4_ENCRYPTION_MODE_AES_256_XTS,
361 {0},
362 sizeof(ext4_key.raw)};
363 memset(ext4_key.raw, 0, sizeof(ext4_key.raw));
364 static_assert(key_length / 8 <= sizeof(ext4_key.raw),
365 "Key too long!");
Paul Crowley95376d62015-05-06 15:04:43 +0100366 memcpy(ext4_key.raw, &key[0], key.size());
Paul Crowley93363482015-07-07 15:17:22 +0100367 return ext4_key;
368}
Paul Lawrence731a7a22015-04-28 22:14:15 +0000369
Paul Crowley93363482015-07-07 15:17:22 +0100370static std::string keyname(const std::string &raw_ref)
371{
Paul Lawrencefd7db732015-04-10 07:48:51 -0700372 std::ostringstream o;
Paul Crowley93363482015-07-07 15:17:22 +0100373 o << "ext4:";
Paul Lawrencefd7db732015-04-10 07:48:51 -0700374 for (auto i = raw_ref.begin(); i != raw_ref.end(); ++i) {
375 o << std::hex << std::setw(2) << std::setfill('0') << (int)*i;
376 }
Paul Crowley93363482015-07-07 15:17:22 +0100377 return o.str();
378}
Paul Lawrencefd7db732015-04-10 07:48:51 -0700379
Paul Crowley93363482015-07-07 15:17:22 +0100380// Get the keyring we store all keys in
381static key_serial_t e4crypt_keyring()
382{
383 return keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "e4crypt", 0);
384}
Paul Lawrence731a7a22015-04-28 22:14:15 +0000385
Paul Crowley93363482015-07-07 15:17:22 +0100386static int e4crypt_install_key(const ext4_encryption_key &ext4_key, const std::string &ref)
387{
388 key_serial_t device_keyring = e4crypt_keyring();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000389 SLOGI("Found device_keyring - id is %d", device_keyring);
Paul Lawrencefd7db732015-04-10 07:48:51 -0700390 key_serial_t key_id = add_key("logon", ref.c_str(),
Paul Lawrence731a7a22015-04-28 22:14:15 +0000391 (void*)&ext4_key, sizeof(ext4_key),
392 device_keyring);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000393 if (key_id == -1) {
394 SLOGE("Failed to insert key into keyring with error %s",
395 strerror(errno));
Paul Crowley93363482015-07-07 15:17:22 +0100396 return -1;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000397 }
Paul Lawrencefd7db732015-04-10 07:48:51 -0700398 SLOGI("Added key %d (%s) to keyring %d in process %d",
399 key_id, ref.c_str(), device_keyring, getpid());
Paul Crowley93363482015-07-07 15:17:22 +0100400 return 0;
401}
Paul Lawrence731a7a22015-04-28 22:14:15 +0000402
Paul Crowley93363482015-07-07 15:17:22 +0100403// Install password into global keyring
404// Return raw key reference for use in policy
405static std::string e4crypt_install_key(const std::string &key)
406{
407 auto ext4_key = fill_key(key);
408 auto raw_ref = generate_key_ref(ext4_key.raw, ext4_key.size);
409 auto ref = keyname(raw_ref);
410 if (e4crypt_install_key(ext4_key, ref) == -1) {
411 return "";
412 }
Paul Crowleyf25a35a2015-05-06 13:38:53 +0100413 return raw_ref;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000414}
415
416int e4crypt_restart(const char* path)
417{
418 SLOGI("e4crypt_restart");
419
420 int rc = 0;
421
422 SLOGI("ext4 restart called on %s", path);
423 property_set("vold.decrypt", "trigger_reset_main");
424 SLOGI("Just asked init to shut down class main");
425 sleep(2);
426
427 std::string tmp_path = std::string() + path + "/tmp_mnt";
428
Paul Lawrence2f32cda2015-05-05 14:28:25 -0700429 rc = wait_and_unmount(tmp_path.c_str(), true);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000430 if (rc) {
431 SLOGE("umount %s failed with rc %d, msg %s",
432 tmp_path.c_str(), rc, strerror(errno));
433 return rc;
434 }
435
Paul Lawrence2f32cda2015-05-05 14:28:25 -0700436 rc = wait_and_unmount(path, true);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000437 if (rc) {
438 SLOGE("umount %s failed with rc %d, msg %s",
439 path, rc, strerror(errno));
440 return rc;
441 }
442
443 return 0;
444}
445
Paul Lawrence731a7a22015-04-28 22:14:15 +0000446int e4crypt_get_password_type(const char* path)
447{
448 SLOGI("e4crypt_get_password_type");
449 return GetPropsOrAltProps(path).GetChild(properties::key)
450 .Get<int>(tag::crypt_type, CRYPT_TYPE_DEFAULT);
451}
Paul Lawrence368d7942015-04-15 14:12:00 -0700452
Paul Lawrence86c942a2015-05-06 13:53:43 -0700453const char* e4crypt_get_password(const char* path)
454{
455 SLOGI("e4crypt_get_password");
456
457 auto i = s_key_store.find(path);
458 if (i == s_key_store.end()) {
459 return 0;
460 }
461
462 struct timespec now;
463 clock_gettime(CLOCK_BOOTTIME, &now);
464 if (i->second.expiry_time < now.tv_sec) {
465 e4crypt_clear_password(path);
466 return 0;
467 }
468
469 return i->second.password.c_str();
470}
471
472void e4crypt_clear_password(const char* path)
473{
474 SLOGI("e4crypt_clear_password");
475
476 auto i = s_key_store.find(path);
477 if (i == s_key_store.end()) {
478 return;
479 }
480
481 memset(&i->second.password[0], 0, i->second.password.size());
482 i->second.password = std::string();
483}
484
Paul Lawrence368d7942015-04-15 14:12:00 -0700485int e4crypt_get_field(const char* path, const char* fieldname,
486 char* value, size_t len)
487{
488 auto v = GetPropsOrAltProps(path).GetChild(properties::props)
489 .Get<std::string>(fieldname);
490
491 if (v == "") {
492 return CRYPTO_GETFIELD_ERROR_NO_FIELD;
493 }
494
495 if (v.length() >= len) {
496 return CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
497 }
498
499 strlcpy(value, v.c_str(), len);
500 return 0;
501}
502
503int e4crypt_set_field(const char* path, const char* fieldname,
504 const char* value)
505{
506 return GetPropsOrAltProps(path).GetChild(properties::props)
507 .Set(fieldname, std::string(value)) ? 0 : -1;
508}
Paul Crowley95376d62015-05-06 15:04:43 +0100509
Paul Crowleyb33e8872015-05-19 12:34:09 +0100510static std::string get_key_path(
Paul Crowley95376d62015-05-06 15:04:43 +0100511 const char *mount_path,
Paul Crowleyb33e8872015-05-19 12:34:09 +0100512 const char *user_handle)
Paul Crowley95376d62015-05-06 15:04:43 +0100513{
514 // ext4enc:TODO get the path properly
515 auto key_dir = android::base::StringPrintf("%s/misc/vold/user_keys",
516 mount_path);
517 if (mkdir(key_dir.c_str(), 0700) < 0 && errno != EEXIST) {
518 SLOGE("Unable to create %s (%s)", key_dir.c_str(), strerror(errno));
519 return "";
520 }
Paul Crowleyb33e8872015-05-19 12:34:09 +0100521 return key_dir + "/" + user_handle;
522}
523
524// ext4enc:TODO this can't be the only place keys are read from /dev/urandom
525// we should unite those places.
Paul Crowley93363482015-07-07 15:17:22 +0100526static std::string e4crypt_get_key(
527 const std::string &key_path,
Paul Crowleyb33e8872015-05-19 12:34:09 +0100528 bool create_if_absent)
529{
Paul Crowley95376d62015-05-06 15:04:43 +0100530 std::string content;
531 if (android::base::ReadFileToString(key_path, &content)) {
532 if (content.size() != key_length/8) {
533 SLOGE("Wrong size key %zu in %s", content.size(), key_path.c_str());
534 return "";
535 }
536 return content;
537 }
538 if (!create_if_absent) {
539 SLOGE("No key found in %s", key_path.c_str());
540 return "";
541 }
542 std::ifstream urandom("/dev/urandom");
543 if (!urandom) {
544 SLOGE("Unable to open /dev/urandom (%s)", strerror(errno));
545 return "";
546 }
547 char key_bytes[key_length / 8];
548 errno = 0;
549 urandom.read(key_bytes, sizeof(key_bytes));
550 if (!urandom) {
551 SLOGE("Unable to read key from /dev/urandom (%s)", strerror(errno));
552 return "";
553 }
Paul Crowley93363482015-07-07 15:17:22 +0100554 std::string key(key_bytes, sizeof(key_bytes));
555 if (!android::base::WriteStringToFile(key, key_path)) {
Paul Crowley95376d62015-05-06 15:04:43 +0100556 SLOGE("Unable to write key to %s (%s)",
557 key_path.c_str(), strerror(errno));
558 return "";
559 }
Paul Crowley93363482015-07-07 15:17:22 +0100560 return key;
Paul Crowley95376d62015-05-06 15:04:43 +0100561}
562
563static int e4crypt_set_user_policy(const char *mount_path, const char *user_handle,
564 const char *path, bool create_if_absent)
565{
566 SLOGD("e4crypt_set_user_policy for %s", user_handle);
Paul Crowley93363482015-07-07 15:17:22 +0100567 auto user_key = e4crypt_get_key(
568 get_key_path(mount_path, user_handle),
Paul Crowley95376d62015-05-06 15:04:43 +0100569 create_if_absent);
570 if (user_key.empty()) {
571 return -1;
572 }
573 auto raw_ref = e4crypt_install_key(user_key);
574 if (raw_ref.empty()) {
575 return -1;
576 }
577 return do_policy_set(path, raw_ref.c_str(), raw_ref.size());
578}
579
580int e4crypt_create_new_user_dir(const char *user_handle, const char *path) {
581 SLOGD("e4crypt_create_new_user_dir(\"%s\", \"%s\")", user_handle, path);
582 if (mkdir(path, S_IRWXU | S_IRWXG | S_IXOTH) < 0) {
583 return -1;
584 }
585 if (chmod(path, S_IRWXU | S_IRWXG | S_IXOTH) < 0) {
586 return -1;
587 }
588 if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
589 // ext4enc:TODO handle errors from this.
590 e4crypt_set_user_policy(DATA_MNT_POINT, user_handle, path, true);
591 }
592 return 0;
593}
594
595static bool is_numeric(const char *name) {
596 for (const char *p = name; *p != '\0'; p++) {
597 if (!isdigit(*p))
598 return false;
599 }
600 return true;
601}
602
603int e4crypt_set_user_crypto_policies(const char *dir)
604{
605 if (e4crypt_crypto_complete(DATA_MNT_POINT) != 0) {
606 return 0;
607 }
608 SLOGD("e4crypt_set_user_crypto_policies");
609 std::unique_ptr<DIR, int(*)(DIR*)> dirp(opendir(dir), closedir);
610 if (!dirp) {
611 SLOGE("Unable to read directory %s, error %s\n",
612 dir, strerror(errno));
613 return -1;
614 }
615 for (;;) {
616 struct dirent *result = readdir(dirp.get());
617 if (!result) {
618 // ext4enc:TODO check errno
619 break;
620 }
621 if (result->d_type != DT_DIR || !is_numeric(result->d_name)) {
622 continue; // skips user 0, which is a symlink
623 }
624 auto user_dir = std::string() + dir + "/" + result->d_name;
625 // ext4enc:TODO don't hardcode /data
626 if (e4crypt_set_user_policy("/data", result->d_name,
627 user_dir.c_str(), false)) {
628 // ext4enc:TODO If this function fails, stop the boot: we must
629 // deliver on promised encryption.
630 SLOGE("Unable to set policy on %s\n", user_dir.c_str());
631 }
632 }
633 return 0;
634}
Paul Crowleyb33e8872015-05-19 12:34:09 +0100635
636int e4crypt_delete_user_key(const char *user_handle) {
637 SLOGD("e4crypt_delete_user_key(\"%s\")", user_handle);
638 auto key_path = get_key_path(DATA_MNT_POINT, user_handle);
Paul Crowley93363482015-07-07 15:17:22 +0100639 auto key = e4crypt_get_key(key_path, false);
640 auto ext4_key = fill_key(key);
641 auto ref = keyname(generate_key_ref(ext4_key.raw, ext4_key.size));
642 auto key_serial = keyctl_search(e4crypt_keyring(), "logon", ref.c_str(), 0);
643 if (keyctl_revoke(key_serial) == 0) {
644 SLOGD("Revoked key with serial %ld ref %s\n", key_serial, ref.c_str());
645 } else {
646 SLOGE("Failed to revoke key with serial %ld ref %s: %s\n",
647 key_serial, ref.c_str(), strerror(errno));
648 }
Paul Crowleycd307b72015-05-19 17:31:39 +0100649 int pid = fork();
650 if (pid < 0) {
651 SLOGE("Unable to fork: %s", strerror(errno));
Paul Crowleyb33e8872015-05-19 12:34:09 +0100652 return -1;
653 }
Paul Crowleycd307b72015-05-19 17:31:39 +0100654 if (pid == 0) {
655 SLOGD("Forked for secdiscard");
656 execl("/system/bin/secdiscard",
657 "/system/bin/secdiscard",
658 key_path.c_str(),
659 NULL);
660 SLOGE("Unable to launch secdiscard on %s: %s\n", key_path.c_str(),
661 strerror(errno));
662 exit(-1);
663 }
664 // ext4enc:TODO reap the zombie
Paul Crowleyb33e8872015-05-19 12:34:09 +0100665 return 0;
666}