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