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 #include <cutils/multiuser.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 <selinux/selinux.h>
     45 #include <selinux/label.h>
     46 
     47 #include "property_service.h"
     48 #include "init.h"
     49 #include "util.h"
     50 #include "log.h"
     51 
     52 #define PERSISTENT_PROPERTY_DIR  "/data/property"
     53 
     54 static int persistent_properties_loaded = 0;
     55 static int property_area_inited = 0;
     56 
     57 static int property_set_fd = -1;
     58 
     59 /* White list of permissions for setting property services. */
     60 struct {
     61     const char *prefix;
     62     unsigned int uid;
     63     unsigned int gid;
     64 } property_perms[] = {
     65     { "net.rmnet0.",      AID_RADIO,    0 },
     66     { "net.gprs.",        AID_RADIO,    0 },
     67     { "net.ppp",          AID_RADIO,    0 },
     68     { "net.qmi",          AID_RADIO,    0 },
     69     { "net.lte",          AID_RADIO,    0 },
     70     { "net.cdma",         AID_RADIO,    0 },
     71     { "ril.",             AID_RADIO,    0 },
     72     { "gsm.",             AID_RADIO,    0 },
     73     { "persist.radio",    AID_RADIO,    0 },
     74     { "net.dns",          AID_RADIO,    0 },
     75     { "sys.usb.config",   AID_RADIO,    0 },
     76     { "net.",             AID_SYSTEM,   0 },
     77     { "dev.",             AID_SYSTEM,   0 },
     78     { "runtime.",         AID_SYSTEM,   0 },
     79     { "hw.",              AID_SYSTEM,   0 },
     80     { "sys.",             AID_SYSTEM,   0 },
     81     { "service.",         AID_SYSTEM,   0 },
     82     { "wlan.",            AID_SYSTEM,   0 },
     83     { "bluetooth.",       AID_BLUETOOTH,   0 },
     84     { "dhcp.",            AID_SYSTEM,   0 },
     85     { "dhcp.",            AID_DHCP,     0 },
     86     { "debug.",           AID_SYSTEM,   0 },
     87     { "debug.",           AID_SHELL,    0 },
     88     { "log.",             AID_SHELL,    0 },
     89     { "service.adb.root", AID_SHELL,    0 },
     90     { "service.adb.tcp.port", AID_SHELL,    0 },
     91     { "persist.sys.",     AID_SYSTEM,   0 },
     92     { "persist.service.", AID_SYSTEM,   0 },
     93     { "persist.security.", AID_SYSTEM,   0 },
     94     { "persist.service.bdroid.", AID_BLUETOOTH,   0 },
     95     { "selinux."         , AID_SYSTEM,   0 },
     96     { NULL, 0, 0 }
     97 };
     98 
     99 /*
    100  * White list of UID that are allowed to start/stop services.
    101  * Currently there are no user apps that require.
    102  */
    103 struct {
    104     const char *service;
    105     unsigned int uid;
    106     unsigned int gid;
    107 } control_perms[] = {
    108     { "dumpstate",AID_SHELL, AID_LOG },
    109     { "ril-daemon",AID_RADIO, AID_RADIO },
    110      {NULL, 0, 0 }
    111 };
    112 
    113 typedef struct {
    114     void *data;
    115     size_t size;
    116     int fd;
    117 } workspace;
    118 
    119 static int init_workspace(workspace *w, size_t size)
    120 {
    121     void *data;
    122     int fd;
    123 
    124         /* dev is a tmpfs that we can use to carve a shared workspace
    125          * out of, so let's do that...
    126          */
    127     fd = open(PROP_FILENAME, O_RDWR | O_CREAT | O_NOFOLLOW, 0644);
    128     if (fd < 0)
    129         return -1;
    130 
    131     if (ftruncate(fd, size) < 0)
    132         goto out;
    133 
    134     data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    135     if(data == MAP_FAILED)
    136         goto out;
    137 
    138     close(fd);
    139 
    140     fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW);
    141     if (fd < 0)
    142         return -1;
    143 
    144     w->data = data;
    145     w->size = size;
    146     w->fd = fd;
    147     return 0;
    148 
    149 out:
    150     close(fd);
    151     return -1;
    152 }
    153 
    154 /* (8 header words + 372 toc words) = 1520 bytes */
    155 /* 1536 bytes header and toc + 372 prop_infos @ 128 bytes = 49152 bytes */
    156 
    157 #define PA_COUNT_MAX  372
    158 #define PA_INFO_START 1536
    159 #define PA_SIZE       49152
    160 
    161 static workspace pa_workspace;
    162 static prop_info *pa_info_array;
    163 
    164 extern prop_area *__system_property_area__;
    165 
    166 static int init_property_area(void)
    167 {
    168     prop_area *pa;
    169 
    170     if(pa_info_array)
    171         return -1;
    172 
    173     if(init_workspace(&pa_workspace, PA_SIZE))
    174         return -1;
    175 
    176     fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC);
    177 
    178     pa_info_array = (void*) (((char*) pa_workspace.data) + PA_INFO_START);
    179 
    180     pa = pa_workspace.data;
    181     memset(pa, 0, PA_SIZE);
    182     pa->magic = PROP_AREA_MAGIC;
    183     pa->version = PROP_AREA_VERSION;
    184 
    185         /* plug into the lib property services */
    186     __system_property_area__ = pa;
    187     property_area_inited = 1;
    188     return 0;
    189 }
    190 
    191 static void update_prop_info(prop_info *pi, const char *value, unsigned len)
    192 {
    193     pi->serial = pi->serial | 1;
    194     memcpy(pi->value, value, len + 1);
    195     pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff);
    196     __futex_wake(&pi->serial, INT32_MAX);
    197 }
    198 
    199 static int check_mac_perms(const char *name, char *sctx)
    200 {
    201     if (is_selinux_enabled() <= 0)
    202         return 1;
    203 
    204     char *tctx = NULL;
    205     const char *class = "property_service";
    206     const char *perm = "set";
    207     int result = 0;
    208 
    209     if (!sctx)
    210         goto err;
    211 
    212     if (!sehandle_prop)
    213         goto err;
    214 
    215     if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
    216         goto err;
    217 
    218     if (selinux_check_access(sctx, tctx, class, perm, name) == 0)
    219         result = 1;
    220 
    221     freecon(tctx);
    222  err:
    223     return result;
    224 }
    225 
    226 static int check_control_mac_perms(const char *name, char *sctx)
    227 {
    228     /*
    229      *  Create a name prefix out of ctl.<service name>
    230      *  The new prefix allows the use of the existing
    231      *  property service backend labeling while avoiding
    232      *  mislabels based on true property prefixes.
    233      */
    234     char ctl_name[PROP_VALUE_MAX+4];
    235     int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
    236 
    237     if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
    238         return 0;
    239 
    240     return check_mac_perms(ctl_name, sctx);
    241 }
    242 
    243 /*
    244  * Checks permissions for starting/stoping system services.
    245  * AID_SYSTEM and AID_ROOT are always allowed.
    246  *
    247  * Returns 1 if uid allowed, 0 otherwise.
    248  */
    249 static int check_control_perms(const char *name, unsigned int uid, unsigned int gid, char *sctx) {
    250 
    251     int i;
    252     if (uid == AID_SYSTEM || uid == AID_ROOT)
    253       return check_control_mac_perms(name, sctx);
    254 
    255     /* Search the ACL */
    256     for (i = 0; control_perms[i].service; i++) {
    257         if (strcmp(control_perms[i].service, name) == 0) {
    258             if ((uid && control_perms[i].uid == uid) ||
    259                 (gid && control_perms[i].gid == gid)) {
    260                 return check_control_mac_perms(name, sctx);
    261             }
    262         }
    263     }
    264     return 0;
    265 }
    266 
    267 /*
    268  * Checks permissions for setting system properties.
    269  * Returns 1 if uid allowed, 0 otherwise.
    270  */
    271 static int check_perms(const char *name, unsigned int uid, unsigned int gid, char *sctx)
    272 {
    273     int i;
    274     unsigned int app_id;
    275 
    276     if(!strncmp(name, "ro.", 3))
    277         name +=3;
    278 
    279     if (uid == 0)
    280         return check_mac_perms(name, sctx);
    281 
    282     app_id = multiuser_get_app_id(uid);
    283     if (app_id == AID_BLUETOOTH) {
    284         uid = app_id;
    285     }
    286 
    287     for (i = 0; property_perms[i].prefix; i++) {
    288         if (strncmp(property_perms[i].prefix, name,
    289                     strlen(property_perms[i].prefix)) == 0) {
    290             if ((uid && property_perms[i].uid == uid) ||
    291                 (gid && property_perms[i].gid == gid)) {
    292 
    293                 return check_mac_perms(name, sctx);
    294             }
    295         }
    296     }
    297 
    298     return 0;
    299 }
    300 
    301 const char* property_get(const char *name)
    302 {
    303     prop_info *pi;
    304 
    305     if(strlen(name) >= PROP_NAME_MAX) return 0;
    306 
    307     pi = (prop_info*) __system_property_find(name);
    308 
    309     if(pi != 0) {
    310         return pi->value;
    311     } else {
    312         return 0;
    313     }
    314 }
    315 
    316 static void write_persistent_property(const char *name, const char *value)
    317 {
    318     char tempPath[PATH_MAX];
    319     char path[PATH_MAX];
    320     int fd;
    321 
    322     snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
    323     fd = mkstemp(tempPath);
    324     if (fd < 0) {
    325         ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno);
    326         return;
    327     }
    328     write(fd, value, strlen(value));
    329     close(fd);
    330 
    331     snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
    332     if (rename(tempPath, path)) {
    333         unlink(tempPath);
    334         ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
    335     }
    336 }
    337 
    338 int property_set(const char *name, const char *value)
    339 {
    340     prop_area *pa;
    341     prop_info *pi;
    342 
    343     size_t namelen = strlen(name);
    344     size_t valuelen = strlen(value);
    345 
    346     if(namelen >= PROP_NAME_MAX) return -1;
    347     if(valuelen >= PROP_VALUE_MAX) return -1;
    348     if(namelen < 1) return -1;
    349 
    350     pi = (prop_info*) __system_property_find(name);
    351 
    352     if(pi != 0) {
    353         /* ro.* properties may NEVER be modified once set */
    354         if(!strncmp(name, "ro.", 3)) return -1;
    355 
    356         pa = __system_property_area__;
    357         update_prop_info(pi, value, valuelen);
    358         pa->serial++;
    359         __futex_wake(&pa->serial, INT32_MAX);
    360     } else {
    361         pa = __system_property_area__;
    362         if(pa->count == PA_COUNT_MAX) return -1;
    363 
    364         pi = pa_info_array + pa->count;
    365         pi->serial = (valuelen << 24);
    366         memcpy(pi->name, name, namelen + 1);
    367         memcpy(pi->value, value, valuelen + 1);
    368 
    369         pa->toc[pa->count] =
    370             (namelen << 24) | (((unsigned) pi) - ((unsigned) pa));
    371 
    372         pa->count++;
    373         pa->serial++;
    374         __futex_wake(&pa->serial, INT32_MAX);
    375     }
    376     /* If name starts with "net." treat as a DNS property. */
    377     if (strncmp("net.", name, strlen("net.")) == 0)  {
    378         if (strcmp("net.change", name) == 0) {
    379             return 0;
    380         }
    381        /*
    382         * The 'net.change' property is a special property used track when any
    383         * 'net.*' property name is updated. It is _ONLY_ updated here. Its value
    384         * contains the last updated 'net.*' property.
    385         */
    386         property_set("net.change", name);
    387     } else if (persistent_properties_loaded &&
    388             strncmp("persist.", name, strlen("persist.")) == 0) {
    389         /*
    390          * Don't write properties to disk until after we have read all default properties
    391          * to prevent them from being overwritten by default values.
    392          */
    393         write_persistent_property(name, value);
    394     } else if (strcmp("selinux.reload_policy", name) == 0 &&
    395                strcmp("1", value) == 0) {
    396         selinux_reload_policy();
    397     }
    398     property_changed(name, value);
    399     return 0;
    400 }
    401 
    402 void handle_property_set_fd()
    403 {
    404     prop_msg msg;
    405     int s;
    406     int r;
    407     int res;
    408     struct ucred cr;
    409     struct sockaddr_un addr;
    410     socklen_t addr_size = sizeof(addr);
    411     socklen_t cr_size = sizeof(cr);
    412     char * source_ctx = NULL;
    413 
    414     if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
    415         return;
    416     }
    417 
    418     /* Check socket options here */
    419     if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
    420         close(s);
    421         ERROR("Unable to receive socket options\n");
    422         return;
    423     }
    424 
    425     r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), 0));
    426     if(r != sizeof(prop_msg)) {
    427         ERROR("sys_prop: mis-match msg size received: %d expected: %d errno: %d\n",
    428               r, sizeof(prop_msg), errno);
    429         close(s);
    430         return;
    431     }
    432 
    433     switch(msg.cmd) {
    434     case PROP_MSG_SETPROP:
    435         msg.name[PROP_NAME_MAX-1] = 0;
    436         msg.value[PROP_VALUE_MAX-1] = 0;
    437 
    438         getpeercon(s, &source_ctx);
    439 
    440         if(memcmp(msg.name,"ctl.",4) == 0) {
    441             // Keep the old close-socket-early behavior when handling
    442             // ctl.* properties.
    443             close(s);
    444             if (check_control_perms(msg.value, cr.uid, cr.gid, source_ctx)) {
    445                 handle_control_message((char*) msg.name + 4, (char*) msg.value);
    446             } else {
    447                 ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",
    448                         msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
    449             }
    450         } else {
    451             if (check_perms(msg.name, cr.uid, cr.gid, source_ctx)) {
    452                 property_set((char*) msg.name, (char*) msg.value);
    453             } else {
    454                 ERROR("sys_prop: permission denied uid:%d  name:%s\n",
    455                       cr.uid, msg.name);
    456             }
    457 
    458             // Note: bionic's property client code assumes that the
    459             // property server will not close the socket until *AFTER*
    460             // the property is written to memory.
    461             close(s);
    462         }
    463         freecon(source_ctx);
    464         break;
    465 
    466     default:
    467         close(s);
    468         break;
    469     }
    470 }
    471 
    472 void get_property_workspace(int *fd, int *sz)
    473 {
    474     *fd = pa_workspace.fd;
    475     *sz = pa_workspace.size;
    476 }
    477 
    478 static void load_properties(char *data)
    479 {
    480     char *key, *value, *eol, *sol, *tmp;
    481 
    482     sol = data;
    483     while((eol = strchr(sol, '\n'))) {
    484         key = sol;
    485         *eol++ = 0;
    486         sol = eol;
    487 
    488         value = strchr(key, '=');
    489         if(value == 0) continue;
    490         *value++ = 0;
    491 
    492         while(isspace(*key)) key++;
    493         if(*key == '#') continue;
    494         tmp = value - 2;
    495         while((tmp > key) && isspace(*tmp)) *tmp-- = 0;
    496 
    497         while(isspace(*value)) value++;
    498         tmp = eol - 2;
    499         while((tmp > value) && isspace(*tmp)) *tmp-- = 0;
    500 
    501         property_set(key, value);
    502     }
    503 }
    504 
    505 static void load_properties_from_file(const char *fn)
    506 {
    507     char *data;
    508     unsigned sz;
    509 
    510     data = read_file(fn, &sz);
    511 
    512     if(data != 0) {
    513         load_properties(data);
    514         free(data);
    515     }
    516 }
    517 
    518 static void load_persistent_properties()
    519 {
    520     DIR* dir = opendir(PERSISTENT_PROPERTY_DIR);
    521     int dir_fd;
    522     struct dirent*  entry;
    523     char value[PROP_VALUE_MAX];
    524     int fd, length;
    525     struct stat sb;
    526 
    527     if (dir) {
    528         dir_fd = dirfd(dir);
    529         while ((entry = readdir(dir)) != NULL) {
    530             if (strncmp("persist.", entry->d_name, strlen("persist.")))
    531                 continue;
    532 #if HAVE_DIRENT_D_TYPE
    533             if (entry->d_type != DT_REG)
    534                 continue;
    535 #endif
    536             /* open the file and read the property value */
    537             fd = openat(dir_fd, entry->d_name, O_RDONLY | O_NOFOLLOW);
    538             if (fd < 0) {
    539                 ERROR("Unable to open persistent property file \"%s\" errno: %d\n",
    540                       entry->d_name, errno);
    541                 continue;
    542             }
    543             if (fstat(fd, &sb) < 0) {
    544                 ERROR("fstat on property file \"%s\" failed errno: %d\n", entry->d_name, errno);
    545                 close(fd);
    546                 continue;
    547             }
    548 
    549             // File must not be accessible to others, be owned by root/root, and
    550             // not be a hard link to any other file.
    551             if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0)
    552                     || (sb.st_uid != 0)
    553                     || (sb.st_gid != 0)
    554                     || (sb.st_nlink != 1)) {
    555                 ERROR("skipping insecure property file %s (uid=%lu gid=%lu nlink=%d mode=%o)\n",
    556                       entry->d_name, sb.st_uid, sb.st_gid, sb.st_nlink, sb.st_mode);
    557                 close(fd);
    558                 continue;
    559             }
    560 
    561             length = read(fd, value, sizeof(value) - 1);
    562             if (length >= 0) {
    563                 value[length] = 0;
    564                 property_set(entry->d_name, value);
    565             } else {
    566                 ERROR("Unable to read persistent property file %s errno: %d\n",
    567                       entry->d_name, errno);
    568             }
    569             close(fd);
    570         }
    571         closedir(dir);
    572     } else {
    573         ERROR("Unable to open persistent property directory %s errno: %d\n", PERSISTENT_PROPERTY_DIR, errno);
    574     }
    575 
    576     persistent_properties_loaded = 1;
    577 }
    578 
    579 void property_init(void)
    580 {
    581     init_property_area();
    582 }
    583 
    584 void property_load_boot_defaults(void)
    585 {
    586     load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT);
    587 }
    588 
    589 int properties_inited(void)
    590 {
    591     return property_area_inited;
    592 }
    593 
    594 static void load_override_properties() {
    595 #ifdef ALLOW_LOCAL_PROP_OVERRIDE
    596     const char *debuggable = property_get("ro.debuggable");
    597     if (debuggable && (strcmp(debuggable, "1") == 0)) {
    598         load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);
    599     }
    600 #endif /* ALLOW_LOCAL_PROP_OVERRIDE */
    601 }
    602 
    603 
    604 /* When booting an encrypted system, /data is not mounted when the
    605  * property service is started, so any properties stored there are
    606  * not loaded.  Vold triggers init to load these properties once it
    607  * has mounted /data.
    608  */
    609 void load_persist_props(void)
    610 {
    611     load_override_properties();
    612     /* Read persistent properties after all default values have been loaded. */
    613     load_persistent_properties();
    614 }
    615 
    616 void start_property_service(void)
    617 {
    618     int fd;
    619 
    620     load_properties_from_file(PROP_PATH_SYSTEM_BUILD);
    621     load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
    622     load_override_properties();
    623     /* Read persistent properties after all default values have been loaded. */
    624     load_persistent_properties();
    625 
    626     fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0);
    627     if(fd < 0) return;
    628     fcntl(fd, F_SETFD, FD_CLOEXEC);
    629     fcntl(fd, F_SETFL, O_NONBLOCK);
    630 
    631     listen(fd, 8);
    632     property_set_fd = fd;
    633 }
    634 
    635 int get_property_set_fd()
    636 {
    637     return property_set_fd;
    638 }
    639