blob: 6960279589de651c0b7cc7500aac8d5b804a881c [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#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
34#include <sys/_system_properties.h>
35
36#include <sys/socket.h>
37#include <sys/un.h>
38#include <sys/select.h>
39#include <sys/types.h>
40#include <netinet/in.h>
41#include <sys/mman.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
Paul Lawrencea8d84342016-11-14 15:40:18 -080043#include <selinux/android.h>
rpcraig63207cd2012-08-09 10:05:49 -040044#include <selinux/selinux.h>
45#include <selinux/label.h>
rpcraig63207cd2012-08-09 10:05:49 -040046
Andres Moralesdb5f5d42015-05-08 08:30:33 -070047#include <fs_mgr.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080048#include <android-base/file.h>
Keun-young Park7d320262017-02-17 17:48:56 -080049#include <android-base/stringprintf.h>
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000050#include <android-base/strings.h>
Andres Moralesdb5f5d42015-05-08 08:30:33 -070051#include "bootimg.h"
52
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053#include "property_service.h"
54#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070055#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070056#include "log.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Keun-young Park7d320262017-02-17 17:48:56 -080058using android::base::StringPrintf;
59
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060#define PERSISTENT_PROPERTY_DIR "/data/property"
Andres Moralesdb5f5d42015-05-08 08:30:33 -070061#define RECOVERY_MOUNT_POINT "/recovery"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
63static int persistent_properties_loaded = 0;
64
Colin Crossd11beb22010-04-13 19:33:37 -070065static int property_set_fd = -1;
66
Elliott Hughesda40c002015-03-27 23:20:44 -070067void property_init() {
Elliott Hughesda40c002015-03-27 23:20:44 -070068 if (__system_property_area_init()) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070069 LOG(ERROR) << "Failed to initialize property area";
Tom Cherry60361142015-12-01 17:16:53 -080070 exit(1);
Elliott Hughesda40c002015-03-27 23:20:44 -070071 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072}
73
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000074static bool check_mac_perms(const std::string& name, char* sctx, struct ucred* cr) {
75
76 if (!sctx) {
77 return false;
78 }
79
80 if (!sehandle_prop) {
81 return false;
82 }
83
84 char* tctx = nullptr;
85 if (selabel_lookup(sehandle_prop, &tctx, name.c_str(), 1) != 0) {
86 return false;
87 }
88
William Robertsd7aea442015-10-01 16:03:47 -070089 property_audit_data audit_data;
rpcraig63207cd2012-08-09 10:05:49 -040090
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000091 audit_data.name = name.c_str();
William Robertsd7aea442015-10-01 16:03:47 -070092 audit_data.cr = cr;
93
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000094 bool has_access = (selinux_check_access(sctx, tctx, "property_service", "set", &audit_data) == 0);
rpcraig63207cd2012-08-09 10:05:49 -040095
96 freecon(tctx);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000097 return has_access;
rpcraig63207cd2012-08-09 10:05:49 -040098}
99
William Robertsd7aea442015-10-01 16:03:47 -0700100static int check_control_mac_perms(const char *name, char *sctx, struct ucred *cr)
rpcraig63207cd2012-08-09 10:05:49 -0400101{
rpcraig63207cd2012-08-09 10:05:49 -0400102 /*
103 * Create a name prefix out of ctl.<service name>
104 * The new prefix allows the use of the existing
105 * property service backend labeling while avoiding
106 * mislabels based on true property prefixes.
107 */
108 char ctl_name[PROP_VALUE_MAX+4];
109 int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
110
111 if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
112 return 0;
113
William Robertsd7aea442015-10-01 16:03:47 -0700114 return check_mac_perms(ctl_name, sctx, cr);
rpcraig63207cd2012-08-09 10:05:49 -0400115}
116
Yabin Cui74edcea2015-07-24 10:11:05 -0700117std::string property_get(const char* name) {
118 char value[PROP_VALUE_MAX] = {0};
119 __system_property_get(name, value);
120 return value;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121}
122
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800123static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124{
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700125 char tempPath[PATH_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 char path[PATH_MAX];
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700127 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700129 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
130 fd = mkstemp(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 if (fd < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700132 PLOG(ERROR) << "Unable to write persistent property to temp file " << tempPath;
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800133 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 }
135 write(fd, value, strlen(value));
OPPOde73a0c2014-03-28 19:12:47 +0800136 fsync(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 close(fd);
138
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700139 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 if (rename(tempPath, path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700141 PLOG(ERROR) << "Unable to rename persistent property file " << tempPath << " to " << path;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 unlink(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 }
144}
145
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000146bool is_legal_property_name(const std::string& name) {
Iliyan Malchevf6554802016-09-19 20:58:20 -0700147 size_t namelen = name.size();
148
Nick Kralevich69463612013-09-13 17:21:28 -0700149 if (namelen < 1) return false;
150 if (name[0] == '.') return false;
151 if (name[namelen - 1] == '.') return false;
152
Iliyan Malchevf6554802016-09-19 20:58:20 -0700153 /* Only allow alphanumeric, plus '.', '-', '@', or '_' */
Nick Kralevich69463612013-09-13 17:21:28 -0700154 /* Don't allow ".." to appear in a property name */
Iliyan Malchevf6554802016-09-19 20:58:20 -0700155 for (size_t i = 0; i < namelen; i++) {
Nick Kralevich69463612013-09-13 17:21:28 -0700156 if (name[i] == '.') {
Nick Kralevichc2c5a242013-09-16 11:30:48 -0700157 // i=0 is guaranteed to never have a dot. See above.
158 if (name[i-1] == '.') return false;
Nick Kralevich69463612013-09-13 17:21:28 -0700159 continue;
160 }
Iliyan Malchevf6554802016-09-19 20:58:20 -0700161 if (name[i] == '_' || name[i] == '-' || name[i] == '@') continue;
Nick Kralevich69463612013-09-13 17:21:28 -0700162 if (name[i] >= 'a' && name[i] <= 'z') continue;
163 if (name[i] >= 'A' && name[i] <= 'Z') continue;
164 if (name[i] >= '0' && name[i] <= '9') continue;
165 return false;
166 }
167
168 return true;
169}
170
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000171uint32_t property_set(const std::string& name, const std::string& value) {
172 size_t valuelen = value.size();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000174 if (!is_legal_property_name(name)) {
175 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: bad name";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000176 return PROP_ERROR_INVALID_NAME;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000177 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000178
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000179 if (valuelen >= PROP_VALUE_MAX) {
180 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
181 << "value too long";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000182 return PROP_ERROR_INVALID_VALUE;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000183 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000185 if (name == "selinux.restorecon_recursive" && valuelen > 0) {
186 if (restorecon(value.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700187 LOG(ERROR) << "Failed to restorecon_recursive " << value;
Jeff Sharkey76417512015-06-09 11:02:55 -0700188 }
189 }
190
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000191 prop_info* pi = (prop_info*) __system_property_find(name.c_str());
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000192 if (pi != nullptr) {
193 // ro.* properties are actually "write-once".
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000194 if (android::base::StartsWith(name, "ro.")) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000195 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
196 << "property already set";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000197 return PROP_ERROR_READ_ONLY_PROPERTY;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000198 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000200 __system_property_update(pi, value.c_str(), valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 } else {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000202 int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700203 if (rc < 0) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000204 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
205 << "__system_property_add failed";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000206 return PROP_ERROR_SET_FAILED;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200207 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000209
Elliott Hughes4f915812016-12-05 13:12:48 -0800210 // 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 Ivanovc9bb0332017-01-24 20:43:58 +0000212 if (persistent_properties_loaded && android::base::StartsWith(name, "persist.")) {
213 write_persistent_property(name.c_str(), value.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000215 property_changed(name.c_str(), value.c_str());
216 return PROP_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217}
218
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000219class SocketConnection {
220 public:
221 SocketConnection(int socket, const struct ucred& cred)
222 : socket_(socket), cred_(cred) {}
223
224 ~SocketConnection() {
225 close(socket_);
226 }
227
228 bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
229 return RecvFully(value, sizeof(*value), timeout_ms);
230 }
231
232 bool RecvChars(char* chars, size_t size, uint32_t* timeout_ms) {
233 return RecvFully(chars, size, timeout_ms);
234 }
235
236 bool RecvString(std::string* value, uint32_t* timeout_ms) {
237 uint32_t len = 0;
238 if (!RecvUint32(&len, timeout_ms)) {
239 return false;
240 }
241
242 if (len == 0) {
243 *value = "";
244 return true;
245 }
246
Elliott Hughesb005d902017-02-22 14:54:15 -0800247 // http://b/35166374: don't allow init to make arbitrarily large allocations.
248 if (len > 0xffff) {
249 LOG(ERROR) << "sys_prop: RecvString asked to read huge string: " << len;
250 errno = ENOMEM;
251 return false;
252 }
253
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000254 std::vector<char> chars(len);
255 if (!RecvChars(&chars[0], len, timeout_ms)) {
256 return false;
257 }
258
259 *value = std::string(&chars[0], len);
260 return true;
261 }
262
263 bool SendUint32(uint32_t value) {
264 int result = TEMP_FAILURE_RETRY(send(socket_, &value, sizeof(value), 0));
265 return result == sizeof(value);
266 }
267
268 int socket() {
269 return socket_;
270 }
271
272 const struct ucred& cred() {
273 return cred_;
274 }
275
276 private:
277 bool PollIn(uint32_t* timeout_ms) {
278 struct pollfd ufds[1];
279 ufds[0].fd = socket_;
280 ufds[0].events = POLLIN;
281 ufds[0].revents = 0;
282 while (*timeout_ms > 0) {
283 Timer timer;
284 int nr = poll(ufds, 1, *timeout_ms);
James Hawkins27c05222017-01-26 11:55:44 -0800285 uint64_t millis = timer.duration_ms();
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000286 *timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis;
287
288 if (nr > 0) {
289 return true;
290 }
291
292 if (nr == 0) {
293 // Timeout
294 break;
295 }
296
297 if (nr < 0 && errno != EINTR) {
298 PLOG(ERROR) << "sys_prop: error waiting for uid " << cred_.uid << " to send property message";
299 return false;
300 } else { // errno == EINTR
301 // Timer rounds milliseconds down in case of EINTR we want it to be rounded up
302 // to avoid slowing init down by causing EINTR with under millisecond timeout.
303 if (*timeout_ms > 0) {
304 --(*timeout_ms);
305 }
306 }
307 }
308
309 LOG(ERROR) << "sys_prop: timeout waiting for uid " << cred_.uid << " to send property message.";
310 return false;
311 }
312
313 bool RecvFully(void* data_ptr, size_t size, uint32_t* timeout_ms) {
314 size_t bytes_left = size;
315 char* data = static_cast<char*>(data_ptr);
316 while (*timeout_ms > 0 && bytes_left > 0) {
317 if (!PollIn(timeout_ms)) {
318 return false;
319 }
320
321 int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
322 if (result <= 0) {
323 return false;
324 }
325
326 bytes_left -= result;
327 data += result;
328 }
329
330 return bytes_left == 0;
331 }
332
333 int socket_;
334 struct ucred cred_;
335
336 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
337};
338
339static void handle_property_set(SocketConnection& socket,
340 const std::string& name,
341 const std::string& value,
342 bool legacy_protocol) {
343 const char* cmd_name = legacy_protocol ? "PROP_MSG_SETPROP" : "PROP_MSG_SETPROP2";
344 if (!is_legal_property_name(name)) {
345 LOG(ERROR) << "sys_prop(" << cmd_name << "): illegal property name \"" << name << "\"";
346 socket.SendUint32(PROP_ERROR_INVALID_NAME);
347 return;
348 }
349
350 struct ucred cr = socket.cred();
351 char* source_ctx = nullptr;
352 getpeercon(socket.socket(), &source_ctx);
353
354 if (android::base::StartsWith(name, "ctl.")) {
355 if (check_control_mac_perms(value.c_str(), source_ctx, &cr)) {
356 handle_control_message(name.c_str() + 4, value.c_str());
357 if (!legacy_protocol) {
358 socket.SendUint32(PROP_SUCCESS);
359 }
360 } else {
361 LOG(ERROR) << "sys_prop(" << cmd_name << "): Unable to " << (name.c_str() + 4)
362 << " service ctl [" << value << "]"
363 << " uid:" << cr.uid
364 << " gid:" << cr.gid
365 << " pid:" << cr.pid;
366 if (!legacy_protocol) {
367 socket.SendUint32(PROP_ERROR_HANDLE_CONTROL_MESSAGE);
368 }
369 }
370 } else {
371 if (check_mac_perms(name, source_ctx, &cr)) {
372 uint32_t result = property_set(name, value);
373 if (!legacy_protocol) {
374 socket.SendUint32(result);
375 }
376 } else {
377 LOG(ERROR) << "sys_prop(" << cmd_name << "): permission denied uid:" << cr.uid << " name:" << name;
378 if (!legacy_protocol) {
379 socket.SendUint32(PROP_ERROR_PERMISSION_DENIED);
380 }
381 }
382 }
383
384 freecon(source_ctx);
385}
386
387static void handle_property_set_fd() {
388 static constexpr uint32_t kDefaultSocketTimeout = 2000; /* ms */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389
Robert Sesekca2da602017-01-25 08:08:51 -0500390 int s = accept4(property_set_fd, nullptr, nullptr, SOCK_CLOEXEC);
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700391 if (s == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 return;
393 }
394
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700395 struct ucred cr;
396 socklen_t cr_size = sizeof(cr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
398 close(s);
Elliott Hughesb005d902017-02-22 14:54:15 -0800399 PLOG(ERROR) << "sys_prop: unable to get SO_PEERCRED";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400 return;
401 }
402
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000403 SocketConnection socket(s, cr);
404 uint32_t timeout_ms = kDefaultSocketTimeout;
405
406 uint32_t cmd = 0;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000407 if (!socket.RecvUint32(&cmd, &timeout_ms)) {
408 PLOG(ERROR) << "sys_prop: error while reading command from the socket";
409 socket.SendUint32(PROP_ERROR_READ_CMD);
JP Abgrall4515d812014-01-31 14:37:07 -0800410 return;
411 }
412
Elliott Hughesb005d902017-02-22 14:54:15 -0800413 switch (cmd) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000414 case PROP_MSG_SETPROP: {
415 char prop_name[PROP_NAME_MAX];
416 char prop_value[PROP_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000418 if (!socket.RecvChars(prop_name, PROP_NAME_MAX, &timeout_ms) ||
419 !socket.RecvChars(prop_value, PROP_VALUE_MAX, &timeout_ms)) {
420 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP): error while reading name/value from the socket";
421 return;
Nick Kralevich69463612013-09-13 17:21:28 -0700422 }
423
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000424 prop_name[PROP_NAME_MAX-1] = 0;
425 prop_value[PROP_VALUE_MAX-1] = 0;
rpcraig63207cd2012-08-09 10:05:49 -0400426
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000427 handle_property_set(socket, prop_value, prop_value, true);
Dimitry Ivanovdee4bd22017-01-19 14:53:00 -0800428 break;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000429 }
Dimitry Ivanov70c4ecf2017-01-24 18:38:09 +0000430
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000431 case PROP_MSG_SETPROP2: {
432 std::string name;
433 std::string value;
434 if (!socket.RecvString(&name, &timeout_ms) ||
435 !socket.RecvString(&value, &timeout_ms)) {
436 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP2): error while reading name/value from the socket";
437 socket.SendUint32(PROP_ERROR_READ_DATA);
438 return;
439 }
440
441 handle_property_set(socket, name, value, false);
442 break;
443 }
Elliott Hughesb005d902017-02-22 14:54:15 -0800444
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445 default:
Elliott Hughesb005d902017-02-22 14:54:15 -0800446 LOG(ERROR) << "sys_prop: invalid command " << cmd;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000447 socket.SendUint32(PROP_ERROR_INVALID_CMD);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 break;
449 }
450}
451
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800452static void load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800454/*
455 * Filter is used to decide which properties to load: NULL loads all keys,
456 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
457 */
458static void load_properties(char *data, const char *filter)
459{
460 char *key, *value, *eol, *sol, *tmp, *fn;
461 size_t flen = 0;
462
463 if (filter) {
464 flen = strlen(filter);
465 }
466
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800468 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 key = sol;
470 *eol++ = 0;
471 sol = eol;
472
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800473 while (isspace(*key)) key++;
474 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800477 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800479 if (!strncmp(key, "import ", 7) && flen == 0) {
480 fn = key + 7;
481 while (isspace(*fn)) fn++;
482
483 key = strchr(fn, ' ');
484 if (key) {
485 *key++ = 0;
486 while (isspace(*key)) key++;
487 }
488
489 load_properties_from_file(fn, key);
490
491 } else {
492 value = strchr(key, '=');
493 if (!value) continue;
494 *value++ = 0;
495
496 tmp = value - 2;
497 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
498
499 while (isspace(*value)) value++;
500
501 if (flen > 0) {
502 if (filter[flen - 1] == '*') {
503 if (strncmp(key, filter, flen - 1)) continue;
504 } else {
505 if (strcmp(key, filter)) continue;
506 }
507 }
508
509 property_set(key, value);
510 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 }
512}
513
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700514// Filter is used to decide which properties to load: NULL loads all keys,
515// "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
Elliott Hughesda40c002015-03-27 23:20:44 -0700516static void load_properties_from_file(const char* filename, const char* filter) {
517 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800518 std::string data;
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700519 if (!read_file(filename, &data)) {
520 PLOG(WARNING) << "Couldn't load properties from " << filename;
521 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522 }
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700523 data.push_back('\n');
524 load_properties(&data[0], filter);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000525 LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t << ".)";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526}
527
Elliott Hughes81399e12015-03-10 08:39:45 -0700528static void load_persistent_properties() {
529 persistent_properties_loaded = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530
Elliott Hughes81399e12015-03-10 08:39:45 -0700531 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
532 if (!dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700533 PLOG(ERROR) << "Unable to open persistent property directory \""
534 << PERSISTENT_PROPERTY_DIR << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700535 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800537
Elliott Hughes81399e12015-03-10 08:39:45 -0700538 struct dirent* entry;
539 while ((entry = readdir(dir.get())) != NULL) {
540 if (strncmp("persist.", entry->d_name, strlen("persist."))) {
541 continue;
542 }
543 if (entry->d_type != DT_REG) {
544 continue;
545 }
546
547 // Open the file and read the property value.
548 int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
549 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700550 PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700551 continue;
552 }
553
554 struct stat sb;
555 if (fstat(fd, &sb) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700556 PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
Elliott Hughes81399e12015-03-10 08:39:45 -0700557 close(fd);
558 continue;
559 }
560
561 // File must not be accessible to others, be owned by root/root, and
562 // not be a hard link to any other file.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700563 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 || sb.st_nlink != 1) {
564 PLOG(ERROR) << "skipping insecure property file " << entry->d_name
565 << " (uid=" << sb.st_uid << " gid=" << sb.st_gid
566 << " nlink=" << sb.st_nlink << " mode=" << std::oct << sb.st_mode << ")";
Elliott Hughes81399e12015-03-10 08:39:45 -0700567 close(fd);
568 continue;
569 }
570
571 char value[PROP_VALUE_MAX];
572 int length = read(fd, value, sizeof(value) - 1);
573 if (length >= 0) {
574 value[length] = 0;
575 property_set(entry->d_name, value);
576 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700577 PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
Elliott Hughes81399e12015-03-10 08:39:45 -0700578 }
579 close(fd);
580 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581}
582
Elliott Hughesda40c002015-03-27 23:20:44 -0700583void property_load_boot_defaults() {
Elliott Hughes795798d2017-01-27 16:21:55 -0800584 load_properties_from_file("/default.prop", NULL);
585 load_properties_from_file("/odm/default.prop", NULL);
586 load_properties_from_file("/vendor/default.prop", NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800587}
588
Nick Kralevich32b90232012-09-19 11:15:24 -0700589static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800590 if (ALLOW_LOCAL_PROP_OVERRIDE) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700591 std::string debuggable = property_get("ro.debuggable");
592 if (debuggable == "1") {
Elliott Hughes795798d2017-01-27 16:21:55 -0800593 load_properties_from_file("/data/local.prop", NULL);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800594 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700595 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700596}
597
Ken Sumrallc5c51032011-03-08 17:01:29 -0800598/* When booting an encrypted system, /data is not mounted when the
599 * property service is started, so any properties stored there are
600 * not loaded. Vold triggers init to load these properties once it
601 * has mounted /data.
602 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700603void load_persist_props(void) {
Nick Kralevich32b90232012-09-19 11:15:24 -0700604 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800605 /* Read persistent properties after all default values have been loaded. */
606 load_persistent_properties();
Keun-young Park404906d2017-02-28 19:20:38 -0800607 property_set("ro.persistent_properties.ready", "true");
Ken Sumrallc5c51032011-03-08 17:01:29 -0800608}
609
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700610void load_recovery_id_prop() {
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800611 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
612 fs_mgr_free_fstab);
613 if (!fstab) {
614 PLOG(ERROR) << "unable to read default fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700615 return;
616 }
617
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800618 fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab.get(), RECOVERY_MOUNT_POINT);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700619 if (rec == NULL) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700620 LOG(ERROR) << "/recovery not specified in fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700621 return;
622 }
623
624 int fd = open(rec->blk_device, O_RDONLY);
625 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700626 PLOG(ERROR) << "error opening block device " << rec->blk_device;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700627 return;
628 }
629
630 boot_img_hdr hdr;
631 if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
632 std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
633 property_set("ro.recovery_id", hex.c_str());
634 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700635 PLOG(ERROR) << "error reading /recovery";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700636 }
637
638 close(fd);
639}
640
Paul Lawrence948410a2015-07-01 14:40:56 -0700641void load_system_props() {
Elliott Hughes795798d2017-01-27 16:21:55 -0800642 load_properties_from_file("/system/build.prop", NULL);
643 load_properties_from_file("/odm/build.prop", NULL);
644 load_properties_from_file("/vendor/build.prop", NULL);
645 load_properties_from_file("/factory/factory.prop", "ro.*");
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700646 load_recovery_id_prop();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700647}
648
Elliott Hughesda40c002015-03-27 23:20:44 -0700649void start_property_service() {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000650 property_set("ro.property_service.version", "2");
651
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700652 property_set_fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
653 0666, 0, 0, NULL);
654 if (property_set_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700655 PLOG(ERROR) << "start_property_service socket creation failed";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700656 exit(1);
657 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800658
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700659 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700660
Elliott Hughes929f4072015-04-24 21:13:44 -0700661 register_epoll_handler(property_set_fd, handle_property_set_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662}