blob: 53288697dda57114e01237b820c82725fa4e27e3 [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 Cherry3f5eaae52017-04-06 16:30:22 -070042#include <memory>
Tom Marshall62696902017-06-09 18:29:23 +000043#include <queue>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070044#include <vector>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
Tom Cherryede0d532017-07-06 14:20:11 -070046#include <android-base/chrono_utils.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080047#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070048#include <android-base/logging.h>
Jaekyun Seok0cf3a072017-04-24 18:52:54 +090049#include <android-base/properties.h>
Tom Cherrya84e14d2017-09-05 12:38:30 -070050#include <android-base/stringprintf.h>
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000051#include <android-base/strings.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070052#include <bootimg.h>
53#include <fs_mgr.h>
Tom Cherry2ae2f602017-12-14 01:58:17 +000054#include <property_info_parser/property_info_parser.h>
55#include <property_info_serializer/property_info_serializer.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070056#include <selinux/android.h>
57#include <selinux/label.h>
58#include <selinux/selinux.h>
Andres Moralesdb5f5d42015-05-08 08:30:33 -070059
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
Tom Cherry2ae2f602017-12-14 01:58:17 +000070using android::base::ReadFileToString;
71using android::base::Split;
Tom Cherry1cf8d692017-10-10 13:35:01 -070072using android::base::StartsWith;
Tom Cherrya84e14d2017-09-05 12:38:30 -070073using android::base::StringPrintf;
Tom Cherryede0d532017-07-06 14:20:11 -070074using android::base::Timer;
Tom Cherry2ae2f602017-12-14 01:58:17 +000075using android::base::Trim;
76using android::base::WriteStringToFile;
77using android::properties::BuildTrie;
Tom Cherry919458c2018-01-03 14:39:28 -080078using android::properties::ParsePropertyInfoFile;
Tom Cherry2ae2f602017-12-14 01:58:17 +000079using android::properties::PropertyInfoAreaFile;
80using android::properties::PropertyInfoEntry;
Tom Cherryede0d532017-07-06 14:20:11 -070081
Andres Moralesdb5f5d42015-05-08 08:30:33 -070082#define RECOVERY_MOUNT_POINT "/recovery"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083
Tom Cherry81f5d3e2017-06-22 12:53:17 -070084namespace android {
85namespace init {
86
Tom Cherry16fad422017-08-04 15:59:03 -070087static bool persistent_properties_loaded = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088
Colin Crossd11beb22010-04-13 19:33:37 -070089static int property_set_fd = -1;
90
Tom Cherry2ae2f602017-12-14 01:58:17 +000091static PropertyInfoAreaFile property_info_area;
92
Tom Cherry32228482018-01-18 16:14:25 -080093uint32_t InitPropertySet(const std::string& name, const std::string& value);
94
95uint32_t (*property_set)(const std::string& name, const std::string& value) = InitPropertySet;
96
Tom Cherry2ae2f602017-12-14 01:58:17 +000097void CreateSerializedPropertyInfo();
Tom Cherryc3692b32017-08-10 12:22:44 -070098
Tom Cherry5ab2e1c2018-05-03 16:57:19 -070099struct PropertyAuditData {
100 const ucred* cr;
101 const char* name;
102};
103
Elliott Hughesda40c002015-03-27 23:20:44 -0700104void property_init() {
Tom Cherry2ae2f602017-12-14 01:58:17 +0000105 mkdir("/dev/__properties__", S_IRWXU | S_IXGRP | S_IXOTH);
106 CreateSerializedPropertyInfo();
Elliott Hughesda40c002015-03-27 23:20:44 -0700107 if (__system_property_area_init()) {
Tom Cherry4a679452017-09-26 16:30:03 -0700108 LOG(FATAL) << "Failed to initialize property area";
Elliott Hughesda40c002015-03-27 23:20:44 -0700109 }
Tom Cherry2ae2f602017-12-14 01:58:17 +0000110 if (!property_info_area.LoadDefaultPath()) {
111 LOG(FATAL) << "Failed to load serialized property info file";
112 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113}
Tom Cherryb35f8272018-10-22 14:50:52 -0700114
115bool CanReadProperty(const std::string& source_context, const std::string& name) {
116 const char* target_context = nullptr;
117 property_info_area->GetPropertyInfo(name.c_str(), &target_context, nullptr);
118
119 PropertyAuditData audit_data;
120
121 audit_data.name = name.c_str();
122
123 ucred cr = {.pid = 0, .uid = 0, .gid = 0};
124 audit_data.cr = &cr;
125
126 return selinux_check_access(source_context.c_str(), target_context, "file", "read",
127 &audit_data) == 0;
128}
129
Tom Cherry927c5d52017-12-11 01:40:07 -0800130static bool CheckMacPerms(const std::string& name, const char* target_context,
Tom Cherry32228482018-01-18 16:14:25 -0800131 const char* source_context, const ucred& cr) {
Tom Cherry927c5d52017-12-11 01:40:07 -0800132 if (!target_context || !source_context) {
Tom Cherry2ae2f602017-12-14 01:58:17 +0000133 return false;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000134 }
135
Tom Cherry5ab2e1c2018-05-03 16:57:19 -0700136 PropertyAuditData audit_data;
rpcraig63207cd2012-08-09 10:05:49 -0400137
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000138 audit_data.name = name.c_str();
Tom Cherry32228482018-01-18 16:14:25 -0800139 audit_data.cr = &cr;
William Robertsd7aea442015-10-01 16:03:47 -0700140
Tom Cherry927c5d52017-12-11 01:40:07 -0800141 bool has_access = (selinux_check_access(source_context, target_context, "property_service",
142 "set", &audit_data) == 0);
rpcraig63207cd2012-08-09 10:05:49 -0400143
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000144 return has_access;
rpcraig63207cd2012-08-09 10:05:49 -0400145}
146
Tom Cherry69d47aa2018-03-01 11:00:57 -0800147static uint32_t PropertySet(const std::string& name, const std::string& value, std::string* error) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000148 size_t valuelen = value.size();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149
Tom Cherryde6bd502018-02-13 16:50:08 -0800150 if (!IsLegalPropertyName(name)) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800151 *error = "Illegal property name";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000152 return PROP_ERROR_INVALID_NAME;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000153 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000154
Tom Cherry1cf8d692017-10-10 13:35:01 -0700155 if (valuelen >= PROP_VALUE_MAX && !StartsWith(name, "ro.")) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800156 *error = "Property value too long";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000157 return PROP_ERROR_INVALID_VALUE;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000158 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159
Tom Cherry8702dcb2017-10-13 16:20:19 -0700160 if (mbstowcs(nullptr, value.data(), 0) == static_cast<std::size_t>(-1)) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800161 *error = "Value is not a UTF8 encoded string";
Tom Cherry8702dcb2017-10-13 16:20:19 -0700162 return PROP_ERROR_INVALID_VALUE;
163 }
164
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000165 prop_info* pi = (prop_info*) __system_property_find(name.c_str());
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000166 if (pi != nullptr) {
167 // ro.* properties are actually "write-once".
Tom Cherry1cf8d692017-10-10 13:35:01 -0700168 if (StartsWith(name, "ro.")) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800169 *error = "Read-only property was already set";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000170 return PROP_ERROR_READ_ONLY_PROPERTY;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000171 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000173 __system_property_update(pi, value.c_str(), valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 } else {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000175 int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700176 if (rc < 0) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800177 *error = "__system_property_add failed";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000178 return PROP_ERROR_SET_FAILED;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200179 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000181
Elliott Hughes4f915812016-12-05 13:12:48 -0800182 // Don't write properties to disk until after we have read all default
183 // properties to prevent them from being overwritten by default values.
Tom Cherry1cf8d692017-10-10 13:35:01 -0700184 if (persistent_properties_loaded && StartsWith(name, "persist.")) {
Tom Cherry16fad422017-08-04 15:59:03 -0700185 WritePersistentProperty(name, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700187 property_changed(name, value);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000188 return PROP_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189}
190
Tom Marshall62696902017-06-09 18:29:23 +0000191typedef int (*PropertyAsyncFunc)(const std::string&, const std::string&);
192
193struct PropertyChildInfo {
194 pid_t pid;
195 PropertyAsyncFunc func;
196 std::string name;
197 std::string value;
198};
199
200static std::queue<PropertyChildInfo> property_children;
201
202static void PropertyChildLaunch() {
203 auto& info = property_children.front();
204 pid_t pid = fork();
205 if (pid < 0) {
206 LOG(ERROR) << "Failed to fork for property_set_async";
207 while (!property_children.empty()) {
208 property_children.pop();
209 }
210 return;
211 }
212 if (pid != 0) {
213 info.pid = pid;
214 } else {
215 if (info.func(info.name, info.value) != 0) {
216 LOG(ERROR) << "property_set_async(\"" << info.name << "\", \"" << info.value
217 << "\") failed";
218 }
Tom Cherry4a679452017-09-26 16:30:03 -0700219 _exit(0);
Tom Marshall62696902017-06-09 18:29:23 +0000220 }
221}
222
223bool PropertyChildReap(pid_t pid) {
224 if (property_children.empty()) {
225 return false;
226 }
227 auto& info = property_children.front();
228 if (info.pid != pid) {
229 return false;
230 }
Tom Cherry69d47aa2018-03-01 11:00:57 -0800231 std::string error;
232 if (PropertySet(info.name, info.value, &error) != PROP_SUCCESS) {
233 LOG(ERROR) << "Failed to set async property " << info.name << " to " << info.value << ": "
234 << error;
Tom Marshall62696902017-06-09 18:29:23 +0000235 }
236 property_children.pop();
237 if (!property_children.empty()) {
238 PropertyChildLaunch();
239 }
240 return true;
241}
242
243static uint32_t PropertySetAsync(const std::string& name, const std::string& value,
Tom Cherry69d47aa2018-03-01 11:00:57 -0800244 PropertyAsyncFunc func, std::string* error) {
Tom Marshall62696902017-06-09 18:29:23 +0000245 if (value.empty()) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800246 return PropertySet(name, value, error);
Tom Marshall62696902017-06-09 18:29:23 +0000247 }
248
249 PropertyChildInfo info;
250 info.func = func;
251 info.name = name;
252 info.value = value;
253 property_children.push(info);
254 if (property_children.size() == 1) {
255 PropertyChildLaunch();
256 }
257 return PROP_SUCCESS;
258}
259
260static int RestoreconRecursiveAsync(const std::string& name, const std::string& value) {
261 return selinux_android_restorecon(value.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
262}
263
Tom Cherry32228482018-01-18 16:14:25 -0800264uint32_t InitPropertySet(const std::string& name, const std::string& value) {
265 if (StartsWith(name, "ctl.")) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800266 LOG(ERROR) << "InitPropertySet: Do not set ctl. properties from init; call the Service "
267 "functions directly";
268 return PROP_ERROR_INVALID_NAME;
269 }
270 if (name == "selinux.restorecon_recursive") {
271 LOG(ERROR) << "InitPropertySet: Do not set selinux.restorecon_recursive from init; use the "
272 "restorecon builtin directly";
Tom Cherry32228482018-01-18 16:14:25 -0800273 return PROP_ERROR_INVALID_NAME;
274 }
275
Tom Cherry69d47aa2018-03-01 11:00:57 -0800276 uint32_t result = 0;
277 ucred cr = {.pid = 1, .uid = 0, .gid = 0};
278 std::string error;
279 result = HandlePropertySet(name, value, kInitContext.c_str(), cr, &error);
280 if (result != PROP_SUCCESS) {
281 LOG(ERROR) << "Init cannot set '" << name << "' to '" << value << "': " << error;
Tom Cherry32228482018-01-18 16:14:25 -0800282 }
283
Tom Cherry69d47aa2018-03-01 11:00:57 -0800284 return result;
Tom Cherry32228482018-01-18 16:14:25 -0800285}
286
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000287class SocketConnection {
Tom Cherry32228482018-01-18 16:14:25 -0800288 public:
289 SocketConnection(int socket, const ucred& cred) : socket_(socket), cred_(cred) {}
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000290
Tom Cherry32228482018-01-18 16:14:25 -0800291 ~SocketConnection() { close(socket_); }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000292
Tom Cherry32228482018-01-18 16:14:25 -0800293 bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
294 return RecvFully(value, sizeof(*value), timeout_ms);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000295 }
296
Tom Cherry32228482018-01-18 16:14:25 -0800297 bool RecvChars(char* chars, size_t size, uint32_t* timeout_ms) {
298 return RecvFully(chars, size, timeout_ms);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000299 }
300
Tom Cherry32228482018-01-18 16:14:25 -0800301 bool RecvString(std::string* value, uint32_t* timeout_ms) {
302 uint32_t len = 0;
303 if (!RecvUint32(&len, timeout_ms)) {
304 return false;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000305 }
Tom Cherry32228482018-01-18 16:14:25 -0800306
307 if (len == 0) {
308 *value = "";
309 return true;
310 }
311
312 // http://b/35166374: don't allow init to make arbitrarily large allocations.
313 if (len > 0xffff) {
314 LOG(ERROR) << "sys_prop: RecvString asked to read huge string: " << len;
315 errno = ENOMEM;
316 return false;
317 }
318
319 std::vector<char> chars(len);
320 if (!RecvChars(&chars[0], len, timeout_ms)) {
321 return false;
322 }
323
324 *value = std::string(&chars[0], len);
325 return true;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000326 }
327
Tom Cherry32228482018-01-18 16:14:25 -0800328 bool SendUint32(uint32_t value) {
329 int result = TEMP_FAILURE_RETRY(send(socket_, &value, sizeof(value), 0));
330 return result == sizeof(value);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000331 }
332
Tom Cherry32228482018-01-18 16:14:25 -0800333 int socket() { return socket_; }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000334
Tom Cherry32228482018-01-18 16:14:25 -0800335 const ucred& cred() { return cred_; }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000336
Tom Cherry32228482018-01-18 16:14:25 -0800337 std::string source_context() const {
338 char* source_context = nullptr;
339 getpeercon(socket_, &source_context);
340 std::string result = source_context;
341 freecon(source_context);
342 return result;
343 }
344
345 private:
346 bool PollIn(uint32_t* timeout_ms) {
347 struct pollfd ufds[1];
348 ufds[0].fd = socket_;
349 ufds[0].events = POLLIN;
350 ufds[0].revents = 0;
351 while (*timeout_ms > 0) {
352 auto start_time = std::chrono::steady_clock::now();
353 int nr = poll(ufds, 1, *timeout_ms);
354 auto now = std::chrono::steady_clock::now();
355 auto time_elapsed =
356 std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
357 uint64_t millis = time_elapsed.count();
358 *timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis;
359
360 if (nr > 0) {
361 return true;
362 }
363
364 if (nr == 0) {
365 // Timeout
366 break;
367 }
368
369 if (nr < 0 && errno != EINTR) {
370 PLOG(ERROR) << "sys_prop: error waiting for uid " << cred_.uid
371 << " 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
383 << " to send property message.";
384 return false;
385 }
386
387 bool RecvFully(void* data_ptr, size_t size, uint32_t* timeout_ms) {
388 size_t bytes_left = size;
389 char* data = static_cast<char*>(data_ptr);
390 while (*timeout_ms > 0 && bytes_left > 0) {
391 if (!PollIn(timeout_ms)) {
392 return false;
393 }
394
395 int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
396 if (result <= 0) {
DuXiao40533592018-05-21 14:43:56 +0800397 PLOG(ERROR) << "sys_prop: recv error";
Tom Cherry32228482018-01-18 16:14:25 -0800398 return false;
399 }
400
401 bytes_left -= result;
402 data += result;
403 }
404
DuXiao40533592018-05-21 14:43:56 +0800405 if (bytes_left != 0) {
406 LOG(ERROR) << "sys_prop: recv data is not properly obtained.";
407 }
408
Tom Cherry32228482018-01-18 16:14:25 -0800409 return bytes_left == 0;
410 }
411
412 int socket_;
413 ucred cred_;
414
415 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000416};
417
Tom Cherry5ab2e1c2018-05-03 16:57:19 -0700418bool CheckControlPropertyPerms(const std::string& name, const std::string& value,
419 const std::string& source_context, const ucred& cr) {
420 // We check the legacy method first but these properties are dontaudit, so we only log an audit
421 // if the newer method fails as well. We only do this with the legacy ctl. properties.
422 if (name == "ctl.start" || name == "ctl.stop" || name == "ctl.restart") {
423 // The legacy permissions model is that ctl. properties have their name ctl.<action> and
424 // their value is the name of the service to apply that action to. Permissions for these
425 // actions are based on the service, so we must create a fake name of ctl.<service> to
426 // check permissions.
427 auto control_string_legacy = "ctl." + value;
428 const char* target_context_legacy = nullptr;
429 const char* type_legacy = nullptr;
430 property_info_area->GetPropertyInfo(control_string_legacy.c_str(), &target_context_legacy,
431 &type_legacy);
432
433 if (CheckMacPerms(control_string_legacy, target_context_legacy, source_context.c_str(), cr)) {
434 return true;
435 }
436 }
437
438 auto control_string_full = name + "$" + value;
439 const char* target_context_full = nullptr;
440 const char* type_full = nullptr;
441 property_info_area->GetPropertyInfo(control_string_full.c_str(), &target_context_full,
442 &type_full);
443
444 return CheckMacPerms(control_string_full, target_context_full, source_context.c_str(), cr);
445}
446
Tom Cherry32228482018-01-18 16:14:25 -0800447// This returns one of the enum of PROP_SUCCESS or PROP_ERROR*.
448uint32_t HandlePropertySet(const std::string& name, const std::string& value,
Tom Cherry69d47aa2018-03-01 11:00:57 -0800449 const std::string& source_context, const ucred& cr, std::string* error) {
Tom Cherryde6bd502018-02-13 16:50:08 -0800450 if (!IsLegalPropertyName(name)) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800451 *error = "Illegal property name";
Tom Cherry32228482018-01-18 16:14:25 -0800452 return PROP_ERROR_INVALID_NAME;
453 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000454
Tom Cherry32228482018-01-18 16:14:25 -0800455 if (StartsWith(name, "ctl.")) {
Tom Cherry5ab2e1c2018-05-03 16:57:19 -0700456 if (!CheckControlPropertyPerms(name, value, source_context, cr)) {
457 *error = StringPrintf("Invalid permissions to perform '%s' on '%s'", name.c_str() + 4,
458 value.c_str());
Tom Cherry32228482018-01-18 16:14:25 -0800459 return PROP_ERROR_HANDLE_CONTROL_MESSAGE;
460 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000461
Tom Cherry6f2d56d2018-02-21 10:37:44 -0800462 HandleControlMessage(name.c_str() + 4, value, cr.pid);
Tom Cherry32228482018-01-18 16:14:25 -0800463 return PROP_SUCCESS;
464 }
Tom Cherry927c5d52017-12-11 01:40:07 -0800465
Tom Cherry32228482018-01-18 16:14:25 -0800466 const char* target_context = nullptr;
467 const char* type = nullptr;
468 property_info_area->GetPropertyInfo(name.c_str(), &target_context, &type);
Tom Cherrya84e14d2017-09-05 12:38:30 -0700469
Tom Cherry32228482018-01-18 16:14:25 -0800470 if (!CheckMacPerms(name, target_context, source_context.c_str(), cr)) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800471 *error = "SELinux permission check failed";
Tom Cherry32228482018-01-18 16:14:25 -0800472 return PROP_ERROR_PERMISSION_DENIED;
473 }
474
475 if (type == nullptr || !CheckType(type, value)) {
Tom Cherry69d47aa2018-03-01 11:00:57 -0800476 *error = StringPrintf("Property type check failed, value doesn't match expected type '%s'",
477 (type ?: "(null)"));
Tom Cherry32228482018-01-18 16:14:25 -0800478 return PROP_ERROR_INVALID_VALUE;
479 }
480
481 // sys.powerctl is a special property that is used to make the device reboot. We want to log
482 // any process that sets this property to be able to accurately blame the cause of a shutdown.
483 if (name == "sys.powerctl") {
484 std::string cmdline_path = StringPrintf("proc/%d/cmdline", cr.pid);
485 std::string process_cmdline;
486 std::string process_log_string;
487 if (ReadFileToString(cmdline_path, &process_cmdline)) {
488 // Since cmdline is null deliminated, .c_str() conveniently gives us just the process
489 // path.
490 process_log_string = StringPrintf(" (%s)", process_cmdline.c_str());
491 }
492 LOG(INFO) << "Received sys.powerctl='" << value << "' from pid: " << cr.pid
493 << process_log_string;
494 }
495
Tom Cherry69d47aa2018-03-01 11:00:57 -0800496 if (name == "selinux.restorecon_recursive") {
497 return PropertySetAsync(name, value, RestoreconRecursiveAsync, error);
498 }
499
500 return PropertySet(name, value, error);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000501}
502
503static void handle_property_set_fd() {
504 static constexpr uint32_t kDefaultSocketTimeout = 2000; /* ms */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505
Robert Sesekca2da602017-01-25 08:08:51 -0500506 int s = accept4(property_set_fd, nullptr, nullptr, SOCK_CLOEXEC);
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700507 if (s == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 return;
509 }
510
Tom Cherry32228482018-01-18 16:14:25 -0800511 ucred cr;
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700512 socklen_t cr_size = sizeof(cr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
514 close(s);
Elliott Hughesb005d902017-02-22 14:54:15 -0800515 PLOG(ERROR) << "sys_prop: unable to get SO_PEERCRED";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516 return;
517 }
518
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000519 SocketConnection socket(s, cr);
520 uint32_t timeout_ms = kDefaultSocketTimeout;
521
522 uint32_t cmd = 0;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000523 if (!socket.RecvUint32(&cmd, &timeout_ms)) {
524 PLOG(ERROR) << "sys_prop: error while reading command from the socket";
525 socket.SendUint32(PROP_ERROR_READ_CMD);
JP Abgrall4515d812014-01-31 14:37:07 -0800526 return;
527 }
528
Elliott Hughesb005d902017-02-22 14:54:15 -0800529 switch (cmd) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000530 case PROP_MSG_SETPROP: {
531 char prop_name[PROP_NAME_MAX];
532 char prop_value[PROP_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000534 if (!socket.RecvChars(prop_name, PROP_NAME_MAX, &timeout_ms) ||
535 !socket.RecvChars(prop_value, PROP_VALUE_MAX, &timeout_ms)) {
536 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP): error while reading name/value from the socket";
537 return;
Nick Kralevich69463612013-09-13 17:21:28 -0700538 }
539
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000540 prop_name[PROP_NAME_MAX-1] = 0;
541 prop_value[PROP_VALUE_MAX-1] = 0;
rpcraig63207cd2012-08-09 10:05:49 -0400542
Tom Cherry69d47aa2018-03-01 11:00:57 -0800543 const auto& cr = socket.cred();
544 std::string error;
545 uint32_t result =
546 HandlePropertySet(prop_name, prop_value, socket.source_context(), cr, &error);
547 if (result != PROP_SUCCESS) {
548 LOG(ERROR) << "Unable to set property '" << prop_name << "' to '" << prop_value
549 << "' from uid:" << cr.uid << " gid:" << cr.gid << " pid:" << cr.pid << ": "
550 << error;
551 }
552
Dimitry Ivanovdee4bd22017-01-19 14:53:00 -0800553 break;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000554 }
Dimitry Ivanov70c4ecf2017-01-24 18:38:09 +0000555
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000556 case PROP_MSG_SETPROP2: {
557 std::string name;
558 std::string value;
559 if (!socket.RecvString(&name, &timeout_ms) ||
560 !socket.RecvString(&value, &timeout_ms)) {
561 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP2): error while reading name/value from the socket";
562 socket.SendUint32(PROP_ERROR_READ_DATA);
563 return;
564 }
565
Tom Cherry69d47aa2018-03-01 11:00:57 -0800566 const auto& cr = socket.cred();
567 std::string error;
568 uint32_t result = HandlePropertySet(name, value, socket.source_context(), cr, &error);
569 if (result != PROP_SUCCESS) {
570 LOG(ERROR) << "Unable to set property '" << name << "' to '" << value
571 << "' from uid:" << cr.uid << " gid:" << cr.gid << " pid:" << cr.pid << ": "
572 << error;
573 }
Tom Cherry32228482018-01-18 16:14:25 -0800574 socket.SendUint32(result);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000575 break;
576 }
Elliott Hughesb005d902017-02-22 14:54:15 -0800577
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800578 default:
Elliott Hughesb005d902017-02-22 14:54:15 -0800579 LOG(ERROR) << "sys_prop: invalid command " << cmd;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000580 socket.SendUint32(PROP_ERROR_INVALID_CMD);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581 break;
582 }
583}
584
Hung-ying Tyanaef2b092017-06-02 18:59:46 +0800585static bool load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800586
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800587/*
588 * Filter is used to decide which properties to load: NULL loads all keys,
589 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
590 */
Tom Cherrydc375862018-02-28 10:39:01 -0800591static void LoadProperties(char* data, const char* filter, const char* filename) {
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800592 char *key, *value, *eol, *sol, *tmp, *fn;
593 size_t flen = 0;
594
Tom Cherrydc375862018-02-28 10:39:01 -0800595 const char* context = kInitContext.c_str();
Tom Cherry40acb372018-08-01 13:41:12 -0700596 if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) {
Tom Cherrya1dbeb82018-04-11 15:50:00 -0700597 for (const auto& [path_prefix, secontext] : paths_and_secontexts) {
598 if (StartsWith(filename, path_prefix)) {
599 context = secontext;
600 }
Tom Cherrydc375862018-02-28 10:39:01 -0800601 }
602 }
603
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800604 if (filter) {
605 flen = strlen(filter);
606 }
607
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800608 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800609 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800610 key = sol;
611 *eol++ = 0;
612 sol = eol;
613
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800614 while (isspace(*key)) key++;
615 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800616
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800617 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800618 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800619
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800620 if (!strncmp(key, "import ", 7) && flen == 0) {
621 fn = key + 7;
622 while (isspace(*fn)) fn++;
623
624 key = strchr(fn, ' ');
625 if (key) {
626 *key++ = 0;
627 while (isspace(*key)) key++;
628 }
629
630 load_properties_from_file(fn, key);
631
632 } else {
633 value = strchr(key, '=');
634 if (!value) continue;
635 *value++ = 0;
636
637 tmp = value - 2;
638 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
639
640 while (isspace(*value)) value++;
641
642 if (flen > 0) {
643 if (filter[flen - 1] == '*') {
644 if (strncmp(key, filter, flen - 1)) continue;
645 } else {
646 if (strcmp(key, filter)) continue;
647 }
648 }
649
Tom Cherrydc375862018-02-28 10:39:01 -0800650 if (StartsWith(key, "ctl.") || key == "sys.powerctl"s ||
651 key == "selinux.restorecon_recursive"s) {
652 LOG(ERROR) << "Ignoring disallowed property '" << key
653 << "' with special meaning in prop file '" << filename << "'";
654 continue;
655 }
656
657 uint32_t result = 0;
658 ucred cr = {.pid = 1, .uid = 0, .gid = 0};
659 std::string error;
660 result = HandlePropertySet(key, value, context, cr, &error);
661 if (result != PROP_SUCCESS) {
662 LOG(ERROR) << "Unable to set property '" << key << "' to '" << value
663 << "' in property file '" << filename << "': " << error;
664 }
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800665 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666 }
667}
668
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700669// Filter is used to decide which properties to load: NULL loads all keys,
670// "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
Hung-ying Tyanaef2b092017-06-02 18:59:46 +0800671static bool load_properties_from_file(const char* filename, const char* filter) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700672 Timer t;
Tom Cherry62ca6632017-08-03 12:54:07 -0700673 auto file_contents = ReadFile(filename);
674 if (!file_contents) {
675 PLOG(WARNING) << "Couldn't load property file '" << filename
676 << "': " << file_contents.error();
Hung-ying Tyanaef2b092017-06-02 18:59:46 +0800677 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678 }
Tom Cherry62ca6632017-08-03 12:54:07 -0700679 file_contents->push_back('\n');
Tom Cherrydc375862018-02-28 10:39:01 -0800680
681 LoadProperties(file_contents->data(), filter, filename);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000682 LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t << ".)";
Hung-ying Tyanaef2b092017-06-02 18:59:46 +0800683 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800684}
685
Jaekyun Seok0cf3a072017-04-24 18:52:54 +0900686// persist.sys.usb.config values can't be combined on build-time when property
687// files are split into each partition.
688// So we need to apply the same rule of build/make/tools/post_process_props.py
689// on runtime.
690static void update_sys_usb_config() {
691 bool is_debuggable = android::base::GetBoolProperty("ro.debuggable", false);
692 std::string config = android::base::GetProperty("persist.sys.usb.config", "");
693 if (config.empty()) {
694 property_set("persist.sys.usb.config", is_debuggable ? "adb" : "none");
695 } else if (is_debuggable && config.find("adb") == std::string::npos &&
696 config.length() + 4 < PROP_VALUE_MAX) {
697 config.append(",adb");
698 property_set("persist.sys.usb.config", config);
699 }
700}
701
Elliott Hughesda40c002015-03-27 23:20:44 -0700702void property_load_boot_defaults() {
Hung-ying Tyanaef2b092017-06-02 18:59:46 +0800703 if (!load_properties_from_file("/system/etc/prop.default", NULL)) {
704 // Try recovery path
705 if (!load_properties_from_file("/prop.default", NULL)) {
706 // Try legacy path
707 load_properties_from_file("/default.prop", NULL);
708 }
709 }
Jaekyun Seokdff165d2017-11-28 12:10:10 +0900710 load_properties_from_file("/product/build.prop", NULL);
Dario Freniab5583b2018-08-17 01:01:25 +0100711 load_properties_from_file("/product_services/build.prop", NULL);
Hung-ying Tyan33463382017-05-25 19:18:17 +0800712 load_properties_from_file("/odm/default.prop", NULL);
713 load_properties_from_file("/vendor/default.prop", NULL);
Jaekyun Seok0cf3a072017-04-24 18:52:54 +0900714
715 update_sys_usb_config();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800716}
717
Nick Kralevich32b90232012-09-19 11:15:24 -0700718static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800719 if (ALLOW_LOCAL_PROP_OVERRIDE) {
Hung-ying Tyan33463382017-05-25 19:18:17 +0800720 load_properties_from_file("/data/local.prop", NULL);
Nick Kralevich32b90232012-09-19 11:15:24 -0700721 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700722}
723
Ken Sumrallc5c51032011-03-08 17:01:29 -0800724/* When booting an encrypted system, /data is not mounted when the
725 * property service is started, so any properties stored there are
726 * not loaded. Vold triggers init to load these properties once it
727 * has mounted /data.
728 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700729void load_persist_props(void) {
Tom Cherry9951b792017-08-24 14:01:25 -0700730 // Devices with FDE have load_persist_props called twice; the first time when the temporary
731 // /data partition is mounted and then again once /data is truly mounted. We do not want to
732 // read persistent properties from the temporary /data partition or mark persistent properties
733 // as having been loaded during the first call, so we return in that case.
734 std::string crypto_state = android::base::GetProperty("ro.crypto.state", "");
735 std::string crypto_type = android::base::GetProperty("ro.crypto.type", "");
736 if (crypto_state == "encrypted" && crypto_type == "block") {
737 static size_t num_calls = 0;
738 if (++num_calls == 1) return;
739 }
740
Nick Kralevich32b90232012-09-19 11:15:24 -0700741 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800742 /* Read persistent properties after all default values have been loaded. */
Tom Cherry16fad422017-08-04 15:59:03 -0700743 auto persistent_properties = LoadPersistentProperties();
Tom Cherrya97faba2017-09-15 15:44:04 -0700744 for (const auto& persistent_property_record : persistent_properties.properties()) {
745 property_set(persistent_property_record.name(), persistent_property_record.value());
Tom Cherry16fad422017-08-04 15:59:03 -0700746 }
747 persistent_properties_loaded = true;
Keun-young Park404906d2017-02-28 19:20:38 -0800748 property_set("ro.persistent_properties.ready", "true");
Ken Sumrallc5c51032011-03-08 17:01:29 -0800749}
750
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700751void load_recovery_id_prop() {
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800752 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
753 fs_mgr_free_fstab);
754 if (!fstab) {
755 PLOG(ERROR) << "unable to read default fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700756 return;
757 }
758
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800759 fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab.get(), RECOVERY_MOUNT_POINT);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700760 if (rec == NULL) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700761 LOG(ERROR) << "/recovery not specified in fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700762 return;
763 }
764
Nick Kralevich29919492018-10-18 16:08:39 -0700765 int fd = open(rec->blk_device, O_RDONLY | O_CLOEXEC);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700766 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700767 PLOG(ERROR) << "error opening block device " << rec->blk_device;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700768 return;
769 }
770
771 boot_img_hdr hdr;
772 if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
773 std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700774 property_set("ro.recovery_id", hex);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700775 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700776 PLOG(ERROR) << "error reading /recovery";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700777 }
778
779 close(fd);
780}
781
Paul Lawrence948410a2015-07-01 14:40:56 -0700782void load_system_props() {
Hung-ying Tyan33463382017-05-25 19:18:17 +0800783 load_properties_from_file("/system/build.prop", NULL);
784 load_properties_from_file("/odm/build.prop", NULL);
785 load_properties_from_file("/vendor/build.prop", NULL);
Elliott Hughes795798d2017-01-27 16:21:55 -0800786 load_properties_from_file("/factory/factory.prop", "ro.*");
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700787 load_recovery_id_prop();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700788}
789
Tom Cherryc3692b32017-08-10 12:22:44 -0700790static int SelinuxAuditCallback(void* data, security_class_t /*cls*/, char* buf, size_t len) {
Tom Cherry5ab2e1c2018-05-03 16:57:19 -0700791 auto* d = reinterpret_cast<PropertyAuditData*>(data);
Tom Cherryc3692b32017-08-10 12:22:44 -0700792
793 if (!d || !d->name || !d->cr) {
794 LOG(ERROR) << "AuditCallback invoked with null data arguments!";
795 return 0;
796 }
797
798 snprintf(buf, len, "property=%s pid=%d uid=%d gid=%d", d->name, d->cr->pid, d->cr->uid,
799 d->cr->gid);
800 return 0;
801}
802
Tom Cherry2ae2f602017-12-14 01:58:17 +0000803bool LoadPropertyInfoFromFile(const std::string& filename,
804 std::vector<PropertyInfoEntry>* property_infos) {
805 auto file_contents = std::string();
806 if (!ReadFileToString(filename, &file_contents)) {
807 PLOG(ERROR) << "Could not read properties from '" << filename << "'";
808 return false;
809 }
810
Tom Cherry919458c2018-01-03 14:39:28 -0800811 auto errors = std::vector<std::string>{};
812 ParsePropertyInfoFile(file_contents, property_infos, &errors);
813 // Individual parsing errors are reported but do not cause a failed boot, which is what
814 // returning false would do here.
815 for (const auto& error : errors) {
816 LOG(ERROR) << "Could not read line from '" << filename << "': " << error;
Tom Cherry2ae2f602017-12-14 01:58:17 +0000817 }
Tom Cherry919458c2018-01-03 14:39:28 -0800818
Tom Cherry2ae2f602017-12-14 01:58:17 +0000819 return true;
820}
821
822void CreateSerializedPropertyInfo() {
823 auto property_infos = std::vector<PropertyInfoEntry>();
824 if (access("/system/etc/selinux/plat_property_contexts", R_OK) != -1) {
825 if (!LoadPropertyInfoFromFile("/system/etc/selinux/plat_property_contexts",
826 &property_infos)) {
827 return;
828 }
829 // Don't check for failure here, so we always have a sane list of properties.
830 // E.g. In case of recovery, the vendor partition will not have mounted and we
831 // still need the system / platform properties to function.
Bowgo Tsai36cf3532017-12-07 16:05:25 +0800832 if (!LoadPropertyInfoFromFile("/vendor/etc/selinux/vendor_property_contexts",
833 &property_infos)) {
834 // Fallback to nonplat_* if vendor_* doesn't exist.
835 LoadPropertyInfoFromFile("/vendor/etc/selinux/nonplat_property_contexts",
836 &property_infos);
837 }
Tom Cherry2ae2f602017-12-14 01:58:17 +0000838 } else {
839 if (!LoadPropertyInfoFromFile("/plat_property_contexts", &property_infos)) {
840 return;
841 }
Bowgo Tsai36cf3532017-12-07 16:05:25 +0800842 if (!LoadPropertyInfoFromFile("/vendor_property_contexts", &property_infos)) {
843 // Fallback to nonplat_* if vendor_* doesn't exist.
844 LoadPropertyInfoFromFile("/nonplat_property_contexts", &property_infos);
845 }
Tom Cherry2ae2f602017-12-14 01:58:17 +0000846 }
Tom Cherry927c5d52017-12-11 01:40:07 -0800847
Tom Cherry2ae2f602017-12-14 01:58:17 +0000848 auto serialized_contexts = std::string();
849 auto error = std::string();
Tom Cherry927c5d52017-12-11 01:40:07 -0800850 if (!BuildTrie(property_infos, "u:object_r:default_prop:s0", "string", &serialized_contexts,
Tom Cherry2ae2f602017-12-14 01:58:17 +0000851 &error)) {
852 LOG(ERROR) << "Unable to serialize property contexts: " << error;
853 return;
854 }
855
856 constexpr static const char kPropertyInfosPath[] = "/dev/__properties__/property_info";
857 if (!WriteStringToFile(serialized_contexts, kPropertyInfosPath, 0444, 0, 0, false)) {
858 PLOG(ERROR) << "Unable to write serialized property infos to file";
859 }
860 selinux_android_restorecon(kPropertyInfosPath, 0);
861}
862
Mark Salyzyn6c6ec722015-10-24 16:20:18 -0700863void StartPropertyService(Epoll* epoll) {
Tom Cherryc3692b32017-08-10 12:22:44 -0700864 selinux_callback cb;
865 cb.func_audit = SelinuxAuditCallback;
866 selinux_set_callback(SELINUX_CB_AUDIT, cb);
867
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000868 property_set("ro.property_service.version", "2");
869
Mark Salyzynb066fcc2017-05-05 14:44:35 -0700870 property_set_fd = CreateSocket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
Tom Cherryc3692b32017-08-10 12:22:44 -0700871 false, 0666, 0, 0, nullptr);
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700872 if (property_set_fd == -1) {
Tom Cherry4a679452017-09-26 16:30:03 -0700873 PLOG(FATAL) << "start_property_service socket creation failed";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700874 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800875
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700876 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700877
Mark Salyzyn6c6ec722015-10-24 16:20:18 -0700878 if (auto result = epoll->RegisterHandler(property_set_fd, handle_property_set_fd); !result) {
879 PLOG(FATAL) << result.error();
880 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800881}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700882
883} // namespace init
884} // namespace android