1 /*
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>
27
28 #include <cutils/misc.h>
29 #include <cutils/sockets.h>
30 #include <cutils/multiuser.h>
31
32 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
33 #include <sys/_system_properties.h>
34
35 #include <sys/socket.h>
36 #include <sys/un.h>
37 #include <sys/select.h>
38 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <sys/mman.h>
41 #include <sys/atomics.h>
42 #include <private/android_filesystem_config.h>
43
44 #include <selinux/selinux.h>
45 #include <selinux/label.h>
46
47 #include "property_service.h"
48 #include "init.h"
49 #include "util.h"
50 #include "log.h"
51
52 #define PERSISTENT_PROPERTY_DIR "/data/property"
53
54 static int persistent_properties_loaded = 0;
55 static int property_area_inited = 0;
56
57 static int property_set_fd = -1;
58
59 /* White list of permissions for setting property services. */
60 struct {
61 const char *prefix;
62 unsigned int uid;
63 unsigned int gid;
64 } property_perms[] = {
65 { "net.rmnet0.", AID_RADIO, 0 },
66 { "net.gprs.", AID_RADIO, 0 },
67 { "net.ppp", AID_RADIO, 0 },
68 { "net.qmi", AID_RADIO, 0 },
69 { "net.lte", AID_RADIO, 0 },
70 { "net.cdma", AID_RADIO, 0 },
71 { "ril.", AID_RADIO, 0 },
72 { "gsm.", AID_RADIO, 0 },
73 { "persist.radio", AID_RADIO, 0 },
74 { "net.dns", AID_RADIO, 0 },
75 { "sys.usb.config", AID_RADIO, 0 },
76 { "net.", AID_SYSTEM, 0 },
77 { "dev.", AID_SYSTEM, 0 },
78 { "runtime.", AID_SYSTEM, 0 },
79 { "hw.", AID_SYSTEM, 0 },
80 { "sys.", AID_SYSTEM, 0 },
81 { "sys.powerctl", AID_SHELL, 0 },
82 { "service.", AID_SYSTEM, 0 },
83 { "wlan.", AID_SYSTEM, 0 },
84 { "bluetooth.", AID_BLUETOOTH, 0 },
85 { "dhcp.", AID_SYSTEM, 0 },
86 { "dhcp.", AID_DHCP, 0 },
87 { "debug.", AID_SYSTEM, 0 },
88 { "debug.", AID_SHELL, 0 },
89 { "log.", AID_SHELL, 0 },
90 { "service.adb.root", AID_SHELL, 0 },
91 { "service.adb.tcp.port", AID_SHELL, 0 },
92 { "persist.sys.", AID_SYSTEM, 0 },
93 { "persist.service.", AID_SYSTEM, 0 },
94 { "persist.security.", AID_SYSTEM, 0 },
95 { "persist.service.bdroid.", AID_BLUETOOTH, 0 },
96 { "selinux." , AID_SYSTEM, 0 },
97 { NULL, 0, 0 }
98 };
99
100 /*
101 * White list of UID that are allowed to start/stop services.
102 * Currently there are no user apps that require.
103 */
104 struct {
105 const char *service;
106 unsigned int uid;
107 unsigned int gid;
108 } control_perms[] = {
109 { "dumpstate",AID_SHELL, AID_LOG },
110 { "ril-daemon",AID_RADIO, AID_RADIO },
111 {NULL, 0, 0 }
112 };
113
114 typedef struct {
115 size_t size;
116 int fd;
117 } workspace;
118
init_workspace(workspace * w,size_t size)119 static int init_workspace(workspace *w, size_t size)
120 {
121 void *data;
122 int fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW);
123 if (fd < 0)
124 return -1;
125
126 w->size = size;
127 w->fd = fd;
128 return 0;
129 }
130
131 static workspace pa_workspace;
132
init_property_area(void)133 static int init_property_area(void)
134 {
135 if (property_area_inited)
136 return -1;
137
138 if(__system_property_area_init())
139 return -1;
140
141 if(init_workspace(&pa_workspace, 0))
142 return -1;
143
144 fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC);
145
146 property_area_inited = 1;
147 return 0;
148 }
149
check_mac_perms(const char * name,char * sctx)150 static int check_mac_perms(const char *name, char *sctx)
151 {
152 if (is_selinux_enabled() <= 0)
153 return 1;
154
155 char *tctx = NULL;
156 const char *class = "property_service";
157 const char *perm = "set";
158 int result = 0;
159
160 if (!sctx)
161 goto err;
162
163 if (!sehandle_prop)
164 goto err;
165
166 if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
167 goto err;
168
169 if (selinux_check_access(sctx, tctx, class, perm, name) == 0)
170 result = 1;
171
172 freecon(tctx);
173 err:
174 return result;
175 }
176
check_control_mac_perms(const char * name,char * sctx)177 static int check_control_mac_perms(const char *name, char *sctx)
178 {
179 /*
180 * Create a name prefix out of ctl.<service name>
181 * The new prefix allows the use of the existing
182 * property service backend labeling while avoiding
183 * mislabels based on true property prefixes.
184 */
185 char ctl_name[PROP_VALUE_MAX+4];
186 int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
187
188 if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
189 return 0;
190
191 return check_mac_perms(ctl_name, sctx);
192 }
193
194 /*
195 * Checks permissions for starting/stoping system services.
196 * AID_SYSTEM and AID_ROOT are always allowed.
197 *
198 * Returns 1 if uid allowed, 0 otherwise.
199 */
check_control_perms(const char * name,unsigned int uid,unsigned int gid,char * sctx)200 static int check_control_perms(const char *name, unsigned int uid, unsigned int gid, char *sctx) {
201
202 int i;
203 if (uid == AID_SYSTEM || uid == AID_ROOT)
204 return check_control_mac_perms(name, sctx);
205
206 /* Search the ACL */
207 for (i = 0; control_perms[i].service; i++) {
208 if (strcmp(control_perms[i].service, name) == 0) {
209 if ((uid && control_perms[i].uid == uid) ||
210 (gid && control_perms[i].gid == gid)) {
211 return check_control_mac_perms(name, sctx);
212 }
213 }
214 }
215 return 0;
216 }
217
218 /*
219 * Checks permissions for setting system properties.
220 * Returns 1 if uid allowed, 0 otherwise.
221 */
check_perms(const char * name,unsigned int uid,unsigned int gid,char * sctx)222 static int check_perms(const char *name, unsigned int uid, unsigned int gid, char *sctx)
223 {
224 int i;
225 unsigned int app_id;
226
227 if(!strncmp(name, "ro.", 3))
228 name +=3;
229
230 if (uid == 0)
231 return check_mac_perms(name, sctx);
232
233 app_id = multiuser_get_app_id(uid);
234 if (app_id == AID_BLUETOOTH) {
235 uid = app_id;
236 }
237
238 for (i = 0; property_perms[i].prefix; i++) {
239 if (strncmp(property_perms[i].prefix, name,
240 strlen(property_perms[i].prefix)) == 0) {
241 if ((uid && property_perms[i].uid == uid) ||
242 (gid && property_perms[i].gid == gid)) {
243
244 return check_mac_perms(name, sctx);
245 }
246 }
247 }
248
249 return 0;
250 }
251
__property_get(const char * name,char * value)252 int __property_get(const char *name, char *value)
253 {
254 return __system_property_get(name, value);
255 }
256
write_persistent_property(const char * name,const char * value)257 static void write_persistent_property(const char *name, const char *value)
258 {
259 char tempPath[PATH_MAX];
260 char path[PATH_MAX];
261 int fd;
262
263 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
264 fd = mkstemp(tempPath);
265 if (fd < 0) {
266 ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno);
267 return;
268 }
269 write(fd, value, strlen(value));
270 close(fd);
271
272 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
273 if (rename(tempPath, path)) {
274 unlink(tempPath);
275 ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
276 }
277 }
278
is_legal_property_name(const char * name,size_t namelen)279 static bool is_legal_property_name(const char* name, size_t namelen)
280 {
281 size_t i;
282 bool previous_was_dot = false;
283 if (namelen >= PROP_NAME_MAX) return false;
284 if (namelen < 1) return false;
285 if (name[0] == '.') return false;
286 if (name[namelen - 1] == '.') return false;
287
288 /* Only allow alphanumeric, plus '.', '-', or '_' */
289 /* Don't allow ".." to appear in a property name */
290 for (i = 0; i < namelen; i++) {
291 if (name[i] == '.') {
292 if (previous_was_dot == true) return false;
293 previous_was_dot = true;
294 continue;
295 }
296 previous_was_dot = false;
297 if (name[i] == '_' || name[i] == '-') continue;
298 if (name[i] >= 'a' && name[i] <= 'z') continue;
299 if (name[i] >= 'A' && name[i] <= 'Z') continue;
300 if (name[i] >= '0' && name[i] <= '9') continue;
301 return false;
302 }
303
304 return true;
305 }
306
property_set(const char * name,const char * value)307 int property_set(const char *name, const char *value)
308 {
309 prop_info *pi;
310 int ret;
311
312 size_t namelen = strlen(name);
313 size_t valuelen = strlen(value);
314
315 if (!is_legal_property_name(name, namelen)) return -1;
316 if (valuelen >= PROP_VALUE_MAX) return -1;
317
318 pi = (prop_info*) __system_property_find(name);
319
320 if(pi != 0) {
321 /* ro.* properties may NEVER be modified once set */
322 if(!strncmp(name, "ro.", 3)) return -1;
323
324 __system_property_update(pi, value, valuelen);
325 } else {
326 ret = __system_property_add(name, namelen, value, valuelen);
327 if (ret < 0) {
328 ERROR("Failed to set '%s'='%s'\n", name, value);
329 return ret;
330 }
331 }
332 /* If name starts with "net." treat as a DNS property. */
333 if (strncmp("net.", name, strlen("net.")) == 0) {
334 if (strcmp("net.change", name) == 0) {
335 return 0;
336 }
337 /*
338 * The 'net.change' property is a special property used track when any
339 * 'net.*' property name is updated. It is _ONLY_ updated here. Its value
340 * contains the last updated 'net.*' property.
341 */
342 property_set("net.change", name);
343 } else if (persistent_properties_loaded &&
344 strncmp("persist.", name, strlen("persist.")) == 0) {
345 /*
346 * Don't write properties to disk until after we have read all default properties
347 * to prevent them from being overwritten by default values.
348 */
349 write_persistent_property(name, value);
350 } else if (strcmp("selinux.reload_policy", name) == 0 &&
351 strcmp("1", value) == 0) {
352 selinux_reload_policy();
353 }
354 property_changed(name, value);
355 return 0;
356 }
357
handle_property_set_fd()358 void handle_property_set_fd()
359 {
360 prop_msg msg;
361 int s;
362 int r;
363 int res;
364 struct ucred cr;
365 struct sockaddr_un addr;
366 socklen_t addr_size = sizeof(addr);
367 socklen_t cr_size = sizeof(cr);
368 char * source_ctx = NULL;
369
370 if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
371 return;
372 }
373
374 /* Check socket options here */
375 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
376 close(s);
377 ERROR("Unable to receive socket options\n");
378 return;
379 }
380
381 r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), 0));
382 if(r != sizeof(prop_msg)) {
383 ERROR("sys_prop: mis-match msg size received: %d expected: %d errno: %d\n",
384 r, sizeof(prop_msg), errno);
385 close(s);
386 return;
387 }
388
389 switch(msg.cmd) {
390 case PROP_MSG_SETPROP:
391 msg.name[PROP_NAME_MAX-1] = 0;
392 msg.value[PROP_VALUE_MAX-1] = 0;
393
394 if (!is_legal_property_name(msg.name, strlen(msg.name))) {
395 ERROR("sys_prop: illegal property name. Got: \"%s\"\n", msg.name);
396 close(s);
397 return;
398 }
399
400 getpeercon(s, &source_ctx);
401
402 if(memcmp(msg.name,"ctl.",4) == 0) {
403 // Keep the old close-socket-early behavior when handling
404 // ctl.* properties.
405 close(s);
406 if (check_control_perms(msg.value, cr.uid, cr.gid, source_ctx)) {
407 handle_control_message((char*) msg.name + 4, (char*) msg.value);
408 } else {
409 ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",
410 msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
411 }
412 } else {
413 if (check_perms(msg.name, cr.uid, cr.gid, source_ctx)) {
414 property_set((char*) msg.name, (char*) msg.value);
415 } else {
416 ERROR("sys_prop: permission denied uid:%d name:%s\n",
417 cr.uid, msg.name);
418 }
419
420 // Note: bionic's property client code assumes that the
421 // property server will not close the socket until *AFTER*
422 // the property is written to memory.
423 close(s);
424 }
425 freecon(source_ctx);
426 break;
427
428 default:
429 close(s);
430 break;
431 }
432 }
433
get_property_workspace(int * fd,int * sz)434 void get_property_workspace(int *fd, int *sz)
435 {
436 *fd = pa_workspace.fd;
437 *sz = pa_workspace.size;
438 }
439
load_properties(char * data)440 static void load_properties(char *data)
441 {
442 char *key, *value, *eol, *sol, *tmp;
443
444 sol = data;
445 while((eol = strchr(sol, '\n'))) {
446 key = sol;
447 *eol++ = 0;
448 sol = eol;
449
450 value = strchr(key, '=');
451 if(value == 0) continue;
452 *value++ = 0;
453
454 while(isspace(*key)) key++;
455 if(*key == '#') continue;
456 tmp = value - 2;
457 while((tmp > key) && isspace(*tmp)) *tmp-- = 0;
458
459 while(isspace(*value)) value++;
460 tmp = eol - 2;
461 while((tmp > value) && isspace(*tmp)) *tmp-- = 0;
462
463 property_set(key, value);
464 }
465 }
466
load_properties_from_file(const char * fn)467 static void load_properties_from_file(const char *fn)
468 {
469 char *data;
470 unsigned sz;
471
472 data = read_file(fn, &sz);
473
474 if(data != 0) {
475 load_properties(data);
476 free(data);
477 }
478 }
479
load_persistent_properties()480 static void load_persistent_properties()
481 {
482 DIR* dir = opendir(PERSISTENT_PROPERTY_DIR);
483 int dir_fd;
484 struct dirent* entry;
485 char value[PROP_VALUE_MAX];
486 int fd, length;
487 struct stat sb;
488
489 if (dir) {
490 dir_fd = dirfd(dir);
491 while ((entry = readdir(dir)) != NULL) {
492 if (strncmp("persist.", entry->d_name, strlen("persist.")))
493 continue;
494 #if HAVE_DIRENT_D_TYPE
495 if (entry->d_type != DT_REG)
496 continue;
497 #endif
498 /* open the file and read the property value */
499 fd = openat(dir_fd, entry->d_name, O_RDONLY | O_NOFOLLOW);
500 if (fd < 0) {
501 ERROR("Unable to open persistent property file \"%s\" errno: %d\n",
502 entry->d_name, errno);
503 continue;
504 }
505 if (fstat(fd, &sb) < 0) {
506 ERROR("fstat on property file \"%s\" failed errno: %d\n", entry->d_name, errno);
507 close(fd);
508 continue;
509 }
510
511 // File must not be accessible to others, be owned by root/root, and
512 // not be a hard link to any other file.
513 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0)
514 || (sb.st_uid != 0)
515 || (sb.st_gid != 0)
516 || (sb.st_nlink != 1)) {
517 ERROR("skipping insecure property file %s (uid=%lu gid=%lu nlink=%d mode=%o)\n",
518 entry->d_name, sb.st_uid, sb.st_gid, sb.st_nlink, sb.st_mode);
519 close(fd);
520 continue;
521 }
522
523 length = read(fd, value, sizeof(value) - 1);
524 if (length >= 0) {
525 value[length] = 0;
526 property_set(entry->d_name, value);
527 } else {
528 ERROR("Unable to read persistent property file %s errno: %d\n",
529 entry->d_name, errno);
530 }
531 close(fd);
532 }
533 closedir(dir);
534 } else {
535 ERROR("Unable to open persistent property directory %s errno: %d\n", PERSISTENT_PROPERTY_DIR, errno);
536 }
537
538 persistent_properties_loaded = 1;
539 }
540
property_init(void)541 void property_init(void)
542 {
543 init_property_area();
544 }
545
property_load_boot_defaults(void)546 void property_load_boot_defaults(void)
547 {
548 load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT);
549 }
550
properties_inited(void)551 int properties_inited(void)
552 {
553 return property_area_inited;
554 }
555
load_override_properties()556 static void load_override_properties() {
557 #ifdef ALLOW_LOCAL_PROP_OVERRIDE
558 char debuggable[PROP_VALUE_MAX];
559 int ret;
560
561 ret = property_get("ro.debuggable", debuggable);
562 if (ret && (strcmp(debuggable, "1") == 0)) {
563 load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);
564 }
565 #endif /* ALLOW_LOCAL_PROP_OVERRIDE */
566 }
567
568
569 /* When booting an encrypted system, /data is not mounted when the
570 * property service is started, so any properties stored there are
571 * not loaded. Vold triggers init to load these properties once it
572 * has mounted /data.
573 */
load_persist_props(void)574 void load_persist_props(void)
575 {
576 load_override_properties();
577 /* Read persistent properties after all default values have been loaded. */
578 load_persistent_properties();
579 }
580
start_property_service(void)581 void start_property_service(void)
582 {
583 int fd;
584
585 load_properties_from_file(PROP_PATH_SYSTEM_BUILD);
586 load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
587 load_override_properties();
588 /* Read persistent properties after all default values have been loaded. */
589 load_persistent_properties();
590
591 fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0);
592 if(fd < 0) return;
593 fcntl(fd, F_SETFD, FD_CLOEXEC);
594 fcntl(fd, F_SETFL, O_NONBLOCK);
595
596 listen(fd, 8);
597 property_set_fd = fd;
598 }
599
get_property_set_fd()600 int get_property_set_fd()
601 {
602 return property_set_fd;
603 }
604