Home | History | Annotate | Download | only in init
      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 
     50 static int persistent_properties_loaded = 0;
     51 static int property_area_inited = 0;
     52 
     53 static int property_set_fd = -1;
     54 
     55 /* White list of permissions for setting property services. */
     56 struct {
     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  */
     95 struct {
     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 
    105 typedef struct {
    106     void *data;
    107     size_t size;
    108     int fd;
    109 } workspace;
    110 
    111 static 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 
    143 out:
    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 
    155 static workspace pa_workspace;
    156 static prop_info *pa_info_array;
    157 
    158 extern prop_area *__system_property_area__;
    159 
    160 static 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 
    185 static 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  */
    199 static 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  */
    220 static 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 
    243 const 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 
    258 static 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 
    280 int 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 
    341 static int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
    342                   void *cookie)
    343 {
    344     char name[PROP_NAME_MAX];
    345     char value[PROP_VALUE_MAX];
    346     const prop_info *pi;
    347     unsigned n;
    348 
    349     for(n = 0; (pi = __system_property_find_nth(n)); n++) {
    350         __system_property_read(pi, name, value);
    351         propfn(name, value, cookie);
    352     }
    353     return 0;
    354 }
    355 
    356 void handle_property_set_fd()
    357 {
    358     prop_msg msg;
    359     int s;
    360     int r;
    361     int res;
    362     struct ucred cr;
    363     struct sockaddr_un addr;
    364     socklen_t addr_size = sizeof(addr);
    365     socklen_t cr_size = sizeof(cr);
    366 
    367     if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
    368         return;
    369     }
    370 
    371     /* Check socket options here */
    372     if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
    373         close(s);
    374         ERROR("Unable to recieve socket options\n");
    375         return;
    376     }
    377 
    378     r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), 0));
    379     if(r != sizeof(prop_msg)) {
    380         ERROR("sys_prop: mis-match msg size recieved: %d expected: %d errno: %d\n",
    381               r, sizeof(prop_msg), errno);
    382         close(s);
    383         return;
    384     }
    385 
    386     switch(msg.cmd) {
    387     case PROP_MSG_SETPROP:
    388         msg.name[PROP_NAME_MAX-1] = 0;
    389         msg.value[PROP_VALUE_MAX-1] = 0;
    390 
    391         if(memcmp(msg.name,"ctl.",4) == 0) {
    392             // Keep the old close-socket-early behavior when handling
    393             // ctl.* properties.
    394             close(s);
    395             if (check_control_perms(msg.value, cr.uid, cr.gid)) {
    396                 handle_control_message((char*) msg.name + 4, (char*) msg.value);
    397             } else {
    398                 ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",
    399                         msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
    400             }
    401         } else {
    402             if (check_perms(msg.name, cr.uid, cr.gid)) {
    403                 property_set((char*) msg.name, (char*) msg.value);
    404             } else {
    405                 ERROR("sys_prop: permission denied uid:%d  name:%s\n",
    406                       cr.uid, msg.name);
    407             }
    408 
    409             // Note: bionic's property client code assumes that the
    410             // property server will not close the socket until *AFTER*
    411             // the property is written to memory.
    412             close(s);
    413         }
    414         break;
    415 
    416     default:
    417         close(s);
    418         break;
    419     }
    420 }
    421 
    422 void get_property_workspace(int *fd, int *sz)
    423 {
    424     *fd = pa_workspace.fd;
    425     *sz = pa_workspace.size;
    426 }
    427 
    428 static void load_properties(char *data)
    429 {
    430     char *key, *value, *eol, *sol, *tmp;
    431 
    432     sol = data;
    433     while((eol = strchr(sol, '\n'))) {
    434         key = sol;
    435         *eol++ = 0;
    436         sol = eol;
    437 
    438         value = strchr(key, '=');
    439         if(value == 0) continue;
    440         *value++ = 0;
    441 
    442         while(isspace(*key)) key++;
    443         if(*key == '#') continue;
    444         tmp = value - 2;
    445         while((tmp > key) && isspace(*tmp)) *tmp-- = 0;
    446 
    447         while(isspace(*value)) value++;
    448         tmp = eol - 2;
    449         while((tmp > value) && isspace(*tmp)) *tmp-- = 0;
    450 
    451         property_set(key, value);
    452     }
    453 }
    454 
    455 static void load_properties_from_file(const char *fn)
    456 {
    457     char *data;
    458     unsigned sz;
    459 
    460     data = read_file(fn, &sz);
    461 
    462     if(data != 0) {
    463         load_properties(data);
    464         free(data);
    465     }
    466 }
    467 
    468 static void load_persistent_properties()
    469 {
    470     DIR* dir = opendir(PERSISTENT_PROPERTY_DIR);
    471     struct dirent*  entry;
    472     char path[PATH_MAX];
    473     char value[PROP_VALUE_MAX];
    474     int fd, length;
    475 
    476     if (dir) {
    477         while ((entry = readdir(dir)) != NULL) {
    478             if (strncmp("persist.", entry->d_name, strlen("persist.")))
    479                 continue;
    480 #if HAVE_DIRENT_D_TYPE
    481             if (entry->d_type != DT_REG)
    482                 continue;
    483 #endif
    484             /* open the file and read the property value */
    485             snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, entry->d_name);
    486             fd = open(path, O_RDONLY);
    487             if (fd >= 0) {
    488                 length = read(fd, value, sizeof(value) - 1);
    489                 if (length >= 0) {
    490                     value[length] = 0;
    491                     property_set(entry->d_name, value);
    492                 } else {
    493                     ERROR("Unable to read persistent property file %s errno: %d\n", path, errno);
    494                 }
    495                 close(fd);
    496             } else {
    497                 ERROR("Unable to open persistent property file %s errno: %d\n", path, errno);
    498             }
    499         }
    500         closedir(dir);
    501     } else {
    502         ERROR("Unable to open persistent property directory %s errno: %d\n", PERSISTENT_PROPERTY_DIR, errno);
    503     }
    504 
    505     persistent_properties_loaded = 1;
    506 }
    507 
    508 void property_init(bool load_defaults)
    509 {
    510     init_property_area();
    511     if (load_defaults)
    512         load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT);
    513 }
    514 
    515 int properties_inited(void)
    516 {
    517     return property_area_inited;
    518 }
    519 
    520 /* When booting an encrypted system, /data is not mounted when the
    521  * property service is started, so any properties stored there are
    522  * not loaded.  Vold triggers init to load these properties once it
    523  * has mounted /data.
    524  */
    525 void load_persist_props(void)
    526 {
    527     load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);
    528     /* Read persistent properties after all default values have been loaded. */
    529     load_persistent_properties();
    530 }
    531 
    532 void start_property_service(void)
    533 {
    534     int fd;
    535 
    536     load_properties_from_file(PROP_PATH_SYSTEM_BUILD);
    537     load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
    538     load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);
    539     /* Read persistent properties after all default values have been loaded. */
    540     load_persistent_properties();
    541 
    542     fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0);
    543     if(fd < 0) return;
    544     fcntl(fd, F_SETFD, FD_CLOEXEC);
    545     fcntl(fd, F_SETFL, O_NONBLOCK);
    546 
    547     listen(fd, 8);
    548     property_set_fd = fd;
    549 }
    550 
    551 int get_property_set_fd()
    552 {
    553     return property_set_fd;
    554 }
    555