| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2017 The Android Open Source Project | 
|  | 3 | * All rights reserved. | 
|  | 4 | * | 
|  | 5 | * Redistribution and use in source and binary forms, with or without | 
|  | 6 | * modification, are permitted provided that the following conditions | 
|  | 7 | * are met: | 
|  | 8 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 |  | 
|  | 29 | #include <errno.h> | 
|  | 30 | #include <poll.h> | 
|  | 31 | #include <stdatomic.h> | 
|  | 32 | #include <stddef.h> | 
|  | 33 | #include <stdint.h> | 
|  | 34 | #include <stdlib.h> | 
|  | 35 | #include <string.h> | 
|  | 36 | #include <sys/socket.h> | 
|  | 37 | #include <sys/types.h> | 
|  | 38 | #include <sys/uio.h> | 
|  | 39 | #include <sys/un.h> | 
|  | 40 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ | 
|  | 41 | #include <sys/_system_properties.h> | 
|  | 42 | #include <unistd.h> | 
|  | 43 |  | 
| Chienyuan Huang | f2b4a97 | 2020-08-20 08:42:31 +0000 | [diff] [blame] | 44 | #include <async_safe/log.h> | 
| Bowgo Tsai | 61a5a83 | 2021-07-15 10:13:33 +0000 | [diff] [blame] | 45 | #include <async_safe/CHECK.h> | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 46 |  | 
| Bowgo Tsai | 61a5a83 | 2021-07-15 10:13:33 +0000 | [diff] [blame] | 47 | #include "private/bionic_defs.h" | 
| Josh Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 48 | #include "platform/bionic/macros.h" | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 49 | #include "private/ScopedFd.h" | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 50 |  | 
|  | 51 | static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME; | 
| Paul Lawrence | 71ade01 | 2023-07-18 08:49:44 -0700 | [diff] [blame] | 52 | static const char property_service_for_system_socket[] = | 
|  | 53 | "/dev/socket/" PROP_SERVICE_FOR_SYSTEM_NAME; | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 54 | static const char* kServiceVersionPropertyName = "ro.property_service.version"; | 
|  | 55 |  | 
|  | 56 | class PropertyServiceConnection { | 
|  | 57 | public: | 
| Paul Lawrence | 71ade01 | 2023-07-18 08:49:44 -0700 | [diff] [blame] | 58 | PropertyServiceConnection(const char* name) : last_error_(0) { | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 59 | socket_.reset(::socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0)); | 
|  | 60 | if (socket_.get() == -1) { | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 61 | last_error_ = errno; | 
|  | 62 | return; | 
|  | 63 | } | 
|  | 64 |  | 
| Paul Lawrence | 71ade01 | 2023-07-18 08:49:44 -0700 | [diff] [blame] | 65 | // If we're trying to set "sys.powerctl" from a privileged process, use the special | 
|  | 66 | // socket. Because this socket is only accessible to privileged processes, it can't | 
|  | 67 | // be DoSed directly by malicious apps. (The shell user should be able to reboot, | 
|  | 68 | // though, so we don't just always use the special socket for "sys.powerctl".) | 
|  | 69 | // See b/262237198 for context | 
|  | 70 | const char* socket = property_service_socket; | 
|  | 71 | if (strcmp(name, "sys.powerctl") == 0 && | 
|  | 72 | access(property_service_for_system_socket, W_OK) == 0) { | 
|  | 73 | socket = property_service_for_system_socket; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | const size_t namelen = strlen(socket); | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 77 | sockaddr_un addr; | 
|  | 78 | memset(&addr, 0, sizeof(addr)); | 
| Paul Lawrence | 71ade01 | 2023-07-18 08:49:44 -0700 | [diff] [blame] | 79 | strlcpy(addr.sun_path, socket, sizeof(addr.sun_path)); | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 80 | addr.sun_family = AF_LOCAL; | 
|  | 81 | socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1; | 
|  | 82 |  | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 83 | if (TEMP_FAILURE_RETRY(connect(socket_.get(), | 
|  | 84 | reinterpret_cast<sockaddr*>(&addr), alen)) == -1) { | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 85 | last_error_ = errno; | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 86 | socket_.reset(); | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 87 | } | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | bool IsValid() { | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 91 | return socket_.get() != -1; | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
|  | 94 | int GetLastError() { | 
|  | 95 | return last_error_; | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | bool RecvInt32(int32_t* value) { | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 99 | int result = TEMP_FAILURE_RETRY(recv(socket_.get(), value, sizeof(*value), MSG_WAITALL)); | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 100 | return CheckSendRecvResult(result, sizeof(*value)); | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | int socket() { | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 104 | return socket_.get(); | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
|  | 107 | private: | 
|  | 108 | bool CheckSendRecvResult(int result, int expected_len) { | 
|  | 109 | if (result == -1) { | 
|  | 110 | last_error_ = errno; | 
|  | 111 | } else if (result != expected_len) { | 
|  | 112 | last_error_ = -1; | 
|  | 113 | } else { | 
|  | 114 | last_error_ = 0; | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | return last_error_ == 0; | 
|  | 118 | } | 
|  | 119 |  | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 120 | ScopedFd socket_; | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 121 | int last_error_; | 
|  | 122 |  | 
|  | 123 | friend class SocketWriter; | 
|  | 124 | }; | 
|  | 125 |  | 
|  | 126 | class SocketWriter { | 
|  | 127 | public: | 
|  | 128 | explicit SocketWriter(PropertyServiceConnection* connection) | 
|  | 129 | : connection_(connection), iov_index_(0), uint_buf_index_(0) { | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | SocketWriter& WriteUint32(uint32_t value) { | 
|  | 133 | CHECK(uint_buf_index_ < kUintBufSize); | 
|  | 134 | CHECK(iov_index_ < kIovSize); | 
|  | 135 | uint32_t* ptr = uint_buf_ + uint_buf_index_; | 
|  | 136 | uint_buf_[uint_buf_index_++] = value; | 
|  | 137 | iov_[iov_index_].iov_base = ptr; | 
|  | 138 | iov_[iov_index_].iov_len = sizeof(*ptr); | 
|  | 139 | ++iov_index_; | 
|  | 140 | return *this; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | SocketWriter& WriteString(const char* value) { | 
|  | 144 | uint32_t valuelen = strlen(value); | 
|  | 145 | WriteUint32(valuelen); | 
|  | 146 | if (valuelen == 0) { | 
|  | 147 | return *this; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | CHECK(iov_index_ < kIovSize); | 
|  | 151 | iov_[iov_index_].iov_base = const_cast<char*>(value); | 
|  | 152 | iov_[iov_index_].iov_len = valuelen; | 
|  | 153 | ++iov_index_; | 
|  | 154 |  | 
|  | 155 | return *this; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | bool Send() { | 
|  | 159 | if (!connection_->IsValid()) { | 
|  | 160 | return false; | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | if (writev(connection_->socket(), iov_, iov_index_) == -1) { | 
|  | 164 | connection_->last_error_ = errno; | 
|  | 165 | return false; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | iov_index_ = uint_buf_index_ = 0; | 
|  | 169 | return true; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | private: | 
|  | 173 | static constexpr size_t kUintBufSize = 8; | 
|  | 174 | static constexpr size_t kIovSize = 8; | 
|  | 175 |  | 
|  | 176 | PropertyServiceConnection* connection_; | 
|  | 177 | iovec iov_[kIovSize]; | 
|  | 178 | size_t iov_index_; | 
|  | 179 | uint32_t uint_buf_[kUintBufSize]; | 
|  | 180 | size_t uint_buf_index_; | 
|  | 181 |  | 
| Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 182 | BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(SocketWriter); | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 183 | }; | 
|  | 184 |  | 
|  | 185 | struct prop_msg { | 
|  | 186 | unsigned cmd; | 
|  | 187 | char name[PROP_NAME_MAX]; | 
|  | 188 | char value[PROP_VALUE_MAX]; | 
|  | 189 | }; | 
|  | 190 |  | 
|  | 191 | static int send_prop_msg(const prop_msg* msg) { | 
| Paul Lawrence | 71ade01 | 2023-07-18 08:49:44 -0700 | [diff] [blame] | 192 | PropertyServiceConnection connection(msg->name); | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 193 | if (!connection.IsValid()) { | 
|  | 194 | return connection.GetLastError(); | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | int result = -1; | 
|  | 198 | int s = connection.socket(); | 
|  | 199 |  | 
|  | 200 | const int num_bytes = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0)); | 
|  | 201 | if (num_bytes == sizeof(prop_msg)) { | 
|  | 202 | // We successfully wrote to the property server but now we | 
|  | 203 | // wait for the property server to finish its work.  It | 
|  | 204 | // acknowledges its completion by closing the socket so we | 
|  | 205 | // poll here (on nothing), waiting for the socket to close. | 
|  | 206 | // If you 'adb shell setprop foo bar' you'll see the POLLHUP | 
|  | 207 | // once the socket closes.  Out of paranoia we cap our poll | 
|  | 208 | // at 250 ms. | 
|  | 209 | pollfd pollfds[1]; | 
|  | 210 | pollfds[0].fd = s; | 
|  | 211 | pollfds[0].events = 0; | 
|  | 212 | const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */)); | 
|  | 213 | if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) { | 
|  | 214 | result = 0; | 
|  | 215 | } else { | 
|  | 216 | // Ignore the timeout and treat it like a success anyway. | 
|  | 217 | // The init process is single-threaded and its property | 
|  | 218 | // service is sometimes slow to respond (perhaps it's off | 
|  | 219 | // starting a child process or something) and thus this | 
|  | 220 | // times out and the caller thinks it failed, even though | 
|  | 221 | // it's still getting around to it.  So we fake it here, | 
|  | 222 | // mostly for ctl.* properties, but we do try and wait 250 | 
|  | 223 | // ms so callers who do read-after-write can reliably see | 
|  | 224 | // what they've written.  Most of the time. | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 225 | async_safe_format_log(ANDROID_LOG_WARN, "libc", | 
|  | 226 | "Property service has timed out while trying to set \"%s\" to \"%s\"", | 
|  | 227 | msg->name, msg->value); | 
|  | 228 | result = 0; | 
|  | 229 | } | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | return result; | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | static constexpr uint32_t kProtocolVersion1 = 1; | 
|  | 236 | static constexpr uint32_t kProtocolVersion2 = 2;  // current | 
|  | 237 |  | 
|  | 238 | static atomic_uint_least32_t g_propservice_protocol_version = 0; | 
|  | 239 |  | 
|  | 240 | static void detect_protocol_version() { | 
|  | 241 | char value[PROP_VALUE_MAX]; | 
|  | 242 | if (__system_property_get(kServiceVersionPropertyName, value) == 0) { | 
|  | 243 | g_propservice_protocol_version = kProtocolVersion1; | 
|  | 244 | async_safe_format_log(ANDROID_LOG_WARN, "libc", | 
|  | 245 | "Using old property service protocol (\"%s\" is not set)", | 
|  | 246 | kServiceVersionPropertyName); | 
|  | 247 | } else { | 
|  | 248 | uint32_t version = static_cast<uint32_t>(atoll(value)); | 
|  | 249 | if (version >= kProtocolVersion2) { | 
|  | 250 | g_propservice_protocol_version = kProtocolVersion2; | 
|  | 251 | } else { | 
|  | 252 | async_safe_format_log(ANDROID_LOG_WARN, "libc", | 
|  | 253 | "Using old property service protocol (\"%s\"=\"%s\")", | 
|  | 254 | kServiceVersionPropertyName, value); | 
|  | 255 | g_propservice_protocol_version = kProtocolVersion1; | 
|  | 256 | } | 
|  | 257 | } | 
|  | 258 | } | 
|  | 259 |  | 
| Elliott Hughes | a933582 | 2024-04-22 21:01:38 +0000 | [diff] [blame] | 260 | static const char* __prop_error_to_string(int error) { | 
|  | 261 | switch (error) { | 
|  | 262 | case PROP_ERROR_READ_CMD: return "PROP_ERROR_READ_CMD"; | 
|  | 263 | case PROP_ERROR_READ_DATA: return "PROP_ERROR_READ_DATA"; | 
|  | 264 | case PROP_ERROR_READ_ONLY_PROPERTY: return "PROP_ERROR_READ_ONLY_PROPERTY"; | 
|  | 265 | case PROP_ERROR_INVALID_NAME: return "PROP_ERROR_INVALID_NAME"; | 
|  | 266 | case PROP_ERROR_INVALID_VALUE: return "PROP_ERROR_INVALID_VALUE"; | 
|  | 267 | case PROP_ERROR_PERMISSION_DENIED: return "PROP_ERROR_PERMISSION_DENIED"; | 
|  | 268 | case PROP_ERROR_INVALID_CMD: return "PROP_ERROR_INVALID_CMD"; | 
|  | 269 | case PROP_ERROR_HANDLE_CONTROL_MESSAGE: return "PROP_ERROR_HANDLE_CONTROL_MESSAGE"; | 
|  | 270 | case PROP_ERROR_SET_FAILED: return "PROP_ERROR_SET_FAILED"; | 
|  | 271 | } | 
|  | 272 | return "<unknown>"; | 
|  | 273 | } | 
|  | 274 |  | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 275 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
|  | 276 | int __system_property_set(const char* key, const char* value) { | 
|  | 277 | if (key == nullptr) return -1; | 
|  | 278 | if (value == nullptr) value = ""; | 
|  | 279 |  | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 280 | if (g_propservice_protocol_version == 0) { | 
|  | 281 | detect_protocol_version(); | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | if (g_propservice_protocol_version == kProtocolVersion1) { | 
|  | 285 | // Old protocol does not support long names or values | 
|  | 286 | if (strlen(key) >= PROP_NAME_MAX) return -1; | 
|  | 287 | if (strlen(value) >= PROP_VALUE_MAX) return -1; | 
|  | 288 |  | 
|  | 289 | prop_msg msg; | 
|  | 290 | memset(&msg, 0, sizeof msg); | 
|  | 291 | msg.cmd = PROP_MSG_SETPROP; | 
|  | 292 | strlcpy(msg.name, key, sizeof msg.name); | 
|  | 293 | strlcpy(msg.value, value, sizeof msg.value); | 
|  | 294 |  | 
|  | 295 | return send_prop_msg(&msg); | 
|  | 296 | } else { | 
|  | 297 | // New protocol only allows long values for ro. properties only. | 
|  | 298 | if (strlen(value) >= PROP_VALUE_MAX && strncmp(key, "ro.", 3) != 0) return -1; | 
|  | 299 | // Use proper protocol | 
| Paul Lawrence | 71ade01 | 2023-07-18 08:49:44 -0700 | [diff] [blame] | 300 | PropertyServiceConnection connection(key); | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 301 | if (!connection.IsValid()) { | 
|  | 302 | errno = connection.GetLastError(); | 
| Elliott Hughes | 2557f73 | 2023-07-12 21:15:23 +0000 | [diff] [blame] | 303 | async_safe_format_log(ANDROID_LOG_WARN, "libc", | 
|  | 304 | "Unable to set property \"%s\" to \"%s\": connection failed: %m", key, | 
|  | 305 | value); | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 306 | return -1; | 
|  | 307 | } | 
|  | 308 |  | 
|  | 309 | SocketWriter writer(&connection); | 
|  | 310 | if (!writer.WriteUint32(PROP_MSG_SETPROP2).WriteString(key).WriteString(value).Send()) { | 
|  | 311 | errno = connection.GetLastError(); | 
|  | 312 | async_safe_format_log(ANDROID_LOG_WARN, "libc", | 
| Elliott Hughes | 2557f73 | 2023-07-12 21:15:23 +0000 | [diff] [blame] | 313 | "Unable to set property \"%s\" to \"%s\": write failed: %m", key, | 
|  | 314 | value); | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 315 | return -1; | 
|  | 316 | } | 
|  | 317 |  | 
|  | 318 | int result = -1; | 
|  | 319 | if (!connection.RecvInt32(&result)) { | 
|  | 320 | errno = connection.GetLastError(); | 
|  | 321 | async_safe_format_log(ANDROID_LOG_WARN, "libc", | 
| Elliott Hughes | 2557f73 | 2023-07-12 21:15:23 +0000 | [diff] [blame] | 322 | "Unable to set property \"%s\" to \"%s\": recv failed: %m", key, value); | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 323 | return -1; | 
|  | 324 | } | 
|  | 325 |  | 
|  | 326 | if (result != PROP_SUCCESS) { | 
|  | 327 | async_safe_format_log(ANDROID_LOG_WARN, "libc", | 
| Elliott Hughes | a933582 | 2024-04-22 21:01:38 +0000 | [diff] [blame] | 328 | "Unable to set property \"%s\" to \"%s\": %s (0x%x)", key, value, | 
|  | 329 | __prop_error_to_string(result), result); | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 330 | return -1; | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | return 0; | 
|  | 334 | } | 
|  | 335 | } |