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