blob: bbe353f4d823287e63ae38d976fd247098899879 [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
Elliott Hughes4f713192015-12-04 22:00:26 -080047#include <android-base/file.h>
Jaekyun Seok03bcaec2017-04-24 18:52:54 +090048#include <android-base/properties.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>
Jaekyun Seok03bcaec2017-04-24 18:52:54 +090051#include <fs_mgr.h>
Andres Moralesdb5f5d42015-05-08 08:30:33 -070052#include "bootimg.h"
53
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054#include "property_service.h"
55#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070056#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070057#include "log.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Keun-young Park7d320262017-02-17 17:48:56 -080059using android::base::StringPrintf;
60
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061#define PERSISTENT_PROPERTY_DIR "/data/property"
Andres Moralesdb5f5d42015-05-08 08:30:33 -070062#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
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800118static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119{
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700120 char tempPath[PATH_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 char path[PATH_MAX];
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700122 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700124 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
125 fd = mkstemp(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 if (fd < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700127 PLOG(ERROR) << "Unable to write persistent property to temp file " << tempPath;
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800128 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 }
130 write(fd, value, strlen(value));
OPPOde73a0c2014-03-28 19:12:47 +0800131 fsync(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 close(fd);
133
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700134 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 if (rename(tempPath, path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700136 PLOG(ERROR) << "Unable to rename persistent property file " << tempPath << " to " << path;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 unlink(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 }
139}
140
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000141bool is_legal_property_name(const std::string& name) {
Iliyan Malchevf6554802016-09-19 20:58:20 -0700142 size_t namelen = name.size();
143
Nick Kralevich69463612013-09-13 17:21:28 -0700144 if (namelen < 1) return false;
145 if (name[0] == '.') return false;
146 if (name[namelen - 1] == '.') return false;
147
Iliyan Malchevf6554802016-09-19 20:58:20 -0700148 /* Only allow alphanumeric, plus '.', '-', '@', or '_' */
Nick Kralevich69463612013-09-13 17:21:28 -0700149 /* Don't allow ".." to appear in a property name */
Iliyan Malchevf6554802016-09-19 20:58:20 -0700150 for (size_t i = 0; i < namelen; i++) {
Nick Kralevich69463612013-09-13 17:21:28 -0700151 if (name[i] == '.') {
Nick Kralevichc2c5a242013-09-16 11:30:48 -0700152 // i=0 is guaranteed to never have a dot. See above.
153 if (name[i-1] == '.') return false;
Nick Kralevich69463612013-09-13 17:21:28 -0700154 continue;
155 }
Iliyan Malchevf6554802016-09-19 20:58:20 -0700156 if (name[i] == '_' || name[i] == '-' || name[i] == '@') continue;
Nick Kralevich69463612013-09-13 17:21:28 -0700157 if (name[i] >= 'a' && name[i] <= 'z') continue;
158 if (name[i] >= 'A' && name[i] <= 'Z') continue;
159 if (name[i] >= '0' && name[i] <= '9') continue;
160 return false;
161 }
162
163 return true;
164}
165
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000166uint32_t property_set(const std::string& name, const std::string& value) {
167 size_t valuelen = value.size();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000169 if (!is_legal_property_name(name)) {
170 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: bad name";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000171 return PROP_ERROR_INVALID_NAME;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000172 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000173
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000174 if (valuelen >= PROP_VALUE_MAX) {
175 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
176 << "value too long";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000177 return PROP_ERROR_INVALID_VALUE;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000178 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000180 if (name == "selinux.restorecon_recursive" && valuelen > 0) {
181 if (restorecon(value.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700182 LOG(ERROR) << "Failed to restorecon_recursive " << value;
Jeff Sharkey76417512015-06-09 11:02:55 -0700183 }
184 }
185
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000186 prop_info* pi = (prop_info*) __system_property_find(name.c_str());
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000187 if (pi != nullptr) {
188 // ro.* properties are actually "write-once".
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000189 if (android::base::StartsWith(name, "ro.")) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000190 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
191 << "property already set";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000192 return PROP_ERROR_READ_ONLY_PROPERTY;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000193 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000195 __system_property_update(pi, value.c_str(), valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 } else {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000197 int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700198 if (rc < 0) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000199 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
200 << "__system_property_add failed";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000201 return PROP_ERROR_SET_FAILED;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200202 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000204
Elliott Hughes4f915812016-12-05 13:12:48 -0800205 // Don't write properties to disk until after we have read all default
206 // properties to prevent them from being overwritten by default values.
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000207 if (persistent_properties_loaded && android::base::StartsWith(name, "persist.")) {
208 write_persistent_property(name.c_str(), value.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 }
Tom Cherry5ceb7b32017-04-17 16:34:20 -0700210 property_changed(name, value);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000211 return PROP_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212}
213
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000214class SocketConnection {
215 public:
216 SocketConnection(int socket, const struct ucred& cred)
217 : socket_(socket), cred_(cred) {}
218
219 ~SocketConnection() {
220 close(socket_);
221 }
222
223 bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
224 return RecvFully(value, sizeof(*value), timeout_ms);
225 }
226
227 bool RecvChars(char* chars, size_t size, uint32_t* timeout_ms) {
228 return RecvFully(chars, size, timeout_ms);
229 }
230
231 bool RecvString(std::string* value, uint32_t* timeout_ms) {
232 uint32_t len = 0;
233 if (!RecvUint32(&len, timeout_ms)) {
234 return false;
235 }
236
237 if (len == 0) {
238 *value = "";
239 return true;
240 }
241
Elliott Hughesb005d902017-02-22 14:54:15 -0800242 // http://b/35166374: don't allow init to make arbitrarily large allocations.
243 if (len > 0xffff) {
244 LOG(ERROR) << "sys_prop: RecvString asked to read huge string: " << len;
245 errno = ENOMEM;
246 return false;
247 }
248
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000249 std::vector<char> chars(len);
250 if (!RecvChars(&chars[0], len, timeout_ms)) {
251 return false;
252 }
253
254 *value = std::string(&chars[0], len);
255 return true;
256 }
257
258 bool SendUint32(uint32_t value) {
259 int result = TEMP_FAILURE_RETRY(send(socket_, &value, sizeof(value), 0));
260 return result == sizeof(value);
261 }
262
263 int socket() {
264 return socket_;
265 }
266
267 const struct ucred& cred() {
268 return cred_;
269 }
270
271 private:
272 bool PollIn(uint32_t* timeout_ms) {
273 struct pollfd ufds[1];
274 ufds[0].fd = socket_;
275 ufds[0].events = POLLIN;
276 ufds[0].revents = 0;
277 while (*timeout_ms > 0) {
278 Timer timer;
279 int nr = poll(ufds, 1, *timeout_ms);
James Hawkins27c05222017-01-26 11:55:44 -0800280 uint64_t millis = timer.duration_ms();
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000281 *timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis;
282
283 if (nr > 0) {
284 return true;
285 }
286
287 if (nr == 0) {
288 // Timeout
289 break;
290 }
291
292 if (nr < 0 && errno != EINTR) {
293 PLOG(ERROR) << "sys_prop: error waiting for uid " << cred_.uid << " to send property message";
294 return false;
295 } else { // errno == EINTR
296 // Timer rounds milliseconds down in case of EINTR we want it to be rounded up
297 // to avoid slowing init down by causing EINTR with under millisecond timeout.
298 if (*timeout_ms > 0) {
299 --(*timeout_ms);
300 }
301 }
302 }
303
304 LOG(ERROR) << "sys_prop: timeout waiting for uid " << cred_.uid << " to send property message.";
305 return false;
306 }
307
308 bool RecvFully(void* data_ptr, size_t size, uint32_t* timeout_ms) {
309 size_t bytes_left = size;
310 char* data = static_cast<char*>(data_ptr);
311 while (*timeout_ms > 0 && bytes_left > 0) {
312 if (!PollIn(timeout_ms)) {
313 return false;
314 }
315
316 int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
317 if (result <= 0) {
318 return false;
319 }
320
321 bytes_left -= result;
322 data += result;
323 }
324
325 return bytes_left == 0;
326 }
327
328 int socket_;
329 struct ucred cred_;
330
331 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
332};
333
334static void handle_property_set(SocketConnection& socket,
335 const std::string& name,
336 const std::string& value,
337 bool legacy_protocol) {
338 const char* cmd_name = legacy_protocol ? "PROP_MSG_SETPROP" : "PROP_MSG_SETPROP2";
339 if (!is_legal_property_name(name)) {
340 LOG(ERROR) << "sys_prop(" << cmd_name << "): illegal property name \"" << name << "\"";
341 socket.SendUint32(PROP_ERROR_INVALID_NAME);
342 return;
343 }
344
345 struct ucred cr = socket.cred();
346 char* source_ctx = nullptr;
347 getpeercon(socket.socket(), &source_ctx);
348
349 if (android::base::StartsWith(name, "ctl.")) {
350 if (check_control_mac_perms(value.c_str(), source_ctx, &cr)) {
351 handle_control_message(name.c_str() + 4, value.c_str());
352 if (!legacy_protocol) {
353 socket.SendUint32(PROP_SUCCESS);
354 }
355 } else {
356 LOG(ERROR) << "sys_prop(" << cmd_name << "): Unable to " << (name.c_str() + 4)
357 << " service ctl [" << value << "]"
358 << " uid:" << cr.uid
359 << " gid:" << cr.gid
360 << " pid:" << cr.pid;
361 if (!legacy_protocol) {
362 socket.SendUint32(PROP_ERROR_HANDLE_CONTROL_MESSAGE);
363 }
364 }
365 } else {
366 if (check_mac_perms(name, source_ctx, &cr)) {
367 uint32_t result = property_set(name, value);
368 if (!legacy_protocol) {
369 socket.SendUint32(result);
370 }
371 } else {
372 LOG(ERROR) << "sys_prop(" << cmd_name << "): permission denied uid:" << cr.uid << " name:" << name;
373 if (!legacy_protocol) {
374 socket.SendUint32(PROP_ERROR_PERMISSION_DENIED);
375 }
376 }
377 }
378
379 freecon(source_ctx);
380}
381
382static void handle_property_set_fd() {
383 static constexpr uint32_t kDefaultSocketTimeout = 2000; /* ms */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384
Robert Sesekca2da602017-01-25 08:08:51 -0500385 int s = accept4(property_set_fd, nullptr, nullptr, SOCK_CLOEXEC);
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700386 if (s == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 return;
388 }
389
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700390 struct ucred cr;
391 socklen_t cr_size = sizeof(cr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
393 close(s);
Elliott Hughesb005d902017-02-22 14:54:15 -0800394 PLOG(ERROR) << "sys_prop: unable to get SO_PEERCRED";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 return;
396 }
397
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000398 SocketConnection socket(s, cr);
399 uint32_t timeout_ms = kDefaultSocketTimeout;
400
401 uint32_t cmd = 0;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000402 if (!socket.RecvUint32(&cmd, &timeout_ms)) {
403 PLOG(ERROR) << "sys_prop: error while reading command from the socket";
404 socket.SendUint32(PROP_ERROR_READ_CMD);
JP Abgrall4515d812014-01-31 14:37:07 -0800405 return;
406 }
407
Elliott Hughesb005d902017-02-22 14:54:15 -0800408 switch (cmd) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000409 case PROP_MSG_SETPROP: {
410 char prop_name[PROP_NAME_MAX];
411 char prop_value[PROP_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000413 if (!socket.RecvChars(prop_name, PROP_NAME_MAX, &timeout_ms) ||
414 !socket.RecvChars(prop_value, PROP_VALUE_MAX, &timeout_ms)) {
415 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP): error while reading name/value from the socket";
416 return;
Nick Kralevich69463612013-09-13 17:21:28 -0700417 }
418
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000419 prop_name[PROP_NAME_MAX-1] = 0;
420 prop_value[PROP_VALUE_MAX-1] = 0;
rpcraig63207cd2012-08-09 10:05:49 -0400421
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000422 handle_property_set(socket, prop_value, prop_value, true);
Dimitry Ivanovdee4bd22017-01-19 14:53:00 -0800423 break;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000424 }
Dimitry Ivanov70c4ecf2017-01-24 18:38:09 +0000425
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000426 case PROP_MSG_SETPROP2: {
427 std::string name;
428 std::string value;
429 if (!socket.RecvString(&name, &timeout_ms) ||
430 !socket.RecvString(&value, &timeout_ms)) {
431 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP2): error while reading name/value from the socket";
432 socket.SendUint32(PROP_ERROR_READ_DATA);
433 return;
434 }
435
436 handle_property_set(socket, name, value, false);
437 break;
438 }
Elliott Hughesb005d902017-02-22 14:54:15 -0800439
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 default:
Elliott Hughesb005d902017-02-22 14:54:15 -0800441 LOG(ERROR) << "sys_prop: invalid command " << cmd;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000442 socket.SendUint32(PROP_ERROR_INVALID_CMD);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443 break;
444 }
445}
446
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800447static void load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800449/*
450 * Filter is used to decide which properties to load: NULL loads all keys,
451 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
452 */
453static void load_properties(char *data, const char *filter)
454{
455 char *key, *value, *eol, *sol, *tmp, *fn;
456 size_t flen = 0;
457
458 if (filter) {
459 flen = strlen(filter);
460 }
461
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800463 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 key = sol;
465 *eol++ = 0;
466 sol = eol;
467
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800468 while (isspace(*key)) key++;
469 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800472 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800474 if (!strncmp(key, "import ", 7) && flen == 0) {
475 fn = key + 7;
476 while (isspace(*fn)) fn++;
477
478 key = strchr(fn, ' ');
479 if (key) {
480 *key++ = 0;
481 while (isspace(*key)) key++;
482 }
483
484 load_properties_from_file(fn, key);
485
486 } else {
487 value = strchr(key, '=');
488 if (!value) continue;
489 *value++ = 0;
490
491 tmp = value - 2;
492 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
493
494 while (isspace(*value)) value++;
495
496 if (flen > 0) {
497 if (filter[flen - 1] == '*') {
498 if (strncmp(key, filter, flen - 1)) continue;
499 } else {
500 if (strcmp(key, filter)) continue;
501 }
502 }
503
504 property_set(key, value);
505 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506 }
507}
508
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700509// Filter is used to decide which properties to load: NULL loads all keys,
510// "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
Elliott Hughesda40c002015-03-27 23:20:44 -0700511static void load_properties_from_file(const char* filename, const char* filter) {
512 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800513 std::string data;
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700514 if (!read_file(filename, &data)) {
515 PLOG(WARNING) << "Couldn't load properties from " << filename;
516 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 }
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700518 data.push_back('\n');
519 load_properties(&data[0], filter);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000520 LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t << ".)";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521}
522
Elliott Hughes81399e12015-03-10 08:39:45 -0700523static void load_persistent_properties() {
524 persistent_properties_loaded = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525
Elliott Hughes81399e12015-03-10 08:39:45 -0700526 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
527 if (!dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700528 PLOG(ERROR) << "Unable to open persistent property directory \""
529 << PERSISTENT_PROPERTY_DIR << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700530 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800532
Elliott Hughes81399e12015-03-10 08:39:45 -0700533 struct dirent* entry;
534 while ((entry = readdir(dir.get())) != NULL) {
535 if (strncmp("persist.", entry->d_name, strlen("persist."))) {
536 continue;
537 }
538 if (entry->d_type != DT_REG) {
539 continue;
540 }
541
542 // Open the file and read the property value.
543 int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
544 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700545 PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700546 continue;
547 }
548
549 struct stat sb;
550 if (fstat(fd, &sb) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700551 PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
Elliott Hughes81399e12015-03-10 08:39:45 -0700552 close(fd);
553 continue;
554 }
555
556 // File must not be accessible to others, be owned by root/root, and
557 // not be a hard link to any other file.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700558 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 || sb.st_nlink != 1) {
559 PLOG(ERROR) << "skipping insecure property file " << entry->d_name
560 << " (uid=" << sb.st_uid << " gid=" << sb.st_gid
561 << " nlink=" << sb.st_nlink << " mode=" << std::oct << sb.st_mode << ")";
Elliott Hughes81399e12015-03-10 08:39:45 -0700562 close(fd);
563 continue;
564 }
565
566 char value[PROP_VALUE_MAX];
567 int length = read(fd, value, sizeof(value) - 1);
568 if (length >= 0) {
569 value[length] = 0;
570 property_set(entry->d_name, value);
571 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700572 PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
Elliott Hughes81399e12015-03-10 08:39:45 -0700573 }
574 close(fd);
575 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800576}
577
Jaekyun Seok03bcaec2017-04-24 18:52:54 +0900578// persist.sys.usb.config values can't be combined on build-time when property
579// files are split into each partition.
580// So we need to apply the same rule of build/make/tools/post_process_props.py
581// on runtime.
582static void update_sys_usb_config() {
583 bool is_debuggable = android::base::GetBoolProperty("ro.debuggable", false);
584 std::string config = android::base::GetProperty("persist.sys.usb.config", "");
585 if (config.empty()) {
586 property_set("persist.sys.usb.config", is_debuggable ? "adb" : "none");
587 } else if (is_debuggable && config.find("adb") == std::string::npos &&
588 config.length() + 4 < PROP_VALUE_MAX) {
589 config.append(",adb");
590 property_set("persist.sys.usb.config", config);
591 }
592}
593
Elliott Hughesda40c002015-03-27 23:20:44 -0700594void property_load_boot_defaults() {
Elliott Hughes795798d2017-01-27 16:21:55 -0800595 load_properties_from_file("/default.prop", NULL);
596 load_properties_from_file("/odm/default.prop", NULL);
597 load_properties_from_file("/vendor/default.prop", NULL);
Jaekyun Seok03bcaec2017-04-24 18:52:54 +0900598
599 update_sys_usb_config();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600}
601
Nick Kralevich32b90232012-09-19 11:15:24 -0700602static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800603 if (ALLOW_LOCAL_PROP_OVERRIDE) {
Tom Cherrye2490972017-03-28 16:40:41 -0700604 load_properties_from_file("/data/local.prop", NULL);
Nick Kralevich32b90232012-09-19 11:15:24 -0700605 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700606}
607
Ken Sumrallc5c51032011-03-08 17:01:29 -0800608/* When booting an encrypted system, /data is not mounted when the
609 * property service is started, so any properties stored there are
610 * not loaded. Vold triggers init to load these properties once it
611 * has mounted /data.
612 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700613void load_persist_props(void) {
Nick Kralevich32b90232012-09-19 11:15:24 -0700614 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800615 /* Read persistent properties after all default values have been loaded. */
616 load_persistent_properties();
Keun-young Park404906d2017-02-28 19:20:38 -0800617 property_set("ro.persistent_properties.ready", "true");
Ken Sumrallc5c51032011-03-08 17:01:29 -0800618}
619
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700620void load_recovery_id_prop() {
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800621 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
622 fs_mgr_free_fstab);
623 if (!fstab) {
624 PLOG(ERROR) << "unable to read default fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700625 return;
626 }
627
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800628 fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab.get(), RECOVERY_MOUNT_POINT);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700629 if (rec == NULL) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700630 LOG(ERROR) << "/recovery not specified in fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700631 return;
632 }
633
634 int fd = open(rec->blk_device, O_RDONLY);
635 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700636 PLOG(ERROR) << "error opening block device " << rec->blk_device;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700637 return;
638 }
639
640 boot_img_hdr hdr;
641 if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
642 std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
643 property_set("ro.recovery_id", hex.c_str());
644 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700645 PLOG(ERROR) << "error reading /recovery";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700646 }
647
648 close(fd);
649}
650
Paul Lawrence948410a2015-07-01 14:40:56 -0700651void load_system_props() {
Elliott Hughes795798d2017-01-27 16:21:55 -0800652 load_properties_from_file("/system/build.prop", NULL);
653 load_properties_from_file("/odm/build.prop", NULL);
654 load_properties_from_file("/vendor/build.prop", NULL);
655 load_properties_from_file("/factory/factory.prop", "ro.*");
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700656 load_recovery_id_prop();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700657}
658
Elliott Hughesda40c002015-03-27 23:20:44 -0700659void start_property_service() {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000660 property_set("ro.property_service.version", "2");
661
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700662 property_set_fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
663 0666, 0, 0, NULL);
664 if (property_set_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700665 PLOG(ERROR) << "start_property_service socket creation failed";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700666 exit(1);
667 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700669 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700670
Elliott Hughes929f4072015-04-24 21:13:44 -0700671 register_epoll_handler(property_set_fd, handle_property_set_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800672}