blob: f0e4d2e969693b2726f70c81e4989854d62d4639 [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
Tom Cherry0c8d6d22017-08-10 12:22:44 -070071static struct selabel_handle* sehandle_prop;
72
Elliott Hughesda40c002015-03-27 23:20:44 -070073void property_init() {
Elliott Hughesda40c002015-03-27 23:20:44 -070074 if (__system_property_area_init()) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070075 LOG(ERROR) << "Failed to initialize property area";
Tom Cherry60361142015-12-01 17:16:53 -080076 exit(1);
Elliott Hughesda40c002015-03-27 23:20:44 -070077 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078}
79
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000080static bool check_mac_perms(const std::string& name, char* sctx, struct ucred* cr) {
81
82 if (!sctx) {
83 return false;
84 }
85
86 if (!sehandle_prop) {
87 return false;
88 }
89
90 char* tctx = nullptr;
91 if (selabel_lookup(sehandle_prop, &tctx, name.c_str(), 1) != 0) {
92 return false;
93 }
94
William Robertsd7aea442015-10-01 16:03:47 -070095 property_audit_data audit_data;
rpcraig63207cd2012-08-09 10:05:49 -040096
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000097 audit_data.name = name.c_str();
William Robertsd7aea442015-10-01 16:03:47 -070098 audit_data.cr = cr;
99
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000100 bool has_access = (selinux_check_access(sctx, tctx, "property_service", "set", &audit_data) == 0);
rpcraig63207cd2012-08-09 10:05:49 -0400101
102 freecon(tctx);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000103 return has_access;
rpcraig63207cd2012-08-09 10:05:49 -0400104}
105
William Robertsd7aea442015-10-01 16:03:47 -0700106static int check_control_mac_perms(const char *name, char *sctx, struct ucred *cr)
rpcraig63207cd2012-08-09 10:05:49 -0400107{
rpcraig63207cd2012-08-09 10:05:49 -0400108 /*
109 * Create a name prefix out of ctl.<service name>
110 * The new prefix allows the use of the existing
111 * property service backend labeling while avoiding
112 * mislabels based on true property prefixes.
113 */
114 char ctl_name[PROP_VALUE_MAX+4];
115 int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
116
117 if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
118 return 0;
119
William Robertsd7aea442015-10-01 16:03:47 -0700120 return check_mac_perms(ctl_name, sctx, cr);
rpcraig63207cd2012-08-09 10:05:49 -0400121}
122
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800123static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124{
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700125 char tempPath[PATH_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 char path[PATH_MAX];
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700127 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700129 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
130 fd = mkstemp(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 if (fd < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700132 PLOG(ERROR) << "Unable to write persistent property to temp file " << tempPath;
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800133 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 }
135 write(fd, value, strlen(value));
OPPOde73a0c2014-03-28 19:12:47 +0800136 fsync(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 close(fd);
138
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700139 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 if (rename(tempPath, path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700141 PLOG(ERROR) << "Unable to rename persistent property file " << tempPath << " to " << path;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 unlink(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 }
144}
145
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000146bool is_legal_property_name(const std::string& name) {
Iliyan Malchevf6554802016-09-19 20:58:20 -0700147 size_t namelen = name.size();
148
Nick Kralevich69463612013-09-13 17:21:28 -0700149 if (namelen < 1) return false;
150 if (name[0] == '.') return false;
151 if (name[namelen - 1] == '.') return false;
152
Tom Cherry13693792017-05-30 13:45:28 -0700153 /* Only allow alphanumeric, plus '.', '-', '@', ':', or '_' */
Nick Kralevich69463612013-09-13 17:21:28 -0700154 /* Don't allow ".." to appear in a property name */
Iliyan Malchevf6554802016-09-19 20:58:20 -0700155 for (size_t i = 0; i < namelen; i++) {
Nick Kralevich69463612013-09-13 17:21:28 -0700156 if (name[i] == '.') {
Nick Kralevichc2c5a242013-09-16 11:30:48 -0700157 // i=0 is guaranteed to never have a dot. See above.
158 if (name[i-1] == '.') return false;
Nick Kralevich69463612013-09-13 17:21:28 -0700159 continue;
160 }
Tom Cherry13693792017-05-30 13:45:28 -0700161 if (name[i] == '_' || name[i] == '-' || name[i] == '@' || name[i] == ':') continue;
Nick Kralevich69463612013-09-13 17:21:28 -0700162 if (name[i] >= 'a' && name[i] <= 'z') continue;
163 if (name[i] >= 'A' && name[i] <= 'Z') continue;
164 if (name[i] >= '0' && name[i] <= '9') continue;
165 return false;
166 }
167
168 return true;
169}
170
Tom Marshall62696902017-06-09 18:29:23 +0000171static uint32_t PropertySetImpl(const std::string& name, const std::string& value) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000172 size_t valuelen = value.size();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000174 if (!is_legal_property_name(name)) {
175 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: bad name";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000176 return PROP_ERROR_INVALID_NAME;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000177 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000178
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000179 if (valuelen >= PROP_VALUE_MAX) {
180 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
181 << "value too long";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000182 return PROP_ERROR_INVALID_VALUE;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000183 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000185 prop_info* pi = (prop_info*) __system_property_find(name.c_str());
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000186 if (pi != nullptr) {
187 // ro.* properties are actually "write-once".
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000188 if (android::base::StartsWith(name, "ro.")) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000189 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
190 << "property already set";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000191 return PROP_ERROR_READ_ONLY_PROPERTY;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000192 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000194 __system_property_update(pi, value.c_str(), valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 } else {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000196 int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700197 if (rc < 0) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000198 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
199 << "__system_property_add failed";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000200 return PROP_ERROR_SET_FAILED;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200201 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000203
Elliott Hughes4f915812016-12-05 13:12:48 -0800204 // Don't write properties to disk until after we have read all default
205 // properties to prevent them from being overwritten by default values.
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000206 if (persistent_properties_loaded && android::base::StartsWith(name, "persist.")) {
207 write_persistent_property(name.c_str(), value.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700209 property_changed(name, value);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000210 return PROP_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211}
212
Tom Marshall62696902017-06-09 18:29:23 +0000213typedef int (*PropertyAsyncFunc)(const std::string&, const std::string&);
214
215struct PropertyChildInfo {
216 pid_t pid;
217 PropertyAsyncFunc func;
218 std::string name;
219 std::string value;
220};
221
222static std::queue<PropertyChildInfo> property_children;
223
224static void PropertyChildLaunch() {
225 auto& info = property_children.front();
226 pid_t pid = fork();
227 if (pid < 0) {
228 LOG(ERROR) << "Failed to fork for property_set_async";
229 while (!property_children.empty()) {
230 property_children.pop();
231 }
232 return;
233 }
234 if (pid != 0) {
235 info.pid = pid;
236 } else {
237 if (info.func(info.name, info.value) != 0) {
238 LOG(ERROR) << "property_set_async(\"" << info.name << "\", \"" << info.value
239 << "\") failed";
240 }
241 exit(0);
242 }
243}
244
245bool PropertyChildReap(pid_t pid) {
246 if (property_children.empty()) {
247 return false;
248 }
249 auto& info = property_children.front();
250 if (info.pid != pid) {
251 return false;
252 }
253 if (PropertySetImpl(info.name, info.value) != PROP_SUCCESS) {
254 LOG(ERROR) << "Failed to set async property " << info.name;
255 }
256 property_children.pop();
257 if (!property_children.empty()) {
258 PropertyChildLaunch();
259 }
260 return true;
261}
262
263static uint32_t PropertySetAsync(const std::string& name, const std::string& value,
264 PropertyAsyncFunc func) {
265 if (value.empty()) {
266 return PropertySetImpl(name, value);
267 }
268
269 PropertyChildInfo info;
270 info.func = func;
271 info.name = name;
272 info.value = value;
273 property_children.push(info);
274 if (property_children.size() == 1) {
275 PropertyChildLaunch();
276 }
277 return PROP_SUCCESS;
278}
279
280static int RestoreconRecursiveAsync(const std::string& name, const std::string& value) {
281 return selinux_android_restorecon(value.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
282}
283
284uint32_t property_set(const std::string& name, const std::string& value) {
285 if (name == "selinux.restorecon_recursive") {
286 return PropertySetAsync(name, value, RestoreconRecursiveAsync);
287 }
288
289 return PropertySetImpl(name, value);
290}
291
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000292class SocketConnection {
293 public:
294 SocketConnection(int socket, const struct ucred& cred)
295 : socket_(socket), cred_(cred) {}
296
297 ~SocketConnection() {
298 close(socket_);
299 }
300
301 bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
302 return RecvFully(value, sizeof(*value), timeout_ms);
303 }
304
305 bool RecvChars(char* chars, size_t size, uint32_t* timeout_ms) {
306 return RecvFully(chars, size, timeout_ms);
307 }
308
309 bool RecvString(std::string* value, uint32_t* timeout_ms) {
310 uint32_t len = 0;
311 if (!RecvUint32(&len, timeout_ms)) {
312 return false;
313 }
314
315 if (len == 0) {
316 *value = "";
317 return true;
318 }
319
Elliott Hughesb005d902017-02-22 14:54:15 -0800320 // http://b/35166374: don't allow init to make arbitrarily large allocations.
321 if (len > 0xffff) {
322 LOG(ERROR) << "sys_prop: RecvString asked to read huge string: " << len;
323 errno = ENOMEM;
324 return false;
325 }
326
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000327 std::vector<char> chars(len);
328 if (!RecvChars(&chars[0], len, timeout_ms)) {
329 return false;
330 }
331
332 *value = std::string(&chars[0], len);
333 return true;
334 }
335
336 bool SendUint32(uint32_t value) {
337 int result = TEMP_FAILURE_RETRY(send(socket_, &value, sizeof(value), 0));
338 return result == sizeof(value);
339 }
340
341 int socket() {
342 return socket_;
343 }
344
345 const struct ucred& cred() {
346 return cred_;
347 }
348
349 private:
350 bool PollIn(uint32_t* timeout_ms) {
351 struct pollfd ufds[1];
352 ufds[0].fd = socket_;
353 ufds[0].events = POLLIN;
354 ufds[0].revents = 0;
355 while (*timeout_ms > 0) {
356 Timer timer;
357 int nr = poll(ufds, 1, *timeout_ms);
Tom Cherryede0d532017-07-06 14:20:11 -0700358 uint64_t millis = timer.duration().count();
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000359 *timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis;
360
361 if (nr > 0) {
362 return true;
363 }
364
365 if (nr == 0) {
366 // Timeout
367 break;
368 }
369
370 if (nr < 0 && errno != EINTR) {
371 PLOG(ERROR) << "sys_prop: error waiting for uid " << cred_.uid << " to send property message";
372 return false;
373 } else { // errno == EINTR
374 // Timer rounds milliseconds down in case of EINTR we want it to be rounded up
375 // to avoid slowing init down by causing EINTR with under millisecond timeout.
376 if (*timeout_ms > 0) {
377 --(*timeout_ms);
378 }
379 }
380 }
381
382 LOG(ERROR) << "sys_prop: timeout waiting for uid " << cred_.uid << " to send property message.";
383 return false;
384 }
385
386 bool RecvFully(void* data_ptr, size_t size, uint32_t* timeout_ms) {
387 size_t bytes_left = size;
388 char* data = static_cast<char*>(data_ptr);
389 while (*timeout_ms > 0 && bytes_left > 0) {
390 if (!PollIn(timeout_ms)) {
391 return false;
392 }
393
394 int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
395 if (result <= 0) {
396 return false;
397 }
398
399 bytes_left -= result;
400 data += result;
401 }
402
403 return bytes_left == 0;
404 }
405
406 int socket_;
407 struct ucred cred_;
408
409 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
410};
411
412static void handle_property_set(SocketConnection& socket,
413 const std::string& name,
414 const std::string& value,
415 bool legacy_protocol) {
416 const char* cmd_name = legacy_protocol ? "PROP_MSG_SETPROP" : "PROP_MSG_SETPROP2";
417 if (!is_legal_property_name(name)) {
418 LOG(ERROR) << "sys_prop(" << cmd_name << "): illegal property name \"" << name << "\"";
419 socket.SendUint32(PROP_ERROR_INVALID_NAME);
420 return;
421 }
422
423 struct ucred cr = socket.cred();
424 char* source_ctx = nullptr;
425 getpeercon(socket.socket(), &source_ctx);
426
427 if (android::base::StartsWith(name, "ctl.")) {
428 if (check_control_mac_perms(value.c_str(), source_ctx, &cr)) {
429 handle_control_message(name.c_str() + 4, value.c_str());
430 if (!legacy_protocol) {
431 socket.SendUint32(PROP_SUCCESS);
432 }
433 } else {
434 LOG(ERROR) << "sys_prop(" << cmd_name << "): Unable to " << (name.c_str() + 4)
435 << " service ctl [" << value << "]"
436 << " uid:" << cr.uid
437 << " gid:" << cr.gid
438 << " pid:" << cr.pid;
439 if (!legacy_protocol) {
440 socket.SendUint32(PROP_ERROR_HANDLE_CONTROL_MESSAGE);
441 }
442 }
443 } else {
444 if (check_mac_perms(name, source_ctx, &cr)) {
445 uint32_t result = property_set(name, value);
446 if (!legacy_protocol) {
447 socket.SendUint32(result);
448 }
449 } else {
450 LOG(ERROR) << "sys_prop(" << cmd_name << "): permission denied uid:" << cr.uid << " name:" << name;
451 if (!legacy_protocol) {
452 socket.SendUint32(PROP_ERROR_PERMISSION_DENIED);
453 }
454 }
455 }
456
457 freecon(source_ctx);
458}
459
460static void handle_property_set_fd() {
461 static constexpr uint32_t kDefaultSocketTimeout = 2000; /* ms */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462
Robert Sesekca2da602017-01-25 08:08:51 -0500463 int s = accept4(property_set_fd, nullptr, nullptr, SOCK_CLOEXEC);
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700464 if (s == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465 return;
466 }
467
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700468 struct ucred cr;
469 socklen_t cr_size = sizeof(cr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
471 close(s);
Elliott Hughesb005d902017-02-22 14:54:15 -0800472 PLOG(ERROR) << "sys_prop: unable to get SO_PEERCRED";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 return;
474 }
475
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000476 SocketConnection socket(s, cr);
477 uint32_t timeout_ms = kDefaultSocketTimeout;
478
479 uint32_t cmd = 0;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000480 if (!socket.RecvUint32(&cmd, &timeout_ms)) {
481 PLOG(ERROR) << "sys_prop: error while reading command from the socket";
482 socket.SendUint32(PROP_ERROR_READ_CMD);
JP Abgrall4515d812014-01-31 14:37:07 -0800483 return;
484 }
485
Elliott Hughesb005d902017-02-22 14:54:15 -0800486 switch (cmd) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000487 case PROP_MSG_SETPROP: {
488 char prop_name[PROP_NAME_MAX];
489 char prop_value[PROP_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000491 if (!socket.RecvChars(prop_name, PROP_NAME_MAX, &timeout_ms) ||
492 !socket.RecvChars(prop_value, PROP_VALUE_MAX, &timeout_ms)) {
493 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP): error while reading name/value from the socket";
494 return;
Nick Kralevich69463612013-09-13 17:21:28 -0700495 }
496
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000497 prop_name[PROP_NAME_MAX-1] = 0;
498 prop_value[PROP_VALUE_MAX-1] = 0;
rpcraig63207cd2012-08-09 10:05:49 -0400499
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000500 handle_property_set(socket, prop_value, prop_value, true);
Dimitry Ivanovdee4bd22017-01-19 14:53:00 -0800501 break;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000502 }
Dimitry Ivanov70c4ecf2017-01-24 18:38:09 +0000503
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000504 case PROP_MSG_SETPROP2: {
505 std::string name;
506 std::string value;
507 if (!socket.RecvString(&name, &timeout_ms) ||
508 !socket.RecvString(&value, &timeout_ms)) {
509 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP2): error while reading name/value from the socket";
510 socket.SendUint32(PROP_ERROR_READ_DATA);
511 return;
512 }
513
514 handle_property_set(socket, name, value, false);
515 break;
516 }
Elliott Hughesb005d902017-02-22 14:54:15 -0800517
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 default:
Elliott Hughesb005d902017-02-22 14:54:15 -0800519 LOG(ERROR) << "sys_prop: invalid command " << cmd;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000520 socket.SendUint32(PROP_ERROR_INVALID_CMD);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521 break;
522 }
523}
524
Hung-ying Tyan33463382017-05-25 19:18:17 +0800525static void load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800527/*
528 * Filter is used to decide which properties to load: NULL loads all keys,
529 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
530 */
531static void load_properties(char *data, const char *filter)
532{
533 char *key, *value, *eol, *sol, *tmp, *fn;
534 size_t flen = 0;
535
536 if (filter) {
537 flen = strlen(filter);
538 }
539
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800541 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542 key = sol;
543 *eol++ = 0;
544 sol = eol;
545
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800546 while (isspace(*key)) key++;
547 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800549 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800550 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800551
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800552 if (!strncmp(key, "import ", 7) && flen == 0) {
553 fn = key + 7;
554 while (isspace(*fn)) fn++;
555
556 key = strchr(fn, ' ');
557 if (key) {
558 *key++ = 0;
559 while (isspace(*key)) key++;
560 }
561
562 load_properties_from_file(fn, key);
563
564 } else {
565 value = strchr(key, '=');
566 if (!value) continue;
567 *value++ = 0;
568
569 tmp = value - 2;
570 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
571
572 while (isspace(*value)) value++;
573
574 if (flen > 0) {
575 if (filter[flen - 1] == '*') {
576 if (strncmp(key, filter, flen - 1)) continue;
577 } else {
578 if (strcmp(key, filter)) continue;
579 }
580 }
581
582 property_set(key, value);
583 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800584 }
585}
586
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700587// Filter is used to decide which properties to load: NULL loads all keys,
588// "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
Hung-ying Tyan33463382017-05-25 19:18:17 +0800589static void load_properties_from_file(const char* filename, const char* filter) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700590 Timer t;
Tom Cherry11a3aee2017-08-03 12:54:07 -0700591 auto file_contents = ReadFile(filename);
592 if (!file_contents) {
593 PLOG(WARNING) << "Couldn't load property file '" << filename
594 << "': " << file_contents.error();
Hung-ying Tyan33463382017-05-25 19:18:17 +0800595 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596 }
Tom Cherry11a3aee2017-08-03 12:54:07 -0700597 file_contents->push_back('\n');
598 load_properties(file_contents->data(), filter);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000599 LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t << ".)";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600}
601
Elliott Hughes81399e12015-03-10 08:39:45 -0700602static void load_persistent_properties() {
603 persistent_properties_loaded = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800604
Elliott Hughes81399e12015-03-10 08:39:45 -0700605 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
606 if (!dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700607 PLOG(ERROR) << "Unable to open persistent property directory \""
608 << PERSISTENT_PROPERTY_DIR << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700609 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800610 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800611
Elliott Hughes81399e12015-03-10 08:39:45 -0700612 struct dirent* entry;
613 while ((entry = readdir(dir.get())) != NULL) {
614 if (strncmp("persist.", entry->d_name, strlen("persist."))) {
615 continue;
616 }
617 if (entry->d_type != DT_REG) {
618 continue;
619 }
620
621 // Open the file and read the property value.
622 int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
623 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700624 PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700625 continue;
626 }
627
628 struct stat sb;
629 if (fstat(fd, &sb) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700630 PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
Elliott Hughes81399e12015-03-10 08:39:45 -0700631 close(fd);
632 continue;
633 }
634
635 // File must not be accessible to others, be owned by root/root, and
636 // not be a hard link to any other file.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700637 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 || sb.st_nlink != 1) {
638 PLOG(ERROR) << "skipping insecure property file " << entry->d_name
639 << " (uid=" << sb.st_uid << " gid=" << sb.st_gid
640 << " nlink=" << sb.st_nlink << " mode=" << std::oct << sb.st_mode << ")";
Elliott Hughes81399e12015-03-10 08:39:45 -0700641 close(fd);
642 continue;
643 }
644
645 char value[PROP_VALUE_MAX];
646 int length = read(fd, value, sizeof(value) - 1);
647 if (length >= 0) {
648 value[length] = 0;
649 property_set(entry->d_name, value);
650 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700651 PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
Elliott Hughes81399e12015-03-10 08:39:45 -0700652 }
653 close(fd);
654 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655}
656
Jaekyun Seok0cf3a072017-04-24 18:52:54 +0900657// persist.sys.usb.config values can't be combined on build-time when property
658// files are split into each partition.
659// So we need to apply the same rule of build/make/tools/post_process_props.py
660// on runtime.
661static void update_sys_usb_config() {
662 bool is_debuggable = android::base::GetBoolProperty("ro.debuggable", false);
663 std::string config = android::base::GetProperty("persist.sys.usb.config", "");
664 if (config.empty()) {
665 property_set("persist.sys.usb.config", is_debuggable ? "adb" : "none");
666 } else if (is_debuggable && config.find("adb") == std::string::npos &&
667 config.length() + 4 < PROP_VALUE_MAX) {
668 config.append(",adb");
669 property_set("persist.sys.usb.config", config);
670 }
671}
672
Elliott Hughesda40c002015-03-27 23:20:44 -0700673void property_load_boot_defaults() {
Hung-ying Tyan33463382017-05-25 19:18:17 +0800674 load_properties_from_file("/default.prop", NULL);
675 load_properties_from_file("/odm/default.prop", NULL);
676 load_properties_from_file("/vendor/default.prop", NULL);
Jaekyun Seok0cf3a072017-04-24 18:52:54 +0900677
678 update_sys_usb_config();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679}
680
Nick Kralevich32b90232012-09-19 11:15:24 -0700681static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800682 if (ALLOW_LOCAL_PROP_OVERRIDE) {
Hung-ying Tyan33463382017-05-25 19:18:17 +0800683 load_properties_from_file("/data/local.prop", NULL);
Nick Kralevich32b90232012-09-19 11:15:24 -0700684 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700685}
686
Ken Sumrallc5c51032011-03-08 17:01:29 -0800687/* When booting an encrypted system, /data is not mounted when the
688 * property service is started, so any properties stored there are
689 * not loaded. Vold triggers init to load these properties once it
690 * has mounted /data.
691 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700692void load_persist_props(void) {
Tom Cherry9951b792017-08-24 14:01:25 -0700693 // Devices with FDE have load_persist_props called twice; the first time when the temporary
694 // /data partition is mounted and then again once /data is truly mounted. We do not want to
695 // read persistent properties from the temporary /data partition or mark persistent properties
696 // as having been loaded during the first call, so we return in that case.
697 std::string crypto_state = android::base::GetProperty("ro.crypto.state", "");
698 std::string crypto_type = android::base::GetProperty("ro.crypto.type", "");
699 if (crypto_state == "encrypted" && crypto_type == "block") {
700 static size_t num_calls = 0;
701 if (++num_calls == 1) return;
702 }
703
Nick Kralevich32b90232012-09-19 11:15:24 -0700704 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800705 /* Read persistent properties after all default values have been loaded. */
706 load_persistent_properties();
Keun-young Park404906d2017-02-28 19:20:38 -0800707 property_set("ro.persistent_properties.ready", "true");
Ken Sumrallc5c51032011-03-08 17:01:29 -0800708}
709
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700710void load_recovery_id_prop() {
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800711 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
712 fs_mgr_free_fstab);
713 if (!fstab) {
714 PLOG(ERROR) << "unable to read default fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700715 return;
716 }
717
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800718 fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab.get(), RECOVERY_MOUNT_POINT);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700719 if (rec == NULL) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700720 LOG(ERROR) << "/recovery not specified in fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700721 return;
722 }
723
724 int fd = open(rec->blk_device, O_RDONLY);
725 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700726 PLOG(ERROR) << "error opening block device " << rec->blk_device;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700727 return;
728 }
729
730 boot_img_hdr hdr;
731 if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
732 std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700733 property_set("ro.recovery_id", hex);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700734 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700735 PLOG(ERROR) << "error reading /recovery";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700736 }
737
738 close(fd);
739}
740
Paul Lawrence948410a2015-07-01 14:40:56 -0700741void load_system_props() {
Hung-ying Tyan33463382017-05-25 19:18:17 +0800742 load_properties_from_file("/system/build.prop", NULL);
743 load_properties_from_file("/odm/build.prop", NULL);
744 load_properties_from_file("/vendor/build.prop", NULL);
Elliott Hughes795798d2017-01-27 16:21:55 -0800745 load_properties_from_file("/factory/factory.prop", "ro.*");
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700746 load_recovery_id_prop();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700747}
748
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700749static int SelinuxAuditCallback(void* data, security_class_t /*cls*/, char* buf, size_t len) {
750 property_audit_data* d = reinterpret_cast<property_audit_data*>(data);
751
752 if (!d || !d->name || !d->cr) {
753 LOG(ERROR) << "AuditCallback invoked with null data arguments!";
754 return 0;
755 }
756
757 snprintf(buf, len, "property=%s pid=%d uid=%d gid=%d", d->name, d->cr->pid, d->cr->uid,
758 d->cr->gid);
759 return 0;
760}
761
Elliott Hughesda40c002015-03-27 23:20:44 -0700762void start_property_service() {
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700763 sehandle_prop = selinux_android_prop_context_handle();
764
765 selinux_callback cb;
766 cb.func_audit = SelinuxAuditCallback;
767 selinux_set_callback(SELINUX_CB_AUDIT, cb);
768
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000769 property_set("ro.property_service.version", "2");
770
Mark Salyzynb066fcc2017-05-05 14:44:35 -0700771 property_set_fd = CreateSocket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700772 false, 0666, 0, 0, nullptr);
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700773 if (property_set_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700774 PLOG(ERROR) << "start_property_service socket creation failed";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700775 exit(1);
776 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800777
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700778 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700779
Elliott Hughes929f4072015-04-24 21:13:44 -0700780 register_epoll_handler(property_set_fd, handle_property_set_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800781}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700782
783} // namespace init
784} // namespace android