blob: 2aa89ff964875fd5edd95f8b7c07fa4b7fb4e5b7 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tom Cherry3f5eaae52017-04-06 16:30:22 -070017#include "property_service.h"
18
19#include <ctype.h>
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
Keun-young Park7d320262017-02-17 17:48:56 -080023#include <inttypes.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070024#include <limits.h>
25#include <netinet/in.h>
26#include <stdarg.h>
Tom Cherryf57c0bf2017-04-07 13:46:21 -070027#include <stddef.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#include <stdio.h>
29#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <string.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070031#include <sys/mman.h>
JP Abgrall4515d812014-01-31 14:37:07 -080032#include <sys/poll.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070033#include <sys/select.h>
34#include <sys/types.h>
35#include <sys/un.h>
36#include <unistd.h>
Elliott Hughes81399e12015-03-10 08:39:45 -070037
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
39#include <sys/_system_properties.h>
40
Tom Cherry3f5eaae52017-04-06 16:30:22 -070041#include <memory>
42#include <vector>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
Elliott Hughes4f713192015-12-04 22:00:26 -080044#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070045#include <android-base/logging.h>
Keun-young Park7d320262017-02-17 17:48:56 -080046#include <android-base/stringprintf.h>
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000047#include <android-base/strings.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070048#include <bootimg.h>
49#include <fs_mgr.h>
50#include <selinux/android.h>
51#include <selinux/label.h>
52#include <selinux/selinux.h>
Andres Moralesdb5f5d42015-05-08 08:30:33 -070053
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070055#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Keun-young Park7d320262017-02-17 17:48:56 -080057using android::base::StringPrintf;
58
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059#define PERSISTENT_PROPERTY_DIR "/data/property"
Andres Moralesdb5f5d42015-05-08 08:30:33 -070060#define RECOVERY_MOUNT_POINT "/recovery"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
62static int persistent_properties_loaded = 0;
63
Colin Crossd11beb22010-04-13 19:33:37 -070064static int property_set_fd = -1;
65
Elliott Hughesda40c002015-03-27 23:20:44 -070066void property_init() {
Elliott Hughesda40c002015-03-27 23:20:44 -070067 if (__system_property_area_init()) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070068 LOG(ERROR) << "Failed to initialize property area";
Tom Cherry60361142015-12-01 17:16:53 -080069 exit(1);
Elliott Hughesda40c002015-03-27 23:20:44 -070070 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071}
72
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000073static bool check_mac_perms(const std::string& name, char* sctx, struct ucred* cr) {
74
75 if (!sctx) {
76 return false;
77 }
78
79 if (!sehandle_prop) {
80 return false;
81 }
82
83 char* tctx = nullptr;
84 if (selabel_lookup(sehandle_prop, &tctx, name.c_str(), 1) != 0) {
85 return false;
86 }
87
William Robertsd7aea442015-10-01 16:03:47 -070088 property_audit_data audit_data;
rpcraig63207cd2012-08-09 10:05:49 -040089
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000090 audit_data.name = name.c_str();
William Robertsd7aea442015-10-01 16:03:47 -070091 audit_data.cr = cr;
92
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000093 bool has_access = (selinux_check_access(sctx, tctx, "property_service", "set", &audit_data) == 0);
rpcraig63207cd2012-08-09 10:05:49 -040094
95 freecon(tctx);
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +000096 return has_access;
rpcraig63207cd2012-08-09 10:05:49 -040097}
98
William Robertsd7aea442015-10-01 16:03:47 -070099static int check_control_mac_perms(const char *name, char *sctx, struct ucred *cr)
rpcraig63207cd2012-08-09 10:05:49 -0400100{
rpcraig63207cd2012-08-09 10:05:49 -0400101 /*
102 * Create a name prefix out of ctl.<service name>
103 * The new prefix allows the use of the existing
104 * property service backend labeling while avoiding
105 * mislabels based on true property prefixes.
106 */
107 char ctl_name[PROP_VALUE_MAX+4];
108 int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
109
110 if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
111 return 0;
112
William Robertsd7aea442015-10-01 16:03:47 -0700113 return check_mac_perms(ctl_name, sctx, cr);
rpcraig63207cd2012-08-09 10:05:49 -0400114}
115
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800116static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117{
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700118 char tempPath[PATH_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 char path[PATH_MAX];
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700120 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700122 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
123 fd = mkstemp(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 if (fd < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700125 PLOG(ERROR) << "Unable to write persistent property to temp file " << tempPath;
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800126 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 }
128 write(fd, value, strlen(value));
OPPOde73a0c2014-03-28 19:12:47 +0800129 fsync(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 close(fd);
131
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700132 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 if (rename(tempPath, path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700134 PLOG(ERROR) << "Unable to rename persistent property file " << tempPath << " to " << path;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 unlink(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 }
137}
138
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000139bool is_legal_property_name(const std::string& name) {
Iliyan Malchevf6554802016-09-19 20:58:20 -0700140 size_t namelen = name.size();
141
Nick Kralevich69463612013-09-13 17:21:28 -0700142 if (namelen < 1) return false;
143 if (name[0] == '.') return false;
144 if (name[namelen - 1] == '.') return false;
145
Iliyan Malchevf6554802016-09-19 20:58:20 -0700146 /* Only allow alphanumeric, plus '.', '-', '@', or '_' */
Nick Kralevich69463612013-09-13 17:21:28 -0700147 /* Don't allow ".." to appear in a property name */
Iliyan Malchevf6554802016-09-19 20:58:20 -0700148 for (size_t i = 0; i < namelen; i++) {
Nick Kralevich69463612013-09-13 17:21:28 -0700149 if (name[i] == '.') {
Nick Kralevichc2c5a242013-09-16 11:30:48 -0700150 // i=0 is guaranteed to never have a dot. See above.
151 if (name[i-1] == '.') return false;
Nick Kralevich69463612013-09-13 17:21:28 -0700152 continue;
153 }
Iliyan Malchevf6554802016-09-19 20:58:20 -0700154 if (name[i] == '_' || name[i] == '-' || name[i] == '@') continue;
Nick Kralevich69463612013-09-13 17:21:28 -0700155 if (name[i] >= 'a' && name[i] <= 'z') continue;
156 if (name[i] >= 'A' && name[i] <= 'Z') continue;
157 if (name[i] >= '0' && name[i] <= '9') continue;
158 return false;
159 }
160
161 return true;
162}
163
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000164uint32_t property_set(const std::string& name, const std::string& value) {
165 size_t valuelen = value.size();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000167 if (!is_legal_property_name(name)) {
168 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: bad name";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000169 return PROP_ERROR_INVALID_NAME;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000170 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000171
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000172 if (valuelen >= PROP_VALUE_MAX) {
173 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
174 << "value too long";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000175 return PROP_ERROR_INVALID_VALUE;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000176 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000178 if (name == "selinux.restorecon_recursive" && valuelen > 0) {
179 if (restorecon(value.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700180 LOG(ERROR) << "Failed to restorecon_recursive " << value;
Jeff Sharkey76417512015-06-09 11:02:55 -0700181 }
182 }
183
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000184 prop_info* pi = (prop_info*) __system_property_find(name.c_str());
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000185 if (pi != nullptr) {
186 // ro.* properties are actually "write-once".
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000187 if (android::base::StartsWith(name, "ro.")) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000188 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
189 << "property already set";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000190 return PROP_ERROR_READ_ONLY_PROPERTY;
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000191 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000193 __system_property_update(pi, value.c_str(), valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 } else {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000195 int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700196 if (rc < 0) {
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000197 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed: "
198 << "__system_property_add failed";
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000199 return PROP_ERROR_SET_FAILED;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200200 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000202
Elliott Hughes4f915812016-12-05 13:12:48 -0800203 // Don't write properties to disk until after we have read all default
204 // properties to prevent them from being overwritten by default values.
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000205 if (persistent_properties_loaded && android::base::StartsWith(name, "persist.")) {
206 write_persistent_property(name.c_str(), value.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 }
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000208 property_changed(name.c_str(), value.c_str());
209 return PROP_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210}
211
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000212class SocketConnection {
213 public:
214 SocketConnection(int socket, const struct ucred& cred)
215 : socket_(socket), cred_(cred) {}
216
217 ~SocketConnection() {
218 close(socket_);
219 }
220
221 bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
222 return RecvFully(value, sizeof(*value), timeout_ms);
223 }
224
225 bool RecvChars(char* chars, size_t size, uint32_t* timeout_ms) {
226 return RecvFully(chars, size, timeout_ms);
227 }
228
229 bool RecvString(std::string* value, uint32_t* timeout_ms) {
230 uint32_t len = 0;
231 if (!RecvUint32(&len, timeout_ms)) {
232 return false;
233 }
234
235 if (len == 0) {
236 *value = "";
237 return true;
238 }
239
Elliott Hughesb005d902017-02-22 14:54:15 -0800240 // http://b/35166374: don't allow init to make arbitrarily large allocations.
241 if (len > 0xffff) {
242 LOG(ERROR) << "sys_prop: RecvString asked to read huge string: " << len;
243 errno = ENOMEM;
244 return false;
245 }
246
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000247 std::vector<char> chars(len);
248 if (!RecvChars(&chars[0], len, timeout_ms)) {
249 return false;
250 }
251
252 *value = std::string(&chars[0], len);
253 return true;
254 }
255
256 bool SendUint32(uint32_t value) {
257 int result = TEMP_FAILURE_RETRY(send(socket_, &value, sizeof(value), 0));
258 return result == sizeof(value);
259 }
260
261 int socket() {
262 return socket_;
263 }
264
265 const struct ucred& cred() {
266 return cred_;
267 }
268
269 private:
270 bool PollIn(uint32_t* timeout_ms) {
271 struct pollfd ufds[1];
272 ufds[0].fd = socket_;
273 ufds[0].events = POLLIN;
274 ufds[0].revents = 0;
275 while (*timeout_ms > 0) {
276 Timer timer;
277 int nr = poll(ufds, 1, *timeout_ms);
James Hawkins27c05222017-01-26 11:55:44 -0800278 uint64_t millis = timer.duration_ms();
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000279 *timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis;
280
281 if (nr > 0) {
282 return true;
283 }
284
285 if (nr == 0) {
286 // Timeout
287 break;
288 }
289
290 if (nr < 0 && errno != EINTR) {
291 PLOG(ERROR) << "sys_prop: error waiting for uid " << cred_.uid << " to send property message";
292 return false;
293 } else { // errno == EINTR
294 // Timer rounds milliseconds down in case of EINTR we want it to be rounded up
295 // to avoid slowing init down by causing EINTR with under millisecond timeout.
296 if (*timeout_ms > 0) {
297 --(*timeout_ms);
298 }
299 }
300 }
301
302 LOG(ERROR) << "sys_prop: timeout waiting for uid " << cred_.uid << " to send property message.";
303 return false;
304 }
305
306 bool RecvFully(void* data_ptr, size_t size, uint32_t* timeout_ms) {
307 size_t bytes_left = size;
308 char* data = static_cast<char*>(data_ptr);
309 while (*timeout_ms > 0 && bytes_left > 0) {
310 if (!PollIn(timeout_ms)) {
311 return false;
312 }
313
314 int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
315 if (result <= 0) {
316 return false;
317 }
318
319 bytes_left -= result;
320 data += result;
321 }
322
323 return bytes_left == 0;
324 }
325
326 int socket_;
327 struct ucred cred_;
328
329 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
330};
331
332static void handle_property_set(SocketConnection& socket,
333 const std::string& name,
334 const std::string& value,
335 bool legacy_protocol) {
336 const char* cmd_name = legacy_protocol ? "PROP_MSG_SETPROP" : "PROP_MSG_SETPROP2";
337 if (!is_legal_property_name(name)) {
338 LOG(ERROR) << "sys_prop(" << cmd_name << "): illegal property name \"" << name << "\"";
339 socket.SendUint32(PROP_ERROR_INVALID_NAME);
340 return;
341 }
342
343 struct ucred cr = socket.cred();
344 char* source_ctx = nullptr;
345 getpeercon(socket.socket(), &source_ctx);
346
347 if (android::base::StartsWith(name, "ctl.")) {
348 if (check_control_mac_perms(value.c_str(), source_ctx, &cr)) {
349 handle_control_message(name.c_str() + 4, value.c_str());
350 if (!legacy_protocol) {
351 socket.SendUint32(PROP_SUCCESS);
352 }
353 } else {
354 LOG(ERROR) << "sys_prop(" << cmd_name << "): Unable to " << (name.c_str() + 4)
355 << " service ctl [" << value << "]"
356 << " uid:" << cr.uid
357 << " gid:" << cr.gid
358 << " pid:" << cr.pid;
359 if (!legacy_protocol) {
360 socket.SendUint32(PROP_ERROR_HANDLE_CONTROL_MESSAGE);
361 }
362 }
363 } else {
364 if (check_mac_perms(name, source_ctx, &cr)) {
365 uint32_t result = property_set(name, value);
366 if (!legacy_protocol) {
367 socket.SendUint32(result);
368 }
369 } else {
370 LOG(ERROR) << "sys_prop(" << cmd_name << "): permission denied uid:" << cr.uid << " name:" << name;
371 if (!legacy_protocol) {
372 socket.SendUint32(PROP_ERROR_PERMISSION_DENIED);
373 }
374 }
375 }
376
377 freecon(source_ctx);
378}
379
380static void handle_property_set_fd() {
381 static constexpr uint32_t kDefaultSocketTimeout = 2000; /* ms */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382
Robert Sesekca2da602017-01-25 08:08:51 -0500383 int s = accept4(property_set_fd, nullptr, nullptr, SOCK_CLOEXEC);
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700384 if (s == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 return;
386 }
387
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700388 struct ucred cr;
389 socklen_t cr_size = sizeof(cr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
391 close(s);
Elliott Hughesb005d902017-02-22 14:54:15 -0800392 PLOG(ERROR) << "sys_prop: unable to get SO_PEERCRED";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 return;
394 }
395
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000396 SocketConnection socket(s, cr);
397 uint32_t timeout_ms = kDefaultSocketTimeout;
398
399 uint32_t cmd = 0;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000400 if (!socket.RecvUint32(&cmd, &timeout_ms)) {
401 PLOG(ERROR) << "sys_prop: error while reading command from the socket";
402 socket.SendUint32(PROP_ERROR_READ_CMD);
JP Abgrall4515d812014-01-31 14:37:07 -0800403 return;
404 }
405
Elliott Hughesb005d902017-02-22 14:54:15 -0800406 switch (cmd) {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000407 case PROP_MSG_SETPROP: {
408 char prop_name[PROP_NAME_MAX];
409 char prop_value[PROP_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000411 if (!socket.RecvChars(prop_name, PROP_NAME_MAX, &timeout_ms) ||
412 !socket.RecvChars(prop_value, PROP_VALUE_MAX, &timeout_ms)) {
413 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP): error while reading name/value from the socket";
414 return;
Nick Kralevich69463612013-09-13 17:21:28 -0700415 }
416
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000417 prop_name[PROP_NAME_MAX-1] = 0;
418 prop_value[PROP_VALUE_MAX-1] = 0;
rpcraig63207cd2012-08-09 10:05:49 -0400419
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000420 handle_property_set(socket, prop_value, prop_value, true);
Dimitry Ivanovdee4bd22017-01-19 14:53:00 -0800421 break;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000422 }
Dimitry Ivanov70c4ecf2017-01-24 18:38:09 +0000423
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000424 case PROP_MSG_SETPROP2: {
425 std::string name;
426 std::string value;
427 if (!socket.RecvString(&name, &timeout_ms) ||
428 !socket.RecvString(&value, &timeout_ms)) {
429 PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP2): error while reading name/value from the socket";
430 socket.SendUint32(PROP_ERROR_READ_DATA);
431 return;
432 }
433
434 handle_property_set(socket, name, value, false);
435 break;
436 }
Elliott Hughesb005d902017-02-22 14:54:15 -0800437
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 default:
Elliott Hughesb005d902017-02-22 14:54:15 -0800439 LOG(ERROR) << "sys_prop: invalid command " << cmd;
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000440 socket.SendUint32(PROP_ERROR_INVALID_CMD);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 break;
442 }
443}
444
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800445static void load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800447/*
448 * Filter is used to decide which properties to load: NULL loads all keys,
449 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
450 */
451static void load_properties(char *data, const char *filter)
452{
453 char *key, *value, *eol, *sol, *tmp, *fn;
454 size_t flen = 0;
455
456 if (filter) {
457 flen = strlen(filter);
458 }
459
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800461 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 key = sol;
463 *eol++ = 0;
464 sol = eol;
465
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800466 while (isspace(*key)) key++;
467 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800470 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800472 if (!strncmp(key, "import ", 7) && flen == 0) {
473 fn = key + 7;
474 while (isspace(*fn)) fn++;
475
476 key = strchr(fn, ' ');
477 if (key) {
478 *key++ = 0;
479 while (isspace(*key)) key++;
480 }
481
482 load_properties_from_file(fn, key);
483
484 } else {
485 value = strchr(key, '=');
486 if (!value) continue;
487 *value++ = 0;
488
489 tmp = value - 2;
490 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
491
492 while (isspace(*value)) value++;
493
494 if (flen > 0) {
495 if (filter[flen - 1] == '*') {
496 if (strncmp(key, filter, flen - 1)) continue;
497 } else {
498 if (strcmp(key, filter)) continue;
499 }
500 }
501
502 property_set(key, value);
503 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 }
505}
506
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700507// Filter is used to decide which properties to load: NULL loads all keys,
508// "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
Elliott Hughesda40c002015-03-27 23:20:44 -0700509static void load_properties_from_file(const char* filename, const char* filter) {
510 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800511 std::string data;
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700512 if (!read_file(filename, &data)) {
513 PLOG(WARNING) << "Couldn't load properties from " << filename;
514 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 }
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700516 data.push_back('\n');
517 load_properties(&data[0], filter);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000518 LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t << ".)";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519}
520
Elliott Hughes81399e12015-03-10 08:39:45 -0700521static void load_persistent_properties() {
522 persistent_properties_loaded = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523
Elliott Hughes81399e12015-03-10 08:39:45 -0700524 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
525 if (!dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700526 PLOG(ERROR) << "Unable to open persistent property directory \""
527 << PERSISTENT_PROPERTY_DIR << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700528 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800530
Elliott Hughes81399e12015-03-10 08:39:45 -0700531 struct dirent* entry;
532 while ((entry = readdir(dir.get())) != NULL) {
533 if (strncmp("persist.", entry->d_name, strlen("persist."))) {
534 continue;
535 }
536 if (entry->d_type != DT_REG) {
537 continue;
538 }
539
540 // Open the file and read the property value.
541 int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
542 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700543 PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700544 continue;
545 }
546
547 struct stat sb;
548 if (fstat(fd, &sb) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700549 PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
Elliott Hughes81399e12015-03-10 08:39:45 -0700550 close(fd);
551 continue;
552 }
553
554 // File must not be accessible to others, be owned by root/root, and
555 // not be a hard link to any other file.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700556 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 || sb.st_nlink != 1) {
557 PLOG(ERROR) << "skipping insecure property file " << entry->d_name
558 << " (uid=" << sb.st_uid << " gid=" << sb.st_gid
559 << " nlink=" << sb.st_nlink << " mode=" << std::oct << sb.st_mode << ")";
Elliott Hughes81399e12015-03-10 08:39:45 -0700560 close(fd);
561 continue;
562 }
563
564 char value[PROP_VALUE_MAX];
565 int length = read(fd, value, sizeof(value) - 1);
566 if (length >= 0) {
567 value[length] = 0;
568 property_set(entry->d_name, value);
569 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700570 PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
Elliott Hughes81399e12015-03-10 08:39:45 -0700571 }
572 close(fd);
573 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800574}
575
Elliott Hughesda40c002015-03-27 23:20:44 -0700576void property_load_boot_defaults() {
Elliott Hughes795798d2017-01-27 16:21:55 -0800577 load_properties_from_file("/default.prop", NULL);
578 load_properties_from_file("/odm/default.prop", NULL);
579 load_properties_from_file("/vendor/default.prop", NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800580}
581
Nick Kralevich32b90232012-09-19 11:15:24 -0700582static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800583 if (ALLOW_LOCAL_PROP_OVERRIDE) {
Tom Cherryccf23532017-03-28 16:40:41 -0700584 load_properties_from_file("/data/local.prop", NULL);
Nick Kralevich32b90232012-09-19 11:15:24 -0700585 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700586}
587
Ken Sumrallc5c51032011-03-08 17:01:29 -0800588/* When booting an encrypted system, /data is not mounted when the
589 * property service is started, so any properties stored there are
590 * not loaded. Vold triggers init to load these properties once it
591 * has mounted /data.
592 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700593void load_persist_props(void) {
Nick Kralevich32b90232012-09-19 11:15:24 -0700594 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800595 /* Read persistent properties after all default values have been loaded. */
596 load_persistent_properties();
Keun-young Park404906d2017-02-28 19:20:38 -0800597 property_set("ro.persistent_properties.ready", "true");
Ken Sumrallc5c51032011-03-08 17:01:29 -0800598}
599
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700600void load_recovery_id_prop() {
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800601 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
602 fs_mgr_free_fstab);
603 if (!fstab) {
604 PLOG(ERROR) << "unable to read default fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700605 return;
606 }
607
Bowgo Tsaic9a18422017-03-09 22:36:34 +0800608 fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab.get(), RECOVERY_MOUNT_POINT);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700609 if (rec == NULL) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700610 LOG(ERROR) << "/recovery not specified in fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700611 return;
612 }
613
614 int fd = open(rec->blk_device, O_RDONLY);
615 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700616 PLOG(ERROR) << "error opening block device " << rec->blk_device;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700617 return;
618 }
619
620 boot_img_hdr hdr;
621 if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
622 std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
623 property_set("ro.recovery_id", hex.c_str());
624 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700625 PLOG(ERROR) << "error reading /recovery";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700626 }
627
628 close(fd);
629}
630
Paul Lawrence948410a2015-07-01 14:40:56 -0700631void load_system_props() {
Elliott Hughes795798d2017-01-27 16:21:55 -0800632 load_properties_from_file("/system/build.prop", NULL);
633 load_properties_from_file("/odm/build.prop", NULL);
634 load_properties_from_file("/vendor/build.prop", NULL);
635 load_properties_from_file("/factory/factory.prop", "ro.*");
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700636 load_recovery_id_prop();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700637}
638
Elliott Hughesda40c002015-03-27 23:20:44 -0700639void start_property_service() {
Dimitry Ivanovc9bb0332017-01-24 20:43:58 +0000640 property_set("ro.property_service.version", "2");
641
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700642 property_set_fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
643 0666, 0, 0, NULL);
644 if (property_set_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700645 PLOG(ERROR) << "start_property_service socket creation failed";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700646 exit(1);
647 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800648
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700649 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700650
Elliott Hughes929f4072015-04-24 21:13:44 -0700651 register_epoll_handler(property_set_fd, handle_property_set_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800652}