blob: 73cf15190e220c487787052f3377e4acd4ffecde [file] [log] [blame]
Tom Cherrye275d6d2017-12-11 23:31:33 -08001/*
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 Huangf2b4a972020-08-20 08:42:31 +000044#include <async_safe/log.h>
Bowgo Tsai61a5a832021-07-15 10:13:33 +000045#include <async_safe/CHECK.h>
Tom Cherrye275d6d2017-12-11 23:31:33 -080046
Bowgo Tsai61a5a832021-07-15 10:13:33 +000047#include "private/bionic_defs.h"
Josh Gao4956c372019-12-19 16:35:51 -080048#include "platform/bionic/macros.h"
Elliott Hughes6cb70ad2019-10-31 15:37:32 -070049#include "private/ScopedFd.h"
Tom Cherrye275d6d2017-12-11 23:31:33 -080050
51static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
Paul Lawrence71ade012023-07-18 08:49:44 -070052static const char property_service_for_system_socket[] =
53 "/dev/socket/" PROP_SERVICE_FOR_SYSTEM_NAME;
Tom Cherrye275d6d2017-12-11 23:31:33 -080054static const char* kServiceVersionPropertyName = "ro.property_service.version";
55
56class PropertyServiceConnection {
57 public:
Paul Lawrence71ade012023-07-18 08:49:44 -070058 PropertyServiceConnection(const char* name) : last_error_(0) {
Elliott Hughes6cb70ad2019-10-31 15:37:32 -070059 socket_.reset(::socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0));
60 if (socket_.get() == -1) {
Tom Cherrye275d6d2017-12-11 23:31:33 -080061 last_error_ = errno;
62 return;
63 }
64
Paul Lawrence71ade012023-07-18 08:49:44 -070065 // 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 Cherrye275d6d2017-12-11 23:31:33 -080077 sockaddr_un addr;
78 memset(&addr, 0, sizeof(addr));
Paul Lawrence71ade012023-07-18 08:49:44 -070079 strlcpy(addr.sun_path, socket, sizeof(addr.sun_path));
Tom Cherrye275d6d2017-12-11 23:31:33 -080080 addr.sun_family = AF_LOCAL;
81 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
82
Elliott Hughes6cb70ad2019-10-31 15:37:32 -070083 if (TEMP_FAILURE_RETRY(connect(socket_.get(),
84 reinterpret_cast<sockaddr*>(&addr), alen)) == -1) {
Tom Cherrye275d6d2017-12-11 23:31:33 -080085 last_error_ = errno;
Elliott Hughes6cb70ad2019-10-31 15:37:32 -070086 socket_.reset();
Tom Cherrye275d6d2017-12-11 23:31:33 -080087 }
88 }
89
90 bool IsValid() {
Elliott Hughes6cb70ad2019-10-31 15:37:32 -070091 return socket_.get() != -1;
Tom Cherrye275d6d2017-12-11 23:31:33 -080092 }
93
94 int GetLastError() {
95 return last_error_;
96 }
97
98 bool RecvInt32(int32_t* value) {
Elliott Hughes6cb70ad2019-10-31 15:37:32 -070099 int result = TEMP_FAILURE_RETRY(recv(socket_.get(), value, sizeof(*value), MSG_WAITALL));
Tom Cherrye275d6d2017-12-11 23:31:33 -0800100 return CheckSendRecvResult(result, sizeof(*value));
101 }
102
103 int socket() {
Elliott Hughes6cb70ad2019-10-31 15:37:32 -0700104 return socket_.get();
Tom Cherrye275d6d2017-12-11 23:31:33 -0800105 }
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 Hughes6cb70ad2019-10-31 15:37:32 -0700120 ScopedFd socket_;
Tom Cherrye275d6d2017-12-11 23:31:33 -0800121 int last_error_;
122
123 friend class SocketWriter;
124};
125
126class 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 Hughes5e62b342018-10-25 11:00:00 -0700182 BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(SocketWriter);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800183};
184
185struct prop_msg {
186 unsigned cmd;
187 char name[PROP_NAME_MAX];
188 char value[PROP_VALUE_MAX];
189};
190
191static int send_prop_msg(const prop_msg* msg) {
Paul Lawrence71ade012023-07-18 08:49:44 -0700192 PropertyServiceConnection connection(msg->name);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800193 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 Cherrye275d6d2017-12-11 23:31:33 -0800225 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
235static constexpr uint32_t kProtocolVersion1 = 1;
236static constexpr uint32_t kProtocolVersion2 = 2; // current
237
238static atomic_uint_least32_t g_propservice_protocol_version = 0;
239
240static 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 Hughesa9335822024-04-22 21:01:38 +0000260static 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 Cherrye275d6d2017-12-11 23:31:33 -0800275__BIONIC_WEAK_FOR_NATIVE_BRIDGE
276int __system_property_set(const char* key, const char* value) {
277 if (key == nullptr) return -1;
278 if (value == nullptr) value = "";
279
Tom Cherrye275d6d2017-12-11 23:31:33 -0800280 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 Lawrence71ade012023-07-18 08:49:44 -0700300 PropertyServiceConnection connection(key);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800301 if (!connection.IsValid()) {
302 errno = connection.GetLastError();
Elliott Hughes2557f732023-07-12 21:15:23 +0000303 async_safe_format_log(ANDROID_LOG_WARN, "libc",
304 "Unable to set property \"%s\" to \"%s\": connection failed: %m", key,
305 value);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800306 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 Hughes2557f732023-07-12 21:15:23 +0000313 "Unable to set property \"%s\" to \"%s\": write failed: %m", key,
314 value);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800315 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 Hughes2557f732023-07-12 21:15:23 +0000322 "Unable to set property \"%s\" to \"%s\": recv failed: %m", key, value);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800323 return -1;
324 }
325
326 if (result != PROP_SUCCESS) {
327 async_safe_format_log(ANDROID_LOG_WARN, "libc",
Elliott Hughesa9335822024-04-22 21:01:38 +0000328 "Unable to set property \"%s\" to \"%s\": %s (0x%x)", key, value,
329 __prop_error_to_string(result), result);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800330 return -1;
331 }
332
333 return 0;
334 }
335}