OSDN Git Service

Merge "simpleperf: fix a small error." am: 3c7ae46f92 am: 9898014244
[android-x86/system-extras.git] / ext4_utils / ext4_crypt_init_extensions.cpp
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "ext4_utils/ext4_crypt_init_extensions.h"
18
19 #include <dirent.h>
20 #include <errno.h>
21 #include <sys/mount.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include <string>
26 #include <vector>
27
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>
34 #include <logwrap/logwrap.h>
35
36 #include "ext4_utils/ext4_crypt.h"
37 #include "ext4_utils/key_control.h"
38
39 #define TAG "ext4_utils"
40
41 static const std::string arbitrary_sequence_number = "42";
42 static const int vold_command_timeout_ms = 60 * 1000;
43
44 int e4crypt_create_device_key(const char* dir,
45                               int ensure_dir_exists(const char*))
46 {
47     // Make sure folder exists. Use make_dir to set selinux permissions.
48     std::string unencrypted_dir = std::string(dir) + e4crypt_unencrypted_folder;
49     if (ensure_dir_exists(unencrypted_dir.c_str())) {
50         PLOG(ERROR) << "Failed to create " << unencrypted_dir;
51         return -1;
52     }
53
54     const char* argv[] = { "/system/bin/vdc", "--wait", "cryptfs", "enablefilecrypto" };
55     int rc = android_fork_execvp_ext(arraysize(argv), (char**) argv, NULL, false,
56                                      LOG_KLOG, false, NULL, NULL, 0);
57     LOG(INFO) << "enablefilecrypto result: " << rc;
58     return rc;
59 }
60
61 int e4crypt_install_keyring()
62 {
63     key_serial_t device_keyring = add_key("keyring", "e4crypt", 0, 0,
64                                           KEY_SPEC_SESSION_KEYRING);
65
66     if (device_keyring == -1) {
67         PLOG(ERROR) << "Failed to create keyring";
68         return -1;
69     }
70
71     LOG(INFO) << "Keyring created with id " << device_keyring << " in process " << getpid();
72
73     return 0;
74 }
75
76 int e4crypt_do_init_user0()
77 {
78     const char* argv[] = { "/system/bin/vdc", "--wait", "cryptfs", "init_user0" };
79     int rc = android_fork_execvp_ext(arraysize(argv), (char**) argv, NULL, false,
80                                      LOG_KLOG, false, NULL, NULL, 0);
81     LOG(INFO) << "init_user0 result: " << rc;
82     return rc;
83 }
84
85 int e4crypt_set_directory_policy(const char* dir)
86 {
87     // Only set policy on first level /data directories
88     // To make this less restrictive, consider using a policy file.
89     // However this is overkill for as long as the policy is simply
90     // to apply a global policy to all /data folders created via makedir
91     if (!dir || strncmp(dir, "/data/", 6) || strchr(dir + 6, '/')) {
92         return 0;
93     }
94
95     // Special case various directories that must not be encrypted,
96     // often because their subdirectories must be encrypted.
97     // This isn't a nice way to do this, see b/26641735
98     std::vector<std::string> directories_to_exclude = {
99         "lost+found",
100         "system_ce", "system_de",
101         "misc_ce", "misc_de",
102         "media",
103         "data", "user", "user_de",
104     };
105     std::string prefix = "/data/";
106     for (auto d: directories_to_exclude) {
107         if ((prefix + d) == dir) {
108             LOG(INFO) << "Not setting policy on " << dir;
109             return 0;
110         }
111     }
112
113     std::string ref_filename = std::string("/data") + e4crypt_key_ref;
114     std::string policy;
115     if (!android::base::ReadFileToString(ref_filename, &policy)) {
116         LOG(ERROR) << "Unable to read system policy to set on " << dir;
117         return -1;
118     }
119
120     auto type_filename = std::string("/data") + e4crypt_key_mode;
121     std::string modestring;
122     if (!android::base::ReadFileToString(type_filename, &modestring)) {
123         LOG(ERROR) << "Cannot read mode";
124     }
125
126     std::vector<std::string> modes = android::base::Split(modestring, ":");
127
128     if (modes.size() < 1 || modes.size() > 2) {
129         LOG(ERROR) << "Invalid encryption mode string: " << modestring;
130         return -1;
131     }
132
133     LOG(INFO) << "Setting policy on " << dir;
134     int result = e4crypt_policy_ensure(dir, policy.c_str(), policy.length(),
135                                        modes[0].c_str(),
136                                        modes.size() >= 2 ?
137                                             modes[1].c_str() : "aes-256-cts");
138     if (result) {
139         LOG(ERROR) << android::base::StringPrintf(
140             "Setting %02x%02x%02x%02x policy on %s failed!",
141             policy[0], policy[1], policy[2], policy[3], dir);
142         return -1;
143     }
144
145     return 0;
146 }