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 "fscrypt/fscrypt.h"
18 #include "fscrypt/fscrypt_init_extensions.h"
19
20 #include <dirent.h>
21 #include <errno.h>
22 #include <sys/mount.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #include <string>
27 #include <vector>
28
29 #include <android-base/file.h>
30 #include <android-base/logging.h>
31 #include <android-base/stringprintf.h>
32 #include <android-base/strings.h>
33 #include <cutils/properties.h>
34 #include <cutils/sockets.h>
35 #include <keyutils.h>
36 #include <logwrap/logwrap.h>
37
38 #define TAG "fscrypt"
39
40 static const std::string arbitrary_sequence_number = "42";
41
42 static int set_system_de_policy_on(char const* dir);
43
fscrypt_install_keyring()44 int fscrypt_install_keyring()
45 {
46 key_serial_t device_keyring = add_key("keyring", "fscrypt", 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
fscrypt_set_directory_policy(const char * dir)59 int fscrypt_set_directory_policy(const char* dir)
60 {
61 if (!dir || strncmp(dir, "/data/", 6)) {
62 return 0;
63 }
64
65 // Special-case /data/media/obb per b/64566063
66 if (strcmp(dir, "/data/media/obb") == 0) {
67 // Try to set policy on this directory, but if it is non-empty this may fail.
68 set_system_de_policy_on(dir);
69 return 0;
70 }
71
72 // Only set policy on first level /data directories
73 // To make this less restrictive, consider using a policy file.
74 // However this is overkill for as long as the policy is simply
75 // to apply a global policy to all /data folders created via makedir
76 if (strchr(dir + 6, '/')) {
77 return 0;
78 }
79
80 // Special case various directories that must not be encrypted,
81 // often because their subdirectories must be encrypted.
82 // This isn't a nice way to do this, see b/26641735
83 std::vector<std::string> directories_to_exclude = {
84 "lost+found",
85 "system_ce", "system_de",
86 "misc_ce", "misc_de",
87 "vendor_ce", "vendor_de",
88 "media",
89 "data", "user", "user_de",
90 "apex", "preloads", "app-staging",
91 "gsi",
92 };
93 std::string prefix = "/data/";
94 for (const auto& d: directories_to_exclude) {
95 if ((prefix + d) == dir) {
96 LOG(INFO) << "Not setting policy on " << dir;
97 return 0;
98 }
99 }
100 return set_system_de_policy_on(dir);
101 }
102
set_system_de_policy_on(char const * dir)103 static int set_system_de_policy_on(char const* dir) {
104 std::string ref_filename = std::string("/data") + fscrypt_key_ref;
105 std::string policy;
106 if (!android::base::ReadFileToString(ref_filename, &policy)) {
107 LOG(ERROR) << "Unable to read system policy to set on " << dir;
108 return -1;
109 }
110
111 auto type_filename = std::string("/data") + fscrypt_key_mode;
112 std::string modestring;
113 if (!android::base::ReadFileToString(type_filename, &modestring)) {
114 LOG(ERROR) << "Cannot read mode";
115 }
116
117 std::vector<std::string> modes = android::base::Split(modestring, ":");
118
119 if (modes.size() < 1 || modes.size() > 2) {
120 LOG(ERROR) << "Invalid encryption mode string: " << modestring;
121 return -1;
122 }
123
124 LOG(INFO) << "Setting policy on " << dir;
125 int result = fscrypt_policy_ensure(dir, policy.c_str(), policy.length(),
126 modes[0].c_str(),
127 modes.size() >= 2 ?
128 modes[1].c_str() : "aes-256-cts");
129 if (result) {
130 LOG(ERROR) << android::base::StringPrintf(
131 "Setting %02x%02x%02x%02x policy on %s failed!",
132 policy[0], policy[1], policy[2], policy[3], dir);
133 return -1;
134 }
135
136 return 0;
137 }
138