2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 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.
|
|
|
|
*/
|
|
|
|
|
2017-04-07 01:30:22 +02:00
|
|
|
#include "property_service.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2017-02-18 02:48:56 +01:00
|
|
|
#include <inttypes.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <limits.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdarg.h>
|
2017-04-07 22:46:21 +02:00
|
|
|
#include <stddef.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <sys/mman.h>
|
2014-01-31 23:37:07 +01:00
|
|
|
#include <sys/poll.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
2015-03-10 16:39:45 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
|
|
|
#include <sys/_system_properties.h>
|
|
|
|
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
2012-08-09 16:05:49 +02:00
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/file.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <android-base/logging.h>
|
2017-02-18 02:48:56 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2017-01-24 21:43:58 +01:00
|
|
|
#include <android-base/strings.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <bootimg.h>
|
|
|
|
#include <fs_mgr.h>
|
|
|
|
#include <selinux/android.h>
|
|
|
|
#include <selinux/label.h>
|
|
|
|
#include <selinux/selinux.h>
|
2015-05-08 17:30:33 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "init.h"
|
2010-04-14 05:35:46 +02:00
|
|
|
#include "util.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-18 02:48:56 +01:00
|
|
|
using android::base::StringPrintf;
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#define PERSISTENT_PROPERTY_DIR "/data/property"
|
2015-05-08 17:30:33 +02:00
|
|
|
#define RECOVERY_MOUNT_POINT "/recovery"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
static int persistent_properties_loaded = 0;
|
|
|
|
|
2010-04-14 04:33:37 +02:00
|
|
|
static int property_set_fd = -1;
|
|
|
|
|
2015-03-28 07:20:44 +01:00
|
|
|
void property_init() {
|
|
|
|
if (__system_property_area_init()) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(ERROR) << "Failed to initialize property area";
|
2015-12-02 02:16:53 +01:00
|
|
|
exit(1);
|
2015-03-28 07:20:44 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
static bool check_mac_perms(const std::string& name, char* sctx, struct ucred* cr) {
|
2012-08-09 16:05:49 +02:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
if (!sctx) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-09 16:05:49 +02:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
if (!sehandle_prop) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-09 16:05:49 +02:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
char* tctx = nullptr;
|
|
|
|
if (selabel_lookup(sehandle_prop, &tctx, name.c_str(), 1) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-09 16:05:49 +02:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
property_audit_data audit_data;
|
|
|
|
|
|
|
|
audit_data.name = name.c_str();
|
2015-10-02 01:03:47 +02:00
|
|
|
audit_data.cr = cr;
|
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
bool has_access = (selinux_check_access(sctx, tctx, "property_service", "set", &audit_data) == 0);
|
2012-08-09 16:05:49 +02:00
|
|
|
|
|
|
|
freecon(tctx);
|
2017-01-24 21:43:58 +01:00
|
|
|
return has_access;
|
2012-08-09 16:05:49 +02:00
|
|
|
}
|
|
|
|
|
2015-10-02 01:03:47 +02:00
|
|
|
static int check_control_mac_perms(const char *name, char *sctx, struct ucred *cr)
|
2012-08-09 16:05:49 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Create a name prefix out of ctl.<service name>
|
|
|
|
* The new prefix allows the use of the existing
|
|
|
|
* property service backend labeling while avoiding
|
|
|
|
* mislabels based on true property prefixes.
|
|
|
|
*/
|
|
|
|
char ctl_name[PROP_VALUE_MAX+4];
|
|
|
|
int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
|
|
|
|
|
|
|
|
if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
|
|
|
|
return 0;
|
|
|
|
|
2015-10-02 01:03:47 +02:00
|
|
|
return check_mac_perms(ctl_name, sctx, cr);
|
2012-08-09 16:05:49 +02:00
|
|
|
}
|
|
|
|
|
2009-08-26 04:13:20 +02:00
|
|
|
static void write_persistent_property(const char *name, const char *value)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2012-10-02 23:59:59 +02:00
|
|
|
char tempPath[PATH_MAX];
|
2009-03-04 04:32:55 +01:00
|
|
|
char path[PATH_MAX];
|
2012-10-02 23:59:59 +02:00
|
|
|
int fd;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2012-10-02 23:59:59 +02:00
|
|
|
snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
|
|
|
|
fd = mkstemp(tempPath);
|
2009-03-04 04:32:55 +01:00
|
|
|
if (fd < 0) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "Unable to write persistent property to temp file " << tempPath;
|
2009-08-26 04:13:20 +02:00
|
|
|
return;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
write(fd, value, strlen(value));
|
2014-03-28 12:12:47 +01:00
|
|
|
fsync(fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
close(fd);
|
|
|
|
|
2012-10-02 23:59:59 +02:00
|
|
|
snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
|
2009-03-04 04:32:55 +01:00
|
|
|
if (rename(tempPath, path)) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "Unable to rename persistent property file " << tempPath << " to " << path;
|
2009-03-04 04:32:55 +01:00
|
|
|
unlink(tempPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
bool is_legal_property_name(const std::string& name) {
|
2016-09-20 05:58:20 +02:00
|
|
|
size_t namelen = name.size();
|
|
|
|
|
2013-09-14 02:21:28 +02:00
|
|
|
if (namelen < 1) return false;
|
|
|
|
if (name[0] == '.') return false;
|
|
|
|
if (name[namelen - 1] == '.') return false;
|
|
|
|
|
2016-09-20 05:58:20 +02:00
|
|
|
/* Only allow alphanumeric, plus '.', '-', '@', or '_' */
|
2013-09-14 02:21:28 +02:00
|
|
|
/* Don't allow ".." to appear in a property name */
|
2016-09-20 05:58:20 +02:00
|
|
|
for (size_t i = 0; i < namelen; i++) {
|
2013-09-14 02:21:28 +02:00
|
|
|
if (name[i] == '.') {
|
2013-09-16 20:30:48 +02:00
|
|
|
// i=0 is guaranteed to never have a dot. See above.
|
|
|
|
if (name[i-1] == '.') return false;
|
2013-09-14 02:21:28 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-09-20 05:58:20 +02:00
|
|
|
if (name[i] == '_' || name[i] == '-' || name[i] == '@') continue;
|
2013-09-14 02:21:28 +02:00
|
|
|
if (name[i] >= 'a' && name[i] <= 'z') continue;
|
|
|
|
if (name[i] >= 'A' && name[i] <= 'Z') continue;
|
|
|
|
if (name[i] >= '0' && name[i] <= '9') continue;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
uint32_t property_set(const std::string& name, const std::string& value) {
|
|
|
|
size_t valuelen = value.size();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-11-29 20:20:58 +01:00
|
|
|
if (!is_legal_property_name(name)) {
|
|
|
|
LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: bad name";
|
2017-01-24 21:43:58 +01:00
|
|
|
return PROP_ERROR_INVALID_NAME;
|
2016-11-29 20:20:58 +01:00
|
|
|
}
|
2017-01-24 21:43:58 +01:00
|
|
|
|
2016-11-29 20:20:58 +01:00
|
|
|
if (valuelen >= PROP_VALUE_MAX) {
|
|
|
|
LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
|
|
|
|
<< "value too long";
|
2017-01-24 21:43:58 +01:00
|
|
|
return PROP_ERROR_INVALID_VALUE;
|
2016-11-29 20:20:58 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
if (name == "selinux.restorecon_recursive" && valuelen > 0) {
|
|
|
|
if (restorecon(value.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(ERROR) << "Failed to restorecon_recursive " << value;
|
2015-06-09 20:02:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
prop_info* pi = (prop_info*) __system_property_find(name.c_str());
|
2016-11-29 20:20:58 +01:00
|
|
|
if (pi != nullptr) {
|
|
|
|
// ro.* properties are actually "write-once".
|
2017-01-24 21:43:58 +01:00
|
|
|
if (android::base::StartsWith(name, "ro.")) {
|
2016-11-29 20:20:58 +01:00
|
|
|
LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
|
|
|
|
<< "property already set";
|
2017-01-24 21:43:58 +01:00
|
|
|
return PROP_ERROR_READ_ONLY_PROPERTY;
|
2016-11-29 20:20:58 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
__system_property_update(pi, value.c_str(), valuelen);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2017-01-24 21:43:58 +01:00
|
|
|
int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
|
2015-03-20 17:45:18 +01:00
|
|
|
if (rc < 0) {
|
2016-11-29 20:20:58 +01:00
|
|
|
LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
|
|
|
|
<< "__system_property_add failed";
|
2017-01-24 21:43:58 +01:00
|
|
|
return PROP_ERROR_SET_FAILED;
|
2013-04-29 09:11:57 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-11-29 20:20:58 +01:00
|
|
|
|
2016-12-05 22:12:48 +01:00
|
|
|
// Don't write properties to disk until after we have read all default
|
|
|
|
// properties to prevent them from being overwritten by default values.
|
2017-01-24 21:43:58 +01:00
|
|
|
if (persistent_properties_loaded && android::base::StartsWith(name, "persist.")) {
|
|
|
|
write_persistent_property(name.c_str(), value.c_str());
|
2017-01-19 23:53:00 +01:00
|
|
|
}
|
2017-01-24 21:43:58 +01:00
|
|
|
property_changed(name.c_str(), value.c_str());
|
|
|
|
return PROP_SUCCESS;
|
2017-01-19 23:53:00 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
class SocketConnection {
|
|
|
|
public:
|
|
|
|
SocketConnection(int socket, const struct ucred& cred)
|
|
|
|
: socket_(socket), cred_(cred) {}
|
|
|
|
|
|
|
|
~SocketConnection() {
|
|
|
|
close(socket_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
|
|
|
|
return RecvFully(value, sizeof(*value), timeout_ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RecvChars(char* chars, size_t size, uint32_t* timeout_ms) {
|
|
|
|
return RecvFully(chars, size, timeout_ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RecvString(std::string* value, uint32_t* timeout_ms) {
|
|
|
|
uint32_t len = 0;
|
|
|
|
if (!RecvUint32(&len, timeout_ms)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
*value = "";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:54:15 +01:00
|
|
|
// http://b/35166374: don't allow init to make arbitrarily large allocations.
|
|
|
|
if (len > 0xffff) {
|
|
|
|
LOG(ERROR) << "sys_prop: RecvString asked to read huge string: " << len;
|
|
|
|
errno = ENOMEM;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
std::vector<char> chars(len);
|
|
|
|
if (!RecvChars(&chars[0], len, timeout_ms)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*value = std::string(&chars[0], len);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SendUint32(uint32_t value) {
|
|
|
|
int result = TEMP_FAILURE_RETRY(send(socket_, &value, sizeof(value), 0));
|
|
|
|
return result == sizeof(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int socket() {
|
|
|
|
return socket_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct ucred& cred() {
|
|
|
|
return cred_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool PollIn(uint32_t* timeout_ms) {
|
|
|
|
struct pollfd ufds[1];
|
|
|
|
ufds[0].fd = socket_;
|
|
|
|
ufds[0].events = POLLIN;
|
|
|
|
ufds[0].revents = 0;
|
|
|
|
while (*timeout_ms > 0) {
|
|
|
|
Timer timer;
|
|
|
|
int nr = poll(ufds, 1, *timeout_ms);
|
2017-01-26 20:55:44 +01:00
|
|
|
uint64_t millis = timer.duration_ms();
|
2017-01-24 21:43:58 +01:00
|
|
|
*timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis;
|
|
|
|
|
|
|
|
if (nr > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nr == 0) {
|
|
|
|
// Timeout
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nr < 0 && errno != EINTR) {
|
|
|
|
PLOG(ERROR) << "sys_prop: error waiting for uid " << cred_.uid << " to send property message";
|
|
|
|
return false;
|
|
|
|
} else { // errno == EINTR
|
|
|
|
// Timer rounds milliseconds down in case of EINTR we want it to be rounded up
|
|
|
|
// to avoid slowing init down by causing EINTR with under millisecond timeout.
|
|
|
|
if (*timeout_ms > 0) {
|
|
|
|
--(*timeout_ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(ERROR) << "sys_prop: timeout waiting for uid " << cred_.uid << " to send property message.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RecvFully(void* data_ptr, size_t size, uint32_t* timeout_ms) {
|
|
|
|
size_t bytes_left = size;
|
|
|
|
char* data = static_cast<char*>(data_ptr);
|
|
|
|
while (*timeout_ms > 0 && bytes_left > 0) {
|
|
|
|
if (!PollIn(timeout_ms)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
|
|
|
|
if (result <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes_left -= result;
|
|
|
|
data += result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes_left == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int socket_;
|
|
|
|
struct ucred cred_;
|
|
|
|
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
|
|
|
|
};
|
|
|
|
|
|
|
|
static void handle_property_set(SocketConnection& socket,
|
|
|
|
const std::string& name,
|
|
|
|
const std::string& value,
|
|
|
|
bool legacy_protocol) {
|
|
|
|
const char* cmd_name = legacy_protocol ? "PROP_MSG_SETPROP" : "PROP_MSG_SETPROP2";
|
|
|
|
if (!is_legal_property_name(name)) {
|
|
|
|
LOG(ERROR) << "sys_prop(" << cmd_name << "): illegal property name \"" << name << "\"";
|
|
|
|
socket.SendUint32(PROP_ERROR_INVALID_NAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ucred cr = socket.cred();
|
|
|
|
char* source_ctx = nullptr;
|
|
|
|
getpeercon(socket.socket(), &source_ctx);
|
|
|
|
|
|
|
|
if (android::base::StartsWith(name, "ctl.")) {
|
|
|
|
if (check_control_mac_perms(value.c_str(), source_ctx, &cr)) {
|
|
|
|
handle_control_message(name.c_str() + 4, value.c_str());
|
|
|
|
if (!legacy_protocol) {
|
|
|
|
socket.SendUint32(PROP_SUCCESS);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "sys_prop(" << cmd_name << "): Unable to " << (name.c_str() + 4)
|
|
|
|
<< " service ctl [" << value << "]"
|
|
|
|
<< " uid:" << cr.uid
|
|
|
|
<< " gid:" << cr.gid
|
|
|
|
<< " pid:" << cr.pid;
|
|
|
|
if (!legacy_protocol) {
|
|
|
|
socket.SendUint32(PROP_ERROR_HANDLE_CONTROL_MESSAGE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (check_mac_perms(name, source_ctx, &cr)) {
|
|
|
|
uint32_t result = property_set(name, value);
|
|
|
|
if (!legacy_protocol) {
|
|
|
|
socket.SendUint32(result);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "sys_prop(" << cmd_name << "): permission denied uid:" << cr.uid << " name:" << name;
|
|
|
|
if (!legacy_protocol) {
|
|
|
|
socket.SendUint32(PROP_ERROR_PERMISSION_DENIED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freecon(source_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_property_set_fd() {
|
|
|
|
static constexpr uint32_t kDefaultSocketTimeout = 2000; /* ms */
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-01-25 14:08:51 +01:00
|
|
|
int s = accept4(property_set_fd, nullptr, nullptr, SOCK_CLOEXEC);
|
2016-08-23 21:50:00 +02:00
|
|
|
if (s == -1) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-23 21:50:00 +02:00
|
|
|
struct ucred cr;
|
|
|
|
socklen_t cr_size = sizeof(cr);
|
2009-03-04 04:32:55 +01:00
|
|
|
if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
|
|
|
|
close(s);
|
2017-02-22 23:54:15 +01:00
|
|
|
PLOG(ERROR) << "sys_prop: unable to get SO_PEERCRED";
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
SocketConnection socket(s, cr);
|
|
|
|
uint32_t timeout_ms = kDefaultSocketTimeout;
|
2017-01-19 23:53:00 +01:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
uint32_t cmd = 0;
|
|
|
|
if (!socket.RecvUint32(&cmd, &timeout_ms)) {
|
|
|
|
PLOG(ERROR) << "sys_prop: error while reading command from the socket";
|
|
|
|
socket.SendUint32(PROP_ERROR_READ_CMD);
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:54:15 +01:00
|
|
|
switch (cmd) {
|
2017-01-24 21:43:58 +01:00
|
|
|
case PROP_MSG_SETPROP: {
|
|
|
|
char prop_name[PROP_NAME_MAX];
|
|
|
|
char prop_value[PROP_VALUE_MAX];
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
if (!socket.RecvChars(prop_name, PROP_NAME_MAX, &timeout_ms) ||
|
|
|
|
!socket.RecvChars(prop_value, PROP_VALUE_MAX, &timeout_ms)) {
|
|
|
|
PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP): error while reading name/value from the socket";
|
|
|
|
return;
|
2013-09-14 02:21:28 +02:00
|
|
|
}
|
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
prop_name[PROP_NAME_MAX-1] = 0;
|
|
|
|
prop_value[PROP_VALUE_MAX-1] = 0;
|
2011-04-01 17:24:13 +02:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
handle_property_set(socket, prop_value, prop_value, true);
|
2017-01-19 23:53:00 +01:00
|
|
|
break;
|
2017-01-24 21:43:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case PROP_MSG_SETPROP2: {
|
|
|
|
std::string name;
|
|
|
|
std::string value;
|
|
|
|
if (!socket.RecvString(&name, &timeout_ms) ||
|
|
|
|
!socket.RecvString(&value, &timeout_ms)) {
|
|
|
|
PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP2): error while reading name/value from the socket";
|
|
|
|
socket.SendUint32(PROP_ERROR_READ_DATA);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-24 19:38:09 +01:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
handle_property_set(socket, name, value, false);
|
|
|
|
break;
|
|
|
|
}
|
2017-02-22 23:54:15 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
default:
|
2017-02-22 23:54:15 +01:00
|
|
|
LOG(ERROR) << "sys_prop: invalid command " << cmd;
|
2017-01-24 21:43:58 +01:00
|
|
|
socket.SendUint32(PROP_ERROR_INVALID_CMD);
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 23:26:36 +01:00
|
|
|
static void load_properties_from_file(const char *, const char *);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Filter is used to decide which properties to load: NULL loads all keys,
|
|
|
|
* "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
|
|
|
|
*/
|
|
|
|
static void load_properties(char *data, const char *filter)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2014-03-06 23:26:36 +01:00
|
|
|
char *key, *value, *eol, *sol, *tmp, *fn;
|
|
|
|
size_t flen = 0;
|
|
|
|
|
|
|
|
if (filter) {
|
|
|
|
flen = strlen(filter);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
sol = data;
|
2014-03-06 23:26:36 +01:00
|
|
|
while ((eol = strchr(sol, '\n'))) {
|
2009-03-04 04:32:55 +01:00
|
|
|
key = sol;
|
|
|
|
*eol++ = 0;
|
|
|
|
sol = eol;
|
|
|
|
|
2014-03-06 23:26:36 +01:00
|
|
|
while (isspace(*key)) key++;
|
|
|
|
if (*key == '#') continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-03-06 23:26:36 +01:00
|
|
|
tmp = eol - 2;
|
|
|
|
while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-03-06 23:26:36 +01:00
|
|
|
if (!strncmp(key, "import ", 7) && flen == 0) {
|
|
|
|
fn = key + 7;
|
|
|
|
while (isspace(*fn)) fn++;
|
2012-10-13 00:23:40 +02:00
|
|
|
|
2014-03-06 23:26:36 +01:00
|
|
|
key = strchr(fn, ' ');
|
|
|
|
if (key) {
|
|
|
|
*key++ = 0;
|
|
|
|
while (isspace(*key)) key++;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-03-06 23:26:36 +01:00
|
|
|
load_properties_from_file(fn, key);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
value = strchr(key, '=');
|
|
|
|
if (!value) continue;
|
|
|
|
*value++ = 0;
|
|
|
|
|
|
|
|
tmp = value - 2;
|
|
|
|
while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
|
|
|
|
|
|
|
|
while (isspace(*value)) value++;
|
|
|
|
|
|
|
|
if (flen > 0) {
|
|
|
|
if (filter[flen - 1] == '*') {
|
|
|
|
if (strncmp(key, filter, flen - 1)) continue;
|
|
|
|
} else {
|
|
|
|
if (strcmp(key, filter)) continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
property_set(key, value);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 23:12:33 +02:00
|
|
|
// Filter is used to decide which properties to load: NULL loads all keys,
|
|
|
|
// "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
|
2015-03-28 07:20:44 +01:00
|
|
|
static void load_properties_from_file(const char* filename, const char* filter) {
|
|
|
|
Timer t;
|
2015-02-06 21:19:48 +01:00
|
|
|
std::string data;
|
2016-09-30 23:12:33 +02:00
|
|
|
if (!read_file(filename, &data)) {
|
|
|
|
PLOG(WARNING) << "Couldn't load properties from " << filename;
|
|
|
|
return;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-09-30 23:12:33 +02:00
|
|
|
data.push_back('\n');
|
|
|
|
load_properties(&data[0], filter);
|
2016-11-29 20:20:58 +01:00
|
|
|
LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t << ".)";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-03-10 16:39:45 +01:00
|
|
|
static void load_persistent_properties() {
|
|
|
|
persistent_properties_loaded = 1;
|
2012-10-02 23:59:59 +02:00
|
|
|
|
2015-03-10 16:39:45 +01:00
|
|
|
std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
|
|
|
|
if (!dir) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "Unable to open persistent property directory \""
|
|
|
|
<< PERSISTENT_PROPERTY_DIR << "\"";
|
2015-03-10 16:39:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-10-02 23:59:59 +02:00
|
|
|
|
2015-03-10 16:39:45 +01:00
|
|
|
struct dirent* entry;
|
|
|
|
while ((entry = readdir(dir.get())) != NULL) {
|
|
|
|
if (strncmp("persist.", entry->d_name, strlen("persist."))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (entry->d_type != DT_REG) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file and read the property value.
|
|
|
|
int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
|
|
|
|
if (fd == -1) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
|
2015-03-10 16:39:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
if (fstat(fd, &sb) == -1) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
|
2012-10-02 23:59:59 +02:00
|
|
|
close(fd);
|
2015-03-10 16:39:45 +01:00
|
|
|
continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2009-08-26 04:13:20 +02:00
|
|
|
|
2015-03-10 16:39:45 +01:00
|
|
|
// File must not be accessible to others, be owned by root/root, and
|
|
|
|
// not be a hard link to any other file.
|
2016-06-25 00:12:21 +02:00
|
|
|
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 << ")";
|
2015-03-10 16:39:45 +01:00
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
char value[PROP_VALUE_MAX];
|
|
|
|
int length = read(fd, value, sizeof(value) - 1);
|
|
|
|
if (length >= 0) {
|
|
|
|
value[length] = 0;
|
|
|
|
property_set(entry->d_name, value);
|
|
|
|
} else {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
|
2015-03-10 16:39:45 +01:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-03-28 07:20:44 +01:00
|
|
|
void property_load_boot_defaults() {
|
2017-01-28 01:21:55 +01:00
|
|
|
load_properties_from_file("/default.prop", NULL);
|
|
|
|
load_properties_from_file("/odm/default.prop", NULL);
|
|
|
|
load_properties_from_file("/vendor/default.prop", NULL);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 20:15:24 +02:00
|
|
|
static void load_override_properties() {
|
2015-02-04 23:46:36 +01:00
|
|
|
if (ALLOW_LOCAL_PROP_OVERRIDE) {
|
2017-03-29 01:40:41 +02:00
|
|
|
load_properties_from_file("/data/local.prop", NULL);
|
2012-09-19 20:15:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-09 02:01:29 +01:00
|
|
|
/* When booting an encrypted system, /data is not mounted when the
|
|
|
|
* property service is started, so any properties stored there are
|
|
|
|
* not loaded. Vold triggers init to load these properties once it
|
|
|
|
* has mounted /data.
|
|
|
|
*/
|
2015-03-28 07:20:44 +01:00
|
|
|
void load_persist_props(void) {
|
2012-09-19 20:15:24 +02:00
|
|
|
load_override_properties();
|
2011-03-09 02:01:29 +01:00
|
|
|
/* Read persistent properties after all default values have been loaded. */
|
|
|
|
load_persistent_properties();
|
2017-03-01 04:20:38 +01:00
|
|
|
property_set("ro.persistent_properties.ready", "true");
|
2011-03-09 02:01:29 +01:00
|
|
|
}
|
|
|
|
|
2015-05-08 17:30:33 +02:00
|
|
|
void load_recovery_id_prop() {
|
2017-03-09 15:36:34 +01:00
|
|
|
std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
|
|
|
|
fs_mgr_free_fstab);
|
|
|
|
if (!fstab) {
|
|
|
|
PLOG(ERROR) << "unable to read default fstab";
|
2015-05-08 17:30:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-09 15:36:34 +01:00
|
|
|
fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab.get(), RECOVERY_MOUNT_POINT);
|
2015-05-08 17:30:33 +02:00
|
|
|
if (rec == NULL) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(ERROR) << "/recovery not specified in fstab";
|
2015-05-08 17:30:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = open(rec->blk_device, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "error opening block device " << rec->blk_device;
|
2015-05-08 17:30:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_img_hdr hdr;
|
|
|
|
if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
|
|
|
|
std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
|
|
|
|
property_set("ro.recovery_id", hex.c_str());
|
|
|
|
} else {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "error reading /recovery";
|
2015-05-08 17:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:40:56 +02:00
|
|
|
void load_system_props() {
|
2017-01-28 01:21:55 +01:00
|
|
|
load_properties_from_file("/system/build.prop", NULL);
|
|
|
|
load_properties_from_file("/odm/build.prop", NULL);
|
|
|
|
load_properties_from_file("/vendor/build.prop", NULL);
|
|
|
|
load_properties_from_file("/factory/factory.prop", "ro.*");
|
2015-05-08 17:30:33 +02:00
|
|
|
load_recovery_id_prop();
|
2014-06-17 00:06:21 +02:00
|
|
|
}
|
|
|
|
|
2015-03-28 07:20:44 +01:00
|
|
|
void start_property_service() {
|
2017-01-24 21:43:58 +01:00
|
|
|
property_set("ro.property_service.version", "2");
|
|
|
|
|
2015-04-25 03:50:30 +02:00
|
|
|
property_set_fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
|
|
|
|
0666, 0, 0, NULL);
|
|
|
|
if (property_set_fd == -1) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "start_property_service socket creation failed";
|
2015-04-25 03:50:30 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-25 03:50:30 +02:00
|
|
|
listen(property_set_fd, 8);
|
2010-04-14 04:33:37 +02:00
|
|
|
|
2015-04-25 06:13:44 +02:00
|
|
|
register_epoll_handler(property_set_fd, handle_property_set_fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|