Merge "Add context mount option for appfuse."
diff --git a/Android.mk b/Android.mk
index bea18b2..99a1739 100644
--- a/Android.mk
+++ b/Android.mk
@@ -88,7 +88,7 @@
 include $(CLEAR_VARS)
 
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
-LOCAL_MODULE:= vold
+LOCAL_MODULE := vold
 LOCAL_CLANG := true
 LOCAL_SRC_FILES := \
 	main.cpp \
@@ -115,9 +115,9 @@
 
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 LOCAL_CLANG := true
-LOCAL_SRC_FILES:= vdc.cpp
-LOCAL_MODULE:= vdc
-LOCAL_SHARED_LIBRARIES := libcutils
+LOCAL_SRC_FILES := vdc.cpp
+LOCAL_MODULE := vdc
+LOCAL_SHARED_LIBRARIES := libcutils libbase
 LOCAL_CFLAGS := $(vold_cflags)
 LOCAL_CONLYFLAGS := $(vold_conlyflags)
 LOCAL_INIT_RC := vdc.rc
diff --git a/CryptCommandListener.cpp b/CryptCommandListener.cpp
index 2eac60e..75c840f 100644
--- a/CryptCommandListener.cpp
+++ b/CryptCommandListener.cpp
@@ -30,6 +30,7 @@
 #include <inttypes.h>
 
 #include <algorithm>
+#include <thread>
 
 #define LOG_TAG "VoldCryptCmdListener"
 
@@ -144,6 +145,25 @@
     return false;
 }
 
+static int do_enablecrypto(char** argv, int type, bool no_ui) {
+    int rc;
+    int tries;
+    for (tries = 0; tries < 2; ++tries) {
+        if (type == CRYPT_TYPE_DEFAULT) {
+            rc = cryptfs_enable_default(argv[2], no_ui);
+        } else {
+            rc = cryptfs_enable(argv[2], type, argv[4], no_ui);
+        }
+
+        if (rc == 0) {
+            return 0;
+        } else if (tries == 0) {
+            Process::killProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
+        }
+    }
+    return -1;
+}
+
 int CryptCommandListener::CryptfsCmd::runCommand(SocketClient *cli,
                                                  int argc, char **argv) {
     if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
@@ -166,7 +186,10 @@
     } else if (subcommand == "restart") {
         if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
         dumpArgs(argc, argv, -1);
-        rc = cryptfs_restart();
+
+        // Spawn as thread so init can issue commands back to vold without
+        // causing deadlock, usually as a result of prep_data_fs.
+        std::thread(&cryptfs_restart).detach();
     } else if (subcommand == "cryptocomplete") {
         if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
         dumpArgs(argc, argv, -1);
@@ -216,31 +239,16 @@
             }
         }
 
