CheckEncryption.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2017 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 "CheckEncryption.h"
  17. #include "FileDeviceUtils.h"
  18. #include "Utils.h"
  19. #include "VolumeManager.h"
  20. #include <android-base/file.h>
  21. #include <android-base/logging.h>
  22. #include <android-base/unique_fd.h>
  23. #include <cutils/iosched_policy.h>
  24. #include <private/android_filesystem_config.h>
  25. #include <sstream>
  26. #include <sys/resource.h>
  27. #include <sys/time.h>
  28. #include <unistd.h>
  29. #include <assert.h>
  30. #include <fcntl.h>
  31. #include <linux/fs.h>
  32. #include <sys/stat.h>
  33. #include <sys/types.h>
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. #include <sys/stat.h>
  37. #include <sys/types.h>
  38. using android::base::unique_fd;
  39. using android::base::ReadFileToString;
  40. using android::base::WriteStringToFile;
  41. namespace android {
  42. namespace vold {
  43. constexpr uint32_t max_extents = 32;
  44. constexpr size_t bytecount = 8;
  45. constexpr int repeats = 256;
  46. bool check_file(const std::string& needle) {
  47. LOG(DEBUG) << "checkEncryption check_file: " << needle;
  48. auto haystack = android::vold::BlockDeviceForPath(needle);
  49. if (haystack.empty()) {
  50. LOG(ERROR) << "Failed to find device for path: " << needle;
  51. return false;
  52. }
  53. std::string randombytes;
  54. if (ReadRandomBytes(bytecount, randombytes) != 0) {
  55. LOG(ERROR) << "Failed to read random bytes";
  56. return false;
  57. }
  58. std::string randomhex;
  59. StrToHex(randombytes, randomhex);
  60. std::ostringstream os;
  61. for (int i = 0; i < repeats; i++) os << randomhex;
  62. auto towrite = os.str();
  63. if (access(needle.c_str(), F_OK) == 0) {
  64. if (unlink(needle.c_str()) != 0) {
  65. PLOG(ERROR) << "Failed to unlink " << needle;
  66. return false;
  67. }
  68. }
  69. LOG(DEBUG) << "Writing to " << needle;
  70. if (!WriteStringToFile(towrite, needle)) {
  71. PLOG(ERROR) << "Failed to write " << needle;
  72. return false;
  73. }
  74. sync();
  75. unique_fd haystack_fd(open(haystack.c_str(), O_RDONLY | O_CLOEXEC));
  76. if (haystack_fd.get() == -1) {
  77. PLOG(ERROR) << "Failed to open " << haystack;
  78. return false;
  79. }
  80. auto fiemap = PathFiemap(needle, max_extents);
  81. std::string area;
  82. for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) {
  83. auto xt = &(fiemap->fm_extents[i]);
  84. LOG(DEBUG) << "Extent " << i << " at " << xt->fe_physical << " length " << xt->fe_length;
  85. if (lseek64(haystack_fd.get(), xt->fe_physical, SEEK_SET) == -1) {
  86. PLOG(ERROR) << "Failed lseek";
  87. return false;
  88. }
  89. auto toread = xt->fe_length;
  90. while (toread > 0) {
  91. char buf[BUFSIZ];
  92. size_t wlen =
  93. static_cast<size_t>(std::min(static_cast<typeof(toread)>(sizeof(buf)), toread));
  94. auto l = read(haystack_fd.get(), buf, wlen);
  95. if (l < 1) {
  96. PLOG(ERROR) << "Failed read";
  97. if (errno != EINTR) {
  98. return false;
  99. }
  100. }
  101. area.append(buf, l);
  102. toread -= l;
  103. }
  104. }
  105. LOG(DEBUG) << "Searching " << area.size() << " bytes of " << needle;
  106. LOG(DEBUG) << "First position of blob: " << area.find(randomhex);
  107. return true;
  108. }
  109. int CheckEncryption(const std::string& path) {
  110. auto deNeedle(path);
  111. deNeedle += "/misc";
  112. if (android::vold::PrepareDir(deNeedle, 01771, AID_SYSTEM, AID_MISC)) {
  113. return -1;
  114. }
  115. deNeedle += "/vold";
  116. if (android::vold::PrepareDir(deNeedle, 0700, AID_ROOT, AID_ROOT)) {
  117. return -1;
  118. }
  119. deNeedle += "/checkEncryption";
  120. auto neNeedle(path);
  121. neNeedle += "/unencrypted/checkEncryption";
  122. check_file(deNeedle);
  123. check_file(neNeedle);
  124. return 0;
  125. }
  126. } // namespace vold
  127. } // namespace android