blob: 7d2f026b6adf2e36c87a52819e46b7e8bc34524f [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
Tom Cherry40acb372018-08-01 13:41:12 -070019#include <android/api-level.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070020#include <ctype.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070021#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>
Tom Cherry8702dcb2017-10-13 16:20:19 -070037#include <wchar.h>
Elliott Hughes81399e12015-03-10 08:39:45 -070038
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
40#include <sys/_system_properties.h>
41
Tom Cherrybe048922019-02-16 12:03:19 -080042#include <map>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070043#include <memory>
Tom Marshall62696902017-06-09 18:29:23 +000044#include <queue>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070045#include <vector>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
Tom Cherryede0d532017-07-06 14:20:11 -070047#include <android-base/chrono_utils.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080048#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070049#include <android-base/logging.h>
Jaekyun Seok0cf3a072017-04-24 18:52:54 +090050#include <android-base/properties.h>
Tom Cherrya84e14d2017-09-05 12:38:30 -070051#include <android-base/stringprintf.h>
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000052#include <android-base/strings.h>
Tom Cherry2ae2f602017-12-14 01:58:17 +000053#include <property_info_parser/property_info_parser.h>
54#include <property_info_serializer/property_info_serializer.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070055#include <selinux/android.h>
56#include <selinux/label.h>
57#include <selinux/selinux.h>
Andres Moralesdb5f5d42015-05-08 08:30:33 -070058
Bowgo Tsai30afda72019-04-11 23:57:24 +080059#include "debug_ramdisk.h"
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070060#include "epoll.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061#include "init.h"
Tom Cherry16fad422017-08-04 15:59:03 -070062#include "persistent_properties.h"
Tom Cherry927c5d52017-12-11 01:40:07 -080063#include "property_type.h"
Logan Chien837b2a42018-05-03 14:33:52 +080064#include "selinux.h"
Tom Cherrydc375862018-02-28 10:39:01 -080065#include "subcontext.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070066#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
Tom Cherrydc375862018-02-28 10:39:01 -080068using namespace std::literals;
69
Steven Laver57a740e2019-01-29 20:19:05 -080070using android::base::GetProperty;
Tom Cherry2ae2f602017-12-14 01:58:17 +000071using android::base::ReadFileToString;
72using android::base::Split;
Tom Cherry1cf8d692017-10-10 13:35:01 -070073using android::base::StartsWith;
Tom Cherrya84e14d2017-09-05 12:38:30 -070074using android::base::StringPrintf;
Tom Cherryede0d532017-07-06 14:20:11 -070075using android::base::Timer;
Tom Cherry2ae2f602017-12-14 01:58:17 +000076using android::base::Trim;
77using android::base::WriteStringToFile;
78using android::properties::BuildTrie;
Tom Cherry919458c2018-01-03 14:39:28 -080079using android::properties::ParsePropertyInfoFile;
Tom Cherry2ae2f602017-12-14 01:58:17 +000080using android::properties::PropertyInfoAreaFile;
81using android::properties::PropertyInfoEntry;
Tom Cherryede0d532017-07-06 14:20:11 -070082
Tom Cherry81f5d3e2017-06-22 12:53:17 -070083namespace android {
84namespace init {
85
Tom Cherry16fad422017-08-04 15:59:03 -070086static bool persistent_properties_loaded = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Colin Crossd11beb22010-04-13 19:33:37 -070088static int property_set_fd = -1;
89
Tom Cherry2ae2f602017-12-14 01:58:17 +000090static PropertyInfoAreaFile property_info_area;
91
Tom Cherry32228482018-01-18 16:14:25 -080092uint32_t InitPropertySet(const std::string& name, const std::string& value);
93
94uint32_t (*property_set)(const std::string& name, const std::string& value) = InitPropertySet;
95
Tom Cherry2ae2f602017-12-14 01:58:17 +000096void CreateSerializedPropertyInfo();
Tom Cherryc3692b32017-08-10 12:22:44 -070097
Tom Cherry5ab2e1c2018-05-03 16:57:19 -070098struct PropertyAuditData {
99 const ucred* cr;
100 const char* name;
101};
102
Tom Cherry2f113ad2019-04-22 10:22:41 -0700103static int PropertyAuditCallback(void* data, security_class_t /*cls*/, char* buf, size_t len) {
104 auto* d = reinterpret_cast<PropertyAuditData*>(data);
105
106 if (!d || !d->name || !d->cr) {
107 LOG(ERROR) << "AuditCallback invoked with null data arguments!";
108 return 0;
109 }
110
111 snprintf(buf, len, "property=%s pid=%d uid=%d gid=%d", d->name, d->cr->pid, d->cr->uid,
112 d->cr->gid);
113 return 0;
114}
115
Elliott Hughesda40c002015-03-27 23:20:44 -0700116void property_init() {
Tom Cherry2f113ad2019-04-22 10:22:41 -0700117 selinux_callback cb;
118 cb.func_audit = PropertyAuditCallback;
119 selinux_set_callback(SELINUX_CB_AUDIT, cb);
120
Tom Cherry2ae2f602017-12-14 01:58:17 +0000121 mkdir("/dev/__properties__", S_IRWXU | S_IXGRP | S_IXOTH);
122 CreateSerializedPropertyInfo();
Elliott Hughesda40c002015-03-27 23:20:44 -0700123 if (__system_property_area_init()) {
Tom Cherry4a679452017-09-26 16:30:03 -0700124 LOG(FATAL) << "Failed to initialize property area";
Elliott Hughesda40c002015-03-27 23:20:44 -0700125 }
Tom Cherry2ae2f602017-12-14 01:58:17 +0000126 if (!property_info_area.LoadDefaultPath()) {
127 LOG(FATAL) << "Failed to load serialized property info file";
128 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129}
Tom Cherryb35f8272018-10-22 14:50:52 -0700130
131bool CanReadProperty(const std::string& source_context, const std::string& name) {
132 const char* target_context = nullptr;
133 property_info_area->GetPropertyInfo(name.c_str(), &target_context, nullptr);
134
135 PropertyAuditData audit_data;
136
137 audit_data.name = name.c_str();
138
139 ucred cr = {.pid = 0, .uid = 0, .gid = 0};
140 audit_data.cr = &cr;
141
142 return selinux_check_access(source_context.c_str(), target_context, "file", "read",
143 &audit_data) == 0;
144}
145
Tom Cherry927c5d52017-12-11 01:40:07 -0800146static bool CheckMacPerms(const std::string& name, const char* target_context,
Tom Cherry32228482018-01-18 16:14:25 -0800147 const char* source_context, const ucred& cr) {
Tom Cherry927c5d52017-12-11 01:40:07 -0800148 if (!target_context || !source_context) {
Tom Cherry2ae2f602017-12-14 01:58:17 +0000149 return false;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000150 }
151
Tom Cherry5ab2e1c2018-05-03 16:57:19 -0700152 PropertyAuditData audit_data;
rpcraig63207cd2012-08-09 10:05:49 -0400153
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000154 audit_data.name = name.c_str();
Tom Cherry32228482018-01-18 16:14:25 -0800155 audit_data.cr = &cr;
William Robertsd7aea442015-10-01 16:03:47 -0700156
Tom Cherry927c5d52017-12-11 01:40:07 -0800157 bool has_access = (selinux_check_access(source_context, target_context, "property_service",
158 "set", &audit_data) == 0);
rpcraig63207cd2012-08-09 10:05:49 -0400159
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000160 return has_access;
rpcraig63207cd2012-08-09 10:05:49 -0400161}
162
Tom Cherry69d47aa2018-03-01 11:00:57 -0800163static uint32_t PropertySet(const std::string& name, const std::string& value, std::string* error) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000164 size_t valuelen = value.size();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165
Tom Cherryde6bd502018-02-13 16:50:08 -0800166 if (!IsLegalPropertyName(name)) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800167 *error = "Illegal property name";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000168 return PROP_ERROR_INVALID_NAME;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000169 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000170
Tom Cherry1cf8d692017-10-10 13:35:01 -0700171 if (valuelen >= PROP_VALUE_MAX && !StartsWith(name, "ro.")) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800172 *error = "Property value too long";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000173 return PROP_ERROR_INVALID_VALUE;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000174 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
Tom Cherry8702dcb2017-10-13 16:20:19 -0700176 if (mbstowcs(nullptr, value.data(), 0) == static_cast<std::size_t>(-1)) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800177 *error = "Value is not a UTF8 encoded string";
Tom Cherry8702dcb2017-10-13 16:20:19 -0700178 return PROP_ERROR_INVALID_VALUE;
179 }
180
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000181 prop_info* pi = (prop_info*) __system_property_find(name.c_str());
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000182 if (pi != nullptr) {
183 // ro.* properties are actually "write-once".
Tom Cherry1cf8d692017-10-10 13:35:01 -0700184 if (StartsWith(name, "ro.")) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800185 *error = "Read-only property was 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) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800193 *error = "__system_property_add failed";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000194 return PROP_ERROR_SET_FAILED;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200195 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000197
Elliott Hughes4f915812016-12-05 13:12:48 -0800198 // Don't write properties to disk until after we have read all default
199 // properties to prevent them from being overwritten by default values.
Tom Cherry1cf8d692017-10-10 13:35:01 -0700200 if (persistent_properties_loaded && StartsWith(name, "persist.")) {
Tom Cherry16fad422017-08-04 15:59:03 -0700201 WritePersistentProperty(name, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700203 property_changed(name, value);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000204 return PROP_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205}
206
Tom Marshall62696902017-06-09 18:29:23 +0000207typedef int (*PropertyAsyncFunc)(const std::string&, const std::string&);
208
209struct PropertyChildInfo {
210 pid_t pid;
211 PropertyAsyncFunc func;
212 std::string name;
213 std::string value;
214};
215
216static std::queue<PropertyChildInfo> property_children;
217
218static void PropertyChildLaunch() {
219 auto& info = property_children.front();
220 pid_t pid = fork();
221 if (pid < 0) {
222 LOG(ERROR) << "Failed to fork for property_set_async";
223 while (!property_children.empty()) {
224 property_children.pop();
225 }
226 return;
227 }
228 if (pid != 0) {
229 info.pid = pid;
230 } else {
231 if (info.func(info.name, info.value) != 0) {
232 LOG(ERROR) << "property_set_async(\"" << info.name << "\", \"" << info.value
233 << "\") failed";
234 }
Tom Cherry4a679452017-09-26 16:30:03 -0700235 _exit(0);
Tom Marshall62696902017-06-09 18:29:23 +0000236 }
237}
238
239bool PropertyChildReap(pid_t pid) {
240 if (property_children.empty()) {
241 return false;
242 }
243 auto& info = property_children.front();
244 if (info.pid != pid) {
245 return false;
246 }
Tom Cherry69d47aa2018-03-01 11:00:57 -0800247 std::string error;
248 if (PropertySet(info.name, info.value, &error) != PROP_SUCCESS) {
249 LOG(ERROR) << "Failed to set async property " << info.name << " to " << info.value << ": "
250 << error;
Tom Marshall62696902017-06-09 18:29:23 +0000251 }
252 property_children.pop();
253 if (!property_children.empty()) {
254 PropertyChildLaunch();
255 }
256 return true;
257}
258
259static uint32_t PropertySetAsync(const std::string& name, const std::string& value,
Tom Cherry69d47aa2018-03-01 11:00:57 -0800260 PropertyAsyncFunc func, std::string* error) {
Tom Marshall62696902017-06-09 18:29:23 +0000261 if (value.empty()) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800262 return PropertySet(name, value, error);
Tom Marshall62696902017-06-09 18:29:23 +0000263 }
264
265 PropertyChildInfo info;
266 info.func = func;
267 info.name = name;
268 info.value = value;
269 property_children.push(info);
270 if (property_children.size() == 1) {
271 PropertyChildLaunch();
272 }
273 return PROP_SUCCESS;
274}
275
276static int RestoreconRecursiveAsync(const std::string& name, const std::string& value) {
277 return selinux_android_restorecon(value.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
278}
279
Tom Cherry32228482018-01-18 16:14:25 -0800280uint32_t InitPropertySet(const std::string& name, const std::string& value) {
281 if (StartsWith(name, "ctl.")) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800282 LOG(ERROR) << "InitPropertySet: Do not set ctl. properties from init; call the Service "
283 "functions directly";
284 return PROP_ERROR_INVALID_NAME;
285 }
286 if (name == "selinux.restorecon_recursive") {
287 LOG(ERROR) << "InitPropertySet: Do not set selinux.restorecon_recursive from init; use the "
288 "restorecon builtin directly";
Tom Cherry32228482018-01-18 16:14:25 -0800289 return PROP_ERROR_INVALID_NAME;
290 }
291
Tom Cherry69d47aa2018-03-01 11:00:57 -0800292 uint32_t result = 0;
293 ucred cr = {.pid = 1, .uid = 0, .gid = 0};
294 std::string error;
295 result = HandlePropertySet(name, value, kInitContext.c_str(), cr, &error);
296 if (result != PROP_SUCCESS) {
297 LOG(ERROR) << "Init cannot set '" << name << "' to '" << value << "': " << error;
Tom Cherry32228482018-01-18 16:14:25 -0800298 }
299
Tom Cherry69d47aa2018-03-01 11:00:57 -0800300 return result;
Tom Cherry32228482018-01-18 16:14:25 -0800301}
302
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000303class SocketConnection {
Tom Cherry32228482018-01-18 16:14:25 -0800304 public:
305 SocketConnection(int socket, const ucred& cred) : socket_(socket), cred_(cred) {}
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000306
Tom Cherry32228482018-01-18 16:14:25 -0800307 ~SocketConnection() { close(socket_); }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000308
Tom Cherry32228482018-01-18 16:14:25 -0800309 bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
310 return RecvFully(value, sizeof(*value), timeout_ms);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000311 }
312
Tom Cherry32228482018-01-18 16:14:25 -0800313 bool RecvChars(char* chars, size_t size, uint32_t* timeout_ms) {
314 return RecvFully(chars, size, timeout_ms);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000315 }
316
Tom Cherry32228482018-01-18 16:14:25 -0800317 bool RecvString(std::string* value, uint32_t* timeout_ms) {
318 uint32_t len = 0;
319 if (!RecvUint32(&len, timeout_ms)) {
320 return false;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000321 }
Tom Cherry32228482018-01-18 16:14:25 -0800322
323 if (len == 0) {
324 *value = "";
325 return true;
326 }
327
328 // http://b/35166374: don't allow init to make arbitrarily large allocations.
329 if (len > 0xffff) {
330 LOG(ERROR) << "sys_prop: RecvString asked to read huge string: " << len;
331 errno = ENOMEM;
332 return false;
333 }
334
335 std::vector<char> chars(len);
336 if (!RecvChars(&chars[0], len, timeout_ms)) {
337 return false;
338 }
339
340 *value = std::string(&chars[0], len);
341 return true;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000342 }
343
Tom Cherry32228482018-01-18 16:14:25 -0800344 bool SendUint32(uint32_t value) {
345 int result = TEMP_FAILURE_RETRY(send(socket_, &value, sizeof(value), 0));
346 return result == sizeof(value);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000347 }
348
Tom Cherry32228482018-01-18 16:14:25 -0800349 int socket() { return socket_; }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000350
Tom Cherry32228482018-01-18 16:14:25 -0800351 const ucred& cred() { return cred_; }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000352
Tom Cherry32228482018-01-18 16:14:25 -0800353 std::string source_context() const {
354 char* source_context = nullptr;
355 getpeercon(socket_, &source_context);
356 std::string result = source_context;
357 freecon(source_context);
358 return result;
359 }
360
361 private:
362 bool PollIn(uint32_t* timeout_ms) {
363 struct pollfd ufds[1];
364 ufds[0].fd = socket_;
365 ufds[0].events = POLLIN;
366 ufds[0].revents = 0;
367 while (*timeout_ms > 0) {
368 auto start_time = std::chrono::steady_clock::now();
369 int nr = poll(ufds, 1, *timeout_ms);
370 auto now = std::chrono::steady_clock::now();
371 auto time_elapsed =
372 std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
373 uint64_t millis = time_elapsed.count();
374 *timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis;
375
376 if (nr > 0) {
377 return true;
378 }
379
380 if (nr == 0) {
381 // Timeout
382 break;
383 }
384
385 if (nr < 0 && errno != EINTR) {
386 PLOG(ERROR) << "sys_prop: error waiting for uid " << cred_.uid
387 << " to send property message";
388 return false;
389 } else { // errno == EINTR
390 // Timer rounds milliseconds down in case of EINTR we want it to be rounded up
391 // to avoid slowing init down by causing EINTR with under millisecond timeout.
392 if (*timeout_ms > 0) {
393 --(*timeout_ms);
394 }
395 }
396 }
397
398 LOG(ERROR) << "sys_prop: timeout waiting for uid " << cred_.uid
399 << " to send property message.";
400 return false;
401 }
402
403 bool RecvFully(void* data_ptr, size_t size, uint32_t* timeout_ms) {
404 size_t bytes_left = size;
405 char* data = static_cast<char*>(data_ptr);
406 while (*timeout_ms > 0 && bytes_left > 0) {
407 if (!PollIn(timeout_ms)) {
408 return false;
409 }
410
411 int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
412 if (result <= 0) {
DuXiao40533592018-05-21 14:43:56 +0800413 PLOG(ERROR) << "sys_prop: recv error";
Tom Cherry32228482018-01-18 16:14:25 -0800414 return false;
415 }
416
417 bytes_left -= result;
418 data += result;
419 }
420
DuXiao40533592018-05-21 14:43:56 +0800421 if (bytes_left != 0) {
422 LOG(ERROR) << "sys_prop: recv data is not properly obtained.";
423 }
424
Tom Cherry32228482018-01-18 16:14:25 -0800425 return bytes_left == 0;
426 }
427
428 int socket_;
429 ucred cred_;
430
431 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000432};
433
Tom Cherry5ab2e1c2018-05-03 16:57:19 -0700434bool CheckControlPropertyPerms(const std::string& name, const std::string& value,
435 const std::string& source_context, const ucred& cr) {
436 // We check the legacy method first but these properties are dontaudit, so we only log an audit
437 // if the newer method fails as well. We only do this with the legacy ctl. properties.
438 if (name == "ctl.start" || name == "ctl.stop" || name == "ctl.restart") {
439 // The legacy permissions model is that ctl. properties have their name ctl.<action> and
440 // their value is the name of the service to apply that action to. Permissions for these
441 // actions are based on the service, so we must create a fake name of ctl.<service> to
442 // check permissions.
443 auto control_string_legacy = "ctl." + value;
444 const char* target_context_legacy = nullptr;
445 const char* type_legacy = nullptr;
446 property_info_area->GetPropertyInfo(control_string_legacy.c_str(), &target_context_legacy,
447 &type_legacy);
448
449 if (CheckMacPerms(control_string_legacy, target_context_legacy, source_context.c_str(), cr)) {
450 return true;
451 }
452 }
453
454 auto control_string_full = name + "$" + value;
455 const char* target_context_full = nullptr;
456 const char* type_full = nullptr;
457 property_info_area->GetPropertyInfo(control_string_full.c_str(), &target_context_full,
458 &type_full);
459
460 return CheckMacPerms(control_string_full, target_context_full, source_context.c_str(), cr);
461}
462
Tom Cherry32228482018-01-18 16:14:25 -0800463// This returns one of the enum of PROP_SUCCESS or PROP_ERROR*.
Tom Cherrybe048922019-02-16 12:03:19 -0800464uint32_t CheckPermissions(const std::string& name, const std::string& value,
465 const std::string& source_context, const ucred& cr, std::string* error) {
Tom Cherryde6bd502018-02-13 16:50:08 -0800466 if (!IsLegalPropertyName(name)) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800467 *error = "Illegal property name";
Tom Cherry32228482018-01-18 16:14:25 -0800468 return PROP_ERROR_INVALID_NAME;
469 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000470
Tom Cherry32228482018-01-18 16:14:25 -0800471 if (StartsWith(name, "ctl.")) {
Tom Cherry5ab2e1c2018-05-03 16:57:19 -0700472 if (!CheckControlPropertyPerms(name, value, source_context, cr)) {
473 *error = StringPrintf("Invalid permissions to perform '%s' on '%s'", name.c_str() + 4,
474 value.c_str());
Tom Cherry32228482018-01-18 16:14:25 -0800475 return PROP_ERROR_HANDLE_CONTROL_MESSAGE;
476 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000477
Tom Cherry32228482018-01-18 16:14:25 -0800478 return PROP_SUCCESS;
479 }
Tom Cherry927c5d52017-12-11 01:40:07 -0800480
Tom Cherry32228482018-01-18 16:14:25 -0800481 const char* target_context = nullptr;
482 const char* type = nullptr;
483 property_info_area->GetPropertyInfo(name.c_str(), &target_context, &type);
Tom Cherrya84e14d2017-09-05 12:38:30 -0700484
Tom Cherry32228482018-01-18 16:14:25 -0800485 if (!CheckMacPerms(name, target_context, source_context.c_str(), cr)) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800486 *error = "SELinux permission check failed";
Tom Cherry32228482018-01-18 16:14:25 -0800487 return PROP_ERROR_PERMISSION_DENIED;
488 }
489
490 if (type == nullptr || !CheckType(type, value)) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800491 *error = StringPrintf("Property type check failed, value doesn't match expected type '%s'",
492 (type ?: "(null)"));
Tom Cherry32228482018-01-18 16:14:25 -0800493 return PROP_ERROR_INVALID_VALUE;
494 }
495
Tom Cherrybe048922019-02-16 12:03:19 -0800496 return PROP_SUCCESS;
497}
498
499// This returns one of the enum of PROP_SUCCESS or PROP_ERROR*.
500uint32_t HandlePropertySet(const std::string& name, const std::string& value,
501 const std::string& source_context, const ucred& cr, std::string* error) {
502 if (auto ret = CheckPermissions(name, value, source_context, cr, error); ret != PROP_SUCCESS) {
503 return ret;
504 }
505
506 if (StartsWith(name, "ctl.")) {
507 HandleControlMessage(name.c_str() + 4, value, cr.pid);
508 return PROP_SUCCESS;
509 }
510
Tom Cherry32228482018-01-18 16:14:25 -0800511 // sys.powerctl is a special property that is used to make the device reboot. We want to log
512 // any process that sets this property to be able to accurately blame the cause of a shutdown.
513 if (name == "sys.powerctl") {
514 std::string cmdline_path = StringPrintf("proc/%d/cmdline", cr.pid);
515 std::string process_cmdline;
516 std::string process_log_string;
517 if (ReadFileToString(cmdline_path, &process_cmdline)) {
518 // Since cmdline is null deliminated, .c_str() conveniently gives us just the process
519 // path.
520 process_log_string = StringPrintf(" (%s)", process_cmdline.c_str());
521 }
522 LOG(INFO) << "Received sys.powerctl='" << value << "' from pid: " << cr.pid
523 << process_log_string;
524 }
525
Tom Cherry69d47aa2018-03-01 11:00:57 -0800526 if (name == "selinux.restorecon_recursive") {
527 return PropertySetAsync(name, value, RestoreconRecursiveAsync, error);
528 }
529
530 return PropertySet(name, value, error);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000531}
532
533static void handle_property_set_fd() {
534 static constexpr uint32_t kDefaultSocketTimeout = 2000; /* ms */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535
Robert Sesekca2da602017-01-25 08:08:51 -0500536 int s = accept4(property_set_fd, nullptr, nullptr, SOCK_CLOEXEC);
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700537 if (s == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800538 return;
539 }
540
Tom Cherry32228482018-01-18 16:14:25 -0800541 ucred cr;
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700542 socklen_t cr_size = sizeof(cr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
544 close(s);
Elliott Hughesb005d902017-02-22 14:54:15 -0800545 PLOG(ERROR) << "sys_prop: unable to get SO_PEERCRED";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546 return;
547 }
548
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000549 SocketConnection socket(s, cr);
550 uint32_t timeout_ms = kDefaultSocketTimeout;
551
552 uint32_t cmd = 0;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000553 if (!socket.RecvUint32(&cmd, &timeout_ms)) {
554 PLOG(ERROR) << "sys_prop: error while reading command from the socket";
555 socket.SendUint32(PROP_ERROR_READ_CMD);
JP Abgrall4515d812014-01-31 14:37:07 -0800556 return;
557 }
558
Elliott Hughesb005d902017-02-22 14:54:15 -0800559 switch (cmd) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000560 case PROP_MSG_SETPROP: {
561 char prop_name[PROP_NAME_MAX];
562 char prop_value[PROP_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000564 if (!socket.RecvChars(prop_name, PROP_NAME_MAX, &timeout_ms) ||
565 !socket.RecvChars(prop_value, PROP_VALUE_MAX, &timeout_ms)) {
566 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP): error while reading name/value from the socket";
567 return;
Nick Kralevich69463612013-09-13 17:21:28 -0700568 }
569
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000570 prop_name[PROP_NAME_MAX-1] = 0;
571 prop_value[PROP_VALUE_MAX-1] = 0;
rpcraig63207cd2012-08-09 10:05:49 -0400572
Tom Cherry69d47aa2018-03-01 11:00:57 -0800573 const auto& cr = socket.cred();
574 std::string error;
575 uint32_t result =
576 HandlePropertySet(prop_name, prop_value, socket.source_context(), cr, &error);
577 if (result != PROP_SUCCESS) {
Nick Kralevich9ca898f2019-04-04 10:10:01 -0700578 LOG(ERROR) << "Unable to set property '" << prop_name << "' from uid:" << cr.uid
579 << " gid:" << cr.gid << " pid:" << cr.pid << ": " << error;
Tom Cherry69d47aa2018-03-01 11:00:57 -0800580 }
581
Dimitry Ivanovdee4bd22017-01-19 14:53:00 -0800582 break;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000583 }
Dimitry Ivanov70c4ecf2017-01-24 18:38:09 +0000584
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000585 case PROP_MSG_SETPROP2: {
586 std::string name;
587 std::string value;
588 if (!socket.RecvString(&name, &timeout_ms) ||
589 !socket.RecvString(&value, &timeout_ms)) {
590 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP2): error while reading name/value from the socket";
591 socket.SendUint32(PROP_ERROR_READ_DATA);
592 return;
593 }
594
Tom Cherry69d47aa2018-03-01 11:00:57 -0800595 const auto& cr = socket.cred();
596 std::string error;
597 uint32_t result = HandlePropertySet(name, value, socket.source_context(), cr, &error);
598 if (result != PROP_SUCCESS) {
Nick Kralevich9ca898f2019-04-04 10:10:01 -0700599 LOG(ERROR) << "Unable to set property '" << name << "' from uid:" << cr.uid
600 << " gid:" << cr.gid << " pid:" << cr.pid << ": " << error;
Tom Cherry69d47aa2018-03-01 11:00:57 -0800601 }
Tom Cherry32228482018-01-18 16:14:25 -0800602 socket.SendUint32(result);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000603 break;
604 }
Elliott Hughesb005d902017-02-22 14:54:15 -0800605
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800606 default:
Elliott Hughesb005d902017-02-22 14:54:15 -0800607 LOG(ERROR) << "sys_prop: invalid command " << cmd;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000608 socket.SendUint32(PROP_ERROR_INVALID_CMD);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609 break;
610 }
611}
612
Tom Cherrybe048922019-02-16 12:03:19 -0800613static bool load_properties_from_file(const char*, const char*,
614 std::map<std::string, std::string>*);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800616/*
617 * Filter is used to decide which properties to load: NULL loads all keys,
618 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
619 */
Tom Cherrybe048922019-02-16 12:03:19 -0800620static void LoadProperties(char* data, const char* filter, const char* filename,
621 std::map<std::string, std::string>* properties) {
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800622 char *key, *value, *eol, *sol, *tmp, *fn;
623 size_t flen = 0;
624
Tom Cherrydc375862018-02-28 10:39:01 -0800625 const char* context = kInitContext.c_str();
Tom Cherry40acb372018-08-01 13:41:12 -0700626 if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) {
Tom Cherrya1dbeb82018-04-11 15:50:00 -0700627 for (const auto& [path_prefix, secontext] : paths_and_secontexts) {
628 if (StartsWith(filename, path_prefix)) {
629 context = secontext;
630 }
Tom Cherrydc375862018-02-28 10:39:01 -0800631 }
632 }
633
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800634 if (filter) {
635 flen = strlen(filter);
636 }
637
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800639 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800640 key = sol;
641 *eol++ = 0;
642 sol = eol;
643
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800644 while (isspace(*key)) key++;
645 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800646
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800647 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800648 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800649
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800650 if (!strncmp(key, "import ", 7) && flen == 0) {
651 fn = key + 7;
652 while (isspace(*fn)) fn++;
653
654 key = strchr(fn, ' ');
655 if (key) {
656 *key++ = 0;
657 while (isspace(*key)) key++;
658 }
659
Tom Cherrybe048922019-02-16 12:03:19 -0800660 load_properties_from_file(fn, key, properties);
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800661
662 } else {
663 value = strchr(key, '=');
664 if (!value) continue;
665 *value++ = 0;
666
667 tmp = value - 2;
668 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
669
670 while (isspace(*value)) value++;
671
672 if (flen > 0) {
673 if (filter[flen - 1] == '*') {
674 if (strncmp(key, filter, flen - 1)) continue;
675 } else {
676 if (strcmp(key, filter)) continue;
677 }
678 }
679
Tom Cherrydc375862018-02-28 10:39:01 -0800680 if (StartsWith(key, "ctl.") || key == "sys.powerctl"s ||
681 key == "selinux.restorecon_recursive"s) {
682 LOG(ERROR) << "Ignoring disallowed property '" << key
683 << "' with special meaning in prop file '" << filename << "'";
684 continue;
685 }
686
Tom Cherrydc375862018-02-28 10:39:01 -0800687 ucred cr = {.pid = 1, .uid = 0, .gid = 0};
688 std::string error;
Tom Cherrybe048922019-02-16 12:03:19 -0800689 if (CheckPermissions(key, value, context, cr, &error) == PROP_SUCCESS) {
690 auto it = properties->find(key);
691 if (it == properties->end()) {
692 (*properties)[key] = value;
693 } else if (it->second != value) {
694 LOG(WARNING) << "Overriding previous 'ro.' property '" << key << "':'"
695 << it->second << "' with new value '" << value << "'";
696 it->second = value;
697 }
698 } else {
699 LOG(ERROR) << "Do not have permissions to set '" << key << "' to '" << value
Tom Cherrydc375862018-02-28 10:39:01 -0800700 << "' in property file '" << filename << "': " << error;
701 }
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800702 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703 }
704}
705
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700706// Filter is used to decide which properties to load: NULL loads all keys,
707// "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
Tom Cherrybe048922019-02-16 12:03:19 -0800708static bool load_properties_from_file(const char* filename, const char* filter,
709 std::map<std::string, std::string>* properties) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700710 Timer t;
Tom Cherry62ca6632017-08-03 12:54:07 -0700711 auto file_contents = ReadFile(filename);
712 if (!file_contents) {
713 PLOG(WARNING) << "Couldn't load property file '" << filename
714 << "': " << file_contents.error();
Hung-ying Tyanaef2b092017-06-02 18:59:46 +0800715 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800716 }
Tom Cherry62ca6632017-08-03 12:54:07 -0700717 file_contents->push_back('\n');
Tom Cherrydc375862018-02-28 10:39:01 -0800718
Tom Cherrybe048922019-02-16 12:03:19 -0800719 LoadProperties(file_contents->data(), filter, filename, properties);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000720 LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t << ".)";
Hung-ying Tyanaef2b092017-06-02 18:59:46 +0800721 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722}
723
Jaekyun Seok0cf3a072017-04-24 18:52:54 +0900724// persist.sys.usb.config values can't be combined on build-time when property
725// files are split into each partition.
726// So we need to apply the same rule of build/make/tools/post_process_props.py
727// on runtime.
728static void update_sys_usb_config() {
729 bool is_debuggable = android::base::GetBoolProperty("ro.debuggable", false);
730 std::string config = android::base::GetProperty("persist.sys.usb.config", "");
731 if (config.empty()) {
732 property_set("persist.sys.usb.config", is_debuggable ? "adb" : "none");
733 } else if (is_debuggable && config.find("adb") == std::string::npos &&
734 config.length() + 4 < PROP_VALUE_MAX) {
735 config.append(",adb");
736 property_set("persist.sys.usb.config", config);
737 }
738}
739
Nick Kralevich32b90232012-09-19 11:15:24 -0700740static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800741 if (ALLOW_LOCAL_PROP_OVERRIDE) {
Tom Cherrybe048922019-02-16 12:03:19 -0800742 std::map<std::string, std::string> properties;
743 load_properties_from_file("/data/local.prop", nullptr, &properties);
744 for (const auto& [name, value] : properties) {
745 std::string error;
746 if (PropertySet(name, value, &error) != PROP_SUCCESS) {
747 LOG(ERROR) << "Could not set '" << name << "' to '" << value
748 << "' in /data/local.prop: " << error;
749 }
750 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700751 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700752}
753
Ken Sumrallc5c51032011-03-08 17:01:29 -0800754/* When booting an encrypted system, /data is not mounted when the
755 * property service is started, so any properties stored there are
756 * not loaded. Vold triggers init to load these properties once it
757 * has mounted /data.
758 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700759void load_persist_props(void) {
Tom Cherry9951b792017-08-24 14:01:25 -0700760 // Devices with FDE have load_persist_props called twice; the first time when the temporary
761 // /data partition is mounted and then again once /data is truly mounted. We do not want to
762 // read persistent properties from the temporary /data partition or mark persistent properties
763 // as having been loaded during the first call, so we return in that case.
764 std::string crypto_state = android::base::GetProperty("ro.crypto.state", "");
765 std::string crypto_type = android::base::GetProperty("ro.crypto.type", "");
766 if (crypto_state == "encrypted" && crypto_type == "block") {
767 static size_t num_calls = 0;
768 if (++num_calls == 1) return;
769 }
770
Nick Kralevich32b90232012-09-19 11:15:24 -0700771 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800772 /* Read persistent properties after all default values have been loaded. */
Tom Cherry16fad422017-08-04 15:59:03 -0700773 auto persistent_properties = LoadPersistentProperties();
Tom Cherrya97faba2017-09-15 15:44:04 -0700774 for (const auto& persistent_property_record : persistent_properties.properties()) {
775 property_set(persistent_property_record.name(), persistent_property_record.value());
Tom Cherry16fad422017-08-04 15:59:03 -0700776 }
777 persistent_properties_loaded = true;
Keun-young Park404906d2017-02-28 19:20:38 -0800778 property_set("ro.persistent_properties.ready", "true");
Ken Sumrallc5c51032011-03-08 17:01:29 -0800779}
780
Steven Laver57a740e2019-01-29 20:19:05 -0800781// If the ro.product.[brand|device|manufacturer|model|name] properties have not been explicitly
782// set, derive them from ro.product.${partition}.* properties
783static void property_initialize_ro_product_props() {
784 const char* RO_PRODUCT_PROPS_PREFIX = "ro.product.";
785 const char* RO_PRODUCT_PROPS[] = {
786 "brand", "device", "manufacturer", "model", "name",
787 };
788 const char* RO_PRODUCT_PROPS_ALLOWED_SOURCES[] = {
789 "odm", "product", "product_services", "system", "vendor",
790 };
791 const char* RO_PRODUCT_PROPS_DEFAULT_SOURCE_ORDER =
792 "product,product_services,odm,vendor,system";
793 const std::string EMPTY = "";
794
795 std::string ro_product_props_source_order =
796 GetProperty("ro.product.property_source_order", EMPTY);
797
798 if (!ro_product_props_source_order.empty()) {
799 // Verify that all specified sources are valid
800 for (const auto& source : Split(ro_product_props_source_order, ",")) {
801 // Verify that the specified source is valid
802 bool is_allowed_source = false;
803 for (const auto& allowed_source : RO_PRODUCT_PROPS_ALLOWED_SOURCES) {
804 if (source == allowed_source) {
805 is_allowed_source = true;
806 break;
807 }
808 }
809 if (!is_allowed_source) {
810 LOG(ERROR) << "Found unexpected source in ro.product.property_source_order; "
811 "using the default property source order";
812 ro_product_props_source_order = RO_PRODUCT_PROPS_DEFAULT_SOURCE_ORDER;
813 break;
814 }
815 }
816 } else {
817 ro_product_props_source_order = RO_PRODUCT_PROPS_DEFAULT_SOURCE_ORDER;
818 }
819
820 for (const auto& ro_product_prop : RO_PRODUCT_PROPS) {
821 std::string base_prop(RO_PRODUCT_PROPS_PREFIX);
822 base_prop += ro_product_prop;
823
824 std::string base_prop_val = GetProperty(base_prop, EMPTY);
825 if (!base_prop_val.empty()) {
826 continue;
827 }
828
829 for (const auto& source : Split(ro_product_props_source_order, ",")) {
830 std::string target_prop(RO_PRODUCT_PROPS_PREFIX);
831 target_prop += source;
832 target_prop += '.';
833 target_prop += ro_product_prop;
834
835 std::string target_prop_val = GetProperty(target_prop, EMPTY);
836 if (!target_prop_val.empty()) {
837 LOG(INFO) << "Setting product property " << base_prop << " to '" << target_prop_val
838 << "' (from " << target_prop << ")";
839 std::string error;
840 uint32_t res = PropertySet(base_prop, target_prop_val, &error);
841 if (res != PROP_SUCCESS) {
842 LOG(ERROR) << "Error setting product property " << base_prop << ": err=" << res
843 << " (" << error << ")";
844 }
845 break;
846 }
847 }
848 }
849}
850
851// If the ro.build.fingerprint property has not been set, derive it from constituent pieces
852static void property_derive_build_fingerprint() {
853 std::string build_fingerprint = GetProperty("ro.build.fingerprint", "");
854 if (!build_fingerprint.empty()) {
855 return;
856 }
857
858 const std::string UNKNOWN = "unknown";
859 build_fingerprint = GetProperty("ro.product.brand", UNKNOWN);
860 build_fingerprint += '/';
861 build_fingerprint += GetProperty("ro.product.name", UNKNOWN);
862 build_fingerprint += '/';
863 build_fingerprint += GetProperty("ro.product.device", UNKNOWN);
864 build_fingerprint += ':';
865 build_fingerprint += GetProperty("ro.build.version.release", UNKNOWN);
866 build_fingerprint += '/';
867 build_fingerprint += GetProperty("ro.build.id", UNKNOWN);
868 build_fingerprint += '/';
869 build_fingerprint += GetProperty("ro.build.version.incremental", UNKNOWN);
870 build_fingerprint += ':';
871 build_fingerprint += GetProperty("ro.build.type", UNKNOWN);
872 build_fingerprint += '/';
873 build_fingerprint += GetProperty("ro.build.tags", UNKNOWN);
874
875 LOG(INFO) << "Setting property 'ro.build.fingerprint' to '" << build_fingerprint << "'";
876
877 std::string error;
878 uint32_t res = PropertySet("ro.build.fingerprint", build_fingerprint, &error);
879 if (res != PROP_SUCCESS) {
880 LOG(ERROR) << "Error setting property 'ro.build.fingerprint': err=" << res << " (" << error
881 << ")";
882 }
883}
884
Bowgo Tsai1dacd422019-03-04 17:53:34 +0800885void property_load_boot_defaults(bool load_debug_prop) {
Jiyong Park3b316ee2019-01-13 02:42:31 +0900886 // TODO(b/117892318): merge prop.default and build.prop files into one
Tom Cherrybe048922019-02-16 12:03:19 -0800887 // We read the properties and their values into a map, in order to always allow properties
888 // loaded in the later property files to override the properties in loaded in the earlier
889 // property files, regardless of if they are "ro." properties or not.
890 std::map<std::string, std::string> properties;
891 if (!load_properties_from_file("/system/etc/prop.default", nullptr, &properties)) {
Jiyong Park3b316ee2019-01-13 02:42:31 +0900892 // Try recovery path
Tom Cherrybe048922019-02-16 12:03:19 -0800893 if (!load_properties_from_file("/prop.default", nullptr, &properties)) {
Jiyong Park3b316ee2019-01-13 02:42:31 +0900894 // Try legacy path
Tom Cherrybe048922019-02-16 12:03:19 -0800895 load_properties_from_file("/default.prop", nullptr, &properties);
Jiyong Park3b316ee2019-01-13 02:42:31 +0900896 }
897 }
Tom Cherrybe048922019-02-16 12:03:19 -0800898 load_properties_from_file("/system/build.prop", nullptr, &properties);
899 load_properties_from_file("/vendor/default.prop", nullptr, &properties);
900 load_properties_from_file("/vendor/build.prop", nullptr, &properties);
901 load_properties_from_file("/odm/default.prop", nullptr, &properties);
902 load_properties_from_file("/odm/build.prop", nullptr, &properties);
903 load_properties_from_file("/product/build.prop", nullptr, &properties);
904 load_properties_from_file("/product_services/build.prop", nullptr, &properties);
905 load_properties_from_file("/factory/factory.prop", "ro.*", &properties);
906
Bowgo Tsai1dacd422019-03-04 17:53:34 +0800907 if (load_debug_prop) {
Bowgo Tsai30afda72019-04-11 23:57:24 +0800908 LOG(INFO) << "Loading " << kDebugRamdiskProp;
909 load_properties_from_file(kDebugRamdiskProp, nullptr, &properties);
Bowgo Tsai1dacd422019-03-04 17:53:34 +0800910 }
911
Tom Cherrybe048922019-02-16 12:03:19 -0800912 for (const auto& [name, value] : properties) {
913 std::string error;
914 if (PropertySet(name, value, &error) != PROP_SUCCESS) {
915 LOG(ERROR) << "Could not set '" << name << "' to '" << value
916 << "' while loading .prop files" << error;
917 }
918 }
Jiyong Park3b316ee2019-01-13 02:42:31 +0900919
Steven Laver57a740e2019-01-29 20:19:05 -0800920 property_initialize_ro_product_props();
921 property_derive_build_fingerprint();
922
Jiyong Park3b316ee2019-01-13 02:42:31 +0900923 update_sys_usb_config();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700924}
925
Tom Cherry2ae2f602017-12-14 01:58:17 +0000926bool LoadPropertyInfoFromFile(const std::string& filename,
927 std::vector<PropertyInfoEntry>* property_infos) {
928 auto file_contents = std::string();
929 if (!ReadFileToString(filename, &file_contents)) {
930 PLOG(ERROR) << "Could not read properties from '" << filename << "'";
931 return false;
932 }
933
Tom Cherry919458c2018-01-03 14:39:28 -0800934 auto errors = std::vector<std::string>{};
935 ParsePropertyInfoFile(file_contents, property_infos, &errors);
936 // Individual parsing errors are reported but do not cause a failed boot, which is what
937 // returning false would do here.
938 for (const auto& error : errors) {
939 LOG(ERROR) << "Could not read line from '" << filename << "': " << error;
Tom Cherry2ae2f602017-12-14 01:58:17 +0000940 }
Tom Cherry919458c2018-01-03 14:39:28 -0800941
Tom Cherry2ae2f602017-12-14 01:58:17 +0000942 return true;
943}
944
945void CreateSerializedPropertyInfo() {
946 auto property_infos = std::vector<PropertyInfoEntry>();
947 if (access("/system/etc/selinux/plat_property_contexts", R_OK) != -1) {
948 if (!LoadPropertyInfoFromFile("/system/etc/selinux/plat_property_contexts",
949 &property_infos)) {
950 return;
951 }
952 // Don't check for failure here, so we always have a sane list of properties.
953 // E.g. In case of recovery, the vendor partition will not have mounted and we
954 // still need the system / platform properties to function.
Bowgo Tsai36cf3532017-12-07 16:05:25 +0800955 if (!LoadPropertyInfoFromFile("/vendor/etc/selinux/vendor_property_contexts",
956 &property_infos)) {
957 // Fallback to nonplat_* if vendor_* doesn't exist.
958 LoadPropertyInfoFromFile("/vendor/etc/selinux/nonplat_property_contexts",
959 &property_infos);
960 }
Jinguang Dongf42e08d2019-02-15 16:56:28 +0800961 if (access("/product/etc/selinux/product_property_contexts", R_OK) != -1) {
962 LoadPropertyInfoFromFile("/product/etc/selinux/product_property_contexts",
963 &property_infos);
964 }
965 if (access("/odm/etc/selinux/odm_property_contexts", R_OK) != -1) {
966 LoadPropertyInfoFromFile("/odm/etc/selinux/odm_property_contexts", &property_infos);
967 }
Tom Cherry2ae2f602017-12-14 01:58:17 +0000968 } else {
969 if (!LoadPropertyInfoFromFile("/plat_property_contexts", &property_infos)) {
970 return;
971 }
Bowgo Tsai36cf3532017-12-07 16:05:25 +0800972 if (!LoadPropertyInfoFromFile("/vendor_property_contexts", &property_infos)) {
973 // Fallback to nonplat_* if vendor_* doesn't exist.
974 LoadPropertyInfoFromFile("/nonplat_property_contexts", &property_infos);
975 }
Jinguang Dongf42e08d2019-02-15 16:56:28 +0800976 LoadPropertyInfoFromFile("/product_property_contexts", &property_infos);
977 LoadPropertyInfoFromFile("/odm_property_contexts", &property_infos);
Tom Cherry2ae2f602017-12-14 01:58:17 +0000978 }
Tom Cherry927c5d52017-12-11 01:40:07 -0800979
Tom Cherry2ae2f602017-12-14 01:58:17 +0000980 auto serialized_contexts = std::string();
981 auto error = std::string();
Tom Cherry927c5d52017-12-11 01:40:07 -0800982 if (!BuildTrie(property_infos, "u:object_r:default_prop:s0", "string", &serialized_contexts,
Tom Cherry2ae2f602017-12-14 01:58:17 +0000983 &error)) {
984 LOG(ERROR) << "Unable to serialize property contexts: " << error;
985 return;
986 }
987
988 constexpr static const char kPropertyInfosPath[] = "/dev/__properties__/property_info";
989 if (!WriteStringToFile(serialized_contexts, kPropertyInfosPath, 0444, 0, 0, false)) {
990 PLOG(ERROR) << "Unable to write serialized property infos to file";
991 }
992 selinux_android_restorecon(kPropertyInfosPath, 0);
993}
994
Mark Salyzyn6c6ec722015-10-24 16:20:18 -0700995void StartPropertyService(Epoll* epoll) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000996 property_set("ro.property_service.version", "2");
997
Mark Salyzynb066fcc2017-05-05 14:44:35 -0700998 property_set_fd = CreateSocket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
Tom Cherryc3692b32017-08-10 12:22:44 -0700999 false, 0666, 0, 0, nullptr);
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001000 if (property_set_fd == -1) {
Tom Cherry4a679452017-09-26 16:30:03 -07001001 PLOG(FATAL) << "start_property_service socket creation failed";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001002 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001003
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001004 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -07001005
Mark Salyzyn6c6ec722015-10-24 16:20:18 -07001006 if (auto result = epoll->RegisterHandler(property_set_fd, handle_property_set_fd); !result) {
1007 PLOG(FATAL) << result.error();
1008 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001009}
Tom Cherry81f5d3e2017-06-22 12:53:17 -07001010
1011} // namespace init
1012} // namespace android