2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
35 #define LOG_TAG "VoldCryptCmdListener"
37 #include <android-base/logging.h>
38 #include <android-base/stringprintf.h>
40 #include <cutils/fs.h>
41 #include <cutils/log.h>
42 #include <cutils/sockets.h>
44 #include <sysutils/SocketClient.h>
45 #include <private/android_filesystem_config.h>
47 #include "CryptCommandListener.h"
49 #include "ResponseCode.h"
51 #include "Ext4Crypt.h"
56 CryptCommandListener::CryptCommandListener() :
57 FrameworkListener("cryptd", true) {
58 registerCmd(new CryptfsCmd());
62 void CryptCommandListener::dumpArgs(int argc, char **argv, int argObscure) {
66 memset(buffer, 0, sizeof(buffer));
68 for (i = 0; i < argc; i++) {
69 unsigned int len = strlen(argv[i]) + 1; // Account for space
70 if (i == argObscure) {
71 len += 2; // Account for {}
73 if (((p - buffer) + len) < (sizeof(buffer)-1)) {
74 if (i == argObscure) {
90 void CryptCommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { }
93 int CryptCommandListener::sendGenericOkFailOnBool(SocketClient *cli, bool success) {
95 return cli->sendMsg(ResponseCode::CommandOkay, "Command succeeded", false);
97 return cli->sendMsg(ResponseCode::OperationFailed, "Command failed", false);
101 CryptCommandListener::CryptfsCmd::CryptfsCmd() :
102 VoldCommand("cryptfs") {
105 static int getType(const char* type)
107 if (!strcmp(type, "default")) {
108 return CRYPT_TYPE_DEFAULT;
109 } else if (!strcmp(type, "password")) {
110 return CRYPT_TYPE_PASSWORD;
111 } else if (!strcmp(type, "pin")) {
112 return CRYPT_TYPE_PIN;
113 } else if (!strcmp(type, "pattern")) {
114 return CRYPT_TYPE_PATTERN;
120 static char* parseNull(char* arg) {
121 if (strcmp(arg, "!") == 0) {
128 static bool check_argc(SocketClient *cli, const std::string &subcommand, int argc,
129 int expected, std::string usage) {
130 assert(expected >= 2);
132 assert(usage.empty());
134 assert(!usage.empty());
135 assert(std::count(usage.begin(), usage.end(), ' ') + 3 == expected);
137 if (argc == expected) {
140 auto message = std::string() + "Usage: cryptfs " + subcommand;
141 if (!usage.empty()) {
142 message += " " + usage;
144 cli->sendMsg(ResponseCode::CommandSyntaxError, message.c_str(), false);
148 static int do_enablecrypto(char* arg2, char* arg4, int type, bool no_ui) {
151 for (tries = 0; tries < 2; ++tries) {
152 if (type == CRYPT_TYPE_DEFAULT) {
153 rc = cryptfs_enable_default(arg2, no_ui);
155 rc = cryptfs_enable(arg2, type, arg4, no_ui);
162 } else if (tries == 0) {
163 Process::killProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
172 int CryptCommandListener::CryptfsCmd::runCommand(SocketClient *cli,
173 int argc, char **argv) {
174 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
175 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run cryptfs commands", false);
180 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing subcommand", false);
186 std::string subcommand(argv[1]);
187 if (subcommand == "checkpw") {
188 if (!check_argc(cli, subcommand, argc, 3, "<passwd>")) return 0;
189 dumpArgs(argc, argv, 2);
190 rc = cryptfs_check_passwd(argv[2]);
191 } else if (subcommand == "restart") {
192 if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
193 dumpArgs(argc, argv, -1);
195 // Spawn as thread so init can issue commands back to vold without
196 // causing deadlock, usually as a result of prep_data_fs.
197 std::thread(&cryptfs_restart).detach();
198 } else if (subcommand == "cryptocomplete") {
199 if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
200 dumpArgs(argc, argv, -1);
201 rc = cryptfs_crypto_complete();
202 } else if (subcommand == "enablecrypto") {
203 const char* syntax = "Usage: cryptfs enablecrypto <wipe|inplace> "
204 "default|password|pin|pattern [passwd] [noui]";
206 // This should be replaced with a command line parser if more options
210 int type = CRYPT_TYPE_DEFAULT;
211 int options = 4; // Optional parameters are at this offset
213 // Minimum 4 parameters
215 } else if (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace") ) {
216 // Second parameter must be wipe or inplace
219 // Third parameter must be valid type
220 type = getType(argv[3]);
223 } else if (type != CRYPT_TYPE_DEFAULT) {
230 // Too few parameters
232 } else if (argc == options) {
234 } else if (argc == options + 1) {
235 // One option, must be noui
236 if (!strcmp(argv[options], "noui")) {
248 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
252 dumpArgs(argc, argv, 4);
254 // Spawn as thread so init can issue commands back to vold without
255 // causing deadlock, usually as a result of prep_data_fs.
256 char* arg2 = argc > 2 ? strdup(argv[2]) : NULL;
257 char* arg4 = argc > 4 ? strdup(argv[4]) : NULL;
258 std::thread(&do_enablecrypto, arg2, arg4, type, no_ui).detach();
259 } else if (subcommand == "enablefilecrypto") {
260 if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
261 dumpArgs(argc, argv, -1);
262 rc = cryptfs_enable_file();
263 } else if (subcommand == "changepw") {
264 const char* syntax = "Usage: cryptfs changepw "
265 "default|password|pin|pattern [newpasswd]";
266 const char* password;
269 } else if (argc == 4) {
272 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
275 int type = getType(argv[2]);
277 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
280 SLOGD("cryptfs changepw %s {}", argv[2]);
281 rc = cryptfs_changepw(type, password);
282 } else if (subcommand == "verifypw") {
283 if (!check_argc(cli, subcommand, argc, 3, "<passwd>")) return 0;
284 SLOGD("cryptfs verifypw {}");
285 rc = cryptfs_verify_passwd(argv[2]);
286 } else if (subcommand == "getfield") {
287 if (!check_argc(cli, subcommand, argc, 3, "<fieldname>")) return 0;
289 int valbuf_len = PROPERTY_VALUE_MAX;
291 dumpArgs(argc, argv, -1);
293 // Increase the buffer size until it is big enough for the field value stored.
295 valbuf = (char*)malloc(valbuf_len);
296 if (valbuf == NULL) {
297 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", false);
300 rc = cryptfs_getfield(argv[2], valbuf, valbuf_len);
301 if (rc != CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL) {
307 if (rc == CRYPTO_GETFIELD_OK) {
308 cli->sendMsg(ResponseCode::CryptfsGetfieldResult, valbuf, false);
311 } else if (subcommand == "setfield") {
312 if (!check_argc(cli, subcommand, argc, 4, "<fieldname> <value>")) return 0;
313 dumpArgs(argc, argv, -1);
314 rc = cryptfs_setfield(argv[2], argv[3]);
315 } else if (subcommand == "mountdefaultencrypted") {
316 if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
317 SLOGD("cryptfs mountdefaultencrypted");
318 dumpArgs(argc, argv, -1);
320 // Spawn as thread so init can issue commands back to vold without
321 // causing deadlock, usually as a result of prep_data_fs.
322 std::thread(&cryptfs_mount_default_encrypted).detach();
323 } else if (subcommand == "getpwtype") {
324 if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
325 SLOGD("cryptfs getpwtype");
326 dumpArgs(argc, argv, -1);
327 switch(cryptfs_get_password_type()) {
328 case CRYPT_TYPE_PASSWORD:
329 cli->sendMsg(ResponseCode::PasswordTypeResult, "password", false);
331 case CRYPT_TYPE_PATTERN:
332 cli->sendMsg(ResponseCode::PasswordTypeResult, "pattern", false);
335 cli->sendMsg(ResponseCode::PasswordTypeResult, "pin", false);
337 case CRYPT_TYPE_DEFAULT:
338 cli->sendMsg(ResponseCode::PasswordTypeResult, "default", false);
341 /** @TODO better error and make sure handled by callers */
342 cli->sendMsg(ResponseCode::OpFailedStorageNotFound, "Error", false);
345 } else if (subcommand == "getpw") {
346 if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
347 SLOGD("cryptfs getpw");
348 dumpArgs(argc, argv, -1);
349 const char* password = cryptfs_get_password();
352 int size = asprintf(&message, "{{sensitive}} %s", password);
354 cli->sendMsg(ResponseCode::CommandOkay, message, false);
355 memset(message, 0, size);
361 } else if (subcommand == "clearpw") {
362 if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
363 SLOGD("cryptfs clearpw");
364 dumpArgs(argc, argv, -1);
365 cryptfs_clear_password();
368 } else if (subcommand == "isConvertibleToFBE") {
369 if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
370 // ext4enc:TODO: send a CommandSyntaxError if argv[2] not an integer
371 SLOGD("cryptfs isConvertibleToFBE");
372 dumpArgs(argc, argv, -1);
373 rc = cryptfs_isConvertibleToFBE();
375 } else if (subcommand == "init_user0") {
376 if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
377 return sendGenericOkFailOnBool(cli, e4crypt_init_user0());
379 } else if (subcommand == "create_user_key") {
380 if (!check_argc(cli, subcommand, argc, 5, "<user> <serial> <ephemeral>")) return 0;
381 return sendGenericOkFailOnBool(cli, e4crypt_vold_create_user_key(
382 atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) != 0));
384 } else if (subcommand == "destroy_user_key") {
385 if (!check_argc(cli, subcommand, argc, 3, "<user>")) return 0;
386 return sendGenericOkFailOnBool(cli, e4crypt_destroy_user_key(atoi(argv[2])));
388 } else if (subcommand == "change_user_key") {
389 if (!check_argc(cli, subcommand, argc, 7,
390 "<user> <serial> <token> <old_secret> <new_secret>")) return 0;
391 return sendGenericOkFailOnBool(cli, e4crypt_change_user_key(
392 atoi(argv[2]), atoi(argv[3]), argv[4], argv[5], argv[6]));
394 } else if (subcommand == "unlock_user_key") {
395 if (!check_argc(cli, subcommand, argc, 6, "<user> <serial> <token> <secret>")) return 0;
396 return sendGenericOkFailOnBool(cli, e4crypt_unlock_user_key(
397 atoi(argv[2]), atoi(argv[3]), argv[4], argv[5]));
399 } else if (subcommand == "lock_user_key") {
400 if (!check_argc(cli, subcommand, argc, 3, "<user>")) return 0;
401 return sendGenericOkFailOnBool(cli, e4crypt_lock_user_key(atoi(argv[2])));
403 } else if (subcommand == "prepare_user_storage") {
404 if (!check_argc(cli, subcommand, argc, 6, "<uuid> <user> <serial> <flags>")) return 0;
405 return sendGenericOkFailOnBool(cli, e4crypt_prepare_user_storage(
406 parseNull(argv[2]), atoi(argv[3]), atoi(argv[4]), atoi(argv[5])));
409 dumpArgs(argc, argv, -1);
410 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs subcommand", false);
414 // Always report that the command succeeded and return the error code.
415 // The caller will check the return value to see what the error was.
417 snprintf(msg, sizeof(msg), "%d", rc);
418 cli->sendMsg(ResponseCode::CommandOkay, msg, false);