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 #include <sys/poll.h>
     28 
     29 #include <memory>
     30 
     31 #include <cutils/misc.h>
     32 #include <cutils/sockets.h>
     33 #include <cutils/multiuser.h>
     34 
     35 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
     36 #include <sys/_system_properties.h>
     37 
     38 #include <sys/socket.h>
     39 #include <sys/un.h>
     40 #include <sys/select.h>
     41 #include <sys/types.h>
     42 #include <netinet/in.h>
     43 #include <sys/mman.h>
     44 #include <private/android_filesystem_config.h>
     45 
     46 #include <selinux/selinux.h>
     47 #include <selinux/label.h>
     48 
     49 #include <fs_mgr.h>
     50 #include <android-base/file.h>
     51 #include "bootimg.h"
     52 
     53 #include "property_service.h"
     54 #include "init.h"
     55 #include "util.h"
     56 #include "log.h"
     57 
     58 #define PERSISTENT_PROPERTY_DIR  "/data/property"
     59 #define FSTAB_PREFIX "/fstab."
     60 #define RECOVERY_MOUNT_POINT "/recovery"
     61 
     62 static int persistent_properties_loaded = 0;
     63 
     64 static int property_set_fd = -1;
     65 
     66 void property_init() {
     67     if (__system_property_area_init()) {
     68         ERROR("Failed to initialize property area\n");
     69         exit(1);
     70     }
     71 }
     72 
     73 static int check_mac_perms(const char *name, char *sctx, struct ucred *cr)
     74 {
     75     char *tctx = NULL;
     76     int result = 0;
     77     property_audit_data audit_data;
     78 
     79     if (!sctx)
     80         goto err;
     81 
     82     if (!sehandle_prop)
     83         goto err;
     84 
     85     if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
     86         goto err;
     87 
     88     audit_data.name = name;
     89     audit_data.cr = cr;
     90 
     91     if (selinux_check_access(sctx, tctx, "property_service", "set", reinterpret_cast<void*>(&audit_data)) == 0)
     92         result = 1;
     93 
     94     freecon(tctx);
     95  err:
     96     return result;
     97 }
     98 
     99 static int check_control_mac_perms(const char *name, char *sctx, struct ucred *cr)
    100 {
    101     /*
    102      *  Create a name prefix out of ctl.<service name>
    103      *  The new prefix allows the use of the existing
    104      *  property service backend labeling while avoiding
    105      *  mislabels based on true property prefixes.
    106      */
    107     char ctl_name[PROP_VALUE_MAX+4];
    108     int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
    109 
    110     if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
    111         return 0;
    112 
    113     return check_mac_perms(ctl_name, sctx, cr);
    114 }
    115 
    116 std::string property_get(const char* name) {
    117     char value[PROP_VALUE_MAX] = {0};
    118     __system_property_get(name, value);
    119     return value;
    120 }
    121 
    122 static void write_persistent_property(const char *name, const char *value)
    123 {
    124     char tempPath[PATH_MAX];
    125     char path[PATH_MAX];
    126     int fd;
    127 
    128     snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
    129     fd = mkstemp(tempPath);
    130     if (fd < 0) {
    131         ERROR("Unable to write persistent property to temp file %s: %s\n", tempPath, strerror(errno));
    132         return;
    133     }
    134     write(fd, value, strlen(value));
    135     fsync(fd);
    136     close(fd);
    137 
    138     snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
    139     if (rename(tempPath, path)) {
    140         unlink(tempPath);
    141         ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
    142     }
    143 }
    144 
    145 static bool is_legal_property_name(const char* name, size_t namelen)
    146 {
    147     size_t i;
    148     if (namelen >= PROP_NAME_MAX) return false;
    149     if (namelen < 1) return false;
    150     if (name[0] == '.') return false;
    151     if (name[namelen - 1] == '.') return false;
    152 
    153     /* Only allow alphanumeric, plus '.', '-', or '_' */
    154     /* Don't allow ".." to appear in a property name */
    155     for (i = 0; i < namelen; i++) {
    156         if (name[i] == '.') {
    157             // i=0 is guaranteed to never have a dot. See above.
    158             if (name[i-1] == '.') return false;
    159             continue;
    160         }
    161         if (name[i] == '_' || name[i] == '-') continue;
    162         if (name[i] >= 'a' && name[i] <= 'z') continue;
    163         if (name[i] >= 'A' && name[i] <= 'Z') continue;
    164         if (name[i] >= '0' && name[i] <= '9') continue;
    165         return false;
    166     }
    167 
    168     return true;
    169 }
    170 
    171 static int property_set_impl(const char* name, const char* value) {
    172     size_t namelen = strlen(name);
    173     size_t valuelen = strlen(value);
    174 
    175     if (!is_legal_property_name(name, namelen)) return -1;
    176     if (valuelen >= PROP_VALUE_MAX) return -1;
    177 
    178     if (strcmp("selinux.reload_policy", name) == 0 && strcmp("1", value) == 0) {
    179         if (selinux_reload_policy() != 0) {
    180             ERROR("Failed to reload policy\n");
    181         }
    182     } else if (strcmp("selinux.restorecon_recursive", name) == 0 && valuelen > 0) {
    183         if (restorecon_recursive(value) != 0) {
    184             ERROR("Failed to restorecon_recursive %s\n", value);
    185         }
    186     }
    187 
    188     prop_info* pi = (prop_info*) __system_property_find(name);
    189 
    190     if(pi != 0) {
    191         /* ro.* properties may NEVER be modified once set */
    192         if(!strncmp(name, "ro.", 3)) return -1;
    193 
    194         __system_property_update(pi, value, valuelen);
    195     } else {
    196         int rc = __system_property_add(name, namelen, value, valuelen);
    197         if (rc < 0) {
    198             return rc;
    199         }
    200     }
    201     /* If name starts with "net." treat as a DNS property. */
    202     if (strncmp("net.", name, strlen("net.")) == 0)  {
    203         if (strcmp("net.change", name) == 0) {
    204             return 0;
    205         }
    206        /*
    207         * The 'net.change' property is a special property used track when any
    208         * 'net.*' property name is updated. It is _ONLY_ updated here. Its value
    209         * contains the last updated 'net.*' property.
    210         */
    211         property_set("net.change", name);
    212     } else if (persistent_properties_loaded &&
    213             strncmp("persist.", name, strlen("persist.")) == 0) {
    214         /*
    215          * Don't write properties to disk until after we have read all default properties
    216          * to prevent them from being overwritten by default values.
    217          */
    218         write_persistent_property(name, value);
    219     }
    220     property_changed(name, value);
    221     return 0;
    222 }
    223 
    224 int property_set(const char* name, const char* value) {
    225     int rc = property_set_impl(name, value);
    226     if (rc == -1) {
    227         ERROR("property_set(\"%s\", \"%s\") failed\n", name, value);
    228     }
    229     return rc;
    230 }
    231 
    232 static void handle_property_set_fd()
    233 {
    234     prop_msg msg;
    235     int s;
    236     int r;
    237     struct ucred cr;
    238     struct sockaddr_un addr;
    239     socklen_t addr_size = sizeof(addr);
    240     socklen_t cr_size = sizeof(cr);
    241     char * source_ctx = NULL;
    242     struct pollfd ufds[1];
    243     const int timeout_ms = 2 * 1000;  /* Default 2 sec timeout for caller to send property. */
    244     int nr;
    245 
    246     if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
    247         return;
    248     }
    249 
    250     /* Check socket options here */
    251     if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
    252         close(s);
    253         ERROR("Unable to receive socket options\n");
    254         return;
    255     }
    256 
    257     ufds[0].fd = s;
    258     ufds[0].events = POLLIN;
    259     ufds[0].revents = 0;
    260     nr = TEMP_FAILURE_RETRY(poll(ufds, 1, timeout_ms));
    261     if (nr == 0) {
    262         ERROR("sys_prop: timeout waiting for uid=%d to send property message.\n", cr.uid);
    263         close(s);
    264         return;
    265     } else if (nr < 0) {
    266         ERROR("sys_prop: error waiting for uid=%d to send property message: %s\n", cr.uid, strerror(errno));
    267         close(s);
    268         return;
    269     }
    270 
    271     r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), MSG_DONTWAIT));
    272     if(r != sizeof(prop_msg)) {
    273         ERROR("sys_prop: mis-match msg size received: %d expected: %zu: %s\n",
    274               r, sizeof(prop_msg), strerror(errno));
    275         close(s);
    276         return;
    277     }
    278 
    279     switch(msg.cmd) {
    280     case PROP_MSG_SETPROP:
    281         msg.name[PROP_NAME_MAX-1] = 0;
    282         msg.value[PROP_VALUE_MAX-1] = 0;
    283 
    284         if (!is_legal_property_name(msg.name, strlen(msg.name))) {
    285             ERROR("sys_prop: illegal property name. Got: \"%s\"\n", msg.name);
    286             close(s);
    287             return;
    288         }
    289 
    290         getpeercon(s, &source_ctx);
    291 
    292         if(memcmp(msg.name,"ctl.",4) == 0) {
    293             // Keep the old close-socket-early behavior when handling
    294             // ctl.* properties.
    295             close(s);
    296             if (check_control_mac_perms(msg.value, source_ctx, &cr)) {
    297                 handle_control_message((char*) msg.name + 4, (char*) msg.value);
    298             } else {
    299                 ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",
    300                         msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
    301             }
    302         } else {
    303             if (check_mac_perms(msg.name, source_ctx, &cr)) {
    304                 property_set((char*) msg.name, (char*) msg.value);
    305             } else {
    306                 ERROR("sys_prop: permission denied uid:%d  name:%s\n",
    307                       cr.uid, msg.name);
    308             }
    309 
    310             // Note: bionic's property client code assumes that the
    311             // property server will not close the socket until *AFTER*
    312             // the property is written to memory.
    313             close(s);
    314         }
    315         freecon(source_ctx);
    316         break;
    317 
    318     default:
    319         close(s);
    320         break;
    321     }
    322 }
    323 
    324 static void load_properties_from_file(const char *, const char *);
    325 
    326 /*
    327  * Filter is used to decide which properties to load: NULL loads all keys,
    328  * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
    329  */
    330 static void load_properties(char *data, const char *filter)
    331 {
    332     char *key, *value, *eol, *sol, *tmp, *fn;
    333     size_t flen = 0;
    334 
    335     if (filter) {
    336         flen = strlen(filter);
    337     }
    338 
    339     sol = data;
    340     while ((eol = strchr(sol, '\n'))) {
    341         key = sol;
    342         *eol++ = 0;
    343         sol = eol;
    344 
    345         while (isspace(*key)) key++;
    346         if (*key == '#') continue;
    347 
    348         tmp = eol - 2;
    349         while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
    350 
    351         if (!strncmp(key, "import ", 7) && flen == 0) {
    352             fn = key + 7;
    353             while (isspace(*fn)) fn++;
    354 
    355             key = strchr(fn, ' ');
    356             if (key) {
    357                 *key++ = 0;
    358                 while (isspace(*key)) key++;
    359             }
    360 
    361             load_properties_from_file(fn, key);
    362 
    363         } else {
    364             value = strchr(key, '=');
    365             if (!value) continue;
    366             *value++ = 0;
    367 
    368             tmp = value - 2;
    369             while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
    370 
    371             while (isspace(*value)) value++;
    372 
    373             if (flen > 0) {
    374                 if (filter[flen - 1] == '*') {
    375                     if (strncmp(key, filter, flen - 1)) continue;
    376                 } else {
    377                     if (strcmp(key, filter)) continue;
    378                 }
    379             }
    380 
    381             property_set(key, value);
    382         }
    383     }
    384 }
    385 
    386 /*
    387  * Filter is used to decide which properties to load: NULL loads all keys,
    388  * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
    389  */
    390 static void load_properties_from_file(const char* filename, const char* filter) {
    391     Timer t;
    392     std::string data;
    393     if (read_file(filename, &data)) {
    394         data.push_back('\n');
    395         load_properties(&data[0], filter);
    396     }
    397     NOTICE("(Loading properties from %s took %.2fs.)\n", filename, t.duration());
    398 }
    399 
    400 static void load_persistent_properties() {
    401     persistent_properties_loaded = 1;
    402 
    403     std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
    404     if (!dir) {
    405         ERROR("Unable to open persistent property directory \"%s\": %s\n",
    406               PERSISTENT_PROPERTY_DIR, strerror(errno));
    407         return;
    408     }
    409 
    410     struct dirent* entry;
    411     while ((entry = readdir(dir.get())) != NULL) {
    412         if (strncmp("persist.", entry->d_name, strlen("persist."))) {
    413             continue;
    414         }
    415         if (entry->d_type != DT_REG) {
    416             continue;
    417         }
    418 
    419         // Open the file and read the property value.
    420         int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
    421         if (fd == -1) {
    422             ERROR("Unable to open persistent property file \"%s\": %s\n",
    423                   entry->d_name, strerror(errno));
    424             continue;
    425         }
    426 
    427         struct stat sb;
    428         if (fstat(fd, &sb) == -1) {
    429             ERROR("fstat on property file \"%s\" failed: %s\n", entry->d_name, strerror(errno));
    430             close(fd);
    431             continue;
    432         }
    433 
    434         // File must not be accessible to others, be owned by root/root, and
    435         // not be a hard link to any other file.
    436         if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || (sb.st_uid != 0) || (sb.st_gid != 0) ||
    437                 (sb.st_nlink != 1)) {
    438             ERROR("skipping insecure property file %s (uid=%u gid=%u nlink=%u mode=%o)\n",
    439                   entry->d_name, (unsigned int)sb.st_uid, (unsigned int)sb.st_gid,
    440                   (unsigned int)sb.st_nlink, sb.st_mode);
    441             close(fd);
    442             continue;
    443         }
    444 
    445         char value[PROP_VALUE_MAX];
    446         int length = read(fd, value, sizeof(value) - 1);
    447         if (length >= 0) {
    448             value[length] = 0;
    449             property_set(entry->d_name, value);
    450         } else {
    451             ERROR("Unable to read persistent property file %s: %s\n",
    452                   entry->d_name, strerror(errno));
    453         }
    454         close(fd);
    455     }
    456 }
    457 
    458 void property_load_boot_defaults() {
    459     load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT, NULL);
    460 }
    461 
    462 static void load_override_properties() {
    463     if (ALLOW_LOCAL_PROP_OVERRIDE) {
    464         std::string debuggable = property_get("ro.debuggable");
    465         if (debuggable == "1") {
    466             load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
    467         }
    468     }
    469 }
    470 
    471 /* When booting an encrypted system, /data is not mounted when the
    472  * property service is started, so any properties stored there are
    473  * not loaded.  Vold triggers init to load these properties once it
    474  * has mounted /data.
    475  */
    476 void load_persist_props(void) {
    477     load_override_properties();
    478     /* Read persistent properties after all default values have been loaded. */
    479     load_persistent_properties();
    480 }
    481 
    482 void load_recovery_id_prop() {
    483     std::string ro_hardware = property_get("ro.hardware");
    484     if (ro_hardware.empty()) {
    485         ERROR("ro.hardware not set - unable to load recovery id\n");
    486         return;
    487     }
    488     std::string fstab_filename = FSTAB_PREFIX + ro_hardware;
    489 
    490     std::unique_ptr<fstab, void(*)(fstab*)> tab(fs_mgr_read_fstab(fstab_filename.c_str()),
    491             fs_mgr_free_fstab);
    492     if (!tab) {
    493         ERROR("unable to read fstab %s: %s\n", fstab_filename.c_str(), strerror(errno));
    494         return;
    495     }
    496 
    497     fstab_rec* rec = fs_mgr_get_entry_for_mount_point(tab.get(), RECOVERY_MOUNT_POINT);
    498     if (rec == NULL) {
    499         ERROR("/recovery not specified in fstab\n");
    500         return;
    501     }
    502 
    503     int fd = open(rec->blk_device, O_RDONLY);
    504     if (fd == -1) {
    505         ERROR("error opening block device %s: %s\n", rec->blk_device, strerror(errno));
    506         return;
    507     }
    508 
    509     boot_img_hdr hdr;
    510     if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
    511         std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
    512         property_set("ro.recovery_id", hex.c_str());
    513     } else {
    514         ERROR("error reading /recovery: %s\n", strerror(errno));
    515     }
    516 
    517     close(fd);
    518 }
    519 
    520 void load_system_props() {
    521     load_properties_from_file(PROP_PATH_SYSTEM_BUILD, NULL);
    522     load_properties_from_file(PROP_PATH_VENDOR_BUILD, NULL);
    523     load_properties_from_file(PROP_PATH_FACTORY, "ro.*");
    524     load_recovery_id_prop();
    525 }
    526 
    527 void start_property_service() {
    528     property_set_fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
    529                                     0666, 0, 0, NULL);
    530     if (property_set_fd == -1) {
    531         ERROR("start_property_service socket creation failed: %s\n", strerror(errno));
    532         exit(1);
    533     }
    534 
    535     listen(property_set_fd, 8);
    536 
    537     register_epoll_handler(property_set_fd, handle_property_set_fd);
    538 }
    539