blob: f9532ea5feec0c240626a52ea0b70e05ae2230cf [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
Paul Crowley03f89d32017-06-16 09:37:31 -070034#include "FileDeviceUtils.h"
35
Paul Crowley4432e732015-07-01 13:33:47 +010036namespace {
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);
Paul Crowley2143ee82016-06-28 14:24:07 -070047bool secdiscard_path(const std::string &path);
Paul Crowley28c4df42015-07-07 17:13:17 +010048bool check_fiemap(const struct fiemap &fiemap, const std::string &path);
Paul Crowley2143ee82016-06-28 14:24:07 -070049bool overwrite_with_zeros(int fd, off64_t start, off64_t length);
Paul Crowley5ab73e92015-07-03 16:17:23 +010050
Paul Crowley4432e732015-07-01 13:33:47 +010051}
Paul Crowley53af81c2015-05-19 17:31:39 +010052
Paul Crowley4432e732015-07-01 13:33:47 +010053int main(int argc, const char * const argv[]) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000054 android::base::InitLogging(const_cast<char **>(argv));
Paul Crowley5ab73e92015-07-03 16:17:23 +010055 Options options;
56 if (!read_command_line(argc, argv, options)) {
Paul Crowley53af81c2015-05-19 17:31:39 +010057 usage(argv[0]);
58 return -1;
59 }
Paul Crowley2143ee82016-06-28 14:24:07 -070060 for (auto const &target: options.targets) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000061 LOG(DEBUG) << "Securely discarding '" << target << "' unlink=" << options.unlink;
Paul Crowley2143ee82016-06-28 14:24:07 -070062 if (!secdiscard_path(target)) {
63 LOG(ERROR) << "Secure discard failed for: " << target;
64 }
Paul Crowley5ab73e92015-07-03 16:17:23 +010065 if (options.unlink) {
66 if (unlink(target.c_str()) != 0 && errno != ENOENT) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000067 PLOG(ERROR) << "Unable to unlink: " << target;
Paul Crowley5ab73e92015-07-03 16:17:23 +010068 }
69 }
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000070 LOG(DEBUG) << "Discarded: " << target;
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 Crowley2143ee82016-06-28 14:24:07 -070099bool secdiscard_path(const std::string &path) {
Paul Crowley03f89d32017-06-16 09:37:31 -0700100 auto fiemap = android::vold::PathFiemap(path, max_extents);
Paul Crowley28c4df42015-07-07 17:13:17 +0100101 if (!fiemap || !check_fiemap(*fiemap, path)) {
Paul Crowley2143ee82016-06-28 14:24:07 -0700102 return false;
Paul Crowley53af81c2015-05-19 17:31:39 +0100103 }
Paul Crowley03f89d32017-06-16 09:37:31 -0700104 auto block_device = android::vold::BlockDeviceForPath(path);
Paul Crowley4432e732015-07-01 13:33:47 +0100105 if (block_device.empty()) {
Paul Crowley2143ee82016-06-28 14:24:07 -0700106 return false;
Paul Crowley53af81c2015-05-19 17:31:39 +0100107 }
Paul Crowleye4c93da2017-06-16 09:21:18 -0700108 android::base::unique_fd fs_fd(TEMP_FAILURE_RETRY(open(
109 block_device.c_str(), O_RDWR | O_LARGEFILE | O_CLOEXEC, 0)));
110 if (fs_fd == -1) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000111 PLOG(ERROR) << "Failed to open device " << block_device;
Paul Crowley2143ee82016-06-28 14:24:07 -0700112 return false;
Paul Crowley4432e732015-07-01 13:33:47 +0100113 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100114 for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) {
115 uint64_t range[2];
116 range[0] = fiemap->fm_extents[i].fe_physical;
117 range[1] = fiemap->fm_extents[i].fe_length;
118 if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000119 PLOG(ERROR) << "Unable to BLKSECDISCARD " << path;
Paul Crowley2143ee82016-06-28 14:24:07 -0700120 if (!overwrite_with_zeros(fs_fd.get(), range[0], range[1])) return false;
121 LOG(DEBUG) << "Used zero overwrite";
Paul Crowley28c4df42015-07-07 17:13:17 +0100122 }
Paul Crowley53af81c2015-05-19 17:31:39 +0100123 }
Paul Crowley2143ee82016-06-28 14:24:07 -0700124 return true;
Paul Crowley53af81c2015-05-19 17:31:39 +0100125}
126
Paul Crowley28c4df42015-07-07 17:13:17 +0100127// Ensure that the FIEMAP covers the file and is OK to discard
128bool check_fiemap(const struct fiemap &fiemap, const std::string &path) {
129 auto mapped = fiemap.fm_mapped_extents;
130 if (!(fiemap.fm_extents[mapped - 1].fe_flags & FIEMAP_EXTENT_LAST)) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000131 LOG(ERROR) << "Extent " << mapped -1 << " was not the last in " << path;
Paul Crowley28c4df42015-07-07 17:13:17 +0100132 return false;
Paul Crowley53af81c2015-05-19 17:31:39 +0100133 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100134 for (uint32_t i = 0; i < mapped; i++) {
135 auto flags = fiemap.fm_extents[i].fe_flags;
136 if (flags & (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000137 LOG(ERROR) << "Extent " << i << " has unexpected flags " << flags << ": " << path;
Paul Crowley28c4df42015-07-07 17:13:17 +0100138 return false;
139 }
Paul Crowley53af81c2015-05-19 17:31:39 +0100140 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100141 return true;
142}
143
Paul Crowley2143ee82016-06-28 14:24:07 -0700144bool overwrite_with_zeros(int fd, off64_t start, off64_t length) {
145 if (lseek64(fd, start, SEEK_SET) != start) {
146 PLOG(ERROR) << "Seek failed for zero overwrite";
147 return false;
148 }
149 char buf[BUFSIZ];
150 memset(buf, 0, sizeof(buf));
151 while (length > 0) {
152 size_t wlen = static_cast<size_t>(std::min(static_cast<off64_t>(sizeof(buf)), length));
153 auto written = write(fd, buf, wlen);
154 if (written < 1) {
155 PLOG(ERROR) << "Write of zeroes failed";
156 return false;
157 }
158 length -= written;
159 }
160 return true;
161}
162
Paul Crowley53af81c2015-05-19 17:31:39 +0100163}