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