2017-08-05 00:59:03 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "persistent_properties.h"
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/system_properties.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/strings.h>
|
|
|
|
#include <android-base/unique_fd.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
2019-12-06 20:16:10 +01:00
|
|
|
using android::base::Dirname;
|
2017-08-05 00:59:03 +02:00
|
|
|
using android::base::ReadFdToString;
|
|
|
|
using android::base::StartsWith;
|
|
|
|
using android::base::unique_fd;
|
2019-12-06 20:16:10 +01:00
|
|
|
using android::base::WriteStringToFd;
|
2017-08-05 00:59:03 +02:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
|
|
|
std::string persistent_property_filename = "/data/property/persistent_properties";
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr const char kLegacyPersistentPropertyDir[] = "/data/property";
|
|
|
|
|
Move staging value application logic to persistent_properties and add
unit tests
1, Previous implementation has the staged prop application done in
property_service, which caused a number of unnecessary changes which
including exposing apis like AddPersistentProperty. In addition, it made
the property_service logic complicated. A better design is to have the
staged value application done while reading the persistent properties
from file. This way, no change to property service. In addition, unit
test is much cleaner and efficient.
2, add a unit test to lock down the behavior. Specifically, it locks down that when a prop is staged, it should be applied the next time when the persistent prop is loaded. In addition, it should lock down that other persistent props are not overwritten.
Bug: b/307752841, b/300111812, b/306062513
Change-Id: I43c603efbb803195065dda3f0bc2145716302bbc
2023-10-24 19:30:12 +02:00
|
|
|
void AddPersistentProperty(const std::string& name, const std::string& value,
|
|
|
|
PersistentProperties* persistent_properties) {
|
|
|
|
auto persistent_property_record = persistent_properties->add_properties();
|
|
|
|
persistent_property_record->set_name(name);
|
|
|
|
persistent_property_record->set_value(value);
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:44:04 +02:00
|
|
|
Result<PersistentProperties> LoadLegacyPersistentProperties() {
|
2017-08-05 00:59:03 +02:00
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(kLegacyPersistentPropertyDir), closedir);
|
|
|
|
if (!dir) {
|
|
|
|
return ErrnoError() << "Unable to open persistent property directory \""
|
|
|
|
<< kLegacyPersistentPropertyDir << "\"";
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:44:04 +02:00
|
|
|
PersistentProperties persistent_properties;
|
2017-08-05 00:59:03 +02:00
|
|
|
dirent* entry;
|
|
|
|
while ((entry = readdir(dir.get())) != nullptr) {
|
|
|
|
if (!StartsWith(entry->d_name, "persist.")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (entry->d_type != DT_REG) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-09 00:09:36 +02:00
|
|
|
unique_fd fd(openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
|
2017-08-05 00:59:03 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat sb;
|
2022-12-03 03:48:15 +01:00
|
|
|
if (fstat(fd.get(), &sb) == -1) {
|
2017-08-05 00:59:03 +02:00
|
|
|
PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// File must not be accessible to others, be owned by root/root, and
|
|
|
|
// not be a hard link to any other file.
|
|
|
|
if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 ||
|
|
|
|
sb.st_nlink != 1) {
|
|
|
|
PLOG(ERROR) << "skipping insecure property file " << entry->d_name
|
|
|
|
<< " (uid=" << sb.st_uid << " gid=" << sb.st_gid << " nlink=" << sb.st_nlink
|
|
|
|
<< " mode=" << std::oct << sb.st_mode << ")";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string value;
|
|
|
|
if (ReadFdToString(fd, &value)) {
|
2017-09-16 00:44:04 +02:00
|
|
|
AddPersistentProperty(entry->d_name, value, &persistent_properties);
|
2017-08-05 00:59:03 +02:00
|
|
|
} else {
|
|
|
|
PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return persistent_properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveLegacyPersistentPropertyFiles() {
|
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(kLegacyPersistentPropertyDir), closedir);
|
|
|
|
if (!dir) {
|
|
|
|
PLOG(ERROR) << "Unable to open persistent property directory \""
|
|
|
|
<< kLegacyPersistentPropertyDir << "\"";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirent* entry;
|
|
|
|
while ((entry = readdir(dir.get())) != nullptr) {
|
|
|
|
if (!StartsWith(entry->d_name, "persist.")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (entry->d_type != DT_REG) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
unlinkat(dirfd(dir.get()), entry->d_name, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:44:04 +02:00
|
|
|
Result<std::string> ReadPersistentPropertyFile() {
|
2017-08-05 00:59:03 +02:00
|
|
|
const std::string temp_filename = persistent_property_filename + ".tmp";
|
|
|
|
if (access(temp_filename.c_str(), F_OK) == 0) {
|
|
|
|
LOG(INFO)
|
|
|
|
<< "Found temporary property file while attempting to persistent system properties"
|
|
|
|
" a previous persistent property write may have failed";
|
|
|
|
unlink(temp_filename.c_str());
|
|
|
|
}
|
|
|
|
auto file_contents = ReadFile(persistent_property_filename);
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!file_contents.ok()) {
|
2017-08-05 00:59:03 +02:00
|
|
|
return Error() << "Unable to read persistent property file: " << file_contents.error();
|
|
|
|
}
|
2017-09-16 00:44:04 +02:00
|
|
|
return *file_contents;
|
2017-08-05 00:59:03 +02:00
|
|
|
}
|
|
|
|
|
2022-08-25 20:38:27 +02:00
|
|
|
Result<PersistentProperties> ParsePersistentPropertyFile(const std::string& file_contents) {
|
|
|
|
PersistentProperties persistent_properties;
|
|
|
|
if (!persistent_properties.ParseFromString(file_contents)) {
|
|
|
|
return Error() << "Unable to parse persistent property file: Could not parse protobuf";
|
|
|
|
}
|
|
|
|
for (auto& prop : persistent_properties.properties()) {
|
2023-09-18 21:52:52 +02:00
|
|
|
if (!StartsWith(prop.name(), "persist.") && !StartsWith(prop.name(), "next_boot.")) {
|
2022-08-25 20:38:27 +02:00
|
|
|
return Error() << "Unable to load persistent property file: property '" << prop.name()
|
2023-09-18 21:52:52 +02:00
|
|
|
<< "' doesn't start with 'persist.' or 'next_boot.'";
|
2022-08-25 20:38:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return persistent_properties;
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:44:04 +02:00
|
|
|
} // namespace
|
2017-08-05 00:59:03 +02:00
|
|
|
|
2017-09-16 00:44:04 +02:00
|
|
|
Result<PersistentProperties> LoadPersistentPropertyFile() {
|
|
|
|
auto file_contents = ReadPersistentPropertyFile();
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!file_contents.ok()) return file_contents.error();
|
2017-08-05 00:59:03 +02:00
|
|
|
|
2022-08-25 20:38:27 +02:00
|
|
|
auto persistent_properties = ParsePersistentPropertyFile(*file_contents);
|
|
|
|
if (!persistent_properties.ok()) {
|
|
|
|
// If the file cannot be parsed in either format, then we don't have any recovery
|
|
|
|
// mechanisms, so we delete it to allow for future writes to take place successfully.
|
|
|
|
unlink(persistent_property_filename.c_str());
|
|
|
|
}
|
|
|
|
return persistent_properties;
|
2017-09-16 00:44:04 +02:00
|
|
|
}
|
2017-08-05 00:59:03 +02:00
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> WritePersistentPropertyFile(const PersistentProperties& persistent_properties) {
|
2017-08-05 00:59:03 +02:00
|
|
|
const std::string temp_filename = persistent_property_filename + ".tmp";
|
|
|
|
unique_fd fd(TEMP_FAILURE_RETRY(
|
|
|
|
open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0600)));
|
|
|
|
if (fd == -1) {
|
|
|
|
return ErrnoError() << "Could not open temporary properties file";
|
|
|
|
}
|
2017-09-16 00:44:04 +02:00
|
|
|
std::string serialized_string;
|
|
|
|
if (!persistent_properties.SerializeToString(&serialized_string)) {
|
|
|
|
return Error() << "Unable to serialize properties";
|
|
|
|
}
|
|
|
|
if (!WriteStringToFd(serialized_string, fd)) {
|
2017-08-05 00:59:03 +02:00
|
|
|
return ErrnoError() << "Unable to write file contents";
|
|
|
|
}
|
2022-12-03 03:48:15 +01:00
|
|
|
fsync(fd.get());
|
2017-08-05 00:59:03 +02:00
|
|
|
fd.reset();
|
|
|
|
|
|
|
|
if (rename(temp_filename.c_str(), persistent_property_filename.c_str())) {
|
|
|
|
int saved_errno = errno;
|
|
|
|
unlink(temp_filename.c_str());
|
|
|
|
return Error(saved_errno) << "Unable to rename persistent property file";
|
|
|
|
}
|
2019-12-06 20:16:10 +01:00
|
|
|
|
|
|
|
// rename() is atomic with regards to the kernel's filesystem buffers, but the parent
|
|
|
|
// directories must be fsync()'ed otherwise, the rename is not necessarily written to storage.
|
|
|
|
// Note in this case, that the source and destination directories are the same, so only one
|
|
|
|
// fsync() is required.
|
|
|
|
auto dir = Dirname(persistent_property_filename);
|
2019-12-09 15:48:37 +01:00
|
|
|
auto dir_fd = unique_fd{open(dir.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC)};
|
2019-12-06 20:16:10 +01:00
|
|
|
if (dir_fd < 0) {
|
|
|
|
return ErrnoError() << "Unable to open persistent properties directory for fsync()";
|
|
|
|
}
|
2022-12-03 03:48:15 +01:00
|
|
|
fsync(dir_fd.get());
|
2019-12-06 20:16:10 +01:00
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2017-08-05 00:59:03 +02:00
|
|
|
}
|
|
|
|
|
2023-10-20 13:42:56 +02:00
|
|
|
PersistentProperties LoadPersistentPropertiesFromMemory() {
|
|
|
|
PersistentProperties persistent_properties;
|
|
|
|
__system_property_foreach(
|
|
|
|
[](const prop_info* pi, void* cookie) {
|
|
|
|
__system_property_read_callback(
|
|
|
|
pi,
|
|
|
|
[](void* cookie, const char* name, const char* value, unsigned serial) {
|
|
|
|
if (StartsWith(name, "persist.")) {
|
|
|
|
auto properties = reinterpret_cast<PersistentProperties*>(cookie);
|
|
|
|
AddPersistentProperty(name, value, properties);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
cookie);
|
|
|
|
},
|
|
|
|
&persistent_properties);
|
|
|
|
return persistent_properties;
|
|
|
|
}
|
|
|
|
|
2017-08-05 00:59:03 +02:00
|
|
|
// Persistent properties are not written often, so we rather not keep any data in memory and read
|
|
|
|
// then rewrite the persistent property file for each update.
|
|
|
|
void WritePersistentProperty(const std::string& name, const std::string& value) {
|
2017-09-19 19:31:27 +02:00
|
|
|
auto persistent_properties = LoadPersistentPropertyFile();
|
2017-09-16 00:44:04 +02:00
|
|
|
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!persistent_properties.ok()) {
|
2017-08-05 00:59:03 +02:00
|
|
|
LOG(ERROR) << "Recovering persistent properties from memory: "
|
2017-09-19 19:31:27 +02:00
|
|
|
<< persistent_properties.error();
|
2017-08-05 00:59:03 +02:00
|
|
|
persistent_properties = LoadPersistentPropertiesFromMemory();
|
|
|
|
}
|
2017-09-19 19:31:27 +02:00
|
|
|
auto it = std::find_if(persistent_properties->mutable_properties()->begin(),
|
|
|
|
persistent_properties->mutable_properties()->end(),
|
2017-09-16 00:44:04 +02:00
|
|
|
[&name](const auto& record) { return record.name() == name; });
|
2017-09-19 19:31:27 +02:00
|
|
|
if (it != persistent_properties->mutable_properties()->end()) {
|
2017-09-16 00:44:04 +02:00
|
|
|
it->set_name(name);
|
|
|
|
it->set_value(value);
|
2017-08-05 00:59:03 +02:00
|
|
|
} else {
|
2017-09-19 19:31:27 +02:00
|
|
|
AddPersistentProperty(name, value, &persistent_properties.value());
|
2017-08-05 00:59:03 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = WritePersistentPropertyFile(*persistent_properties); !result.ok()) {
|
2017-08-05 00:59:03 +02:00
|
|
|
LOG(ERROR) << "Could not store persistent property: " << result.error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:44:04 +02:00
|
|
|
PersistentProperties LoadPersistentProperties() {
|
2017-08-05 00:59:03 +02:00
|
|
|
auto persistent_properties = LoadPersistentPropertyFile();
|
|
|
|
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!persistent_properties.ok()) {
|
2017-08-05 00:59:03 +02:00
|
|
|
LOG(ERROR) << "Could not load single persistent property file, trying legacy directory: "
|
|
|
|
<< persistent_properties.error();
|
|
|
|
persistent_properties = LoadLegacyPersistentProperties();
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!persistent_properties.ok()) {
|
2017-08-05 00:59:03 +02:00
|
|
|
LOG(ERROR) << "Unable to load legacy persistent properties: "
|
|
|
|
<< persistent_properties.error();
|
|
|
|
return {};
|
|
|
|
}
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = WritePersistentPropertyFile(*persistent_properties); result.ok()) {
|
2017-08-05 00:59:03 +02:00
|
|
|
RemoveLegacyPersistentPropertyFiles();
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Unable to write single persistent property file: " << result.error();
|
|
|
|
// Fall through so that we still set the properties that we've read.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Move staging value application logic to persistent_properties and add
unit tests
1, Previous implementation has the staged prop application done in
property_service, which caused a number of unnecessary changes which
including exposing apis like AddPersistentProperty. In addition, it made
the property_service logic complicated. A better design is to have the
staged value application done while reading the persistent properties
from file. This way, no change to property service. In addition, unit
test is much cleaner and efficient.
2, add a unit test to lock down the behavior. Specifically, it locks down that when a prop is staged, it should be applied the next time when the persistent prop is loaded. In addition, it should lock down that other persistent props are not overwritten.
Bug: b/307752841, b/300111812, b/306062513
Change-Id: I43c603efbb803195065dda3f0bc2145716302bbc
2023-10-24 19:30:12 +02:00
|
|
|
// loop over to find all staged props
|
|
|
|
auto const staged_prefix = std::string_view("next_boot.");
|
|
|
|
auto staged_props = std::unordered_map<std::string, std::string>();
|
|
|
|
for (const auto& property_record : persistent_properties->properties()) {
|
|
|
|
auto const& prop_name = property_record.name();
|
|
|
|
auto const& prop_value = property_record.value();
|
|
|
|
if (StartsWith(prop_name, staged_prefix)) {
|
|
|
|
auto actual_prop_name = prop_name.substr(staged_prefix.size());
|
|
|
|
staged_props[actual_prop_name] = prop_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (staged_props.empty()) {
|
|
|
|
return *persistent_properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if has staging, apply staging and perserve the original prop order
|
|
|
|
PersistentProperties updated_persistent_properties;
|
|
|
|
for (const auto& property_record : persistent_properties->properties()) {
|
|
|
|
auto const& prop_name = property_record.name();
|
|
|
|
auto const& prop_value = property_record.value();
|
|
|
|
|
|
|
|
// don't include staged props anymore
|
|
|
|
if (StartsWith(prop_name, staged_prefix)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto iter = staged_props.find(prop_name);
|
|
|
|
if (iter != staged_props.end()) {
|
|
|
|
AddPersistentProperty(prop_name, iter->second, &updated_persistent_properties);
|
|
|
|
staged_props.erase(iter);
|
|
|
|
} else {
|
|
|
|
AddPersistentProperty(prop_name, prop_value, &updated_persistent_properties);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add any additional staged props
|
|
|
|
for (auto const& [prop_name, prop_value] : staged_props) {
|
|
|
|
AddPersistentProperty(prop_name, prop_value, &updated_persistent_properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write current updated persist prop file
|
|
|
|
auto result = WritePersistentPropertyFile(updated_persistent_properties);
|
|
|
|
if (!result.ok()) {
|
|
|
|
LOG(ERROR) << "Could not store persistent property: " << result.error();
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated_persistent_properties;
|
2017-08-05 00:59:03 +02:00
|
|
|
}
|
|
|
|
|
Move staging value application logic to persistent_properties and add
unit tests
1, Previous implementation has the staged prop application done in
property_service, which caused a number of unnecessary changes which
including exposing apis like AddPersistentProperty. In addition, it made
the property_service logic complicated. A better design is to have the
staged value application done while reading the persistent properties
from file. This way, no change to property service. In addition, unit
test is much cleaner and efficient.
2, add a unit test to lock down the behavior. Specifically, it locks down that when a prop is staged, it should be applied the next time when the persistent prop is loaded. In addition, it should lock down that other persistent props are not overwritten.
Bug: b/307752841, b/300111812, b/306062513
Change-Id: I43c603efbb803195065dda3f0bc2145716302bbc
2023-10-24 19:30:12 +02:00
|
|
|
|
|
|
|
|
2017-08-05 00:59:03 +02:00
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|