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