-        if (!valid ) {
+        if (!valid) {
             cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
             return 0;
         }
 
         dumpArgs(argc, argv, 4);
 
-        int tries;
-        for (tries = 0; tries < 2; ++tries) {
-            if (type == -1) {
-                cli->sendMsg(ResponseCode::CommandSyntaxError, syntax,
-                             false);
-                return 0;
-            } else if (type == CRYPT_TYPE_DEFAULT) {
-              rc = cryptfs_enable_default(argv[2], no_ui);
-            } else {
-                rc = cryptfs_enable(argv[2], type, argv[4], no_ui);
-            }
-
-            if (rc == 0) {
-                break;
-            } else if (tries == 0) {
-                Process::killProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
-            }
-        }
+        // Spawn as thread so init can issue commands back to vold without
+        // causing deadlock, usually as a result of prep_data_fs.
+        std::thread(&do_enablecrypto, argv, type, no_ui).detach();
     } else if (subcommand == "enablefilecrypto") {
         if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
         dumpArgs(argc, argv, -1);
@@ -301,7 +309,10 @@
         if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
         SLOGD("cryptfs mountdefaultencrypted");
         dumpArgs(argc, argv, -1);
-        rc = cryptfs_mount_default_encrypted();
+
+        // Spawn as thread so init can issue commands back to vold without
+        // causing deadlock, usually as a result of prep_data_fs.
+        std::thread(&cryptfs_mount_default_encrypted).detach();
     } else if (subcommand == "getpwtype") {
         if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
         SLOGD("cryptfs getpwtype");
@@ -379,12 +390,12 @@
         return sendGenericOkFail(cli, e4crypt_lock_user_key(atoi(argv[2])));
 
     } else if (subcommand == "prepare_user_storage") {
-        if (!check_argc(cli, subcommand, argc, 6, "<uuid> <user> <serial> <ephemeral>")) return 0;
+        if (!check_argc(cli, subcommand, argc, 6, "<uuid> <user> <serial> <flags>")) return 0;
         return sendGenericOkFail(cli,
                                  e4crypt_prepare_user_storage(parseNull(argv[2]),
                                                               atoi(argv[3]),
                                                               atoi(argv[4]),
-                                                              atoi(argv[5]) != 0));
+                                                              atoi(argv[5])));
 
     } else {
         dumpArgs(argc, argv, -1);
diff --git a/Ext4Crypt.cpp b/Ext4Crypt.cpp
index 1b9661c..d66fdc6 100644
--- a/Ext4Crypt.cpp
+++ b/Ext4Crypt.cpp
@@ -38,10 +38,9 @@
 
 #include <private/android_filesystem_config.h>
 
-#include "unencrypted_properties.h"
 #include "key_control.h"
 #include "cryptfs.h"
-#include "ext4_crypt_init_extensions.h"
+#include "ext4_crypt.h"
 
 #define LOG_TAG "Ext4Crypt"
 
@@ -55,11 +54,12 @@
 #include <android-base/logging.h>
 #include <android-base/stringprintf.h>
 
-// TODO - remove when switch to using keymaster keys for device data
-static int e4crypt_check_passwd(const char* path, const char* password);
-
 using android::base::StringPrintf;
 
+// NOTE: keep in sync with StorageManager
+static constexpr int FLAG_STORAGE_DE = 1 << 0;
+static constexpr int FLAG_STORAGE_CE = 1 << 1;
+
 static bool e4crypt_is_native() {
     char value[PROPERTY_VALUE_MAX];
     property_get("ro.crypto.type", value, "none");
@@ -70,12 +70,19 @@
     return property_get_bool("persist.sys.emulate_fbe", false);
 }
 
+static const char* escape_null(const char* value) {
+    return (value == nullptr) ? "null" : value;
+}
+
 namespace {
     // Key length in bits
     const int key_length = 128;
     static_assert(key_length % 8 == 0,
                   "Key length must be multiple of 8 bits");
 
+    const std::string device_key_leaf = "/unencrypted/key";
+    const std::string device_key_temp = "/unencrypted/temp";
+
     const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
     const std::string user_key_temp = user_key_dir + "/temp";
 
@@ -83,6 +90,7 @@
 
     // Some users are ephemeral, don't try to wipe their keys from disk
     std::set<userid_t> s_ephemeral_users;
+
     // Map user ids to key references
     std::map<userid_t, std::string> s_de_key_raw_refs;
     std::map<userid_t, std::string> s_ce_key_raw_refs;
@@ -99,177 +107,12 @@
         char raw[EXT4_MAX_KEY_SIZE];
         uint32_t size;
     };
-
-    namespace tag {
-        const char* magic = "magic";
-        const char* major_version = "major_version";
-        const char* minor_version = "minor_version";
-        const char* flags = "flags";
-        const char* crypt_type = "crypt_type";
-        const char* failed_decrypt_count = "failed_decrypt_count";
-        const char* crypto_type_name = "crypto_type_name";
-        const char* master_key = "master_key";
-        const char* salt = "salt";
-        const char* kdf_type = "kdf_type";
-        const char* N_factor = "N_factor";
-        const char* r_factor = "r_factor";
-        const char* p_factor = "p_factor";
-        const char* keymaster_blob = "keymaster_blob";
-        const char* scrypted_intermediate_key = "scrypted_intermediate_key";
-    }
 }
 
