blob: decd6445c435ec5e46be2c0a6a5c0cfafe2a2b2e [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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
Keun-young Park7d320262017-02-17 17:48:56 -080017#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <string.h>
22#include <ctype.h>
23#include <fcntl.h>
24#include <stdarg.h>
25#include <dirent.h>
26#include <limits.h>
27#include <errno.h>
JP Abgrall4515d812014-01-31 14:37:07 -080028#include <sys/poll.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Elliott Hughes81399e12015-03-10 08:39:45 -070030#include <memory>
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000031#include <vector>
Elliott Hughes81399e12015-03-10 08:39:45 -070032
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033#include <cutils/misc.h>
34#include <cutils/sockets.h>
Matthew Xie40a91a22013-05-20 14:22:01 -070035#include <cutils/multiuser.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
37#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
38#include <sys/_system_properties.h>
39
40#include <sys/socket.h>
41#include <sys/un.h>
42#include <sys/select.h>
43#include <sys/types.h>
44#include <netinet/in.h>
45#include <sys/mman.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
Paul Lawrencea8d84342016-11-14 15:40:18 -080047#include <selinux/android.h>
rpcraig63207cd2012-08-09 10:05:49 -040048#include <selinux/selinux.h>
49#include <selinux/label.h>
rpcraig63207cd2012-08-09 10:05:49 -040050
Andres Moralesdb5f5d42015-05-08 08:30:33 -070051#include <fs_mgr.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/file.h>
Keun-young Park7d320262017-02-17 17:48:56 -080053#include <android-base/stringprintf.h>
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000054#include <android-base/strings.h>
Andres Moralesdb5f5d42015-05-08 08:30:33 -070055#include "bootimg.h"
56
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057#include "property_service.h"
58#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070059#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070060#include "log.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
Keun-young Park7d320262017-02-17 17:48:56 -080062using android::base::StringPrintf;
63
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064#define PERSISTENT_PROPERTY_DIR "/data/property"
Andres Moralesdb5f5d42015-05-08 08:30:33 -070065#define FSTAB_PREFIX "/fstab."
66#define RECOVERY_MOUNT_POINT "/recovery"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
68static int persistent_properties_loaded = 0;
69
Colin Crossd11beb22010-04-13 19:33:37 -070070static int property_set_fd = -1;
71
Elliott Hughesda40c002015-03-27 23:20:44 -070072void property_init() {
Elliott Hughesda40c002015-03-27 23:20:44 -070073 if (__system_property_area_init()) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070074 LOG(ERROR) << "Failed to initialize property area";
Tom Cherry60361142015-12-01 17:16:53 -080075 exit(1);
Elliott Hughesda40c002015-03-27 23:20:44 -070076 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077}
78
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000079static bool check_mac_perms(const std::string& name, char* sctx, struct ucred* cr) {
80
81 if (!sctx) {
82 return false;
83 }
84
85 if (!sehandle_prop) {
86 return false;
87 }
88
89 char* tctx = nullptr;
90 if (selabel_lookup(sehandle_prop, &tctx, name.c_str(), 1) != 0) {
91 return false;
92 }
93
William Robertsd7aea442015-10-01 16:03:47 -070094 property_audit_data audit_data;
rpcraig63207cd2012-08-09 10:05:49 -040095
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000096 audit_data.name = name.c_str();
William Robertsd7aea442015-10-01 16:03:47 -070097 audit_data.cr = cr;
98
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000099 bool has_access = (selinux_check_access(sctx, tctx, "property_service", "set", &audit_data) == 0);
rpcraig63207cd2012-08-09 10:05:49 -0400100
101 freecon(tctx);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000102 return has_access;
rpcraig63207cd2012-08-09 10:05:49 -0400103}
104
William Robertsd7aea442015-10-01 16:03:47 -0700105static int check_control_mac_perms(const char *name, char *sctx, struct ucred *cr)
rpcraig63207cd2012-08-09 10:05:49 -0400106{
rpcraig63207cd2012-08-09 10:05:49 -0400107 /*
108 * Create a name prefix out of ctl.<service name>
109 * The new prefix allows the use of the existing
110 * property service backend labeling while avoiding
111 * mislabels based on true property prefixes.
112 */
113 char ctl_name[PROP_VALUE_MAX+4];
114 int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
115
116 if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
117 return 0;
118
William Robertsd7aea442015-10-01 16:03:47 -0700119 return check_mac_perms(ctl_name, sctx, cr);
rpcraig63207cd2012-08-09 10:05:49 -0400120}
121
Yabin Cui74edcea2015-07-24 10:11:05 -0700122std::string property_get(const char* name) {
123 char value[PROP_VALUE_MAX] = {0};
124 __system_property_get(name, value);
125 return value;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126}
127
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800128static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129{
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700130 char tempPath[PATH_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 char path[PATH_MAX];
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700132 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700134 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
135 fd = mkstemp(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 if (fd < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700137 PLOG(ERROR) << "Unable to write persistent property to temp file " << tempPath;
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800138 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 }
140 write(fd, value, strlen(value));
OPPOde73a0c2014-03-28 19:12:47 +0800141 fsync(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 close(fd);
143
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700144 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 if (rename(tempPath, path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700146 PLOG(ERROR) << "Unable to rename persistent property file " << tempPath << " to " << path;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 unlink(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 }
149}
150
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000151bool is_legal_property_name(const std::string& name) {
Iliyan Malchevf6554802016-09-19 20:58:20 -0700152 size_t namelen = name.size();
153
Nick Kralevich69463612013-09-13 17:21:28 -0700154 if (namelen < 1) return false;
155 if (name[0] == '.') return false;
156 if (name[namelen - 1] == '.') return false;
157
Iliyan Malchevf6554802016-09-19 20:58:20 -0700158 /* Only allow alphanumeric, plus '.', '-', '@', or '_' */
Nick Kralevich69463612013-09-13 17:21:28 -0700159 /* Don't allow ".." to appear in a property name */
Iliyan Malchevf6554802016-09-19 20:58:20 -0700160 for (size_t i = 0; i < namelen; i++) {
Nick Kralevich69463612013-09-13 17:21:28 -0700161 if (name[i] == '.') {
Nick Kralevichc2c5a242013-09-16 11:30:48 -0700162 // i=0 is guaranteed to never have a dot. See above.
163 if (name[i-1] == '.') return false;
Nick Kralevich69463612013-09-13 17:21:28 -0700164 continue;
165 }
Iliyan Malchevf6554802016-09-19 20:58:20 -0700166 if (name[i] == '_' || name[i] == '-' || name[i] == '@') continue;
Nick Kralevich69463612013-09-13 17:21:28 -0700167 if (name[i] >= 'a' && name[i] <= 'z') continue;
168 if (name[i] >= 'A' && name[i] <= 'Z') continue;
169 if (name[i] >= '0' && name[i] <= '9') continue;
170 return false;
171 }
172
173 return true;
174}
175
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000176uint32_t property_set(const std::string& name, const std::string& value) {
177 size_t valuelen = value.size();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000179 if (!is_legal_property_name(name)) {
180 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: bad name";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000181 return PROP_ERROR_INVALID_NAME;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000182 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000183
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000184 if (valuelen >= PROP_VALUE_MAX) {
185 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
186 << "value too long";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000187 return PROP_ERROR_INVALID_VALUE;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000188 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000190 if (name == "selinux.restorecon_recursive" && valuelen > 0) {
191 if (restorecon(value.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700192 LOG(ERROR) << "Failed to restorecon_recursive " << value;
Jeff Sharkey76417512015-06-09 11:02:55 -0700193 }
194 }
195
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000196 prop_info* pi = (prop_info*) __system_property_find(name.c_str());
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000197 if (pi != nullptr) {
198 // ro.* properties are actually "write-once".
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000199 if (android::base::StartsWith(name, "ro.")) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000200 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
201 << "property already set";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000202 return PROP_ERROR_READ_ONLY_PROPERTY;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000203 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000205 __system_property_update(pi, value.c_str(), valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 } else {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000207 int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700208 if (rc < 0) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000209 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
210 << "__system_property_add failed";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000211 return PROP_ERROR_SET_FAILED;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200212 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000214
Elliott Hughes4f915812016-12-05 13:12:48 -0800215 // Don't write properties to disk until after we have read all default
216 // properties to prevent them from being overwritten by default values.
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000217 if (persistent_properties_loaded && android::base::StartsWith(name, "persist.")) {
218 write_persistent_property(name.c_str(), value.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000220 property_changed(name.c_str(), value.c_str());
221 return PROP_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222}
223
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000224class SocketConnection {
225 public:
226 SocketConnection(int socket, const struct ucred& cred)
227 : socket_(socket), cred_(cred) {}
228
229 ~SocketConnection() {
230 close(socket_);
231 }
232
233 bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
234 return RecvFully(value, sizeof(*value), timeout_ms);
235 }
236
237 bool RecvChars(char* chars, size_t size, uint32_t* timeout_ms) {
238 return RecvFully(chars, size, timeout_ms);
239 }
240
241 bool RecvString(std::string* value, uint32_t* timeout_ms) {
242 uint32_t len = 0;
243 if (!RecvUint32(&len, timeout_ms)) {
244 return false;
245 }
246
247 if (len == 0) {
248 *value = "";
249 return true;
250 }
251
Elliott Hughesb005d902017-02-22 14:54:15 -0800252 // http://b/35166374: don't allow init to make arbitrarily large allocations.
253 if (len > 0xffff) {
254 LOG(ERROR) << "sys_prop: RecvString asked to read huge string: " << len;
255 errno = ENOMEM;
256 return false;
257 }
258
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000259 std::vector<char> chars(len);
260 if (!RecvChars(&chars[0], len, timeout_ms)) {
261 return false;
262 }
263
264 *value = std::string(&chars[0], len);
265 return true;
266 }
267
268 bool SendUint32(uint32_t value) {
269 int result = TEMP_FAILURE_RETRY(send(socket_, &value, sizeof(value), 0));
270 return result == sizeof(value);
271 }
272
273 int socket() {
274 return socket_;
275 }
276
277 const struct ucred& cred() {
278 return cred_;
279 }
280
281 private:
282 bool PollIn(uint32_t* timeout_ms) {
283 struct pollfd ufds[1];
284 ufds[0].fd = socket_;
285 ufds[0].events = POLLIN;
286 ufds[0].revents = 0;
287 while (*timeout_ms > 0) {
288 Timer timer;
289 int nr = poll(ufds, 1, *timeout_ms);
James Hawkins27c05222017-01-26 11:55:44 -0800290 uint64_t millis = timer.duration_ms();
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000291 *timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis;
292
293 if (nr > 0) {
294 return true;
295 }
296
297 if (nr == 0) {
298 // Timeout
299 break;
300 }
301
302 if (nr < 0 && errno != EINTR) {
303 PLOG(ERROR) << "sys_prop: error waiting for uid " << cred_.uid << " to send property message";
304 return false;
305 } else { // errno == EINTR
306 // Timer rounds milliseconds down in case of EINTR we want it to be rounded up
307 // to avoid slowing init down by causing EINTR with under millisecond timeout.
308 if (*timeout_ms > 0) {
309 --(*timeout_ms);
310 }
311 }
312 }
313
314 LOG(ERROR) << "sys_prop: timeout waiting for uid " << cred_.uid << " to send property message.";
315 return false;
316 }
317
318 bool RecvFully(void* data_ptr, size_t size, uint32_t* timeout_ms) {
319 size_t bytes_left = size;
320 char* data = static_cast<char*>(data_ptr);
321 while (*timeout_ms > 0 && bytes_left > 0) {
322 if (!PollIn(timeout_ms)) {
323 return false;
324 }
325
326 int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
327 if (result <= 0) {
328 return false;
329 }
330
331 bytes_left -= result;
332 data += result;
333 }
334
335 return bytes_left == 0;
336 }
337
338 int socket_;
339 struct ucred cred_;
340
341 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
342};
343
344static void handle_property_set(SocketConnection& socket,
345 const std::string& name,
346 const std::string& value,
347 bool legacy_protocol) {
348 const char* cmd_name = legacy_protocol ? "PROP_MSG_SETPROP" : "PROP_MSG_SETPROP2";
349 if (!is_legal_property_name(name)) {
350 LOG(ERROR) << "sys_prop(" << cmd_name << "): illegal property name \"" << name << "\"";
351 socket.SendUint32(PROP_ERROR_INVALID_NAME);
352 return;
353 }
354
355 struct ucred cr = socket.cred();
356 char* source_ctx = nullptr;
357 getpeercon(socket.socket(), &source_ctx);
358
359 if (android::base::StartsWith(name, "ctl.")) {
360 if (check_control_mac_perms(value.c_str(), source_ctx, &cr)) {
361 handle_control_message(name.c_str() + 4, value.c_str());
362 if (!legacy_protocol) {
363 socket.SendUint32(PROP_SUCCESS);
364 }
365 } else {
366 LOG(ERROR) << "sys_prop(" << cmd_name << "): Unable to " << (name.c_str() + 4)
367 << " service ctl [" << value << "]"
368 << " uid:" << cr.uid
369 << " gid:" << cr.gid
370 << " pid:" << cr.pid;
371 if (!legacy_protocol) {
372 socket.SendUint32(PROP_ERROR_HANDLE_CONTROL_MESSAGE);
373 }
374 }
375 } else {
376 if (check_mac_perms(name, source_ctx, &cr)) {
377 uint32_t result = property_set(name, value);
378 if (!legacy_protocol) {
379 socket.SendUint32(result);
380 }
381 } else {
382 LOG(ERROR) << "sys_prop(" << cmd_name << "): permission denied uid:" << cr.uid << " name:" << name;
383 if (!legacy_protocol) {
384 socket.SendUint32(PROP_ERROR_PERMISSION_DENIED);
385 }
386 }
387 }
388
389 freecon(source_ctx);
390}
391
392static void handle_property_set_fd() {
393 static constexpr uint32_t kDefaultSocketTimeout = 2000; /* ms */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394
Robert Sesekca2da602017-01-25 08:08:51 -0500395 int s = accept4(property_set_fd, nullptr, nullptr, SOCK_CLOEXEC);
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700396 if (s == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 return;
398 }
399
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700400 struct ucred cr;
401 socklen_t cr_size = sizeof(cr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
403 close(s);
Elliott Hughesb005d902017-02-22 14:54:15 -0800404 PLOG(ERROR) << "sys_prop: unable to get SO_PEERCRED";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 return;
406 }
407
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000408 SocketConnection socket(s, cr);
409 uint32_t timeout_ms = kDefaultSocketTimeout;
410
411 uint32_t cmd = 0;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000412 if (!socket.RecvUint32(&cmd, &timeout_ms)) {
413 PLOG(ERROR) << "sys_prop: error while reading command from the socket";
414 socket.SendUint32(PROP_ERROR_READ_CMD);
JP Abgrall4515d812014-01-31 14:37:07 -0800415 return;
416 }
417
Elliott Hughesb005d902017-02-22 14:54:15 -0800418 switch (cmd) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000419 case PROP_MSG_SETPROP: {
420 char prop_name[PROP_NAME_MAX];
421 char prop_value[PROP_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000423 if (!socket.RecvChars(prop_name, PROP_NAME_MAX, &timeout_ms) ||
424 !socket.RecvChars(prop_value, PROP_VALUE_MAX, &timeout_ms)) {
425 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP): error while reading name/value from the socket";
426 return;
Nick Kralevich69463612013-09-13 17:21:28 -0700427 }
428
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000429 prop_name[PROP_NAME_MAX-1] = 0;
430 prop_value[PROP_VALUE_MAX-1] = 0;
rpcraig63207cd2012-08-09 10:05:49 -0400431
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000432 handle_property_set(socket, prop_value, prop_value, true);
Dimitry Ivanovdee4bd22017-01-19 14:53:00 -0800433 break;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000434 }
Dimitry Ivanov70c4ecf2017-01-24 18:38:09 +0000435
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000436 case PROP_MSG_SETPROP2: {
437 std::string name;
438 std::string value;
439 if (!socket.RecvString(&name, &timeout_ms) ||
440 !socket.RecvString(&value, &timeout_ms)) {
441 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP2): error while reading name/value from the socket";
442 socket.SendUint32(PROP_ERROR_READ_DATA);
443 return;
444 }
445
446 handle_property_set(socket, name, value, false);
447 break;
448 }
Elliott Hughesb005d902017-02-22 14:54:15 -0800449
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 default:
Elliott Hughesb005d902017-02-22 14:54:15 -0800451 LOG(ERROR) << "sys_prop: invalid command " << cmd;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000452 socket.SendUint32(PROP_ERROR_INVALID_CMD);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 break;
454 }
455}
456
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800457static void load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800459/*
460 * Filter is used to decide which properties to load: NULL loads all keys,
461 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
462 */
463static void load_properties(char *data, const char *filter)
464{
465 char *key, *value, *eol, *sol, *tmp, *fn;
466 size_t flen = 0;
467
468 if (filter) {
469 flen = strlen(filter);
470 }
471
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800473 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 key = sol;
475 *eol++ = 0;
476 sol = eol;
477
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800478 while (isspace(*key)) key++;
479 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800482 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800484 if (!strncmp(key, "import ", 7) && flen == 0) {
485 fn = key + 7;
486 while (isspace(*fn)) fn++;
487
488 key = strchr(fn, ' ');
489 if (key) {
490 *key++ = 0;
491 while (isspace(*key)) key++;
492 }
493
494 load_properties_from_file(fn, key);
495
496 } else {
497 value = strchr(key, '=');
498 if (!value) continue;
499 *value++ = 0;
500
501 tmp = value - 2;
502 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
503
504 while (isspace(*value)) value++;
505
506 if (flen > 0) {
507 if (filter[flen - 1] == '*') {
508 if (strncmp(key, filter, flen - 1)) continue;
509 } else {
510 if (strcmp(key, filter)) continue;
511 }
512 }
513
514 property_set(key, value);
515 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516 }
517}
518
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700519// Filter is used to decide which properties to load: NULL loads all keys,
520// "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
Elliott Hughesda40c002015-03-27 23:20:44 -0700521static void load_properties_from_file(const char* filename, const char* filter) {
522 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800523 std::string data;
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700524 if (!read_file(filename, &data)) {
525 PLOG(WARNING) << "Couldn't load properties from " << filename;
526 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 }
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700528 data.push_back('\n');
529 load_properties(&data[0], filter);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000530 LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t << ".)";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531}
532
Elliott Hughes81399e12015-03-10 08:39:45 -0700533static void load_persistent_properties() {
534 persistent_properties_loaded = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535
Elliott Hughes81399e12015-03-10 08:39:45 -0700536 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
537 if (!dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700538 PLOG(ERROR) << "Unable to open persistent property directory \""
539 << PERSISTENT_PROPERTY_DIR << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700540 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800542
Elliott Hughes81399e12015-03-10 08:39:45 -0700543 struct dirent* entry;
544 while ((entry = readdir(dir.get())) != NULL) {
545 if (strncmp("persist.", entry->d_name, strlen("persist."))) {
546 continue;
547 }
548 if (entry->d_type != DT_REG) {
549 continue;
550 }
551
552 // Open the file and read the property value.
553 int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
554 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700555 PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700556 continue;
557 }
558
559 struct stat sb;
560 if (fstat(fd, &sb) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700561 PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
Elliott Hughes81399e12015-03-10 08:39:45 -0700562 close(fd);
563 continue;
564 }
565
566 // File must not be accessible to others, be owned by root/root, and
567 // not be a hard link to any other file.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700568 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 || sb.st_nlink != 1) {
569 PLOG(ERROR) << "skipping insecure property file " << entry->d_name
570 << " (uid=" << sb.st_uid << " gid=" << sb.st_gid
571 << " nlink=" << sb.st_nlink << " mode=" << std::oct << sb.st_mode << ")";
Elliott Hughes81399e12015-03-10 08:39:45 -0700572 close(fd);
573 continue;
574 }
575
576 char value[PROP_VALUE_MAX];
577 int length = read(fd, value, sizeof(value) - 1);
578 if (length >= 0) {
579 value[length] = 0;
580 property_set(entry->d_name, value);
581 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700582 PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
Elliott Hughes81399e12015-03-10 08:39:45 -0700583 }
584 close(fd);
585 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800586}
587
Elliott Hughesda40c002015-03-27 23:20:44 -0700588void property_load_boot_defaults() {
Elliott Hughes795798d2017-01-27 16:21:55 -0800589 load_properties_from_file("/default.prop", NULL);
590 load_properties_from_file("/odm/default.prop", NULL);
591 load_properties_from_file("/vendor/default.prop", NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800592}
593
Nick Kralevich32b90232012-09-19 11:15:24 -0700594static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800595 if (ALLOW_LOCAL_PROP_OVERRIDE) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700596 std::string debuggable = property_get("ro.debuggable");
597 if (debuggable == "1") {
Elliott Hughes795798d2017-01-27 16:21:55 -0800598 load_properties_from_file("/data/local.prop", NULL);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800599 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700600 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700601}
602
Ken Sumrallc5c51032011-03-08 17:01:29 -0800603/* When booting an encrypted system, /data is not mounted when the
604 * property service is started, so any properties stored there are
605 * not loaded. Vold triggers init to load these properties once it
606 * has mounted /data.
607 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700608void load_persist_props(void) {
Nick Kralevich32b90232012-09-19 11:15:24 -0700609 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800610 /* Read persistent properties after all default values have been loaded. */
611 load_persistent_properties();
Keun-young Park7d320262017-02-17 17:48:56 -0800612 uint64_t start_ns = boot_clock::now().time_since_epoch().count();
613 property_set("ro.boottime.persistent_properties", StringPrintf("%" PRIu64, start_ns).c_str());
Ken Sumrallc5c51032011-03-08 17:01:29 -0800614}
615
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700616void load_recovery_id_prop() {
Yabin Cui74edcea2015-07-24 10:11:05 -0700617 std::string ro_hardware = property_get("ro.hardware");
618 if (ro_hardware.empty()) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700619 LOG(ERROR) << "ro.hardware not set - unable to load recovery id";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700620 return;
621 }
Yabin Cui74edcea2015-07-24 10:11:05 -0700622 std::string fstab_filename = FSTAB_PREFIX + ro_hardware;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700623
Yabin Cui74edcea2015-07-24 10:11:05 -0700624 std::unique_ptr<fstab, void(*)(fstab*)> tab(fs_mgr_read_fstab(fstab_filename.c_str()),
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700625 fs_mgr_free_fstab);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700626 if (!tab) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700627 PLOG(ERROR) << "unable to read fstab " << fstab_filename;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700628 return;
629 }
630
631 fstab_rec* rec = fs_mgr_get_entry_for_mount_point(tab.get(), RECOVERY_MOUNT_POINT);
632 if (rec == NULL) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700633 LOG(ERROR) << "/recovery not specified in fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700634 return;
635 }
636
637 int fd = open(rec->blk_device, O_RDONLY);
638 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700639 PLOG(ERROR) << "error opening block device " << rec->blk_device;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700640 return;
641 }
642
643 boot_img_hdr hdr;
644 if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
645 std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
646 property_set("ro.recovery_id", hex.c_str());
647 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700648 PLOG(ERROR) << "error reading /recovery";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700649 }
650
651 close(fd);
652}
653
Paul Lawrence948410a2015-07-01 14:40:56 -0700654void load_system_props() {
Elliott Hughes795798d2017-01-27 16:21:55 -0800655 load_properties_from_file("/system/build.prop", NULL);
656 load_properties_from_file("/odm/build.prop", NULL);
657 load_properties_from_file("/vendor/build.prop", NULL);
658 load_properties_from_file("/factory/factory.prop", "ro.*");
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700659 load_recovery_id_prop();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700660}
661
Elliott Hughesda40c002015-03-27 23:20:44 -0700662void start_property_service() {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000663 property_set("ro.property_service.version", "2");
664
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700665 property_set_fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
666 0666, 0, 0, NULL);
667 if (property_set_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700668 PLOG(ERROR) << "start_property_service socket creation failed";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700669 exit(1);
670 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700672 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700673
Elliott Hughes929f4072015-04-24 21:13:44 -0700674 register_epoll_handler(property_set_fd, handle_property_set_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675}