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> |
| 21 | #include <string.h> |
| 22 | #include <errno.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <linux/fs.h> |
| 27 | #include <linux/fiemap.h> |
| 28 | |
| 29 | #define LOG_TAG "secdiscard" |
| 30 | #include "cutils/log.h" |
| 31 | |
| 32 | // Deliberately limit ourselves to wiping small files. |
| 33 | #define MAX_WIPE_LENGTH 4096 |
| 34 | #define INIT_BUFFER_SIZE 2048 |
| 35 | |
| 36 | static void usage(char *progname); |
| 37 | static void destroy_key(const std::string &path); |
| 38 | static int file_device_range(const std::string &path, uint64_t range[2]); |
| 39 | static int open_block_device_for_path(const std::string &path); |
| 40 | static int read_file_as_string_atomically(const std::string &path, std::string &contents); |
| 41 | static int find_block_device_for_path( |
| 42 | const std::string &mounts, |
| 43 | const std::string &path, |
| 44 | std::string &block_device); |
| 45 | |
| 46 | int main(int argc, char **argv) { |
| 47 | if (argc != 2 || argv[1][0] != '/') { |
| 48 | usage(argv[0]); |
| 49 | return -1; |
| 50 | } |
| 51 | SLOGD("Running: %s %s", argv[0], argv[1]); |
| 52 | std::string target(argv[1]); |
| 53 | destroy_key(target); |
| 54 | if (unlink(argv[1]) != 0 && errno != ENOENT) { |
| 55 | SLOGE("Unable to delete %s: %s", |
| 56 | argv[1], strerror(errno)); |
| 57 | return -1; |
| 58 | } |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static void usage(char *progname) { |
| 63 | fprintf(stderr, "Usage: %s <absolute path>\n", progname); |
| 64 | } |
| 65 | |
| 66 | // BLKSECDISCARD all content in "path", if it's small enough. |
| 67 | static void destroy_key(const std::string &path) { |
| 68 | uint64_t range[2]; |
| 69 | if (file_device_range(path, range) < 0) { |
| 70 | return; |
| 71 | } |
| 72 | int fs_fd = open_block_device_for_path(path); |
| 73 | if (fs_fd < 0) { |
| 74 | return; |
| 75 | } |
| 76 | if (ioctl(fs_fd, BLKSECDISCARD, range) != 0) { |
| 77 | SLOGE("Unable to BLKSECDISCARD %s: %s", path.c_str(), strerror(errno)); |
| 78 | close(fs_fd); |
| 79 | return; |
| 80 | } |
| 81 | close(fs_fd); |
| 82 | SLOGD("Discarded %s", path.c_str()); |
| 83 | } |
| 84 | |
| 85 | // Find a short range that completely covers the file. |
| 86 | // If there isn't one, return -1, otherwise 0. |
| 87 | static int file_device_range(const std::string &path, uint64_t range[2]) |
| 88 | { |
| 89 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); |
| 90 | if (fd < 0) { |
| 91 | if (errno == ENOENT) { |
| 92 | SLOGD("Unable to open %s: %s", path.c_str(), strerror(errno)); |
| 93 | } else { |
| 94 | SLOGE("Unable to open %s: %s", path.c_str(), strerror(errno)); |
| 95 | } |
| 96 | return -1; |
| 97 | } |
| 98 | alignas(struct fiemap) char fiemap_buffer[offsetof(struct fiemap, fm_extents[1])]; |
| 99 | memset(fiemap_buffer, 0, sizeof(fiemap_buffer)); |
| 100 | struct fiemap *fiemap = (struct fiemap *)fiemap_buffer; |
| 101 | fiemap->fm_start = 0; |
| 102 | fiemap->fm_length = UINT64_MAX; |
| 103 | fiemap->fm_flags = 0; |
| 104 | fiemap->fm_extent_count = 1; |
| 105 | fiemap->fm_mapped_extents = 0; |
| 106 | if (ioctl(fd, FS_IOC_FIEMAP, fiemap) != 0) { |
| 107 | SLOGE("Unable to FIEMAP %s: %s", path.c_str(), strerror(errno)); |
| 108 | close(fd); |
| 109 | return -1; |
| 110 | } |
| 111 | close(fd); |
| 112 | if (fiemap->fm_mapped_extents != 1) { |
| 113 | SLOGE("Expecting one extent, got %d in %s", fiemap->fm_mapped_extents, path.c_str()); |
| 114 | return -1; |
| 115 | } |
| 116 | struct fiemap_extent *extent = &fiemap->fm_extents[0]; |
| 117 | if (!(extent->fe_flags & FIEMAP_EXTENT_LAST)) { |
| 118 | SLOGE("First extent was not the last in %s", path.c_str()); |
| 119 | return -1; |
| 120 | } |
| 121 | if (extent->fe_flags & |
| 122 | (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) { |
| 123 | SLOGE("Extent has unexpected flags %ulx: %s", extent->fe_flags, path.c_str()); |
| 124 | return -1; |
| 125 | } |
| 126 | if (extent->fe_length > MAX_WIPE_LENGTH) { |
| 127 | SLOGE("Extent too big, %llu bytes in %s", extent->fe_length, path.c_str()); |
| 128 | return -1; |
| 129 | } |
| 130 | range[0] = extent->fe_physical; |
| 131 | range[1] = extent->fe_length; |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | // Given a file path, look for the corresponding |
| 136 | // block device in /proc/mounts and open it. |
| 137 | static int open_block_device_for_path(const std::string &path) |
| 138 | { |
| 139 | std::string mountsfile("/proc/mounts"); |
| 140 | std::string mounts; |
| 141 | if (read_file_as_string_atomically(mountsfile, mounts) < 0) { |
| 142 | return -1; |
| 143 | } |
| 144 | std::string block_device; |
| 145 | if (find_block_device_for_path(mounts, path, block_device) < 0) { |
| 146 | return -1; |
| 147 | } |
| 148 | SLOGD("For path %s block device is %s", path.c_str(), block_device.c_str()); |
| 149 | int res = open(block_device.c_str(), O_RDWR | O_LARGEFILE | O_CLOEXEC); |
| 150 | if (res < 0) { |
| 151 | SLOGE("Failed to open device %s: %s", block_device.c_str(), strerror(errno)); |
| 152 | return -1; |
| 153 | } |
| 154 | return res; |
| 155 | } |
| 156 | |
| 157 | // Read a file into a buffer in a single gulp, for atomicity. |
| 158 | // Null-terminate the buffer. |
| 159 | // Retry until the buffer is big enough. |
| 160 | static int read_file_as_string_atomically(const std::string &path, std::string &contents) |
| 161 | { |
| 162 | ssize_t buffer_size = INIT_BUFFER_SIZE; |
| 163 | while (true) { |
| 164 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); |
| 165 | if (fd < 0) { |
| 166 | SLOGE("Failed to open %s: %s", path.c_str(), strerror(errno)); |
| 167 | return -1; |
| 168 | } |
| 169 | contents.resize(buffer_size); |
| 170 | ssize_t read_size = read(fd, &contents[0], buffer_size); |
| 171 | if (read_size < 0) { |
| 172 | SLOGE("Failed to read from %s: %s", path.c_str(), strerror(errno)); |
| 173 | close(fd); |
| 174 | return -1; |
| 175 | } |
| 176 | close(fd); |
| 177 | if (read_size < buffer_size) { |
| 178 | contents.resize(read_size); |
| 179 | return 0; |
| 180 | } |
| 181 | SLOGD("%s too big for buffer of size %zu", path.c_str(), buffer_size); |
| 182 | buffer_size <<= 1; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // Search a string representing result from /proc/mounts |
| 187 | // for the mount point of a particular file by prefix matching |
| 188 | // and return the corresponding block device as a |
| 189 | // pointer into the mounts string. |
| 190 | // This destroys the mounts string. |
| 191 | static int find_block_device_for_path( |
| 192 | const std::string &mounts, |
| 193 | const std::string &path, |
| 194 | std::string &block_device) |
| 195 | { |
| 196 | auto line_begin = mounts.begin(); |
| 197 | size_t best_prefix = 0; |
| 198 | std::string::const_iterator line_end; |
| 199 | while (line_begin != mounts.end()) { |
| 200 | line_end = std::find(line_begin, mounts.end(), '\n'); |
| 201 | if (line_end == mounts.end()) { |
| 202 | break; |
| 203 | } |
| 204 | auto device_end = std::find(line_begin, line_end, ' ');; |
| 205 | if (device_end == line_end) { |
| 206 | break; |
| 207 | } |
| 208 | auto mountpoint_begin = device_end +1; |
| 209 | auto mountpoint_end = std::find(mountpoint_begin, line_end, ' '); |
| 210 | if (mountpoint_end == line_end) { |
| 211 | break; |
| 212 | } |
| 213 | if (std::find(line_begin, mountpoint_end, '\\') != mountpoint_end) { |
| 214 | // We don't correctly handle escape sequences |
| 215 | // and we don't expect to encounter any |
| 216 | // so fail if we do. |
| 217 | break; |
| 218 | } |
| 219 | size_t mountpoint_len = mountpoint_end - mountpoint_begin; |
| 220 | if (mountpoint_len > best_prefix && |
| 221 | mountpoint_len < path.length() && |
| 222 | path[mountpoint_len] == '/' && |
| 223 | std::equal(mountpoint_begin, mountpoint_end, path.begin())) { |
| 224 | block_device = std::string(line_begin, device_end); |
| 225 | best_prefix = mountpoint_len; |
| 226 | } |
| 227 | line_begin = line_end +1; |
| 228 | } |
| 229 | // All of the "break"s above are fatal parse errors. |
| 230 | if (line_begin != mounts.end()) { |
| 231 | SLOGE("Unable to parse line in %s: %s", |
| 232 | path.c_str(), std::string(line_begin, line_end).c_str()); |
| 233 | return -1; |
| 234 | } |
| 235 | if (best_prefix == 0) { |
| 236 | SLOGE("No prefix found for path: %s", path.c_str()); |
| 237 | return -1; |
| 238 | } |
| 239 | return 0; |
| 240 | } |