1 /*
2 * Copyright (C) 2015 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 <memory>
18 #include <string>
19 #include <vector>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <linux/fs.h>
28 #include <linux/fiemap.h>
29 #include <mntent.h>
30
31 #include <android-base/logging.h>
32
33 #include <AutoCloseFD.h>
34
35 namespace {
36
37 struct Options {
38 std::vector<std::string> targets;
39 bool unlink{true};
40 };
41
42 constexpr uint32_t max_extents = 32;
43
44 bool read_command_line(int argc, const char * const argv[], Options &options);
45 void usage(const char *progname);
46 int secdiscard_path(const std::string &path);
47 std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count);
48 bool check_fiemap(const struct fiemap &fiemap, const std::string &path);
49 std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count);
50 std::string block_device_for_path(const std::string &path);
51
52 }
53
main(int argc,const char * const argv[])54 int main(int argc, const char * const argv[]) {
55 android::base::InitLogging(const_cast<char **>(argv));
56 Options options;
57 if (!read_command_line(argc, argv, options)) {
58 usage(argv[0]);
59 return -1;
60 }
61 for (auto target: options.targets) {
62 LOG(DEBUG) << "Securely discarding '" << target << "' unlink=" << options.unlink;
63 secdiscard_path(target);
64 if (options.unlink) {
65 if (unlink(target.c_str()) != 0 && errno != ENOENT) {
66 PLOG(ERROR) << "Unable to unlink: " << target;
67 }
68 }
69 LOG(DEBUG) << "Discarded: " << target;
70 }
71 return 0;
72 }
73
74 namespace {
75
read_command_line(int argc,const char * const argv[],Options & options)76 bool read_command_line(int argc, const char * const argv[], Options &options) {
77 for (int i = 1; i < argc; i++) {
78 if (!strcmp("--no-unlink", argv[i])) {
79 options.unlink = false;
80 } else if (!strcmp("--", argv[i])) {
81 for (int j = i+1; j < argc; j++) {
82 if (argv[j][0] != '/') return false; // Must be absolute path
83 options.targets.emplace_back(argv[j]);
84 }
85 return options.targets.size() > 0;
86 } else {
87 return false; // Unknown option
88 }
89 }
90 return false; // "--" not found
91 }
92
usage(const char * progname)93 void usage(const char *progname) {
94 fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname);
95 }
96
97 // BLKSECDISCARD all content in "path", if it's small enough.
secdiscard_path(const std::string & path)98 int secdiscard_path(const std::string &path) {
99 auto fiemap = path_fiemap(path, max_extents);
100 if (!fiemap || !check_fiemap(*fiemap, path)) {
101 return -1;
102 }
103 auto block_device = block_device_for_path(path);
104 if (block_device.empty()) {
105 return -1;
106 }
107 AutoCloseFD fs_fd(block_device, O_RDWR | O_LARGEFILE);
108 if (!fs_fd) {
109 PLOG(ERROR) << "Failed to open device " << block_device;
110 return -1;
111 }
112 for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) {
113 uint64_t range[2];
114 range[0] = fiemap->fm_extents[i].fe_physical;
115 range[1] = fiemap->fm_extents[i].fe_length;
116 if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) {
117 PLOG(ERROR) << "Unable to BLKSECDISCARD " << path;
118 return -1;
119 }
120 }
121 return 0;
122 }
123
124 // Read the file's FIEMAP
path_fiemap(const std::string & path,uint32_t extent_count)125 std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count)
126 {
127 AutoCloseFD fd(path);
128 if (!fd) {
129 if (errno == ENOENT) {
130 PLOG(DEBUG) << "Unable to open " << path;
131 } else {
132 PLOG(ERROR) << "Unable to open " << path;
133 }
134 return nullptr;
135 }
136 auto fiemap = alloc_fiemap(extent_count);
137 if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap.get()) != 0) {
138 PLOG(ERROR) << "Unable to FIEMAP " << path;
139 return nullptr;
140 }
141 auto mapped = fiemap->fm_mapped_extents;
142 if (mapped < 1 || mapped > extent_count) {
143 LOG(ERROR) << "Extent count not in bounds 1 <= " << mapped << " <= " << extent_count
144 << " in " << path;
145 return nullptr;
146 }
147 return fiemap;
148 }
149
150 // Ensure that the FIEMAP covers the file and is OK to discard
check_fiemap(const struct fiemap & fiemap,const std::string & path)151 bool check_fiemap(const struct fiemap &fiemap, const std::string &path) {
152 auto mapped = fiemap.fm_mapped_extents;
153 if (!(fiemap.fm_extents[mapped - 1].fe_flags & FIEMAP_EXTENT_LAST)) {
154 LOG(ERROR) << "Extent " << mapped -1 << " was not the last in " << path;
155 return false;
156 }
157 for (uint32_t i = 0; i < mapped; i++) {
158 auto flags = fiemap.fm_extents[i].fe_flags;
159 if (flags & (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) {
160 LOG(ERROR) << "Extent " << i << " has unexpected flags " << flags << ": " << path;
161 return false;
162 }
163 }
164 return true;
165 }
166
alloc_fiemap(uint32_t extent_count)167 std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count)
168 {
169 size_t allocsize = offsetof(struct fiemap, fm_extents[extent_count]);
170 std::unique_ptr<struct fiemap> res(new (::operator new (allocsize)) struct fiemap);
171 memset(res.get(), 0, allocsize);
172 res->fm_start = 0;
173 res->fm_length = UINT64_MAX;
174 res->fm_flags = 0;
175 res->fm_extent_count = extent_count;
176 res->fm_mapped_extents = 0;
177 return res;
178 }
179
180 // Given a file path, look for the corresponding block device in /proc/mount
block_device_for_path(const std::string & path)181 std::string block_device_for_path(const std::string &path)
182 {
183 std::unique_ptr<FILE, int(*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent);
184 if (!mnts) {
185 PLOG(ERROR) << "Unable to open /proc/mounts";
186 return "";
187 }
188 std::string result;
189 size_t best_length = 0;
190 struct mntent *mnt; // getmntent returns a thread local, so it's safe.
191 while ((mnt = getmntent(mnts.get())) != nullptr) {
192 auto l = strlen(mnt->mnt_dir);
193 if (l > best_length &&
194 path.size() > l &&
195 path[l] == '/' &&
196 path.compare(0, l, mnt->mnt_dir) == 0) {
197 result = mnt->mnt_fsname;
198 best_length = l;
199 }
200 }
201 if (result.empty()) {
202 LOG(ERROR) <<"Didn't find a mountpoint to match path " << path;
203 return "";
204 }
205 LOG(DEBUG) << "For path " << path << " block device is " << result;
206 return result;
207 }
208
209 }
210