Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 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 | |
| 17 | #include "persistent_properties.h" |
| 18 | |
| 19 | #include <dirent.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/system_properties.h> |
| 23 | #include <sys/types.h> |
| 24 | |
| 25 | #include <memory> |
| 26 | |
| 27 | #include <android-base/file.h> |
| 28 | #include <android-base/logging.h> |
| 29 | #include <android-base/strings.h> |
| 30 | #include <android-base/unique_fd.h> |
| 31 | |
| 32 | #include "util.h" |
| 33 | |
Tom Cherry | 97437a7 | 2019-12-06 11:16:10 -0800 | [diff] [blame] | 34 | using android::base::Dirname; |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 35 | using android::base::ReadFdToString; |
| 36 | using android::base::StartsWith; |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 37 | using android::base::unique_fd; |
Tom Cherry | 97437a7 | 2019-12-06 11:16:10 -0800 | [diff] [blame] | 38 | using android::base::WriteStringToFd; |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 39 | |
| 40 | namespace android { |
| 41 | namespace init { |
| 42 | |
| 43 | std::string persistent_property_filename = "/data/property/persistent_properties"; |
| 44 | |
| 45 | namespace { |
| 46 | |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 47 | constexpr const char kLegacyPersistentPropertyDir[] = "/data/property"; |
| 48 | |
Dennis Shen | 79283ef | 2023-10-24 17:30:12 +0000 | [diff] [blame^] | 49 | void AddPersistentProperty(const std::string& name, const std::string& value, |
| 50 | PersistentProperties* persistent_properties) { |
| 51 | auto persistent_property_record = persistent_properties->add_properties(); |
| 52 | persistent_property_record->set_name(name); |
| 53 | persistent_property_record->set_value(value); |
| 54 | } |
| 55 | |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 56 | Result<PersistentProperties> LoadLegacyPersistentProperties() { |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 57 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(kLegacyPersistentPropertyDir), closedir); |
| 58 | if (!dir) { |
| 59 | return ErrnoError() << "Unable to open persistent property directory \"" |
| 60 | << kLegacyPersistentPropertyDir << "\""; |
| 61 | } |
| 62 | |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 63 | PersistentProperties persistent_properties; |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 64 | dirent* entry; |
| 65 | while ((entry = readdir(dir.get())) != nullptr) { |
| 66 | if (!StartsWith(entry->d_name, "persist.")) { |
| 67 | continue; |
| 68 | } |
| 69 | if (entry->d_type != DT_REG) { |
| 70 | continue; |
| 71 | } |
| 72 | |
Tom Cherry | 247ffbf | 2019-07-08 15:09:36 -0700 | [diff] [blame] | 73 | unique_fd fd(openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC)); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 74 | if (fd == -1) { |
| 75 | PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\""; |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | struct stat sb; |
Bart Van Assche | aee2ec8 | 2022-12-02 18:48:15 -0800 | [diff] [blame] | 80 | if (fstat(fd.get(), &sb) == -1) { |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 81 | PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed"; |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | // File must not be accessible to others, be owned by root/root, and |
| 86 | // not be a hard link to any other file. |
| 87 | if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 || |
| 88 | sb.st_nlink != 1) { |
| 89 | PLOG(ERROR) << "skipping insecure property file " << entry->d_name |
| 90 | << " (uid=" << sb.st_uid << " gid=" << sb.st_gid << " nlink=" << sb.st_nlink |
| 91 | << " mode=" << std::oct << sb.st_mode << ")"; |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | std::string value; |
| 96 | if (ReadFdToString(fd, &value)) { |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 97 | AddPersistentProperty(entry->d_name, value, &persistent_properties); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 98 | } else { |
| 99 | PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name; |
| 100 | } |
| 101 | } |
| 102 | return persistent_properties; |
| 103 | } |
| 104 | |
| 105 | void RemoveLegacyPersistentPropertyFiles() { |
| 106 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(kLegacyPersistentPropertyDir), closedir); |
| 107 | if (!dir) { |
| 108 | PLOG(ERROR) << "Unable to open persistent property directory \"" |
| 109 | << kLegacyPersistentPropertyDir << "\""; |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | dirent* entry; |
| 114 | while ((entry = readdir(dir.get())) != nullptr) { |
| 115 | if (!StartsWith(entry->d_name, "persist.")) { |
| 116 | continue; |
| 117 | } |
| 118 | if (entry->d_type != DT_REG) { |
| 119 | continue; |
| 120 | } |
| 121 | unlinkat(dirfd(dir.get()), entry->d_name, 0); |
| 122 | } |
| 123 | } |
| 124 | |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 125 | Result<std::string> ReadPersistentPropertyFile() { |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 126 | const std::string temp_filename = persistent_property_filename + ".tmp"; |
| 127 | if (access(temp_filename.c_str(), F_OK) == 0) { |
| 128 | LOG(INFO) |
| 129 | << "Found temporary property file while attempting to persistent system properties" |
| 130 | " a previous persistent property write may have failed"; |
| 131 | unlink(temp_filename.c_str()); |
| 132 | } |
| 133 | auto file_contents = ReadFile(persistent_property_filename); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 134 | if (!file_contents.ok()) { |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 135 | return Error() << "Unable to read persistent property file: " << file_contents.error(); |
| 136 | } |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 137 | return *file_contents; |
| 138 | } |
| 139 | |
Paul Crowley | f7c7469 | 2022-08-25 11:38:27 -0700 | [diff] [blame] | 140 | Result<PersistentProperties> ParsePersistentPropertyFile(const std::string& file_contents) { |
| 141 | PersistentProperties persistent_properties; |
| 142 | if (!persistent_properties.ParseFromString(file_contents)) { |
| 143 | return Error() << "Unable to parse persistent property file: Could not parse protobuf"; |
| 144 | } |
| 145 | for (auto& prop : persistent_properties.properties()) { |
Dennis Shen | 678d268 | 2023-09-18 19:52:52 +0000 | [diff] [blame] | 146 | if (!StartsWith(prop.name(), "persist.") && !StartsWith(prop.name(), "next_boot.")) { |
Paul Crowley | f7c7469 | 2022-08-25 11:38:27 -0700 | [diff] [blame] | 147 | return Error() << "Unable to load persistent property file: property '" << prop.name() |
Dennis Shen | 678d268 | 2023-09-18 19:52:52 +0000 | [diff] [blame] | 148 | << "' doesn't start with 'persist.' or 'next_boot.'"; |
Paul Crowley | f7c7469 | 2022-08-25 11:38:27 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | return persistent_properties; |
| 152 | } |
| 153 | |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 154 | } // namespace |
| 155 | |
| 156 | Result<PersistentProperties> LoadPersistentPropertyFile() { |
| 157 | auto file_contents = ReadPersistentPropertyFile(); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 158 | if (!file_contents.ok()) return file_contents.error(); |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 159 | |
Paul Crowley | f7c7469 | 2022-08-25 11:38:27 -0700 | [diff] [blame] | 160 | auto persistent_properties = ParsePersistentPropertyFile(*file_contents); |
| 161 | if (!persistent_properties.ok()) { |
| 162 | // If the file cannot be parsed in either format, then we don't have any recovery |
| 163 | // mechanisms, so we delete it to allow for future writes to take place successfully. |
| 164 | unlink(persistent_property_filename.c_str()); |
| 165 | } |
| 166 | return persistent_properties; |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 169 | Result<void> WritePersistentPropertyFile(const PersistentProperties& persistent_properties) { |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 170 | const std::string temp_filename = persistent_property_filename + ".tmp"; |
| 171 | unique_fd fd(TEMP_FAILURE_RETRY( |
| 172 | open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0600))); |
| 173 | if (fd == -1) { |
| 174 | return ErrnoError() << "Could not open temporary properties file"; |
| 175 | } |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 176 | std::string serialized_string; |
| 177 | if (!persistent_properties.SerializeToString(&serialized_string)) { |
| 178 | return Error() << "Unable to serialize properties"; |
| 179 | } |
| 180 | if (!WriteStringToFd(serialized_string, fd)) { |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 181 | return ErrnoError() << "Unable to write file contents"; |
| 182 | } |
Bart Van Assche | aee2ec8 | 2022-12-02 18:48:15 -0800 | [diff] [blame] | 183 | fsync(fd.get()); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 184 | fd.reset(); |
| 185 | |
| 186 | if (rename(temp_filename.c_str(), persistent_property_filename.c_str())) { |
| 187 | int saved_errno = errno; |
| 188 | unlink(temp_filename.c_str()); |
| 189 | return Error(saved_errno) << "Unable to rename persistent property file"; |
| 190 | } |
Tom Cherry | 97437a7 | 2019-12-06 11:16:10 -0800 | [diff] [blame] | 191 | |
| 192 | // rename() is atomic with regards to the kernel's filesystem buffers, but the parent |
| 193 | // directories must be fsync()'ed otherwise, the rename is not necessarily written to storage. |
| 194 | // Note in this case, that the source and destination directories are the same, so only one |
| 195 | // fsync() is required. |
| 196 | auto dir = Dirname(persistent_property_filename); |
Tom Cherry | c99d60c | 2019-12-09 06:48:37 -0800 | [diff] [blame] | 197 | auto dir_fd = unique_fd{open(dir.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC)}; |
Tom Cherry | 97437a7 | 2019-12-06 11:16:10 -0800 | [diff] [blame] | 198 | if (dir_fd < 0) { |
| 199 | return ErrnoError() << "Unable to open persistent properties directory for fsync()"; |
| 200 | } |
Bart Van Assche | aee2ec8 | 2022-12-02 18:48:15 -0800 | [diff] [blame] | 201 | fsync(dir_fd.get()); |
Tom Cherry | 97437a7 | 2019-12-06 11:16:10 -0800 | [diff] [blame] | 202 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 203 | return {}; |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Jiyong Park | c7230a1 | 2023-10-20 20:42:56 +0900 | [diff] [blame] | 206 | PersistentProperties LoadPersistentPropertiesFromMemory() { |
| 207 | PersistentProperties persistent_properties; |
| 208 | __system_property_foreach( |
| 209 | [](const prop_info* pi, void* cookie) { |
| 210 | __system_property_read_callback( |
| 211 | pi, |
| 212 | [](void* cookie, const char* name, const char* value, unsigned serial) { |
| 213 | if (StartsWith(name, "persist.")) { |
| 214 | auto properties = reinterpret_cast<PersistentProperties*>(cookie); |
| 215 | AddPersistentProperty(name, value, properties); |
| 216 | } |
| 217 | }, |
| 218 | cookie); |
| 219 | }, |
| 220 | &persistent_properties); |
| 221 | return persistent_properties; |
| 222 | } |
| 223 | |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 224 | // Persistent properties are not written often, so we rather not keep any data in memory and read |
| 225 | // then rewrite the persistent property file for each update. |
| 226 | void WritePersistentProperty(const std::string& name, const std::string& value) { |
Tom Cherry | 9614e4d | 2017-09-19 10:31:27 -0700 | [diff] [blame] | 227 | auto persistent_properties = LoadPersistentPropertyFile(); |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 228 | |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 229 | if (!persistent_properties.ok()) { |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 230 | LOG(ERROR) << "Recovering persistent properties from memory: " |
Tom Cherry | 9614e4d | 2017-09-19 10:31:27 -0700 | [diff] [blame] | 231 | << persistent_properties.error(); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 232 | persistent_properties = LoadPersistentPropertiesFromMemory(); |
| 233 | } |
Tom Cherry | 9614e4d | 2017-09-19 10:31:27 -0700 | [diff] [blame] | 234 | auto it = std::find_if(persistent_properties->mutable_properties()->begin(), |
| 235 | persistent_properties->mutable_properties()->end(), |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 236 | [&name](const auto& record) { return record.name() == name; }); |
Tom Cherry | 9614e4d | 2017-09-19 10:31:27 -0700 | [diff] [blame] | 237 | if (it != persistent_properties->mutable_properties()->end()) { |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 238 | it->set_name(name); |
| 239 | it->set_value(value); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 240 | } else { |
Tom Cherry | 9614e4d | 2017-09-19 10:31:27 -0700 | [diff] [blame] | 241 | AddPersistentProperty(name, value, &persistent_properties.value()); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 244 | if (auto result = WritePersistentPropertyFile(*persistent_properties); !result.ok()) { |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 245 | LOG(ERROR) << "Could not store persistent property: " << result.error(); |
| 246 | } |
| 247 | } |
| 248 | |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 249 | PersistentProperties LoadPersistentProperties() { |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 250 | auto persistent_properties = LoadPersistentPropertyFile(); |
| 251 | |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 252 | if (!persistent_properties.ok()) { |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 253 | LOG(ERROR) << "Could not load single persistent property file, trying legacy directory: " |
| 254 | << persistent_properties.error(); |
| 255 | persistent_properties = LoadLegacyPersistentProperties(); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 256 | if (!persistent_properties.ok()) { |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 257 | LOG(ERROR) << "Unable to load legacy persistent properties: " |
| 258 | << persistent_properties.error(); |
| 259 | return {}; |
| 260 | } |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 261 | if (auto result = WritePersistentPropertyFile(*persistent_properties); result.ok()) { |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 262 | RemoveLegacyPersistentPropertyFiles(); |
| 263 | } else { |
| 264 | LOG(ERROR) << "Unable to write single persistent property file: " << result.error(); |
| 265 | // Fall through so that we still set the properties that we've read. |
| 266 | } |
| 267 | } |
| 268 | |
Dennis Shen | 79283ef | 2023-10-24 17:30:12 +0000 | [diff] [blame^] | 269 | // loop over to find all staged props |
| 270 | auto const staged_prefix = std::string_view("next_boot."); |
| 271 | auto staged_props = std::unordered_map<std::string, std::string>(); |
| 272 | for (const auto& property_record : persistent_properties->properties()) { |
| 273 | auto const& prop_name = property_record.name(); |
| 274 | auto const& prop_value = property_record.value(); |
| 275 | if (StartsWith(prop_name, staged_prefix)) { |
| 276 | auto actual_prop_name = prop_name.substr(staged_prefix.size()); |
| 277 | staged_props[actual_prop_name] = prop_value; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if (staged_props.empty()) { |
| 282 | return *persistent_properties; |
| 283 | } |
| 284 | |
| 285 | // if has staging, apply staging and perserve the original prop order |
| 286 | PersistentProperties updated_persistent_properties; |
| 287 | for (const auto& property_record : persistent_properties->properties()) { |
| 288 | auto const& prop_name = property_record.name(); |
| 289 | auto const& prop_value = property_record.value(); |
| 290 | |
| 291 | // don't include staged props anymore |
| 292 | if (StartsWith(prop_name, staged_prefix)) { |
| 293 | continue; |
| 294 | } |
| 295 | |
| 296 | auto iter = staged_props.find(prop_name); |
| 297 | if (iter != staged_props.end()) { |
| 298 | AddPersistentProperty(prop_name, iter->second, &updated_persistent_properties); |
| 299 | staged_props.erase(iter); |
| 300 | } else { |
| 301 | AddPersistentProperty(prop_name, prop_value, &updated_persistent_properties); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // add any additional staged props |
| 306 | for (auto const& [prop_name, prop_value] : staged_props) { |
| 307 | AddPersistentProperty(prop_name, prop_value, &updated_persistent_properties); |
| 308 | } |
| 309 | |
| 310 | // write current updated persist prop file |
| 311 | auto result = WritePersistentPropertyFile(updated_persistent_properties); |
| 312 | if (!result.ok()) { |
| 313 | LOG(ERROR) << "Could not store persistent property: " << result.error(); |
| 314 | } |
| 315 | |
| 316 | return updated_persistent_properties; |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Dennis Shen | 79283ef | 2023-10-24 17:30:12 +0000 | [diff] [blame^] | 319 | |
| 320 | |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 321 | } // namespace init |
| 322 | } // namespace android |