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