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