-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfscrypt_policy.cpp
More file actions
executable file
·198 lines (181 loc) · 6.49 KB
/
fscrypt_policy.cpp
File metadata and controls
executable file
·198 lines (181 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <array>
#include <asm/ioctl.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <cutils/properties.h>
#include <logwrap/logwrap.h>
#include <utils/misc.h>
#include <fscrypt/fscrypt.h>
#include "KeyUtil.h"
#include "fscrypt_policy.h"
static int encryption_mode = FS_ENCRYPTION_MODE_PRIVATE;
bool fscrypt_is_native() {
char value[PROPERTY_VALUE_MAX];
property_get("ro.crypto.type", value, "none");
return !strcmp(value, "file");
}
extern "C" void bytes_to_hex(const uint8_t *bytes, size_t num_bytes, char *hex) {
for (size_t i = 0; i < num_bytes; i++) {
sprintf(&hex[2 * i], "%02x", bytes[i]);
}
}
extern "C" bool fscrypt_set_mode() {
const char* mode_file = "/data/unencrypted/mode";
struct stat st;
if (stat(mode_file, &st) != 0 || st.st_size <= 0) {
printf("Invalid encryption mode file %s\n", mode_file);
return false;
}
ssize_t mode_size = st.st_size;
char contents_encryption_mode[mode_size + 1];
memset((void*)contents_encryption_mode, 0, mode_size + 1);
int fd = open(mode_file, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
printf("error opening '%s': %s\n", mode_file, strerror(errno));
return false;
}
if (read(fd, contents_encryption_mode, mode_size) != mode_size) {
printf("read error on '%s': %s\n", mode_file, strerror(errno));
close(fd);
return false;
}
close(fd);
std::string contents_encryption_mode_string = std::string(contents_encryption_mode);
int pos = contents_encryption_mode_string.find(":");
LOG(INFO) << "contents_encryption_mode_string: " << contents_encryption_mode_string.substr(0, pos);
if (contents_encryption_mode_string.substr(0, pos) == "software") {
encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
} else if (contents_encryption_mode_string.substr(0, pos) == "ice") {
encryption_mode = FS_ENCRYPTION_MODE_PRIVATE;
} else {
printf("Invalid encryption mode '%s'\n", contents_encryption_mode);
return false;
}
printf("set encryption mode to %i\n", encryption_mode);
return true;
}
#ifdef USE_FSCRYPT_POLICY_V1
extern "C" bool fscrypt_policy_set_struct(const char *directory, const struct fscrypt_policy_v1 *fep) {
#else
extern "C" bool fscrypt_policy_set_struct(const char *directory, const struct fscrypt_policy_v2 *fep) {
#endif
int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
if (fd == -1) {
printf("failed to open %s\n", directory);
PLOG(ERROR) << "Failed to open directory " << directory;
return false;
}
// if (android::vold::isFsKeyringSupported()) {
if (ioctl(fd, FS_IOC_SET_ENCRYPTION_POLICY, fep)) {
PLOG(ERROR) << "Failed to set encryption policy for " << directory;
close(fd);
return false;
}
// } else {
// if (ioctl(fd, FS_IOC_SET_ENCRYPTION_POLICY, fep)) {
// PLOG(ERROR) << "Failed to set encryption policy for " << directory;
// close(fd);
// return false;
// }
// }
close(fd);
return true;
}
#ifdef USE_FSCRYPT_POLICY_V1
extern "C" bool fscrypt_policy_get_struct(const char *directory, struct fscrypt_policy_v1 *fep) {
#else
extern "C" bool fscrypt_policy_get_struct(const char *directory, struct fscrypt_policy_v2 *fep) {
#endif
int fd = open(directory, O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
if (fd == -1) {
PLOG(ERROR) << "Failed to open directory " << directory;
return false;
}
#ifdef USE_FSCRYPT_POLICY_V1
memset(fep, 0, sizeof(fscrypt_policy_v1));
#else
memset(fep, 0, sizeof(fscrypt_policy_v2));
#endif
struct fscrypt_get_policy_ex_arg ex_policy = {0};
// if (android::vold::isFsKeyringSupported()) {
ex_policy.policy_size = sizeof(ex_policy.policy);
if (ioctl(fd, FS_IOC_GET_ENCRYPTION_POLICY_EX, &ex_policy) != 0) {
PLOG(ERROR) << "Failed to get encryption policy for " << directory;
close(fd);
return false;
}
#ifdef USE_FSCRYPT_POLICY_V1
memcpy(fep, &ex_policy.policy.v1, sizeof(ex_policy.policy.v1));
#else
memcpy(fep, &ex_policy.policy.v2, sizeof(ex_policy.policy.v2));
#endif
// } else {
// if (ioctl(fd, FS_IOC_GET_ENCRYPTION_POLICY, &ex_policy.policy.v1) != 0) {
// PLOG(ERROR) << "Failed to get encryption policy for " << directory;
// close(fd);
// return false;
// }
// memcpy(fep, &ex_policy.policy.v1, sizeof(ex_policy.policy.v1));
// }
close(fd);
return true;
}
#ifdef TW_INCLUDE_CRYPTO
#ifdef USE_FSCRYPT_POLICY_V1
extern "C" bool Get_Encryption_Policy(struct fscrypt_policy_v1 &policy, std::string path) {
#else
extern "C" bool Get_Encryption_Policy(struct fscrypt_policy_v2 &policy, std::string path) {
#endif
if (!android::vold::pathExists(path)) {
LOGERR("Unable to find %s to get policy\n", path.c_str());
return false;
}
if (!fscrypt_policy_get_struct(path.c_str(), &policy)) {
LOGERR("No policy set for path %s\n", path.c_str());
return false;
}
return true;
}
#ifdef USE_FSCRYPT_POLICY_V1
extern "C" bool Set_Encryption_Policy(std::string path, struct fscrypt_policy_v1 &policy) {
#else
extern "C" bool Set_Encryption_Policy(std::string path, struct fscrypt_policy_v2 &policy) {
#endif
if (!android::vold::pathExists(path)) {
LOGERR("unable to find %s to set policy\n", path.c_str());
return false;
}
uint8_t binary_policy[FS_KEY_DESCRIPTOR_SIZE];
char policy_hex[FSCRYPT_KEY_IDENTIFIER_HEX_SIZE];
bytes_to_hex(binary_policy, FS_KEY_DESCRIPTOR_SIZE, policy_hex);
if (!fscrypt_policy_set_struct(path.c_str(), &policy)) {
LOGERR("unable to set policy for path: %s\n", path.c_str());
return false;
}
return true;
}
#endif