The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | #include <string.h> |
| 21 | #include <ctype.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <dirent.h> |
| 25 | #include <limits.h> |
| 26 | #include <errno.h> |
| 27 | |
| 28 | #include <cutils/misc.h> |
| 29 | #include <cutils/sockets.h> |
Matthew Xie | 40a91a2 | 2013-05-20 14:22:01 -0700 | [diff] [blame] | 30 | #include <cutils/multiuser.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | |
| 32 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 33 | #include <sys/_system_properties.h> |
| 34 | |
| 35 | #include <sys/socket.h> |
| 36 | #include <sys/un.h> |
| 37 | #include <sys/select.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <netinet/in.h> |
| 40 | #include <sys/mman.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | #include <private/android_filesystem_config.h> |
| 42 | |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 43 | #include <selinux/selinux.h> |
| 44 | #include <selinux/label.h> |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 45 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | #include "property_service.h" |
| 47 | #include "init.h" |
Colin Cross | 3899e9f | 2010-04-13 20:35:46 -0700 | [diff] [blame] | 48 | #include "util.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 49 | #include "log.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | |
| 51 | #define PERSISTENT_PROPERTY_DIR "/data/property" |
| 52 | |
| 53 | static int persistent_properties_loaded = 0; |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 54 | static int property_area_inited = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 56 | static int property_set_fd = -1; |
| 57 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | typedef struct { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | size_t size; |
Nick Kralevich | 2840647 | 2013-01-22 12:46:09 -0800 | [diff] [blame] | 60 | int fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | } workspace; |
| 62 | |
| 63 | static int init_workspace(workspace *w, size_t size) |
| 64 | { |
| 65 | void *data; |
Greg Hackmann | f14eef0 | 2013-02-12 14:39:31 -0800 | [diff] [blame] | 66 | int fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW); |
Brian Swetland | 25b15be | 2010-07-13 16:43:56 -0700 | [diff] [blame] | 67 | if (fd < 0) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | return -1; |
| 69 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | w->size = size; |
Nick Kralevich | 2840647 | 2013-01-22 12:46:09 -0800 | [diff] [blame] | 71 | w->fd = fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 73 | } |
| 74 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | static workspace pa_workspace; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | |
| 77 | static int init_property_area(void) |
| 78 | { |
Colin Cross | 9f5af63 | 2013-01-23 23:09:48 -0800 | [diff] [blame] | 79 | if (property_area_inited) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | return -1; |
| 81 | |
Greg Hackmann | f14eef0 | 2013-02-12 14:39:31 -0800 | [diff] [blame] | 82 | if(__system_property_area_init()) |
| 83 | return -1; |
| 84 | |
| 85 | if(init_workspace(&pa_workspace, 0)) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 86 | return -1; |
| 87 | |
Nick Kralevich | 2840647 | 2013-01-22 12:46:09 -0800 | [diff] [blame] | 88 | fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC); |
| 89 | |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 90 | property_area_inited = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 94 | static int check_mac_perms(const char *name, char *sctx) |
| 95 | { |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 96 | if (is_selinux_enabled() <= 0) |
| 97 | return 1; |
| 98 | |
| 99 | char *tctx = NULL; |
| 100 | const char *class = "property_service"; |
| 101 | const char *perm = "set"; |
| 102 | int result = 0; |
| 103 | |
| 104 | if (!sctx) |
| 105 | goto err; |
| 106 | |
| 107 | if (!sehandle_prop) |
| 108 | goto err; |
| 109 | |
| 110 | if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0) |
| 111 | goto err; |
| 112 | |
Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 113 | if (selinux_check_access(sctx, tctx, class, perm, (void*) name) == 0) |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 114 | result = 1; |
| 115 | |
| 116 | freecon(tctx); |
| 117 | err: |
| 118 | return result; |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static int check_control_mac_perms(const char *name, char *sctx) |
| 122 | { |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 123 | /* |
| 124 | * Create a name prefix out of ctl.<service name> |
| 125 | * The new prefix allows the use of the existing |
| 126 | * property service backend labeling while avoiding |
| 127 | * mislabels based on true property prefixes. |
| 128 | */ |
| 129 | char ctl_name[PROP_VALUE_MAX+4]; |
| 130 | int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name); |
| 131 | |
| 132 | if (ret < 0 || (size_t) ret >= sizeof(ctl_name)) |
| 133 | return 0; |
| 134 | |
| 135 | return check_mac_perms(ctl_name, sctx); |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 136 | } |
| 137 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 138 | /* |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 139 | * Checks permissions for setting system properties. |
| 140 | * Returns 1 if uid allowed, 0 otherwise. |
| 141 | */ |
Nick Kralevich | 528c13e | 2014-06-17 22:23:54 -0700 | [diff] [blame^] | 142 | static int check_perms(const char *name, char *sctx) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | { |
| 144 | int i; |
Matthew Xie | 40a91a2 | 2013-05-20 14:22:01 -0700 | [diff] [blame] | 145 | unsigned int app_id; |
| 146 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | if(!strncmp(name, "ro.", 3)) |
| 148 | name +=3; |
| 149 | |
Nick Kralevich | 528c13e | 2014-06-17 22:23:54 -0700 | [diff] [blame^] | 150 | return check_mac_perms(name, sctx); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Colin Cross | 88ac54a | 2013-01-29 14:58:57 -0800 | [diff] [blame] | 153 | int __property_get(const char *name, char *value) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | { |
Colin Cross | 2deedfe | 2013-01-28 17:13:35 -0800 | [diff] [blame] | 155 | return __system_property_get(name, value); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Tammo Spalink | 3dfe6c6 | 2009-08-26 10:13:20 +0800 | [diff] [blame] | 158 | static void write_persistent_property(const char *name, const char *value) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | { |
Nick Kralevich | 7ecfe6a | 2012-10-02 14:59:59 -0700 | [diff] [blame] | 160 | char tempPath[PATH_MAX]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 161 | char path[PATH_MAX]; |
Nick Kralevich | 7ecfe6a | 2012-10-02 14:59:59 -0700 | [diff] [blame] | 162 | int fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 163 | |
Nick Kralevich | 7ecfe6a | 2012-10-02 14:59:59 -0700 | [diff] [blame] | 164 | snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR); |
| 165 | fd = mkstemp(tempPath); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 166 | if (fd < 0) { |
| 167 | ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno); |
Tammo Spalink | 3dfe6c6 | 2009-08-26 10:13:20 +0800 | [diff] [blame] | 168 | return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 169 | } |
| 170 | write(fd, value, strlen(value)); |
OPPO | de73a0c | 2014-03-28 19:12:47 +0800 | [diff] [blame] | 171 | fsync(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | close(fd); |
| 173 | |
Nick Kralevich | 7ecfe6a | 2012-10-02 14:59:59 -0700 | [diff] [blame] | 174 | snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 175 | if (rename(tempPath, path)) { |
| 176 | unlink(tempPath); |
| 177 | ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path); |
| 178 | } |
| 179 | } |
| 180 | |
Nick Kralevich | 6946361 | 2013-09-13 17:21:28 -0700 | [diff] [blame] | 181 | static bool is_legal_property_name(const char* name, size_t namelen) |
| 182 | { |
| 183 | size_t i; |
| 184 | bool previous_was_dot = false; |
| 185 | if (namelen >= PROP_NAME_MAX) return false; |
| 186 | if (namelen < 1) return false; |
| 187 | if (name[0] == '.') return false; |
| 188 | if (name[namelen - 1] == '.') return false; |
| 189 | |
| 190 | /* Only allow alphanumeric, plus '.', '-', or '_' */ |
| 191 | /* Don't allow ".." to appear in a property name */ |
| 192 | for (i = 0; i < namelen; i++) { |
| 193 | if (name[i] == '.') { |
| 194 | if (previous_was_dot == true) return false; |
| 195 | previous_was_dot = true; |
| 196 | continue; |
| 197 | } |
| 198 | previous_was_dot = false; |
| 199 | if (name[i] == '_' || name[i] == '-') continue; |
| 200 | if (name[i] >= 'a' && name[i] <= 'z') continue; |
| 201 | if (name[i] >= 'A' && name[i] <= 'Z') continue; |
| 202 | if (name[i] >= '0' && name[i] <= '9') continue; |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | return true; |
| 207 | } |
| 208 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 209 | int property_set(const char *name, const char *value) |
| 210 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 211 | prop_info *pi; |
Colin Cross | 9f5af63 | 2013-01-23 23:09:48 -0800 | [diff] [blame] | 212 | int ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 213 | |
Nick Kralevich | 7ecfe6a | 2012-10-02 14:59:59 -0700 | [diff] [blame] | 214 | size_t namelen = strlen(name); |
| 215 | size_t valuelen = strlen(value); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 216 | |
Nick Kralevich | 6946361 | 2013-09-13 17:21:28 -0700 | [diff] [blame] | 217 | if (!is_legal_property_name(name, namelen)) return -1; |
| 218 | if (valuelen >= PROP_VALUE_MAX) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 219 | |
| 220 | pi = (prop_info*) __system_property_find(name); |
| 221 | |
| 222 | if(pi != 0) { |
| 223 | /* ro.* properties may NEVER be modified once set */ |
| 224 | if(!strncmp(name, "ro.", 3)) return -1; |
| 225 | |
Colin Cross | 9f5af63 | 2013-01-23 23:09:48 -0800 | [diff] [blame] | 226 | __system_property_update(pi, value, valuelen); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 227 | } else { |
Colin Cross | 9f5af63 | 2013-01-23 23:09:48 -0800 | [diff] [blame] | 228 | ret = __system_property_add(name, namelen, value, valuelen); |
| 229 | if (ret < 0) { |
Nick Kralevich | 6946361 | 2013-09-13 17:21:28 -0700 | [diff] [blame] | 230 | ERROR("Failed to set '%s'='%s'\n", name, value); |
Colin Cross | 9f5af63 | 2013-01-23 23:09:48 -0800 | [diff] [blame] | 231 | return ret; |
Johan Redestig | fd7ffb1 | 2013-04-29 09:11:57 +0200 | [diff] [blame] | 232 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 233 | } |
| 234 | /* If name starts with "net." treat as a DNS property. */ |
Mike Lockwood | b377955 | 2009-05-08 14:27:42 -0400 | [diff] [blame] | 235 | if (strncmp("net.", name, strlen("net.")) == 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 236 | if (strcmp("net.change", name) == 0) { |
| 237 | return 0; |
| 238 | } |
Tammo Spalink | 3dfe6c6 | 2009-08-26 10:13:20 +0800 | [diff] [blame] | 239 | /* |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 240 | * The 'net.change' property is a special property used track when any |
| 241 | * 'net.*' property name is updated. It is _ONLY_ updated here. Its value |
| 242 | * contains the last updated 'net.*' property. |
| 243 | */ |
| 244 | property_set("net.change", name); |
| 245 | } else if (persistent_properties_loaded && |
Mike Lockwood | b377955 | 2009-05-08 14:27:42 -0400 | [diff] [blame] | 246 | strncmp("persist.", name, strlen("persist.")) == 0) { |
Tammo Spalink | 3dfe6c6 | 2009-08-26 10:13:20 +0800 | [diff] [blame] | 247 | /* |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 248 | * Don't write properties to disk until after we have read all default properties |
| 249 | * to prevent them from being overwritten by default values. |
| 250 | */ |
Tammo Spalink | 3dfe6c6 | 2009-08-26 10:13:20 +0800 | [diff] [blame] | 251 | write_persistent_property(name, value); |
repo sync | 8a38787 | 2013-05-17 12:47:04 -0700 | [diff] [blame] | 252 | } else if (strcmp("selinux.reload_policy", name) == 0 && |
| 253 | strcmp("1", value) == 0) { |
| 254 | selinux_reload_policy(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 255 | } |
| 256 | property_changed(name, value); |
| 257 | return 0; |
| 258 | } |
| 259 | |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 260 | void handle_property_set_fd() |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 261 | { |
| 262 | prop_msg msg; |
| 263 | int s; |
| 264 | int r; |
| 265 | int res; |
| 266 | struct ucred cr; |
| 267 | struct sockaddr_un addr; |
| 268 | socklen_t addr_size = sizeof(addr); |
| 269 | socklen_t cr_size = sizeof(cr); |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 270 | char * source_ctx = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 271 | |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 272 | if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 273 | return; |
| 274 | } |
| 275 | |
| 276 | /* Check socket options here */ |
| 277 | if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) { |
| 278 | close(s); |
Nick Kralevich | 7ecfe6a | 2012-10-02 14:59:59 -0700 | [diff] [blame] | 279 | ERROR("Unable to receive socket options\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
Mike Lockwood | d18678f | 2011-07-01 14:48:50 -0400 | [diff] [blame] | 283 | r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), 0)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 284 | if(r != sizeof(prop_msg)) { |
Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 285 | ERROR("sys_prop: mis-match msg size received: %d expected: %zu errno: %d\n", |
Mike Lockwood | d18678f | 2011-07-01 14:48:50 -0400 | [diff] [blame] | 286 | r, sizeof(prop_msg), errno); |
Brad Fitzpatrick | 9f1e0e3 | 2011-03-30 12:39:56 -0700 | [diff] [blame] | 287 | close(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
| 291 | switch(msg.cmd) { |
| 292 | case PROP_MSG_SETPROP: |
| 293 | msg.name[PROP_NAME_MAX-1] = 0; |
| 294 | msg.value[PROP_VALUE_MAX-1] = 0; |
| 295 | |
Nick Kralevich | 6946361 | 2013-09-13 17:21:28 -0700 | [diff] [blame] | 296 | if (!is_legal_property_name(msg.name, strlen(msg.name))) { |
| 297 | ERROR("sys_prop: illegal property name. Got: \"%s\"\n", msg.name); |
| 298 | close(s); |
| 299 | return; |
| 300 | } |
| 301 | |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 302 | getpeercon(s, &source_ctx); |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 303 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 304 | if(memcmp(msg.name,"ctl.",4) == 0) { |
Brad Fitzpatrick | 71ead18 | 2011-04-01 08:24:13 -0700 | [diff] [blame] | 305 | // Keep the old close-socket-early behavior when handling |
| 306 | // ctl.* properties. |
| 307 | close(s); |
Nick Kralevich | 528c13e | 2014-06-17 22:23:54 -0700 | [diff] [blame^] | 308 | if (check_control_mac_perms(msg.value, source_ctx)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 | handle_control_message((char*) msg.name + 4, (char*) msg.value); |
| 310 | } else { |
Wink Saville | cfa0d84 | 2010-10-03 13:30:11 -0700 | [diff] [blame] | 311 | ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n", |
| 312 | msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 313 | } |
| 314 | } else { |
Nick Kralevich | 528c13e | 2014-06-17 22:23:54 -0700 | [diff] [blame^] | 315 | if (check_perms(msg.name, source_ctx)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 316 | property_set((char*) msg.name, (char*) msg.value); |
| 317 | } else { |
| 318 | ERROR("sys_prop: permission denied uid:%d name:%s\n", |
| 319 | cr.uid, msg.name); |
| 320 | } |
Brad Fitzpatrick | 71ead18 | 2011-04-01 08:24:13 -0700 | [diff] [blame] | 321 | |
| 322 | // Note: bionic's property client code assumes that the |
| 323 | // property server will not close the socket until *AFTER* |
| 324 | // the property is written to memory. |
| 325 | close(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 326 | } |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 327 | freecon(source_ctx); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 328 | break; |
| 329 | |
| 330 | default: |
Brad Fitzpatrick | 71ead18 | 2011-04-01 08:24:13 -0700 | [diff] [blame] | 331 | close(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 332 | break; |
| 333 | } |
| 334 | } |
| 335 | |
Nick Kralevich | 2840647 | 2013-01-22 12:46:09 -0800 | [diff] [blame] | 336 | void get_property_workspace(int *fd, int *sz) |
| 337 | { |
| 338 | *fd = pa_workspace.fd; |
| 339 | *sz = pa_workspace.size; |
| 340 | } |
| 341 | |
Jeff Sharkey | 06f8c09 | 2014-03-06 14:26:36 -0800 | [diff] [blame] | 342 | static void load_properties_from_file(const char *, const char *); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 343 | |
Jeff Sharkey | 06f8c09 | 2014-03-06 14:26:36 -0800 | [diff] [blame] | 344 | /* |
| 345 | * Filter is used to decide which properties to load: NULL loads all keys, |
| 346 | * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match. |
| 347 | */ |
| 348 | static void load_properties(char *data, const char *filter) |
| 349 | { |
| 350 | char *key, *value, *eol, *sol, *tmp, *fn; |
| 351 | size_t flen = 0; |
| 352 | |
| 353 | if (filter) { |
| 354 | flen = strlen(filter); |
| 355 | } |
| 356 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 357 | sol = data; |
Jeff Sharkey | 06f8c09 | 2014-03-06 14:26:36 -0800 | [diff] [blame] | 358 | while ((eol = strchr(sol, '\n'))) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 359 | key = sol; |
| 360 | *eol++ = 0; |
| 361 | sol = eol; |
| 362 | |
Jeff Sharkey | 06f8c09 | 2014-03-06 14:26:36 -0800 | [diff] [blame] | 363 | while (isspace(*key)) key++; |
| 364 | if (*key == '#') continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 365 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 366 | tmp = eol - 2; |
Jeff Sharkey | 06f8c09 | 2014-03-06 14:26:36 -0800 | [diff] [blame] | 367 | while ((tmp > key) && isspace(*tmp)) *tmp-- = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 368 | |
Jeff Sharkey | 06f8c09 | 2014-03-06 14:26:36 -0800 | [diff] [blame] | 369 | if (!strncmp(key, "import ", 7) && flen == 0) { |
| 370 | fn = key + 7; |
| 371 | while (isspace(*fn)) fn++; |
| 372 | |
| 373 | key = strchr(fn, ' '); |
| 374 | if (key) { |
| 375 | *key++ = 0; |
| 376 | while (isspace(*key)) key++; |
| 377 | } |
| 378 | |
| 379 | load_properties_from_file(fn, key); |
| 380 | |
| 381 | } else { |
| 382 | value = strchr(key, '='); |
| 383 | if (!value) continue; |
| 384 | *value++ = 0; |
| 385 | |
| 386 | tmp = value - 2; |
| 387 | while ((tmp > key) && isspace(*tmp)) *tmp-- = 0; |
| 388 | |
| 389 | while (isspace(*value)) value++; |
| 390 | |
| 391 | if (flen > 0) { |
| 392 | if (filter[flen - 1] == '*') { |
| 393 | if (strncmp(key, filter, flen - 1)) continue; |
| 394 | } else { |
| 395 | if (strcmp(key, filter)) continue; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | property_set(key, value); |
| 400 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Jeff Sharkey | 06f8c09 | 2014-03-06 14:26:36 -0800 | [diff] [blame] | 404 | /* |
| 405 | * Filter is used to decide which properties to load: NULL loads all keys, |
| 406 | * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match. |
| 407 | */ |
| 408 | static void load_properties_from_file(const char *fn, const char *filter) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 409 | { |
| 410 | char *data; |
| 411 | unsigned sz; |
| 412 | |
| 413 | data = read_file(fn, &sz); |
| 414 | |
| 415 | if(data != 0) { |
Jeff Sharkey | 06f8c09 | 2014-03-06 14:26:36 -0800 | [diff] [blame] | 416 | load_properties(data, filter); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 417 | free(data); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static void load_persistent_properties() |
| 422 | { |
| 423 | DIR* dir = opendir(PERSISTENT_PROPERTY_DIR); |
Yi-wei Zhao | e41bc31 | 2012-10-23 21:09:56 +0800 | [diff] [blame] | 424 | int dir_fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 425 | struct dirent* entry; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 426 | char value[PROP_VALUE_MAX]; |
| 427 | int fd, length; |
Nick Kralevich | 7ecfe6a | 2012-10-02 14:59:59 -0700 | [diff] [blame] | 428 | struct stat sb; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 429 | |
| 430 | if (dir) { |
Yi-wei Zhao | e41bc31 | 2012-10-23 21:09:56 +0800 | [diff] [blame] | 431 | dir_fd = dirfd(dir); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 432 | while ((entry = readdir(dir)) != NULL) { |
Mike Lockwood | b377955 | 2009-05-08 14:27:42 -0400 | [diff] [blame] | 433 | if (strncmp("persist.", entry->d_name, strlen("persist."))) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 434 | continue; |
| 435 | #if HAVE_DIRENT_D_TYPE |
| 436 | if (entry->d_type != DT_REG) |
| 437 | continue; |
| 438 | #endif |
| 439 | /* open the file and read the property value */ |
Nick Kralevich | 7ecfe6a | 2012-10-02 14:59:59 -0700 | [diff] [blame] | 440 | fd = openat(dir_fd, entry->d_name, O_RDONLY | O_NOFOLLOW); |
| 441 | if (fd < 0) { |
| 442 | ERROR("Unable to open persistent property file \"%s\" errno: %d\n", |
| 443 | entry->d_name, errno); |
| 444 | continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 445 | } |
Nick Kralevich | 7ecfe6a | 2012-10-02 14:59:59 -0700 | [diff] [blame] | 446 | if (fstat(fd, &sb) < 0) { |
| 447 | ERROR("fstat on property file \"%s\" failed errno: %d\n", entry->d_name, errno); |
| 448 | close(fd); |
| 449 | continue; |
| 450 | } |
| 451 | |
| 452 | // File must not be accessible to others, be owned by root/root, and |
| 453 | // not be a hard link to any other file. |
| 454 | if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) |
| 455 | || (sb.st_uid != 0) |
| 456 | || (sb.st_gid != 0) |
| 457 | || (sb.st_nlink != 1)) { |
Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 458 | ERROR("skipping insecure property file %s (uid=%u gid=%u nlink=%d mode=%o)\n", |
Mark Salyzyn | ebf49f3 | 2014-05-01 08:34:26 -0700 | [diff] [blame] | 459 | entry->d_name, (unsigned int)sb.st_uid, (unsigned int)sb.st_gid, |
| 460 | sb.st_nlink, sb.st_mode); |
Nick Kralevich | 7ecfe6a | 2012-10-02 14:59:59 -0700 | [diff] [blame] | 461 | close(fd); |
| 462 | continue; |
| 463 | } |
| 464 | |
| 465 | length = read(fd, value, sizeof(value) - 1); |
| 466 | if (length >= 0) { |
| 467 | value[length] = 0; |
| 468 | property_set(entry->d_name, value); |
| 469 | } else { |
| 470 | ERROR("Unable to read persistent property file %s errno: %d\n", |
| 471 | entry->d_name, errno); |
| 472 | } |
| 473 | close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 474 | } |
| 475 | closedir(dir); |
| 476 | } else { |
| 477 | ERROR("Unable to open persistent property directory %s errno: %d\n", PERSISTENT_PROPERTY_DIR, errno); |
| 478 | } |
Tammo Spalink | 3dfe6c6 | 2009-08-26 10:13:20 +0800 | [diff] [blame] | 479 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 480 | persistent_properties_loaded = 1; |
| 481 | } |
| 482 | |
Dima Zavin | 8886112 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 483 | void property_init(void) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 484 | { |
| 485 | init_property_area(); |
Dima Zavin | 8886112 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | void property_load_boot_defaults(void) |
| 489 | { |
Andrew Boie | 3899f52 | 2012-10-12 15:23:40 -0700 | [diff] [blame] | 490 | load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT, NULL); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 493 | int properties_inited(void) |
| 494 | { |
| 495 | return property_area_inited; |
| 496 | } |
| 497 | |
Nick Kralevich | 32b9023 | 2012-09-19 11:15:24 -0700 | [diff] [blame] | 498 | static void load_override_properties() { |
| 499 | #ifdef ALLOW_LOCAL_PROP_OVERRIDE |
Colin Cross | 2deedfe | 2013-01-28 17:13:35 -0800 | [diff] [blame] | 500 | char debuggable[PROP_VALUE_MAX]; |
| 501 | int ret; |
| 502 | |
| 503 | ret = property_get("ro.debuggable", debuggable); |
| 504 | if (ret && (strcmp(debuggable, "1") == 0)) { |
Andrew Boie | 3899f52 | 2012-10-12 15:23:40 -0700 | [diff] [blame] | 505 | load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL); |
Nick Kralevich | 32b9023 | 2012-09-19 11:15:24 -0700 | [diff] [blame] | 506 | } |
| 507 | #endif /* ALLOW_LOCAL_PROP_OVERRIDE */ |
| 508 | } |
| 509 | |
| 510 | |
Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 511 | /* When booting an encrypted system, /data is not mounted when the |
| 512 | * property service is started, so any properties stored there are |
| 513 | * not loaded. Vold triggers init to load these properties once it |
| 514 | * has mounted /data. |
| 515 | */ |
| 516 | void load_persist_props(void) |
| 517 | { |
Nick Kralevich | 32b9023 | 2012-09-19 11:15:24 -0700 | [diff] [blame] | 518 | load_override_properties(); |
Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 519 | /* Read persistent properties after all default values have been loaded. */ |
| 520 | load_persistent_properties(); |
| 521 | } |
| 522 | |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 523 | void start_property_service(void) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 524 | { |
| 525 | int fd; |
| 526 | |
Andrew Boie | 3899f52 | 2012-10-12 15:23:40 -0700 | [diff] [blame] | 527 | load_properties_from_file(PROP_PATH_SYSTEM_BUILD, NULL); |
| 528 | load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT, NULL); |
Jeff Sharkey | 06f8c09 | 2014-03-06 14:26:36 -0800 | [diff] [blame] | 529 | load_properties_from_file(PROP_PATH_FACTORY, "ro.*"); |
| 530 | |
Nick Kralevich | 32b9023 | 2012-09-19 11:15:24 -0700 | [diff] [blame] | 531 | load_override_properties(); |
Jeff Sharkey | 06f8c09 | 2014-03-06 14:26:36 -0800 | [diff] [blame] | 532 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 533 | /* Read persistent properties after all default values have been loaded. */ |
| 534 | load_persistent_properties(); |
| 535 | |
Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 536 | fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0, NULL); |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 537 | if(fd < 0) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 538 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 539 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 540 | |
| 541 | listen(fd, 8); |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 542 | property_set_fd = fd; |
| 543 | } |
| 544 | |
| 545 | int get_property_set_fd() |
| 546 | { |
| 547 | return property_set_fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 548 | } |