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