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