ext4_crypt_init_extensions.cpp revision 59ffd6d9c7dd3ddaa036956d89c3e4d416769bf6
1#define TAG "ext4_utils" 2 3#include "ext4_crypt_init_extensions.h" 4 5#include <string> 6#include <vector> 7 8#include <dirent.h> 9#include <errno.h> 10#include <sys/mount.h> 11#include <sys/stat.h> 12#include <unistd.h> 13 14#include <android-base/file.h> 15 16#include <cutils/klog.h> 17#include <cutils/properties.h> 18#include <cutils/sockets.h> 19#include <poll.h> 20 21#include "key_control.h" 22 23static const std::string arbitrary_sequence_number = "42"; 24static const int vold_command_timeout_ms = 60 * 1000; 25 26static std::string vold_command(std::string const& command) 27{ 28 KLOG_INFO(TAG, "Running command %s\n", command.c_str()); 29 int sock = -1; 30 31 while (true) { 32 sock = socket_local_client("cryptd", 33 ANDROID_SOCKET_NAMESPACE_RESERVED, 34 SOCK_STREAM); 35 if (sock >= 0) { 36 break; 37 } 38 usleep(10000); 39 } 40 41 if (sock < 0) { 42 KLOG_INFO(TAG, "Cannot open vold, failing command (%s)\n", strerror(errno)); 43 return ""; 44 } 45 46 class CloseSocket 47 { 48 int sock_; 49 public: 50 CloseSocket(int sock) : sock_(sock) {} 51 ~CloseSocket() { close(sock_); } 52 }; 53 54 CloseSocket cs(sock); 55 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)); 61 return ""; 62 } 63 64 struct pollfd poll_sock = {sock, POLLIN, 0}; 65 66 int rc = TEMP_FAILURE_RETRY(poll(&poll_sock, 1, vold_command_timeout_ms)); 67 if (rc < 0) { 68 KLOG_ERROR(TAG, "Error in poll (%s)\n", strerror(errno)); 69 return ""; 70 } 71 72 if (!(poll_sock.revents & POLLIN)) { 73 KLOG_ERROR(TAG, "Timeout\n"); 74 return ""; 75 } 76 char buffer[4096]; 77 memset(buffer, 0, sizeof(buffer)); 78 rc = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer))); 79 if (rc <= 0) { 80 if (rc == 0) { 81 KLOG_ERROR(TAG, "Lost connection to Vold - did it crash?\n"); 82 } else { 83 KLOG_ERROR(TAG, "Error reading data (%s)\n", strerror(errno)); 84 } 85 return ""; 86 } 87 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 92 // error 93 return std::string(buffer, rc); 94} 95 96int e4crypt_create_device_key(const char* dir, 97 int ensure_dir_exists(const char*)) 98{ 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(), 104 strerror(errno)); 105 return -1; 106 } 107 108 auto result = vold_command("cryptfs enablefilecrypto"); 109 // ext4enc:TODO proper error handling 110 KLOG_INFO(TAG, "enablefilecrypto returned with result %s\n", 111 result.c_str()); 112 113 return 0; 114} 115 116int e4crypt_install_keyring() 117{ 118 key_serial_t device_keyring = add_key("keyring", "e4crypt", 0, 0, 119 KEY_SPEC_SESSION_KEYRING); 120 121 if (device_keyring == -1) { 122 KLOG_ERROR(TAG, "Failed to create keyring (%s)\n", strerror(errno)); 123 return -1; 124 } 125 126 KLOG_INFO(TAG, "Keyring created wth id %d in process %d\n", 127 device_keyring, getpid()); 128 129 return 0; 130} 131 132int e4crypt_do_init_user0() 133{ 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", 137 result.c_str()); 138 return 0; 139} 140 141int e4crypt_set_directory_policy(const char* dir) 142{ 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, '/')) { 148 return 0; 149 } 150 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 157 "misc", 158 }; 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); 163 return 0; 164 } 165 } 166 167 std::string ref_filename = std::string("/data") + e4crypt_key_ref; 168 std::string policy; 169 if (!android::base::ReadFileToString(ref_filename, &policy)) { 170 KLOG_INFO(TAG, "Not file encrypted so no policy for %s\n", dir); 171 return 0; 172 } 173 174 KLOG_INFO(TAG, "Setting policy on %s\n", dir); 175 int result = do_policy_set(dir, policy.c_str(), policy.size()); 176 if (result) { 177 KLOG_ERROR(TAG, "Setting %02x%02x%02x%02x policy on %s failed!\n", 178 policy[0], policy[1], policy[2], policy[3], dir); 179 return -1; 180 } 181 182 return 0; 183} 184