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