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 | |
| 31 | #define LOG_TAG "secdiscard" |
| 32 | #include "cutils/log.h" |
| 33 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 34 | #include <AutoCloseFD.h> |
| 35 | |
| 36 | namespace { |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 37 | |
| 38 | struct Options { |
| 39 | std::vector<std::string> targets; |
| 40 | bool unlink{true}; |
| 41 | }; |
| 42 | |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame^] | 43 | constexpr uint32_t max_extents = 32; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 44 | |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 45 | bool read_command_line(int argc, const char * const argv[], Options &options); |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 46 | void usage(const char *progname); |
| 47 | int secdiscard_path(const std::string &path); |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame^] | 48 | std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count); |
| 49 | bool check_fiemap(const struct fiemap &fiemap, const std::string &path); |
| 50 | std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count); |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 51 | std::string block_device_for_path(const std::string &path); |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 52 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 53 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 54 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 55 | int main(int argc, const char * const 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) { |
| 62 | SLOGD("Securely discarding '%s' unlink=%d", target.c_str(), options.unlink); |
| 63 | secdiscard_path(target); |
| 64 | if (options.unlink) { |
| 65 | if (unlink(target.c_str()) != 0 && errno != ENOENT) { |
| 66 | SLOGE("Unable to unlink %s: %s", |
| 67 | target.c_str(), strerror(errno)); |
| 68 | } |
| 69 | } |
| 70 | SLOGD("Discarded %s", target.c_str()); |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 75 | namespace { |
| 76 | |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 77 | bool read_command_line(int argc, const char * const argv[], Options &options) { |
| 78 | for (int i = 1; i < argc; i++) { |
| 79 | if (!strcmp("--no-unlink", argv[i])) { |
| 80 | options.unlink = false; |
| 81 | } else if (!strcmp("--", argv[i])) { |
| 82 | for (int j = i+1; j < argc; j++) { |
| 83 | if (argv[j][0] != '/') return false; // Must be absolute path |
| 84 | options.targets.emplace_back(argv[j]); |
| 85 | } |
| 86 | return options.targets.size() > 0; |
| 87 | } else { |
| 88 | return false; // Unknown option |
| 89 | } |
| 90 | } |
| 91 | return false; // "--" not found |
| 92 | } |
| 93 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 94 | void usage(const char *progname) { |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 95 | fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname); |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // BLKSECDISCARD all content in "path", if it's small enough. |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 99 | int secdiscard_path(const std::string &path) { |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame^] | 100 | auto fiemap = path_fiemap(path, max_extents); |
| 101 | if (!fiemap || !check_fiemap(*fiemap, path)) { |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 102 | return -1; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 103 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 104 | auto block_device = block_device_for_path(path); |
| 105 | if (block_device.empty()) { |
| 106 | return -1; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 107 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 108 | AutoCloseFD fs_fd(block_device, O_RDWR | O_LARGEFILE); |
| 109 | if (!fs_fd) { |
| 110 | SLOGE("Failed to open device %s: %s", block_device.c_str(), strerror(errno)); |
| 111 | return -1; |
| 112 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame^] | 113 | for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) { |
| 114 | uint64_t range[2]; |
| 115 | range[0] = fiemap->fm_extents[i].fe_physical; |
| 116 | range[1] = fiemap->fm_extents[i].fe_length; |
| 117 | if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) { |
| 118 | SLOGE("Unable to BLKSECDISCARD %s: %s", path.c_str(), strerror(errno)); |
| 119 | return -1; |
| 120 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 121 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 122 | return 0; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame^] | 125 | // Read the file's FIEMAP |
| 126 | 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] | 127 | { |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 128 | AutoCloseFD fd(path); |
| 129 | if (!fd) { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 130 | if (errno == ENOENT) { |
| 131 | SLOGD("Unable to open %s: %s", path.c_str(), strerror(errno)); |
| 132 | } else { |
| 133 | SLOGE("Unable to open %s: %s", path.c_str(), strerror(errno)); |
| 134 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame^] | 135 | return nullptr; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 136 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame^] | 137 | auto fiemap = alloc_fiemap(extent_count); |
| 138 | if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap.get()) != 0) { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 139 | SLOGE("Unable to FIEMAP %s: %s", path.c_str(), strerror(errno)); |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame^] | 140 | return nullptr; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 141 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame^] | 142 | auto mapped = fiemap->fm_mapped_extents; |
| 143 | if (mapped < 1 || mapped > extent_count) { |
| 144 | SLOGE("Extent count not in bounds 1 <= %u <= %u in %s", mapped, extent_count, path.c_str()); |
| 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)) { |
| 154 | SLOGE("Extent %u was not the last in %s", mapped - 1, path.c_str()); |
| 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)) { |
| 160 | SLOGE("Extent %u has unexpected flags %ulx: %s", i, flags, path.c_str()); |
| 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) { |
| 185 | SLOGE("Unable to open /proc/mounts: %s", strerror(errno)); |
| 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()) { |
| 202 | SLOGE("Didn't find a mountpoint to match path %s", path.c_str()); |
| 203 | return ""; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 204 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 205 | SLOGD("For path %s block device is %s", path.c_str(), result.c_str()); |
| 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 | } |