blob: fd14bd66f651dab65e2543224514b847493d1d69 [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
Tom Cherry3f5eaae52017-04-06 16:30:22 -070017#include "property_service.h"
18
19#include <ctype.h>
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
Keun-young Park7d320262017-02-17 17:48:56 -080023#include <inttypes.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070024#include <limits.h>
25#include <netinet/in.h>
26#include <stdarg.h>
Tom Cherryf57c0bf2017-04-07 13:46:21 -070027#include <stddef.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#include <stdio.h>
29#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <string.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070031#include <sys/mman.h>
JP Abgrall4515d812014-01-31 14:37:07 -080032#include <sys/poll.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070033#include <sys/select.h>
34#include <sys/types.h>
35#include <sys/un.h>
36#include <unistd.h>
Elliott Hughes81399e12015-03-10 08:39:45 -070037
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
39#include <sys/_system_properties.h>
40
Tom Cherry3f5eaae52017-04-06 16:30:22 -070041#include <memory>
Tom Marshall62696902017-06-09 18:29:23 +000042#include <queue>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070043#include <vector>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044
Tom Cherryede0d532017-07-06 14:20:11 -070045#include <android-base/chrono_utils.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080046#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070047#include <android-base/logging.h>
Jaekyun Seok0cf3a072017-04-24 18:52:54 +090048#include <android-base/properties.h>
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000049#include <android-base/strings.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070050#include <bootimg.h>
51#include <fs_mgr.h>
52#include <selinux/android.h>
53#include <selinux/label.h>
54#include <selinux/selinux.h>
Andres Moralesdb5f5d42015-05-08 08:30:33 -070055
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070057#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Tom Cherryede0d532017-07-06 14:20:11 -070059using android::base::Timer;
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
Tom Cherry81f5d3e2017-06-22 12:53:17 -070064namespace android {
65namespace init {
66
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067static 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
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800121static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122{
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700123 char tempPath[PATH_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 char path[PATH_MAX];
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700125 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700127 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
128 fd = mkstemp(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 if (fd < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700130 PLOG(ERROR) << "Unable to write persistent property to temp file " << tempPath;
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800131 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 }
133 write(fd, value, strlen(value));
OPPOde73a0c2014-03-28 19:12:47 +0800134 fsync(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 close(fd);
136
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700137 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 if (rename(tempPath, path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700139 PLOG(ERROR) << "Unable to rename persistent property file " << tempPath << " to " << path;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 unlink(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 }
142}
143
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000144bool is_legal_property_name(const std::string& name) {
Iliyan Malchevf6554802016-09-19 20:58:20 -0700145 size_t namelen = name.size();
146
Nick Kralevich69463612013-09-13 17:21:28 -0700147 if (namelen < 1) return false;
148 if (name[0] == '.') return false;
149 if (name[namelen - 1] == '.') return false;
150
Tom Cherry13693792017-05-30 13:45:28 -0700151 /* Only allow alphanumeric, plus '.', '-', '@', ':', or '_' */
Nick Kralevich69463612013-09-13 17:21:28 -0700152 /* Don't allow ".." to appear in a property name */
Iliyan Malchevf6554802016-09-19 20:58:20 -0700153 for (size_t i = 0; i < namelen; i++) {
Nick Kralevich69463612013-09-13 17:21:28 -0700154 if (name[i] == '.') {
Nick Kralevichc2c5a242013-09-16 11:30:48 -0700155 // i=0 is guaranteed to never have a dot. See above.
156 if (name[i-1] == '.') return false;
Nick Kralevich69463612013-09-13 17:21:28 -0700157 continue;
158 }
Tom Cherry13693792017-05-30 13:45:28 -0700159 if (name[i] == '_' || name[i] == '-' || name[i] == '@' || name[i] == ':') continue;
Nick Kralevich69463612013-09-13 17:21:28 -0700160 if (name[i] >= 'a' && name[i] <= 'z') continue;
161 if (name[i] >= 'A' && name[i] <= 'Z') continue;
162 if (name[i] >= '0' && name[i] <= '9') continue;
163 return false;
164 }
165
166 return true;
167}
168
Tom Marshall62696902017-06-09 18:29:23 +0000169static uint32_t PropertySetImpl(const std::string& name, const std::string& value) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000170 size_t valuelen = value.size();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000172 if (!is_legal_property_name(name)) {
173 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: bad name";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000174 return PROP_ERROR_INVALID_NAME;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000175 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000176
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000177 if (valuelen >= PROP_VALUE_MAX) {
178 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
179 << "value too long";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000180 return PROP_ERROR_INVALID_VALUE;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000181 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000183 prop_info* pi = (prop_info*) __system_property_find(name.c_str());
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000184 if (pi != nullptr) {
185 // ro.* properties are actually "write-once".
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000186 if (android::base::StartsWith(name, "ro.")) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000187 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
188 << "property already set";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000189 return PROP_ERROR_READ_ONLY_PROPERTY;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000190 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000192 __system_property_update(pi, value.c_str(), valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 } else {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000194 int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700195 if (rc < 0) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000196 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
197 << "__system_property_add failed";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000198 return PROP_ERROR_SET_FAILED;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200199 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000201
Elliott Hughes4f915812016-12-05 13:12:48 -0800202 // Don't write properties to disk until after we have read all default
203 // properties to prevent them from being overwritten by default values.
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000204 if (persistent_properties_loaded && android::base::StartsWith(name, "persist.")) {
205 write_persistent_property(name.c_str(), value.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700207 property_changed(name, value);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000208 return PROP_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209}
210
Tom Marshall62696902017-06-09 18:29:23 +0000211typedef int (*PropertyAsyncFunc)(const std::string&, const std::string&);
212
213struct PropertyChildInfo {
214 pid_t pid;
215 PropertyAsyncFunc func;
216 std::string name;
217 std::string value;
218};
219
220static std::queue<PropertyChildInfo> property_children;
221
222static void PropertyChildLaunch() {
223 auto& info = property_children.front();
224 pid_t pid = fork();
225 if (pid < 0) {
226 LOG(ERROR) << "Failed to fork for property_set_async";
227 while (!property_children.empty()) {
228 property_children.pop();
229 }
230 return;
231 }
232 if (pid != 0) {
233 info.pid = pid;
234 } else {
235 if (info.func(info.name, info.value) != 0) {
236 LOG(ERROR) << "property_set_async(\"" << info.name << "\", \"" << info.value
237 << "\") failed";
238 }
239 exit(0);
240 }
241}
242
243bool PropertyChildReap(pid_t pid) {
244 if (property_children.empty()) {
245 return false;
246 }
247 auto& info = property_children.front();
248 if (info.pid != pid) {
249 return false;
250 }
251 if (PropertySetImpl(info.name, info.value) != PROP_SUCCESS) {
252 LOG(ERROR) << "Failed to set async property " << info.name;
253 }
254 property_children.pop();
255 if (!property_children.empty()) {
256 PropertyChildLaunch();
257 }
258 return true;
259}
260
261static uint32_t PropertySetAsync(const std::string& name, const std::string& value,
262 PropertyAsyncFunc func) {
263 if (value.empty()) {
264 return PropertySetImpl(name, value);
265 }
266
267 PropertyChildInfo info;
268 info.func = func;
269 info.name = name;
270 info.value = value;
271 property_children.push(info);
272 if (property_children.size() == 1) {
273 PropertyChildLaunch();
274 }
275 return PROP_SUCCESS;
276}
277
278static int RestoreconRecursiveAsync(const std::string& name, const std::string& value) {
279 return selinux_android_restorecon(value.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
280}
281
282uint32_t property_set(const std::string& name, const std::string& value) {
283 if (name == "selinux.restorecon_recursive") {
284 return PropertySetAsync(name, value, RestoreconRecursiveAsync);
285 }
286
287 return PropertySetImpl(name, value);
288}
289
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000290class SocketConnection {
291 public:
292 SocketConnection(int socket, const struct ucred& cred)
293 : socket_(socket), cred_(cred) {}
294
295 ~SocketConnection() {
296 close(socket_);
297 }
298
299 bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
300 return RecvFully(value, sizeof(*value), timeout_ms);
301 }
302
303 bool RecvChars(char* chars, size_t size, uint32_t* timeout_ms) {
304 return RecvFully(chars, size, timeout_ms);
305 }
306
307 bool RecvString(std::string* value, uint32_t* timeout_ms) {
308 uint32_t len = 0;
309 if (!RecvUint32(&len, timeout_ms)) {
310 return false;
311 }
312
313 if (len == 0) {
314 *value = "";
315 return true;
316 }
317
Elliott Hughesb005d902017-02-22 14:54:15 -0800318 // http://b/35166374: don't allow init to make arbitrarily large allocations.
319 if (len > 0xffff) {
320 LOG(ERROR) << "sys_prop: RecvString asked to read huge string: " << len;
321 errno = ENOMEM;
322 return false;
323 }
324
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000325 std::vector<char> chars(len);
326 if (!RecvChars(&chars[0], len, timeout_ms)) {
327 return false;
328 }
329
330 *value = std::string(&chars[0], len);
331 return true;
332 }
333
334 bool SendUint32(uint32_t value) {
335 int result = TEMP_FAILURE_RETRY(send(socket_, &value, sizeof(value), 0));
336 return result == sizeof(value);
337 }
338
339 int socket() {
340 return socket_;
341 }
342
343 const struct ucred& cred() {
344 return cred_;
345 }
346
347 private:
348 bool PollIn(uint32_t* timeout_ms) {
349 struct pollfd ufds[1];
350 ufds[0].fd = socket_;
351 ufds[0].events = POLLIN;
352 ufds[0].revents = 0;
353 while (*timeout_ms > 0) {
354 Timer timer;
355 int nr = poll(ufds, 1, *timeout_ms);
Tom Cherryede0d532017-07-06 14:20:11 -0700356 uint64_t millis = timer.duration().count();
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000357 *timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis;
358
359 if (nr > 0) {
360 return true;
361 }
362
363 if (nr == 0) {
364 // Timeout
365 break;
366 }
367
368 if (nr < 0 && errno != EINTR) {
369 PLOG(ERROR) << "sys_prop: error waiting for uid " << cred_.uid << " to send property message";
370 return false;
371 } else { // errno == EINTR
372 // Timer rounds milliseconds down in case of EINTR we want it to be rounded up
373 // to avoid slowing init down by causing EINTR with under millisecond timeout.
374 if (*timeout_ms > 0) {
375 --(*timeout_ms);
376 }
377 }
378 }
379
380 LOG(ERROR) << "sys_prop: timeout waiting for uid " << cred_.uid << " to send property message.";
381 return false;
382 }
383
384 bool RecvFully(void* data_ptr, size_t size, uint32_t* timeout_ms) {
385 size_t bytes_left = size;
386 char* data = static_cast<char*>(data_ptr);
387 while (*timeout_ms > 0 && bytes_left > 0) {
388 if (!PollIn(timeout_ms)) {
389 return false;
390 }
391
392 int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
393 if (result <= 0) {
394 return false;
395 }
396
397 bytes_left -= result;
398 data += result;
399 }
400
401 return bytes_left == 0;
402 }
403
404 int socket_;
405 struct ucred cred_;
406
407 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
408};
409
410static void handle_property_set(SocketConnection& socket,
411 const std::string& name,
412 const std::string& value,
413 bool legacy_protocol) {
414 const char* cmd_name = legacy_protocol ? "PROP_MSG_SETPROP" : "PROP_MSG_SETPROP2";
415 if (!is_legal_property_name(name)) {
416 LOG(ERROR) << "sys_prop(" << cmd_name << "): illegal property name \"" << name << "\"";
417 socket.SendUint32(PROP_ERROR_INVALID_NAME);
418 return;
419 }
420
421 struct ucred cr = socket.cred();
422 char* source_ctx = nullptr;
423 getpeercon(socket.socket(), &source_ctx);
424
425 if (android::base::StartsWith(name, "ctl.")) {
426 if (check_control_mac_perms(value.c_str(), source_ctx, &cr)) {
427 handle_control_message(name.c_str() + 4, value.c_str());
428 if (!legacy_protocol) {
429 socket.SendUint32(PROP_SUCCESS);
430 }
431 } else {
432 LOG(ERROR) << "sys_prop(" << cmd_name << "): Unable to " << (name.c_str() + 4)
433 << " service ctl [" << value << "]"
434 << " uid:" << cr.uid
435 << " gid:" << cr.gid
436 << " pid:" << cr.pid;
437 if (!legacy_protocol) {
438 socket.SendUint32(PROP_ERROR_HANDLE_CONTROL_MESSAGE);
439 }
440 }
441 } else {
442 if (check_mac_perms(name, source_ctx, &cr)) {
443 uint32_t result = property_set(name, value);
444 if (!legacy_protocol) {
445 socket.SendUint32(result);
446 }
447 } else {
448 LOG(ERROR) << "sys_prop(" << cmd_name << "): permission denied uid:" << cr.uid << " name:" << name;
449 if (!legacy_protocol) {
450 socket.SendUint32(PROP_ERROR_PERMISSION_DENIED);
451 }
452 }
453 }
454
455 freecon(source_ctx);
456}
457
458static void handle_property_set_fd() {
459 static constexpr uint32_t kDefaultSocketTimeout = 2000; /* ms */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460
Robert Sesekca2da602017-01-25 08:08:51 -0500461 int s = accept4(property_set_fd, nullptr, nullptr, SOCK_CLOEXEC);
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700462 if (s == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 return;
464 }
465
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700466 struct ucred cr;
467 socklen_t cr_size = sizeof(cr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
469 close(s);
Elliott Hughesb005d902017-02-22 14:54:15 -0800470 PLOG(ERROR) << "sys_prop: unable to get SO_PEERCRED";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 return;
472 }
473
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000474 SocketConnection socket(s, cr);
475 uint32_t timeout_ms = kDefaultSocketTimeout;
476
477 uint32_t cmd = 0;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000478 if (!socket.RecvUint32(&cmd, &timeout_ms)) {
479 PLOG(ERROR) << "sys_prop: error while reading command from the socket";
480 socket.SendUint32(PROP_ERROR_READ_CMD);
JP Abgrall4515d812014-01-31 14:37:07 -0800481 return;
482 }
483
Elliott Hughesb005d902017-02-22 14:54:15 -0800484 switch (cmd) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000485 case PROP_MSG_SETPROP: {
486 char prop_name[PROP_NAME_MAX];
487 char prop_value[PROP_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000489 if (!socket.RecvChars(prop_name, PROP_NAME_MAX, &timeout_ms) ||
490 !socket.RecvChars(prop_value, PROP_VALUE_MAX, &timeout_ms)) {
491 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP): error while reading name/value from the socket";
492 return;
Nick Kralevich69463612013-09-13 17:21:28 -0700493 }
494
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000495 prop_name[PROP_NAME_MAX-1] = 0;
496 prop_value[PROP_VALUE_MAX-1] = 0;
rpcraig63207cd2012-08-09 10:05:49 -0400497
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000498 handle_property_set(socket, prop_value, prop_value, true);
Dimitry Ivanovdee4bd22017-01-19 14:53:00 -0800499 break;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000500 }
Dimitry Ivanov70c4ecf2017-01-24 18:38:09 +0000501
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000502 case PROP_MSG_SETPROP2: {
503 std::string name;
504 std::string value;
505 if (!socket.RecvString(&name, &timeout_ms) ||
506 !socket.RecvString(&value, &timeout_ms)) {
507 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP2): error while reading name/value from the socket";
508 socket.SendUint32(PROP_ERROR_READ_DATA);
509 return;
510 }
511
512 handle_property_set(socket, name, value, false);
513 break;
514 }
Elliott Hughesb005d902017-02-22 14:54:15 -0800515
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516 default:
Elliott Hughesb005d902017-02-22 14:54:15 -0800517 LOG(ERROR) << "sys_prop: invalid command " << cmd;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000518 socket.SendUint32(PROP_ERROR_INVALID_CMD);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 break;
520 }
521}
522
Hung-ying Tyanaef2b092017-06-02 18:59:46 +0800523static bool load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800525/*
526 * Filter is used to decide which properties to load: NULL loads all keys,
527 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
528 */
529static void load_properties(char *data, const char *filter)
530{
531 char *key, *value, *eol, *sol, *tmp, *fn;
532 size_t flen = 0;
533
534 if (filter) {
535 flen = strlen(filter);
536 }
537
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800538 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800539 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540 key = sol;
541 *eol++ = 0;
542 sol = eol;
543
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800544 while (isspace(*key)) key++;
545 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800547 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800548 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800549
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800550 if (!strncmp(key, "import ", 7) && flen == 0) {
551 fn = key + 7;
552 while (isspace(*fn)) fn++;
553
554 key = strchr(fn, ' ');
555 if (key) {
556 *key++ = 0;
557 while (isspace(*key)) key++;
558 }
559
560 load_properties_from_file(fn, key);
561
562 } else {
563 value = strchr(key, '=');
564 if (!value) continue;
565 *value++ = 0;
566
567 tmp = value - 2;
568 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
569
570 while (isspace(*value)) value++;
571
572 if (flen > 0) {
573 if (filter[flen - 1] == '*') {
574 if (strncmp(key, filter, flen - 1)) continue;
575 } else {
576 if (strcmp(key, filter)) continue;
577 }
578 }
579
580 property_set(key, value);
581 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800582 }
583}
584
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700585// Filter is used to decide which properties to load: NULL loads all keys,
586// "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
Hung-ying Tyanaef2b092017-06-02 18:59:46 +0800587static bool load_properties_from_file(const char* filename, const char* filter) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700588 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800589 std::string data;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700590 std::string err;
591 if (!ReadFile(filename, &data, &err)) {
Hung-ying Tyan33463382017-05-25 19:18:17 +0800592 PLOG(WARNING) << "Couldn't load property file: " << err;
Hung-ying Tyanaef2b092017-06-02 18:59:46 +0800593 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800594 }
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700595 data.push_back('\n');
596 load_properties(&data[0], filter);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000597 LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t << ".)";
Hung-ying Tyanaef2b092017-06-02 18:59:46 +0800598 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599}
600
Elliott Hughes81399e12015-03-10 08:39:45 -0700601static void load_persistent_properties() {
602 persistent_properties_loaded = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800603
Elliott Hughes81399e12015-03-10 08:39:45 -0700604 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
605 if (!dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700606 PLOG(ERROR) << "Unable to open persistent property directory \""
607 << PERSISTENT_PROPERTY_DIR << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700608 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800610
Elliott Hughes81399e12015-03-10 08:39:45 -0700611 struct dirent* entry;
612 while ((entry = readdir(dir.get())) != NULL) {
613 if (strncmp("persist.", entry->d_name, strlen("persist."))) {
614 continue;
615 }
616 if (entry->d_type != DT_REG) {
617 continue;
618 }
619
620 // Open the file and read the property value.
621 int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
622 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700623 PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700624 continue;
625 }
626
627 struct stat sb;
628 if (fstat(fd, &sb) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700629 PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
Elliott Hughes81399e12015-03-10 08:39:45 -0700630 close(fd);
631 continue;
632 }
633
634 // File must not be accessible to others, be owned by root/root, and
635 // not be a hard link to any other file.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700636 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 || sb.st_nlink != 1) {
637 PLOG(ERROR) << "skipping insecure property file " << entry->d_name
638 << " (uid=" << sb.st_uid << " gid=" << sb.st_gid
639 << " nlink=" << sb.st_nlink << " mode=" << std::oct << sb.st_mode << ")";
Elliott Hughes81399e12015-03-10 08:39:45 -0700640 close(fd);
641 continue;
642 }
643
644 char value[PROP_VALUE_MAX];
645 int length = read(fd, value, sizeof(value) - 1);
646 if (length >= 0) {
647 value[length] = 0;
648 property_set(entry->d_name, value);
649 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700650 PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
Elliott Hughes81399e12015-03-10 08:39:45 -0700651 }
652 close(fd);
653 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800654}
655
Jaekyun Seok0cf3a072017-04-24 18:52:54 +0900656// persist.sys.usb.config values can't be combined on build-time when property
657// files are split into each partition.
658// So we need to apply the same rule of build/make/tools/post_process_props.py
659// on runtime.
660static void update_sys_usb_config() {
661 bool is_debuggable = android::base::GetBoolProperty("ro.debuggable", false);
662 std::string config = android::base::GetProperty("persist.sys.usb.config", "");
663 if (config.empty()) {
664 property_set("persist.sys.usb.config", is_debuggable ? "adb" : "none");
665 } else if (is_debuggable && config.find("adb") == std::string::npos &&
666 config.length() + 4 < PROP_VALUE_MAX) {
667 config.append(",adb");
668 property_set("persist.sys.usb.config", config);
669 }
670}
671
Elliott Hughesda40c002015-03-27 23:20:44 -0700672void property_load_boot_defaults() {
Hung-ying Tyanaef2b092017-06-02 18:59:46 +0800673 if (!load_properties_from_file("/system/etc/prop.default", NULL)) {
674 // Try recovery path
675 if (!load_properties_from_file("/prop.default", NULL)) {
676 // Try legacy path
677 load_properties_from_file("/default.prop", NULL);
678 }
679 }
Hung-ying Tyan33463382017-05-25 19:18:17 +0800680 load_properties_from_file("/odm/default.prop", NULL);
681 load_properties_from_file("/vendor/default.prop", NULL);
Jaekyun Seok0cf3a072017-04-24 18:52:54 +0900682
683 update_sys_usb_config();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800684}
685
Nick Kralevich32b90232012-09-19 11:15:24 -0700686static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800687 if (ALLOW_LOCAL_PROP_OVERRIDE) {
Hung-ying Tyan33463382017-05-25 19:18:17 +0800688 load_properties_from_file("/data/local.prop", NULL);
Nick Kralevich32b90232012-09-19 11:15:24 -0700689 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700690}
691
Ken Sumrallc5c51032011-03-08 17:01:29 -0800692/* When booting an encrypted system, /data is not mounted when the
693 * property service is started, so any properties stored there are
694 * not loaded. Vold triggers init to load these properties once it
695 * has mounted /data.
696 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700697void load_persist_props(void) {
Nick Kralevich32b90232012-09-19 11:15:24 -0700698 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800699 /* Read persistent properties after all default values have been loaded. */
700 load_persistent_properties();
Keun-young Park404906d2017-02-28 19:20:38 -0800701 property_set("ro.persistent_properties.ready", "true");
Ken Sumrallc5c51032011-03-08 17:01:29 -0800702}
703
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700704void load_recovery_id_prop() {
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800705 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
706 fs_mgr_free_fstab);
707 if (!fstab) {
708 PLOG(ERROR) << "unable to read default fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700709 return;
710 }
711
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800712 fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab.get(), RECOVERY_MOUNT_POINT);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700713 if (rec == NULL) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700714 LOG(ERROR) << "/recovery not specified in fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700715 return;
716 }
717
718 int fd = open(rec->blk_device, O_RDONLY);
719 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700720 PLOG(ERROR) << "error opening block device " << rec->blk_device;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700721 return;
722 }
723
724 boot_img_hdr hdr;
725 if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
726 std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700727 property_set("ro.recovery_id", hex);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700728 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700729 PLOG(ERROR) << "error reading /recovery";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700730 }
731
732 close(fd);
733}
734
Paul Lawrence948410a2015-07-01 14:40:56 -0700735void load_system_props() {
Hung-ying Tyan33463382017-05-25 19:18:17 +0800736 load_properties_from_file("/system/build.prop", NULL);
737 load_properties_from_file("/odm/build.prop", NULL);
738 load_properties_from_file("/vendor/build.prop", NULL);
Elliott Hughes795798d2017-01-27 16:21:55 -0800739 load_properties_from_file("/factory/factory.prop", "ro.*");
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700740 load_recovery_id_prop();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700741}
742
Elliott Hughesda40c002015-03-27 23:20:44 -0700743void start_property_service() {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000744 property_set("ro.property_service.version", "2");
745
Mark Salyzynb066fcc2017-05-05 14:44:35 -0700746 property_set_fd = CreateSocket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
747 false, 0666, 0, 0, nullptr, sehandle);
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700748 if (property_set_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700749 PLOG(ERROR) << "start_property_service socket creation failed";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700750 exit(1);
751 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800752
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700753 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700754
Elliott Hughes929f4072015-04-24 21:13:44 -0700755 register_epoll_handler(property_set_fd, handle_property_set_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700757
758} // namespace init
759} // namespace android