property_service.c revision 63207cd20f2156bec937a85e789f07de0d1afa9d
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 55static int persistent_properties_loaded = 0; 56static int property_area_inited = 0; 57 58static int property_set_fd = -1; 59 60/* White list of permissions for setting property services. */ 61struct { 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 { "dhcp.", AID_SYSTEM, 0 }, 85 { "dhcp.", AID_DHCP, 0 }, 86 { "debug.", AID_SYSTEM, 0 }, 87 { "debug.", AID_SHELL, 0 }, 88 { "log.", AID_SHELL, 0 }, 89 { "service.adb.root", AID_SHELL, 0 }, 90 { "service.adb.tcp.port", AID_SHELL, 0 }, 91 { "persist.sys.", AID_SYSTEM, 0 }, 92 { "persist.service.", AID_SYSTEM, 0 }, 93 { "persist.security.", AID_SYSTEM, 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, 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); 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#ifdef HAVE_SELINUX 203 if (is_selinux_enabled() <= 0) 204 return 1; 205 206 char *tctx = NULL; 207 const char *class = "property_service"; 208 const char *perm = "set"; 209 int result = 0; 210 211 if (!sctx) 212 goto err; 213 214 if (!sehandle_prop) 215 goto err; 216 217 if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0) 218 goto err; 219 220 if (selinux_check_access(sctx, tctx, class, perm, name) == 0) 221 result = 1; 222 223 freecon(tctx); 224 err: 225 return result; 226 227#endif 228 return 1; 229} 230 231static int check_control_mac_perms(const char *name, char *sctx) 232{ 233#ifdef HAVE_SELINUX 234 235 /* 236 * Create a name prefix out of ctl.<service name> 237 * The new prefix allows the use of the existing 238 * property service backend labeling while avoiding 239 * mislabels based on true property prefixes. 240 */ 241 char ctl_name[PROP_VALUE_MAX+4]; 242 int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name); 243 244 if (ret < 0 || (size_t) ret >= sizeof(ctl_name)) 245 return 0; 246 247 return check_mac_perms(ctl_name, sctx); 248 249#endif 250 return 1; 251} 252 253/* 254 * Checks permissions for starting/stoping system services. 255 * AID_SYSTEM and AID_ROOT are always allowed. 256 * 257 * Returns 1 if uid allowed, 0 otherwise. 258 */ 259static int check_control_perms(const char *name, unsigned int uid, unsigned int gid, char *sctx) { 260 261 int i; 262 if (uid == AID_SYSTEM || uid == AID_ROOT) 263 return check_control_mac_perms(name, sctx); 264 265 /* Search the ACL */ 266 for (i = 0; control_perms[i].service; i++) { 267 if (strcmp(control_perms[i].service, name) == 0) { 268 if ((uid && control_perms[i].uid == uid) || 269 (gid && control_perms[i].gid == gid)) { 270 return check_control_mac_perms(name, sctx); 271 } 272 } 273 } 274 return 0; 275} 276 277/* 278 * Checks permissions for setting system properties. 279 * Returns 1 if uid allowed, 0 otherwise. 280 */ 281static int check_perms(const char *name, unsigned int uid, unsigned int gid, char *sctx) 282{ 283 int i; 284 if(!strncmp(name, "ro.", 3)) 285 name +=3; 286 287 if (uid == 0) 288 return check_mac_perms(name, sctx); 289 290 for (i = 0; property_perms[i].prefix; i++) { 291 if (strncmp(property_perms[i].prefix, name, 292 strlen(property_perms[i].prefix)) == 0) { 293 if ((uid && property_perms[i].uid == uid) || 294 (gid && property_perms[i].gid == gid)) { 295 296 return check_mac_perms(name, sctx); 297 } 298 } 299 } 300 301 return 0; 302} 303 304const char* property_get(const char *name) 305{ 306 prop_info *pi; 307 308 if(strlen(name) >= PROP_NAME_MAX) return 0; 309 310 pi = (prop_info*) __system_property_find(name); 311 312 if(pi != 0) { 313 return pi->value; 314 } else { 315 return 0; 316 } 317} 318 319static void write_persistent_property(const char *name, const char *value) 320{ 321 const char *tempPath = PERSISTENT_PROPERTY_DIR "/.temp"; 322 char path[PATH_MAX]; 323 int fd, length; 324 325 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name); 326 327 fd = open(tempPath, O_WRONLY|O_CREAT|O_TRUNC, 0600); 328 if (fd < 0) { 329 ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno); 330 return; 331 } 332 write(fd, value, strlen(value)); 333 close(fd); 334 335 if (rename(tempPath, path)) { 336 unlink(tempPath); 337 ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path); 338 } 339} 340 341int property_set(const char *name, const char *value) 342{ 343 prop_area *pa; 344 prop_info *pi; 345 346 int namelen = strlen(name); 347 int valuelen = strlen(value); 348 349 if(namelen >= PROP_NAME_MAX) return -1; 350 if(valuelen >= PROP_VALUE_MAX) return -1; 351 if(namelen < 1) return -1; 352 353 pi = (prop_info*) __system_property_find(name); 354 355 if(pi != 0) { 356 /* ro.* properties may NEVER be modified once set */ 357 if(!strncmp(name, "ro.", 3)) return -1; 358 359 pa = __system_property_area__; 360 update_prop_info(pi, value, valuelen); 361 pa->serial++; 362 __futex_wake(&pa->serial, INT32_MAX); 363 } else { 364 pa = __system_property_area__; 365 if(pa->count == PA_COUNT_MAX) return -1; 366 367 pi = pa_info_array + pa->count; 368 pi->serial = (valuelen << 24); 369 memcpy(pi->name, name, namelen + 1); 370 memcpy(pi->value, value, valuelen + 1); 371 372 pa->toc[pa->count] = 373 (namelen << 24) | (((unsigned) pi) - ((unsigned) pa)); 374 375 pa->count++; 376 pa->serial++; 377 __futex_wake(&pa->serial, INT32_MAX); 378 } 379 /* If name starts with "net." treat as a DNS property. */ 380 if (strncmp("net.", name, strlen("net.")) == 0) { 381 if (strcmp("net.change", name) == 0) { 382 return 0; 383 } 384 /* 385 * The 'net.change' property is a special property used track when any 386 * 'net.*' property name is updated. It is _ONLY_ updated here. Its value 387 * contains the last updated 'net.*' property. 388 */ 389 property_set("net.change", name); 390 } else if (persistent_properties_loaded && 391 strncmp("persist.", name, strlen("persist.")) == 0) { 392 /* 393 * Don't write properties to disk until after we have read all default properties 394 * to prevent them from being overwritten by default values. 395 */ 396 write_persistent_property(name, value); 397#ifdef HAVE_SELINUX 398 } else if (strcmp("selinux.reload_policy", name) == 0 && 399 strcmp("1", value) == 0) { 400 selinux_reload_policy(); 401#endif 402 } 403 property_changed(name, value); 404 return 0; 405} 406 407void handle_property_set_fd() 408{ 409 prop_msg msg; 410 int s; 411 int r; 412 int res; 413 struct ucred cr; 414 struct sockaddr_un addr; 415 socklen_t addr_size = sizeof(addr); 416 socklen_t cr_size = sizeof(cr); 417 char * source_ctx = NULL; 418 419 if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) { 420 return; 421 } 422 423 /* Check socket options here */ 424 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) { 425 close(s); 426 ERROR("Unable to recieve socket options\n"); 427 return; 428 } 429 430 r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), 0)); 431 if(r != sizeof(prop_msg)) { 432 ERROR("sys_prop: mis-match msg size recieved: %d expected: %d errno: %d\n", 433 r, sizeof(prop_msg), errno); 434 close(s); 435 return; 436 } 437 438 switch(msg.cmd) { 439 case PROP_MSG_SETPROP: 440 msg.name[PROP_NAME_MAX-1] = 0; 441 msg.value[PROP_VALUE_MAX-1] = 0; 442 443#ifdef HAVE_SELINUX 444 getpeercon(s, &source_ctx); 445#endif 446 447 if(memcmp(msg.name,"ctl.",4) == 0) { 448 // Keep the old close-socket-early behavior when handling 449 // ctl.* properties. 450 close(s); 451 if (check_control_perms(msg.value, cr.uid, cr.gid, source_ctx)) { 452 handle_control_message((char*) msg.name + 4, (char*) msg.value); 453 } else { 454 ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n", 455 msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid); 456 } 457 } else { 458 if (check_perms(msg.name, cr.uid, cr.gid, source_ctx)) { 459 property_set((char*) msg.name, (char*) msg.value); 460 } else { 461 ERROR("sys_prop: permission denied uid:%d name:%s\n", 462 cr.uid, msg.name); 463 } 464 465 // Note: bionic's property client code assumes that the 466 // property server will not close the socket until *AFTER* 467 // the property is written to memory. 468 close(s); 469 } 470#ifdef HAVE_SELINUX 471 freecon(source_ctx); 472#endif 473 474 break; 475 476 default: 477 close(s); 478 break; 479 } 480} 481 482void get_property_workspace(int *fd, int *sz) 483{ 484 *fd = pa_workspace.fd; 485 *sz = pa_workspace.size; 486} 487 488static void load_properties(char *data) 489{ 490 char *key, *value, *eol, *sol, *tmp; 491 492 sol = data; 493 while((eol = strchr(sol, '\n'))) { 494 key = sol; 495 *eol++ = 0; 496 sol = eol; 497 498 value = strchr(key, '='); 499 if(value == 0) continue; 500 *value++ = 0; 501 502 while(isspace(*key)) key++; 503 if(*key == '#') continue; 504 tmp = value - 2; 505 while((tmp > key) && isspace(*tmp)) *tmp-- = 0; 506 507 while(isspace(*value)) value++; 508 tmp = eol - 2; 509 while((tmp > value) && isspace(*tmp)) *tmp-- = 0; 510 511 property_set(key, value); 512 } 513} 514 515static void load_properties_from_file(const char *fn) 516{ 517 char *data; 518 unsigned sz; 519 520 data = read_file(fn, &sz); 521 522 if(data != 0) { 523 load_properties(data); 524 free(data); 525 } 526} 527 528static void load_persistent_properties() 529{ 530 DIR* dir = opendir(PERSISTENT_PROPERTY_DIR); 531 struct dirent* entry; 532 char path[PATH_MAX]; 533 char value[PROP_VALUE_MAX]; 534 int fd, length; 535 536 if (dir) { 537 while ((entry = readdir(dir)) != NULL) { 538 if (strncmp("persist.", entry->d_name, strlen("persist."))) 539 continue; 540#if HAVE_DIRENT_D_TYPE 541 if (entry->d_type != DT_REG) 542 continue; 543#endif 544 /* open the file and read the property value */ 545 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, entry->d_name); 546 fd = open(path, O_RDONLY); 547 if (fd >= 0) { 548 length = read(fd, value, sizeof(value) - 1); 549 if (length >= 0) { 550 value[length] = 0; 551 property_set(entry->d_name, value); 552 } else { 553 ERROR("Unable to read persistent property file %s errno: %d\n", path, errno); 554 } 555 close(fd); 556 } else { 557 ERROR("Unable to open persistent property file %s errno: %d\n", path, errno); 558 } 559 } 560 closedir(dir); 561 } else { 562 ERROR("Unable to open persistent property directory %s errno: %d\n", PERSISTENT_PROPERTY_DIR, errno); 563 } 564 565 persistent_properties_loaded = 1; 566} 567 568void property_init(void) 569{ 570 init_property_area(); 571} 572 573void property_load_boot_defaults(void) 574{ 575 load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT); 576} 577 578int properties_inited(void) 579{ 580 return property_area_inited; 581} 582 583/* When booting an encrypted system, /data is not mounted when the 584 * property service is started, so any properties stored there are 585 * not loaded. Vold triggers init to load these properties once it 586 * has mounted /data. 587 */ 588void load_persist_props(void) 589{ 590#ifdef ALLOW_LOCAL_PROP_OVERRIDE 591 load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE); 592#endif /* ALLOW_LOCAL_PROP_OVERRIDE */ 593 /* Read persistent properties after all default values have been loaded. */ 594 load_persistent_properties(); 595} 596 597void start_property_service(void) 598{ 599 int fd; 600 601 load_properties_from_file(PROP_PATH_SYSTEM_BUILD); 602 load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT); 603#ifdef ALLOW_LOCAL_PROP_OVERRIDE 604 load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE); 605#endif /* ALLOW_LOCAL_PROP_OVERRIDE */ 606 /* Read persistent properties after all default values have been loaded. */ 607 load_persistent_properties(); 608 609 fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0); 610 if(fd < 0) return; 611 fcntl(fd, F_SETFD, FD_CLOEXEC); 612 fcntl(fd, F_SETFL, O_NONBLOCK); 613 614 listen(fd, 8); 615 property_set_fd = fd; 616} 617 618int get_property_set_fd() 619{ 620 return property_set_fd; 621} 622