blob: 2fa81d49ae27b0a2ca56d814d90efcbfcaef797d [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#include <private/android_filesystem_config.h>
45
rpcraig63207cd2012-08-09 10:05:49 -040046#include <selinux/selinux.h>
47#include <selinux/label.h>
rpcraig63207cd2012-08-09 10:05:49 -040048
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049#include "property_service.h"
50#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070051#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070052#include "log.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
54#define PERSISTENT_PROPERTY_DIR "/data/property"
55
56static int persistent_properties_loaded = 0;
Elliott Hughesda40c002015-03-27 23:20:44 -070057static bool property_area_initialized = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Colin Crossd11beb22010-04-13 19:33:37 -070059static int property_set_fd = -1;
60
Elliott Hughesc0e919c2015-02-04 14:46:36 -080061struct workspace {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062 size_t size;
Nick Kralevich28406472013-01-22 12:46:09 -080063 int fd;
Elliott Hughesc0e919c2015-02-04 14:46:36 -080064};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066static workspace pa_workspace;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
Elliott Hughesda40c002015-03-27 23:20:44 -070068void property_init() {
69 if (property_area_initialized) {
70 return;
71 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072
Elliott Hughesda40c002015-03-27 23:20:44 -070073 property_area_initialized = true;
Greg Hackmannf14eef02013-02-12 14:39:31 -080074
Elliott Hughesda40c002015-03-27 23:20:44 -070075 if (__system_property_area_init()) {
76 return;
77 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
Elliott Hughesda40c002015-03-27 23:20:44 -070079 pa_workspace.size = 0;
80 pa_workspace.fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
81 if (pa_workspace.fd == -1) {
82 ERROR("Failed to open %s: %s\n", PROP_FILENAME, strerror(errno));
83 return;
84 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085}
86
rpcraig63207cd2012-08-09 10:05:49 -040087static int check_mac_perms(const char *name, char *sctx)
88{
rpcraig63207cd2012-08-09 10:05:49 -040089 if (is_selinux_enabled() <= 0)
90 return 1;
91
92 char *tctx = NULL;
rpcraig63207cd2012-08-09 10:05:49 -040093 int result = 0;
94
95 if (!sctx)
96 goto err;
97
98 if (!sehandle_prop)
99 goto err;
100
101 if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
102 goto err;
103
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800104 if (selinux_check_access(sctx, tctx, "property_service", "set", (void*) name) == 0)
rpcraig63207cd2012-08-09 10:05:49 -0400105 result = 1;
106
107 freecon(tctx);
108 err:
109 return result;
rpcraig63207cd2012-08-09 10:05:49 -0400110}
111
112static int check_control_mac_perms(const char *name, char *sctx)
113{
rpcraig63207cd2012-08-09 10:05:49 -0400114 /*
115 * Create a name prefix out of ctl.<service name>
116 * The new prefix allows the use of the existing
117 * property service backend labeling while avoiding
118 * mislabels based on true property prefixes.
119 */
120 char ctl_name[PROP_VALUE_MAX+4];
121 int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
122
123 if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
124 return 0;
125
126 return check_mac_perms(ctl_name, sctx);
rpcraig63207cd2012-08-09 10:05:49 -0400127}
128
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129/*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 * Checks permissions for setting system properties.
131 * Returns 1 if uid allowed, 0 otherwise.
132 */
Nick Kralevich528c13e2014-06-17 22:23:54 -0700133static int check_perms(const char *name, char *sctx)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 if(!strncmp(name, "ro.", 3))
136 name +=3;
137
Nick Kralevich528c13e2014-06-17 22:23:54 -0700138 return check_mac_perms(name, sctx);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139}
140
Colin Cross88ac54a2013-01-29 14:58:57 -0800141int __property_get(const char *name, char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142{
Colin Cross2deedfe2013-01-28 17:13:35 -0800143 return __system_property_get(name, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144}
145
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800146static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147{
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700148 char tempPath[PATH_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 char path[PATH_MAX];
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700150 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700152 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
153 fd = mkstemp(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154 if (fd < 0) {
Elliott Hughescd67f002015-03-20 17:05:56 -0700155 ERROR("Unable to write persistent property to temp file %s: %s\n", tempPath, strerror(errno));
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800156 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157 }
158 write(fd, value, strlen(value));
OPPOde73a0c2014-03-28 19:12:47 +0800159 fsync(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 close(fd);
161
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700162 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 if (rename(tempPath, path)) {
164 unlink(tempPath);
165 ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
166 }
167}
168
Nick Kralevich69463612013-09-13 17:21:28 -0700169static bool is_legal_property_name(const char* name, size_t namelen)
170{
171 size_t i;
Nick Kralevich69463612013-09-13 17:21:28 -0700172 if (namelen >= PROP_NAME_MAX) return false;
173 if (namelen < 1) return false;
174 if (name[0] == '.') return false;
175 if (name[namelen - 1] == '.') return false;
176
177 /* Only allow alphanumeric, plus '.', '-', or '_' */
178 /* Don't allow ".." to appear in a property name */
179 for (i = 0; i < namelen; i++) {
180 if (name[i] == '.') {
Nick Kralevichc2c5a242013-09-16 11:30:48 -0700181 // i=0 is guaranteed to never have a dot. See above.
182 if (name[i-1] == '.') return false;
Nick Kralevich69463612013-09-13 17:21:28 -0700183 continue;
184 }
Nick Kralevich69463612013-09-13 17:21:28 -0700185 if (name[i] == '_' || name[i] == '-') continue;
186 if (name[i] >= 'a' && name[i] <= 'z') continue;
187 if (name[i] >= 'A' && name[i] <= 'Z') continue;
188 if (name[i] >= '0' && name[i] <= '9') continue;
189 return false;
190 }
191
192 return true;
193}
194
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700195static int property_set_impl(const char* name, const char* value) {
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700196 size_t namelen = strlen(name);
197 size_t valuelen = strlen(value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198
Nick Kralevich69463612013-09-13 17:21:28 -0700199 if (!is_legal_property_name(name, namelen)) return -1;
200 if (valuelen >= PROP_VALUE_MAX) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700202 prop_info* pi = (prop_info*) __system_property_find(name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203
204 if(pi != 0) {
205 /* ro.* properties may NEVER be modified once set */
206 if(!strncmp(name, "ro.", 3)) return -1;
207
Colin Cross9f5af632013-01-23 23:09:48 -0800208 __system_property_update(pi, value, valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 } else {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700210 int rc = __system_property_add(name, namelen, value, valuelen);
211 if (rc < 0) {
212 return rc;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200213 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 }
215 /* If name starts with "net." treat as a DNS property. */
Mike Lockwoodb3779552009-05-08 14:27:42 -0400216 if (strncmp("net.", name, strlen("net.")) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 if (strcmp("net.change", name) == 0) {
218 return 0;
219 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800220 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 * The 'net.change' property is a special property used track when any
222 * 'net.*' property name is updated. It is _ONLY_ updated here. Its value
223 * contains the last updated 'net.*' property.
224 */
225 property_set("net.change", name);
226 } else if (persistent_properties_loaded &&
Mike Lockwoodb3779552009-05-08 14:27:42 -0400227 strncmp("persist.", name, strlen("persist.")) == 0) {
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800228 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 * Don't write properties to disk until after we have read all default properties
230 * to prevent them from being overwritten by default values.
231 */
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800232 write_persistent_property(name, value);
repo sync8a387872013-05-17 12:47:04 -0700233 } else if (strcmp("selinux.reload_policy", name) == 0 &&
234 strcmp("1", value) == 0) {
235 selinux_reload_policy();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 }
237 property_changed(name, value);
238 return 0;
239}
240
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700241int property_set(const char* name, const char* value) {
242 int rc = property_set_impl(name, value);
243 if (rc == -1) {
Elliott Hughes930974c2015-03-23 08:07:19 -0700244 ERROR("property_set(\"%s\", \"%s\") failed\n", name, value);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700245 }
246 return rc;
247}
248
Colin Crossd11beb22010-04-13 19:33:37 -0700249void handle_property_set_fd()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250{
251 prop_msg msg;
252 int s;
253 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 struct ucred cr;
255 struct sockaddr_un addr;
256 socklen_t addr_size = sizeof(addr);
257 socklen_t cr_size = sizeof(cr);
rpcraig63207cd2012-08-09 10:05:49 -0400258 char * source_ctx = NULL;
JP Abgrall4515d812014-01-31 14:37:07 -0800259 struct pollfd ufds[1];
260 const int timeout_ms = 2 * 1000; /* Default 2 sec timeout for caller to send property. */
261 int nr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262
Colin Crossd11beb22010-04-13 19:33:37 -0700263 if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264 return;
265 }
266
267 /* Check socket options here */
268 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
269 close(s);
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700270 ERROR("Unable to receive socket options\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 return;
272 }
273
JP Abgrall4515d812014-01-31 14:37:07 -0800274 ufds[0].fd = s;
275 ufds[0].events = POLLIN;
276 ufds[0].revents = 0;
277 nr = TEMP_FAILURE_RETRY(poll(ufds, 1, timeout_ms));
278 if (nr == 0) {
279 ERROR("sys_prop: timeout waiting for uid=%d to send property message.\n", cr.uid);
280 close(s);
281 return;
282 } else if (nr < 0) {
Elliott Hughescd67f002015-03-20 17:05:56 -0700283 ERROR("sys_prop: error waiting for uid=%d to send property message: %s\n", cr.uid, strerror(errno));
JP Abgrall4515d812014-01-31 14:37:07 -0800284 close(s);
285 return;
286 }
287
288 r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), MSG_DONTWAIT));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 if(r != sizeof(prop_msg)) {
Elliott Hughescd67f002015-03-20 17:05:56 -0700290 ERROR("sys_prop: mis-match msg size received: %d expected: %zu: %s\n",
291 r, sizeof(prop_msg), strerror(errno));
Brad Fitzpatrick9f1e0e32011-03-30 12:39:56 -0700292 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 return;
294 }
295
296 switch(msg.cmd) {
297 case PROP_MSG_SETPROP:
298 msg.name[PROP_NAME_MAX-1] = 0;
299 msg.value[PROP_VALUE_MAX-1] = 0;
300
Nick Kralevich69463612013-09-13 17:21:28 -0700301 if (!is_legal_property_name(msg.name, strlen(msg.name))) {
302 ERROR("sys_prop: illegal property name. Got: \"%s\"\n", msg.name);
303 close(s);
304 return;
305 }
306
rpcraig63207cd2012-08-09 10:05:49 -0400307 getpeercon(s, &source_ctx);
rpcraig63207cd2012-08-09 10:05:49 -0400308
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 if(memcmp(msg.name,"ctl.",4) == 0) {
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700310 // Keep the old close-socket-early behavior when handling
311 // ctl.* properties.
312 close(s);
Nick Kralevich528c13e2014-06-17 22:23:54 -0700313 if (check_control_mac_perms(msg.value, source_ctx)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 handle_control_message((char*) msg.name + 4, (char*) msg.value);
315 } else {
Wink Savillecfa0d842010-10-03 13:30:11 -0700316 ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",
317 msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318 }
319 } else {
Nick Kralevich528c13e2014-06-17 22:23:54 -0700320 if (check_perms(msg.name, source_ctx)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321 property_set((char*) msg.name, (char*) msg.value);
322 } else {
323 ERROR("sys_prop: permission denied uid:%d name:%s\n",
324 cr.uid, msg.name);
325 }
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700326
327 // Note: bionic's property client code assumes that the
328 // property server will not close the socket until *AFTER*
329 // the property is written to memory.
330 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 }
rpcraig63207cd2012-08-09 10:05:49 -0400332 freecon(source_ctx);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 break;
334
335 default:
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700336 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 break;
338 }
339}
340
Nick Kralevich28406472013-01-22 12:46:09 -0800341void get_property_workspace(int *fd, int *sz)
342{
343 *fd = pa_workspace.fd;
344 *sz = pa_workspace.size;
345}
346
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800347static void load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800349/*
350 * Filter is used to decide which properties to load: NULL loads all keys,
351 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
352 */
353static void load_properties(char *data, const char *filter)
354{
355 char *key, *value, *eol, *sol, *tmp, *fn;
356 size_t flen = 0;
357
358 if (filter) {
359 flen = strlen(filter);
360 }
361
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800363 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 key = sol;
365 *eol++ = 0;
366 sol = eol;
367
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800368 while (isspace(*key)) key++;
369 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800372 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800374 if (!strncmp(key, "import ", 7) && flen == 0) {
375 fn = key + 7;
376 while (isspace(*fn)) fn++;
377
378 key = strchr(fn, ' ');
379 if (key) {
380 *key++ = 0;
381 while (isspace(*key)) key++;
382 }
383
384 load_properties_from_file(fn, key);
385
386 } else {
387 value = strchr(key, '=');
388 if (!value) continue;
389 *value++ = 0;
390
391 tmp = value - 2;
392 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
393
394 while (isspace(*value)) value++;
395
396 if (flen > 0) {
397 if (filter[flen - 1] == '*') {
398 if (strncmp(key, filter, flen - 1)) continue;
399 } else {
400 if (strcmp(key, filter)) continue;
401 }
402 }
403
404 property_set(key, value);
405 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406 }
407}
408
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800409/*
410 * Filter is used to decide which properties to load: NULL loads all keys,
411 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
412 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700413static void load_properties_from_file(const char* filename, const char* filter) {
414 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800415 std::string data;
Elliott Hughesda40c002015-03-27 23:20:44 -0700416 if (read_file(filename, &data)) {
Elliott Hughesf682b472015-02-06 12:19:48 -0800417 load_properties(&data[0], filter);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 }
Elliott Hughesda40c002015-03-27 23:20:44 -0700419 NOTICE("(Loading properties from %s took %.2fs.)\n", filename, t.duration());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420}
421
Elliott Hughes81399e12015-03-10 08:39:45 -0700422static void load_persistent_properties() {
423 persistent_properties_loaded = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424
Elliott Hughes81399e12015-03-10 08:39:45 -0700425 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
426 if (!dir) {
427 ERROR("Unable to open persistent property directory \"%s\": %s\n",
428 PERSISTENT_PROPERTY_DIR, strerror(errno));
429 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800431
Elliott Hughes81399e12015-03-10 08:39:45 -0700432 struct dirent* entry;
433 while ((entry = readdir(dir.get())) != NULL) {
434 if (strncmp("persist.", entry->d_name, strlen("persist."))) {
435 continue;
436 }
437 if (entry->d_type != DT_REG) {
438 continue;
439 }
440
441 // Open the file and read the property value.
442 int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
443 if (fd == -1) {
444 ERROR("Unable to open persistent property file \"%s\": %s\n",
445 entry->d_name, strerror(errno));
446 continue;
447 }
448
449 struct stat sb;
450 if (fstat(fd, &sb) == -1) {
451 ERROR("fstat on property file \"%s\" failed: %s\n", entry->d_name, strerror(errno));
452 close(fd);
453 continue;
454 }
455
456 // File must not be accessible to others, be owned by root/root, and
457 // not be a hard link to any other file.
458 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || (sb.st_uid != 0) || (sb.st_gid != 0) ||
459 (sb.st_nlink != 1)) {
460 ERROR("skipping insecure property file %s (uid=%u gid=%u nlink=%u mode=%o)\n",
461 entry->d_name, (unsigned int)sb.st_uid, (unsigned int)sb.st_gid,
462 (unsigned int)sb.st_nlink, sb.st_mode);
463 close(fd);
464 continue;
465 }
466
467 char value[PROP_VALUE_MAX];
468 int length = read(fd, value, sizeof(value) - 1);
469 if (length >= 0) {
470 value[length] = 0;
471 property_set(entry->d_name, value);
472 } else {
473 ERROR("Unable to read persistent property file %s: %s\n",
474 entry->d_name, strerror(errno));
475 }
476 close(fd);
477 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478}
479
Elliott Hughesda40c002015-03-27 23:20:44 -0700480void property_load_boot_defaults() {
Andrew Boie3899f522012-10-12 15:23:40 -0700481 load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482}
483
Elliott Hughesda40c002015-03-27 23:20:44 -0700484bool properties_initialized() {
485 return property_area_initialized;
Colin Cross3294bbb2010-04-19 17:11:33 -0700486}
487
Nick Kralevich32b90232012-09-19 11:15:24 -0700488static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800489 if (ALLOW_LOCAL_PROP_OVERRIDE) {
490 char debuggable[PROP_VALUE_MAX];
491 int ret = property_get("ro.debuggable", debuggable);
492 if (ret && (strcmp(debuggable, "1") == 0)) {
493 load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
494 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700495 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700496}
497
Ken Sumrallc5c51032011-03-08 17:01:29 -0800498/* When booting an encrypted system, /data is not mounted when the
499 * property service is started, so any properties stored there are
500 * not loaded. Vold triggers init to load these properties once it
501 * has mounted /data.
502 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700503void load_persist_props(void) {
Nick Kralevich32b90232012-09-19 11:15:24 -0700504 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800505 /* Read persistent properties after all default values have been loaded. */
506 load_persistent_properties();
507}
508
Elliott Hughesda40c002015-03-27 23:20:44 -0700509void load_all_props() {
Andrew Boie3899f522012-10-12 15:23:40 -0700510 load_properties_from_file(PROP_PATH_SYSTEM_BUILD, NULL);
511 load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT, NULL);
Daniel Rosenbergb9512222014-11-10 17:01:33 -0800512 load_properties_from_file(PROP_PATH_VENDOR_BUILD, NULL);
Andres Morales1f1b0582015-01-13 18:10:24 -0800513 load_properties_from_file(PROP_PATH_BOOTIMAGE_BUILD, NULL);
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800514 load_properties_from_file(PROP_PATH_FACTORY, "ro.*");
515
Nick Kralevich32b90232012-09-19 11:15:24 -0700516 load_override_properties();
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800517
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 /* Read persistent properties after all default values have been loaded. */
519 load_persistent_properties();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700520}
521
Elliott Hughesda40c002015-03-27 23:20:44 -0700522void start_property_service() {
523 int fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0, NULL);
524 if (fd == -1) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 fcntl(fd, F_SETFD, FD_CLOEXEC);
527 fcntl(fd, F_SETFL, O_NONBLOCK);
528
529 listen(fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700530 property_set_fd = fd;
531}
532
Elliott Hughesda40c002015-03-27 23:20:44 -0700533int get_property_set_fd() {
Colin Crossd11beb22010-04-13 19:33:37 -0700534 return property_set_fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535}