blob: a7500439b61bd569405822bfb96951f44fb30aad [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
17#include <string>
18
19#include <stdio.h>
20#include <stdlib.h>
Paul Crowley53af81c2015-05-19 17:31:39 +010021#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 Crowley4432e732015-07-01 13:33:47 +010027#include <mntent.h>
Paul Crowley53af81c2015-05-19 17:31:39 +010028
29#define LOG_TAG "secdiscard"
30#include "cutils/log.h"
31
Paul Crowley4432e732015-07-01 13:33:47 +010032#include <AutoCloseFD.h>
33
34namespace {
Paul Crowley53af81c2015-05-19 17:31:39 +010035// Deliberately limit ourselves to wiping small files.
Paul Crowley4432e732015-07-01 13:33:47 +010036constexpr uint64_t max_wipe_length = 4096;
Paul Crowley53af81c2015-05-19 17:31:39 +010037
Paul Crowley4432e732015-07-01 13:33:47 +010038void usage(const char *progname);
39int secdiscard_path(const std::string &path);
40int path_device_range(const std::string &path, uint64_t range[2]);
41std::string block_device_for_path(const std::string &path);
42}
Paul Crowley53af81c2015-05-19 17:31:39 +010043
Paul Crowley4432e732015-07-01 13:33:47 +010044int main(int argc, const char * const argv[]) {
Paul Crowley53af81c2015-05-19 17:31:39 +010045 if (argc != 2 || argv[1][0] != '/') {
46 usage(argv[0]);
47 return -1;
48 }
49 SLOGD("Running: %s %s", argv[0], argv[1]);
Paul Crowley4432e732015-07-01 13:33:47 +010050 secdiscard_path(argv[1]);
Paul Crowley53af81c2015-05-19 17:31:39 +010051 if (unlink(argv[1]) != 0 && errno != ENOENT) {
52 SLOGE("Unable to delete %s: %s",
53 argv[1], strerror(errno));
54 return -1;
55 }
Paul Crowley4432e732015-07-01 13:33:47 +010056 SLOGD("Discarded %s", argv[1]);
Paul Crowley53af81c2015-05-19 17:31:39 +010057 return 0;
58}
59
Paul Crowley4432e732015-07-01 13:33:47 +010060namespace {
61
62void usage(const char *progname) {
Paul Crowley53af81c2015-05-19 17:31:39 +010063 fprintf(stderr, "Usage: %s <absolute path>\n", progname);
64}
65
66// BLKSECDISCARD all content in "path", if it's small enough.
Paul Crowley4432e732015-07-01 13:33:47 +010067int secdiscard_path(const std::string &path) {
Paul Crowley53af81c2015-05-19 17:31:39 +010068 uint64_t range[2];
Paul Crowley4432e732015-07-01 13:33:47 +010069 if (path_device_range(path, range) == -1) {
70 return -1;
Paul Crowley53af81c2015-05-19 17:31:39 +010071 }
Paul Crowley4432e732015-07-01 13:33:47 +010072 auto block_device = block_device_for_path(path);
73 if (block_device.empty()) {
74 return -1;
Paul Crowley53af81c2015-05-19 17:31:39 +010075 }
Paul Crowley4432e732015-07-01 13:33:47 +010076 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 Crowley53af81c2015-05-19 17:31:39 +010082 SLOGE("Unable to BLKSECDISCARD %s: %s", path.c_str(), strerror(errno));
Paul Crowley4432e732015-07-01 13:33:47 +010083 return -1;
Paul Crowley53af81c2015-05-19 17:31:39 +010084 }
Paul Crowley4432e732015-07-01 13:33:47 +010085 return 0;
Paul Crowley53af81c2015-05-19 17:31:39 +010086}
87
88// Find a short range that completely covers the file.
89// If there isn't one, return -1, otherwise 0.
Paul Crowley4432e732015-07-01 13:33:47 +010090int path_device_range(const std::string &path, uint64_t range[2])
Paul Crowley53af81c2015-05-19 17:31:39 +010091{
Paul Crowley4432e732015-07-01 13:33:47 +010092 AutoCloseFD fd(path);
93 if (!fd) {
Paul Crowley53af81c2015-05-19 17:31:39 +010094 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 Crowley4432e732015-07-01 13:33:47 +0100109 if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap) != 0) {
Paul Crowley53af81c2015-05-19 17:31:39 +0100110 SLOGE("Unable to FIEMAP %s: %s", path.c_str(), strerror(errno));
Paul Crowley53af81c2015-05-19 17:31:39 +0100111 return -1;
112 }
Paul Crowley53af81c2015-05-19 17:31:39 +0100113 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 Crowley4432e732015-07-01 13:33:47 +0100127 if (extent->fe_length > max_wipe_length) {
Paul Crowley53af81c2015-05-19 17:31:39 +0100128 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 Crowley4432e732015-07-01 13:33:47 +0100136// Given a file path, look for the corresponding block device in /proc/mount
137std::string block_device_for_path(const std::string &path)
Paul Crowley53af81c2015-05-19 17:31:39 +0100138{
Paul Crowley4432e732015-07-01 13:33:47 +0100139 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 Crowley53af81c2015-05-19 17:31:39 +0100143 }
Paul Crowley4432e732015-07-01 13:33:47 +0100144 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 Crowley53af81c2015-05-19 17:31:39 +0100156 }
Paul Crowley4432e732015-07-01 13:33:47 +0100157 if (result.empty()) {
158 SLOGE("Didn't find a mountpoint to match path %s", path.c_str());
159 return "";
Paul Crowley53af81c2015-05-19 17:31:39 +0100160 }
Paul Crowley4432e732015-07-01 13:33:47 +0100161 SLOGD("For path %s block device is %s", path.c_str(), result.c_str());
162 return result;
Paul Crowley53af81c2015-05-19 17:31:39 +0100163}
164
Paul Crowley53af81c2015-05-19 17:31:39 +0100165}