blob: a335ab6687651768860337101851963fcf822e96 [file] [log] [blame]
Paul Crowley53af81c2015-05-19 17:31:39 +01001/*
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 Crowley28c4df42015-07-07 17:13:17 +010017#include <memory>
Paul Crowley53af81c2015-05-19 17:31:39 +010018#include <string>
Paul Crowley5ab73e92015-07-03 16:17:23 +010019#include <vector>
Paul Crowley53af81c2015-05-19 17:31:39 +010020
21#include <stdio.h>
22#include <stdlib.h>
Paul Crowley53af81c2015-05-19 17:31:39 +010023#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 Crowley4432e732015-07-01 13:33:47 +010029#include <mntent.h>
Paul Crowley53af81c2015-05-19 17:31:39 +010030
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000031#include <android-base/logging.h>
Paul Crowleye4c93da2017-06-16 09:21:18 -070032#include <android-base/unique_fd.h>
Paul Crowley4432e732015-07-01 13:33:47 +010033
34namespace {
Paul Crowley5ab73e92015-07-03 16:17:23 +010035
36struct Options {
37 std::vector<std::string> targets;
38 bool unlink{true};
39};
40
Paul Crowley28c4df42015-07-07 17:13:17 +010041constexpr uint32_t max_extents = 32;
Paul Crowley53af81c2015-05-19 17:31:39 +010042
Paul Crowley5ab73e92015-07-03 16:17:23 +010043bool read_command_line(int argc, const char * const argv[], Options &options);
Paul Crowley4432e732015-07-01 13:33:47 +010044void usage(const char *progname);
Paul Crowley2143ee82016-06-28 14:24:07 -070045bool secdiscard_path(const std::string &path);
Paul Crowley28c4df42015-07-07 17:13:17 +010046std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count);
47bool check_fiemap(const struct fiemap &fiemap, const std::string &path);
48std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count);
Paul Crowley4432e732015-07-01 13:33:47 +010049std::string block_device_for_path(const std::string &path);
Paul Crowley2143ee82016-06-28 14:24:07 -070050bool overwrite_with_zeros(int fd, off64_t start, off64_t length);
Paul Crowley5ab73e92015-07-03 16:17:23 +010051
Paul Crowley4432e732015-07-01 13:33:47 +010052}
Paul Crowley53af81c2015-05-19 17:31:39 +010053
Paul Crowley4432e732015-07-01 13:33:47 +010054int main(int argc, const char * const argv[]) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000055 android::base::InitLogging(const_cast<char **>(argv));
Paul Crowley5ab73e92015-07-03 16:17:23 +010056 Options options;
57 if (!read_command_line(argc, argv, options)) {
Paul Crowley53af81c2015-05-19 17:31:39 +010058 usage(argv[0]);
59 return -1;
60 }
Paul Crowley2143ee82016-06-28 14:24:07 -070061 for (auto const &target: options.targets) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000062 LOG(DEBUG) << "Securely discarding '" << target << "' unlink=" << options.unlink;
Paul Crowley2143ee82016-06-28 14:24:07 -070063 if (!secdiscard_path(target)) {
64 LOG(ERROR) << "Secure discard failed for: " << target;
65 }
Paul Crowley5ab73e92015-07-03 16:17:23 +010066 if (options.unlink) {
67 if (unlink(target.c_str()) != 0 && errno != ENOENT) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000068 PLOG(ERROR) << "Unable to unlink: " << target;
Paul Crowley5ab73e92015-07-03 16:17:23 +010069 }
70 }
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000071 LOG(DEBUG) << "Discarded: " << target;
Paul Crowley53af81c2015-05-19 17:31:39 +010072 }
73 return 0;
74}
75
Paul Crowley4432e732015-07-01 13:33:47 +010076namespace {
77
Paul Crowley5ab73e92015-07-03 16:17:23 +010078bool read_command_line(int argc, const char * const argv[], Options &options) {
79 for (int i = 1; i < argc; i++) {
80 if (!strcmp("--no-unlink", argv[i])) {
81 options.unlink = false;
82 } else if (!strcmp("--", argv[i])) {
83 for (int j = i+1; j < argc; j++) {
84 if (argv[j][0] != '/') return false; // Must be absolute path
85 options.targets.emplace_back(argv[j]);
86 }
87 return options.targets.size() > 0;
88 } else {
89 return false; // Unknown option
90 }
91 }
92 return false; // "--" not found
93}
94
Paul Crowley4432e732015-07-01 13:33:47 +010095void usage(const char *progname) {
Paul Crowley5ab73e92015-07-03 16:17:23 +010096 fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname);
Paul Crowley53af81c2015-05-19 17:31:39 +010097}
98
99// BLKSECDISCARD all content in "path", if it's small enough.
Paul Crowley2143ee82016-06-28 14:24:07 -0700100bool secdiscard_path(const std::string &path) {
Paul Crowley28c4df42015-07-07 17:13:17 +0100101 auto fiemap = path_fiemap(path, max_extents);
102 if (!fiemap || !check_fiemap(*fiemap, path)) {
Paul Crowley2143ee82016-06-28 14:24:07 -0700103 return false;
Paul Crowley53af81c2015-05-19 17:31:39 +0100104 }
Paul Crowley4432e732015-07-01 13:33:47 +0100105 auto block_device = block_device_for_path(path);
106 if (block_device.empty()) {
Paul Crowley2143ee82016-06-28 14:24:07 -0700107 return false;
Paul Crowley53af81c2015-05-19 17:31:39 +0100108 }
Paul Crowleye4c93da2017-06-16 09:21:18 -0700109 android::base::unique_fd fs_fd(TEMP_FAILURE_RETRY(open(
110 block_device.c_str(), O_RDWR | O_LARGEFILE | O_CLOEXEC, 0)));
111 if (fs_fd == -1) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000112 PLOG(ERROR) << "Failed to open device " << block_device;
Paul Crowley2143ee82016-06-28 14:24:07 -0700113 return false;
Paul Crowley4432e732015-07-01 13:33:47 +0100114 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100115 for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) {
116 uint64_t range[2];
117 range[0] = fiemap->fm_extents[i].fe_physical;
118 range[1] = fiemap->fm_extents[i].fe_length;
119 if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000120 PLOG(ERROR) << "Unable to BLKSECDISCARD " << path;
Paul Crowley2143ee82016-06-28 14:24:07 -0700121 if (!overwrite_with_zeros(fs_fd.get(), range[0], range[1])) return false;
122 LOG(DEBUG) << "Used zero overwrite";
Paul Crowley28c4df42015-07-07 17:13:17 +0100123 }
Paul Crowley53af81c2015-05-19 17:31:39 +0100124 }
Paul Crowley2143ee82016-06-28 14:24:07 -0700125 return true;
Paul Crowley53af81c2015-05-19 17:31:39 +0100126}
127
Paul Crowley28c4df42015-07-07 17:13:17 +0100128// Read the file's FIEMAP
129std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count)
Paul Crowley53af81c2015-05-19 17:31:39 +0100130{
Paul Crowleye4c93da2017-06-16 09:21:18 -0700131 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(
132 path.c_str(), O_RDONLY | O_CLOEXEC, 0)));
133 if (fd == -1) {
Paul Crowley53af81c2015-05-19 17:31:39 +0100134 if (errno == ENOENT) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000135 PLOG(DEBUG) << "Unable to open " << path;
Paul Crowley53af81c2015-05-19 17:31:39 +0100136 } else {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000137 PLOG(ERROR) << "Unable to open " << path;
Paul Crowley53af81c2015-05-19 17:31:39 +0100138 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100139 return nullptr;
Paul Crowley53af81c2015-05-19 17:31:39 +0100140 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100141 auto fiemap = alloc_fiemap(extent_count);
142 if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap.get()) != 0) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000143 PLOG(ERROR) << "Unable to FIEMAP " << path;
Paul Crowley28c4df42015-07-07 17:13:17 +0100144 return nullptr;
Paul Crowley53af81c2015-05-19 17:31:39 +0100145 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100146 auto mapped = fiemap->fm_mapped_extents;
147 if (mapped < 1 || mapped > extent_count) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000148 LOG(ERROR) << "Extent count not in bounds 1 <= " << mapped << " <= " << extent_count
149 << " in " << path;
Paul Crowley28c4df42015-07-07 17:13:17 +0100150 return nullptr;
Paul Crowley53af81c2015-05-19 17:31:39 +0100151 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100152 return fiemap;
153}
154
155// Ensure that the FIEMAP covers the file and is OK to discard
156bool check_fiemap(const struct fiemap &fiemap, const std::string &path) {
157 auto mapped = fiemap.fm_mapped_extents;
158 if (!(fiemap.fm_extents[mapped - 1].fe_flags & FIEMAP_EXTENT_LAST)) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000159 LOG(ERROR) << "Extent " << mapped -1 << " was not the last in " << path;
Paul Crowley28c4df42015-07-07 17:13:17 +0100160 return false;
Paul Crowley53af81c2015-05-19 17:31:39 +0100161 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100162 for (uint32_t i = 0; i < mapped; i++) {
163 auto flags = fiemap.fm_extents[i].fe_flags;
164 if (flags & (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000165 LOG(ERROR) << "Extent " << i << " has unexpected flags " << flags << ": " << path;
Paul Crowley28c4df42015-07-07 17:13:17 +0100166 return false;
167 }
Paul Crowley53af81c2015-05-19 17:31:39 +0100168 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100169 return true;
170}
171
172std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count)
173{
174 size_t allocsize = offsetof(struct fiemap, fm_extents[extent_count]);
175 std::unique_ptr<struct fiemap> res(new (::operator new (allocsize)) struct fiemap);
176 memset(res.get(), 0, allocsize);
177 res->fm_start = 0;
178 res->fm_length = UINT64_MAX;
179 res->fm_flags = 0;
180 res->fm_extent_count = extent_count;
181 res->fm_mapped_extents = 0;
182 return res;
Paul Crowley53af81c2015-05-19 17:31:39 +0100183}
184
Paul Crowley4432e732015-07-01 13:33:47 +0100185// Given a file path, look for the corresponding block device in /proc/mount
186std::string block_device_for_path(const std::string &path)
Paul Crowley53af81c2015-05-19 17:31:39 +0100187{
Paul Crowley4432e732015-07-01 13:33:47 +0100188 std::unique_ptr<FILE, int(*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent);
189 if (!mnts) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000190 PLOG(ERROR) << "Unable to open /proc/mounts";
Paul Crowley4432e732015-07-01 13:33:47 +0100191 return "";
Paul Crowley53af81c2015-05-19 17:31:39 +0100192 }
Paul Crowley4432e732015-07-01 13:33:47 +0100193 std::string result;
194 size_t best_length = 0;
195 struct mntent *mnt; // getmntent returns a thread local, so it's safe.
196 while ((mnt = getmntent(mnts.get())) != nullptr) {
197 auto l = strlen(mnt->mnt_dir);
198 if (l > best_length &&
199 path.size() > l &&
200 path[l] == '/' &&
201 path.compare(0, l, mnt->mnt_dir) == 0) {
202 result = mnt->mnt_fsname;
203 best_length = l;
204 }
Paul Crowley53af81c2015-05-19 17:31:39 +0100205 }
Paul Crowley4432e732015-07-01 13:33:47 +0100206 if (result.empty()) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000207 LOG(ERROR) <<"Didn't find a mountpoint to match path " << path;
Paul Crowley4432e732015-07-01 13:33:47 +0100208 return "";
Paul Crowley53af81c2015-05-19 17:31:39 +0100209 }
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000210 LOG(DEBUG) << "For path " << path << " block device is " << result;
Paul Crowley4432e732015-07-01 13:33:47 +0100211 return result;
Paul Crowley53af81c2015-05-19 17:31:39 +0100212}
213
Paul Crowley2143ee82016-06-28 14:24:07 -0700214bool overwrite_with_zeros(int fd, off64_t start, off64_t length) {
215 if (lseek64(fd, start, SEEK_SET) != start) {
216 PLOG(ERROR) << "Seek failed for zero overwrite";
217 return false;
218 }
219 char buf[BUFSIZ];
220 memset(buf, 0, sizeof(buf));
221 while (length > 0) {
222 size_t wlen = static_cast<size_t>(std::min(static_cast<off64_t>(sizeof(buf)), length));
223 auto written = write(fd, buf, wlen);
224 if (written < 1) {
225 PLOG(ERROR) << "Write of zeroes failed";
226 return false;
227 }
228 length -= written;
229 }
230 return true;
231}
232
Paul Crowley53af81c2015-05-19 17:31:39 +0100233}