blob: c337dbbe5d6098881b8e65d9724748917dc7e681 [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
Paul Crowley480fcd22015-08-24 14:53:28 +010018#include <private/android_filesystem_config.h>
19
Paul Lawrence731a7a22015-04-28 22:14:15 +000020#include "unencrypted_properties.h"
21#include "key_control.h"
22#include "cryptfs.h"
Paul Crowley95376d62015-05-06 15:04:43 +010023#include "ext4_crypt_init_extensions.h"
Paul Lawrence731a7a22015-04-28 22:14:15 +000024
25#define LOG_TAG "Ext4Crypt"
26#include "cutils/log.h"
27#include <cutils/klog.h>
Paul Crowley95376d62015-05-06 15:04:43 +010028#include <base/file.h>
29#include <base/stringprintf.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000030
31namespace {
32 // Key length in bits
33 const int key_length = 128;
Paul Lawrencefd7db732015-04-10 07:48:51 -070034 static_assert(key_length % 8 == 0,
35 "Key length must be multiple of 8 bits");
Paul Lawrence731a7a22015-04-28 22:14:15 +000036
Paul Lawrence86c942a2015-05-06 13:53:43 -070037 // How long do we store passwords for?
38 const int password_max_age_seconds = 60;
39
Paul Lawrence731a7a22015-04-28 22:14:15 +000040 // How is device encrypted
41 struct keys {
42 std::string master_key;
43 std::string password;
Paul Lawrence86c942a2015-05-06 13:53:43 -070044 time_t expiry_time;
Paul Lawrence731a7a22015-04-28 22:14:15 +000045 };
46 std::map<std::string, keys> s_key_store;
47
Paul Lawrencefd7db732015-04-10 07:48:51 -070048 // ext4enc:TODO get these consts from somewhere good
49 const int SHA512_LENGTH = 64;
50 const int EXT4_KEY_DESCRIPTOR_SIZE = 8;
51
Paul Lawrence731a7a22015-04-28 22:14:15 +000052 // ext4enc:TODO Include structure from somewhere sensible
53 // MUST be in sync with ext4_crypto.c in kernel
Paul Lawrencefd7db732015-04-10 07:48:51 -070054 const int EXT4_MAX_KEY_SIZE = 64;
55 const int EXT4_ENCRYPTION_MODE_AES_256_XTS = 1;
Paul Lawrence731a7a22015-04-28 22:14:15 +000056 struct ext4_encryption_key {
Paul Lawrencefd7db732015-04-10 07:48:51 -070057 uint32_t mode;
58 char raw[EXT4_MAX_KEY_SIZE];
59 uint32_t size;
Paul Lawrence731a7a22015-04-28 22:14:15 +000060 };
61
62 namespace tag {
63 const char* magic = "magic";
64 const char* major_version = "major_version";
65 const char* minor_version = "minor_version";
66 const char* flags = "flags";
67 const char* crypt_type = "crypt_type";
68 const char* failed_decrypt_count = "failed_decrypt_count";
69 const char* crypto_type_name = "crypto_type_name";
70 const char* master_key = "master_key";
71 const char* salt = "salt";
72 const char* kdf_type = "kdf_type";
73 const char* N_factor = "N_factor";
74 const char* r_factor = "r_factor";
75 const char* p_factor = "p_factor";
76 const char* keymaster_blob = "keymaster_blob";
77 const char* scrypted_intermediate_key = "scrypted_intermediate_key";
78 }
79}
80
Paul Crowley95376d62015-05-06 15:04:43 +010081static std::string e4crypt_install_key(const std::string &key);
Paul Crowleyf25a35a2015-05-06 13:38:53 +010082
Paul Lawrence731a7a22015-04-28 22:14:15 +000083static int put_crypt_ftr_and_key(const crypt_mnt_ftr& crypt_ftr,
84 UnencryptedProperties& props)
85{
86 SLOGI("Putting crypt footer");
87
88 bool success = props.Set<int>(tag::magic, crypt_ftr.magic)
89 && props.Set<int>(tag::major_version, crypt_ftr.major_version)
90 && props.Set<int>(tag::minor_version, crypt_ftr.minor_version)
91 && props.Set<int>(tag::flags, crypt_ftr.flags)
92 && props.Set<int>(tag::crypt_type, crypt_ftr.crypt_type)
93 && props.Set<int>(tag::failed_decrypt_count,
94 crypt_ftr.failed_decrypt_count)
95 && props.Set<std::string>(tag::crypto_type_name,
96 std::string(reinterpret_cast<const char*>(crypt_ftr.crypto_type_name)))
97 && props.Set<std::string>(tag::master_key,
98 std::string((const char*) crypt_ftr.master_key,
99 crypt_ftr.keysize))
100 && props.Set<std::string>(tag::salt,
101 std::string((const char*) crypt_ftr.salt,
102 SALT_LEN))
103 && props.Set<int>(tag::kdf_type, crypt_ftr.kdf_type)
104 && props.Set<int>(tag::N_factor, crypt_ftr.N_factor)
105 && props.Set<int>(tag::r_factor, crypt_ftr.r_factor)
106 && props.Set<int>(tag::p_factor, crypt_ftr.p_factor)
107 && props.Set<std::string>(tag::keymaster_blob,
108 std::string((const char*) crypt_ftr.keymaster_blob,
109 crypt_ftr.keymaster_blob_size))
110 && props.Set<std::string>(tag::scrypted_intermediate_key,
111 std::string((const char*) crypt_ftr.scrypted_intermediate_key,
112 SCRYPT_LEN));
113 return success ? 0 : -1;
114}
115
116static int get_crypt_ftr_and_key(crypt_mnt_ftr& crypt_ftr,
117 const UnencryptedProperties& props)
118{
119 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
120 crypt_ftr.magic = props.Get<int>(tag::magic);
121 crypt_ftr.major_version = props.Get<int>(tag::major_version);
122 crypt_ftr.minor_version = props.Get<int>(tag::minor_version);
Paul Lawrence0d9cd9e2015-05-05 15:58:27 -0700123 crypt_ftr.ftr_size = sizeof(crypt_ftr);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000124 crypt_ftr.flags = props.Get<int>(tag::flags);
125 crypt_ftr.crypt_type = props.Get<int>(tag::crypt_type);
126 crypt_ftr.failed_decrypt_count = props.Get<int>(tag::failed_decrypt_count);
127 std::string crypto_type_name = props.Get<std::string>(tag::crypto_type_name);
128 strlcpy(reinterpret_cast<char*>(crypt_ftr.crypto_type_name),
129 crypto_type_name.c_str(),
130 sizeof(crypt_ftr.crypto_type_name));
131 std::string master_key = props.Get<std::string>(tag::master_key);
132 crypt_ftr.keysize = master_key.size();
133 if (crypt_ftr.keysize > sizeof(crypt_ftr.master_key)) {
134 SLOGE("Master key size too long");
135 return -1;
136 }
137 memcpy(crypt_ftr.master_key, &master_key[0], crypt_ftr.keysize);
138 std::string salt = props.Get<std::string>(tag::salt);
139 if (salt.size() != SALT_LEN) {
140 SLOGE("Salt wrong length");
141 return -1;
142 }
143 memcpy(crypt_ftr.salt, &salt[0], SALT_LEN);
144 crypt_ftr.kdf_type = props.Get<int>(tag::kdf_type);
145 crypt_ftr.N_factor = props.Get<int>(tag::N_factor);
146 crypt_ftr.r_factor = props.Get<int>(tag::r_factor);
147 crypt_ftr.p_factor = props.Get<int>(tag::p_factor);
148 std::string keymaster_blob = props.Get<std::string>(tag::keymaster_blob);
149 crypt_ftr.keymaster_blob_size = keymaster_blob.size();
150 if (crypt_ftr.keymaster_blob_size > sizeof(crypt_ftr.keymaster_blob)) {
151 SLOGE("Keymaster blob too long");
152 return -1;
153 }
154 memcpy(crypt_ftr.keymaster_blob, &keymaster_blob[0],
155 crypt_ftr.keymaster_blob_size);
156 std::string scrypted_intermediate_key = props.Get<std::string>(tag::scrypted_intermediate_key);
157 if (scrypted_intermediate_key.size() != SCRYPT_LEN) {
158 SLOGE("scrypted intermediate key wrong length");
159 return -1;
160 }
161 memcpy(crypt_ftr.scrypted_intermediate_key, &scrypted_intermediate_key[0],
162 SCRYPT_LEN);
163
164 return 0;
165}
166
167static UnencryptedProperties GetProps(const char* path)
168{
169 return UnencryptedProperties(path);
170}
171
172static UnencryptedProperties GetAltProps(const char* path)
173{
174 return UnencryptedProperties((std::string() + path + "/tmp_mnt").c_str());
175}
176
177static UnencryptedProperties GetPropsOrAltProps(const char* path)
178{
179 UnencryptedProperties props = GetProps(path);
180 if (props.OK()) {
181 return props;
182 }
183 return GetAltProps(path);
184}
185
186int e4crypt_enable(const char* path)
187{
188 // Already enabled?
189 if (s_key_store.find(path) != s_key_store.end()) {
190 return 0;
191 }
192
193 // Not an encryptable device?
194 UnencryptedProperties key_props = GetProps(path).GetChild(properties::key);
195 if (!key_props.OK()) {
196 return 0;
197 }
198
199 if (key_props.Get<std::string>(tag::master_key).empty()) {
200 crypt_mnt_ftr ftr;
201 if (cryptfs_create_default_ftr(&ftr, key_length)) {
202 SLOGE("Failed to create crypto footer");
203 return -1;
204 }
205
Paul Lawrence0d9cd9e2015-05-05 15:58:27 -0700206 // Scrub fields not used by ext4enc
207 ftr.persist_data_offset[0] = 0;
208 ftr.persist_data_offset[1] = 0;
209 ftr.persist_data_size = 0;
210
Paul Lawrence731a7a22015-04-28 22:14:15 +0000211 if (put_crypt_ftr_and_key(ftr, key_props)) {
212 SLOGE("Failed to write crypto footer");
213 return -1;
214 }
215
216 crypt_mnt_ftr ftr2;
217 if (get_crypt_ftr_and_key(ftr2, key_props)) {
218 SLOGE("Failed to read crypto footer back");
219 return -1;
220 }
221
222 if (memcmp(&ftr, &ftr2, sizeof(ftr)) != 0) {
223 SLOGE("Crypto footer not correctly written");
Paul Lawrence0d9cd9e2015-05-05 15:58:27 -0700224 return -1;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000225 }
226 }
227
228 if (!UnencryptedProperties(path).Remove(properties::ref)) {
229 SLOGE("Failed to remove key ref");
230 return -1;
231 }
232
233 return e4crypt_check_passwd(path, "");
234}
235
236int e4crypt_change_password(const char* path, int crypt_type,
237 const char* password)
238{
239 SLOGI("e4crypt_change_password");
Paul Lawrencea56d3132015-05-04 15:48:24 -0700240 auto key_props = GetProps(path).GetChild(properties::key);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000241
242 crypt_mnt_ftr ftr;
243 if (get_crypt_ftr_and_key(ftr, key_props)) {
244 SLOGE("Failed to read crypto footer back");
245 return -1;
246 }
247
248 auto mki = s_key_store.find(path);
249 if (mki == s_key_store.end()) {
250 SLOGE("No stored master key - can't change password");
251 return -1;
252 }
253
Paul Crowley95376d62015-05-06 15:04:43 +0100254 const unsigned char* master_key_bytes
Paul Lawrence731a7a22015-04-28 22:14:15 +0000255 = reinterpret_cast<const unsigned char*>(&mki->second.master_key[0]);
256
Paul Crowley95376d62015-05-06 15:04:43 +0100257 if (cryptfs_set_password(&ftr, password, master_key_bytes)) {
Paul Lawrence731a7a22015-04-28 22:14:15 +0000258 SLOGE("Failed to set password");
259 return -1;
260 }
261
262 ftr.crypt_type = crypt_type;
263
264 if (put_crypt_ftr_and_key(ftr, key_props)) {
265 SLOGE("Failed to write crypto footer");
266 return -1;
267 }
268
269 if (!UnencryptedProperties(path).Set(properties::is_default,
270 crypt_type == CRYPT_TYPE_DEFAULT)) {
271 SLOGE("Failed to update default flag");
272 return -1;
273 }
274
275 return 0;
276}
277
278int e4crypt_crypto_complete(const char* path)
279{
280 SLOGI("ext4 crypto complete called on %s", path);
Paul Lawrencea56d3132015-05-04 15:48:24 -0700281 auto key_props = GetPropsOrAltProps(path).GetChild(properties::key);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000282 if (key_props.Get<std::string>(tag::master_key).empty()) {
283 SLOGI("No master key, so not ext4enc");
284 return -1;
285 }
286
287 return 0;
288}
289
Paul Crowley93363482015-07-07 15:17:22 +0100290// Get raw keyref - used to make keyname and to pass to ioctl
Paul Lawrencefd7db732015-04-10 07:48:51 -0700291static std::string generate_key_ref(const char* key, int length)
292{
293 SHA512_CTX c;
294
295 SHA512_Init(&c);
296 SHA512_Update(&c, key, length);
297 unsigned char key_ref1[SHA512_LENGTH];
298 SHA512_Final(key_ref1, &c);
299
300 SHA512_Init(&c);
301 SHA512_Update(&c, key_ref1, SHA512_LENGTH);
302 unsigned char key_ref2[SHA512_LENGTH];
303 SHA512_Final(key_ref2, &c);
304
305 return std::string((char*)key_ref2, EXT4_KEY_DESCRIPTOR_SIZE);
306}
307
Paul Lawrence731a7a22015-04-28 22:14:15 +0000308int e4crypt_check_passwd(const char* path, const char* password)
309{
310 SLOGI("e4crypt_check_password");
Paul Lawrencea56d3132015-05-04 15:48:24 -0700311 auto props = GetPropsOrAltProps(path);
312 auto key_props = props.GetChild(properties::key);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000313
314 crypt_mnt_ftr ftr;
315 if (get_crypt_ftr_and_key(ftr, key_props)) {
316 SLOGE("Failed to read crypto footer back");
317 return -1;
318 }
319
Paul Crowley95376d62015-05-06 15:04:43 +0100320 unsigned char master_key_bytes[key_length / 8];
321 if (cryptfs_get_master_key (&ftr, password, master_key_bytes)){
Paul Lawrence731a7a22015-04-28 22:14:15 +0000322 SLOGI("Incorrect password");
Paul Lawrencec78c71b2015-04-14 15:26:29 -0700323 ftr.failed_decrypt_count++;
324 if (put_crypt_ftr_and_key(ftr, key_props)) {
325 SLOGW("Failed to update failed_decrypt_count");
326 }
327 return ftr.failed_decrypt_count;
328 }
329
330 if (ftr.failed_decrypt_count) {
331 ftr.failed_decrypt_count = 0;
332 if (put_crypt_ftr_and_key(ftr, key_props)) {
333 SLOGW("Failed to reset failed_decrypt_count");
334 }
Paul Lawrence731a7a22015-04-28 22:14:15 +0000335 }
Paul Crowley95376d62015-05-06 15:04:43 +0100336 std::string master_key(reinterpret_cast<char*>(master_key_bytes),
337 sizeof(master_key_bytes));
Paul Lawrence731a7a22015-04-28 22:14:15 +0000338
Paul Lawrence86c942a2015-05-06 13:53:43 -0700339 struct timespec now;
340 clock_gettime(CLOCK_BOOTTIME, &now);
Paul Crowley95376d62015-05-06 15:04:43 +0100341 s_key_store[path] = keys{master_key, password,
Paul Lawrence86c942a2015-05-06 13:53:43 -0700342 now.tv_sec + password_max_age_seconds};
Paul Crowleyf25a35a2015-05-06 13:38:53 +0100343 auto raw_ref = e4crypt_install_key(master_key);
344 if (raw_ref.empty()) {
345 return -1;
346 }
Paul Lawrence731a7a22015-04-28 22:14:15 +0000347
Paul Crowleyf25a35a2015-05-06 13:38:53 +0100348 // Save reference to key so we can set policy later
349 if (!props.Set(properties::ref, raw_ref)) {
350 SLOGE("Cannot save key reference");
351 return -1;
352 }
353
354 return 0;
355}
356
Paul Crowley93363482015-07-07 15:17:22 +0100357static ext4_encryption_key fill_key(const std::string &key)
Paul Crowleyf25a35a2015-05-06 13:38:53 +0100358{
Paul Lawrencefd7db732015-04-10 07:48:51 -0700359 // ext4enc:TODO Currently raw key is required to be of length
360 // sizeof(ext4_key.raw) == EXT4_MAX_KEY_SIZE, so zero pad to
361 // this length. Change when kernel bug is fixed.
362 ext4_encryption_key ext4_key = {EXT4_ENCRYPTION_MODE_AES_256_XTS,
363 {0},
364 sizeof(ext4_key.raw)};
365 memset(ext4_key.raw, 0, sizeof(ext4_key.raw));
366 static_assert(key_length / 8 <= sizeof(ext4_key.raw),
367 "Key too long!");
Paul Crowley95376d62015-05-06 15:04:43 +0100368 memcpy(ext4_key.raw, &key[0], key.size());
Paul Crowley93363482015-07-07 15:17:22 +0100369 return ext4_key;
370}
Paul Lawrence731a7a22015-04-28 22:14:15 +0000371
Paul Crowley93363482015-07-07 15:17:22 +0100372static std::string keyname(const std::string &raw_ref)
373{
Paul Lawrencefd7db732015-04-10 07:48:51 -0700374 std::ostringstream o;
Paul Crowley93363482015-07-07 15:17:22 +0100375 o << "ext4:";
Paul Lawrencefd7db732015-04-10 07:48:51 -0700376 for (auto i = raw_ref.begin(); i != raw_ref.end(); ++i) {
377 o << std::hex << std::setw(2) << std::setfill('0') << (int)*i;
378 }
Paul Crowley93363482015-07-07 15:17:22 +0100379 return o.str();
380}
Paul Lawrencefd7db732015-04-10 07:48:51 -0700381
Paul Crowley93363482015-07-07 15:17:22 +0100382// Get the keyring we store all keys in
383static key_serial_t e4crypt_keyring()
384{
385 return keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "e4crypt", 0);
386}
Paul Lawrence731a7a22015-04-28 22:14:15 +0000387
Paul Crowley93363482015-07-07 15:17:22 +0100388static int e4crypt_install_key(const ext4_encryption_key &ext4_key, const std::string &ref)
389{
390 key_serial_t device_keyring = e4crypt_keyring();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000391 SLOGI("Found device_keyring - id is %d", device_keyring);
Paul Lawrencefd7db732015-04-10 07:48:51 -0700392 key_serial_t key_id = add_key("logon", ref.c_str(),
Paul Lawrence731a7a22015-04-28 22:14:15 +0000393 (void*)&ext4_key, sizeof(ext4_key),
394 device_keyring);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000395 if (key_id == -1) {
396 SLOGE("Failed to insert key into keyring with error %s",
397 strerror(errno));
Paul Crowley93363482015-07-07 15:17:22 +0100398 return -1;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000399 }
Paul Lawrencefd7db732015-04-10 07:48:51 -0700400 SLOGI("Added key %d (%s) to keyring %d in process %d",
401 key_id, ref.c_str(), device_keyring, getpid());
Paul Crowley93363482015-07-07 15:17:22 +0100402 return 0;
403}
Paul Lawrence731a7a22015-04-28 22:14:15 +0000404
Paul Crowley93363482015-07-07 15:17:22 +0100405// Install password into global keyring
406// Return raw key reference for use in policy
407static std::string e4crypt_install_key(const std::string &key)
408{
409 auto ext4_key = fill_key(key);
410 auto raw_ref = generate_key_ref(ext4_key.raw, ext4_key.size);
411 auto ref = keyname(raw_ref);
412 if (e4crypt_install_key(ext4_key, ref) == -1) {
413 return "";
414 }
Paul Crowleyf25a35a2015-05-06 13:38:53 +0100415 return raw_ref;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000416}
417
418int e4crypt_restart(const char* path)
419{
420 SLOGI("e4crypt_restart");
421
422 int rc = 0;
423
424 SLOGI("ext4 restart called on %s", path);
425 property_set("vold.decrypt", "trigger_reset_main");
426 SLOGI("Just asked init to shut down class main");
427 sleep(2);
428
429 std::string tmp_path = std::string() + path + "/tmp_mnt";
430
Paul Lawrence2f32cda2015-05-05 14:28:25 -0700431 rc = wait_and_unmount(tmp_path.c_str(), true);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000432 if (rc) {
433 SLOGE("umount %s failed with rc %d, msg %s",
434 tmp_path.c_str(), rc, strerror(errno));
435 return rc;
436 }
437
Paul Lawrence2f32cda2015-05-05 14:28:25 -0700438 rc = wait_and_unmount(path, true);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000439 if (rc) {
440 SLOGE("umount %s failed with rc %d, msg %s",
441 path, rc, strerror(errno));
442 return rc;
443 }
444
445 return 0;
446}
447
Paul Lawrence731a7a22015-04-28 22:14:15 +0000448int e4crypt_get_password_type(const char* path)
449{
450 SLOGI("e4crypt_get_password_type");
451 return GetPropsOrAltProps(path).GetChild(properties::key)
452 .Get<int>(tag::crypt_type, CRYPT_TYPE_DEFAULT);
453}
Paul Lawrence368d7942015-04-15 14:12:00 -0700454
Paul Lawrence86c942a2015-05-06 13:53:43 -0700455const char* e4crypt_get_password(const char* path)
456{
457 SLOGI("e4crypt_get_password");
458
459 auto i = s_key_store.find(path);
460 if (i == s_key_store.end()) {
461 return 0;
462 }
463
464 struct timespec now;
465 clock_gettime(CLOCK_BOOTTIME, &now);
466 if (i->second.expiry_time < now.tv_sec) {
467 e4crypt_clear_password(path);
468 return 0;
469 }
470
471 return i->second.password.c_str();
472}
473
474void e4crypt_clear_password(const char* path)
475{
476 SLOGI("e4crypt_clear_password");
477
478 auto i = s_key_store.find(path);
479 if (i == s_key_store.end()) {
480 return;
481 }
482
483 memset(&i->second.password[0], 0, i->second.password.size());
484 i->second.password = std::string();
485}
486
Paul Lawrence368d7942015-04-15 14:12:00 -0700487int e4crypt_get_field(const char* path, const char* fieldname,
488 char* value, size_t len)
489{
490 auto v = GetPropsOrAltProps(path).GetChild(properties::props)
491 .Get<std::string>(fieldname);
492
493 if (v == "") {
494 return CRYPTO_GETFIELD_ERROR_NO_FIELD;
495 }
496
497 if (v.length() >= len) {
498 return CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
499 }
500
501 strlcpy(value, v.c_str(), len);
502 return 0;
503}
504
505int e4crypt_set_field(const char* path, const char* fieldname,
506 const char* value)
507{
508 return GetPropsOrAltProps(path).GetChild(properties::props)
509 .Set(fieldname, std::string(value)) ? 0 : -1;
510}
Paul Crowley95376d62015-05-06 15:04:43 +0100511
Paul Crowleyb33e8872015-05-19 12:34:09 +0100512static std::string get_key_path(
Paul Crowley95376d62015-05-06 15:04:43 +0100513 const char *mount_path,
Paul Crowleyb33e8872015-05-19 12:34:09 +0100514 const char *user_handle)
Paul Crowley95376d62015-05-06 15:04:43 +0100515{
516 // ext4enc:TODO get the path properly
517 auto key_dir = android::base::StringPrintf("%s/misc/vold/user_keys",
518 mount_path);
519 if (mkdir(key_dir.c_str(), 0700) < 0 && errno != EEXIST) {
520 SLOGE("Unable to create %s (%s)", key_dir.c_str(), strerror(errno));
521 return "";
522 }
Paul Crowleyb33e8872015-05-19 12:34:09 +0100523 return key_dir + "/" + user_handle;
524}
525
526// ext4enc:TODO this can't be the only place keys are read from /dev/urandom
527// we should unite those places.
Paul Crowley93363482015-07-07 15:17:22 +0100528static std::string e4crypt_get_key(
529 const std::string &key_path,
Paul Crowleyb33e8872015-05-19 12:34:09 +0100530 bool create_if_absent)
531{
Paul Crowley95376d62015-05-06 15:04:43 +0100532 std::string content;
533 if (android::base::ReadFileToString(key_path, &content)) {
534 if (content.size() != key_length/8) {
535 SLOGE("Wrong size key %zu in %s", content.size(), key_path.c_str());
536 return "";
537 }
538 return content;
539 }
540 if (!create_if_absent) {
541 SLOGE("No key found in %s", key_path.c_str());
542 return "";
543 }
544 std::ifstream urandom("/dev/urandom");
545 if (!urandom) {
546 SLOGE("Unable to open /dev/urandom (%s)", strerror(errno));
547 return "";
548 }
549 char key_bytes[key_length / 8];
550 errno = 0;
551 urandom.read(key_bytes, sizeof(key_bytes));
552 if (!urandom) {
553 SLOGE("Unable to read key from /dev/urandom (%s)", strerror(errno));
554 return "";
555 }
Paul Crowley93363482015-07-07 15:17:22 +0100556 std::string key(key_bytes, sizeof(key_bytes));
557 if (!android::base::WriteStringToFile(key, key_path)) {
Paul Crowley95376d62015-05-06 15:04:43 +0100558 SLOGE("Unable to write key to %s (%s)",
559 key_path.c_str(), strerror(errno));
560 return "";
561 }
Paul Crowley93363482015-07-07 15:17:22 +0100562 return key;
Paul Crowley95376d62015-05-06 15:04:43 +0100563}
564
565static int e4crypt_set_user_policy(const char *mount_path, const char *user_handle,
566 const char *path, bool create_if_absent)
567{
568 SLOGD("e4crypt_set_user_policy for %s", user_handle);
Paul Crowley93363482015-07-07 15:17:22 +0100569 auto user_key = e4crypt_get_key(
570 get_key_path(mount_path, user_handle),
Paul Crowley95376d62015-05-06 15:04:43 +0100571 create_if_absent);
572 if (user_key.empty()) {
573 return -1;
574 }
575 auto raw_ref = e4crypt_install_key(user_key);
576 if (raw_ref.empty()) {
577 return -1;
578 }
579 return do_policy_set(path, raw_ref.c_str(), raw_ref.size());
580}
581
582int e4crypt_create_new_user_dir(const char *user_handle, const char *path) {
583 SLOGD("e4crypt_create_new_user_dir(\"%s\", \"%s\")", user_handle, path);
584 if (mkdir(path, S_IRWXU | S_IRWXG | S_IXOTH) < 0) {
585 return -1;
586 }
587 if (chmod(path, S_IRWXU | S_IRWXG | S_IXOTH) < 0) {
588 return -1;
589 }
Paul Crowley480fcd22015-08-24 14:53:28 +0100590 if (chown(path, AID_SYSTEM, AID_SYSTEM) < 0) {
591 return -1;
592 }
Paul Crowley95376d62015-05-06 15:04:43 +0100593 if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
594 // ext4enc:TODO handle errors from this.
595 e4crypt_set_user_policy(DATA_MNT_POINT, user_handle, path, true);
596 }
597 return 0;
598}
599
600static bool is_numeric(const char *name) {
601 for (const char *p = name; *p != '\0'; p++) {
602 if (!isdigit(*p))
603 return false;
604 }
605 return true;
606}
607
608int e4crypt_set_user_crypto_policies(const char *dir)
609{
610 if (e4crypt_crypto_complete(DATA_MNT_POINT) != 0) {
611 return 0;
612 }
613 SLOGD("e4crypt_set_user_crypto_policies");
614 std::unique_ptr<DIR, int(*)(DIR*)> dirp(opendir(dir), closedir);
615 if (!dirp) {
616 SLOGE("Unable to read directory %s, error %s\n",
617 dir, strerror(errno));
618 return -1;
619 }
620 for (;;) {
621 struct dirent *result = readdir(dirp.get());
622 if (!result) {
623 // ext4enc:TODO check errno
624 break;
625 }
626 if (result->d_type != DT_DIR || !is_numeric(result->d_name)) {
627 continue; // skips user 0, which is a symlink
628 }
629 auto user_dir = std::string() + dir + "/" + result->d_name;
630 // ext4enc:TODO don't hardcode /data
631 if (e4crypt_set_user_policy("/data", result->d_name,
632 user_dir.c_str(), false)) {
633 // ext4enc:TODO If this function fails, stop the boot: we must
634 // deliver on promised encryption.
635 SLOGE("Unable to set policy on %s\n", user_dir.c_str());
636 }
637 }
638 return 0;
639}
Paul Crowleyb33e8872015-05-19 12:34:09 +0100640
641int e4crypt_delete_user_key(const char *user_handle) {
642 SLOGD("e4crypt_delete_user_key(\"%s\")", user_handle);
643 auto key_path = get_key_path(DATA_MNT_POINT, user_handle);
Paul Crowley93363482015-07-07 15:17:22 +0100644 auto key = e4crypt_get_key(key_path, false);
645 auto ext4_key = fill_key(key);
646 auto ref = keyname(generate_key_ref(ext4_key.raw, ext4_key.size));
647 auto key_serial = keyctl_search(e4crypt_keyring(), "logon", ref.c_str(), 0);
648 if (keyctl_revoke(key_serial) == 0) {
649 SLOGD("Revoked key with serial %ld ref %s\n", key_serial, ref.c_str());
650 } else {
651 SLOGE("Failed to revoke key with serial %ld ref %s: %s\n",
652 key_serial, ref.c_str(), strerror(errno));
653 }
Paul Crowleycd307b72015-05-19 17:31:39 +0100654 int pid = fork();
655 if (pid < 0) {
656 SLOGE("Unable to fork: %s", strerror(errno));
Paul Crowleyb33e8872015-05-19 12:34:09 +0100657 return -1;
658 }
Paul Crowleycd307b72015-05-19 17:31:39 +0100659 if (pid == 0) {
660 SLOGD("Forked for secdiscard");
661 execl("/system/bin/secdiscard",
662 "/system/bin/secdiscard",
663 key_path.c_str(),
664 NULL);
665 SLOGE("Unable to launch secdiscard on %s: %s\n", key_path.c_str(),
666 strerror(errno));
667 exit(-1);
668 }
669 // ext4enc:TODO reap the zombie
Paul Crowleyb33e8872015-05-19 12:34:09 +0100670 return 0;
671}