blob: 0af6c212799e663f6d7610c68cdd2409e154089b [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
Elliott Hughes4f713192015-12-04 22:00:26 -080045#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070046#include <android-base/logging.h>
Jaekyun Seok0cf3a072017-04-24 18:52:54 +090047#include <android-base/properties.h>
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000048#include <android-base/strings.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070049#include <bootimg.h>
50#include <fs_mgr.h>
51#include <selinux/android.h>
52#include <selinux/label.h>
53#include <selinux/selinux.h>
Andres Moralesdb5f5d42015-05-08 08:30:33 -070054
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070056#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
58#define PERSISTENT_PROPERTY_DIR "/data/property"
Andres Moralesdb5f5d42015-05-08 08:30:33 -070059#define RECOVERY_MOUNT_POINT "/recovery"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Tom Cherry81f5d3e2017-06-22 12:53:17 -070061namespace android {
62namespace init {
63
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064static 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
Tom Cherry13693792017-05-30 13:45:28 -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 }
Tom Cherry13693792017-05-30 13:45:28 -0700156 if (name[i] == '_' || 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
Tom Marshall62696902017-06-09 18:29:23 +0000166static uint32_t PropertySetImpl(const std::string& name, const std::string& value) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000167 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 prop_info* pi = (prop_info*) __system_property_find(name.c_str());
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000181 if (pi != nullptr) {
182 // ro.* properties are actually "write-once".
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000183 if (android::base::StartsWith(name, "ro.")) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000184 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
185 << "property already set";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000186 return PROP_ERROR_READ_ONLY_PROPERTY;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000187 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000189 __system_property_update(pi, value.c_str(), valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 } else {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000191 int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700192 if (rc < 0) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000193 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
194 << "__system_property_add failed";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000195 return PROP_ERROR_SET_FAILED;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200196 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000198
Elliott Hughes4f915812016-12-05 13:12:48 -0800199 // Don't write properties to disk until after we have read all default
200 // properties to prevent them from being overwritten by default values.
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000201 if (persistent_properties_loaded && android::base::StartsWith(name, "persist.")) {
202 write_persistent_property(name.c_str(), value.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700204 property_changed(name, value);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000205 return PROP_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206}
207
Tom Marshall62696902017-06-09 18:29:23 +0000208typedef int (*PropertyAsyncFunc)(const std::string&, const std::string&);
209
210struct PropertyChildInfo {
211 pid_t pid;
212 PropertyAsyncFunc func;
213 std::string name;
214 std::string value;
215};
216
217static std::queue<PropertyChildInfo> property_children;
218
219static void PropertyChildLaunch() {
220 auto& info = property_children.front();
221 pid_t pid = fork();
222 if (pid < 0) {
223 LOG(ERROR) << "Failed to fork for property_set_async";
224 while (!property_children.empty()) {
225 property_children.pop();
226 }
227 return;
228 }
229 if (pid != 0) {
230 info.pid = pid;
231 } else {
232 if (info.func(info.name, info.value) != 0) {
233 LOG(ERROR) << "property_set_async(\"" << info.name << "\", \"" << info.value
234 << "\") failed";
235 }
236 exit(0);
237 }
238}
239
240bool PropertyChildReap(pid_t pid) {
241 if (property_children.empty()) {
242 return false;
243 }
244 auto& info = property_children.front();
245 if (info.pid != pid) {
246 return false;
247 }
248 if (PropertySetImpl(info.name, info.value) != PROP_SUCCESS) {
249 LOG(ERROR) << "Failed to set async property " << info.name;
250 }
251 property_children.pop();
252 if (!property_children.empty()) {
253 PropertyChildLaunch();
254 }
255 return true;
256}
257
258static uint32_t PropertySetAsync(const std::string& name, const std::string& value,
259 PropertyAsyncFunc func) {
260 if (value.empty()) {
261 return PropertySetImpl(name, value);
262 }
263
264 PropertyChildInfo info;
265 info.func = func;
266 info.name = name;
267 info.value = value;
268 property_children.push(info);
269 if (property_children.size() == 1) {
270 PropertyChildLaunch();
271 }
272 return PROP_SUCCESS;
273}
274
275static int RestoreconRecursiveAsync(const std::string& name, const std::string& value) {
276 return selinux_android_restorecon(value.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
277}
278
279uint32_t property_set(const std::string& name, const std::string& value) {
280 if (name == "selinux.restorecon_recursive") {
281 return PropertySetAsync(name, value, RestoreconRecursiveAsync);
282 }
283
284 return PropertySetImpl(name, value);
285}
286
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000287class SocketConnection {
288 public:
289 SocketConnection(int socket, const struct ucred& cred)
290 : socket_(socket), cred_(cred) {}
291
292 ~SocketConnection() {
293 close(socket_);
294 }
295
296 bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
297 return RecvFully(value, sizeof(*value), timeout_ms);
298 }
299
300 bool RecvChars(char* chars, size_t size, uint32_t* timeout_ms) {
301 return RecvFully(chars, size, timeout_ms);
302 }
303
304 bool RecvString(std::string* value, uint32_t* timeout_ms) {
305 uint32_t len = 0;
306 if (!RecvUint32(&len, timeout_ms)) {
307 return false;
308 }
309
310 if (len == 0) {
311 *value = "";
312 return true;
313 }
314
Elliott Hughesb005d902017-02-22 14:54:15 -0800315 // http://b/35166374: don't allow init to make arbitrarily large allocations.
316 if (len > 0xffff) {
317 LOG(ERROR) << "sys_prop: RecvString asked to read huge string: " << len;
318 errno = ENOMEM;
319 return false;
320 }
321
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000322 std::vector<char> chars(len);
323 if (!RecvChars(&chars[0], len, timeout_ms)) {
324 return false;
325 }
326
327 *value = std::string(&chars[0], len);
328 return true;
329 }
330
331 bool SendUint32(uint32_t value) {
332 int result = TEMP_FAILURE_RETRY(send(socket_, &value, sizeof(value), 0));
333 return result == sizeof(value);
334 }
335
336 int socket() {
337 return socket_;
338 }
339
340 const struct ucred& cred() {
341 return cred_;
342 }
343
344 private:
345 bool PollIn(uint32_t* timeout_ms) {
346 struct pollfd ufds[1];
347 ufds[0].fd = socket_;
348 ufds[0].events = POLLIN;
349 ufds[0].revents = 0;
350 while (*timeout_ms > 0) {
351 Timer timer;
352 int nr = poll(ufds, 1, *timeout_ms);
James Hawkins27c05222017-01-26 11:55:44 -0800353 uint64_t millis = timer.duration_ms();
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000354 *timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis;
355
356 if (nr > 0) {
357 return true;
358 }
359
360 if (nr == 0) {
361 // Timeout
362 break;
363 }
364
365 if (nr < 0 && errno != EINTR) {
366 PLOG(ERROR) << "sys_prop: error waiting for uid " << cred_.uid << " to send property message";
367 return false;
368 } else { // errno == EINTR
369 // Timer rounds milliseconds down in case of EINTR we want it to be rounded up
370 // to avoid slowing init down by causing EINTR with under millisecond timeout.
371 if (*timeout_ms > 0) {
372 --(*timeout_ms);
373 }
374 }
375 }
376
377 LOG(ERROR) << "sys_prop: timeout waiting for uid " << cred_.uid << " to send property message.";
378 return false;
379 }
380
381 bool RecvFully(void* data_ptr, size_t size, uint32_t* timeout_ms) {
382 size_t bytes_left = size;
383 char* data = static_cast<char*>(data_ptr);
384 while (*timeout_ms > 0 && bytes_left > 0) {
385 if (!PollIn(timeout_ms)) {
386 return false;
387 }
388
389 int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
390 if (result <= 0) {
391 return false;
392 }
393
394 bytes_left -= result;
395 data += result;
396 }
397
398 return bytes_left == 0;
399 }
400
401 int socket_;
402 struct ucred cred_;
403
404 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
405};
406
407static void handle_property_set(SocketConnection& socket,
408 const std::string& name,
409 const std::string& value,
410 bool legacy_protocol) {
411 const char* cmd_name = legacy_protocol ? "PROP_MSG_SETPROP" : "PROP_MSG_SETPROP2";
412 if (!is_legal_property_name(name)) {
413 LOG(ERROR) << "sys_prop(" << cmd_name << "): illegal property name \"" << name << "\"";
414 socket.SendUint32(PROP_ERROR_INVALID_NAME);
415 return;
416 }
417
418 struct ucred cr = socket.cred();
419 char* source_ctx = nullptr;
420 getpeercon(socket.socket(), &source_ctx);
421
422 if (android::base::StartsWith(name, "ctl.")) {
423 if (check_control_mac_perms(value.c_str(), source_ctx, &cr)) {
424 handle_control_message(name.c_str() + 4, value.c_str());
425 if (!legacy_protocol) {
426 socket.SendUint32(PROP_SUCCESS);
427 }
428 } else {
429 LOG(ERROR) << "sys_prop(" << cmd_name << "): Unable to " << (name.c_str() + 4)
430 << " service ctl [" << value << "]"
431 << " uid:" << cr.uid
432 << " gid:" << cr.gid
433 << " pid:" << cr.pid;
434 if (!legacy_protocol) {
435 socket.SendUint32(PROP_ERROR_HANDLE_CONTROL_MESSAGE);
436 }
437 }
438 } else {
439 if (check_mac_perms(name, source_ctx, &cr)) {
440 uint32_t result = property_set(name, value);
441 if (!legacy_protocol) {
442 socket.SendUint32(result);
443 }
444 } else {
445 LOG(ERROR) << "sys_prop(" << cmd_name << "): permission denied uid:" << cr.uid << " name:" << name;
446 if (!legacy_protocol) {
447 socket.SendUint32(PROP_ERROR_PERMISSION_DENIED);
448 }
449 }
450 }
451
452 freecon(source_ctx);
453}
454
455static void handle_property_set_fd() {
456 static constexpr uint32_t kDefaultSocketTimeout = 2000; /* ms */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457
Robert Sesekca2da602017-01-25 08:08:51 -0500458 int s = accept4(property_set_fd, nullptr, nullptr, SOCK_CLOEXEC);
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700459 if (s == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 return;
461 }
462
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700463 struct ucred cr;
464 socklen_t cr_size = sizeof(cr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
466 close(s);
Elliott Hughesb005d902017-02-22 14:54:15 -0800467 PLOG(ERROR) << "sys_prop: unable to get SO_PEERCRED";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 return;
469 }
470
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000471 SocketConnection socket(s, cr);
472 uint32_t timeout_ms = kDefaultSocketTimeout;
473
474 uint32_t cmd = 0;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000475 if (!socket.RecvUint32(&cmd, &timeout_ms)) {
476 PLOG(ERROR) << "sys_prop: error while reading command from the socket";
477 socket.SendUint32(PROP_ERROR_READ_CMD);
JP Abgrall4515d812014-01-31 14:37:07 -0800478 return;
479 }
480
Elliott Hughesb005d902017-02-22 14:54:15 -0800481 switch (cmd) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000482 case PROP_MSG_SETPROP: {
483 char prop_name[PROP_NAME_MAX];
484 char prop_value[PROP_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000486 if (!socket.RecvChars(prop_name, PROP_NAME_MAX, &timeout_ms) ||
487 !socket.RecvChars(prop_value, PROP_VALUE_MAX, &timeout_ms)) {
488 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP): error while reading name/value from the socket";
489 return;
Nick Kralevich69463612013-09-13 17:21:28 -0700490 }
491
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000492 prop_name[PROP_NAME_MAX-1] = 0;
493 prop_value[PROP_VALUE_MAX-1] = 0;
rpcraig63207cd2012-08-09 10:05:49 -0400494
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000495 handle_property_set(socket, prop_value, prop_value, true);
Dimitry Ivanovdee4bd22017-01-19 14:53:00 -0800496 break;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000497 }
Dimitry Ivanov70c4ecf2017-01-24 18:38:09 +0000498
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000499 case PROP_MSG_SETPROP2: {
500 std::string name;
501 std::string value;
502 if (!socket.RecvString(&name, &timeout_ms) ||
503 !socket.RecvString(&value, &timeout_ms)) {
504 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP2): error while reading name/value from the socket";
505 socket.SendUint32(PROP_ERROR_READ_DATA);
506 return;
507 }
508
509 handle_property_set(socket, name, value, false);
510 break;
511 }
Elliott Hughesb005d902017-02-22 14:54:15 -0800512
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513 default:
Elliott Hughesb005d902017-02-22 14:54:15 -0800514 LOG(ERROR) << "sys_prop: invalid command " << cmd;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000515 socket.SendUint32(PROP_ERROR_INVALID_CMD);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516 break;
517 }
518}
519
Hung-ying Tyan33463382017-05-25 19:18:17 +0800520static void load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800522/*
523 * Filter is used to decide which properties to load: NULL loads all keys,
524 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
525 */
526static void load_properties(char *data, const char *filter)
527{
528 char *key, *value, *eol, *sol, *tmp, *fn;
529 size_t flen = 0;
530
531 if (filter) {
532 flen = strlen(filter);
533 }
534
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800536 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537 key = sol;
538 *eol++ = 0;
539 sol = eol;
540
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800541 while (isspace(*key)) key++;
542 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800545 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800547 if (!strncmp(key, "import ", 7) && flen == 0) {
548 fn = key + 7;
549 while (isspace(*fn)) fn++;
550
551 key = strchr(fn, ' ');
552 if (key) {
553 *key++ = 0;
554 while (isspace(*key)) key++;
555 }
556
557 load_properties_from_file(fn, key);
558
559 } else {
560 value = strchr(key, '=');
561 if (!value) continue;
562 *value++ = 0;
563
564 tmp = value - 2;
565 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
566
567 while (isspace(*value)) value++;
568
569 if (flen > 0) {
570 if (filter[flen - 1] == '*') {
571 if (strncmp(key, filter, flen - 1)) continue;
572 } else {
573 if (strcmp(key, filter)) continue;
574 }
575 }
576
577 property_set(key, value);
578 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800579 }
580}
581
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700582// Filter is used to decide which properties to load: NULL loads all keys,
583// "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
Hung-ying Tyan33463382017-05-25 19:18:17 +0800584static void load_properties_from_file(const char* filename, const char* filter) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700585 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800586 std::string data;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700587 std::string err;
588 if (!ReadFile(filename, &data, &err)) {
Hung-ying Tyan33463382017-05-25 19:18:17 +0800589 PLOG(WARNING) << "Couldn't load property file: " << err;
590 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800591 }
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700592 data.push_back('\n');
593 load_properties(&data[0], filter);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000594 LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t << ".)";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595}
596
Elliott Hughes81399e12015-03-10 08:39:45 -0700597static void load_persistent_properties() {
598 persistent_properties_loaded = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599
Elliott Hughes81399e12015-03-10 08:39:45 -0700600 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
601 if (!dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700602 PLOG(ERROR) << "Unable to open persistent property directory \""
603 << PERSISTENT_PROPERTY_DIR << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700604 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800605 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800606
Elliott Hughes81399e12015-03-10 08:39:45 -0700607 struct dirent* entry;
608 while ((entry = readdir(dir.get())) != NULL) {
609 if (strncmp("persist.", entry->d_name, strlen("persist."))) {
610 continue;
611 }
612 if (entry->d_type != DT_REG) {
613 continue;
614 }
615
616 // Open the file and read the property value.
617 int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
618 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700619 PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700620 continue;
621 }
622
623 struct stat sb;
624 if (fstat(fd, &sb) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700625 PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
Elliott Hughes81399e12015-03-10 08:39:45 -0700626 close(fd);
627 continue;
628 }
629
630 // File must not be accessible to others, be owned by root/root, and
631 // not be a hard link to any other file.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700632 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 || sb.st_nlink != 1) {
633 PLOG(ERROR) << "skipping insecure property file " << entry->d_name
634 << " (uid=" << sb.st_uid << " gid=" << sb.st_gid
635 << " nlink=" << sb.st_nlink << " mode=" << std::oct << sb.st_mode << ")";
Elliott Hughes81399e12015-03-10 08:39:45 -0700636 close(fd);
637 continue;
638 }
639
640 char value[PROP_VALUE_MAX];
641 int length = read(fd, value, sizeof(value) - 1);
642 if (length >= 0) {
643 value[length] = 0;
644 property_set(entry->d_name, value);
645 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700646 PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
Elliott Hughes81399e12015-03-10 08:39:45 -0700647 }
648 close(fd);
649 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650}
651
Jaekyun Seok0cf3a072017-04-24 18:52:54 +0900652// persist.sys.usb.config values can't be combined on build-time when property
653// files are split into each partition.
654// So we need to apply the same rule of build/make/tools/post_process_props.py
655// on runtime.
656static void update_sys_usb_config() {
657 bool is_debuggable = android::base::GetBoolProperty("ro.debuggable", false);
658 std::string config = android::base::GetProperty("persist.sys.usb.config", "");
659 if (config.empty()) {
660 property_set("persist.sys.usb.config", is_debuggable ? "adb" : "none");
661 } else if (is_debuggable && config.find("adb") == std::string::npos &&
662 config.length() + 4 < PROP_VALUE_MAX) {
663 config.append(",adb");
664 property_set("persist.sys.usb.config", config);
665 }
666}
667
Elliott Hughesda40c002015-03-27 23:20:44 -0700668void property_load_boot_defaults() {
Hung-ying Tyan33463382017-05-25 19:18:17 +0800669 load_properties_from_file("/default.prop", NULL);
670 load_properties_from_file("/odm/default.prop", NULL);
671 load_properties_from_file("/vendor/default.prop", NULL);
Jaekyun Seok0cf3a072017-04-24 18:52:54 +0900672
673 update_sys_usb_config();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800674}
675
Nick Kralevich32b90232012-09-19 11:15:24 -0700676static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800677 if (ALLOW_LOCAL_PROP_OVERRIDE) {
Hung-ying Tyan33463382017-05-25 19:18:17 +0800678 load_properties_from_file("/data/local.prop", NULL);
Nick Kralevich32b90232012-09-19 11:15:24 -0700679 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700680}
681
Ken Sumrallc5c51032011-03-08 17:01:29 -0800682/* When booting an encrypted system, /data is not mounted when the
683 * property service is started, so any properties stored there are
684 * not loaded. Vold triggers init to load these properties once it
685 * has mounted /data.
686 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700687void load_persist_props(void) {
Nick Kralevich32b90232012-09-19 11:15:24 -0700688 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800689 /* Read persistent properties after all default values have been loaded. */
690 load_persistent_properties();
Keun-young Park404906d2017-02-28 19:20:38 -0800691 property_set("ro.persistent_properties.ready", "true");
Ken Sumrallc5c51032011-03-08 17:01:29 -0800692}
693
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700694void load_recovery_id_prop() {
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800695 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
696 fs_mgr_free_fstab);
697 if (!fstab) {
698 PLOG(ERROR) << "unable to read default fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700699 return;
700 }
701
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800702 fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab.get(), RECOVERY_MOUNT_POINT);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700703 if (rec == NULL) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700704 LOG(ERROR) << "/recovery not specified in fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700705 return;
706 }
707
708 int fd = open(rec->blk_device, O_RDONLY);
709 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700710 PLOG(ERROR) << "error opening block device " << rec->blk_device;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700711 return;
712 }
713
714 boot_img_hdr hdr;
715 if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
716 std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700717 property_set("ro.recovery_id", hex);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700718 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700719 PLOG(ERROR) << "error reading /recovery";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700720 }
721
722 close(fd);
723}
724
Paul Lawrence948410a2015-07-01 14:40:56 -0700725void load_system_props() {
Hung-ying Tyan33463382017-05-25 19:18:17 +0800726 load_properties_from_file("/system/build.prop", NULL);
727 load_properties_from_file("/odm/build.prop", NULL);
728 load_properties_from_file("/vendor/build.prop", NULL);
Elliott Hughes795798d2017-01-27 16:21:55 -0800729 load_properties_from_file("/factory/factory.prop", "ro.*");
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700730 load_recovery_id_prop();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700731}
732
Elliott Hughesda40c002015-03-27 23:20:44 -0700733void start_property_service() {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000734 property_set("ro.property_service.version", "2");
735
Mark Salyzynb066fcc2017-05-05 14:44:35 -0700736 property_set_fd = CreateSocket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
737 false, 0666, 0, 0, nullptr, sehandle);
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700738 if (property_set_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700739 PLOG(ERROR) << "start_property_service socket creation failed";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700740 exit(1);
741 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800742
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700743 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700744
Elliott Hughes929f4072015-04-24 21:13:44 -0700745 register_epoll_handler(property_set_fd, handle_property_set_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700747
748} // namespace init
749} // namespace android