• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
e4crypt_install_keyring()44 int e4crypt_install_keyring()
45 {
46     key_serial_t device_keyring = add_key("keyring", "e4crypt", 0, 0,
47                                           KEY_SPEC_SESSION_KEYRING);
48 
49     if (device_keyring == -1) {
50         PLOG(ERROR) << "Failed to create keyring";
51         return -1;
52     }
53 
54     LOG(INFO) << "Keyring created with id " << device_keyring << " in process " << getpid();
55 
56     return 0;
57 }
58 
e4crypt_do_init_user0()59 int e4crypt_do_init_user0()
60 {
61     const char* argv[] = { "/system/bin/vdc", "--wait", "cryptfs", "init_user0" };
62     int rc = android_fork_execvp_ext(arraysize(argv), (char**) argv, NULL, false,
63                                      LOG_KLOG, false, NULL, NULL, 0);
64     LOG(INFO) << "init_user0 result: " << rc;
65     return rc;
66 }
67 
e4crypt_set_directory_policy(const char * dir)68 int e4crypt_set_directory_policy(const char* dir)
69 {
70     // Only set policy on first level /data directories
71     // To make this less restrictive, consider using a policy file.
72     // However this is overkill for as long as the policy is simply
73     // to apply a global policy to all /data folders created via makedir
74     if (!dir || strncmp(dir, "/data/", 6) || strchr(dir + 6, '/')) {
75         return 0;
76     }
77 
78     // Special case various directories that must not be encrypted,
79     // often because their subdirectories must be encrypted.
80     // This isn't a nice way to do this, see b/26641735
81     std::vector<std::string> directories_to_exclude = {
82         "lost+found",
83         "system_ce", "system_de",
84         "misc_ce", "misc_de",
85         "media",
86         "data", "user", "user_de",
87     };
88     std::string prefix = "/data/";
89     for (auto d: directories_to_exclude) {
90         if ((prefix + d) == dir) {
91             LOG(INFO) << "Not setting policy on " << dir;
92             return 0;
93         }
94     }
95 
96     std::string ref_filename = std::string("/data") + e4crypt_key_ref;
97     std::string policy;
98     if (!android::base::ReadFileToString(ref_filename, &policy)) {
99         LOG(ERROR) << "Unable to read system policy to set on " << dir;
100         return -1;
101     }
102 
103     auto type_filename = std::string("/data") + e4crypt_key_mode;
104     std::string modestring;
105     if (!android::base::ReadFileToString(type_filename, &modestring)) {
106         LOG(ERROR) << "Cannot read mode";
107     }
108 
109     std::vector<std::string> modes = android::base::Split(modestring, ":");
110 
111     if (modes.size() < 1 || modes.size() > 2) {
112         LOG(ERROR) << "Invalid encryption mode string: " << modestring;
113         return -1;
114     }
115 
116     LOG(INFO) << "Setting policy on " << dir;
117     int result = e4crypt_policy_ensure(dir, policy.c_str(), policy.length(),
118                                        modes[0].c_str(),
119                                        modes.size() >= 2 ?
120                                             modes[1].c_str() : "aes-256-cts");
121     if (result) {
122         LOG(ERROR) << android::base::StringPrintf(
123             "Setting %02x%02x%02x%02x policy on %s failed!",
124             policy[0], policy[1], policy[2], policy[3], dir);
125         return -1;
126     }
127 
128     return 0;
129 }
130