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