ext4_crypt_init_extensions.cpp revision 019954add8af22d618ae3b68529a3f941a17f2bf
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#include <unistd.h>
12
13#include <cutils/klog.h>
14#include <cutils/properties.h>
15#include <cutils/sockets.h>
16#include <poll.h>
17
18#include "key_control.h"
19#include "unencrypted_properties.h"
20
21static const std::string arbitrary_sequence_number = "42";
22static const int vold_command_timeout_ms = 60 * 1000;
23
24static std::string vold_command(std::string const& command)
25{
26    KLOG_INFO(TAG, "Running command %s\n", command.c_str());
27    int sock = -1;
28
29    while (true) {
30        sock = socket_local_client("vold",
31                                   ANDROID_SOCKET_NAMESPACE_RESERVED,
32                                   SOCK_STREAM);
33        if (sock >= 0) {
34            break;
35        }
36        usleep(10000);
37    }
38
39    if (sock < 0) {
40        KLOG_INFO(TAG, "Cannot open vold, failing command\n");
41        return "";
42    }
43
44    class CloseSocket
45    {
46        int sock_;
47    public:
48        CloseSocket(int sock) : sock_(sock) {}
49        ~CloseSocket() { close(sock_); }
50    };
51
52    CloseSocket cs(sock);
53
54    // Use arbitrary sequence number. This should only be used when the
55    // framework is down, so this is (mostly) OK.
56    std::string actual_command = arbitrary_sequence_number + " " + command;
57    if (write(sock, actual_command.c_str(), actual_command.size() + 1) < 0) {
58        KLOG_ERROR(TAG, "Cannot write command\n");
59        return "";
60    }
61
62    struct pollfd poll_sock = {sock, POLLIN, 0};
63
64    int rc = poll(&poll_sock, 1, vold_command_timeout_ms);
65    if (rc < 0) {
66        KLOG_ERROR(TAG, "Error in poll %s\n", strerror(errno));
67        return "";
68    }
69    if (!(poll_sock.revents & POLLIN)) {
70        KLOG_ERROR(TAG, "Timeout\n");
71        return "";
72    }
73    char buffer[4096];
74    memset(buffer, 0, sizeof(buffer));
75    rc = read(sock, buffer, sizeof(buffer));
76    if (rc <= 0) {
77        if (rc == 0) {
78            KLOG_ERROR(TAG, "Lost connection to Vold - did it crash?\n");
79        } else {
80            KLOG_ERROR(TAG, "Error reading data (%s)\n", strerror(errno));
81        }
82        return "";
83    }
84
85    // We don't truly know that this is the correct result. However,
86    // since this will only be used when the framework is down,
87    // it should be OK unless someone is running vdc at the same time.
88    // Worst case we force a reboot in the very rare synchronization
89    // error
90    return std::string(buffer, rc);
91}
92
93int e4crypt_create_device_key(const char* dir,
94                              int ensure_dir_exists(const char*))
95{
96    // Already encrypted with password? If so bail
97    std::string temp_folder = std::string() + dir + "/tmp_mnt";
98    DIR* temp_dir = opendir(temp_folder.c_str());
99    if (temp_dir) {
100        closedir(temp_dir);
101        return 0;
102    }
103
104    // Make sure folder exists. Use make_dir to set selinux permissions.
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    return 0;
134}
135
136int e4crypt_set_directory_policy(const char* dir)
137{
138    // Only set policy on first level /data directories
139    // To make this less restrictive, consider using a policy file.
140    // However this is overkill for as long as the policy is simply
141    // to apply a global policy to all /data folders created via makedir
142    if (!dir || strncmp(dir, "/data/", 6) || strchr(dir + 6, '/')) {
143        return 0;
144    }
145    // ext4enc:TODO exclude /data/user with a horrible special case.
146    if (!strcmp(dir, "/data/user")) {
147        return 0;
148    }
149
150    UnencryptedProperties props("/data");
151    std::string policy = props.Get<std::string>(properties::ref);
152    if (policy.empty()) {
153        // ext4enc:TODO why is this OK?
154        return 0;
155    }
156
157    KLOG_INFO(TAG, "Setting policy on %s\n", dir);
158    int result = do_policy_set(dir, policy.c_str(), policy.size());
159    if (result) {
160        KLOG_ERROR(TAG, "Setting %s policy on %s failed!\n",
161                   policy.c_str(), dir);
162        return -1;
163    }
164
165    return 0;
166}
167