2015-05-19 18:31:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-07-07 18:13:17 +02:00
|
|
|
#include <memory>
|
2015-05-19 18:31:39 +02:00
|
|
|
#include <string>
|
2015-07-03 17:17:23 +02:00
|
|
|
#include <vector>
|
2015-05-19 18:31:39 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <linux/fiemap.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <linux/fs.h>
|
2015-07-01 14:33:47 +02:00
|
|
|
#include <mntent.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2015-05-19 18:31:39 +02:00
|
|
|
|
2016-01-11 13:26:44 +01:00
|
|
|
#include <android-base/logging.h>
|
2017-06-16 18:21:18 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2015-07-01 14:33:47 +02:00
|
|
|
|
2017-06-16 18:37:31 +02:00
|
|
|
#include "FileDeviceUtils.h"
|
|
|
|
|
2015-07-01 14:33:47 +02:00
|
|
|
namespace {
|
2015-07-03 17:17:23 +02:00
|
|
|
|
|
|
|
struct Options {
|
|
|
|
std::vector<std::string> targets;
|
|
|
|
bool unlink{true};
|
|
|
|
};
|
|
|
|
|
2015-07-07 18:13:17 +02:00
|
|
|
constexpr uint32_t max_extents = 32;
|
2015-05-19 18:31:39 +02:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
bool read_command_line(int argc, const char* const argv[], Options& options);
|
|
|
|
void usage(const char* progname);
|
|
|
|
bool secdiscard_path(const std::string& path);
|
|
|
|
bool check_fiemap(const struct fiemap& fiemap, const std::string& path);
|
2016-06-28 23:24:07 +02:00
|
|
|
bool overwrite_with_zeros(int fd, off64_t start, off64_t length);
|
2015-07-03 17:17:23 +02:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
} // namespace
|
2015-05-19 18:31:39 +02:00
|
|
|
|
2020-09-08 05:59:28 +02:00
|
|
|
#ifndef F2FS_IOCTL_MAGIC
|
|
|
|
#define F2FS_IOCTL_MAGIC 0xf5
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// F2FS-specific ioctl
|
|
|
|
// It requires the below kernel commit merged in v5.9-rc1.
|
|
|
|
// 9af846486d78 ("f2fs: add F2FS_IOC_SEC_TRIM_FILE ioctl")
|
|
|
|
// In android12-5.4,
|
|
|
|
// 7fc27297c44d ("Merge remote-tracking branch 'aosp/upstream-f2fs-stable-linux-5.4.y'
|
|
|
|
// into android12-5.4")
|
|
|
|
#ifndef F2FS_IOC_SEC_TRIM_FILE
|
|
|
|
struct f2fs_sectrim_range {
|
|
|
|
__u64 start;
|
|
|
|
__u64 len;
|
|
|
|
__u64 flags;
|
|
|
|
};
|
|
|
|
#define F2FS_IOC_SEC_TRIM_FILE _IOW(F2FS_IOCTL_MAGIC, 20, struct f2fs_sectrim_range)
|
|
|
|
#define F2FS_TRIM_FILE_DISCARD 0x1
|
|
|
|
#define F2FS_TRIM_FILE_ZEROOUT 0x2
|
|
|
|
#endif
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
int main(int argc, const char* const argv[]) {
|
|
|
|
android::base::InitLogging(const_cast<char**>(argv));
|
2015-07-03 17:17:23 +02:00
|
|
|
Options options;
|
|
|
|
if (!read_command_line(argc, argv, options)) {
|
2015-05-19 18:31:39 +02:00
|
|
|
usage(argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-08-02 21:41:45 +02:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
for (auto const& target : options.targets) {
|
2018-08-02 21:41:45 +02:00
|
|
|
// F2FS-specific ioctl
|
|
|
|
// It requires the below kernel commit merged in v4.16-rc1.
|
|
|
|
// 1ad71a27124c ("f2fs: add an ioctl to disable GC for specific file")
|
|
|
|
// In android-4.4,
|
|
|
|
// 56ee1e817908 ("f2fs: updates on v4.16-rc1")
|
|
|
|
// In android-4.9,
|
|
|
|
// 2f17e34672a8 ("f2fs: updates on v4.16-rc1")
|
|
|
|
// In android-4.14,
|
|
|
|
// ce767d9a55bc ("f2fs: updates on v4.16-rc1")
|
|
|
|
#ifndef F2FS_IOC_SET_PIN_FILE
|
2018-09-18 22:30:21 +02:00
|
|
|
#define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
|
|
|
|
#define F2FS_IOC_GET_PIN_FILE _IOR(F2FS_IOCTL_MAGIC, 14, __u32)
|
2018-08-02 21:41:45 +02:00
|
|
|
#endif
|
2019-03-30 00:03:51 +01:00
|
|
|
android::base::unique_fd fd(
|
|
|
|
TEMP_FAILURE_RETRY(open(target.c_str(), O_WRONLY | O_CLOEXEC, 0)));
|
2018-08-02 21:41:45 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
LOG(ERROR) << "Secure discard open failed for: " << target;
|
2022-10-07 07:19:50 +02:00
|
|
|
continue;
|
2018-08-02 21:41:45 +02:00
|
|
|
}
|
|
|
|
__u32 set = 1;
|
|
|
|
ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set);
|
|
|
|
|
2016-01-11 13:26:44 +01:00
|
|
|
LOG(DEBUG) << "Securely discarding '" << target << "' unlink=" << options.unlink;
|
2020-09-08 05:59:28 +02:00
|
|
|
struct f2fs_sectrim_range secRange;
|
|
|
|
secRange.start = 0;
|
|
|
|
secRange.len = -1; // until end of file
|
|
|
|
secRange.flags = F2FS_TRIM_FILE_DISCARD | F2FS_TRIM_FILE_ZEROOUT;
|
|
|
|
/*
|
|
|
|
* F2FS_IOC_SEC_TRIM_FILE is only supported by F2FS.
|
|
|
|
* 1. If device supports secure discard, it sends secure discard command on the file.
|
|
|
|
* 2. Otherwise, it sends discard command on the file.
|
|
|
|
* 3. Lastly, it overwrites zero data on it.
|
|
|
|
*/
|
|
|
|
int ret = ioctl(fd, F2FS_IOC_SEC_TRIM_FILE, &secRange);
|
|
|
|
if (ret != 0) {
|
|
|
|
if (errno == EOPNOTSUPP) {
|
|
|
|
// If device doesn't support any type of discard, just overwrite zero data.
|
|
|
|
secRange.flags = F2FS_TRIM_FILE_ZEROOUT;
|
|
|
|
ret = ioctl(fd, F2FS_IOC_SEC_TRIM_FILE, &secRange);
|
|
|
|
}
|
|
|
|
if (ret != 0 && errno != ENOTTY) {
|
|
|
|
PLOG(WARNING) << "F2FS_IOC_SEC_TRIM_FILE failed on " << target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret != 0) {
|
|
|
|
if (!secdiscard_path(target)) {
|
|
|
|
LOG(ERROR) << "Secure discard failed for: " << target;
|
|
|
|
}
|
2016-06-28 23:24:07 +02:00
|
|
|
}
|
2015-07-03 17:17:23 +02:00
|
|
|
if (options.unlink) {
|
|
|
|
if (unlink(target.c_str()) != 0 && errno != ENOENT) {
|
2016-01-11 13:26:44 +01:00
|
|
|
PLOG(ERROR) << "Unable to unlink: " << target;
|
2015-07-03 17:17:23 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-02 21:41:45 +02:00
|
|
|
set = 0;
|
|
|
|
ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set);
|
2015-05-19 18:31:39 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-01 14:33:47 +02:00
|
|
|
namespace {
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
bool read_command_line(int argc, const char* const argv[], Options& options) {
|
2015-07-03 17:17:23 +02:00
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
if (!strcmp("--no-unlink", argv[i])) {
|
|
|
|
options.unlink = false;
|
|
|
|
} else if (!strcmp("--", argv[i])) {
|
2018-09-18 22:30:21 +02:00
|
|
|
for (int j = i + 1; j < argc; j++) {
|
|
|
|
if (argv[j][0] != '/') return false; // Must be absolute path
|
2015-07-03 17:17:23 +02:00
|
|
|
options.targets.emplace_back(argv[j]);
|
|
|
|
}
|
|
|
|
return options.targets.size() > 0;
|
|
|
|
} else {
|
2018-09-18 22:30:21 +02:00
|
|
|
return false; // Unknown option
|
2015-07-03 17:17:23 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-18 22:30:21 +02:00
|
|
|
return false; // "--" not found
|
2015-07-03 17:17:23 +02:00
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
void usage(const char* progname) {
|
2015-07-03 17:17:23 +02:00
|
|
|
fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname);
|
2015-05-19 18:31:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// BLKSECDISCARD all content in "path", if it's small enough.
|
2018-09-18 22:30:21 +02:00
|
|
|
bool secdiscard_path(const std::string& path) {
|
2017-06-16 18:37:31 +02:00
|
|
|
auto fiemap = android::vold::PathFiemap(path, max_extents);
|
2015-07-07 18:13:17 +02:00
|
|
|
if (!fiemap || !check_fiemap(*fiemap, path)) {
|
2016-06-28 23:24:07 +02:00
|
|
|
return false;
|
2015-05-19 18:31:39 +02:00
|
|
|
}
|
2017-06-16 18:37:31 +02:00
|
|
|
auto block_device = android::vold::BlockDeviceForPath(path);
|
2015-07-01 14:33:47 +02:00
|
|
|
if (block_device.empty()) {
|
2016-06-28 23:24:07 +02:00
|
|
|
return false;
|
2015-05-19 18:31:39 +02:00
|
|
|
}
|
2018-09-18 22:30:21 +02:00
|
|
|
android::base::unique_fd fs_fd(
|
|
|
|
TEMP_FAILURE_RETRY(open(block_device.c_str(), O_RDWR | O_LARGEFILE | O_CLOEXEC, 0)));
|
2017-06-16 18:21:18 +02:00
|
|
|
if (fs_fd == -1) {
|
2016-01-11 13:26:44 +01:00
|
|
|
PLOG(ERROR) << "Failed to open device " << block_device;
|
2016-06-28 23:24:07 +02:00
|
|
|
return false;
|
2015-07-01 14:33:47 +02:00
|
|
|
}
|
2015-07-07 18:13:17 +02:00
|
|
|
for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) {
|
|
|
|
uint64_t range[2];
|
|
|
|
range[0] = fiemap->fm_extents[i].fe_physical;
|
|
|
|
range[1] = fiemap->fm_extents[i].fe_length;
|
|
|
|
if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) {
|
2019-03-21 19:13:40 +01:00
|
|
|
// Use zero overwrite as a fallback for BLKSECDISCARD
|
2016-06-28 23:24:07 +02:00
|
|
|
if (!overwrite_with_zeros(fs_fd.get(), range[0], range[1])) return false;
|
2015-07-07 18:13:17 +02:00
|
|
|
}
|
2015-05-19 18:31:39 +02:00
|
|
|
}
|
2019-08-08 18:07:58 +02:00
|
|
|
// Should wait for overwrites completion. Otherwise after unlink(),
|
|
|
|
// filesystem can allocate these blocks and IO can be reordered, resulting
|
|
|
|
// in making zero blocks to filesystem blocks.
|
|
|
|
fsync(fs_fd.get());
|
2016-06-28 23:24:07 +02:00
|
|
|
return true;
|
2015-05-19 18:31:39 +02:00
|
|
|
}
|
|
|
|
|
2015-07-07 18:13:17 +02:00
|
|
|
// Ensure that the FIEMAP covers the file and is OK to discard
|
2018-09-18 22:30:21 +02:00
|
|
|
bool check_fiemap(const struct fiemap& fiemap, const std::string& path) {
|
2015-07-07 18:13:17 +02:00
|
|
|
auto mapped = fiemap.fm_mapped_extents;
|
|
|
|
if (!(fiemap.fm_extents[mapped - 1].fe_flags & FIEMAP_EXTENT_LAST)) {
|
2018-09-18 22:30:21 +02:00
|
|
|
LOG(ERROR) << "Extent " << mapped - 1 << " was not the last in " << path;
|
2015-07-07 18:13:17 +02:00
|
|
|
return false;
|
2015-05-19 18:31:39 +02:00
|
|
|
}
|
2015-07-07 18:13:17 +02:00
|
|
|
for (uint32_t i = 0; i < mapped; i++) {
|
|
|
|
auto flags = fiemap.fm_extents[i].fe_flags;
|
|
|
|
if (flags & (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) {
|
2016-01-11 13:26:44 +01:00
|
|
|
LOG(ERROR) << "Extent " << i << " has unexpected flags " << flags << ": " << path;
|
2015-07-07 18:13:17 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-05-19 18:31:39 +02:00
|
|
|
}
|
2015-07-07 18:13:17 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:24:07 +02:00
|
|
|
bool overwrite_with_zeros(int fd, off64_t start, off64_t length) {
|
|
|
|
if (lseek64(fd, start, SEEK_SET) != start) {
|
|
|
|
PLOG(ERROR) << "Seek failed for zero overwrite";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
while (length > 0) {
|
|
|
|
size_t wlen = static_cast<size_t>(std::min(static_cast<off64_t>(sizeof(buf)), length));
|
|
|
|
auto written = write(fd, buf, wlen);
|
|
|
|
if (written < 1) {
|
|
|
|
PLOG(ERROR) << "Write of zeroes failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
length -= written;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
} // namespace
|