blob: cacbea091e03108f956fd449edaba83b6551953b [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" },
Mike Lockwood94afecf2012-10-24 10:45:23 -070053};
54
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000055uint32_t svcmgr_handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -070056
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000057const char *str8(const uint16_t *x)
Mike Lockwood94afecf2012-10-24 10:45:23 -070058{
59 static char buf[128];
60 unsigned max = 127;
61 char *p = buf;
62
63 if (x) {
64 while (*x && max--) {
65 *p++ = *x++;
66 }
67 }
68 *p++ = 0;
69 return buf;
70}
71
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000072int str16eq(const uint16_t *a, const char *b)
Mike Lockwood94afecf2012-10-24 10:45:23 -070073{
74 while (*a && *b)
75 if (*a++ != *b++) return 0;
76 if (*a || *b)
77 return 0;
78 return 1;
79}
80
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000081int svc_can_register(uid_t uid, const uint16_t *name)
Mike Lockwood94afecf2012-10-24 10:45:23 -070082{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000083 size_t n;
84
Mike Lockwood94afecf2012-10-24 10:45:23 -070085 if ((uid == 0) || (uid == AID_SYSTEM))
86 return 1;
87
88 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
89 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
90 return 1;
91
92 return 0;
93}
94
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000095struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -070096{
97 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000098 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -070099 struct binder_death death;
100 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000101 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700102 uint16_t name[0];
103};
104
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000105struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700106
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000107struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700108{
109 struct svcinfo *si;
110
111 for (si = svclist; si; si = si->next) {
112 if ((len == si->len) &&
113 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
114 return si;
115 }
116 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000117 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700118}
119
120void svcinfo_death(struct binder_state *bs, void *ptr)
121{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000122 struct svcinfo *si = (struct svcinfo* ) ptr;
123
Mike Lockwood94afecf2012-10-24 10:45:23 -0700124 ALOGI("service '%s' died\n", str8(si->name));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000125 if (si->handle) {
126 binder_release(bs, si->handle);
127 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000128 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700129}
130
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000131uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700132 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000133 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700134};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700135
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000136
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000137uint32_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 -0700138{
139 struct svcinfo *si;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700140
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000141 si = find_svc(s, len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000142 //ALOGI("check_service('%s') handle = %x\n", str8(s), si ? si->handle : 0);
143 if (si && si->handle) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700144 if (!si->allow_isolated) {
145 // If this service doesn't allow access from isolated processes,
146 // then check the uid to see if it is isolated.
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000147 uid_t appid = uid % AID_USER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700148 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
149 return 0;
150 }
151 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000152 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700153 } else {
154 return 0;
155 }
156}
157
158int do_add_service(struct binder_state *bs,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000159 const uint16_t *s, size_t len,
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000160 uint32_t handle, uid_t uid, int allow_isolated)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700161{
162 struct svcinfo *si;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000163
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000164 //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s), handle,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700165 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
166
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000167 if (!handle || (len == 0) || (len > 127))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700168 return -1;
169
170 if (!svc_can_register(uid, s)) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000171 ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
172 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700173 return -1;
174 }
175
176 si = find_svc(s, len);
177 if (si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000178 if (si->handle) {
179 ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
180 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700181 svcinfo_death(bs, si);
182 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000183 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700184 } else {
185 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
186 if (!si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000187 ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
188 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700189 return -1;
190 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000191 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700192 si->len = len;
193 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
194 si->name[len] = '\0';
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000195 si->death.func = (void*) svcinfo_death;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700196 si->death.ptr = si;
197 si->allow_isolated = allow_isolated;
198 si->next = svclist;
199 svclist = si;
200 }
201
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000202 binder_acquire(bs, handle);
203 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700204 return 0;
205}
206
207int svcmgr_handler(struct binder_state *bs,
Serban Constantinescubcf38882014-01-10 13:56:27 +0000208 struct binder_transaction_data *txn,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700209 struct binder_io *msg,
210 struct binder_io *reply)
211{
212 struct svcinfo *si;
213 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000214 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000215 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700216 uint32_t strict_policy;
217 int allow_isolated;
218
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000219 //ALOGI("target=%x code=%d pid=%d uid=%d\n",
220 // txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700221
Serban Constantinescubcf38882014-01-10 13:56:27 +0000222 if (txn->target.handle != svcmgr_handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700223 return -1;
224
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800225 if (txn->code == PING_TRANSACTION)
226 return 0;
227
Mike Lockwood94afecf2012-10-24 10:45:23 -0700228 // Equivalent to Parcel::enforceInterface(), reading the RPC
229 // header with the strict mode policy mask and the interface name.
230 // Note that we ignore the strict_policy and don't propagate it
231 // further (since we do no outbound RPCs anyway).
232 strict_policy = bio_get_uint32(msg);
233 s = bio_get_string16(msg, &len);
234 if ((len != (sizeof(svcmgr_id) / 2)) ||
235 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
236 fprintf(stderr,"invalid id %s\n", str8(s));
237 return -1;
238 }
239
240 switch(txn->code) {
241 case SVC_MGR_GET_SERVICE:
242 case SVC_MGR_CHECK_SERVICE:
243 s = bio_get_string16(msg, &len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000244 handle = do_find_service(bs, s, len, txn->sender_euid);
245 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700246 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000247 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700248 return 0;
249
250 case SVC_MGR_ADD_SERVICE:
251 s = bio_get_string16(msg, &len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000252 handle = bio_get_ref(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700253 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000254 if (do_add_service(bs, s, len, handle, txn->sender_euid, allow_isolated))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700255 return -1;
256 break;
257
258 case SVC_MGR_LIST_SERVICES: {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000259 uint32_t n = bio_get_uint32(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700260
261 si = svclist;
262 while ((n-- > 0) && si)
263 si = si->next;
264 if (si) {
265 bio_put_string16(reply, si->name);
266 return 0;
267 }
268 return -1;
269 }
270 default:
271 ALOGE("unknown code %d\n", txn->code);
272 return -1;
273 }
274
275 bio_put_uint32(reply, 0);
276 return 0;
277}
278
279int main(int argc, char **argv)
280{
281 struct binder_state *bs;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700282
283 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000284 if (!bs) {
285 ALOGE("failed to open binder driver\n");
286 return -1;
287 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700288
289 if (binder_become_context_manager(bs)) {
290 ALOGE("cannot become context manager (%s)\n", strerror(errno));
291 return -1;
292 }
293
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000294 svcmgr_handle = BINDER_SERVICE_MANAGER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700295 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000296
Mike Lockwood94afecf2012-10-24 10:45:23 -0700297 return 0;
298}