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