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 | |
| 17 | #include <string> |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <linux/fs.h> |
| 26 | #include <linux/fiemap.h> |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 27 | #include <mntent.h> |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 28 | |
| 29 | #define LOG_TAG "secdiscard" |
| 30 | #include "cutils/log.h" |
| 31 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 32 | #include <AutoCloseFD.h> |
| 33 | |
| 34 | namespace { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 35 | // Deliberately limit ourselves to wiping small files. |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 36 | constexpr uint64_t max_wipe_length = 4096; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 37 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 38 | void usage(const char *progname); |
| 39 | int secdiscard_path(const std::string &path); |
| 40 | int path_device_range(const std::string &path, uint64_t range[2]); |
| 41 | std::string block_device_for_path(const std::string &path); |
| 42 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 43 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 44 | int main(int argc, const char * const argv[]) { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 45 | if (argc != 2 || argv[1][0] != '/') { |
| 46 | usage(argv[0]); |
| 47 | return -1; |
| 48 | } |
| 49 | SLOGD("Running: %s %s", argv[0], argv[1]); |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 50 | secdiscard_path(argv[1]); |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 51 | if (unlink(argv[1]) != 0 && errno != ENOENT) { |
| 52 | SLOGE("Unable to delete %s: %s", |
| 53 | argv[1], strerror(errno)); |
| 54 | return -1; |
| 55 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 56 | SLOGD("Discarded %s", argv[1]); |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 60 | namespace { |
| 61 | |
| 62 | void usage(const char *progname) { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 63 | fprintf(stderr, "Usage: %s <absolute path>\n", progname); |
| 64 | } |
| 65 | |
| 66 | // BLKSECDISCARD all content in "path", if it's small enough. |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 67 | int secdiscard_path(const std::string &path) { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 68 | uint64_t range[2]; |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 69 | if (path_device_range(path, range) == -1) { |
| 70 | return -1; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 71 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 72 | auto block_device = block_device_for_path(path); |
| 73 | if (block_device.empty()) { |
| 74 | return -1; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 75 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 76 | AutoCloseFD fs_fd(block_device, O_RDWR | O_LARGEFILE); |
| 77 | if (!fs_fd) { |
| 78 | SLOGE("Failed to open device %s: %s", block_device.c_str(), strerror(errno)); |
| 79 | return -1; |
| 80 | } |
| 81 | if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 82 | SLOGE("Unable to BLKSECDISCARD %s: %s", path.c_str(), strerror(errno)); |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 83 | return -1; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 84 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 85 | return 0; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Find a short range that completely covers the file. |
| 89 | // If there isn't one, return -1, otherwise 0. |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 90 | int path_device_range(const std::string &path, uint64_t range[2]) |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 91 | { |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 92 | AutoCloseFD fd(path); |
| 93 | if (!fd) { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 94 | if (errno == ENOENT) { |
| 95 | SLOGD("Unable to open %s: %s", path.c_str(), strerror(errno)); |
| 96 | } else { |
| 97 | SLOGE("Unable to open %s: %s", path.c_str(), strerror(errno)); |
| 98 | } |
| 99 | return -1; |
| 100 | } |
| 101 | alignas(struct fiemap) char fiemap_buffer[offsetof(struct fiemap, fm_extents[1])]; |
| 102 | memset(fiemap_buffer, 0, sizeof(fiemap_buffer)); |
| 103 | struct fiemap *fiemap = (struct fiemap *)fiemap_buffer; |
| 104 | fiemap->fm_start = 0; |
| 105 | fiemap->fm_length = UINT64_MAX; |
| 106 | fiemap->fm_flags = 0; |
| 107 | fiemap->fm_extent_count = 1; |
| 108 | fiemap->fm_mapped_extents = 0; |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 109 | if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap) != 0) { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 110 | SLOGE("Unable to FIEMAP %s: %s", path.c_str(), strerror(errno)); |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 111 | return -1; |
| 112 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 113 | if (fiemap->fm_mapped_extents != 1) { |
| 114 | SLOGE("Expecting one extent, got %d in %s", fiemap->fm_mapped_extents, path.c_str()); |
| 115 | return -1; |
| 116 | } |
| 117 | struct fiemap_extent *extent = &fiemap->fm_extents[0]; |
| 118 | if (!(extent->fe_flags & FIEMAP_EXTENT_LAST)) { |
| 119 | SLOGE("First extent was not the last in %s", path.c_str()); |
| 120 | return -1; |
| 121 | } |
| 122 | if (extent->fe_flags & |
| 123 | (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) { |
| 124 | SLOGE("Extent has unexpected flags %ulx: %s", extent->fe_flags, path.c_str()); |
| 125 | return -1; |
| 126 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 127 | if (extent->fe_length > max_wipe_length) { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 128 | SLOGE("Extent too big, %llu bytes in %s", extent->fe_length, path.c_str()); |
| 129 | return -1; |
| 130 | } |
| 131 | range[0] = extent->fe_physical; |
| 132 | range[1] = extent->fe_length; |
| 133 | return 0; |
| 134 | } |
| 135 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 136 | // Given a file path, look for the corresponding block device in /proc/mount |
| 137 | std::string block_device_for_path(const std::string &path) |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 138 | { |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 139 | std::unique_ptr<FILE, int(*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent); |
| 140 | if (!mnts) { |
| 141 | SLOGE("Unable to open /proc/mounts: %s", strerror(errno)); |
| 142 | return ""; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 143 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 144 | std::string result; |
| 145 | size_t best_length = 0; |
| 146 | struct mntent *mnt; // getmntent returns a thread local, so it's safe. |
| 147 | while ((mnt = getmntent(mnts.get())) != nullptr) { |
| 148 | auto l = strlen(mnt->mnt_dir); |
| 149 | if (l > best_length && |
| 150 | path.size() > l && |
| 151 | path[l] == '/' && |
| 152 | path.compare(0, l, mnt->mnt_dir) == 0) { |
| 153 | result = mnt->mnt_fsname; |
| 154 | best_length = l; |
| 155 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 156 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 157 | if (result.empty()) { |
| 158 | SLOGE("Didn't find a mountpoint to match path %s", path.c_str()); |
| 159 | return ""; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 160 | } |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame^] | 161 | SLOGD("For path %s block device is %s", path.c_str(), result.c_str()); |
| 162 | return result; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 165 | } |