blob: 04bcb18626b53f0ad9a23e3fd549930f34a15671 [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <string.h>
21#include <ctype.h>
22#include <fcntl.h>
23#include <stdarg.h>
24#include <dirent.h>
25#include <limits.h>
26#include <errno.h>
JP Abgrall4515d812014-01-31 14:37:07 -080027#include <sys/poll.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Elliott Hughes81399e12015-03-10 08:39:45 -070029#include <memory>
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000030#include <vector>
Elliott Hughes81399e12015-03-10 08:39:45 -070031
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <cutils/misc.h>
33#include <cutils/sockets.h>
Matthew Xie40a91a22013-05-20 14:22:01 -070034#include <cutils/multiuser.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
36#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
37#include <sys/_system_properties.h>
38
39#include <sys/socket.h>
40#include <sys/un.h>
41#include <sys/select.h>
42#include <sys/types.h>
43#include <netinet/in.h>
44#include <sys/mman.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
Paul Lawrencea8d84342016-11-14 15:40:18 -080046#include <selinux/android.h>
rpcraig63207cd2012-08-09 10:05:49 -040047#include <selinux/selinux.h>
48#include <selinux/label.h>
rpcraig63207cd2012-08-09 10:05:49 -040049
Andres Moralesdb5f5d42015-05-08 08:30:33 -070050#include <fs_mgr.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080051#include <android-base/file.h>
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000052#include <android-base/strings.h>
Andres Moralesdb5f5d42015-05-08 08:30:33 -070053#include "bootimg.h"
54
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055#include "property_service.h"
56#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070057#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070058#include "log.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
60#define PERSISTENT_PROPERTY_DIR "/data/property"
Andres Moralesdb5f5d42015-05-08 08:30:33 -070061#define FSTAB_PREFIX "/fstab."
62#define RECOVERY_MOUNT_POINT "/recovery"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
64static int persistent_properties_loaded = 0;
65
Colin Crossd11beb22010-04-13 19:33:37 -070066static int property_set_fd = -1;
67
Elliott Hughesda40c002015-03-27 23:20:44 -070068void property_init() {
Elliott Hughesda40c002015-03-27 23:20:44 -070069 if (__system_property_area_init()) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070070 LOG(ERROR) << "Failed to initialize property area";
Tom Cherry60361142015-12-01 17:16:53 -080071 exit(1);
Elliott Hughesda40c002015-03-27 23:20:44 -070072 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073}
74
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000075static bool check_mac_perms(const std::string& name, char* sctx, struct ucred* cr) {
76
77 if (!sctx) {
78 return false;
79 }
80
81 if (!sehandle_prop) {
82 return false;
83 }
84
85 char* tctx = nullptr;
86 if (selabel_lookup(sehandle_prop, &tctx, name.c_str(), 1) != 0) {
87 return false;
88 }
89
William Robertsd7aea442015-10-01 16:03:47 -070090 property_audit_data audit_data;
rpcraig63207cd2012-08-09 10:05:49 -040091
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000092 audit_data.name = name.c_str();
William Robertsd7aea442015-10-01 16:03:47 -070093 audit_data.cr = cr;
94
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000095 bool has_access = (selinux_check_access(sctx, tctx, "property_service", "set", &audit_data) == 0);
rpcraig63207cd2012-08-09 10:05:49 -040096
97 freecon(tctx);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000098 return has_access;
rpcraig63207cd2012-08-09 10:05:49 -040099}
100
William Robertsd7aea442015-10-01 16:03:47 -0700101static int check_control_mac_perms(const char *name, char *sctx, struct ucred *cr)
rpcraig63207cd2012-08-09 10:05:49 -0400102{
rpcraig63207cd2012-08-09 10:05:49 -0400103 /*
104 * Create a name prefix out of ctl.<service name>
105 * The new prefix allows the use of the existing
106 * property service backend labeling while avoiding
107 * mislabels based on true property prefixes.
108 */
109 char ctl_name[PROP_VALUE_MAX+4];
110 int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
111
112 if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
113 return 0;
114
William Robertsd7aea442015-10-01 16:03:47 -0700115 return check_mac_perms(ctl_name, sctx, cr);
rpcraig63207cd2012-08-09 10:05:49 -0400116}
117
Yabin Cui74edcea2015-07-24 10:11:05 -0700118std::string property_get(const char* name) {
119 char value[PROP_VALUE_MAX] = {0};
120 __system_property_get(name, value);
121 return value;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122}
123
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800124static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125{
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700126 char tempPath[PATH_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 char path[PATH_MAX];
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700128 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700130 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
131 fd = mkstemp(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 if (fd < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700133 PLOG(ERROR) << "Unable to write persistent property to temp file " << tempPath;
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800134 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 }
136 write(fd, value, strlen(value));
OPPOde73a0c2014-03-28 19:12:47 +0800137 fsync(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 close(fd);
139
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700140 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 if (rename(tempPath, path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700142 PLOG(ERROR) << "Unable to rename persistent property file " << tempPath << " to " << path;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 unlink(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 }
145}
146
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000147bool is_legal_property_name(const std::string& name) {
Iliyan Malchevf6554802016-09-19 20:58:20 -0700148 size_t namelen = name.size();
149
Nick Kralevich69463612013-09-13 17:21:28 -0700150 if (namelen < 1) return false;
151 if (name[0] == '.') return false;
152 if (name[namelen - 1] == '.') return false;
153
Iliyan Malchevf6554802016-09-19 20:58:20 -0700154 /* Only allow alphanumeric, plus '.', '-', '@', or '_' */
Nick Kralevich69463612013-09-13 17:21:28 -0700155 /* Don't allow ".." to appear in a property name */
Iliyan Malchevf6554802016-09-19 20:58:20 -0700156 for (size_t i = 0; i < namelen; i++) {
Nick Kralevich69463612013-09-13 17:21:28 -0700157 if (name[i] == '.') {
Nick Kralevichc2c5a242013-09-16 11:30:48 -0700158 // i=0 is guaranteed to never have a dot. See above.
159 if (name[i-1] == '.') return false;
Nick Kralevich69463612013-09-13 17:21:28 -0700160 continue;
161 }
Iliyan Malchevf6554802016-09-19 20:58:20 -0700162 if (name[i] == '_' || name[i] == '-' || name[i] == '@') continue;
Nick Kralevich69463612013-09-13 17:21:28 -0700163 if (name[i] >= 'a' && name[i] <= 'z') continue;
164 if (name[i] >= 'A' && name[i] <= 'Z') continue;
165 if (name[i] >= '0' && name[i] <= '9') continue;
166 return false;
167 }
168
169 return true;
170}
171
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000172uint32_t property_set(const std::string& name, const std::string& value) {
173 size_t valuelen = value.size();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000175 if (!is_legal_property_name(name)) {
176 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: bad name";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000177 return PROP_ERROR_INVALID_NAME;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000178 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000179
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000180 if (valuelen >= PROP_VALUE_MAX) {
181 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
182 << "value too long";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000183 return PROP_ERROR_INVALID_VALUE;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000184 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000186 if (name == "selinux.restorecon_recursive" && valuelen > 0) {
187 if (restorecon(value.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700188 LOG(ERROR) << "Failed to restorecon_recursive " << value;
Jeff Sharkey76417512015-06-09 11:02:55 -0700189 }
190 }
191
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000192 prop_info* pi = (prop_info*) __system_property_find(name.c_str());
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000193 if (pi != nullptr) {
194 // ro.* properties are actually "write-once".
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000195 if (android::base::StartsWith(name, "ro.")) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000196 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
197 << "property already set";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000198 return PROP_ERROR_READ_ONLY_PROPERTY;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000199 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000201 __system_property_update(pi, value.c_str(), valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 } else {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000203 int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700204 if (rc < 0) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000205 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
206 << "__system_property_add failed";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000207 return PROP_ERROR_SET_FAILED;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200208 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000210
Elliott Hughes4f915812016-12-05 13:12:48 -0800211 // Don't write properties to disk until after we have read all default
212 // properties to prevent them from being overwritten by default values.
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000213 if (persistent_properties_loaded && android::base::StartsWith(name, "persist.")) {
214 write_persistent_property(name.c_str(), value.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000216 property_changed(name.c_str(), value.c_str());
217 return PROP_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218}
219
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000220class SocketConnection {
221 public:
222 SocketConnection(int socket, const struct ucred& cred)
223 : socket_(socket), cred_(cred) {}
224
225 ~SocketConnection() {
226 close(socket_);
227 }
228
229 bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
230 return RecvFully(value, sizeof(*value), timeout_ms);
231 }
232
233 bool RecvChars(char* chars, size_t size, uint32_t* timeout_ms) {
234 return RecvFully(chars, size, timeout_ms);
235 }
236
237 bool RecvString(std::string* value, uint32_t* timeout_ms) {
238 uint32_t len = 0;
239 if (!RecvUint32(&len, timeout_ms)) {
240 return false;
241 }
242
243 if (len == 0) {
244 *value = "";
245 return true;
246 }
247
Elliott Hughesb005d902017-02-22 14:54:15 -0800248 // http://b/35166374: don't allow init to make arbitrarily large allocations.
249 if (len > 0xffff) {
250 LOG(ERROR) << "sys_prop: RecvString asked to read huge string: " << len;
251 errno = ENOMEM;
252 return false;
253 }
254
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000255 std::vector<char> chars(len);
256 if (!RecvChars(&chars[0], len, timeout_ms)) {
257 return false;
258 }
259
260 *value = std::string(&chars[0], len);
261 return true;
262 }
263
264 bool SendUint32(uint32_t value) {
265 int result = TEMP_FAILURE_RETRY(send(socket_, &value, sizeof(value), 0));
266 return result == sizeof(value);
267 }
268
269 int socket() {
270 return socket_;
271 }
272
273 const struct ucred& cred() {
274 return cred_;
275 }
276
277 private:
278 bool PollIn(uint32_t* timeout_ms) {
279 struct pollfd ufds[1];
280 ufds[0].fd = socket_;
281 ufds[0].events = POLLIN;
282 ufds[0].revents = 0;
283 while (*timeout_ms > 0) {
284 Timer timer;
285 int nr = poll(ufds, 1, *timeout_ms);
James Hawkins27c05222017-01-26 11:55:44 -0800286 uint64_t millis = timer.duration_ms();
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000287 *timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis;
288
289 if (nr > 0) {
290 return true;
291 }
292
293 if (nr == 0) {
294 // Timeout
295 break;
296 }
297
298 if (nr < 0 && errno != EINTR) {
299 PLOG(ERROR) << "sys_prop: error waiting for uid " << cred_.uid << " to send property message";
300 return false;
301 } else { // errno == EINTR
302 // Timer rounds milliseconds down in case of EINTR we want it to be rounded up
303 // to avoid slowing init down by causing EINTR with under millisecond timeout.
304 if (*timeout_ms > 0) {
305 --(*timeout_ms);
306 }
307 }
308 }
309
310 LOG(ERROR) << "sys_prop: timeout waiting for uid " << cred_.uid << " to send property message.";
311 return false;
312 }
313
314 bool RecvFully(void* data_ptr, size_t size, uint32_t* timeout_ms) {
315 size_t bytes_left = size;
316 char* data = static_cast<char*>(data_ptr);
317 while (*timeout_ms > 0 && bytes_left > 0) {
318 if (!PollIn(timeout_ms)) {
319 return false;
320 }
321
322 int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
323 if (result <= 0) {
324 return false;
325 }
326
327 bytes_left -= result;
328 data += result;
329 }
330
331 return bytes_left == 0;
332 }
333
334 int socket_;
335 struct ucred cred_;
336
337 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
338};
339
340static void handle_property_set(SocketConnection& socket,
341 const std::string& name,
342 const std::string& value,
343 bool legacy_protocol) {
344 const char* cmd_name = legacy_protocol ? "PROP_MSG_SETPROP" : "PROP_MSG_SETPROP2";
345 if (!is_legal_property_name(name)) {
346 LOG(ERROR) << "sys_prop(" << cmd_name << "): illegal property name \"" << name << "\"";
347 socket.SendUint32(PROP_ERROR_INVALID_NAME);
348 return;
349 }
350
351 struct ucred cr = socket.cred();
352 char* source_ctx = nullptr;
353 getpeercon(socket.socket(), &source_ctx);
354
355 if (android::base::StartsWith(name, "ctl.")) {
356 if (check_control_mac_perms(value.c_str(), source_ctx, &cr)) {
357 handle_control_message(name.c_str() + 4, value.c_str());
358 if (!legacy_protocol) {
359 socket.SendUint32(PROP_SUCCESS);
360 }
361 } else {
362 LOG(ERROR) << "sys_prop(" << cmd_name << "): Unable to " << (name.c_str() + 4)
363 << " service ctl [" << value << "]"
364 << " uid:" << cr.uid
365 << " gid:" << cr.gid
366 << " pid:" << cr.pid;
367 if (!legacy_protocol) {
368 socket.SendUint32(PROP_ERROR_HANDLE_CONTROL_MESSAGE);
369 }
370 }
371 } else {
372 if (check_mac_perms(name, source_ctx, &cr)) {
373 uint32_t result = property_set(name, value);
374 if (!legacy_protocol) {
375 socket.SendUint32(result);
376 }
377 } else {
378 LOG(ERROR) << "sys_prop(" << cmd_name << "): permission denied uid:" << cr.uid << " name:" << name;
379 if (!legacy_protocol) {
380 socket.SendUint32(PROP_ERROR_PERMISSION_DENIED);
381 }
382 }
383 }
384
385 freecon(source_ctx);
386}
387
388static void handle_property_set_fd() {
389 static constexpr uint32_t kDefaultSocketTimeout = 2000; /* ms */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390
Robert Sesekca2da602017-01-25 08:08:51 -0500391 int s = accept4(property_set_fd, nullptr, nullptr, SOCK_CLOEXEC);
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700392 if (s == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 return;
394 }
395
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700396 struct ucred cr;
397 socklen_t cr_size = sizeof(cr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
399 close(s);
Elliott Hughesb005d902017-02-22 14:54:15 -0800400 PLOG(ERROR) << "sys_prop: unable to get SO_PEERCRED";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 return;
402 }
403
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000404 SocketConnection socket(s, cr);
405 uint32_t timeout_ms = kDefaultSocketTimeout;
406
407 uint32_t cmd = 0;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000408 if (!socket.RecvUint32(&cmd, &timeout_ms)) {
409 PLOG(ERROR) << "sys_prop: error while reading command from the socket";
410 socket.SendUint32(PROP_ERROR_READ_CMD);
JP Abgrall4515d812014-01-31 14:37:07 -0800411 return;
412 }
413
Elliott Hughesb005d902017-02-22 14:54:15 -0800414 switch (cmd) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000415 case PROP_MSG_SETPROP: {
416 char prop_name[PROP_NAME_MAX];
417 char prop_value[PROP_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000419 if (!socket.RecvChars(prop_name, PROP_NAME_MAX, &timeout_ms) ||
420 !socket.RecvChars(prop_value, PROP_VALUE_MAX, &timeout_ms)) {
421 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP): error while reading name/value from the socket";
422 return;
Nick Kralevich69463612013-09-13 17:21:28 -0700423 }
424
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000425 prop_name[PROP_NAME_MAX-1] = 0;
426 prop_value[PROP_VALUE_MAX-1] = 0;
rpcraig63207cd2012-08-09 10:05:49 -0400427
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000428 handle_property_set(socket, prop_value, prop_value, true);
Dimitry Ivanovdee4bd22017-01-19 14:53:00 -0800429 break;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000430 }
Dimitry Ivanov70c4ecf2017-01-24 18:38:09 +0000431
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000432 case PROP_MSG_SETPROP2: {
433 std::string name;
434 std::string value;
435 if (!socket.RecvString(&name, &timeout_ms) ||
436 !socket.RecvString(&value, &timeout_ms)) {
437 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP2): error while reading name/value from the socket";
438 socket.SendUint32(PROP_ERROR_READ_DATA);
439 return;
440 }
441
442 handle_property_set(socket, name, value, false);
443 break;
444 }
Elliott Hughesb005d902017-02-22 14:54:15 -0800445
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446 default:
Elliott Hughesb005d902017-02-22 14:54:15 -0800447 LOG(ERROR) << "sys_prop: invalid command " << cmd;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000448 socket.SendUint32(PROP_ERROR_INVALID_CMD);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 break;
450 }
451}
452
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800453static void load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800455/*
456 * Filter is used to decide which properties to load: NULL loads all keys,
457 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
458 */
459static void load_properties(char *data, const char *filter)
460{
461 char *key, *value, *eol, *sol, *tmp, *fn;
462 size_t flen = 0;
463
464 if (filter) {
465 flen = strlen(filter);
466 }
467
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800469 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 key = sol;
471 *eol++ = 0;
472 sol = eol;
473
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800474 while (isspace(*key)) key++;
475 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800478 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800480 if (!strncmp(key, "import ", 7) && flen == 0) {
481 fn = key + 7;
482 while (isspace(*fn)) fn++;
483
484 key = strchr(fn, ' ');
485 if (key) {
486 *key++ = 0;
487 while (isspace(*key)) key++;
488 }
489
490 load_properties_from_file(fn, key);
491
492 } else {
493 value = strchr(key, '=');
494 if (!value) continue;
495 *value++ = 0;
496
497 tmp = value - 2;
498 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
499
500 while (isspace(*value)) value++;
501
502 if (flen > 0) {
503 if (filter[flen - 1] == '*') {
504 if (strncmp(key, filter, flen - 1)) continue;
505 } else {
506 if (strcmp(key, filter)) continue;
507 }
508 }
509
510 property_set(key, value);
511 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 }
513}
514
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700515// Filter is used to decide which properties to load: NULL loads all keys,
516// "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
Elliott Hughesda40c002015-03-27 23:20:44 -0700517static void load_properties_from_file(const char* filename, const char* filter) {
518 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800519 std::string data;
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700520 if (!read_file(filename, &data)) {
521 PLOG(WARNING) << "Couldn't load properties from " << filename;
522 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 }
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700524 data.push_back('\n');
525 load_properties(&data[0], filter);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000526 LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t << ".)";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527}
528
Elliott Hughes81399e12015-03-10 08:39:45 -0700529static void load_persistent_properties() {
530 persistent_properties_loaded = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531
Elliott Hughes81399e12015-03-10 08:39:45 -0700532 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
533 if (!dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700534 PLOG(ERROR) << "Unable to open persistent property directory \""
535 << PERSISTENT_PROPERTY_DIR << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700536 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800538
Elliott Hughes81399e12015-03-10 08:39:45 -0700539 struct dirent* entry;
540 while ((entry = readdir(dir.get())) != NULL) {
541 if (strncmp("persist.", entry->d_name, strlen("persist."))) {
542 continue;
543 }
544 if (entry->d_type != DT_REG) {
545 continue;
546 }
547
548 // Open the file and read the property value.
549 int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
550 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700551 PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700552 continue;
553 }
554
555 struct stat sb;
556 if (fstat(fd, &sb) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700557 PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
Elliott Hughes81399e12015-03-10 08:39:45 -0700558 close(fd);
559 continue;
560 }
561
562 // File must not be accessible to others, be owned by root/root, and
563 // not be a hard link to any other file.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700564 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 || sb.st_nlink != 1) {
565 PLOG(ERROR) << "skipping insecure property file " << entry->d_name
566 << " (uid=" << sb.st_uid << " gid=" << sb.st_gid
567 << " nlink=" << sb.st_nlink << " mode=" << std::oct << sb.st_mode << ")";
Elliott Hughes81399e12015-03-10 08:39:45 -0700568 close(fd);
569 continue;
570 }
571
572 char value[PROP_VALUE_MAX];
573 int length = read(fd, value, sizeof(value) - 1);
574 if (length >= 0) {
575 value[length] = 0;
576 property_set(entry->d_name, value);
577 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700578 PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
Elliott Hughes81399e12015-03-10 08:39:45 -0700579 }
580 close(fd);
581 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800582}
583
Elliott Hughesda40c002015-03-27 23:20:44 -0700584void property_load_boot_defaults() {
Elliott Hughes795798d2017-01-27 16:21:55 -0800585 load_properties_from_file("/default.prop", NULL);
586 load_properties_from_file("/odm/default.prop", NULL);
587 load_properties_from_file("/vendor/default.prop", NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800588}
589
Nick Kralevich32b90232012-09-19 11:15:24 -0700590static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800591 if (ALLOW_LOCAL_PROP_OVERRIDE) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700592 std::string debuggable = property_get("ro.debuggable");
593 if (debuggable == "1") {
Elliott Hughes795798d2017-01-27 16:21:55 -0800594 load_properties_from_file("/data/local.prop", NULL);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800595 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700596 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700597}
598
Ken Sumrallc5c51032011-03-08 17:01:29 -0800599/* When booting an encrypted system, /data is not mounted when the
600 * property service is started, so any properties stored there are
601 * not loaded. Vold triggers init to load these properties once it
602 * has mounted /data.
603 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700604void load_persist_props(void) {
Nick Kralevich32b90232012-09-19 11:15:24 -0700605 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800606 /* Read persistent properties after all default values have been loaded. */
607 load_persistent_properties();
608}
609
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700610void load_recovery_id_prop() {
Yabin Cui74edcea2015-07-24 10:11:05 -0700611 std::string ro_hardware = property_get("ro.hardware");
612 if (ro_hardware.empty()) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700613 LOG(ERROR) << "ro.hardware not set - unable to load recovery id";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700614 return;
615 }
Yabin Cui74edcea2015-07-24 10:11:05 -0700616 std::string fstab_filename = FSTAB_PREFIX + ro_hardware;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700617
Yabin Cui74edcea2015-07-24 10:11:05 -0700618 std::unique_ptr<fstab, void(*)(fstab*)> tab(fs_mgr_read_fstab(fstab_filename.c_str()),
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700619 fs_mgr_free_fstab);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700620 if (!tab) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700621 PLOG(ERROR) << "unable to read fstab " << fstab_filename;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700622 return;
623 }
624
625 fstab_rec* rec = fs_mgr_get_entry_for_mount_point(tab.get(), RECOVERY_MOUNT_POINT);
626 if (rec == NULL) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700627 LOG(ERROR) << "/recovery not specified in fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700628 return;
629 }
630
631 int fd = open(rec->blk_device, O_RDONLY);
632 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700633 PLOG(ERROR) << "error opening block device " << rec->blk_device;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700634 return;
635 }
636
637 boot_img_hdr hdr;
638 if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
639 std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
640 property_set("ro.recovery_id", hex.c_str());
641 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700642 PLOG(ERROR) << "error reading /recovery";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700643 }
644
645 close(fd);
646}
647
Paul Lawrence948410a2015-07-01 14:40:56 -0700648void load_system_props() {
Elliott Hughes795798d2017-01-27 16:21:55 -0800649 load_properties_from_file("/system/build.prop", NULL);
650 load_properties_from_file("/odm/build.prop", NULL);
651 load_properties_from_file("/vendor/build.prop", NULL);
652 load_properties_from_file("/factory/factory.prop", "ro.*");
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700653 load_recovery_id_prop();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700654}
655
Elliott Hughesda40c002015-03-27 23:20:44 -0700656void start_property_service() {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000657 property_set("ro.property_service.version", "2");
658
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700659 property_set_fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
660 0666, 0, 0, NULL);
661 if (property_set_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700662 PLOG(ERROR) << "start_property_service socket creation failed";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700663 exit(1);
664 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700666 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700667
Elliott Hughes929f4072015-04-24 21:13:44 -0700668 register_epoll_handler(property_set_fd, handle_property_set_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800669}