-static bool install_key(const std::string &key, std::string &raw_ref);
-
-static int put_crypt_ftr_and_key(const crypt_mnt_ftr& crypt_ftr,
-                                 UnencryptedProperties& props)
-{
-    SLOGI("Putting crypt footer");
-
-    bool success = props.Set<int>(tag::magic, crypt_ftr.magic)
-      && props.Set<int>(tag::major_version, crypt_ftr.major_version)
-      && props.Set<int>(tag::minor_version, crypt_ftr.minor_version)
-      && props.Set<int>(tag::flags, crypt_ftr.flags)
-      && props.Set<int>(tag::crypt_type, crypt_ftr.crypt_type)
-      && props.Set<int>(tag::failed_decrypt_count,
-                        crypt_ftr.failed_decrypt_count)
-      && props.Set<std::string>(tag::crypto_type_name,
-                                std::string(reinterpret_cast<const char*>(crypt_ftr.crypto_type_name)))
-      && props.Set<std::string>(tag::master_key,
-                                std::string((const char*) crypt_ftr.master_key,
-                                            crypt_ftr.keysize))
-      && props.Set<std::string>(tag::salt,
-                                std::string((const char*) crypt_ftr.salt,
-                                            SALT_LEN))
-      && props.Set<int>(tag::kdf_type, crypt_ftr.kdf_type)
-      && props.Set<int>(tag::N_factor, crypt_ftr.N_factor)
-      && props.Set<int>(tag::r_factor, crypt_ftr.r_factor)
-      && props.Set<int>(tag::p_factor, crypt_ftr.p_factor)
-      && props.Set<std::string>(tag::keymaster_blob,
-                                std::string((const char*) crypt_ftr.keymaster_blob,
-                                            crypt_ftr.keymaster_blob_size))
-      && props.Set<std::string>(tag::scrypted_intermediate_key,
-                                std::string((const char*) crypt_ftr.scrypted_intermediate_key,
-                                            SCRYPT_LEN));
-    return success ? 0 : -1;
-}
-
-static int get_crypt_ftr_and_key(crypt_mnt_ftr& crypt_ftr,
-                                 const UnencryptedProperties& props)
-{
-    memset(&crypt_ftr, 0, sizeof(crypt_ftr));
-    crypt_ftr.magic = props.Get<int>(tag::magic);
-    crypt_ftr.major_version = props.Get<int>(tag::major_version);
-    crypt_ftr.minor_version = props.Get<int>(tag::minor_version);
-    crypt_ftr.ftr_size = sizeof(crypt_ftr);
-    crypt_ftr.flags = props.Get<int>(tag::flags);
-    crypt_ftr.crypt_type = props.Get<int>(tag::crypt_type);
-    crypt_ftr.failed_decrypt_count = props.Get<int>(tag::failed_decrypt_count);
-    std::string crypto_type_name = props.Get<std::string>(tag::crypto_type_name);
-    strlcpy(reinterpret_cast<char*>(crypt_ftr.crypto_type_name),
-            crypto_type_name.c_str(),
-            sizeof(crypt_ftr.crypto_type_name));
-    std::string master_key = props.Get<std::string>(tag::master_key);
-    crypt_ftr.keysize = master_key.size();
-    if (crypt_ftr.keysize > sizeof(crypt_ftr.master_key)) {
-        SLOGE("Master key size too long");
-        return -1;
-    }
-    memcpy(crypt_ftr.master_key, &master_key[0], crypt_ftr.keysize);
-    std::string salt = props.Get<std::string>(tag::salt);
-    if (salt.size() != SALT_LEN) {
-        SLOGE("Salt wrong length");
-        return -1;
-    }
-    memcpy(crypt_ftr.salt, &salt[0], SALT_LEN);
-    crypt_ftr.kdf_type = props.Get<int>(tag::kdf_type);
-    crypt_ftr.N_factor = props.Get<int>(tag::N_factor);
-    crypt_ftr.r_factor = props.Get<int>(tag::r_factor);
-    crypt_ftr.p_factor = props.Get<int>(tag::p_factor);
-    std::string keymaster_blob = props.Get<std::string>(tag::keymaster_blob);
-    crypt_ftr.keymaster_blob_size = keymaster_blob.size();
-    if (crypt_ftr.keymaster_blob_size > sizeof(crypt_ftr.keymaster_blob)) {
-        SLOGE("Keymaster blob too long");
-        return -1;
-    }
-    memcpy(crypt_ftr.keymaster_blob, &keymaster_blob[0],
-           crypt_ftr.keymaster_blob_size);
-    std::string scrypted_intermediate_key = props.Get<std::string>(tag::scrypted_intermediate_key);
-    if (scrypted_intermediate_key.size() != SCRYPT_LEN) {
-        SLOGE("scrypted intermediate key wrong length");
-        return -1;
-    }
-    memcpy(crypt_ftr.scrypted_intermediate_key, &scrypted_intermediate_key[0],
-           SCRYPT_LEN);
-
-    return 0;
-}
-
-static UnencryptedProperties GetProps(const char* path)
-{
-    return UnencryptedProperties(path);
-}
-
-int e4crypt_enable(const char* path)
-{
-    // Already enabled?
-    if (s_enabled) {
-        return 0;
-    }
-
-    // Not an encryptable device?
-    UnencryptedProperties key_props = GetProps(path).GetChild(properties::key);
-    if (!key_props.OK()) {
-        return 0;
-    }
-
-    if (key_props.Get<std::string>(tag::master_key).empty()) {
-        crypt_mnt_ftr ftr;
-        if (cryptfs_create_default_ftr(&ftr, key_length)) {
-            SLOGE("Failed to create crypto footer");
-            return -1;
-        }
-
-        // Scrub fields not used by ext4enc
-        ftr.persist_data_offset[0] = 0;
-        ftr.persist_data_offset[1] = 0;
-        ftr.persist_data_size = 0;
-
-        if (put_crypt_ftr_and_key(ftr, key_props)) {
-            SLOGE("Failed to write crypto footer");
-            return -1;
-        }
-
-        crypt_mnt_ftr ftr2;
-        if (get_crypt_ftr_and_key(ftr2, key_props)) {
-            SLOGE("Failed to read crypto footer back");
-            return -1;
-        }
-
-        if (memcmp(&ftr, &ftr2, sizeof(ftr)) != 0) {
-            SLOGE("Crypto footer not correctly written");
-            return -1;
-        }
-    }
-
-    if (!UnencryptedProperties(path).Remove(properties::ref)) {
-        SLOGE("Failed to remove key ref");
-        return -1;
-    }
-
-    return e4crypt_check_passwd(path, "");
-}
-
+// TODO replace with proper function to test for file encryption
 int e4crypt_crypto_complete(const char* path)
 {
-    SLOGI("ext4 crypto complete called on %s", path);
-    auto key_props = GetProps(path).GetChild(properties::key);
-    if (key_props.Get<std::string>(tag::master_key).empty()) {
-        SLOGI("No master key, so not ext4enc");
-        return -1;
-    }
-
-    return 0;
+    return e4crypt_is_native() ? 0 : -1;
 }
 
 // Get raw keyref - used to make keyname and to pass to ioctl
