1 /*
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 <memory>
18 #include <string>
19 #include <vector>
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <linux/fiemap.h>
24 #include <linux/fs.h>
25 #include <mntent.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30
31 #include <android-base/logging.h>
32 #include <android-base/unique_fd.h>
33
34 #include "FileDeviceUtils.h"
35
36 namespace {
37
38 struct Options {
39 std::vector<std::string> targets;
40 bool unlink{true};
41 };
42
43 constexpr uint32_t max_extents = 32;
44
45 bool read_command_line(int argc, const char* const argv[], Options& options);
46 void usage(const char* progname);
47 bool secdiscard_path(const std::string& path);
48 bool check_fiemap(const struct fiemap& fiemap, const std::string& path);
49 bool overwrite_with_zeros(int fd, off64_t start, off64_t length);
50
51 } // namespace
52
main(int argc,const char * const argv[])53 int main(int argc, const char* const argv[]) {
54 android::base::InitLogging(const_cast<char**>(argv));
55 Options options;
56 if (!read_command_line(argc, argv, options)) {
57 usage(argv[0]);
58 return -1;
59 }
60
61 for (auto const& target : options.targets) {
62 // F2FS-specific ioctl
63 // It requires the below kernel commit merged in v4.16-rc1.
64 // 1ad71a27124c ("f2fs: add an ioctl to disable GC for specific file")
65 // In android-4.4,
66 // 56ee1e817908 ("f2fs: updates on v4.16-rc1")
67 // In android-4.9,
68 // 2f17e34672a8 ("f2fs: updates on v4.16-rc1")
69 // In android-4.14,
70 // ce767d9a55bc ("f2fs: updates on v4.16-rc1")
71 #ifndef F2FS_IOC_SET_PIN_FILE
72 #ifndef F2FS_IOCTL_MAGIC
73 #define F2FS_IOCTL_MAGIC 0xf5
74 #endif
75 #define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
76 #define F2FS_IOC_GET_PIN_FILE _IOR(F2FS_IOCTL_MAGIC, 14, __u32)
77 #endif
78 android::base::unique_fd fd(
79 TEMP_FAILURE_RETRY(open(target.c_str(), O_WRONLY | O_CLOEXEC, 0)));
80 if (fd == -1) {
81 LOG(ERROR) << "Secure discard open failed for: " << target;
82 return 0;
83 }
84 __u32 set = 1;
85 ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set);
86
87 LOG(DEBUG) << "Securely discarding '" << target << "' unlink=" << options.unlink;
88 if (!secdiscard_path(target)) {
89 LOG(ERROR) << "Secure discard failed for: " << target;
90 }
91 if (options.unlink) {
92 if (unlink(target.c_str()) != 0 && errno != ENOENT) {
93 PLOG(ERROR) << "Unable to unlink: " << target;
94 }
95 }
96 set = 0;
97 ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set);
98 }
99 return 0;
100 }
101
102 namespace {
103
read_command_line(int argc,const char * const argv[],Options & options)104 bool read_command_line(int argc, const char* const argv[], Options& options) {
105 for (int i = 1; i < argc; i++) {
106 if (!strcmp("--no-unlink", argv[i])) {
107 options.unlink = false;
108 } else if (!strcmp("--", argv[i])) {
109 for (int j = i + 1; j < argc; j++) {
110 if (argv[j][0] != '/') return false; // Must be absolute path
111 options.targets.emplace_back(argv[j]);
112 }
113 return options.targets.size() > 0;
114 } else {
115 return false; // Unknown option
116 }
117 }
118 return false; // "--" not found
119 }
120
usage(const char * progname)121 void usage(const char* progname) {
122 fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname);
123 }
124
125 // BLKSECDISCARD all content in "path", if it's small enough.
secdiscard_path(const std::string & path)126 bool secdiscard_path(const std::string& path) {
127 auto fiemap = android::vold::PathFiemap(path, max_extents);
128 if (!fiemap || !check_fiemap(*fiemap, path)) {
129 return false;
130 }
131 auto block_device = android::vold::BlockDeviceForPath(path);
132 if (block_device.empty()) {
133 return false;
134 }
135 android::base::unique_fd fs_fd(
136 TEMP_FAILURE_RETRY(open(block_device.c_str(), O_RDWR | O_LARGEFILE | O_CLOEXEC, 0)));
137 if (fs_fd == -1) {
138 PLOG(ERROR) << "Failed to open device " << block_device;
139 return false;
140 }
141 for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) {
142 uint64_t range[2];
143 range[0] = fiemap->fm_extents[i].fe_physical;
144 range[1] = fiemap->fm_extents[i].fe_length;
145 if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) {
146 // Use zero overwrite as a fallback for BLKSECDISCARD
147 if (!overwrite_with_zeros(fs_fd.get(), range[0], range[1])) return false;
148 }
149 }
150 return true;
151 }
152
153 // Ensure that the FIEMAP covers the file and is OK to discard
check_fiemap(const struct fiemap & fiemap,const std::string & path)154 bool check_fiemap(const struct fiemap& fiemap, const std::string& path) {
155 auto mapped = fiemap.fm_mapped_extents;
156 if (!(fiemap.fm_extents[mapped - 1].fe_flags & FIEMAP_EXTENT_LAST)) {
157 LOG(ERROR) << "Extent " << mapped - 1 << " was not the last in " << path;
158 return false;
159 }
160 for (uint32_t i = 0; i < mapped; i++) {
161 auto flags = fiemap.fm_extents[i].fe_flags;
162 if (flags & (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) {
163 LOG(ERROR) << "Extent " << i << " has unexpected flags " << flags << ": " << path;
164 return false;
165 }
166 }
167 return true;
168 }
169
overwrite_with_zeros(int fd,off64_t start,off64_t length)170 bool overwrite_with_zeros(int fd, off64_t start, off64_t length) {
171 if (lseek64(fd, start, SEEK_SET) != start) {
172 PLOG(ERROR) << "Seek failed for zero overwrite";
173 return false;
174 }
175 char buf[BUFSIZ];
176 memset(buf, 0, sizeof(buf));
177 while (length > 0) {
178 size_t wlen = static_cast<size_t>(std::min(static_cast<off64_t>(sizeof(buf)), length));
179 auto written = write(fd, buf, wlen);
180 if (written < 1) {
181 PLOG(ERROR) << "Write of zeroes failed";
182 return false;
183 }
184 length -= written;
185 }
186 return true;
187 }
188
189 } // namespace
190