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 #include "ext4_utils/ext4_crypt_init_extensions.h"
21 #include <sys/mount.h>
28 #include <android-base/file.h>
29 #include <android-base/logging.h>
30 #include <android-base/stringprintf.h>
31 #include <android-base/strings.h>
32 #include <cutils/properties.h>
33 #include <cutils/sockets.h>
35 #include <logwrap/logwrap.h>
37 #include "ext4_utils/ext4_crypt.h"
39 #define TAG "ext4_utils"
41 static const std::string arbitrary_sequence_number = "42";
42 static const int vold_command_timeout_ms = 60 * 1000;
44 int e4crypt_install_keyring()
46 key_serial_t device_keyring = add_key("keyring", "e4crypt", 0, 0,
47 KEY_SPEC_SESSION_KEYRING);
49 if (device_keyring == -1) {
50 PLOG(ERROR) << "Failed to create keyring";
54 LOG(INFO) << "Keyring created with id " << device_keyring << " in process " << getpid();
59 int e4crypt_set_directory_policy(const char* dir)
61 // Only set policy on first level /data directories
62 // To make this less restrictive, consider using a policy file.
63 // However this is overkill for as long as the policy is simply
64 // to apply a global policy to all /data folders created via makedir
65 if (!dir || strncmp(dir, "/data/", 6) || strchr(dir + 6, '/')) {
69 // Special case various directories that must not be encrypted,
70 // often because their subdirectories must be encrypted.
71 // This isn't a nice way to do this, see b/26641735
72 std::vector<std::string> directories_to_exclude = {
74 "system_ce", "system_de",
77 "data", "user", "user_de",
79 std::string prefix = "/data/";
80 for (auto d: directories_to_exclude) {
81 if ((prefix + d) == dir) {
82 LOG(INFO) << "Not setting policy on " << dir;
87 std::string ref_filename = std::string("/data") + e4crypt_key_ref;
89 if (!android::base::ReadFileToString(ref_filename, &policy)) {
90 LOG(ERROR) << "Unable to read system policy to set on " << dir;
94 auto type_filename = std::string("/data") + e4crypt_key_mode;
95 std::string modestring;
96 if (!android::base::ReadFileToString(type_filename, &modestring)) {
97 LOG(ERROR) << "Cannot read mode";
100 std::vector<std::string> modes = android::base::Split(modestring, ":");
102 if (modes.size() < 1 || modes.size() > 2) {
103 LOG(ERROR) << "Invalid encryption mode string: " << modestring;
107 LOG(INFO) << "Setting policy on " << dir;
108 int result = e4crypt_policy_ensure(dir, policy.c_str(), policy.length(),
111 modes[1].c_str() : "aes-256-cts");
113 LOG(ERROR) << android::base::StringPrintf(
114 "Setting %02x%02x%02x%02x policy on %s failed!",
115 policy[0], policy[1], policy[2], policy[3], dir);