Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 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 | |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 17 | #include <memory> |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 18 | #include <string> |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 19 | #include <vector> |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 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> |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 29 | #include <mntent.h> |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 30 | |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 31 | #include <android-base/logging.h> |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 32 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 33 | #include <AutoCloseFD.h> |
| 34 | |
| 35 | namespace { |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 36 | |
| 37 | struct Options { |
| 38 | std::vector<std::string> targets; |
| 39 | bool unlink{true}; |
| 40 | }; |
| 41 | |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 42 | constexpr uint32_t max_extents = 32; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 43 | |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 44 | bool read_command_line(int argc, const char * const argv[], Options &options); |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 45 | void usage(const char *progname); |
| 46 | int secdiscard_path(const std::string &path); |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 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); |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 50 | std::string block_device_for_path(const std::string &path); |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 51 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 52 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 53 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 54 | int main(int argc, const char * const argv[]) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 55 | android::base::InitLogging(const_cast<char **>(argv)); |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 56 | Options options; |
| 57 | if (!read_command_line(argc, argv, options)) { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 58 | usage(argv[0]); |
| 59 | return -1; |
| 60 | } |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 61 | for (auto target: options.targets) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 62 | LOG(DEBUG) << "Securely discarding '" << target << "' unlink=" << options.unlink; |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 63 | secdiscard_path(target); |
| 64 | if (options.unlink) { |
| 65 | if (unlink(target.c_str()) != 0 && errno != ENOENT) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 66 | PLOG(ERROR) << "Unable to unlink: " << target; |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 67 | } |
| 68 | } |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 69 | LOG(DEBUG) << "Discarded: " << target; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 70 | } |
| 71 | return 0; |
| 72 | } |
| 73 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 74 | namespace { |
| 75 | |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 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 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 93 | void usage(const char *progname) { |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 94 | fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname); |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // BLKSECDISCARD all content in "path", if it's small enough. |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 98 | int secdiscard_path(const std::string &path) { |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 99 | auto fiemap = path_fiemap(path, max_extents); |
| 100 | if (!fiemap || !check_fiemap(*fiemap, path)) { |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 101 | return -1; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 102 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 103 | auto block_device = block_device_for_path(path); |
| 104 | if (block_device.empty()) { |
| 105 | return -1; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 106 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 107 | AutoCloseFD fs_fd(block_device, O_RDWR | O_LARGEFILE); |
| 108 | if (!fs_fd) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 109 | PLOG(ERROR) << "Failed to open device " << block_device; |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 110 | return -1; |
| 111 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 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) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 117 | PLOG(ERROR) << "Unable to BLKSECDISCARD " << path; |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 118 | return -1; |
| 119 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 120 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 121 | return 0; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 124 | // Read the file's FIEMAP |
| 125 | std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count) |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 126 | { |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 127 | AutoCloseFD fd(path); |
| 128 | if (!fd) { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 129 | if (errno == ENOENT) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 130 | PLOG(DEBUG) << "Unable to open " << path; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 131 | } else { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 132 | PLOG(ERROR) << "Unable to open " << path; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 133 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 134 | return nullptr; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 135 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 136 | auto fiemap = alloc_fiemap(extent_count); |
| 137 | if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap.get()) != 0) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 138 | PLOG(ERROR) << "Unable to FIEMAP " << path; |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 139 | return nullptr; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 140 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 141 | auto mapped = fiemap->fm_mapped_extents; |
| 142 | if (mapped < 1 || mapped > extent_count) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 143 | LOG(ERROR) << "Extent count not in bounds 1 <= " << mapped << " <= " << extent_count |
| 144 | << " in " << path; |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 145 | return nullptr; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 146 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 147 | return fiemap; |
| 148 | } |
| 149 | |
| 150 | // Ensure that the FIEMAP covers the file and is OK to discard |
| 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)) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 154 | LOG(ERROR) << "Extent " << mapped -1 << " was not the last in " << path; |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 155 | return false; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 156 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 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)) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 160 | LOG(ERROR) << "Extent " << i << " has unexpected flags " << flags << ": " << path; |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 161 | return false; |
| 162 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 163 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
| 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; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 180 | // Given a file path, look for the corresponding block device in /proc/mount |
| 181 | std::string block_device_for_path(const std::string &path) |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 182 | { |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 183 | std::unique_ptr<FILE, int(*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent); |
| 184 | if (!mnts) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 185 | PLOG(ERROR) << "Unable to open /proc/mounts"; |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 186 | return ""; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 187 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 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 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 200 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 201 | if (result.empty()) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 202 | LOG(ERROR) <<"Didn't find a mountpoint to match path " << path; |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 203 | return ""; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 204 | } |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 205 | LOG(DEBUG) << "For path " << path << " block device is " << result; |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 206 | return result; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 209 | } |