OSDN Git Service

Merge "Add event monitoring to Inferno."
[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 <cutils/properties.h>
32 #include <cutils/sockets.h>
33 #include <keyutils.h>
34 #include <logwrap/logwrap.h>
35
36 #include "ext4_utils/ext4_crypt.h"
37
38 #define TAG "ext4_utils"
39
40 static const std::string arbitrary_sequence_number = "42";
41 static const int vold_command_timeout_ms = 60 * 1000;
42
43 int e4crypt_install_keyring()
44 {
45     key_serial_t device_keyring = add_key("keyring", "e4crypt", 0, 0,
46                                           KEY_SPEC_SESSION_KEYRING);
47
48     if (device_keyring == -1) {
49         PLOG(ERROR) << "Failed to create keyring";
50         return -1;
51     }
52
53     LOG(INFO) << "Keyring created with id " << device_keyring << " in process " << getpid();
54
55     return 0;
56 }
57
58 int e4crypt_set_directory_policy(const char* dir)
59 {
60     // Only set policy on first level /data directories
61     // To make this less restrictive, consider using a policy file.
62     // However this is overkill for as long as the policy is simply
63     // to apply a global policy to all /data folders created via makedir
64     if (!dir || strncmp(dir, "/data/", 6) || strchr(dir + 6, '/')) {
65         return 0;
66     }
67
68     // Special case various directories that must not be encrypted,
69     // often because their subdirectories must be encrypted.
70     // This isn't a nice way to do this, see b/26641735
71     std::vector<std::string> directories_to_exclude = {
72         "lost+found",
73         "system_ce", "system_de",
74         "misc_ce", "misc_de",
75         "media",
76         "data", "user", "user_de",
77     };
78     std::string prefix = "/data/";
79     for (auto d: directories_to_exclude) {
80         if ((prefix + d) == dir) {
81             LOG(INFO) << "Not setting policy on " << dir;
82             return 0;
83         }
84     }
85
86     std::string ref_filename = std::string("/data") + e4crypt_key_ref;
87     std::string policy;
88     if (!android::base::ReadFileToString(ref_filename, &policy)) {
89         LOG(ERROR) << "Unable to read system policy to set on " << dir;
90         return -1;
91     }
92
93     auto type_filename = std::string("/data") + e4crypt_key_mode;
94     std::string contents_encryption_mode;
95     if (!android::base::ReadFileToString(type_filename, &contents_encryption_mode)) {
96         LOG(ERROR) << "Cannot read mode";
97     }
98
99     LOG(INFO) << "Setting policy on " << dir;
100     int result = e4crypt_policy_ensure(dir, policy.c_str(), policy.length(),
101                                        contents_encryption_mode.c_str());
102     if (result) {
103         LOG(ERROR) << android::base::StringPrintf(
104             "Setting %02x%02x%02x%02x policy on %s failed!",
105             policy[0], policy[1], policy[2], policy[3], dir);
106         return -1;
107     }
108
109     return 0;
110 }