1 #define TAG "ext4_utils"
3 #include "ext4_crypt_init_extensions.h"
10 #include <sys/mount.h>
14 #include <android-base/file.h>
16 #include <cutils/klog.h>
17 #include <cutils/properties.h>
18 #include <cutils/sockets.h>
21 #include "key_control.h"
23 static const std::string arbitrary_sequence_number = "42";
24 static const int vold_command_timeout_ms = 60 * 1000;
26 static std::string vold_command(std::string const& command)
28 KLOG_INFO(TAG, "Running command %s\n", command.c_str());
32 sock = socket_local_client("cryptd",
33 ANDROID_SOCKET_NAMESPACE_RESERVED,
42 KLOG_INFO(TAG, "Cannot open vold, failing command (%s)\n", strerror(errno));
50 CloseSocket(int sock) : sock_(sock) {}
51 ~CloseSocket() { close(sock_); }
56 // Use arbitrary sequence number. This should only be used when the
57 // framework is down, so this is (mostly) OK.
58 std::string actual_command = arbitrary_sequence_number + " " + command;
59 if (write(sock, actual_command.c_str(), actual_command.size() + 1) < 0) {
60 KLOG_ERROR(TAG, "Cannot write command (%s)\n", strerror(errno));
64 struct pollfd poll_sock = {sock, POLLIN, 0};
66 int rc = TEMP_FAILURE_RETRY(poll(&poll_sock, 1, vold_command_timeout_ms));
68 KLOG_ERROR(TAG, "Error in poll (%s)\n", strerror(errno));
72 if (!(poll_sock.revents & POLLIN)) {
73 KLOG_ERROR(TAG, "Timeout\n");
77 memset(buffer, 0, sizeof(buffer));
78 rc = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
81 KLOG_ERROR(TAG, "Lost connection to Vold - did it crash?\n");
83 KLOG_ERROR(TAG, "Error reading data (%s)\n", strerror(errno));
88 // We don't truly know that this is the correct result. However,
89 // since this will only be used when the framework is down,
90 // it should be OK unless someone is running vdc at the same time.
91 // Worst case we force a reboot in the very rare synchronization
93 return std::string(buffer, rc);
96 int e4crypt_create_device_key(const char* dir,
97 int ensure_dir_exists(const char*))
99 // Make sure folder exists. Use make_dir to set selinux permissions.
100 std::string unencrypted_dir = std::string(dir) + "/unencrypted";
101 if (ensure_dir_exists(unencrypted_dir.c_str())) {
102 KLOG_ERROR(TAG, "Failed to create %s (%s)\n",
103 unencrypted_dir.c_str(),
108 auto result = vold_command("cryptfs enablefilecrypto");
109 // ext4enc:TODO proper error handling
110 KLOG_INFO(TAG, "enablefilecrypto returned with result %s\n",
116 int e4crypt_install_keyring()
118 key_serial_t device_keyring = add_key("keyring", "e4crypt", 0, 0,
119 KEY_SPEC_SESSION_KEYRING);
121 if (device_keyring == -1) {
122 KLOG_ERROR(TAG, "Failed to create keyring (%s)\n", strerror(errno));
126 KLOG_INFO(TAG, "Keyring created wth id %d in process %d\n",
127 device_keyring, getpid());
132 int e4crypt_do_init_user0()
134 auto result = vold_command("cryptfs init_user0");
135 // ext4enc:TODO proper error handling
136 KLOG_INFO(TAG, "init_user0 returned with result %s\n",
141 int e4crypt_set_directory_policy(const char* dir)
143 // Only set policy on first level /data directories
144 // To make this less restrictive, consider using a policy file.
145 // However this is overkill for as long as the policy is simply
146 // to apply a global policy to all /data folders created via makedir
147 if (!dir || strncmp(dir, "/data/", 6) || strchr(dir + 6, '/')) {
151 // Special case various directories that must not be encrypted,
152 // often because their subdirectories must be encrypted.
153 // This isn't a nice way to do this, see b/26641735
154 std::vector<std::string> directories_to_exclude = {
155 "lost+found", "user", "system_ce", "media", "data", "user_de",
156 // ext4enc:TODO workaround that must be removed b/26673855
159 std::string prefix = "/data/";
160 for (auto d: directories_to_exclude) {
161 if ((prefix + d) == dir) {
162 KLOG_INFO(TAG, "Not setting policy on %s\n", dir);
167 std::string ref_filename = std::string("/data") + e4crypt_key_ref;
169 if (!android::base::ReadFileToString(ref_filename, &policy)) {
170 KLOG_INFO(TAG, "Not file encrypted so no policy for %s\n", dir);
174 KLOG_INFO(TAG, "Setting policy on %s\n", dir);
175 int result = do_policy_set(dir, policy.c_str(), policy.size());
177 KLOG_ERROR(TAG, "Setting %02x%02x%02x%02x policy on %s failed!\n",
178 policy[0], policy[1], policy[2], policy[3], dir);