blob: 5c12cdd03bd14cf9a724206e2baf1a94f0b19d51 [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 Crowley53af81c2015-05-19 17:31:39 +010032
Paul Crowley4432e732015-07-01 13:33:47 +010033#include <AutoCloseFD.h>
34
35namespace {
Paul Crowley5ab73e92015-07-03 16:17:23 +010036
37struct Options {
38 std::vector<std::string> targets;
39 bool unlink{true};
40};
41
Paul Crowley28c4df42015-07-07 17:13:17 +010042constexpr uint32_t max_extents = 32;
Paul Crowley53af81c2015-05-19 17:31:39 +010043
Paul Crowley5ab73e92015-07-03 16:17:23 +010044bool read_command_line(int argc, const char * const argv[], Options &options);
Paul Crowley4432e732015-07-01 13:33:47 +010045void usage(const char *progname);
46int secdiscard_path(const std::string &path);
Paul Crowley28c4df42015-07-07 17:13:17 +010047std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count);
48bool check_fiemap(const struct fiemap &fiemap, const std::string &path);
49std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count);
Paul Crowley4432e732015-07-01 13:33:47 +010050std::string block_device_for_path(const std::string &path);
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 Crowley5ab73e92015-07-03 16:17:23 +010061 for (auto target: options.targets) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000062 LOG(DEBUG) << "Securely discarding '" << target << "' unlink=" << options.unlink;
Paul Crowley5ab73e92015-07-03 16:17:23 +010063 secdiscard_path(target);
64 if (options.unlink) {
65 if (unlink(target.c_str()) != 0 && errno != ENOENT) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000066 PLOG(ERROR) << "Unable to unlink: " << target;
Paul Crowley5ab73e92015-07-03 16:17:23 +010067 }
68 }
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000069 LOG(DEBUG) << "Discarded: " << target;
Paul Crowley53af81c2015-05-19 17:31:39 +010070 }
71 return 0;
72}
73
Paul Crowley4432e732015-07-01 13:33:47 +010074namespace {
75
Paul Crowley5ab73e92015-07-03 16:17:23 +010076bool 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 Crowley4432e732015-07-01 13:33:47 +010093void usage(const char *progname) {
Paul Crowley5ab73e92015-07-03 16:17:23 +010094 fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname);
Paul Crowley53af81c2015-05-19 17:31:39 +010095}
96
97// BLKSECDISCARD all content in "path", if it's small enough.
Paul Crowley4432e732015-07-01 13:33:47 +010098int secdiscard_path(const std::string &path) {
Paul Crowley28c4df42015-07-07 17:13:17 +010099 auto fiemap = path_fiemap(path, max_extents);
100 if (!fiemap || !check_fiemap(*fiemap, path)) {
Paul Crowley4432e732015-07-01 13:33:47 +0100101 return -1;
Paul Crowley53af81c2015-05-19 17:31:39 +0100102 }
Paul Crowley4432e732015-07-01 13:33:47 +0100103 auto block_device = block_device_for_path(path);
104 if (block_device.empty()) {
105 return -1;
Paul Crowley53af81c2015-05-19 17:31:39 +0100106 }
Paul Crowley4432e732015-07-01 13:33:47 +0100107 AutoCloseFD fs_fd(block_device, O_RDWR | O_LARGEFILE);
108 if (!fs_fd) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000109 PLOG(ERROR) << "Failed to open device " << block_device;
Paul Crowley4432e732015-07-01 13:33:47 +0100110 return -1;
111 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100112 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 Crowley8bb8fcf2016-01-11 12:26:44 +0000117 PLOG(ERROR) << "Unable to BLKSECDISCARD " << path;
Paul Crowley28c4df42015-07-07 17:13:17 +0100118 return -1;
119 }
Paul Crowley53af81c2015-05-19 17:31:39 +0100120 }
Paul Crowley4432e732015-07-01 13:33:47 +0100121 return 0;
Paul Crowley53af81c2015-05-19 17:31:39 +0100122}
123
Paul Crowley28c4df42015-07-07 17:13:17 +0100124// Read the file's FIEMAP
125std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count)
Paul Crowley53af81c2015-05-19 17:31:39 +0100126{
Paul Crowley4432e732015-07-01 13:33:47 +0100127 AutoCloseFD fd(path);
128 if (!fd) {
Paul Crowley53af81c2015-05-19 17:31:39 +0100129 if (errno == ENOENT) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000130 PLOG(DEBUG) << "Unable to open " << path;
Paul Crowley53af81c2015-05-19 17:31:39 +0100131 } else {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000132 PLOG(ERROR) << "Unable to open " << path;
Paul Crowley53af81c2015-05-19 17:31:39 +0100133 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100134 return nullptr;
Paul Crowley53af81c2015-05-19 17:31:39 +0100135 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100136 auto fiemap = alloc_fiemap(extent_count);
137 if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap.get()) != 0) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000138 PLOG(ERROR) << "Unable to FIEMAP " << path;
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 mapped = fiemap->fm_mapped_extents;
142 if (mapped < 1 || mapped > extent_count) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000143 LOG(ERROR) << "Extent count not in bounds 1 <= " << mapped << " <= " << extent_count
144 << " in " << path;
Paul Crowley28c4df42015-07-07 17:13:17 +0100145 return nullptr;
Paul Crowley53af81c2015-05-19 17:31:39 +0100146 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100147 return fiemap;
148}
149
150// Ensure that the FIEMAP covers the file and is OK to discard
151bool 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 Crowley8bb8fcf2016-01-11 12:26:44 +0000154 LOG(ERROR) << "Extent " << mapped -1 << " was not the last in " << path;
Paul Crowley28c4df42015-07-07 17:13:17 +0100155 return false;
Paul Crowley53af81c2015-05-19 17:31:39 +0100156 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100157 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 Crowley8bb8fcf2016-01-11 12:26:44 +0000160 LOG(ERROR) << "Extent " << i << " has unexpected flags " << flags << ": " << path;
Paul Crowley28c4df42015-07-07 17:13:17 +0100161 return false;
162 }
Paul Crowley53af81c2015-05-19 17:31:39 +0100163 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100164 return true;
165}
166
167std::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 Crowley53af81c2015-05-19 17:31:39 +0100178}
179
Paul Crowley4432e732015-07-01 13:33:47 +0100180// Given a file path, look for the corresponding block device in /proc/mount
181std::string block_device_for_path(const std::string &path)
Paul Crowley53af81c2015-05-19 17:31:39 +0100182{
Paul Crowley4432e732015-07-01 13:33:47 +0100183 std::unique_ptr<FILE, int(*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent);
184 if (!mnts) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000185 PLOG(ERROR) << "Unable to open /proc/mounts";
Paul Crowley4432e732015-07-01 13:33:47 +0100186 return "";
Paul Crowley53af81c2015-05-19 17:31:39 +0100187 }
Paul Crowley4432e732015-07-01 13:33:47 +0100188 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 Crowley53af81c2015-05-19 17:31:39 +0100200 }
Paul Crowley4432e732015-07-01 13:33:47 +0100201 if (result.empty()) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000202 LOG(ERROR) <<"Didn't find a mountpoint to match path " << path;
Paul Crowley4432e732015-07-01 13:33:47 +0100203 return "";
Paul Crowley53af81c2015-05-19 17:31:39 +0100204 }
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000205 LOG(DEBUG) << "For path " << path << " block device is " << result;
Paul Crowley4432e732015-07-01 13:33:47 +0100206 return result;
Paul Crowley53af81c2015-05-19 17:31:39 +0100207}
208
Paul Crowley53af81c2015-05-19 17:31:39 +0100209}