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