2 * Copyright (C) 2016 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.
17 #define TAG "ext4_utils"
19 #include "ext4_crypt_init_extensions.h"
20 #include "ext4_crypt.h"
22 #include <android-base/logging.h>
29 #include <sys/mount.h>
33 #include <android-base/file.h>
35 #include <cutils/klog.h>
36 #include <cutils/properties.h>
37 #include <cutils/sockets.h>
38 #include <logwrap/logwrap.h>
40 #include "key_control.h"
42 static const std::string arbitrary_sequence_number = "42";
43 static const int vold_command_timeout_ms = 60 * 1000;
45 static void kernel_logger(android::base::LogId, android::base::LogSeverity severity, const char*,
46 const char*, unsigned int, const char* message) {
47 if (severity == android::base::ERROR || severity == android::base::FATAL) {
48 KLOG_ERROR(TAG, "%s\n", message);
49 } else if (severity == android::base::WARNING) {
50 KLOG_WARNING(TAG, "%s\n", message);
52 KLOG_INFO(TAG, "%s\n", message);
56 static void init_logging() {
57 android::base::SetLogger(kernel_logger);
60 int e4crypt_create_device_key(const char* dir,
61 int ensure_dir_exists(const char*))
65 // Make sure folder exists. Use make_dir to set selinux permissions.
66 std::string unencrypted_dir = std::string(dir) + e4crypt_unencrypted_folder;
67 if (ensure_dir_exists(unencrypted_dir.c_str())) {
68 KLOG_ERROR(TAG, "Failed to create %s (%s)\n",
69 unencrypted_dir.c_str(),
74 const char* argv[] = { "/system/bin/vdc", "--wait", "cryptfs", "enablefilecrypto" };
75 int rc = android_fork_execvp(4, (char**) argv, NULL, false, true);
76 LOG(INFO) << "enablefilecrypto result: " << rc;
80 int e4crypt_install_keyring()
84 key_serial_t device_keyring = add_key("keyring", "e4crypt", 0, 0,
85 KEY_SPEC_SESSION_KEYRING);
87 if (device_keyring == -1) {
88 KLOG_ERROR(TAG, "Failed to create keyring (%s)\n", strerror(errno));
92 KLOG_INFO(TAG, "Keyring created with id %d in process %d\n",
93 device_keyring, getpid());
98 int e4crypt_do_init_user0()
102 const char* argv[] = { "/system/bin/vdc", "--wait", "cryptfs", "init_user0" };
103 int rc = android_fork_execvp(4, (char**) argv, NULL, false, true);
104 LOG(INFO) << "init_user0 result: " << rc;
108 int e4crypt_set_directory_policy(const char* dir)
112 // Only set policy on first level /data directories
113 // To make this less restrictive, consider using a policy file.
114 // However this is overkill for as long as the policy is simply
115 // to apply a global policy to all /data folders created via makedir
116 if (!dir || strncmp(dir, "/data/", 6) || strchr(dir + 6, '/')) {
120 // Special case various directories that must not be encrypted,
121 // often because their subdirectories must be encrypted.
122 // This isn't a nice way to do this, see b/26641735
123 std::vector<std::string> directories_to_exclude = {
125 "system_ce", "system_de",
126 "misc_ce", "misc_de",
128 "data", "user", "user_de",
130 std::string prefix = "/data/";
131 for (auto d: directories_to_exclude) {
132 if ((prefix + d) == dir) {
133 KLOG_INFO(TAG, "Not setting policy on %s\n", dir);
138 std::string ref_filename = std::string("/data") + e4crypt_key_ref;
140 if (!android::base::ReadFileToString(ref_filename, &policy)) {
141 KLOG_ERROR(TAG, "Unable to read system policy to set on %s\n", dir);
145 auto type_filename = std::string("/data") + e4crypt_key_mode;
146 std::string contents_encryption_mode;
147 if (!android::base::ReadFileToString(type_filename, &contents_encryption_mode)) {
148 LOG(ERROR) << "Cannot read mode";
151 KLOG_INFO(TAG, "Setting policy on %s\n", dir);
152 int result = e4crypt_policy_ensure(dir, policy.c_str(), policy.length(),
153 contents_encryption_mode.c_str());
155 KLOG_ERROR(TAG, "Setting %02x%02x%02x%02x policy on %s failed!\n",
156 policy[0], policy[1], policy[2], policy[3], dir);