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