ext4_crypt_init_extensions.cpp revision 377cd1957172c19ee21baa3d6bda0840f1ce020d
1#define TAG "ext4_utils" 2 3#include "ext4_crypt_init_extensions.h" 4 5#include <string> 6 7#include <dirent.h> 8#include <errno.h> 9#include <sys/mount.h> 10#include <sys/stat.h> 11 12#include <cutils/klog.h> 13#include <cutils/properties.h> 14#include <cutils/sockets.h> 15#include <poll.h> 16 17#include "key_control.h" 18#include "unencrypted_properties.h" 19 20static const std::string arbitrary_sequence_number = "42"; 21static const int vold_command_timeout_ms = 60 * 1000; 22 23static std::string vold_command(std::string const& command) 24{ 25 KLOG_INFO(TAG, "Running command %s\n", command.c_str()); 26 int sock = -1; 27 28 while (true) { 29 sock = socket_local_client("vold", 30 ANDROID_SOCKET_NAMESPACE_RESERVED, 31 SOCK_STREAM); 32 if (sock >= 0) { 33 break; 34 } 35 usleep(10000); 36 } 37 38 if (sock < 0) { 39 KLOG_INFO(TAG, "Cannot open vold, failing command\n"); 40 return ""; 41 } 42 43 class CloseSocket 44 { 45 int sock_; 46 public: 47 CloseSocket(int sock) : sock_(sock) {} 48 ~CloseSocket() { close(sock_); } 49 }; 50 51 CloseSocket cs(sock); 52 53 // Use arbitrary sequence number. This should only be used when the 54 // framework is down, so this is (mostly) OK. 55 std::string actual_command = arbitrary_sequence_number + " " + command; 56 if (write(sock, actual_command.c_str(), actual_command.size() + 1) < 0) { 57 KLOG_ERROR(TAG, "Cannot write command\n"); 58 return ""; 59 } 60 61 struct pollfd poll_sock = {sock, POLLIN, 0}; 62 63 int rc = poll(&poll_sock, 1, vold_command_timeout_ms); 64 if (rc < 0) { 65 KLOG_ERROR(TAG, "Error in poll %s\n", strerror(errno)); 66 return ""; 67 } 68 if (!(poll_sock.revents & POLLIN)) { 69 KLOG_ERROR(TAG, "Timeout\n"); 70 return ""; 71 } 72 char buffer[4096]; 73 memset(buffer, 0, sizeof(buffer)); 74 rc = read(sock, buffer, sizeof(buffer)); 75 if (rc <= 0) { 76 if (rc == 0) { 77 KLOG_ERROR(TAG, "Lost connection to Vold - did it crash?\n"); 78 } else { 79 KLOG_ERROR(TAG, "Error reading data (%s)\n", strerror(errno)); 80 } 81 return ""; 82 } 83 84 // We don't truly know that this is the correct result. However, 85 // since this will only be used when the framework is down, 86 // it should be OK unless someone is running vdc at the same time. 87 // Worst case we force a reboot in the very rare synchronization 88 // error 89 return std::string(buffer, rc); 90} 91 92int e4crypt_create_device_key(const char* dir, 93 int ensure_dir_exists(const char*)) 94{ 95 // Already encrypted with password? If so bail 96 std::string temp_folder = std::string() + dir + "/tmp_mnt"; 97 DIR* temp_dir = opendir(temp_folder.c_str()); 98 if (temp_dir) { 99 closedir(temp_dir); 100 return 0; 101 } 102 103 // Make sure folder exists. Use make_dir to set selinux permissions. 104 KLOG_INFO(TAG, "Creating test device key\n"); 105 if (ensure_dir_exists(UnencryptedProperties::GetPath(dir).c_str())) { 106 KLOG_ERROR(TAG, "Failed to create %s with error %s\n", 107 UnencryptedProperties::GetPath(dir).c_str(), 108 strerror(errno)); 109 return -1; 110 } 111 112 auto result = vold_command("cryptfs enablefilecrypto"); 113 // ext4enc:TODO proper error handling 114 KLOG_INFO(TAG, "enablefilecrypto returned with result %s\n", 115 result.c_str()); 116 117 return 0; 118} 119 120int e4crypt_install_keyring() 121{ 122 key_serial_t device_keyring = add_key("keyring", "e4crypt", 0, 0, 123 KEY_SPEC_SESSION_KEYRING); 124 125 if (device_keyring == -1) { 126 KLOG_ERROR(TAG, "Failed to create keyring\n"); 127 return -1; 128 } 129 130 KLOG_INFO(TAG, "Keyring created wth id %d in process %d\n", 131 device_keyring, getpid()); 132 133 // ext4enc:TODO set correct permissions 134 long result = keyctl_setperm(device_keyring, 0x3f3f3f3f); 135 if (result) { 136 KLOG_ERROR(TAG, "KEYCTL_SETPERM failed with error %ld\n", result); 137 return -1; 138 } 139 140 return 0; 141} 142 143int e4crypt_set_directory_policy(const char* dir) 144{ 145 // Only set policy on first level /data directories 146 // To make this less restrictive, consider using a policy file. 147 // However this is overkill for as long as the policy is simply 148 // to apply a global policy to all /data folders created via makedir 149 if (!dir || strncmp(dir, "/data/", 6) || strchr(dir + 6, '/')) { 150 return 0; 151 } 152 153 UnencryptedProperties props("/data"); 154 std::string policy = props.Get<std::string>(properties::ref); 155 KLOG_INFO(TAG, "Setting policy %s\n", policy.c_str()); 156 int result = do_policy_set(dir, policy.c_str()); 157 if (result) { 158 KLOG_ERROR(TAG, "Setting %s policy on %s failed!\n", 159 policy.c_str(), dir); 160 return -1; 161 } 162 163 return 0; 164} 165