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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <cutils/misc.h>
|
|
|
|
#include <cutils/sockets.h>
|
2013-05-20 23:22:01 +02:00
|
|
|
#include <cutils/multiuser.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
|
|
|
#include <sys/_system_properties.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
2012-08-09 16:05:49 +02:00
|
|
|
#include <selinux/selinux.h>
|
|
|
|
#include <selinux/label.h>
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "property_service.h"
|
|
|
|
#include "init.h"
|
2010-04-14 05:35:46 +02:00
|
|
|
#include "util.h"
|
2010-04-20 02:05:34 +02:00
|
|
|
#include "log.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#define PERSISTENT_PROPERTY_DIR "/data/property"
|
|
|
|
|
|
|
|
static int persistent_properties_loaded = 0;
|
2010-04-20 02:11:33 +02:00
|
|
|
static int property_area_inited = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-04-14 04:33:37 +02:00
|
|
|
static int property_set_fd = -1;
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/* White list of permissions for setting property services. */
|
|
|
|
struct {
|
|
|
|
const char *prefix;
|
|
|
|
unsigned int uid;
|
2009-09-03 00:08:52 +02:00
|
|
|
unsigned int gid;
|
2009-03-04 04:32:55 +01:00
|
|
|
} property_perms[] = {
|
2009-09-03 00:08:52 +02:00
|
|
|
{ "net.rmnet0.", AID_RADIO, 0 },
|
|
|
|
{ "net.gprs.", AID_RADIO, 0 },
|
|
|
|
{ "net.ppp", AID_RADIO, 0 },
|
2011-02-19 00:54:17 +01:00
|
|
|
{ "net.qmi", AID_RADIO, 0 },
|
2011-07-26 03:02:06 +02:00
|
|
|
{ "net.lte", AID_RADIO, 0 },
|
2011-07-29 03:09:55 +02:00
|
|
|
{ "net.cdma", AID_RADIO, 0 },
|
2009-09-03 00:08:52 +02:00
|
|
|
{ "ril.", AID_RADIO, 0 },
|
|
|
|
{ "gsm.", AID_RADIO, 0 },
|
|
|
|
{ "persist.radio", AID_RADIO, 0 },
|
|
|
|
{ "net.dns", AID_RADIO, 0 },
|
2011-08-22 21:12:37 +02:00
|
|
|
{ "sys.usb.config", AID_RADIO, 0 },
|
2009-09-03 00:08:52 +02:00
|
|
|
{ "net.", AID_SYSTEM, 0 },
|
|
|
|
{ "dev.", AID_SYSTEM, 0 },
|
|
|
|
{ "runtime.", AID_SYSTEM, 0 },
|
|
|
|
{ "hw.", AID_SYSTEM, 0 },
|
|
|
|
{ "sys.", AID_SYSTEM, 0 },
|
2013-04-18 21:20:02 +02:00
|
|
|
{ "sys.powerctl", AID_SHELL, 0 },
|
2009-09-03 00:08:52 +02:00
|
|
|
{ "service.", AID_SYSTEM, 0 },
|
|
|
|
{ "wlan.", AID_SYSTEM, 0 },
|
2013-06-21 15:59:20 +02:00
|
|
|
{ "gps.", AID_GPS, 0 },
|
2012-08-15 03:12:33 +02:00
|
|
|
{ "bluetooth.", AID_BLUETOOTH, 0 },
|
2009-09-03 00:08:52 +02:00
|
|
|
{ "dhcp.", AID_SYSTEM, 0 },
|
|
|
|
{ "dhcp.", AID_DHCP, 0 },
|
2012-04-28 00:19:30 +02:00
|
|
|
{ "debug.", AID_SYSTEM, 0 },
|
2009-09-03 00:08:52 +02:00
|
|
|
{ "debug.", AID_SHELL, 0 },
|
|
|
|
{ "log.", AID_SHELL, 0 },
|
|
|
|
{ "service.adb.root", AID_SHELL, 0 },
|
2010-12-10 18:48:41 +01:00
|
|
|
{ "service.adb.tcp.port", AID_SHELL, 0 },
|
2014-05-10 02:44:18 +02:00
|
|
|
{ "persist.logd.size",AID_SYSTEM, 0 },
|
2009-09-03 00:08:52 +02:00
|
|
|
{ "persist.sys.", AID_SYSTEM, 0 },
|
|
|
|
{ "persist.service.", AID_SYSTEM, 0 },
|
2009-11-12 21:02:24 +01:00
|
|
|
{ "persist.security.", AID_SYSTEM, 0 },
|
2013-06-21 15:59:20 +02:00
|
|
|
{ "persist.gps.", AID_GPS, 0 },
|
2012-08-02 13:58:21 +02:00
|
|
|
{ "persist.service.bdroid.", AID_BLUETOOTH, 0 },
|
2012-05-01 21:02:53 +02:00
|
|
|
{ "selinux." , AID_SYSTEM, 0 },
|
2009-09-03 00:08:52 +02:00
|
|
|
{ NULL, 0, 0 }
|
2009-03-04 04:32:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* White list of UID that are allowed to start/stop services.
|
|
|
|
* Currently there are no user apps that require.
|
|
|
|
*/
|
|
|
|
struct {
|
|
|
|
const char *service;
|
|
|
|
unsigned int uid;
|
2009-09-03 00:08:52 +02:00
|
|
|
unsigned int gid;
|
2009-03-04 04:32:55 +01:00
|
|
|
} control_perms[] = {
|
2009-09-03 00:08:52 +02:00
|
|
|
{ "dumpstate",AID_SHELL, AID_LOG },
|
2010-10-03 22:30:11 +02:00
|
|
|
{ "ril-daemon",AID_RADIO, AID_RADIO },
|
2009-09-03 00:08:52 +02:00
|
|
|
{NULL, 0, 0 }
|
2009-03-04 04:32:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
size_t size;
|
2013-01-22 21:46:09 +01:00
|
|
|
int fd;
|
2009-03-04 04:32:55 +01:00
|
|
|
} workspace;
|
|
|
|
|
|
|
|
static int init_workspace(workspace *w, size_t size)
|
|
|
|
{
|
|
|
|
void *data;
|
2013-02-12 23:39:31 +01:00
|
|
|
int fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW);
|
2010-07-14 01:43:56 +02:00
|
|
|
if (fd < 0)
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
w->size = size;
|
2013-01-22 21:46:09 +01:00
|
|
|
w->fd = fd;
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static workspace pa_workspace;
|
|
|
|
|
|
|
|
static int init_property_area(void)
|
|
|
|
{
|
2013-01-24 08:09:48 +01:00
|
|
|
if (property_area_inited)
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
|
2013-02-12 23:39:31 +01:00
|
|
|
if(__system_property_area_init())
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
|
2013-02-12 23:39:31 +01:00
|
|
|
if(init_workspace(&pa_workspace, 0))
|
|
|
|
return -1;
|
2013-01-22 21:46:09 +01:00
|
|
|
|
2013-02-12 23:39:31 +01:00
|
|
|
fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-04-20 02:11:33 +02:00
|
|
|
property_area_inited = 1;
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-09 16:05:49 +02:00
|
|
|
static int check_mac_perms(const char *name, char *sctx)
|
|
|
|
{
|
|
|
|
if (is_selinux_enabled() <= 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
char *tctx = NULL;
|
|
|
|
const char *class = "property_service";
|
|
|
|
const char *perm = "set";
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
if (!sctx)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!sehandle_prop)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
|
|
|
|
goto err;
|
|
|
|
|
2014-01-16 19:53:11 +01:00
|
|
|
if (selinux_check_access(sctx, tctx, class, perm, (void*) name) == 0)
|
2012-08-09 16:05:49 +02:00
|
|
|
result = 1;
|
|
|
|
|
|
|
|
freecon(tctx);
|
|
|
|
err:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_control_mac_perms(const char *name, char *sctx)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
return check_mac_perms(ctl_name, sctx);
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Checks permissions for starting/stoping system services.
|
|
|
|
* AID_SYSTEM and AID_ROOT are always allowed.
|
|
|
|
*
|
|
|
|
* Returns 1 if uid allowed, 0 otherwise.
|
|
|
|
*/
|
2012-08-09 16:05:49 +02:00
|
|
|
static int check_control_perms(const char *name, unsigned int uid, unsigned int gid, char *sctx) {
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
int i;
|
|
|
|
if (uid == AID_SYSTEM || uid == AID_ROOT)
|
2012-08-09 16:05:49 +02:00
|
|
|
return check_control_mac_perms(name, sctx);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
/* Search the ACL */
|
|
|
|
for (i = 0; control_perms[i].service; i++) {
|
|
|
|
if (strcmp(control_perms[i].service, name) == 0) {
|
2009-09-03 00:08:52 +02:00
|
|
|
if ((uid && control_perms[i].uid == uid) ||
|
|
|
|
(gid && control_perms[i].gid == gid)) {
|
2012-08-09 16:05:49 +02:00
|
|
|
return check_control_mac_perms(name, sctx);
|
2009-09-03 00:08:52 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks permissions for setting system properties.
|
|
|
|
* Returns 1 if uid allowed, 0 otherwise.
|
|
|
|
*/
|
2012-08-09 16:05:49 +02:00
|
|
|
static int check_perms(const char *name, unsigned int uid, unsigned int gid, char *sctx)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
int i;
|
2013-05-20 23:22:01 +02:00
|
|
|
unsigned int app_id;
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
if(!strncmp(name, "ro.", 3))
|
|
|
|
name +=3;
|
|
|
|
|
2012-08-09 16:05:49 +02:00
|
|
|
if (uid == 0)
|
|
|
|
return check_mac_perms(name, sctx);
|
|
|
|
|
2013-05-20 23:22:01 +02:00
|
|
|
app_id = multiuser_get_app_id(uid);
|
|
|
|
if (app_id == AID_BLUETOOTH) {
|
|
|
|
uid = app_id;
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
for (i = 0; property_perms[i].prefix; i++) {
|
|
|
|
if (strncmp(property_perms[i].prefix, name,
|
|
|
|
strlen(property_perms[i].prefix)) == 0) {
|
2009-09-03 00:08:52 +02:00
|
|
|
if ((uid && property_perms[i].uid == uid) ||
|
|
|
|
(gid && property_perms[i].gid == gid)) {
|
2012-08-09 16:05:49 +02:00
|
|
|
|
|
|
|
return check_mac_perms(name, sctx);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-29 23:58:57 +01:00
|
|
|
int __property_get(const char *name, char *value)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2013-01-29 02:13:35 +01:00
|
|
|
return __system_property_get(name, value);
|
2009-03-04 04:32:55 +01: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) {
|
|
|
|
ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno);
|
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)) {
|
|
|
|
unlink(tempPath);
|
|
|
|
ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 02:21:28 +02:00
|
|
|
static bool is_legal_property_name(const char* name, size_t namelen)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
bool previous_was_dot = false;
|
|
|
|
if (namelen >= PROP_NAME_MAX) return false;
|
|
|
|
if (namelen < 1) return false;
|
|
|
|
if (name[0] == '.') return false;
|
|
|
|
if (name[namelen - 1] == '.') return false;
|
|
|
|
|
|
|
|
/* Only allow alphanumeric, plus '.', '-', or '_' */
|
|
|
|
/* Don't allow ".." to appear in a property name */
|
|
|
|
for (i = 0; i < namelen; i++) {
|
|
|
|
if (name[i] == '.') {
|
|
|
|
if (previous_was_dot == true) return false;
|
|
|
|
previous_was_dot = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
previous_was_dot = false;
|
|
|
|
if (name[i] == '_' || name[i] == '-') continue;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
int property_set(const char *name, const char *value)
|
|
|
|
{
|
|
|
|
prop_info *pi;
|
2013-01-24 08:09:48 +01:00
|
|
|
int ret;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2012-10-02 23:59:59 +02:00
|
|
|
size_t namelen = strlen(name);
|
|
|
|
size_t valuelen = strlen(value);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2013-09-14 02:21:28 +02:00
|
|
|
if (!is_legal_property_name(name, namelen)) return -1;
|
|
|
|
if (valuelen >= PROP_VALUE_MAX) return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
pi = (prop_info*) __system_property_find(name);
|
|
|
|
|
|
|
|
if(pi != 0) {
|
|
|
|
/* ro.* properties may NEVER be modified once set */
|
|
|
|
if(!strncmp(name, "ro.", 3)) return -1;
|
|
|
|
|
2013-01-24 08:09:48 +01:00
|
|
|
__system_property_update(pi, value, valuelen);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2013-01-24 08:09:48 +01:00
|
|
|
ret = __system_property_add(name, namelen, value, valuelen);
|
|
|
|
if (ret < 0) {
|
2013-09-14 02:21:28 +02:00
|
|
|
ERROR("Failed to set '%s'='%s'\n", name, value);
|
2013-01-24 08:09:48 +01:00
|
|
|
return ret;
|
2013-04-29 09:11:57 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
/* If name starts with "net." treat as a DNS property. */
|
2009-05-08 20:27:42 +02:00
|
|
|
if (strncmp("net.", name, strlen("net.")) == 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
if (strcmp("net.change", name) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-08-26 04:13:20 +02:00
|
|
|
/*
|
2009-03-04 04:32:55 +01:00
|
|
|
* The 'net.change' property is a special property used track when any
|
|
|
|
* 'net.*' property name is updated. It is _ONLY_ updated here. Its value
|
|
|
|
* contains the last updated 'net.*' property.
|
|
|
|
*/
|
|
|
|
property_set("net.change", name);
|
|
|
|
} else if (persistent_properties_loaded &&
|
2009-05-08 20:27:42 +02:00
|
|
|
strncmp("persist.", name, strlen("persist.")) == 0) {
|
2009-08-26 04:13:20 +02:00
|
|
|
/*
|
2009-03-04 04:32:55 +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.
|
|
|
|
*/
|
2009-08-26 04:13:20 +02:00
|
|
|
write_persistent_property(name, value);
|
2013-05-17 21:47:04 +02:00
|
|
|
} else if (strcmp("selinux.reload_policy", name) == 0 &&
|
|
|
|
strcmp("1", value) == 0) {
|
|
|
|
selinux_reload_policy();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
property_changed(name, value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-14 04:33:37 +02:00
|
|
|
void handle_property_set_fd()
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
prop_msg msg;
|
|
|
|
int s;
|
|
|
|
int r;
|
|
|
|
int res;
|
|
|
|
struct ucred cr;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t addr_size = sizeof(addr);
|
|
|
|
socklen_t cr_size = sizeof(cr);
|
2012-08-09 16:05:49 +02:00
|
|
|
char * source_ctx = NULL;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-04-14 04:33:37 +02:00
|
|
|
if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check socket options here */
|
|
|
|
if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
|
|
|
|
close(s);
|
2012-10-02 23:59:59 +02:00
|
|
|
ERROR("Unable to receive socket options\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-01 20:48:50 +02:00
|
|
|
r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), 0));
|
2009-03-04 04:32:55 +01:00
|
|
|
if(r != sizeof(prop_msg)) {
|
2014-01-16 19:53:11 +01:00
|
|
|
ERROR("sys_prop: mis-match msg size received: %d expected: %zu errno: %d\n",
|
2011-07-01 20:48:50 +02:00
|
|
|
r, sizeof(prop_msg), errno);
|
2011-03-30 21:39:56 +02:00
|
|
|
close(s);
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(msg.cmd) {
|
|
|
|
case PROP_MSG_SETPROP:
|
|
|
|
msg.name[PROP_NAME_MAX-1] = 0;
|
|
|
|
msg.value[PROP_VALUE_MAX-1] = 0;
|
|
|
|
|
2013-09-14 02:21:28 +02:00
|
|
|
if (!is_legal_property_name(msg.name, strlen(msg.name))) {
|
|
|
|
ERROR("sys_prop: illegal property name. Got: \"%s\"\n", msg.name);
|
|
|
|
close(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-09 16:05:49 +02:00
|
|
|
getpeercon(s, &source_ctx);
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
if(memcmp(msg.name,"ctl.",4) == 0) {
|
2011-04-01 17:24:13 +02:00
|
|
|
// Keep the old close-socket-early behavior when handling
|
|
|
|
// ctl.* properties.
|
|
|
|
close(s);
|
2012-08-09 16:05:49 +02:00
|
|
|
if (check_control_perms(msg.value, cr.uid, cr.gid, source_ctx)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
handle_control_message((char*) msg.name + 4, (char*) msg.value);
|
|
|
|
} else {
|
2010-10-03 22:30:11 +02:00
|
|
|
ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",
|
|
|
|
msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
} else {
|
2012-08-09 16:05:49 +02:00
|
|
|
if (check_perms(msg.name, cr.uid, cr.gid, source_ctx)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
property_set((char*) msg.name, (char*) msg.value);
|
|
|
|
} else {
|
|
|
|
ERROR("sys_prop: permission denied uid:%d name:%s\n",
|
|
|
|
cr.uid, msg.name);
|
|
|
|
}
|
2011-04-01 17:24:13 +02:00
|
|
|
|
|
|
|
// Note: bionic's property client code assumes that the
|
|
|
|
// property server will not close the socket until *AFTER*
|
|
|
|
// the property is written to memory.
|
|
|
|
close(s);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2012-08-09 16:05:49 +02:00
|
|
|
freecon(source_ctx);
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-04-01 17:24:13 +02:00
|
|
|
close(s);
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 21:46:09 +01:00
|
|
|
void get_property_workspace(int *fd, int *sz)
|
|
|
|
{
|
|
|
|
*fd = pa_workspace.fd;
|
|
|
|
*sz = pa_workspace.size;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 23:26:36 +01: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.
|
|
|
|
*/
|
|
|
|
static void load_properties_from_file(const char *fn, const char *filter)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
char *data;
|
|
|
|
unsigned sz;
|
|
|
|
|
|
|
|
data = read_file(fn, &sz);
|
|
|
|
|
|
|
|
if(data != 0) {
|
2014-03-06 23:26:36 +01:00
|
|
|
load_properties(data, filter);
|
2009-03-04 04:32:55 +01:00
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void load_persistent_properties()
|
|
|
|
{
|
|
|
|
DIR* dir = opendir(PERSISTENT_PROPERTY_DIR);
|
2012-10-23 15:09:56 +02:00
|
|
|
int dir_fd;
|
2009-03-04 04:32:55 +01:00
|
|
|
struct dirent* entry;
|
|
|
|
char value[PROP_VALUE_MAX];
|
|
|
|
int fd, length;
|
2012-10-02 23:59:59 +02:00
|
|
|
struct stat sb;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (dir) {
|
2012-10-23 15:09:56 +02:00
|
|
|
dir_fd = dirfd(dir);
|
2009-03-04 04:32:55 +01:00
|
|
|
while ((entry = readdir(dir)) != NULL) {
|
2009-05-08 20:27:42 +02:00
|
|
|
if (strncmp("persist.", entry->d_name, strlen("persist.")))
|
2009-03-04 04:32:55 +01:00
|
|
|
continue;
|
|
|
|
#if HAVE_DIRENT_D_TYPE
|
|
|
|
if (entry->d_type != DT_REG)
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
/* open the file and read the property value */
|
2012-10-02 23:59:59 +02:00
|
|
|
fd = openat(dir_fd, entry->d_name, O_RDONLY | O_NOFOLLOW);
|
|
|
|
if (fd < 0) {
|
|
|
|
ERROR("Unable to open persistent property file \"%s\" errno: %d\n",
|
|
|
|
entry->d_name, errno);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (fstat(fd, &sb) < 0) {
|
|
|
|
ERROR("fstat on property file \"%s\" failed errno: %d\n", entry->d_name, errno);
|
2009-03-04 04:32:55 +01:00
|
|
|
close(fd);
|
2012-10-02 23:59:59 +02:00
|
|
|
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)) {
|
2014-01-16 19:53:11 +01:00
|
|
|
ERROR("skipping insecure property file %s (uid=%u gid=%u nlink=%d mode=%o)\n",
|
2014-05-01 17:34:26 +02:00
|
|
|
entry->d_name, (unsigned int)sb.st_uid, (unsigned int)sb.st_gid,
|
|
|
|
sb.st_nlink, sb.st_mode);
|
2012-10-02 23:59:59 +02:00
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
length = read(fd, value, sizeof(value) - 1);
|
|
|
|
if (length >= 0) {
|
|
|
|
value[length] = 0;
|
|
|
|
property_set(entry->d_name, value);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2012-10-02 23:59:59 +02:00
|
|
|
ERROR("Unable to read persistent property file %s errno: %d\n",
|
|
|
|
entry->d_name, errno);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2012-10-02 23:59:59 +02:00
|
|
|
close(fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
} else {
|
|
|
|
ERROR("Unable to open persistent property directory %s errno: %d\n", PERSISTENT_PROPERTY_DIR, errno);
|
|
|
|
}
|
2009-08-26 04:13:20 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
persistent_properties_loaded = 1;
|
|
|
|
}
|
|
|
|
|
2011-12-19 20:21:32 +01:00
|
|
|
void property_init(void)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
init_property_area();
|
2011-12-19 20:21:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void property_load_boot_defaults(void)
|
|
|
|
{
|
2012-10-13 00:23:40 +02:00
|
|
|
load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT, NULL);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2010-04-20 02:11:33 +02:00
|
|
|
int properties_inited(void)
|
|
|
|
{
|
|
|
|
return property_area_inited;
|
|
|
|
}
|
|
|
|
|
2012-09-19 20:15:24 +02:00
|
|
|
static void load_override_properties() {
|
|
|
|
#ifdef ALLOW_LOCAL_PROP_OVERRIDE
|
2013-01-29 02:13:35 +01:00
|
|
|
char debuggable[PROP_VALUE_MAX];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = property_get("ro.debuggable", debuggable);
|
|
|
|
if (ret && (strcmp(debuggable, "1") == 0)) {
|
2012-10-13 00:23:40 +02:00
|
|
|
load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
|
2012-09-19 20:15:24 +02:00
|
|
|
}
|
|
|
|
#endif /* ALLOW_LOCAL_PROP_OVERRIDE */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2010-04-14 04:33:37 +02:00
|
|
|
void start_property_service(void)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2012-10-13 00:23:40 +02:00
|
|
|
load_properties_from_file(PROP_PATH_SYSTEM_BUILD, NULL);
|
|
|
|
load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT, NULL);
|
2014-03-06 23:26:36 +01:00
|
|
|
load_properties_from_file(PROP_PATH_FACTORY, "ro.*");
|
|
|
|
|
2012-09-19 20:15:24 +02:00
|
|
|
load_override_properties();
|
2014-03-06 23:26:36 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/* Read persistent properties after all default values have been loaded. */
|
|
|
|
load_persistent_properties();
|
|
|
|
|
2013-05-13 18:37:04 +02:00
|
|
|
fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0, NULL);
|
2010-04-14 04:33:37 +02:00
|
|
|
if(fd < 0) return;
|
2009-03-04 04:32:55 +01:00
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
|
|
|
|
|
|
listen(fd, 8);
|
2010-04-14 04:33:37 +02:00
|
|
|
property_set_fd = fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_property_set_fd()
|
|
|
|
{
|
|
|
|
return property_set_fd;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|