fake_nvram_storage.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2016 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 <nvram/core/storage.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdio.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. #include <android-base/file.h>
  24. #include <android-base/logging.h>
  25. #include <android-base/unique_fd.h>
  26. #include <nvram/core/logger.h>
  27. // An NVRAM storage layer implementation backed by the file system.
  28. //
  29. // NOTE: This does not meet the tamper evidence requirements for
  30. // access-controlled NVRAM implementations, since the file system can't provide
  31. // sufficient protection against tampering by attackers.
  32. namespace {
  33. // Name of the storage object holding the header.
  34. const char kHeaderFileName[] = "header";
  35. // Pattern for space data storage object names.
  36. const char kSpaceDataFileNamePattern[] = "space_%08x";
  37. // Temporary file name used in write-rename atomic write operations.
  38. const char kTempFileName[] = "temp";
  39. // Maximum size of objects we're willing to read and write.
  40. const off_t kMaxFileSize = 2048;
  41. // Buffer size for formatting names.
  42. using NameBuffer = char[16];
  43. // Global data directory descriptor.
  44. int g_data_dir_fd = -1;
  45. // Formats the storage object name for the given space index.
  46. bool FormatSpaceFileName(NameBuffer name, uint32_t index) {
  47. int ret =
  48. snprintf(name, sizeof(NameBuffer), kSpaceDataFileNamePattern, index);
  49. return ret >= 0 && ret < static_cast<int>(sizeof(NameBuffer));
  50. };
  51. nvram::storage::Status DeleteFile(const char* name) {
  52. if (TEMP_FAILURE_RETRY(unlinkat(g_data_dir_fd, name, 0))) {
  53. if (errno == ENOENT) {
  54. return nvram::storage::Status::kNotFound;
  55. }
  56. PLOG(ERROR) << "Failed to remove " << name;
  57. return nvram::storage::Status::kStorageError;
  58. }
  59. return nvram::storage::Status::kSuccess;
  60. }
  61. // Loads the storage object identified by |name|.
  62. nvram::storage::Status LoadFile(const char* name, nvram::Blob* blob) {
  63. android::base::unique_fd data_file_fd(
  64. TEMP_FAILURE_RETRY(openat(g_data_dir_fd, name, O_RDONLY)));
  65. if (data_file_fd.get() < 0) {
  66. if (errno == ENOENT) {
  67. return nvram::storage::Status::kNotFound;
  68. }
  69. PLOG(ERROR) << "Failed to open " << name;
  70. return nvram::storage::Status::kStorageError;
  71. }
  72. struct stat data_file_stat;
  73. if (TEMP_FAILURE_RETRY(fstat(data_file_fd.get(), &data_file_stat))) {
  74. PLOG(ERROR) << "Failed to stat " << name;
  75. return nvram::storage::Status::kStorageError;
  76. }
  77. if (data_file_stat.st_size > kMaxFileSize) {
  78. LOG(ERROR) << "Bad size for " << name << ":" << data_file_stat.st_size;
  79. return nvram::storage::Status::kStorageError;
  80. }
  81. if (!blob->Resize(data_file_stat.st_size)) {
  82. LOG(ERROR) << "Failed to allocate read buffer for " << name;
  83. return nvram::storage::Status::kStorageError;
  84. }
  85. if (!android::base::ReadFully(data_file_fd.get(), blob->data(),
  86. blob->size())) {
  87. PLOG(ERROR) << "Failed to read " << name;
  88. return nvram::storage::Status::kStorageError;
  89. }
  90. return nvram::storage::Status::kSuccess;
  91. }
  92. // Writes blob to the storage object indicated by |name|.
  93. nvram::storage::Status StoreFile(const char* name, const nvram::Blob& blob) {
  94. android::base::unique_fd data_file_fd(TEMP_FAILURE_RETRY(
  95. openat(g_data_dir_fd, kTempFileName, O_WRONLY | O_CREAT | O_TRUNC,
  96. S_IRUSR | S_IWUSR)));
  97. if (data_file_fd.get() < 0) {
  98. if (errno == ENOENT) {
  99. return nvram::storage::Status::kNotFound;
  100. }
  101. PLOG(ERROR) << "Failed to open " << kTempFileName;
  102. return nvram::storage::Status::kStorageError;
  103. }
  104. if (!android::base::WriteFully(data_file_fd.get(), blob.data(),
  105. blob.size())) {
  106. PLOG(ERROR) << "Failed to write " << kTempFileName;
  107. DeleteFile(kTempFileName);
  108. return nvram::storage::Status::kStorageError;
  109. }
  110. // Force the file contents to be written to disk.
  111. if (TEMP_FAILURE_RETRY(fdatasync(data_file_fd.get()))) {
  112. PLOG(ERROR) << "Failed to sync " << kTempFileName;
  113. DeleteFile(kTempFileName);
  114. return nvram::storage::Status::kStorageError;
  115. }
  116. data_file_fd.reset();
  117. // Move the file into place.
  118. if (TEMP_FAILURE_RETRY(
  119. renameat(g_data_dir_fd, kTempFileName, g_data_dir_fd, name))) {
  120. PLOG(ERROR) << "Failed to move " << kTempFileName << " to " << name;
  121. DeleteFile(kTempFileName);
  122. return nvram::storage::Status::kStorageError;
  123. }
  124. // Force the directory meta data to be written to disk.
  125. if (TEMP_FAILURE_RETRY(fsync(g_data_dir_fd))) {
  126. PLOG(ERROR) << "Failed to sync data directory";
  127. return nvram::storage::Status::kStorageError;
  128. }
  129. return nvram::storage::Status::kSuccess;
  130. }
  131. } // namespace
  132. // Initializes the storage layer with the provided data directory descriptor.
  133. void InitStorage(int data_dir_fd) {
  134. g_data_dir_fd = data_dir_fd;
  135. }
  136. namespace nvram {
  137. namespace storage {
  138. Status LoadHeader(Blob* blob) {
  139. return LoadFile(kHeaderFileName, blob);
  140. }
  141. Status StoreHeader(const Blob& blob) {
  142. return StoreFile(kHeaderFileName, blob);
  143. }
  144. Status LoadSpace(uint32_t index, Blob* blob) {
  145. NameBuffer name;
  146. if (!FormatSpaceFileName(name, index)) {
  147. return Status::kStorageError;
  148. }
  149. return LoadFile(name, blob);
  150. }
  151. Status StoreSpace(uint32_t index, const Blob& blob) {
  152. NameBuffer name;
  153. if (!FormatSpaceFileName(name, index)) {
  154. return Status::kStorageError;
  155. }
  156. return StoreFile(name, blob);
  157. }
  158. Status DeleteSpace(uint32_t index) {
  159. NameBuffer name;
  160. if (!FormatSpaceFileName(name, index)) {
  161. return Status::kStorageError;
  162. }
  163. return DeleteFile(name);
  164. }
  165. } // namespace storage
  166. } // namespace nvram