blob: cc1ee346b5fbeacc5ecc811110780442353bb5e7 [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
29#include <cutils/misc.h>
30#include <cutils/sockets.h>
Matthew Xie40a91a22013-05-20 14:22:01 -070031#include <cutils/multiuser.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
33#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
34#include <sys/_system_properties.h>
35
36#include <sys/socket.h>
37#include <sys/un.h>
38#include <sys/select.h>
39#include <sys/types.h>
40#include <netinet/in.h>
41#include <sys/mman.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <private/android_filesystem_config.h>
43
rpcraig63207cd2012-08-09 10:05:49 -040044#include <selinux/selinux.h>
45#include <selinux/label.h>
rpcraig63207cd2012-08-09 10:05:49 -040046
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047#include "property_service.h"
48#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070049#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070050#include "log.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
52#define PERSISTENT_PROPERTY_DIR "/data/property"
53
54static int persistent_properties_loaded = 0;
Colin Cross3294bbb2010-04-19 17:11:33 -070055static int property_area_inited = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Colin Crossd11beb22010-04-13 19:33:37 -070057static int property_set_fd = -1;
58
Elliott Hughesc0e919c2015-02-04 14:46:36 -080059struct workspace {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060 size_t size;
Nick Kralevich28406472013-01-22 12:46:09 -080061 int fd;
Elliott Hughesc0e919c2015-02-04 14:46:36 -080062};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
64static int init_workspace(workspace *w, size_t size)
65{
Greg Hackmannf14eef02013-02-12 14:39:31 -080066 int fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW);
Brian Swetland25b15be2010-07-13 16:43:56 -070067 if (fd < 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068 return -1;
69
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070 w->size = size;
Nick Kralevich28406472013-01-22 12:46:09 -080071 w->fd = fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073}
74
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075static workspace pa_workspace;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
77static int init_property_area(void)
78{
Colin Cross9f5af632013-01-23 23:09:48 -080079 if (property_area_inited)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080 return -1;
81
Greg Hackmannf14eef02013-02-12 14:39:31 -080082 if(__system_property_area_init())
83 return -1;
84
85 if(init_workspace(&pa_workspace, 0))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086 return -1;
87
Nick Kralevich28406472013-01-22 12:46:09 -080088 fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC);
89
Colin Cross3294bbb2010-04-19 17:11:33 -070090 property_area_inited = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091 return 0;
92}
93
rpcraig63207cd2012-08-09 10:05:49 -040094static int check_mac_perms(const char *name, char *sctx)
95{
rpcraig63207cd2012-08-09 10:05:49 -040096 if (is_selinux_enabled() <= 0)
97 return 1;
98
99 char *tctx = NULL;
rpcraig63207cd2012-08-09 10:05:49 -0400100 int result = 0;
101
102 if (!sctx)
103 goto err;
104
105 if (!sehandle_prop)
106 goto err;
107
108 if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
109 goto err;
110
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800111 if (selinux_check_access(sctx, tctx, "property_service", "set", (void*) name) == 0)
rpcraig63207cd2012-08-09 10:05:49 -0400112 result = 1;
113
114 freecon(tctx);
115 err:
116 return result;
rpcraig63207cd2012-08-09 10:05:49 -0400117}
118
119static int check_control_mac_perms(const char *name, char *sctx)
120{
rpcraig63207cd2012-08-09 10:05:49 -0400121 /*
122 * Create a name prefix out of ctl.<service name>
123 * The new prefix allows the use of the existing
124 * property service backend labeling while avoiding
125 * mislabels based on true property prefixes.
126 */
127 char ctl_name[PROP_VALUE_MAX+4];
128 int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
129
130 if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
131 return 0;
132
133 return check_mac_perms(ctl_name, sctx);
rpcraig63207cd2012-08-09 10:05:49 -0400134}
135
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136/*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 * Checks permissions for setting system properties.
138 * Returns 1 if uid allowed, 0 otherwise.
139 */
Nick Kralevich528c13e2014-06-17 22:23:54 -0700140static int check_perms(const char *name, char *sctx)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 if(!strncmp(name, "ro.", 3))
143 name +=3;
144
Nick Kralevich528c13e2014-06-17 22:23:54 -0700145 return check_mac_perms(name, sctx);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146}
147
Colin Cross88ac54a2013-01-29 14:58:57 -0800148int __property_get(const char *name, char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149{
Colin Cross2deedfe2013-01-28 17:13:35 -0800150 return __system_property_get(name, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151}
152
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800153static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154{
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700155 char tempPath[PATH_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156 char path[PATH_MAX];
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700157 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700159 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
160 fd = mkstemp(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 if (fd < 0) {
162 ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno);
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800163 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 }
165 write(fd, value, strlen(value));
OPPOde73a0c2014-03-28 19:12:47 +0800166 fsync(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 close(fd);
168
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700169 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 if (rename(tempPath, path)) {
171 unlink(tempPath);
172 ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
173 }
174}
175
Nick Kralevich69463612013-09-13 17:21:28 -0700176static bool is_legal_property_name(const char* name, size_t namelen)
177{
178 size_t i;
Nick Kralevich69463612013-09-13 17:21:28 -0700179 if (namelen >= PROP_NAME_MAX) return false;
180 if (namelen < 1) return false;
181 if (name[0] == '.') return false;
182 if (name[namelen - 1] == '.') return false;
183
184 /* Only allow alphanumeric, plus '.', '-', or '_' */
185 /* Don't allow ".." to appear in a property name */
186 for (i = 0; i < namelen; i++) {
187 if (name[i] == '.') {
Nick Kralevichc2c5a242013-09-16 11:30:48 -0700188 // i=0 is guaranteed to never have a dot. See above.
189 if (name[i-1] == '.') return false;
Nick Kralevich69463612013-09-13 17:21:28 -0700190 continue;
191 }
Nick Kralevich69463612013-09-13 17:21:28 -0700192 if (name[i] == '_' || name[i] == '-') continue;
193 if (name[i] >= 'a' && name[i] <= 'z') continue;
194 if (name[i] >= 'A' && name[i] <= 'Z') continue;
195 if (name[i] >= '0' && name[i] <= '9') continue;
196 return false;
197 }
198
199 return true;
200}
201
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202int property_set(const char *name, const char *value)
203{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 prop_info *pi;
Colin Cross9f5af632013-01-23 23:09:48 -0800205 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700207 size_t namelen = strlen(name);
208 size_t valuelen = strlen(value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209
Nick Kralevich69463612013-09-13 17:21:28 -0700210 if (!is_legal_property_name(name, namelen)) return -1;
211 if (valuelen >= PROP_VALUE_MAX) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212
213 pi = (prop_info*) __system_property_find(name);
214
215 if(pi != 0) {
216 /* ro.* properties may NEVER be modified once set */
217 if(!strncmp(name, "ro.", 3)) return -1;
218
Colin Cross9f5af632013-01-23 23:09:48 -0800219 __system_property_update(pi, value, valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 } else {
Colin Cross9f5af632013-01-23 23:09:48 -0800221 ret = __system_property_add(name, namelen, value, valuelen);
222 if (ret < 0) {
Nick Kralevich69463612013-09-13 17:21:28 -0700223 ERROR("Failed to set '%s'='%s'\n", name, value);
Colin Cross9f5af632013-01-23 23:09:48 -0800224 return ret;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200225 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 }
227 /* If name starts with "net." treat as a DNS property. */
Mike Lockwoodb3779552009-05-08 14:27:42 -0400228 if (strncmp("net.", name, strlen("net.")) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 if (strcmp("net.change", name) == 0) {
230 return 0;
231 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800232 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 * The 'net.change' property is a special property used track when any
234 * 'net.*' property name is updated. It is _ONLY_ updated here. Its value
235 * contains the last updated 'net.*' property.
236 */
237 property_set("net.change", name);
238 } else if (persistent_properties_loaded &&
Mike Lockwoodb3779552009-05-08 14:27:42 -0400239 strncmp("persist.", name, strlen("persist.")) == 0) {
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800240 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 * Don't write properties to disk until after we have read all default properties
242 * to prevent them from being overwritten by default values.
243 */
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800244 write_persistent_property(name, value);
repo sync8a387872013-05-17 12:47:04 -0700245 } else if (strcmp("selinux.reload_policy", name) == 0 &&
246 strcmp("1", value) == 0) {
247 selinux_reload_policy();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 }
249 property_changed(name, value);
250 return 0;
251}
252
Colin Crossd11beb22010-04-13 19:33:37 -0700253void handle_property_set_fd()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254{
255 prop_msg msg;
256 int s;
257 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 struct ucred cr;
259 struct sockaddr_un addr;
260 socklen_t addr_size = sizeof(addr);
261 socklen_t cr_size = sizeof(cr);
rpcraig63207cd2012-08-09 10:05:49 -0400262 char * source_ctx = NULL;
JP Abgrall4515d812014-01-31 14:37:07 -0800263 struct pollfd ufds[1];
264 const int timeout_ms = 2 * 1000; /* Default 2 sec timeout for caller to send property. */
265 int nr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266
Colin Crossd11beb22010-04-13 19:33:37 -0700267 if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268 return;
269 }
270
271 /* Check socket options here */
272 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
273 close(s);
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700274 ERROR("Unable to receive socket options\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275 return;
276 }
277
JP Abgrall4515d812014-01-31 14:37:07 -0800278 ufds[0].fd = s;
279 ufds[0].events = POLLIN;
280 ufds[0].revents = 0;
281 nr = TEMP_FAILURE_RETRY(poll(ufds, 1, timeout_ms));
282 if (nr == 0) {
283 ERROR("sys_prop: timeout waiting for uid=%d to send property message.\n", cr.uid);
284 close(s);
285 return;
286 } else if (nr < 0) {
287 ERROR("sys_prop: error waiting for uid=%d to send property message. err=%d %s\n", cr.uid, errno, strerror(errno));
288 close(s);
289 return;
290 }
291
292 r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), MSG_DONTWAIT));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 if(r != sizeof(prop_msg)) {
Elliott Hughesccecf142014-01-16 10:53:11 -0800294 ERROR("sys_prop: mis-match msg size received: %d expected: %zu errno: %d\n",
Mike Lockwoodd18678f2011-07-01 14:48:50 -0400295 r, sizeof(prop_msg), errno);
Brad Fitzpatrick9f1e0e32011-03-30 12:39:56 -0700296 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 return;
298 }
299
300 switch(msg.cmd) {
301 case PROP_MSG_SETPROP:
302 msg.name[PROP_NAME_MAX-1] = 0;
303 msg.value[PROP_VALUE_MAX-1] = 0;
304
Nick Kralevich69463612013-09-13 17:21:28 -0700305 if (!is_legal_property_name(msg.name, strlen(msg.name))) {
306 ERROR("sys_prop: illegal property name. Got: \"%s\"\n", msg.name);
307 close(s);
308 return;
309 }
310
rpcraig63207cd2012-08-09 10:05:49 -0400311 getpeercon(s, &source_ctx);
rpcraig63207cd2012-08-09 10:05:49 -0400312
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313 if(memcmp(msg.name,"ctl.",4) == 0) {
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700314 // Keep the old close-socket-early behavior when handling
315 // ctl.* properties.
316 close(s);
Nick Kralevich528c13e2014-06-17 22:23:54 -0700317 if (check_control_mac_perms(msg.value, source_ctx)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318 handle_control_message((char*) msg.name + 4, (char*) msg.value);
319 } else {
Wink Savillecfa0d842010-10-03 13:30:11 -0700320 ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",
321 msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 }
323 } else {
Nick Kralevich528c13e2014-06-17 22:23:54 -0700324 if (check_perms(msg.name, source_ctx)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 property_set((char*) msg.name, (char*) msg.value);
326 } else {
327 ERROR("sys_prop: permission denied uid:%d name:%s\n",
328 cr.uid, msg.name);
329 }
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700330
331 // Note: bionic's property client code assumes that the
332 // property server will not close the socket until *AFTER*
333 // the property is written to memory.
334 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 }
rpcraig63207cd2012-08-09 10:05:49 -0400336 freecon(source_ctx);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 break;
338
339 default:
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700340 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 break;
342 }
343}
344
Nick Kralevich28406472013-01-22 12:46:09 -0800345void get_property_workspace(int *fd, int *sz)
346{
347 *fd = pa_workspace.fd;
348 *sz = pa_workspace.size;
349}
350
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800351static void load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800353/*
354 * Filter is used to decide which properties to load: NULL loads all keys,
355 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
356 */
357static void load_properties(char *data, const char *filter)
358{
359 char *key, *value, *eol, *sol, *tmp, *fn;
360 size_t flen = 0;
361
362 if (filter) {
363 flen = strlen(filter);
364 }
365
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800367 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368 key = sol;
369 *eol++ = 0;
370 sol = eol;
371
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800372 while (isspace(*key)) key++;
373 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800376 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800378 if (!strncmp(key, "import ", 7) && flen == 0) {
379 fn = key + 7;
380 while (isspace(*fn)) fn++;
381
382 key = strchr(fn, ' ');
383 if (key) {
384 *key++ = 0;
385 while (isspace(*key)) key++;
386 }
387
388 load_properties_from_file(fn, key);
389
390 } else {
391 value = strchr(key, '=');
392 if (!value) continue;
393 *value++ = 0;
394
395 tmp = value - 2;
396 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
397
398 while (isspace(*value)) value++;
399
400 if (flen > 0) {
401 if (filter[flen - 1] == '*') {
402 if (strncmp(key, filter, flen - 1)) continue;
403 } else {
404 if (strcmp(key, filter)) continue;
405 }
406 }
407
408 property_set(key, value);
409 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 }
411}
412
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800413/*
414 * Filter is used to decide which properties to load: NULL loads all keys,
415 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
416 */
417static void load_properties_from_file(const char *fn, const char *filter)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418{
419 char *data;
420 unsigned sz;
421
422 data = read_file(fn, &sz);
423
424 if(data != 0) {
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800425 load_properties(data, filter);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 free(data);
427 }
428}
429
430static void load_persistent_properties()
431{
432 DIR* dir = opendir(PERSISTENT_PROPERTY_DIR);
Yi-wei Zhaoe41bc312012-10-23 21:09:56 +0800433 int dir_fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434 struct dirent* entry;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 char value[PROP_VALUE_MAX];
436 int fd, length;
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700437 struct stat sb;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438
439 if (dir) {
Yi-wei Zhaoe41bc312012-10-23 21:09:56 +0800440 dir_fd = dirfd(dir);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 while ((entry = readdir(dir)) != NULL) {
Mike Lockwoodb3779552009-05-08 14:27:42 -0400442 if (strncmp("persist.", entry->d_name, strlen("persist.")))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 if (entry->d_type != DT_REG)
445 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446 /* open the file and read the property value */
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700447 fd = openat(dir_fd, entry->d_name, O_RDONLY | O_NOFOLLOW);
448 if (fd < 0) {
449 ERROR("Unable to open persistent property file \"%s\" errno: %d\n",
450 entry->d_name, errno);
451 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452 }
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700453 if (fstat(fd, &sb) < 0) {
454 ERROR("fstat on property file \"%s\" failed errno: %d\n", entry->d_name, errno);
455 close(fd);
456 continue;
457 }
458
459 // File must not be accessible to others, be owned by root/root, and
460 // not be a hard link to any other file.
461 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0)
462 || (sb.st_uid != 0)
463 || (sb.st_gid != 0)
464 || (sb.st_nlink != 1)) {
Dmitriy Ivanov3107a2f2015-02-03 16:42:09 -0800465 ERROR("skipping insecure property file %s (uid=%u gid=%u nlink=%u mode=%o)\n",
Mark Salyzyn0aff05e2014-03-17 08:38:37 -0700466 entry->d_name, (unsigned int)sb.st_uid, (unsigned int)sb.st_gid,
Dmitriy Ivanov3107a2f2015-02-03 16:42:09 -0800467 (unsigned int)sb.st_nlink, sb.st_mode);
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700468 close(fd);
469 continue;
470 }
471
472 length = read(fd, value, sizeof(value) - 1);
473 if (length >= 0) {
474 value[length] = 0;
475 property_set(entry->d_name, value);
476 } else {
477 ERROR("Unable to read persistent property file %s errno: %d\n",
478 entry->d_name, errno);
479 }
480 close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 }
482 closedir(dir);
483 } else {
484 ERROR("Unable to open persistent property directory %s errno: %d\n", PERSISTENT_PROPERTY_DIR, errno);
485 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800486
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 persistent_properties_loaded = 1;
488}
489
Dima Zavin88861122011-12-19 11:21:32 -0800490void property_init(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491{
492 init_property_area();
Dima Zavin88861122011-12-19 11:21:32 -0800493}
494
495void property_load_boot_defaults(void)
496{
Andrew Boie3899f522012-10-12 15:23:40 -0700497 load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498}
499
Colin Cross3294bbb2010-04-19 17:11:33 -0700500int properties_inited(void)
501{
502 return property_area_inited;
503}
504
Nick Kralevich32b90232012-09-19 11:15:24 -0700505static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800506 if (ALLOW_LOCAL_PROP_OVERRIDE) {
507 char debuggable[PROP_VALUE_MAX];
508 int ret = property_get("ro.debuggable", debuggable);
509 if (ret && (strcmp(debuggable, "1") == 0)) {
510 load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
511 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700512 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700513}
514
515
Ken Sumrallc5c51032011-03-08 17:01:29 -0800516/* When booting an encrypted system, /data is not mounted when the
517 * property service is started, so any properties stored there are
518 * not loaded. Vold triggers init to load these properties once it
519 * has mounted /data.
520 */
521void load_persist_props(void)
522{
Nick Kralevich32b90232012-09-19 11:15:24 -0700523 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800524 /* Read persistent properties after all default values have been loaded. */
525 load_persistent_properties();
526}
527
Riley Andrewse4b7b292014-06-16 15:06:21 -0700528void load_all_props(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529{
Andrew Boie3899f522012-10-12 15:23:40 -0700530 load_properties_from_file(PROP_PATH_SYSTEM_BUILD, NULL);
531 load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT, NULL);
Daniel Rosenbergb9512222014-11-10 17:01:33 -0800532 load_properties_from_file(PROP_PATH_VENDOR_BUILD, NULL);
Andres Morales1f1b0582015-01-13 18:10:24 -0800533 load_properties_from_file(PROP_PATH_BOOTIMAGE_BUILD, NULL);
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800534 load_properties_from_file(PROP_PATH_FACTORY, "ro.*");
535
Nick Kralevich32b90232012-09-19 11:15:24 -0700536 load_override_properties();
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800537
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800538 /* Read persistent properties after all default values have been loaded. */
539 load_persistent_properties();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700540}
541
542void start_property_service(void)
543{
544 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545
Stephen Smalley8348d272013-05-13 12:37:04 -0400546 fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0, NULL);
Colin Crossd11beb22010-04-13 19:33:37 -0700547 if(fd < 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548 fcntl(fd, F_SETFD, FD_CLOEXEC);
549 fcntl(fd, F_SETFL, O_NONBLOCK);
550
551 listen(fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700552 property_set_fd = fd;
553}
554
555int get_property_set_fd()
556{
557 return property_set_fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558}