blob: f7adb0df66dcc78563db248e64ec3d917e00b056 [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
31#define LOG_TAG "secdiscard"
32#include "cutils/log.h"
33
Paul Crowley4432e732015-07-01 13:33:47 +010034#include <AutoCloseFD.h>
35
36namespace {
Paul Crowley5ab73e92015-07-03 16:17:23 +010037
38struct Options {
39 std::vector<std::string> targets;
40 bool unlink{true};
41};
42
Paul Crowley28c4df42015-07-07 17:13:17 +010043constexpr uint32_t max_extents = 32;
Paul Crowley53af81c2015-05-19 17:31:39 +010044
Paul Crowley5ab73e92015-07-03 16:17:23 +010045bool read_command_line(int argc, const char * const argv[], Options &options);
Paul Crowley4432e732015-07-01 13:33:47 +010046void usage(const char *progname);
47int secdiscard_path(const std::string &path);
Paul Crowley28c4df42015-07-07 17:13:17 +010048std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count);
49bool check_fiemap(const struct fiemap &fiemap, const std::string &path);
50std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count);
Paul Crowley4432e732015-07-01 13:33:47 +010051std::string block_device_for_path(const std::string &path);
Paul Crowley5ab73e92015-07-03 16:17:23 +010052
Paul Crowley4432e732015-07-01 13:33:47 +010053}
Paul Crowley53af81c2015-05-19 17:31:39 +010054
Paul Crowley4432e732015-07-01 13:33:47 +010055int main(int argc, const char * const 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) {
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 Crowley53af81c2015-05-19 17:31:39 +010071 }
72 return 0;
73}
74
Paul Crowley4432e732015-07-01 13:33:47 +010075namespace {
76
Paul Crowley5ab73e92015-07-03 16:17:23 +010077bool 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 Crowley4432e732015-07-01 13:33:47 +010094void usage(const char *progname) {
Paul Crowley5ab73e92015-07-03 16:17:23 +010095 fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname);
Paul Crowley53af81c2015-05-19 17:31:39 +010096}
97
98// BLKSECDISCARD all content in "path", if it's small enough.
Paul Crowley4432e732015-07-01 13:33:47 +010099int secdiscard_path(const std::string &path) {
Paul Crowley28c4df42015-07-07 17:13:17 +0100100 auto fiemap = path_fiemap(path, max_extents);
101 if (!fiemap || !check_fiemap(*fiemap, path)) {
Paul Crowley4432e732015-07-01 13:33:47 +0100102 return -1;
Paul Crowley53af81c2015-05-19 17:31:39 +0100103 }
Paul Crowley4432e732015-07-01 13:33:47 +0100104 auto block_device = block_device_for_path(path);
105 if (block_device.empty()) {
106 return -1;
Paul Crowley53af81c2015-05-19 17:31:39 +0100107 }
Paul Crowley4432e732015-07-01 13:33:47 +0100108 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 Crowley28c4df42015-07-07 17:13:17 +0100113 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 Crowley53af81c2015-05-19 17:31:39 +0100121 }
Paul Crowley4432e732015-07-01 13:33:47 +0100122 return 0;
Paul Crowley53af81c2015-05-19 17:31:39 +0100123}
124
Paul Crowley28c4df42015-07-07 17:13:17 +0100125// Read the file's FIEMAP
126std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count)
Paul Crowley53af81c2015-05-19 17:31:39 +0100127{
Paul Crowley4432e732015-07-01 13:33:47 +0100128 AutoCloseFD fd(path);
129 if (!fd) {
Paul Crowley53af81c2015-05-19 17:31:39 +0100130 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 Crowley28c4df42015-07-07 17:13:17 +0100135 return nullptr;
Paul Crowley53af81c2015-05-19 17:31:39 +0100136 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100137 auto fiemap = alloc_fiemap(extent_count);
138 if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap.get()) != 0) {
Paul Crowley53af81c2015-05-19 17:31:39 +0100139 SLOGE("Unable to FIEMAP %s: %s", path.c_str(), strerror(errno));
Paul Crowley28c4df42015-07-07 17:13:17 +0100140 return nullptr;
Paul Crowley53af81c2015-05-19 17:31:39 +0100141 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100142 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 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)) {
154 SLOGE("Extent %u was not the last in %s", mapped - 1, path.c_str());
155 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)) {
160 SLOGE("Extent %u has unexpected flags %ulx: %s", i, flags, path.c_str());
161 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) {
185 SLOGE("Unable to open /proc/mounts: %s", strerror(errno));
186 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()) {
202 SLOGE("Didn't find a mountpoint to match path %s", path.c_str());
203 return "";
Paul Crowley53af81c2015-05-19 17:31:39 +0100204 }
Paul Crowley4432e732015-07-01 13:33:47 +0100205 SLOGD("For path %s block device is %s", path.c_str(), result.c_str());
206 return result;
Paul Crowley53af81c2015-05-19 17:31:39 +0100207}
208
Paul Crowley53af81c2015-05-19 17:31:39 +0100209}