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