blob: 983e6846be8c2ad18df3bae031bb34e8f7ace69b [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 RECOVERY_MOUNT_POINT "/recovery"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066
67static int persistent_properties_loaded = 0;
68
Colin Crossd11beb22010-04-13 19:33:37 -070069static int property_set_fd = -1;
70
Elliott Hughesda40c002015-03-27 23:20:44 -070071void property_init() {
Elliott Hughesda40c002015-03-27 23:20:44 -070072 if (__system_property_area_init()) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070073 LOG(ERROR) << "Failed to initialize property area";
Tom Cherry60361142015-12-01 17:16:53 -080074 exit(1);
Elliott Hughesda40c002015-03-27 23:20:44 -070075 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076}
77
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000078static bool check_mac_perms(const std::string& name, char* sctx, struct ucred* cr) {
79
80 if (!sctx) {
81 return false;
82 }
83
84 if (!sehandle_prop) {
85 return false;
86 }
87
88 char* tctx = nullptr;
89 if (selabel_lookup(sehandle_prop, &tctx, name.c_str(), 1) != 0) {
90 return false;
91 }
92
William Robertsd7aea442015-10-01 16:03:47 -070093 property_audit_data audit_data;
rpcraig63207cd2012-08-09 10:05:49 -040094
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000095 audit_data.name = name.c_str();
William Robertsd7aea442015-10-01 16:03:47 -070096 audit_data.cr = cr;
97
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000098 bool has_access = (selinux_check_access(sctx, tctx, "property_service", "set", &audit_data) == 0);
rpcraig63207cd2012-08-09 10:05:49 -040099
100 freecon(tctx);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000101 return has_access;
rpcraig63207cd2012-08-09 10:05:49 -0400102}
103
William Robertsd7aea442015-10-01 16:03:47 -0700104static int check_control_mac_perms(const char *name, char *sctx, struct ucred *cr)
rpcraig63207cd2012-08-09 10:05:49 -0400105{
rpcraig63207cd2012-08-09 10:05:49 -0400106 /*
107 * Create a name prefix out of ctl.<service name>
108 * The new prefix allows the use of the existing
109 * property service backend labeling while avoiding
110 * mislabels based on true property prefixes.
111 */
112 char ctl_name[PROP_VALUE_MAX+4];
113 int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
114
115 if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
116 return 0;
117
William Robertsd7aea442015-10-01 16:03:47 -0700118 return check_mac_perms(ctl_name, sctx, cr);
rpcraig63207cd2012-08-09 10:05:49 -0400119}
120
Yabin Cui74edcea2015-07-24 10:11:05 -0700121std::string property_get(const char* name) {
122 char value[PROP_VALUE_MAX] = {0};
123 __system_property_get(name, value);
124 return value;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125}
126
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800127static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128{
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700129 char tempPath[PATH_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 char path[PATH_MAX];
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700131 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700133 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
134 fd = mkstemp(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 if (fd < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700136 PLOG(ERROR) << "Unable to write persistent property to temp file " << tempPath;
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800137 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 }
139 write(fd, value, strlen(value));
OPPOde73a0c2014-03-28 19:12:47 +0800140 fsync(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 close(fd);
142
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700143 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 if (rename(tempPath, path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700145 PLOG(ERROR) << "Unable to rename persistent property file " << tempPath << " to " << path;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 unlink(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 }
148}
149
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000150bool is_legal_property_name(const std::string& name) {
Iliyan Malchevf6554802016-09-19 20:58:20 -0700151 size_t namelen = name.size();
152
Nick Kralevich69463612013-09-13 17:21:28 -0700153 if (namelen < 1) return false;
154 if (name[0] == '.') return false;
155 if (name[namelen - 1] == '.') return false;
156
Iliyan Malchevf6554802016-09-19 20:58:20 -0700157 /* Only allow alphanumeric, plus '.', '-', '@', or '_' */
Nick Kralevich69463612013-09-13 17:21:28 -0700158 /* Don't allow ".." to appear in a property name */
Iliyan Malchevf6554802016-09-19 20:58:20 -0700159 for (size_t i = 0; i < namelen; i++) {
Nick Kralevich69463612013-09-13 17:21:28 -0700160 if (name[i] == '.') {
Nick Kralevichc2c5a242013-09-16 11:30:48 -0700161 // i=0 is guaranteed to never have a dot. See above.
162 if (name[i-1] == '.') return false;
Nick Kralevich69463612013-09-13 17:21:28 -0700163 continue;
164 }
Iliyan Malchevf6554802016-09-19 20:58:20 -0700165 if (name[i] == '_' || name[i] == '-' || name[i] == '@') continue;
Nick Kralevich69463612013-09-13 17:21:28 -0700166 if (name[i] >= 'a' && name[i] <= 'z') continue;
167 if (name[i] >= 'A' && name[i] <= 'Z') continue;
168 if (name[i] >= '0' && name[i] <= '9') continue;
169 return false;
170 }
171
172 return true;
173}
174
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000175uint32_t property_set(const std::string& name, const std::string& value) {
176 size_t valuelen = value.size();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000178 if (!is_legal_property_name(name)) {
179 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: bad name";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000180 return PROP_ERROR_INVALID_NAME;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000181 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000182
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000183 if (valuelen >= PROP_VALUE_MAX) {
184 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
185 << "value too long";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000186 return PROP_ERROR_INVALID_VALUE;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000187 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000189 if (name == "selinux.restorecon_recursive" && valuelen > 0) {
190 if (restorecon(value.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700191 LOG(ERROR) << "Failed to restorecon_recursive " << value;
Jeff Sharkey76417512015-06-09 11:02:55 -0700192 }
193 }
194
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000195 prop_info* pi = (prop_info*) __system_property_find(name.c_str());
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000196 if (pi != nullptr) {
197 // ro.* properties are actually "write-once".
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000198 if (android::base::StartsWith(name, "ro.")) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000199 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
200 << "property already set";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000201 return PROP_ERROR_READ_ONLY_PROPERTY;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000202 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000204 __system_property_update(pi, value.c_str(), valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 } else {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000206 int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700207 if (rc < 0) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000208 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
209 << "__system_property_add failed";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000210 return PROP_ERROR_SET_FAILED;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200211 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000213
Elliott Hughes4f915812016-12-05 13:12:48 -0800214 // Don't write properties to disk until after we have read all default
215 // properties to prevent them from being overwritten by default values.
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000216 if (persistent_properties_loaded && android::base::StartsWith(name, "persist.")) {
217 write_persistent_property(name.c_str(), value.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000219 property_changed(name.c_str(), value.c_str());
220 return PROP_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221}
222
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000223class SocketConnection {
224 public:
225 SocketConnection(int socket, const struct ucred& cred)
226 : socket_(socket), cred_(cred) {}
227
228 ~SocketConnection() {
229 close(socket_);
230 }
231
232 bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
233 return RecvFully(value, sizeof(*value), timeout_ms);
234 }
235
236 bool RecvChars(char* chars, size_t size, uint32_t* timeout_ms) {
237 return RecvFully(chars, size, timeout_ms);
238 }
239
240 bool RecvString(std::string* value, uint32_t* timeout_ms) {
241 uint32_t len = 0;
242 if (!RecvUint32(&len, timeout_ms)) {
243 return false;
244 }
245
246 if (len == 0) {
247 *value = "";
248 return true;
249 }
250
Elliott Hughesb005d902017-02-22 14:54:15 -0800251 // http://b/35166374: don't allow init to make arbitrarily large allocations.
252 if (len > 0xffff) {
253 LOG(ERROR) << "sys_prop: RecvString asked to read huge string: " << len;
254 errno = ENOMEM;
255 return false;
256 }
257
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000258 std::vector<char> chars(len);
259 if (!RecvChars(&chars[0], len, timeout_ms)) {
260 return false;
261 }
262
263 *value = std::string(&chars[0], len);
264 return true;
265 }
266
267 bool SendUint32(uint32_t value) {
268 int result = TEMP_FAILURE_RETRY(send(socket_, &value, sizeof(value), 0));
269 return result == sizeof(value);
270 }
271
272 int socket() {
273 return socket_;
274 }
275
276 const struct ucred& cred() {
277 return cred_;
278 }
279
280 private:
281 bool PollIn(uint32_t* timeout_ms) {
282 struct pollfd ufds[1];
283 ufds[0].fd = socket_;
284 ufds[0].events = POLLIN;
285 ufds[0].revents = 0;
286 while (*timeout_ms > 0) {
287 Timer timer;
288 int nr = poll(ufds, 1, *timeout_ms);
James Hawkins27c05222017-01-26 11:55:44 -0800289 uint64_t millis = timer.duration_ms();
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000290 *timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis;
291
292 if (nr > 0) {
293 return true;
294 }
295
296 if (nr == 0) {
297 // Timeout
298 break;
299 }
300
301 if (nr < 0 && errno != EINTR) {
302 PLOG(ERROR) << "sys_prop: error waiting for uid " << cred_.uid << " to send property message";
303 return false;
304 } else { // errno == EINTR
305 // Timer rounds milliseconds down in case of EINTR we want it to be rounded up
306 // to avoid slowing init down by causing EINTR with under millisecond timeout.
307 if (*timeout_ms > 0) {
308 --(*timeout_ms);
309 }
310 }
311 }
312
313 LOG(ERROR) << "sys_prop: timeout waiting for uid " << cred_.uid << " to send property message.";
314 return false;
315 }
316
317 bool RecvFully(void* data_ptr, size_t size, uint32_t* timeout_ms) {
318 size_t bytes_left = size;
319 char* data = static_cast<char*>(data_ptr);
320 while (*timeout_ms > 0 && bytes_left > 0) {
321 if (!PollIn(timeout_ms)) {
322 return false;
323 }
324
325 int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
326 if (result <= 0) {
327 return false;
328 }
329
330 bytes_left -= result;
331 data += result;
332 }
333
334 return bytes_left == 0;
335 }
336
337 int socket_;
338 struct ucred cred_;
339
340 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
341};
342
343static void handle_property_set(SocketConnection& socket,
344 const std::string& name,
345 const std::string& value,
346 bool legacy_protocol) {
347 const char* cmd_name = legacy_protocol ? "PROP_MSG_SETPROP" : "PROP_MSG_SETPROP2";
348 if (!is_legal_property_name(name)) {
349 LOG(ERROR) << "sys_prop(" << cmd_name << "): illegal property name \"" << name << "\"";
350 socket.SendUint32(PROP_ERROR_INVALID_NAME);
351 return;
352 }
353
354 struct ucred cr = socket.cred();
355 char* source_ctx = nullptr;
356 getpeercon(socket.socket(), &source_ctx);
357
358 if (android::base::StartsWith(name, "ctl.")) {
359 if (check_control_mac_perms(value.c_str(), source_ctx, &cr)) {
360 handle_control_message(name.c_str() + 4, value.c_str());
361 if (!legacy_protocol) {
362 socket.SendUint32(PROP_SUCCESS);
363 }
364 } else {
365 LOG(ERROR) << "sys_prop(" << cmd_name << "): Unable to " << (name.c_str() + 4)
366 << " service ctl [" << value << "]"
367 << " uid:" << cr.uid
368 << " gid:" << cr.gid
369 << " pid:" << cr.pid;
370 if (!legacy_protocol) {
371 socket.SendUint32(PROP_ERROR_HANDLE_CONTROL_MESSAGE);
372 }
373 }
374 } else {
375 if (check_mac_perms(name, source_ctx, &cr)) {
376 uint32_t result = property_set(name, value);
377 if (!legacy_protocol) {
378 socket.SendUint32(result);
379 }
380 } else {
381 LOG(ERROR) << "sys_prop(" << cmd_name << "): permission denied uid:" << cr.uid << " name:" << name;
382 if (!legacy_protocol) {
383 socket.SendUint32(PROP_ERROR_PERMISSION_DENIED);
384 }
385 }
386 }
387
388 freecon(source_ctx);
389}
390
391static void handle_property_set_fd() {
392 static constexpr uint32_t kDefaultSocketTimeout = 2000; /* ms */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393
Robert Sesekca2da602017-01-25 08:08:51 -0500394 int s = accept4(property_set_fd, nullptr, nullptr, SOCK_CLOEXEC);
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700395 if (s == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396 return;
397 }
398
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700399 struct ucred cr;
400 socklen_t cr_size = sizeof(cr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
402 close(s);
Elliott Hughesb005d902017-02-22 14:54:15 -0800403 PLOG(ERROR) << "sys_prop: unable to get SO_PEERCRED";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404 return;
405 }
406
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000407 SocketConnection socket(s, cr);
408 uint32_t timeout_ms = kDefaultSocketTimeout;
409
410 uint32_t cmd = 0;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000411 if (!socket.RecvUint32(&cmd, &timeout_ms)) {
412 PLOG(ERROR) << "sys_prop: error while reading command from the socket";
413 socket.SendUint32(PROP_ERROR_READ_CMD);
JP Abgrall4515d812014-01-31 14:37:07 -0800414 return;
415 }
416
Elliott Hughesb005d902017-02-22 14:54:15 -0800417 switch (cmd) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000418 case PROP_MSG_SETPROP: {
419 char prop_name[PROP_NAME_MAX];
420 char prop_value[PROP_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000422 if (!socket.RecvChars(prop_name, PROP_NAME_MAX, &timeout_ms) ||
423 !socket.RecvChars(prop_value, PROP_VALUE_MAX, &timeout_ms)) {
424 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP): error while reading name/value from the socket";
425 return;
Nick Kralevich69463612013-09-13 17:21:28 -0700426 }
427
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000428 prop_name[PROP_NAME_MAX-1] = 0;
429 prop_value[PROP_VALUE_MAX-1] = 0;
rpcraig63207cd2012-08-09 10:05:49 -0400430
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000431 handle_property_set(socket, prop_value, prop_value, true);
Dimitry Ivanovdee4bd22017-01-19 14:53:00 -0800432 break;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000433 }
Dimitry Ivanov70c4ecf2017-01-24 18:38:09 +0000434
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000435 case PROP_MSG_SETPROP2: {
436 std::string name;
437 std::string value;
438 if (!socket.RecvString(&name, &timeout_ms) ||
439 !socket.RecvString(&value, &timeout_ms)) {
440 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP2): error while reading name/value from the socket";
441 socket.SendUint32(PROP_ERROR_READ_DATA);
442 return;
443 }
444
445 handle_property_set(socket, name, value, false);
446 break;
447 }
Elliott Hughesb005d902017-02-22 14:54:15 -0800448
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 default:
Elliott Hughesb005d902017-02-22 14:54:15 -0800450 LOG(ERROR) << "sys_prop: invalid command " << cmd;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000451 socket.SendUint32(PROP_ERROR_INVALID_CMD);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452 break;
453 }
454}
455
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800456static void load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800458/*
459 * Filter is used to decide which properties to load: NULL loads all keys,
460 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
461 */
462static void load_properties(char *data, const char *filter)
463{
464 char *key, *value, *eol, *sol, *tmp, *fn;
465 size_t flen = 0;
466
467 if (filter) {
468 flen = strlen(filter);
469 }
470
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800472 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 key = sol;
474 *eol++ = 0;
475 sol = eol;
476
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800477 while (isspace(*key)) key++;
478 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800481 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800483 if (!strncmp(key, "import ", 7) && flen == 0) {
484 fn = key + 7;
485 while (isspace(*fn)) fn++;
486
487 key = strchr(fn, ' ');
488 if (key) {
489 *key++ = 0;
490 while (isspace(*key)) key++;
491 }
492
493 load_properties_from_file(fn, key);
494
495 } else {
496 value = strchr(key, '=');
497 if (!value) continue;
498 *value++ = 0;
499
500 tmp = value - 2;
501 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
502
503 while (isspace(*value)) value++;
504
505 if (flen > 0) {
506 if (filter[flen - 1] == '*') {
507 if (strncmp(key, filter, flen - 1)) continue;
508 } else {
509 if (strcmp(key, filter)) continue;
510 }
511 }
512
513 property_set(key, value);
514 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 }
516}
517
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700518// Filter is used to decide which properties to load: NULL loads all keys,
519// "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
Elliott Hughesda40c002015-03-27 23:20:44 -0700520static void load_properties_from_file(const char* filename, const char* filter) {
521 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800522 std::string data;
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700523 if (!read_file(filename, &data)) {
524 PLOG(WARNING) << "Couldn't load properties from " << filename;
525 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 }
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700527 data.push_back('\n');
528 load_properties(&data[0], filter);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000529 LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t << ".)";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530}
531
Elliott Hughes81399e12015-03-10 08:39:45 -0700532static void load_persistent_properties() {
533 persistent_properties_loaded = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534
Elliott Hughes81399e12015-03-10 08:39:45 -0700535 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
536 if (!dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700537 PLOG(ERROR) << "Unable to open persistent property directory \""
538 << PERSISTENT_PROPERTY_DIR << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700539 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800541
Elliott Hughes81399e12015-03-10 08:39:45 -0700542 struct dirent* entry;
543 while ((entry = readdir(dir.get())) != NULL) {
544 if (strncmp("persist.", entry->d_name, strlen("persist."))) {
545 continue;
546 }
547 if (entry->d_type != DT_REG) {
548 continue;
549 }
550
551 // Open the file and read the property value.
552 int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
553 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700554 PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700555 continue;
556 }
557
558 struct stat sb;
559 if (fstat(fd, &sb) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700560 PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
Elliott Hughes81399e12015-03-10 08:39:45 -0700561 close(fd);
562 continue;
563 }
564
565 // File must not be accessible to others, be owned by root/root, and
566 // not be a hard link to any other file.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700567 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 || sb.st_nlink != 1) {
568 PLOG(ERROR) << "skipping insecure property file " << entry->d_name
569 << " (uid=" << sb.st_uid << " gid=" << sb.st_gid
570 << " nlink=" << sb.st_nlink << " mode=" << std::oct << sb.st_mode << ")";
Elliott Hughes81399e12015-03-10 08:39:45 -0700571 close(fd);
572 continue;
573 }
574
575 char value[PROP_VALUE_MAX];
576 int length = read(fd, value, sizeof(value) - 1);
577 if (length >= 0) {
578 value[length] = 0;
579 property_set(entry->d_name, value);
580 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700581 PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
Elliott Hughes81399e12015-03-10 08:39:45 -0700582 }
583 close(fd);
584 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585}
586
Elliott Hughesda40c002015-03-27 23:20:44 -0700587void property_load_boot_defaults() {
Elliott Hughes795798d2017-01-27 16:21:55 -0800588 load_properties_from_file("/default.prop", NULL);
589 load_properties_from_file("/odm/default.prop", NULL);
590 load_properties_from_file("/vendor/default.prop", NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800591}
592
Nick Kralevich32b90232012-09-19 11:15:24 -0700593static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800594 if (ALLOW_LOCAL_PROP_OVERRIDE) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700595 std::string debuggable = property_get("ro.debuggable");
596 if (debuggable == "1") {
Elliott Hughes795798d2017-01-27 16:21:55 -0800597 load_properties_from_file("/data/local.prop", NULL);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800598 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700599 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700600}
601
Ken Sumrallc5c51032011-03-08 17:01:29 -0800602/* When booting an encrypted system, /data is not mounted when the
603 * property service is started, so any properties stored there are
604 * not loaded. Vold triggers init to load these properties once it
605 * has mounted /data.
606 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700607void load_persist_props(void) {
Nick Kralevich32b90232012-09-19 11:15:24 -0700608 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800609 /* Read persistent properties after all default values have been loaded. */
610 load_persistent_properties();
Keun-young Park404906d2017-02-28 19:20:38 -0800611 property_set("ro.persistent_properties.ready", "true");
Ken Sumrallc5c51032011-03-08 17:01:29 -0800612}
613
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700614void load_recovery_id_prop() {
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800615 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
616 fs_mgr_free_fstab);
617 if (!fstab) {
618 PLOG(ERROR) << "unable to read default fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700619 return;
620 }
621
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800622 fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab.get(), RECOVERY_MOUNT_POINT);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700623 if (rec == NULL) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700624 LOG(ERROR) << "/recovery not specified in fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700625 return;
626 }
627
628 int fd = open(rec->blk_device, O_RDONLY);
629 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700630 PLOG(ERROR) << "error opening block device " << rec->blk_device;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700631 return;
632 }
633
634 boot_img_hdr hdr;
635 if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
636 std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
637 property_set("ro.recovery_id", hex.c_str());
638 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700639 PLOG(ERROR) << "error reading /recovery";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700640 }
641
642 close(fd);
643}
644
Paul Lawrence948410a2015-07-01 14:40:56 -0700645void load_system_props() {
Elliott Hughes795798d2017-01-27 16:21:55 -0800646 load_properties_from_file("/system/build.prop", NULL);
647 load_properties_from_file("/odm/build.prop", NULL);
648 load_properties_from_file("/vendor/build.prop", NULL);
649 load_properties_from_file("/factory/factory.prop", "ro.*");
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700650 load_recovery_id_prop();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700651}
652
Elliott Hughesda40c002015-03-27 23:20:44 -0700653void start_property_service() {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000654 property_set("ro.property_service.version", "2");
655
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700656 property_set_fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
657 0666, 0, 0, NULL);
658 if (property_set_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700659 PLOG(ERROR) << "start_property_service socket creation failed";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700660 exit(1);
661 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700663 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700664
Elliott Hughes929f4072015-04-24 21:13:44 -0700665 register_epoll_handler(property_set_fd, handle_property_set_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666}