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