blob: b5d52dd53c7febc8f375e2f331a34a0f7dacb72a [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/* Copyright 2008 The Android Open Source Project
2 */
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <errno.h>
7#include <fcntl.h>
8
9#include <private/android_filesystem_config.h>
10
11#include "binder.h"
12
13#if 0
14#define ALOGI(x...) fprintf(stderr, "svcmgr: " x)
15#define ALOGE(x...) fprintf(stderr, "svcmgr: " x)
16#else
17#define LOG_TAG "ServiceManager"
18#include <cutils/log.h>
19#endif
20
21/* TODO:
22 * These should come from a config file or perhaps be
23 * based on some namespace rules of some sort (media
24 * uid can register media.*, etc)
25 */
26static struct {
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000027 uid_t uid;
Mike Lockwood94afecf2012-10-24 10:45:23 -070028 const char *name;
29} allowed[] = {
30 { AID_MEDIA, "media.audio_flinger" },
Glenn Kasten64c8be02013-01-16 12:06:39 -080031 { AID_MEDIA, "media.log" },
Mike Lockwood94afecf2012-10-24 10:45:23 -070032 { AID_MEDIA, "media.player" },
33 { AID_MEDIA, "media.camera" },
34 { AID_MEDIA, "media.audio_policy" },
Mike Lockwood2a5fd8b2013-09-11 08:10:11 -070035 { AID_AUDIO, "audio" },
Mike Lockwood30a7d5a2013-09-13 07:26:05 -070036 { AID_INPUT, "input" },
Mike Lockwood94afecf2012-10-24 10:45:23 -070037 { AID_DRM, "drm.drmManager" },
38 { AID_NFC, "nfc" },
39 { AID_BLUETOOTH, "bluetooth" },
40 { AID_RADIO, "radio.phone" },
41 { AID_RADIO, "radio.sms" },
42 { AID_RADIO, "radio.phonesubinfo" },
43 { AID_RADIO, "radio.simphonebook" },
44/* TODO: remove after phone services are updated: */
45 { AID_RADIO, "phone" },
46 { AID_RADIO, "sip" },
47 { AID_RADIO, "isms" },
48 { AID_RADIO, "iphonesubinfo" },
49 { AID_RADIO, "simphonebook" },
50 { AID_MEDIA, "common_time.clock" },
51 { AID_MEDIA, "common_time.config" },
Kenny Root24440872012-11-14 15:42:38 -080052 { AID_KEYSTORE, "android.security.keystore" },
Santos Cordon3fb4de72014-05-21 22:58:55 -070053 { AID_RADIO, "telecomm" },
Mike Lockwood94afecf2012-10-24 10:45:23 -070054};
55
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000056uint32_t svcmgr_handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -070057
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000058const char *str8(const uint16_t *x)
Mike Lockwood94afecf2012-10-24 10:45:23 -070059{
60 static char buf[128];
61 unsigned max = 127;
62 char *p = buf;
63
64 if (x) {
65 while (*x && max--) {
66 *p++ = *x++;
67 }
68 }
69 *p++ = 0;
70 return buf;
71}
72
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000073int str16eq(const uint16_t *a, const char *b)
Mike Lockwood94afecf2012-10-24 10:45:23 -070074{
75 while (*a && *b)
76 if (*a++ != *b++) return 0;
77 if (*a || *b)
78 return 0;
79 return 1;
80}
81
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000082int svc_can_register(uid_t uid, const uint16_t *name)
Mike Lockwood94afecf2012-10-24 10:45:23 -070083{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000084 size_t n;
85
Mike Lockwood94afecf2012-10-24 10:45:23 -070086 if ((uid == 0) || (uid == AID_SYSTEM))
87 return 1;
88
89 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
90 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
91 return 1;
92
93 return 0;
94}
95
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000096struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -070097{
98 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000099 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700100 struct binder_death death;
101 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000102 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700103 uint16_t name[0];
104};
105
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000106struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700107
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000108struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700109{
110 struct svcinfo *si;
111
112 for (si = svclist; si; si = si->next) {
113 if ((len == si->len) &&
114 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
115 return si;
116 }
117 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000118 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700119}
120
121void svcinfo_death(struct binder_state *bs, void *ptr)
122{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000123 struct svcinfo *si = (struct svcinfo* ) ptr;
124
Mike Lockwood94afecf2012-10-24 10:45:23 -0700125 ALOGI("service '%s' died\n", str8(si->name));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000126 if (si->handle) {
127 binder_release(bs, si->handle);
128 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000129 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700130}
131
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000132uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700133 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000134 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700135};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700136
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000137
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000138uint32_t do_find_service(struct binder_state *bs, const uint16_t *s, size_t len, uid_t uid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700139{
140 struct svcinfo *si;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700141
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000142 si = find_svc(s, len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000143 //ALOGI("check_service('%s') handle = %x\n", str8(s), si ? si->handle : 0);
144 if (si && si->handle) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700145 if (!si->allow_isolated) {
146 // If this service doesn't allow access from isolated processes,
147 // then check the uid to see if it is isolated.
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000148 uid_t appid = uid % AID_USER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700149 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
150 return 0;
151 }
152 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000153 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700154 } else {
155 return 0;
156 }
157}
158
159int do_add_service(struct binder_state *bs,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000160 const uint16_t *s, size_t len,
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000161 uint32_t handle, uid_t uid, int allow_isolated)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700162{
163 struct svcinfo *si;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000164
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000165 //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s), handle,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700166 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
167
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000168 if (!handle || (len == 0) || (len > 127))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700169 return -1;
170
171 if (!svc_can_register(uid, s)) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000172 ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
173 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700174 return -1;
175 }
176
177 si = find_svc(s, len);
178 if (si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000179 if (si->handle) {
180 ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
181 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700182 svcinfo_death(bs, si);
183 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000184 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700185 } else {
186 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
187 if (!si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000188 ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
189 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700190 return -1;
191 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000192 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700193 si->len = len;
194 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
195 si->name[len] = '\0';
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000196 si->death.func = (void*) svcinfo_death;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700197 si->death.ptr = si;
198 si->allow_isolated = allow_isolated;
199 si->next = svclist;
200 svclist = si;
201 }
202
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000203 binder_acquire(bs, handle);
204 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700205 return 0;
206}
207
208int svcmgr_handler(struct binder_state *bs,
Serban Constantinescubcf38882014-01-10 13:56:27 +0000209 struct binder_transaction_data *txn,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700210 struct binder_io *msg,
211 struct binder_io *reply)
212{
213 struct svcinfo *si;
214 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000215 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000216 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700217 uint32_t strict_policy;
218 int allow_isolated;
219
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000220 //ALOGI("target=%x code=%d pid=%d uid=%d\n",
221 // txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700222
Serban Constantinescubcf38882014-01-10 13:56:27 +0000223 if (txn->target.handle != svcmgr_handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700224 return -1;
225
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800226 if (txn->code == PING_TRANSACTION)
227 return 0;
228
Mike Lockwood94afecf2012-10-24 10:45:23 -0700229 // Equivalent to Parcel::enforceInterface(), reading the RPC
230 // header with the strict mode policy mask and the interface name.
231 // Note that we ignore the strict_policy and don't propagate it
232 // further (since we do no outbound RPCs anyway).
233 strict_policy = bio_get_uint32(msg);
234 s = bio_get_string16(msg, &len);
235 if ((len != (sizeof(svcmgr_id) / 2)) ||
236 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
237 fprintf(stderr,"invalid id %s\n", str8(s));
238 return -1;
239 }
240
241 switch(txn->code) {
242 case SVC_MGR_GET_SERVICE:
243 case SVC_MGR_CHECK_SERVICE:
244 s = bio_get_string16(msg, &len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000245 handle = do_find_service(bs, s, len, txn->sender_euid);
246 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700247 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000248 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700249 return 0;
250
251 case SVC_MGR_ADD_SERVICE:
252 s = bio_get_string16(msg, &len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000253 handle = bio_get_ref(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700254 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000255 if (do_add_service(bs, s, len, handle, txn->sender_euid, allow_isolated))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700256 return -1;
257 break;
258
259 case SVC_MGR_LIST_SERVICES: {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000260 uint32_t n = bio_get_uint32(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700261
262 si = svclist;
263 while ((n-- > 0) && si)
264 si = si->next;
265 if (si) {
266 bio_put_string16(reply, si->name);
267 return 0;
268 }
269 return -1;
270 }
271 default:
272 ALOGE("unknown code %d\n", txn->code);
273 return -1;
274 }
275
276 bio_put_uint32(reply, 0);
277 return 0;
278}
279
280int main(int argc, char **argv)
281{
282 struct binder_state *bs;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700283
284 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000285 if (!bs) {
286 ALOGE("failed to open binder driver\n");
287 return -1;
288 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700289
290 if (binder_become_context_manager(bs)) {
291 ALOGE("cannot become context manager (%s)\n", strerror(errno));
292 return -1;
293 }
294
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000295 svcmgr_handle = BINDER_SERVICE_MANAGER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700296 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000297
Mike Lockwood94afecf2012-10-24 10:45:23 -0700298 return 0;
299}