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