@@ -290,53 +133,6 @@
     return std::string((char*)key_ref2, EXT4_KEY_DESCRIPTOR_SIZE);
 }
 
-static int e4crypt_check_passwd(const char* path, const char* password)
-{
-    SLOGI("e4crypt_check_password");
-    auto props = GetProps(path);
-    auto key_props = props.GetChild(properties::key);
-
-    crypt_mnt_ftr ftr;
-    if (get_crypt_ftr_and_key(ftr, key_props)) {
-        SLOGE("Failed to read crypto footer back");
-        return -1;
-    }
-
-    unsigned char master_key_bytes[key_length / 8];
-    if (cryptfs_get_master_key (&ftr, password, master_key_bytes)){
-        SLOGI("Incorrect password");
-        ftr.failed_decrypt_count++;
-        if (put_crypt_ftr_and_key(ftr, key_props)) {
-            SLOGW("Failed to update failed_decrypt_count");
-        }
-        return ftr.failed_decrypt_count;
-    }
-
-    if (ftr.failed_decrypt_count) {
-        ftr.failed_decrypt_count = 0;
-        if (put_crypt_ftr_and_key(ftr, key_props)) {
-            SLOGW("Failed to reset failed_decrypt_count");
-        }
-    }
-    std::string master_key(reinterpret_cast<char*>(master_key_bytes),
-                           sizeof(master_key_bytes));
-
-    std::string raw_ref;
-    if (!install_key(master_key, raw_ref)) {
-        return -1;
-    }
-    SLOGD("Installed master key");
-
-    // Save reference to key so we can set policy later
-    if (!props.Set(properties::ref, raw_ref)) {
-        SLOGE("Cannot save key reference");
-        return -1;
-    }
-
-    s_enabled = true;
-    return 0;
-}
-
 static ext4_encryption_key fill_key(const std::string &key)
 {
     // ext4enc:TODO Currently raw key is required to be of length
@@ -392,31 +188,6 @@
     return true;
 }
 
-int e4crypt_get_field(const char* path, const char* fieldname,
-                      char* value, size_t len)
-{
-    auto v = GetProps(path).GetChild(properties::props)
-      .Get<std::string>(fieldname);
-
-    if (v == "") {
-        return CRYPTO_GETFIELD_ERROR_NO_FIELD;
-    }
-
-    if (v.length() >= len) {
-        return CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
-    }
-
-    strlcpy(value, v.c_str(), len);
-    return 0;
-}
-
-int e4crypt_set_field(const char* path, const char* fieldname,
-                      const char* value)
-{
-    return GetProps(path).GetChild(properties::props)
-        .Set(fieldname, std::string(value)) ? 0 : -1;
-}
-
 static std::string get_de_key_path(userid_t user_id) {
     return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
 }
@@ -519,8 +290,8 @@
     return true;
 }
 
