secdiscard.cpp 6.3 KB

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