blob: 72c79fa5d48a04515cdfdf08ea4730c6d1ac677d [file] [log] [blame]
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -08001/*
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
Paul Lawrence731a7a22015-04-28 22:14:15 +000017#include "Ext4Crypt.h"
18
Paul Crowley1ef25582016-01-21 20:26:12 +000019#include "KeyStorage.h"
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080020#include "Utils.h"
21
Paul Lawrencefd7db732015-04-10 07:48:51 -070022#include <iomanip>
Paul Lawrence731a7a22015-04-28 22:14:15 +000023#include <map>
Paul Crowleyb1f3d242016-01-28 10:09:46 +000024#include <set>
Paul Lawrencefd7db732015-04-10 07:48:51 -070025#include <string>
26#include <sstream>
Paul Lawrence731a7a22015-04-28 22:14:15 +000027
Paul Crowleyb1f3d242016-01-28 10:09:46 +000028#include <stdio.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000029#include <errno.h>
Paul Crowley95376d62015-05-06 15:04:43 +010030#include <dirent.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000031#include <sys/mount.h>
Paul Crowley95376d62015-05-06 15:04:43 +010032#include <sys/types.h>
33#include <sys/stat.h>
34#include <fcntl.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000035#include <cutils/properties.h>
Paul Lawrencefd7db732015-04-10 07:48:51 -070036#include <openssl/sha.h>
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070037#include <selinux/android.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000038
Paul Crowley480fcd22015-08-24 14:53:28 +010039#include <private/android_filesystem_config.h>
40
Paul Lawrence731a7a22015-04-28 22:14:15 +000041#include "unencrypted_properties.h"
42#include "key_control.h"
43#include "cryptfs.h"
Paul Crowley95376d62015-05-06 15:04:43 +010044#include "ext4_crypt_init_extensions.h"
Paul Lawrence731a7a22015-04-28 22:14:15 +000045
46#define LOG_TAG "Ext4Crypt"
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080047
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070048#define EMULATED_USES_SELINUX 0
49
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080050#include <cutils/fs.h>
51#include <cutils/log.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000052#include <cutils/klog.h>
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080053
Elliott Hughes7e128fb2015-12-04 15:50:53 -080054#include <android-base/file.h>
Elliott Hughes6bf05472015-12-04 17:55:33 -080055#include <android-base/logging.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080056#include <android-base/stringprintf.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000057
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080058using android::base::StringPrintf;
59
Jeff Sharkeyfc505c32015-12-07 17:27:01 -070060static bool e4crypt_is_native() {
61 char value[PROPERTY_VALUE_MAX];
62 property_get("ro.crypto.type", value, "none");
63 return !strcmp(value, "file");
64}
65
66static bool e4crypt_is_emulated() {
67 return property_get_bool("persist.sys.emulate_fbe", false);
68}
Jeff Sharkeyc79fb892015-11-12 20:18:02 -080069
Paul Lawrence731a7a22015-04-28 22:14:15 +000070namespace {
71 // Key length in bits
72 const int key_length = 128;
Paul Lawrencefd7db732015-04-10 07:48:51 -070073 static_assert(key_length % 8 == 0,
74 "Key length must be multiple of 8 bits");
Paul Lawrence731a7a22015-04-28 22:14:15 +000075
Paul Lawrence86c942a2015-05-06 13:53:43 -070076 // How long do we store passwords for?
77 const int password_max_age_seconds = 60;
78
Paul Crowley285956f2016-01-20 13:12:38 +000079 const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
Paul Crowleyb1f3d242016-01-28 10:09:46 +000080 const std::string user_key_temp = user_key_dir + "/temp";
Paul Crowley285956f2016-01-20 13:12:38 +000081
Paul Lawrence731a7a22015-04-28 22:14:15 +000082 // How is device encrypted
83 struct keys {
84 std::string master_key;
85 std::string password;
Paul Lawrence86c942a2015-05-06 13:53:43 -070086 time_t expiry_time;
Paul Lawrence731a7a22015-04-28 22:14:15 +000087 };
88 std::map<std::string, keys> s_key_store;
Paul Crowleyb1f3d242016-01-28 10:09:46 +000089 // Some users are ephemeral, don't try to wipe their keys from disk
90 std::set<userid_t> s_ephemeral_users;
91 // Map user ids to key references
Paul Crowleyb92f83c2016-02-01 14:10:43 +000092 std::map<userid_t, std::string> s_de_key_raw_refs;
Paul Crowleyb1f3d242016-01-28 10:09:46 +000093 std::map<userid_t, std::string> s_ce_key_raw_refs;
Paul Lawrence731a7a22015-04-28 22:14:15 +000094
Paul Crowley285956f2016-01-20 13:12:38 +000095 // ext4enc:TODO get this const from somewhere good
Paul Lawrencefd7db732015-04-10 07:48:51 -070096 const int EXT4_KEY_DESCRIPTOR_SIZE = 8;
97
Paul Lawrence731a7a22015-04-28 22:14:15 +000098 // ext4enc:TODO Include structure from somewhere sensible
99 // MUST be in sync with ext4_crypto.c in kernel
Paul Lawrencefd7db732015-04-10 07:48:51 -0700100 const int EXT4_MAX_KEY_SIZE = 64;
101 const int EXT4_ENCRYPTION_MODE_AES_256_XTS = 1;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000102 struct ext4_encryption_key {
Paul Lawrencefd7db732015-04-10 07:48:51 -0700103 uint32_t mode;
104 char raw[EXT4_MAX_KEY_SIZE];
105 uint32_t size;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000106 };
107
108 namespace tag {
109 const char* magic = "magic";
110 const char* major_version = "major_version";
111 const char* minor_version = "minor_version";
112 const char* flags = "flags";
113 const char* crypt_type = "crypt_type";
114 const char* failed_decrypt_count = "failed_decrypt_count";
115 const char* crypto_type_name = "crypto_type_name";
116 const char* master_key = "master_key";
117 const char* salt = "salt";
118 const char* kdf_type = "kdf_type";
119 const char* N_factor = "N_factor";
120 const char* r_factor = "r_factor";
121 const char* p_factor = "p_factor";
122 const char* keymaster_blob = "keymaster_blob";
123 const char* scrypted_intermediate_key = "scrypted_intermediate_key";
124 }
125}
126
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000127static bool install_key(const std::string &key, std::string &raw_ref);
Paul Crowleyf25a35a2015-05-06 13:38:53 +0100128
Paul Lawrence731a7a22015-04-28 22:14:15 +0000129static int put_crypt_ftr_and_key(const crypt_mnt_ftr& crypt_ftr,
130 UnencryptedProperties& props)
131{
132 SLOGI("Putting crypt footer");
133
134 bool success = props.Set<int>(tag::magic, crypt_ftr.magic)
135 && props.Set<int>(tag::major_version, crypt_ftr.major_version)
136 && props.Set<int>(tag::minor_version, crypt_ftr.minor_version)
137 && props.Set<int>(tag::flags, crypt_ftr.flags)
138 && props.Set<int>(tag::crypt_type, crypt_ftr.crypt_type)
139 && props.Set<int>(tag::failed_decrypt_count,
140 crypt_ftr.failed_decrypt_count)
141 && props.Set<std::string>(tag::crypto_type_name,
142 std::string(reinterpret_cast<const char*>(crypt_ftr.crypto_type_name)))
143 && props.Set<std::string>(tag::master_key,
144 std::string((const char*) crypt_ftr.master_key,
145 crypt_ftr.keysize))
146 && props.Set<std::string>(tag::salt,
147 std::string((const char*) crypt_ftr.salt,
148 SALT_LEN))
149 && props.Set<int>(tag::kdf_type, crypt_ftr.kdf_type)
150 && props.Set<int>(tag::N_factor, crypt_ftr.N_factor)
151 && props.Set<int>(tag::r_factor, crypt_ftr.r_factor)
152 && props.Set<int>(tag::p_factor, crypt_ftr.p_factor)
153 && props.Set<std::string>(tag::keymaster_blob,
154 std::string((const char*) crypt_ftr.keymaster_blob,
155 crypt_ftr.keymaster_blob_size))
156 && props.Set<std::string>(tag::scrypted_intermediate_key,
157 std::string((const char*) crypt_ftr.scrypted_intermediate_key,
158 SCRYPT_LEN));
159 return success ? 0 : -1;
160}
161
162static int get_crypt_ftr_and_key(crypt_mnt_ftr& crypt_ftr,
163 const UnencryptedProperties& props)
164{
165 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
166 crypt_ftr.magic = props.Get<int>(tag::magic);
167 crypt_ftr.major_version = props.Get<int>(tag::major_version);
168 crypt_ftr.minor_version = props.Get<int>(tag::minor_version);
Paul Lawrence0d9cd9e2015-05-05 15:58:27 -0700169 crypt_ftr.ftr_size = sizeof(crypt_ftr);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000170 crypt_ftr.flags = props.Get<int>(tag::flags);
171 crypt_ftr.crypt_type = props.Get<int>(tag::crypt_type);
172 crypt_ftr.failed_decrypt_count = props.Get<int>(tag::failed_decrypt_count);
173 std::string crypto_type_name = props.Get<std::string>(tag::crypto_type_name);
174 strlcpy(reinterpret_cast<char*>(crypt_ftr.crypto_type_name),
175 crypto_type_name.c_str(),
176 sizeof(crypt_ftr.crypto_type_name));
177 std::string master_key = props.Get<std::string>(tag::master_key);
178 crypt_ftr.keysize = master_key.size();
179 if (crypt_ftr.keysize > sizeof(crypt_ftr.master_key)) {
180 SLOGE("Master key size too long");
181 return -1;
182 }
183 memcpy(crypt_ftr.master_key, &master_key[0], crypt_ftr.keysize);
184 std::string salt = props.Get<std::string>(tag::salt);
185 if (salt.size() != SALT_LEN) {
186 SLOGE("Salt wrong length");
187 return -1;
188 }
189 memcpy(crypt_ftr.salt, &salt[0], SALT_LEN);
190 crypt_ftr.kdf_type = props.Get<int>(tag::kdf_type);
191 crypt_ftr.N_factor = props.Get<int>(tag::N_factor);
192 crypt_ftr.r_factor = props.Get<int>(tag::r_factor);
193 crypt_ftr.p_factor = props.Get<int>(tag::p_factor);
194 std::string keymaster_blob = props.Get<std::string>(tag::keymaster_blob);
195 crypt_ftr.keymaster_blob_size = keymaster_blob.size();
196 if (crypt_ftr.keymaster_blob_size > sizeof(crypt_ftr.keymaster_blob)) {
197 SLOGE("Keymaster blob too long");
198 return -1;
199 }
200 memcpy(crypt_ftr.keymaster_blob, &keymaster_blob[0],
201 crypt_ftr.keymaster_blob_size);
202 std::string scrypted_intermediate_key = props.Get<std::string>(tag::scrypted_intermediate_key);
203 if (scrypted_intermediate_key.size() != SCRYPT_LEN) {
204 SLOGE("scrypted intermediate key wrong length");
205 return -1;
206 }
207 memcpy(crypt_ftr.scrypted_intermediate_key, &scrypted_intermediate_key[0],
208 SCRYPT_LEN);
209
210 return 0;
211}
212
213static UnencryptedProperties GetProps(const char* path)
214{
215 return UnencryptedProperties(path);
216}
217
218static UnencryptedProperties GetAltProps(const char* path)
219{
220 return UnencryptedProperties((std::string() + path + "/tmp_mnt").c_str());
221}
222
223static UnencryptedProperties GetPropsOrAltProps(const char* path)
224{
225 UnencryptedProperties props = GetProps(path);
226 if (props.OK()) {
227 return props;
228 }
229 return GetAltProps(path);
230}
231
232int e4crypt_enable(const char* path)
233{
234 // Already enabled?
235 if (s_key_store.find(path) != s_key_store.end()) {
236 return 0;
237 }
238
239 // Not an encryptable device?
240 UnencryptedProperties key_props = GetProps(path).GetChild(properties::key);
241 if (!key_props.OK()) {
242 return 0;
243 }
244
245 if (key_props.Get<std::string>(tag::master_key).empty()) {
246 crypt_mnt_ftr ftr;
247 if (cryptfs_create_default_ftr(&ftr, key_length)) {
248 SLOGE("Failed to create crypto footer");
249 return -1;
250 }
251
Paul Lawrence0d9cd9e2015-05-05 15:58:27 -0700252 // Scrub fields not used by ext4enc
253 ftr.persist_data_offset[0] = 0;
254 ftr.persist_data_offset[1] = 0;
255 ftr.persist_data_size = 0;
256
Paul Lawrence731a7a22015-04-28 22:14:15 +0000257 if (put_crypt_ftr_and_key(ftr, key_props)) {
258 SLOGE("Failed to write crypto footer");
259 return -1;
260 }
261
262 crypt_mnt_ftr ftr2;
263 if (get_crypt_ftr_and_key(ftr2, key_props)) {
264 SLOGE("Failed to read crypto footer back");
265 return -1;
266 }
267
268 if (memcmp(&ftr, &ftr2, sizeof(ftr)) != 0) {
269 SLOGE("Crypto footer not correctly written");
Paul Lawrence0d9cd9e2015-05-05 15:58:27 -0700270 return -1;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000271 }
272 }
273
274 if (!UnencryptedProperties(path).Remove(properties::ref)) {
275 SLOGE("Failed to remove key ref");
276 return -1;
277 }
278
279 return e4crypt_check_passwd(path, "");
280}
281
282int e4crypt_change_password(const char* path, int crypt_type,
283 const char* password)
284{
285 SLOGI("e4crypt_change_password");
Paul Lawrencea56d3132015-05-04 15:48:24 -0700286 auto key_props = GetProps(path).GetChild(properties::key);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000287
288 crypt_mnt_ftr ftr;
289 if (get_crypt_ftr_and_key(ftr, key_props)) {
290 SLOGE("Failed to read crypto footer back");
291 return -1;
292 }
293
294 auto mki = s_key_store.find(path);
295 if (mki == s_key_store.end()) {
296 SLOGE("No stored master key - can't change password");
297 return -1;
298 }
299
Paul Crowley95376d62015-05-06 15:04:43 +0100300 const unsigned char* master_key_bytes
Paul Lawrence731a7a22015-04-28 22:14:15 +0000301 = reinterpret_cast<const unsigned char*>(&mki->second.master_key[0]);
302
Paul Crowley95376d62015-05-06 15:04:43 +0100303 if (cryptfs_set_password(&ftr, password, master_key_bytes)) {
Paul Lawrence731a7a22015-04-28 22:14:15 +0000304 SLOGE("Failed to set password");
305 return -1;
306 }
307
308 ftr.crypt_type = crypt_type;
309
310 if (put_crypt_ftr_and_key(ftr, key_props)) {
311 SLOGE("Failed to write crypto footer");
312 return -1;
313 }
314
315 if (!UnencryptedProperties(path).Set(properties::is_default,
316 crypt_type == CRYPT_TYPE_DEFAULT)) {
317 SLOGE("Failed to update default flag");
318 return -1;
319 }
320
321 return 0;
322}
323
324int e4crypt_crypto_complete(const char* path)
325{
326 SLOGI("ext4 crypto complete called on %s", path);
Paul Lawrencea56d3132015-05-04 15:48:24 -0700327 auto key_props = GetPropsOrAltProps(path).GetChild(properties::key);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000328 if (key_props.Get<std::string>(tag::master_key).empty()) {
329 SLOGI("No master key, so not ext4enc");
330 return -1;
331 }
332
333 return 0;
334}
335
Paul Crowley93363482015-07-07 15:17:22 +0100336// Get raw keyref - used to make keyname and to pass to ioctl
Paul Lawrencefd7db732015-04-10 07:48:51 -0700337static std::string generate_key_ref(const char* key, int length)
338{
339 SHA512_CTX c;
340
341 SHA512_Init(&c);
342 SHA512_Update(&c, key, length);
Paul Crowley285956f2016-01-20 13:12:38 +0000343 unsigned char key_ref1[SHA512_DIGEST_LENGTH];
Paul Lawrencefd7db732015-04-10 07:48:51 -0700344 SHA512_Final(key_ref1, &c);
345
346 SHA512_Init(&c);
Paul Crowley285956f2016-01-20 13:12:38 +0000347 SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH);
348 unsigned char key_ref2[SHA512_DIGEST_LENGTH];
Paul Lawrencefd7db732015-04-10 07:48:51 -0700349 SHA512_Final(key_ref2, &c);
350
351 return std::string((char*)key_ref2, EXT4_KEY_DESCRIPTOR_SIZE);
352}
353
Paul Lawrence731a7a22015-04-28 22:14:15 +0000354int e4crypt_check_passwd(const char* path, const char* password)
355{
356 SLOGI("e4crypt_check_password");
Paul Lawrencea56d3132015-05-04 15:48:24 -0700357 auto props = GetPropsOrAltProps(path);
358 auto key_props = props.GetChild(properties::key);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000359
360 crypt_mnt_ftr ftr;
361 if (get_crypt_ftr_and_key(ftr, key_props)) {
362 SLOGE("Failed to read crypto footer back");
363 return -1;
364 }
365
Paul Crowley95376d62015-05-06 15:04:43 +0100366 unsigned char master_key_bytes[key_length / 8];
367 if (cryptfs_get_master_key (&ftr, password, master_key_bytes)){
Paul Lawrence731a7a22015-04-28 22:14:15 +0000368 SLOGI("Incorrect password");
Paul Lawrencec78c71b2015-04-14 15:26:29 -0700369 ftr.failed_decrypt_count++;
370 if (put_crypt_ftr_and_key(ftr, key_props)) {
371 SLOGW("Failed to update failed_decrypt_count");
372 }
373 return ftr.failed_decrypt_count;
374 }
375
376 if (ftr.failed_decrypt_count) {
377 ftr.failed_decrypt_count = 0;
378 if (put_crypt_ftr_and_key(ftr, key_props)) {
379 SLOGW("Failed to reset failed_decrypt_count");
380 }
Paul Lawrence731a7a22015-04-28 22:14:15 +0000381 }
Paul Crowley95376d62015-05-06 15:04:43 +0100382 std::string master_key(reinterpret_cast<char*>(master_key_bytes),
383 sizeof(master_key_bytes));
Paul Lawrence731a7a22015-04-28 22:14:15 +0000384
Paul Lawrence86c942a2015-05-06 13:53:43 -0700385 struct timespec now;
386 clock_gettime(CLOCK_BOOTTIME, &now);
Paul Crowley95376d62015-05-06 15:04:43 +0100387 s_key_store[path] = keys{master_key, password,
Paul Lawrence86c942a2015-05-06 13:53:43 -0700388 now.tv_sec + password_max_age_seconds};
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000389 std::string raw_ref;
390 if (!install_key(master_key, raw_ref)) {
Paul Crowleyf25a35a2015-05-06 13:38:53 +0100391 return -1;
392 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000393 SLOGD("Installed master key");
Paul Lawrence731a7a22015-04-28 22:14:15 +0000394
Paul Crowleyf25a35a2015-05-06 13:38:53 +0100395 // Save reference to key so we can set policy later
396 if (!props.Set(properties::ref, raw_ref)) {
397 SLOGE("Cannot save key reference");
398 return -1;
399 }
400
401 return 0;
402}
403
Paul Crowley93363482015-07-07 15:17:22 +0100404static ext4_encryption_key fill_key(const std::string &key)
Paul Crowleyf25a35a2015-05-06 13:38:53 +0100405{
Paul Lawrencefd7db732015-04-10 07:48:51 -0700406 // ext4enc:TODO Currently raw key is required to be of length
407 // sizeof(ext4_key.raw) == EXT4_MAX_KEY_SIZE, so zero pad to
408 // this length. Change when kernel bug is fixed.
409 ext4_encryption_key ext4_key = {EXT4_ENCRYPTION_MODE_AES_256_XTS,
410 {0},
411 sizeof(ext4_key.raw)};
412 memset(ext4_key.raw, 0, sizeof(ext4_key.raw));
413 static_assert(key_length / 8 <= sizeof(ext4_key.raw),
414 "Key too long!");
Paul Crowley95376d62015-05-06 15:04:43 +0100415 memcpy(ext4_key.raw, &key[0], key.size());
Paul Crowley93363482015-07-07 15:17:22 +0100416 return ext4_key;
417}
Paul Lawrence731a7a22015-04-28 22:14:15 +0000418
Paul Crowley93363482015-07-07 15:17:22 +0100419static std::string keyname(const std::string &raw_ref)
420{
Paul Lawrencefd7db732015-04-10 07:48:51 -0700421 std::ostringstream o;
Paul Crowley93363482015-07-07 15:17:22 +0100422 o << "ext4:";
Paul Lawrencefd7db732015-04-10 07:48:51 -0700423 for (auto i = raw_ref.begin(); i != raw_ref.end(); ++i) {
424 o << std::hex << std::setw(2) << std::setfill('0') << (int)*i;
425 }
Paul Crowley93363482015-07-07 15:17:22 +0100426 return o.str();
427}
Paul Lawrencefd7db732015-04-10 07:48:51 -0700428
Paul Crowley93363482015-07-07 15:17:22 +0100429// Get the keyring we store all keys in
430static key_serial_t e4crypt_keyring()
431{
432 return keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "e4crypt", 0);
433}
Paul Lawrence731a7a22015-04-28 22:14:15 +0000434
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000435// Install password into global keyring
436// Return raw key reference for use in policy
437static bool install_key(const std::string &key, std::string &raw_ref)
Paul Crowley93363482015-07-07 15:17:22 +0100438{
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000439 if (key.size() != key_length/8) {
440 LOG(ERROR) << "Wrong size key " << key.size();
441 return false;
442 }
443 auto ext4_key = fill_key(key);
444 raw_ref = generate_key_ref(ext4_key.raw, ext4_key.size);
445 auto ref = keyname(raw_ref);
Paul Crowley93363482015-07-07 15:17:22 +0100446 key_serial_t device_keyring = e4crypt_keyring();
Paul Lawrencefd7db732015-04-10 07:48:51 -0700447 key_serial_t key_id = add_key("logon", ref.c_str(),
Paul Lawrence731a7a22015-04-28 22:14:15 +0000448 (void*)&ext4_key, sizeof(ext4_key),
449 device_keyring);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000450 if (key_id == -1) {
Paul Crowley285956f2016-01-20 13:12:38 +0000451 PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000452 return false;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000453 }
Paul Crowley285956f2016-01-20 13:12:38 +0000454 LOG(INFO) << "Added key " << key_id << " (" << ref << ") to keyring "
455 << device_keyring << " in process " << getpid();
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000456 return true;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000457}
458
459int e4crypt_restart(const char* path)
460{
461 SLOGI("e4crypt_restart");
462
463 int rc = 0;
464
465 SLOGI("ext4 restart called on %s", path);
466 property_set("vold.decrypt", "trigger_reset_main");
467 SLOGI("Just asked init to shut down class main");
468 sleep(2);
469
470 std::string tmp_path = std::string() + path + "/tmp_mnt";
471
Paul Lawrence2f32cda2015-05-05 14:28:25 -0700472 rc = wait_and_unmount(tmp_path.c_str(), true);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000473 if (rc) {
474 SLOGE("umount %s failed with rc %d, msg %s",
475 tmp_path.c_str(), rc, strerror(errno));
476 return rc;
477 }
478
Paul Lawrence2f32cda2015-05-05 14:28:25 -0700479 rc = wait_and_unmount(path, true);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000480 if (rc) {
481 SLOGE("umount %s failed with rc %d, msg %s",
482 path, rc, strerror(errno));
483 return rc;
484 }
485
486 return 0;
487}
488
Paul Lawrence731a7a22015-04-28 22:14:15 +0000489int e4crypt_get_password_type(const char* path)
490{
491 SLOGI("e4crypt_get_password_type");
492 return GetPropsOrAltProps(path).GetChild(properties::key)
493 .Get<int>(tag::crypt_type, CRYPT_TYPE_DEFAULT);
494}
Paul Lawrence368d7942015-04-15 14:12:00 -0700495
Paul Lawrence86c942a2015-05-06 13:53:43 -0700496const char* e4crypt_get_password(const char* path)
497{
498 SLOGI("e4crypt_get_password");
499
500 auto i = s_key_store.find(path);
501 if (i == s_key_store.end()) {
502 return 0;
503 }
504
505 struct timespec now;
506 clock_gettime(CLOCK_BOOTTIME, &now);
507 if (i->second.expiry_time < now.tv_sec) {
508 e4crypt_clear_password(path);
509 return 0;
510 }
511
512 return i->second.password.c_str();
513}
514
515void e4crypt_clear_password(const char* path)
516{
517 SLOGI("e4crypt_clear_password");
518
519 auto i = s_key_store.find(path);
520 if (i == s_key_store.end()) {
521 return;
522 }
523
524 memset(&i->second.password[0], 0, i->second.password.size());
525 i->second.password = std::string();
526}
527
Paul Lawrence368d7942015-04-15 14:12:00 -0700528int e4crypt_get_field(const char* path, const char* fieldname,
529 char* value, size_t len)
530{
531 auto v = GetPropsOrAltProps(path).GetChild(properties::props)
532 .Get<std::string>(fieldname);
533
534 if (v == "") {
535 return CRYPTO_GETFIELD_ERROR_NO_FIELD;
536 }
537
538 if (v.length() >= len) {
539 return CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
540 }
541
542 strlcpy(value, v.c_str(), len);
543 return 0;
544}
545
546int e4crypt_set_field(const char* path, const char* fieldname,
547 const char* value)
548{
549 return GetPropsOrAltProps(path).GetChild(properties::props)
550 .Set(fieldname, std::string(value)) ? 0 : -1;
551}
Paul Crowley95376d62015-05-06 15:04:43 +0100552
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000553static std::string get_de_key_path(userid_t user_id) {
554 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
555}
556
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000557static std::string get_ce_key_path(userid_t user_id) {
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000558 return StringPrintf("%s/ce/%d/current", user_key_dir.c_str(), user_id);
Paul Crowleyb33e8872015-05-19 12:34:09 +0100559}
560
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000561static bool read_and_install_key(const std::string &key_path, std::string &raw_ref)
562{
563 std::string key;
564 if (!android::vold::retrieveKey(key_path, key)) return false;
565 if (!install_key(key, raw_ref)) return false;
566 return true;
Lenka Trochtova395039f2015-11-25 10:13:03 +0100567}
568
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000569static bool read_and_install_user_ce_key(userid_t user_id)
Paul Crowleyb33e8872015-05-19 12:34:09 +0100570{
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000571 if (s_ce_key_raw_refs.count(user_id) != 0) return true;
572 const auto key_path = get_ce_key_path(user_id);
573 std::string raw_ref;
574 if (!read_and_install_key(key_path, raw_ref)) return false;
575 s_ce_key_raw_refs[user_id] = raw_ref;
576 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000577 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000578}
579
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000580static bool prepare_dir(const std::string &dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000581 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000582 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
583 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000584 return false;
585 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000586 return true;
587}
588
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000589static bool random_key(std::string &key) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000590 if (android::vold::ReadRandomBytes(key_length / 8, key) != 0) {
591 // TODO status_t plays badly with PLOG, fix it.
592 LOG(ERROR) << "Random read failed";
Paul Crowley285956f2016-01-20 13:12:38 +0000593 return false;
594 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000595 return true;
596}
597
598static bool path_exists(const std::string &path) {
599 return access(path.c_str(), F_OK) == 0;
600}
601
602// NB this assumes that there is only one thread listening for crypt commands, because
603// it creates keys in a fixed location.
604static bool store_key(const std::string &key_path, const std::string &key) {
605 if (path_exists(key_path)) {
606 LOG(ERROR) << "Already exists, cannot create key at: " << key_path;
607 return false;
608 }
609 if (path_exists(user_key_temp)) {
610 android::vold::destroyKey(user_key_temp);
611 }
612 if (!android::vold::storeKey(user_key_temp, key)) return false;
613 if (rename(user_key_temp.c_str(), key_path.c_str()) != 0) {
614 PLOG(ERROR) << "Unable to move new key to location: " << key_path;
615 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100616 }
Paul Crowley285956f2016-01-20 13:12:38 +0000617 LOG(DEBUG) << "Created key " << key_path;
Paul Crowley95376d62015-05-06 15:04:43 +0100618 return true;
619}
620
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000621static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
622 std::string de_key, ce_key;
623 if (!random_key(de_key)) return false;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000624 if (!random_key(ce_key)) return false;
625 if (create_ephemeral) {
626 // If the key should be created as ephemeral, don't store it.
627 s_ephemeral_users.insert(user_id);
628 } else {
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000629 if (!store_key(get_de_key_path(user_id), de_key)) return false;
630 if (!prepare_dir(user_key_dir + "/ce/" + std::to_string(user_id),
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000631 0700, AID_ROOT, AID_ROOT)) return false;
632 if (!store_key(get_ce_key_path(user_id), ce_key)) return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100633 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000634 std::string de_raw_ref;
635 if (!install_key(de_key, de_raw_ref)) return false;
636 s_de_key_raw_refs[user_id] = de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000637 std::string ce_raw_ref;
638 if (!install_key(ce_key, ce_raw_ref)) return false;
639 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000640 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000641 return true;
642}
643
644static bool lookup_key_ref(const std::map<userid_t, std::string> &key_map,
645 userid_t user_id, std::string &raw_ref) {
646 auto refi = key_map.find(user_id);
647 if (refi == key_map.end()) {
648 LOG(ERROR) << "Cannot find key for " << user_id;
649 return false;
650 }
651 raw_ref = refi->second;
652 return true;
653}
654
655static bool set_policy(const std::string &raw_ref, const std::string& path) {
656 if (do_policy_set(path.c_str(), raw_ref.data(), raw_ref.size()) != 0) {
657 LOG(ERROR) << "Failed to set policy on: " << path;
658 return false;
659 }
660 return true;
Paul Crowley95376d62015-05-06 15:04:43 +0100661}
Paul Crowleyb33e8872015-05-19 12:34:09 +0100662
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000663static bool is_numeric(const char *name) {
664 for (const char *p = name; *p != '\0'; p++) {
665 if (!isdigit(*p))
666 return false;
667 }
668 return true;
669}
670
671static bool load_all_de_keys() {
672 auto de_dir = user_key_dir + "/de";
673 auto dirp = std::unique_ptr<DIR, int(*)(DIR*)>(opendir(de_dir.c_str()), closedir);
674 if (!dirp) {
675 PLOG(ERROR) << "Unable to read de key directory";
676 return false;
677 }
678 for (;;) {
679 errno = 0;
680 auto entry = readdir(dirp.get());
681 if (!entry) {
682 if (errno) {
683 PLOG(ERROR) << "Unable to read de key directory";
684 return false;
685 }
686 break;
687 }
688 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
689 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
690 continue;
691 }
692 userid_t user_id = atoi(entry->d_name);
693 if (s_de_key_raw_refs.count(user_id) == 0) {
694 std::string raw_ref;
695 if (!read_and_install_key(de_dir + "/" + entry->d_name, raw_ref)) return false;
696 s_de_key_raw_refs[user_id] = raw_ref;
697 LOG(DEBUG) << "Installed de key for user " << user_id;
698 }
699 }
700 // ext4enc:TODO: go through all DE directories, ensure that all user dirs have the
701 // correct policy set on them, and that no rogue ones exist.
702 return true;
703}
704
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000705int e4crypt_init_user0() {
706 LOG(DEBUG) << "e4crypt_init_user0";
707 if (e4crypt_is_native()) {
708 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return -1;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000709 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return -1;
710 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return -1;
711 auto de_path = get_de_key_path(0);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000712 auto ce_path = get_ce_key_path(0);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000713 if (!path_exists(de_path) || !path_exists(ce_path)) {
714 if (path_exists(de_path)) {
715 android::vold::destroyKey(de_path); // Ignore failure
716 }
717 if (path_exists(ce_path)) {
718 android::vold::destroyKey(ce_path); // Ignore failure
719 }
720 if (!create_and_install_user_keys(0, false)) return -1;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000721 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000722 if (!load_all_de_keys()) return -1;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000723 }
724 // Ignore failures. FIXME this is horrid
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000725 // FIXME: we need an idempotent policy-setting call, which simply verifies the
726 // policy is already set on a second run, even if the directory is nonempty.
727 // Then we need to call it all the time.
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000728 e4crypt_prepare_user_storage(nullptr, 0, 0, false);
729 return 0;
730}
731
Paul Crowley27cbce92015-12-10 14:51:30 +0000732int e4crypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
Paul Crowley285956f2016-01-20 13:12:38 +0000733 LOG(DEBUG) << "e4crypt_vold_create_user_key for " << user_id << " serial " << serial;
Paul Crowleyea62e262016-01-28 12:23:53 +0000734 if (!e4crypt_is_native()) {
735 return 0;
736 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000737 // FIXME test for existence of key that is not loaded yet
738 if (s_ce_key_raw_refs.count(user_id) != 0) {
Paul Crowley285956f2016-01-20 13:12:38 +0000739 LOG(ERROR) << "Already exists, can't e4crypt_vold_create_user_key for "
740 << user_id << " serial " << serial;
741 // FIXME should we fail the command?
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800742 return 0;
743 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000744 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley285956f2016-01-20 13:12:38 +0000745 return -1;
746 }
747 // TODO: create second key for user_de data
748 return 0;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800749}
750
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000751static bool evict_key(const std::string &raw_ref) {
752 auto ref = keyname(raw_ref);
Paul Crowley93363482015-07-07 15:17:22 +0100753 auto key_serial = keyctl_search(e4crypt_keyring(), "logon", ref.c_str(), 0);
Paul Crowley1ef25582016-01-21 20:26:12 +0000754 if (keyctl_revoke(key_serial) != 0) {
Paul Crowley285956f2016-01-20 13:12:38 +0000755 PLOG(ERROR) << "Failed to revoke key with serial " << key_serial << " ref " << ref;
Paul Crowley1ef25582016-01-21 20:26:12 +0000756 return false;
Paul Crowley93363482015-07-07 15:17:22 +0100757 }
Paul Crowley1ef25582016-01-21 20:26:12 +0000758 LOG(DEBUG) << "Revoked key with serial " << key_serial << " ref " << ref;
759 return true;
760}
761
762int e4crypt_destroy_user_key(userid_t user_id) {
763 LOG(DEBUG) << "e4crypt_destroy_user_key(" << user_id << ")";
Paul Crowleyea62e262016-01-28 12:23:53 +0000764 if (!e4crypt_is_native()) {
765 return 0;
766 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000767 bool success = true;
768 std::string raw_ref;
769 success &= lookup_key_ref(s_ce_key_raw_refs, user_id, raw_ref) && evict_key(raw_ref);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000770 success &= lookup_key_ref(s_de_key_raw_refs, user_id, raw_ref) && evict_key(raw_ref);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000771 auto it = s_ephemeral_users.find(user_id);
772 if (it != s_ephemeral_users.end()) {
773 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000774 } else {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000775 success &= android::vold::destroyKey(get_ce_key_path(user_id));
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000776 success &= android::vold::destroyKey(get_de_key_path(user_id));
Lenka Trochtova395039f2015-11-25 10:13:03 +0100777 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000778 return success ? 0 : -1;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100779}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800780
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700781static int emulated_lock(const std::string& path) {
782 if (chmod(path.c_str(), 0000) != 0) {
783 PLOG(ERROR) << "Failed to chmod " << path;
784 return -1;
785 }
786#if EMULATED_USES_SELINUX
787 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
788 PLOG(WARNING) << "Failed to setfilecon " << path;
789 return -1;
790 }
791#endif
792 return 0;
793}
794
795static int emulated_unlock(const std::string& path, mode_t mode) {
796 if (chmod(path.c_str(), mode) != 0) {
797 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000798 // FIXME temporary workaround for b/26713622
799 if (e4crypt_is_emulated()) return -1;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700800 }
801#if EMULATED_USES_SELINUX
802 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
803 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000804 // FIXME temporary workaround for b/26713622
805 if (e4crypt_is_emulated()) return -1;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700806 }
807#endif
808 return 0;
809}
810
Paul Crowley285956f2016-01-20 13:12:38 +0000811int e4crypt_unlock_user_key(userid_t user_id, int serial, const char* token) {
812 LOG(DEBUG) << "e4crypt_unlock_user_key " << user_id << " " << (token != nullptr);
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700813 if (e4crypt_is_native()) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000814 if (!read_and_install_user_ce_key(user_id)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000815 LOG(ERROR) << "Couldn't read key for " << user_id;
816 return -1;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800817 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700818 } else {
819 // When in emulation mode, we just use chmod. However, we also
820 // unlock directories when not in emulation mode, to bring devices
821 // back into a known-good state.
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700822 if (emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
823 emulated_unlock(android::vold::BuildDataMediaPath(nullptr, user_id), 0770) ||
824 emulated_unlock(android::vold::BuildDataUserPath(nullptr, user_id), 0771)) {
825 LOG(ERROR) << "Failed to unlock user " << user_id;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700826 return -1;
827 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800828 }
829 return 0;
830}
831
832int e4crypt_lock_user_key(userid_t user_id) {
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700833 if (e4crypt_is_native()) {
834 // TODO: remove from kernel keyring
835 } else if (e4crypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800836 // When in emulation mode, we just use chmod
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700837 if (emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
838 emulated_lock(android::vold::BuildDataMediaPath(nullptr, user_id)) ||
839 emulated_lock(android::vold::BuildDataUserPath(nullptr, user_id))) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800840 PLOG(ERROR) << "Failed to lock user " << user_id;
841 return -1;
842 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800843 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700844
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800845 return 0;
846}
847
Lenka Trochtova9ad43692015-12-11 13:27:26 +0100848int e4crypt_prepare_user_storage(const char* volume_uuid,
849 userid_t user_id,
850 int serial,
851 bool ephemeral) {
Paul Crowley285956f2016-01-20 13:12:38 +0000852 if (volume_uuid) {
853 LOG(DEBUG) << "e4crypt_prepare_user_storage " << volume_uuid << " " << user_id;
854 } else {
855 LOG(DEBUG) << "e4crypt_prepare_user_storage, null volume " << user_id;
856 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000857 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
858 auto media_ce_path = android::vold::BuildDataMediaPath(volume_uuid, user_id);
859 auto user_ce_path = android::vold::BuildDataUserPath(volume_uuid, user_id);
860 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800861
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000862 // FIXME: should this be 0770 or 0700?
863 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return -1;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000864 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return -1;
865 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return -1;
866 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return -1;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800867
868 if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000869 std::string ce_raw_ref, de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000870 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, ce_raw_ref)) return -1;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000871 if (!lookup_key_ref(s_de_key_raw_refs, user_id, de_raw_ref)) return -1;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000872 if (!set_policy(ce_raw_ref, system_ce_path)) return -1;
873 if (!set_policy(ce_raw_ref, media_ce_path)) return -1;
874 if (!set_policy(ce_raw_ref, user_ce_path)) return -1;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000875 if (!set_policy(de_raw_ref, user_de_path)) return -1;
876 // FIXME I thought there were more DE directories than this
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800877 }
878
879 return 0;
880}