1 #define TAG "ext4_utils"
3 #include "ext4_crypt_init_extensions.h"
12 #include <cutils/klog.h>
13 #include <cutils/properties.h>
14 #include <cutils/sockets.h>
17 #include "key_control.h"
18 #include "unencrypted_properties.h"
20 static const std::string arbitrary_sequence_number = "42";
21 static const int vold_command_timeout_ms = 60 * 1000;
23 static std::string vold_command(std::string const& command)
25 KLOG_INFO(TAG, "Running command %s\n", command.c_str());
28 #ifdef TARGET_USES_LOGD
30 sock = socket_local_client("cryptd",
31 ANDROID_SOCKET_NAMESPACE_RESERVED,
41 KLOG_INFO(TAG, "Cannot open vold, failing command (%s)\n", strerror(errno));
49 CloseSocket(int sock) : sock_(sock) {}
50 ~CloseSocket() { close(sock_); }
55 // Use arbitrary sequence number. This should only be used when the
56 // framework is down, so this is (mostly) OK.
57 std::string actual_command = arbitrary_sequence_number + " " + command;
58 if (write(sock, actual_command.c_str(), actual_command.size() + 1) < 0) {
59 KLOG_ERROR(TAG, "Cannot write command (%s)\n", strerror(errno));
63 struct pollfd poll_sock = {sock, POLLIN, 0};
65 int rc = TEMP_FAILURE_RETRY(poll(&poll_sock, 1, vold_command_timeout_ms));
67 KLOG_ERROR(TAG, "Error in poll (%s)\n", strerror(errno));
71 if (!(poll_sock.revents & POLLIN)) {
72 KLOG_ERROR(TAG, "Timeout\n");
76 memset(buffer, 0, sizeof(buffer));
77 rc = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
80 KLOG_ERROR(TAG, "Lost connection to Vold - did it crash?\n");
82 KLOG_ERROR(TAG, "Error reading data (%s)\n", strerror(errno));
87 // We don't truly know that this is the correct result. However,
88 // since this will only be used when the framework is down,
89 // it should be OK unless someone is running vdc at the same time.
90 // Worst case we force a reboot in the very rare synchronization
92 return std::string(buffer, rc);
95 int e4crypt_create_device_key(const char* dir,
96 int ensure_dir_exists(const char*))
98 // Already encrypted with password? If so bail
99 std::string temp_folder = std::string() + dir + "/tmp_mnt";
100 DIR* temp_dir = opendir(temp_folder.c_str());
106 // Make sure folder exists. Use make_dir to set selinux permissions.
107 if (ensure_dir_exists(UnencryptedProperties::GetPath(dir).c_str())) {
108 KLOG_ERROR(TAG, "Failed to create %s (%s)\n",
109 UnencryptedProperties::GetPath(dir).c_str(),
114 auto result = vold_command("cryptfs enablefilecrypto");
115 // ext4enc:TODO proper error handling
116 KLOG_INFO(TAG, "enablefilecrypto returned with result %s\n",
122 int e4crypt_install_keyring()
124 key_serial_t device_keyring = add_key("keyring", "e4crypt", 0, 0,
125 KEY_SPEC_SESSION_KEYRING);
127 if (device_keyring == -1) {
128 KLOG_ERROR(TAG, "Failed to create keyring (%s)\n", strerror(errno));
132 KLOG_INFO(TAG, "Keyring created wth id %d in process %d\n",
133 device_keyring, getpid());
138 int e4crypt_set_directory_policy(const char* dir)
140 // Only set policy on first level /data directories
141 // To make this less restrictive, consider using a policy file.
142 // However this is overkill for as long as the policy is simply
143 // to apply a global policy to all /data folders created via makedir
144 if (!dir || strncmp(dir, "/data/", 6) || strchr(dir + 6, '/')) {
148 // Don't encrypt lost+found - ext4 doesn't like it
149 if (!strcmp(dir, "/data/lost+found")) {
153 // ext4enc:TODO exclude /data/user with a horrible special case.
154 if (!strcmp(dir, "/data/user")) {
158 UnencryptedProperties props("/data");
159 std::string policy = props.Get<std::string>(properties::ref);
160 if (policy.empty()) {
161 // ext4enc:TODO why is this OK?
165 KLOG_INFO(TAG, "Setting policy on %s\n", dir);
166 int result = do_policy_set(dir, policy.c_str(), policy.size());
168 KLOG_ERROR(TAG, "Setting %02x%02x%02x%02x policy on %s failed!\n",
169 policy[0], policy[1], policy[2], policy[3], dir);
176 int e4crypt_set_user_crypto_policies(const char* dir)
178 auto command = std::string() + "cryptfs setusercryptopolicies " + dir;
179 auto result = vold_command(command);
180 // ext4enc:TODO proper error handling
181 KLOG_INFO(TAG, "setusercryptopolicies returned with result %s\n",