persistent_properties.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "persistent_properties.h"
  17. #include <dirent.h>
  18. #include <fcntl.h>
  19. #include <sys/stat.h>
  20. #include <sys/system_properties.h>
  21. #include <sys/types.h>
  22. #include <memory>
  23. #include <android-base/file.h>
  24. #include <android-base/logging.h>
  25. #include <android-base/strings.h>
  26. #include <android-base/unique_fd.h>
  27. #include "util.h"
  28. using android::base::ReadFdToString;
  29. using android::base::StartsWith;
  30. using android::base::WriteStringToFd;
  31. using android::base::unique_fd;
  32. namespace android {
  33. namespace init {
  34. std::string persistent_property_filename = "/data/property/persistent_properties";
  35. namespace {
  36. constexpr const char kLegacyPersistentPropertyDir[] = "/data/property";
  37. void AddPersistentProperty(const std::string& name, const std::string& value,
  38. PersistentProperties* persistent_properties) {
  39. auto persistent_property_record = persistent_properties->add_properties();
  40. persistent_property_record->set_name(name);
  41. persistent_property_record->set_value(value);
  42. }
  43. Result<PersistentProperties> LoadLegacyPersistentProperties() {
  44. std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(kLegacyPersistentPropertyDir), closedir);
  45. if (!dir) {
  46. return ErrnoError() << "Unable to open persistent property directory \""
  47. << kLegacyPersistentPropertyDir << "\"";
  48. }
  49. PersistentProperties persistent_properties;
  50. dirent* entry;
  51. while ((entry = readdir(dir.get())) != nullptr) {
  52. if (!StartsWith(entry->d_name, "persist.")) {
  53. continue;
  54. }
  55. if (entry->d_type != DT_REG) {
  56. continue;
  57. }
  58. unique_fd fd(openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW));
  59. if (fd == -1) {
  60. PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
  61. continue;
  62. }
  63. struct stat sb;
  64. if (fstat(fd, &sb) == -1) {
  65. PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
  66. continue;
  67. }
  68. // File must not be accessible to others, be owned by root/root, and
  69. // not be a hard link to any other file.
  70. if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 ||
  71. sb.st_nlink != 1) {
  72. PLOG(ERROR) << "skipping insecure property file " << entry->d_name
  73. << " (uid=" << sb.st_uid << " gid=" << sb.st_gid << " nlink=" << sb.st_nlink
  74. << " mode=" << std::oct << sb.st_mode << ")";
  75. continue;
  76. }
  77. std::string value;
  78. if (ReadFdToString(fd, &value)) {
  79. AddPersistentProperty(entry->d_name, value, &persistent_properties);
  80. } else {
  81. PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
  82. }
  83. }
  84. return persistent_properties;
  85. }
  86. void RemoveLegacyPersistentPropertyFiles() {
  87. std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(kLegacyPersistentPropertyDir), closedir);
  88. if (!dir) {
  89. PLOG(ERROR) << "Unable to open persistent property directory \""
  90. << kLegacyPersistentPropertyDir << "\"";
  91. return;
  92. }
  93. dirent* entry;
  94. while ((entry = readdir(dir.get())) != nullptr) {
  95. if (!StartsWith(entry->d_name, "persist.")) {
  96. continue;
  97. }
  98. if (entry->d_type != DT_REG) {
  99. continue;
  100. }
  101. unlinkat(dirfd(dir.get()), entry->d_name, 0);
  102. }
  103. }
  104. PersistentProperties LoadPersistentPropertiesFromMemory() {
  105. PersistentProperties persistent_properties;
  106. __system_property_foreach(
  107. [](const prop_info* pi, void* cookie) {
  108. __system_property_read_callback(
  109. pi,
  110. [](void* cookie, const char* name, const char* value, unsigned serial) {
  111. if (StartsWith(name, "persist.")) {
  112. auto properties = reinterpret_cast<PersistentProperties*>(cookie);
  113. AddPersistentProperty(name, value, properties);
  114. }
  115. },
  116. cookie);
  117. },
  118. &persistent_properties);
  119. return persistent_properties;
  120. }
  121. Result<std::string> ReadPersistentPropertyFile() {
  122. const std::string temp_filename = persistent_property_filename + ".tmp";
  123. if (access(temp_filename.c_str(), F_OK) == 0) {
  124. LOG(INFO)
  125. << "Found temporary property file while attempting to persistent system properties"
  126. " a previous persistent property write may have failed";
  127. unlink(temp_filename.c_str());
  128. }
  129. auto file_contents = ReadFile(persistent_property_filename);
  130. if (!file_contents) {
  131. return Error() << "Unable to read persistent property file: " << file_contents.error();
  132. }
  133. return *file_contents;
  134. }
  135. } // namespace
  136. Result<PersistentProperties> LoadPersistentPropertyFile() {
  137. auto file_contents = ReadPersistentPropertyFile();
  138. if (!file_contents) return file_contents.error();
  139. PersistentProperties persistent_properties;
  140. if (persistent_properties.ParseFromString(*file_contents)) return persistent_properties;
  141. // If the file cannot be parsed in either format, then we don't have any recovery
  142. // mechanisms, so we delete it to allow for future writes to take place successfully.
  143. unlink(persistent_property_filename.c_str());
  144. return Error() << "Unable to parse persistent property file: Could not parse protobuf";
  145. }
  146. Result<Success> WritePersistentPropertyFile(const PersistentProperties& persistent_properties) {
  147. const std::string temp_filename = persistent_property_filename + ".tmp";
  148. unique_fd fd(TEMP_FAILURE_RETRY(
  149. open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0600)));
  150. if (fd == -1) {
  151. return ErrnoError() << "Could not open temporary properties file";
  152. }
  153. std::string serialized_string;
  154. if (!persistent_properties.SerializeToString(&serialized_string)) {
  155. return Error() << "Unable to serialize properties";
  156. }
  157. if (!WriteStringToFd(serialized_string, fd)) {
  158. return ErrnoError() << "Unable to write file contents";
  159. }
  160. fsync(fd);
  161. fd.reset();
  162. if (rename(temp_filename.c_str(), persistent_property_filename.c_str())) {
  163. int saved_errno = errno;
  164. unlink(temp_filename.c_str());
  165. return Error(saved_errno) << "Unable to rename persistent property file";
  166. }
  167. return Success();
  168. }
  169. // Persistent properties are not written often, so we rather not keep any data in memory and read
  170. // then rewrite the persistent property file for each update.
  171. void WritePersistentProperty(const std::string& name, const std::string& value) {
  172. auto persistent_properties = LoadPersistentPropertyFile();
  173. if (!persistent_properties) {
  174. LOG(ERROR) << "Recovering persistent properties from memory: "
  175. << persistent_properties.error();
  176. persistent_properties = LoadPersistentPropertiesFromMemory();
  177. }
  178. auto it = std::find_if(persistent_properties->mutable_properties()->begin(),
  179. persistent_properties->mutable_properties()->end(),
  180. [&name](const auto& record) { return record.name() == name; });
  181. if (it != persistent_properties->mutable_properties()->end()) {
  182. it->set_name(name);
  183. it->set_value(value);
  184. } else {
  185. AddPersistentProperty(name, value, &persistent_properties.value());
  186. }
  187. if (auto result = WritePersistentPropertyFile(*persistent_properties); !result) {
  188. LOG(ERROR) << "Could not store persistent property: " << result.error();
  189. }
  190. }
  191. PersistentProperties LoadPersistentProperties() {
  192. auto persistent_properties = LoadPersistentPropertyFile();
  193. if (!persistent_properties) {
  194. LOG(ERROR) << "Could not load single persistent property file, trying legacy directory: "
  195. << persistent_properties.error();
  196. persistent_properties = LoadLegacyPersistentProperties();
  197. if (!persistent_properties) {
  198. LOG(ERROR) << "Unable to load legacy persistent properties: "
  199. << persistent_properties.error();
  200. return {};
  201. }
  202. if (auto result = WritePersistentPropertyFile(*persistent_properties); result) {
  203. RemoveLegacyPersistentPropertyFiles();
  204. } else {
  205. LOG(ERROR) << "Unable to write single persistent property file: " << result.error();
  206. // Fall through so that we still set the properties that we've read.
  207. }
  208. }
  209. return *persistent_properties;
  210. }
  211. } // namespace init
  212. } // namespace android