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