blob: e19829765d9f214b9a8da5c5a5f1d749879e47a1 [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <string.h>
21#include <ctype.h>
22#include <fcntl.h>
23#include <stdarg.h>
24#include <dirent.h>
25#include <limits.h>
26#include <errno.h>
JP Abgrall4515d812014-01-31 14:37:07 -080027#include <sys/poll.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Elliott Hughes81399e12015-03-10 08:39:45 -070029#include <memory>
30
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031#include <cutils/misc.h>
32#include <cutils/sockets.h>
Matthew Xie40a91a22013-05-20 14:22:01 -070033#include <cutils/multiuser.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
35#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
36#include <sys/_system_properties.h>
37
38#include <sys/socket.h>
39#include <sys/un.h>
40#include <sys/select.h>
41#include <sys/types.h>
42#include <netinet/in.h>
43#include <sys/mman.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044
Paul Lawrencea8d84342016-11-14 15:40:18 -080045#include <selinux/android.h>
rpcraig63207cd2012-08-09 10:05:49 -040046#include <selinux/selinux.h>
47#include <selinux/label.h>
rpcraig63207cd2012-08-09 10:05:49 -040048
Andres Moralesdb5f5d42015-05-08 08:30:33 -070049#include <fs_mgr.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080050#include <android-base/file.h>
Andres Moralesdb5f5d42015-05-08 08:30:33 -070051#include "bootimg.h"
52
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053#include "property_service.h"
54#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070055#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070056#include "log.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
58#define PERSISTENT_PROPERTY_DIR "/data/property"
Andres Moralesdb5f5d42015-05-08 08:30:33 -070059#define FSTAB_PREFIX "/fstab."
60#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
William Robertsd7aea442015-10-01 16:03:47 -070073static int check_mac_perms(const char *name, char *sctx, struct ucred *cr)
rpcraig63207cd2012-08-09 10:05:49 -040074{
rpcraig63207cd2012-08-09 10:05:49 -040075 char *tctx = NULL;
rpcraig63207cd2012-08-09 10:05:49 -040076 int result = 0;
William Robertsd7aea442015-10-01 16:03:47 -070077 property_audit_data audit_data;
rpcraig63207cd2012-08-09 10:05:49 -040078
79 if (!sctx)
80 goto err;
81
82 if (!sehandle_prop)
83 goto err;
84
85 if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
86 goto err;
87
William Robertsd7aea442015-10-01 16:03:47 -070088 audit_data.name = name;
89 audit_data.cr = cr;
90
91 if (selinux_check_access(sctx, tctx, "property_service", "set", reinterpret_cast<void*>(&audit_data)) == 0)
rpcraig63207cd2012-08-09 10:05:49 -040092 result = 1;
93
94 freecon(tctx);
95 err:
96 return result;
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
Yabin Cui74edcea2015-07-24 10:11:05 -0700116std::string property_get(const char* name) {
117 char value[PROP_VALUE_MAX] = {0};
118 __system_property_get(name, value);
119 return value;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120}
121
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800122static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123{
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700124 char tempPath[PATH_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 char path[PATH_MAX];
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700126 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700128 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
129 fd = mkstemp(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 if (fd < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700131 PLOG(ERROR) << "Unable to write persistent property to temp file " << tempPath;
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800132 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 }
134 write(fd, value, strlen(value));
OPPOde73a0c2014-03-28 19:12:47 +0800135 fsync(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 close(fd);
137
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700138 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 if (rename(tempPath, path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700140 PLOG(ERROR) << "Unable to rename persistent property file " << tempPath << " to " << path;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 unlink(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 }
143}
144
Iliyan Malchevf6554802016-09-19 20:58:20 -0700145bool is_legal_property_name(const std::string &name)
Nick Kralevich69463612013-09-13 17:21:28 -0700146{
Iliyan Malchevf6554802016-09-19 20:58:20 -0700147 size_t namelen = name.size();
148
Nick Kralevich69463612013-09-13 17:21:28 -0700149 if (namelen >= PROP_NAME_MAX) return false;
150 if (namelen < 1) return false;
151 if (name[0] == '.') return false;
152 if (name[namelen - 1] == '.') return false;
153
Iliyan Malchevf6554802016-09-19 20:58:20 -0700154 /* Only allow alphanumeric, plus '.', '-', '@', or '_' */
Nick Kralevich69463612013-09-13 17:21:28 -0700155 /* Don't allow ".." to appear in a property name */
Iliyan Malchevf6554802016-09-19 20:58:20 -0700156 for (size_t i = 0; i < namelen; i++) {
Nick Kralevich69463612013-09-13 17:21:28 -0700157 if (name[i] == '.') {
Nick Kralevichc2c5a242013-09-16 11:30:48 -0700158 // i=0 is guaranteed to never have a dot. See above.
159 if (name[i-1] == '.') return false;
Nick Kralevich69463612013-09-13 17:21:28 -0700160 continue;
161 }
Iliyan Malchevf6554802016-09-19 20:58:20 -0700162 if (name[i] == '_' || name[i] == '-' || name[i] == '@') continue;
Nick Kralevich69463612013-09-13 17:21:28 -0700163 if (name[i] >= 'a' && name[i] <= 'z') continue;
164 if (name[i] >= 'A' && name[i] <= 'Z') continue;
165 if (name[i] >= '0' && name[i] <= '9') continue;
166 return false;
167 }
168
169 return true;
170}
171
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700172static int property_set_impl(const char* name, const char* value) {
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700173 size_t valuelen = strlen(value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174
Iliyan Malchevf6554802016-09-19 20:58:20 -0700175 if (!is_legal_property_name(name)) return -1;
Nick Kralevich69463612013-09-13 17:21:28 -0700176 if (valuelen >= PROP_VALUE_MAX) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177
Janis Danisevskis3d1dff22016-03-08 18:35:28 +0000178 if (strcmp("selinux.restorecon_recursive", name) == 0 && valuelen > 0) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800179 if (restorecon(value, 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
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700184 prop_info* pi = (prop_info*) __system_property_find(name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185
186 if(pi != 0) {
187 /* ro.* properties may NEVER be modified once set */
188 if(!strncmp(name, "ro.", 3)) return -1;
189
Colin Cross9f5af632013-01-23 23:09:48 -0800190 __system_property_update(pi, value, valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 } else {
Iliyan Malchevf6554802016-09-19 20:58:20 -0700192 int rc = __system_property_add(name, strlen(name), value, valuelen);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700193 if (rc < 0) {
194 return rc;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200195 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 }
197 /* If name starts with "net." treat as a DNS property. */
Mike Lockwoodb3779552009-05-08 14:27:42 -0400198 if (strncmp("net.", name, strlen("net.")) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 if (strcmp("net.change", name) == 0) {
200 return 0;
201 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800202 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 * The 'net.change' property is a special property used track when any
204 * 'net.*' property name is updated. It is _ONLY_ updated here. Its value
205 * contains the last updated 'net.*' property.
206 */
207 property_set("net.change", name);
208 } else if (persistent_properties_loaded &&
Mike Lockwoodb3779552009-05-08 14:27:42 -0400209 strncmp("persist.", name, strlen("persist.")) == 0) {
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800210 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 * Don't write properties to disk until after we have read all default properties
212 * to prevent them from being overwritten by default values.
213 */
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800214 write_persistent_property(name, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 }
216 property_changed(name, value);
217 return 0;
218}
219
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700220int property_set(const char* name, const char* value) {
221 int rc = property_set_impl(name, value);
222 if (rc == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700223 LOG(ERROR) << "property_set(\"" << name << "\", \"" << value << "\") failed";
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700224 }
225 return rc;
226}
227
Elliott Hughes929f4072015-04-24 21:13:44 -0700228static void handle_property_set_fd()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229{
230 prop_msg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 int r;
rpcraig63207cd2012-08-09 10:05:49 -0400232 char * source_ctx = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700234 int s = accept(property_set_fd, nullptr, nullptr);
235 if (s == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 return;
237 }
238
239 /* Check socket options here */
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700240 struct ucred cr;
241 socklen_t cr_size = sizeof(cr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
243 close(s);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700244 PLOG(ERROR) << "Unable to receive socket options";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 return;
246 }
247
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700248 static constexpr int timeout_ms = 2 * 1000; /* Default 2 sec timeout for caller to send property. */
249 struct pollfd ufds[1];
JP Abgrall4515d812014-01-31 14:37:07 -0800250 ufds[0].fd = s;
251 ufds[0].events = POLLIN;
252 ufds[0].revents = 0;
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700253 int nr = TEMP_FAILURE_RETRY(poll(ufds, 1, timeout_ms));
JP Abgrall4515d812014-01-31 14:37:07 -0800254 if (nr == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700255 LOG(ERROR) << "sys_prop: timeout waiting for uid " << cr.uid << " to send property message.";
JP Abgrall4515d812014-01-31 14:37:07 -0800256 close(s);
257 return;
258 } else if (nr < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700259 PLOG(ERROR) << "sys_prop: error waiting for uid " << cr.uid << " to send property message";
JP Abgrall4515d812014-01-31 14:37:07 -0800260 close(s);
261 return;
262 }
263
264 r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), MSG_DONTWAIT));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 if(r != sizeof(prop_msg)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700266 PLOG(ERROR) << "sys_prop: mis-match msg size received: " << r << " expected: " << sizeof(prop_msg);
Brad Fitzpatrick9f1e0e32011-03-30 12:39:56 -0700267 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268 return;
269 }
270
271 switch(msg.cmd) {
272 case PROP_MSG_SETPROP:
273 msg.name[PROP_NAME_MAX-1] = 0;
274 msg.value[PROP_VALUE_MAX-1] = 0;
275
Iliyan Malchevf6554802016-09-19 20:58:20 -0700276 if (!is_legal_property_name(msg.name)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700277 LOG(ERROR) << "sys_prop: illegal property name \"" << msg.name << "\"";
Nick Kralevich69463612013-09-13 17:21:28 -0700278 close(s);
279 return;
280 }
281
rpcraig63207cd2012-08-09 10:05:49 -0400282 getpeercon(s, &source_ctx);
rpcraig63207cd2012-08-09 10:05:49 -0400283
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 if(memcmp(msg.name,"ctl.",4) == 0) {
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700285 // Keep the old close-socket-early behavior when handling
286 // ctl.* properties.
287 close(s);
William Robertsd7aea442015-10-01 16:03:47 -0700288 if (check_control_mac_perms(msg.value, source_ctx, &cr)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 handle_control_message((char*) msg.name + 4, (char*) msg.value);
290 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700291 LOG(ERROR) << "sys_prop: Unable to " << (msg.name + 4)
292 << " service ctl [" << msg.value << "]"
293 << " uid:" << cr.uid
294 << " gid:" << cr.gid
295 << " pid:" << cr.pid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 }
297 } else {
Tom Cherryc787cf22016-01-11 13:04:19 -0800298 if (check_mac_perms(msg.name, source_ctx, &cr)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 property_set((char*) msg.name, (char*) msg.value);
300 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700301 LOG(ERROR) << "sys_prop: permission denied uid:" << cr.uid << " name:" << msg.name;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 }
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700303
304 // Note: bionic's property client code assumes that the
305 // property server will not close the socket until *AFTER*
306 // the property is written to memory.
307 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 }
rpcraig63207cd2012-08-09 10:05:49 -0400309 freecon(source_ctx);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310 break;
311
312 default:
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700313 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 break;
315 }
316}
317
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800318static void load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800320/*
321 * Filter is used to decide which properties to load: NULL loads all keys,
322 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
323 */
324static void load_properties(char *data, const char *filter)
325{
326 char *key, *value, *eol, *sol, *tmp, *fn;
327 size_t flen = 0;
328
329 if (filter) {
330 flen = strlen(filter);
331 }
332
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800334 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 key = sol;
336 *eol++ = 0;
337 sol = eol;
338
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800339 while (isspace(*key)) key++;
340 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800343 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800345 if (!strncmp(key, "import ", 7) && flen == 0) {
346 fn = key + 7;
347 while (isspace(*fn)) fn++;
348
349 key = strchr(fn, ' ');
350 if (key) {
351 *key++ = 0;
352 while (isspace(*key)) key++;
353 }
354
355 load_properties_from_file(fn, key);
356
357 } else {
358 value = strchr(key, '=');
359 if (!value) continue;
360 *value++ = 0;
361
362 tmp = value - 2;
363 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
364
365 while (isspace(*value)) value++;
366
367 if (flen > 0) {
368 if (filter[flen - 1] == '*') {
369 if (strncmp(key, filter, flen - 1)) continue;
370 } else {
371 if (strcmp(key, filter)) continue;
372 }
373 }
374
375 property_set(key, value);
376 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377 }
378}
379
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700380// Filter is used to decide which properties to load: NULL loads all keys,
381// "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
Elliott Hughesda40c002015-03-27 23:20:44 -0700382static void load_properties_from_file(const char* filename, const char* filter) {
383 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800384 std::string data;
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700385 if (!read_file(filename, &data)) {
386 PLOG(WARNING) << "Couldn't load properties from " << filename;
387 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 }
Elliott Hughes5a7ad842016-09-30 14:12:33 -0700389 data.push_back('\n');
390 load_properties(&data[0], filter);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700391 LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t.duration() << "s.)";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392}
393
Elliott Hughes81399e12015-03-10 08:39:45 -0700394static void load_persistent_properties() {
395 persistent_properties_loaded = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396
Elliott Hughes81399e12015-03-10 08:39:45 -0700397 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
398 if (!dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700399 PLOG(ERROR) << "Unable to open persistent property directory \""
400 << PERSISTENT_PROPERTY_DIR << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700401 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800403
Elliott Hughes81399e12015-03-10 08:39:45 -0700404 struct dirent* entry;
405 while ((entry = readdir(dir.get())) != NULL) {
406 if (strncmp("persist.", entry->d_name, strlen("persist."))) {
407 continue;
408 }
409 if (entry->d_type != DT_REG) {
410 continue;
411 }
412
413 // Open the file and read the property value.
414 int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
415 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700416 PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
Elliott Hughes81399e12015-03-10 08:39:45 -0700417 continue;
418 }
419
420 struct stat sb;
421 if (fstat(fd, &sb) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700422 PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
Elliott Hughes81399e12015-03-10 08:39:45 -0700423 close(fd);
424 continue;
425 }
426
427 // File must not be accessible to others, be owned by root/root, and
428 // not be a hard link to any other file.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700429 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 || sb.st_nlink != 1) {
430 PLOG(ERROR) << "skipping insecure property file " << entry->d_name
431 << " (uid=" << sb.st_uid << " gid=" << sb.st_gid
432 << " nlink=" << sb.st_nlink << " mode=" << std::oct << sb.st_mode << ")";
Elliott Hughes81399e12015-03-10 08:39:45 -0700433 close(fd);
434 continue;
435 }
436
437 char value[PROP_VALUE_MAX];
438 int length = read(fd, value, sizeof(value) - 1);
439 if (length >= 0) {
440 value[length] = 0;
441 property_set(entry->d_name, value);
442 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700443 PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
Elliott Hughes81399e12015-03-10 08:39:45 -0700444 }
445 close(fd);
446 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447}
448
Elliott Hughesda40c002015-03-27 23:20:44 -0700449void property_load_boot_defaults() {
Andrew Boie3899f522012-10-12 15:23:40 -0700450 load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451}
452
Nick Kralevich32b90232012-09-19 11:15:24 -0700453static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800454 if (ALLOW_LOCAL_PROP_OVERRIDE) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700455 std::string debuggable = property_get("ro.debuggable");
456 if (debuggable == "1") {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800457 load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
458 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700459 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700460}
461
Ken Sumrallc5c51032011-03-08 17:01:29 -0800462/* When booting an encrypted system, /data is not mounted when the
463 * property service is started, so any properties stored there are
464 * not loaded. Vold triggers init to load these properties once it
465 * has mounted /data.
466 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700467void load_persist_props(void) {
Nick Kralevich32b90232012-09-19 11:15:24 -0700468 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800469 /* Read persistent properties after all default values have been loaded. */
470 load_persistent_properties();
471}
472
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700473void load_recovery_id_prop() {
Yabin Cui74edcea2015-07-24 10:11:05 -0700474 std::string ro_hardware = property_get("ro.hardware");
475 if (ro_hardware.empty()) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700476 LOG(ERROR) << "ro.hardware not set - unable to load recovery id";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700477 return;
478 }
Yabin Cui74edcea2015-07-24 10:11:05 -0700479 std::string fstab_filename = FSTAB_PREFIX + ro_hardware;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700480
Yabin Cui74edcea2015-07-24 10:11:05 -0700481 std::unique_ptr<fstab, void(*)(fstab*)> tab(fs_mgr_read_fstab(fstab_filename.c_str()),
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700482 fs_mgr_free_fstab);
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700483 if (!tab) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700484 PLOG(ERROR) << "unable to read fstab " << fstab_filename;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700485 return;
486 }
487
488 fstab_rec* rec = fs_mgr_get_entry_for_mount_point(tab.get(), RECOVERY_MOUNT_POINT);
489 if (rec == NULL) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700490 LOG(ERROR) << "/recovery not specified in fstab";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700491 return;
492 }
493
494 int fd = open(rec->blk_device, O_RDONLY);
495 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700496 PLOG(ERROR) << "error opening block device " << rec->blk_device;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700497 return;
498 }
499
500 boot_img_hdr hdr;
501 if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
502 std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
503 property_set("ro.recovery_id", hex.c_str());
504 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700505 PLOG(ERROR) << "error reading /recovery";
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700506 }
507
508 close(fd);
509}
510
Paul Lawrence948410a2015-07-01 14:40:56 -0700511void load_system_props() {
Andrew Boie3899f522012-10-12 15:23:40 -0700512 load_properties_from_file(PROP_PATH_SYSTEM_BUILD, NULL);
Daniel Rosenbergb9512222014-11-10 17:01:33 -0800513 load_properties_from_file(PROP_PATH_VENDOR_BUILD, NULL);
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800514 load_properties_from_file(PROP_PATH_FACTORY, "ro.*");
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700515 load_recovery_id_prop();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700516}
517
Elliott Hughesda40c002015-03-27 23:20:44 -0700518void start_property_service() {
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700519 property_set_fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
520 0666, 0, 0, NULL);
521 if (property_set_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700522 PLOG(ERROR) << "start_property_service socket creation failed";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700523 exit(1);
524 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700526 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700527
Elliott Hughes929f4072015-04-24 21:13:44 -0700528 register_epoll_handler(property_set_fd, handle_property_set_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529}