blob: ffa369803fc702d2bd422a507a7dc38f9123aa8d [file] [log] [blame]
Jeff Sharkey2048a282017-06-15 09:59:43 -06001/*
2 * Copyright (C) 2017 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 "CheckEncryption.h"
18#include "FileDeviceUtils.h"
19#include "Utils.h"
20#include "VolumeManager.h"
21
22#include <android-base/file.h>
23#include <android-base/logging.h>
24#include <android-base/unique_fd.h>
25#include <cutils/iosched_policy.h>
26#include <private/android_filesystem_config.h>
27
28#include <sstream>
29
30#include <sys/resource.h>
31#include <sys/time.h>
32#include <unistd.h>
33
34#include <assert.h>
35#include <fcntl.h>
36#include <linux/fs.h>
37#include <sys/stat.h>
38#include <sys/types.h>
39#include <unistd.h>
40
41#include <fcntl.h>
42#include <sys/stat.h>
43#include <sys/types.h>
44
45using android::base::unique_fd;
46
47using android::base::ReadFileToString;
48using android::base::WriteStringToFile;
49
50namespace android {
51namespace vold {
52
53constexpr uint32_t max_extents = 32;
54constexpr size_t bytecount = 8;
55constexpr int repeats = 256;
56
57bool check_file(const std::string& needle) {
58 LOG(DEBUG) << "checkEncryption check_file: " << needle;
59 auto haystack = android::vold::BlockDeviceForPath(needle);
60 if (haystack.empty()) {
61 LOG(ERROR) << "Failed to find device for path: " << needle;
62 return false;
63 }
64
65 std::string randombytes;
66 if (ReadRandomBytes(bytecount, randombytes) != 0) {
67 LOG(ERROR) << "Failed to read random bytes";
68 return false;
69 }
70 std::string randomhex;
71 StrToHex(randombytes, randomhex);
72 std::ostringstream os;
73 for (int i = 0; i < repeats; i++) os << randomhex;
74 auto towrite = os.str();
75
76 if (access(needle.c_str(), F_OK) == 0) {
77 if (unlink(needle.c_str()) != 0) {
78 PLOG(ERROR) << "Failed to unlink " << needle;
79 return false;
80 }
81 }
82 LOG(DEBUG) << "Writing to " << needle;
83 if (!WriteStringToFile(towrite, needle)) {
84 PLOG(ERROR) << "Failed to write " << needle;
85 return false;
86 }
87 sync();
88
89 unique_fd haystack_fd(open(haystack.c_str(), O_RDONLY | O_CLOEXEC));
90 if (haystack_fd.get() == -1) {
91 PLOG(ERROR) << "Failed to open " << haystack;
92 return false;
93 }
94
95 auto fiemap = PathFiemap(needle, max_extents);
96
97 std::string area;
98 for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) {
99 auto xt = &(fiemap->fm_extents[i]);
100 LOG(DEBUG) << "Extent " << i << " at " << xt->fe_physical << " length " << xt->fe_length;
101 if (lseek64(haystack_fd.get(), xt->fe_physical, SEEK_SET) == -1) {
102 PLOG(ERROR) << "Failed lseek";
103 return false;
104 }
105 auto toread = xt->fe_length;
106 while (toread > 0) {
107 char buf[BUFSIZ];
108 size_t wlen =
109 static_cast<size_t>(std::min(static_cast<typeof(toread)>(sizeof(buf)), toread));
110 auto l = read(haystack_fd.get(), buf, wlen);
111 if (l < 1) {
112 PLOG(ERROR) << "Failed read";
113 if (errno != EINTR) {
114 return false;
115 }
116 }
117 area.append(buf, l);
118 toread -= l;
119 }
120 }
121
122 LOG(DEBUG) << "Searching " << area.size() << " bytes of " << needle;
123 LOG(DEBUG) << "First position of blob: " << area.find(randomhex);
124 return true;
125}
126
127int CheckEncryption(const std::string& path) {
128 auto deNeedle(path);
129 deNeedle += "/misc";
130 if (android::vold::PrepareDir(deNeedle, 01771, AID_SYSTEM, AID_MISC)) {
131 return -1;
132 }
133 deNeedle += "/vold";
134 if (android::vold::PrepareDir(deNeedle, 0700, AID_ROOT, AID_ROOT)) {
135 return -1;
136 }
137 deNeedle += "/checkEncryption";
138
139 auto neNeedle(path);
140 neNeedle += "/unencrypted/checkEncryption";
141
142 check_file(deNeedle);
143 check_file(neNeedle);
144
145 return 0;
146}
147
148} // namespace vold
149} // namespace android