-static bool set_policy(const std::string &raw_ref, const std::string& path) {
-    if (do_policy_set(path.c_str(), raw_ref.data(), raw_ref.size()) != 0) {
+static bool ensure_policy(const std::string &raw_ref, const std::string& path) {
+    if (e4crypt_policy_ensure(path.c_str(), raw_ref.data(), raw_ref.size()) != 0) {
         LOG(ERROR) << "Failed to set policy on: " << path;
         return false;
     }
@@ -569,6 +340,52 @@
     return true;
 }
 
+int e4crypt_enable(const char* path)
+{
+    LOG(INFO) << "e4crypt_enable";
+
+    if (s_enabled) {
+        LOG(INFO) << "Already enabled";
+        return 0;
+    }
+
+    std::string device_key;
+    std::string device_key_path = std::string(path) + device_key_leaf;
+    if (!android::vold::retrieveKey(device_key_path, device_key)) {
+        LOG(INFO) << "Creating new key";
+        if (!random_key(device_key)) {
+            return -1;
+        }
+
+        std::string key_temp = std::string(path) + device_key_temp;
+        if (path_exists(key_temp)) {
+            android::vold::destroyKey(key_temp);
+        }
+
+        if (!android::vold::storeKey(key_temp, device_key)) return false;
+        if (rename(key_temp.c_str(), device_key_path.c_str()) != 0) {
+            PLOG(ERROR) << "Unable to move new key to location: "
+                        << device_key_path;
+            return false;
+        }
+    }
+
+    std::string device_key_ref;
+    if (!install_key(device_key, device_key_ref)) {
+        LOG(ERROR) << "Failed to install device key";
+        return -1;
+    }
+
+    std::string ref_filename = std::string("/data") + e4crypt_key_ref;
+    if (!android::base::WriteStringToFile(device_key_ref, ref_filename)) {
+        PLOG(ERROR) << "Cannot save key reference";
+        return -1;
+    }
+
+    s_enabled = true;
+    return 0;
+}
+
 int e4crypt_init_user0() {
     LOG(DEBUG) << "e4crypt_init_user0";
     if (e4crypt_is_native()) {
@@ -586,13 +403,17 @@
             }
             if (!create_and_install_user_keys(0, false)) return -1;
         }
+        // TODO: switch to loading only DE_0 here once framework makes
+        // explicit calls to install DE keys for secondary users
         if (!load_all_de_keys()) return -1;
     }
-    // Ignore failures. FIXME this is horrid
-    // FIXME: we need an idempotent policy-setting call, which simply verifies the
-    // policy is already set on a second run, even if the directory is nonempty.
-    // Then we need to call it all the time.
-    e4crypt_prepare_user_storage(nullptr, 0, 0, false);
+    // We can only safely prepare DE storage here, since CE keys are probably
+    // entangled with user credentials.  The framework will always prepare CE
+    // storage once CE keys are installed.
+    if (e4crypt_prepare_user_storage(nullptr, 0, 0, FLAG_STORAGE_DE) != 0) {
+        LOG(ERROR) << "Failed to prepare user 0 storage";
+        return -1;
+    }
     return 0;
 }
 
@@ -675,6 +496,7 @@
     return 0;
 }
 
+// TODO: rename to 'install' for consistency, and take flags to know which keys to install
 int e4crypt_unlock_user_key(userid_t user_id, int serial, const char* token) {
     LOG(DEBUG) << "e4crypt_unlock_user_key " << user_id << " " << (token != nullptr);
     if (e4crypt_is_native()) {
@@ -696,6 +518,7 @@
     return 0;
 }
 
+// TODO: rename to 'evict' for consistency
 int e4crypt_lock_user_key(userid_t user_id) {
     if (e4crypt_is_native()) {
         // TODO: remove from kernel keyring
@@ -712,35 +535,48 @@
     return 0;
 }
 
-int e4crypt_prepare_user_storage(const char* volume_uuid,
-                                 userid_t user_id,
-                                 int serial,
-                                 bool ephemeral) {
-    if (volume_uuid) {
-        LOG(DEBUG) << "e4crypt_prepare_user_storage " << volume_uuid << " " << user_id;
-    } else {
-        LOG(DEBUG) << "e4crypt_prepare_user_storage, null volume " << user_id;
+int e4crypt_prepare_user_storage(const char* volume_uuid, userid_t user_id,
+        int serial, int flags) {
+    LOG(DEBUG) << "e4crypt_prepare_user_storage for volume " << escape_null(volume_uuid)
+            << ", user " << user_id << ", serial " << serial << ", flags " << flags;
+
+    if (flags & FLAG_STORAGE_DE) {
+        auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
+        auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
+        auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
+
+        if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return -1;
+        if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return -1;
+        if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return -1;
+
+        if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
+            std::string de_raw_ref;
+            if (!lookup_key_ref(s_de_key_raw_refs, user_id, de_raw_ref)) return -1;
+            if (!ensure_policy(de_raw_ref, system_de_path)) return -1;
+            if (!ensure_policy(de_raw_ref, misc_de_path)) return -1;
+            if (!ensure_policy(de_raw_ref, user_de_path)) return -1;
+        }
     }
-    auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
-    auto media_ce_path = android::vold::BuildDataMediaPath(volume_uuid, user_id);
-    auto user_ce_path = android::vold::BuildDataUserPath(volume_uuid, user_id);
-    auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
 
-    // FIXME: should this be 0770 or 0700?
-    if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return -1;
-    if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return -1;
-    if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return -1;
-    if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return -1;
+    if (flags & FLAG_STORAGE_CE) {
+        auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
+        auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
+        auto media_ce_path = android::vold::BuildDataMediaPath(volume_uuid, user_id);
+        auto user_ce_path = android::vold::BuildDataUserPath(volume_uuid, user_id);
 
-    if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
-        std::string ce_raw_ref, de_raw_ref;
-        if (!lookup_key_ref(s_ce_key_raw_refs, user_id, ce_raw_ref)) return -1;
-        if (!lookup_key_ref(s_de_key_raw_refs, user_id, de_raw_ref)) return -1;
-        if (!set_policy(ce_raw_ref, system_ce_path)) return -1;
-        if (!set_policy(ce_raw_ref, media_ce_path)) return -1;
-        if (!set_policy(ce_raw_ref, user_ce_path)) return -1;
-        if (!set_policy(de_raw_ref, user_de_path)) return -1;
-        // FIXME I thought there were more DE directories than this
+        if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return -1;
+        if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return -1;
+        if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return -1;
+        if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return -1;
+
+        if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
+            std::string ce_raw_ref;
+            if (!lookup_key_ref(s_ce_key_raw_refs, user_id, ce_raw_ref)) return -1;
+            if (!ensure_policy(ce_raw_ref, system_ce_path)) return -1;
+            if (!ensure_policy(ce_raw_ref, misc_ce_path)) return -1;
+            if (!ensure_policy(ce_raw_ref, media_ce_path)) return -1;
+            if (!ensure_policy(ce_raw_ref, user_ce_path)) return -1;
+        }
     }
 
     return 0;
diff --git a/Ext4Crypt.h b/Ext4Crypt.h
index 50378a9..d732e86 100644
--- a/Ext4Crypt.h
+++ b/Ext4Crypt.h
@@ -24,10 +24,6 @@
 // General functions
 int e4crypt_enable(const char* path);
 int e4crypt_crypto_complete(const char* path);
-int e4crypt_get_field(const char* path, const char* fieldname,
-                      char* value, size_t len);
-int e4crypt_set_field(const char* path, const char* fieldname,
-                      const char* value);
 
 int e4crypt_init_user0();
 int e4crypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral);
@@ -36,9 +32,7 @@
 int e4crypt_unlock_user_key(userid_t user_id, int serial, const char* token);
 int e4crypt_lock_user_key(userid_t user_id);
 
-int e4crypt_prepare_user_storage(const char* volume_uuid,
-                                 userid_t user_id,
-                                 int serial,
-                                 bool ephemeral);
+int e4crypt_prepare_user_storage(const char* volume_uuid, userid_t user_id,
+        int serial, int flags);
 
 __END_DECLS
diff --git a/Utils.cpp b/Utils.cpp
index 9f0e0f3..1d1b236 100644
--- a/Utils.cpp
+++ b/Utils.cpp
@@ -561,9 +561,19 @@
 }
 
 std::string BuildDataSystemCePath(userid_t userId) {
-    // TODO: unify with installd path generation logic
-    std::string data(BuildDataPath(nullptr));
-    return StringPrintf("%s/system_ce/%u", data.c_str(), userId);
+    return StringPrintf("%s/system_ce/%u", BuildDataPath(nullptr).c_str(), userId);
+}
+
+std::string BuildDataSystemDePath(userid_t userId) {
+    return StringPrintf("%s/system_de/%u", BuildDataPath(nullptr).c_str(), userId);
+}
+
+std::string BuildDataMiscCePath(userid_t userId) {
+    return StringPrintf("%s/misc_ce/%u", BuildDataPath(nullptr).c_str(), userId);
+}
+
+std::string BuildDataMiscDePath(userid_t userId) {
+    return StringPrintf("%s/misc_de/%u", BuildDataPath(nullptr).c_str(), userId);
 }
 
 std::string BuildDataPath(const char* volumeUuid) {
diff --git a/Utils.h b/Utils.h
index f717da5..7ff92c8 100644
--- a/Utils.h
+++ b/Utils.h
@@ -97,6 +97,9 @@
 std::string BuildKeyPath(const std::string& partGuid);
 
 std::string BuildDataSystemCePath(userid_t userid);
+std::string BuildDataSystemDePath(userid_t userid);
+std::string BuildDataMiscCePath(userid_t userid);
+std::string BuildDataMiscDePath(userid_t userid);
 
 std::string BuildDataPath(const char* volumeUuid);
 std::string BuildDataMediaPath(const char* volumeUuid, userid_t userid);
diff --git a/cryptfs.c b/cryptfs.c
index bd7dd46..bd5807f 100644
--- a/cryptfs.c
+++ b/cryptfs.c
@@ -1582,6 +1582,9 @@
 {
     int i;
 
+    // NOTE: post_fs_data results in init calling back around to vold, so all
+    // callers to this method must be async
+
     /* Do the prep of the /data filesystem */
     property_set("vold.post_fs_data_done", "0");
     property_set("vold.decrypt", "trigger_post_fs_data");
@@ -3573,7 +3576,8 @@
 int cryptfs_getfield(const char *fieldname, char *value, int len)
 {
     if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
-        return e4crypt_get_field(DATA_MNT_POINT, fieldname, value, len);
+        SLOGE("Cannot get field when file encrypted");
+        return -1;
     }
 
     char temp_value[PROPERTY_VALUE_MAX];
@@ -3638,7 +3642,8 @@
 int cryptfs_setfield(const char *fieldname, const char *value)
 {
     if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
-        return e4crypt_set_field(DATA_MNT_POINT, fieldname, value);
+        SLOGE("Cannot set field when file encrypted");
+        return -1;
     }
 
     char encrypted_state[PROPERTY_VALUE_MAX];
diff --git a/vdc.cpp b/vdc.cpp
index d8476b7..4eb26cd 100644
--- a/vdc.cpp
+++ b/vdc.cpp
@@ -22,6 +22,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <stdlib.h>
+#include <poll.h>
 
 #include <sys/socket.h>
 #include <sys/select.h>
@@ -29,6 +30,8 @@
 #include <sys/types.h>
 #include <sys/un.h>
 
+#include <android-base/stringprintf.h>
+
 #include <cutils/sockets.h>
 #include <private/android_filesystem_config.h>
 
@@ -36,6 +39,8 @@
 static int do_monitor(int sock, int stop_after_cmd);
 static int do_cmd(int sock, int argc, char **argv);
 
+static constexpr int kCommandTimeoutMs = 20 * 1000;
+
 int main(int argc, char **argv) {
     int sock;
     int wait_for_socket;
@@ -44,12 +49,12 @@
     progname = argv[0];
 
     wait_for_socket = argc > 1 && strcmp(argv[1], "--wait") == 0;
-    if(wait_for_socket) {
+    if (wait_for_socket) {
         argv++;
         argc--;
     }
 
-    if(argc < 2) {
+    if (argc < 2) {
         usage(progname);
         exit(5);
     }
@@ -62,8 +67,8 @@
     while ((sock = socket_local_client(sockname,
                                  ANDROID_SOCKET_NAMESPACE_RESERVED,
                                  SOCK_STREAM)) < 0) {
-        if(!wait_for_socket) {
-            fprintf(stderr, "Error connecting (%s)\n", strerror(errno));
+        if (!wait_for_socket) {
+            fprintf(stdout, "Error connecting to %s: %s\n", sockname, strerror(errno));
             exit(4);
         } else {
             usleep(10000);
@@ -78,97 +83,92 @@
 }
 
 static int do_cmd(int sock, int argc, char **argv) {
-    char final_cmd[255] = "0 "; /* 0 is a (now required) sequence number */
+    int seq = getpid();
 
-    int i;
-    size_t ret;
+    std::string cmd(android::base::StringPrintf("%d ", seq));
+    for (int i = 1; i < argc; i++) {
+        if (!strchr(argv[i], ' ')) {
+            cmd.append(argv[i]);
+        } else {
+            cmd.push_back('\"');
+            cmd.append(argv[i]);
+            cmd.push_back('\"');
+        }
 
-    for (i = 1; i < argc; i++) {
-        char *cmp;
-
-        if (!strchr(argv[i], ' '))
-            asprintf(&cmp, "%s%s", argv[i], (i == (argc -1)) ? "" : " ");
-        else
-            asprintf(&cmp, "\"%s\"%s", argv[i], (i == (argc -1)) ? "" : " ");
-
-        ret = strlcat(final_cmd, cmp, sizeof(final_cmd));
-        if (ret >= sizeof(final_cmd))
-            abort();
-        free(cmp);
+        if (i < argc - 1) {
+            cmd.push_back(' ');
+        }
     }
 
-    if (write(sock, final_cmd, strlen(final_cmd) + 1) < 0) {
-        perror("write");
+    if (TEMP_FAILURE_RETRY(write(sock, cmd.c_str(), cmd.length() + 1)) < 0) {
+        fprintf(stderr, "Failed to write command: %s\n", strerror(errno));
         return errno;
     }
 
-    return do_monitor(sock, 1);
+    return do_monitor(sock, seq);
 }
 
-static int do_monitor(int sock, int stop_after_cmd) {
-    char *buffer = (char *) malloc(4096);
+static int do_monitor(int sock, int stop_after_seq) {
+    char buffer[4096];
+    int timeout = kCommandTimeoutMs;
 
-    if (!stop_after_cmd)
-        printf("[Connected to Vold]\n");
+    if (stop_after_seq == 0) {
+        fprintf(stderr, "Connected to vold\n");
+        timeout = -1;
+    }
 
-    while(1) {
-        fd_set read_fds;
-        struct timeval to;
-        int rc = 0;
-
-        to.tv_sec = 10;
-        to.tv_usec = 0;
-
-        FD_ZERO(&read_fds);
-        FD_SET(sock, &read_fds);
-
-        if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) {
-            fprintf(stderr, "Error in select (%s)\n", strerror(errno));
-            free(buffer);
-            return errno;
-        } else if (!rc) {
-            continue;
-            fprintf(stderr, "[TIMEOUT]\n");
+    while (1) {
+        struct pollfd poll_sock = { sock, POLLIN, 0 };
+        int rc = TEMP_FAILURE_RETRY(poll(&poll_sock, 1, timeout));
+        if (rc == 0) {
+            fprintf(stderr, "Timeout waiting for %d\n", stop_after_seq);
             return ETIMEDOUT;
-        } else if (FD_ISSET(sock, &read_fds)) {
-            memset(buffer, 0, 4096);
-            if ((rc = read(sock, buffer, 4096)) <= 0) {
-                if (rc == 0)
-                    fprintf(stderr, "Lost connection to Vold - did it crash?\n");
-                else
-                    fprintf(stderr, "Error reading data (%s)\n", strerror(errno));
-                free(buffer);
-                if (rc == 0)
-                    return ECONNRESET;
-                return errno;
-            }
+        } else if (rc < 0) {
+            fprintf(stderr, "Failed during poll: %s\n", strerror(errno));
+            return errno;
+        }
 
-            int offset = 0;
-            int i = 0;
+        if (!(poll_sock.revents & POLLIN)) {
+            fprintf(stderr, "No data; trying again\n");
+            continue;
+        }
 
-            for (i = 0; i < rc; i++) {
-                if (buffer[i] == '\0') {
-                    int code;
-                    char tmp[4];
+        memset(buffer, 0, sizeof(buffer));
+        rc = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
+        if (rc == 0) {
+            fprintf(stderr, "Lost connection, did vold crash?\n");
+            return ECONNRESET;
+        } else if (rc < 0) {
+            fprintf(stderr, "Error reading data: %s\n", strerror(errno));
+            return errno;
+        }
 
-                    strlcpy(tmp, buffer + offset, sizeof(tmp));
-                    code = atoi(tmp);
+        int offset = 0;
+        for (int i = 0; i < rc; i++) {
+            if (buffer[i] == '\0') {
+                char* res = buffer + offset;
+                fprintf(stdout, "%s\n", res);
 
-                    printf("%s\n", buffer + offset);
-                    if (stop_after_cmd) {
-                        if (code >= 200 && code < 600)
+                int code = atoi(strtok(res, " "));
+                if (code >= 200 && code < 600) {
+                    int seq = atoi(strtok(nullptr, " "));
+                    if (seq == stop_after_seq) {
+                        if (code == 200) {
                             return 0;
+                        } else {
+                            return code;
+                        }
                     }
-                    offset = i + 1;
                 }
+
+                offset = i + 1;
             }
         }
     }
-    free(buffer);
-    return 0;
+    return EIO;
 }
 
 static void usage(char *progname) {
     fprintf(stderr,
             "Usage: %s [--wait] <monitor>|<cmd> [arg1] [arg2...]\n", progname);
- }
+}