blob: 79ce6eda5b8bc90255fb5a99b3ce115f42047a59 [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" },
35 { AID_DRM, "drm.drmManager" },
36 { AID_NFC, "nfc" },
37 { AID_BLUETOOTH, "bluetooth" },
38 { AID_RADIO, "radio.phone" },
39 { AID_RADIO, "radio.sms" },
40 { AID_RADIO, "radio.phonesubinfo" },
41 { AID_RADIO, "radio.simphonebook" },
42/* TODO: remove after phone services are updated: */
43 { AID_RADIO, "phone" },
44 { AID_RADIO, "sip" },
45 { AID_RADIO, "isms" },
46 { AID_RADIO, "iphonesubinfo" },
47 { AID_RADIO, "simphonebook" },
48 { AID_MEDIA, "common_time.clock" },
49 { AID_MEDIA, "common_time.config" },
Kenny Root24440872012-11-14 15:42:38 -080050 { AID_KEYSTORE, "android.security.keystore" },
Mike Lockwood94afecf2012-10-24 10:45:23 -070051};
52
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000053uint32_t svcmgr_handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -070054
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000055const char *str8(const uint16_t *x)
Mike Lockwood94afecf2012-10-24 10:45:23 -070056{
57 static char buf[128];
58 unsigned max = 127;
59 char *p = buf;
60
61 if (x) {
62 while (*x && max--) {
63 *p++ = *x++;
64 }
65 }
66 *p++ = 0;
67 return buf;
68}
69
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000070int str16eq(const uint16_t *a, const char *b)
Mike Lockwood94afecf2012-10-24 10:45:23 -070071{
72 while (*a && *b)
73 if (*a++ != *b++) return 0;
74 if (*a || *b)
75 return 0;
76 return 1;
77}
78
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000079int svc_can_register(uid_t uid, const uint16_t *name)
Mike Lockwood94afecf2012-10-24 10:45:23 -070080{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000081 size_t n;
82
Mike Lockwood94afecf2012-10-24 10:45:23 -070083 if ((uid == 0) || (uid == AID_SYSTEM))
84 return 1;
85
86 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
87 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
88 return 1;
89
90 return 0;
91}
92
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000093struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -070094{
95 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000096 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -070097 struct binder_death death;
98 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000099 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700100 uint16_t name[0];
101};
102
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000103struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700104
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000105struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700106{
107 struct svcinfo *si;
108
109 for (si = svclist; si; si = si->next) {
110 if ((len == si->len) &&
111 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
112 return si;
113 }
114 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000115 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700116}
117
118void svcinfo_death(struct binder_state *bs, void *ptr)
119{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000120 struct svcinfo *si = (struct svcinfo* ) ptr;
121
Mike Lockwood94afecf2012-10-24 10:45:23 -0700122 ALOGI("service '%s' died\n", str8(si->name));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000123 if (si->handle) {
124 binder_release(bs, si->handle);
125 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000126 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700127}
128
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000129uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700130 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000131 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700132};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700133
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000134
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000135uint32_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 -0700136{
137 struct svcinfo *si;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700138
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000139 si = find_svc(s, len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000140 //ALOGI("check_service('%s') handle = %x\n", str8(s), si ? si->handle : 0);
141 if (si && si->handle) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700142 if (!si->allow_isolated) {
143 // If this service doesn't allow access from isolated processes,
144 // then check the uid to see if it is isolated.
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000145 uid_t appid = uid % AID_USER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700146 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
147 return 0;
148 }
149 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000150 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700151 } else {
152 return 0;
153 }
154}
155
156int do_add_service(struct binder_state *bs,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000157 const uint16_t *s, size_t len,
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000158 uint32_t handle, uid_t uid, int allow_isolated)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700159{
160 struct svcinfo *si;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000161
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000162 //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s), handle,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700163 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
164
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000165 if (!handle || (len == 0) || (len > 127))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700166 return -1;
167
168 if (!svc_can_register(uid, s)) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000169 ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
170 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700171 return -1;
172 }
173
174 si = find_svc(s, len);
175 if (si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000176 if (si->handle) {
177 ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
178 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700179 svcinfo_death(bs, si);
180 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000181 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700182 } else {
183 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
184 if (!si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000185 ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
186 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700187 return -1;
188 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000189 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700190 si->len = len;
191 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
192 si->name[len] = '\0';
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000193 si->death.func = (void*) svcinfo_death;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700194 si->death.ptr = si;
195 si->allow_isolated = allow_isolated;
196 si->next = svclist;
197 svclist = si;
198 }
199
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000200 binder_acquire(bs, handle);
201 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700202 return 0;
203}
204
205int svcmgr_handler(struct binder_state *bs,
Serban Constantinescubcf38882014-01-10 13:56:27 +0000206 struct binder_transaction_data *txn,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700207 struct binder_io *msg,
208 struct binder_io *reply)
209{
210 struct svcinfo *si;
211 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000212 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000213 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700214 uint32_t strict_policy;
215 int allow_isolated;
216
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000217 //ALOGI("target=%x code=%d pid=%d uid=%d\n",
218 // txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700219
Serban Constantinescubcf38882014-01-10 13:56:27 +0000220 if (txn->target.handle != svcmgr_handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700221 return -1;
222
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800223 if (txn->code == PING_TRANSACTION)
224 return 0;
225
Mike Lockwood94afecf2012-10-24 10:45:23 -0700226 // Equivalent to Parcel::enforceInterface(), reading the RPC
227 // header with the strict mode policy mask and the interface name.
228 // Note that we ignore the strict_policy and don't propagate it
229 // further (since we do no outbound RPCs anyway).
230 strict_policy = bio_get_uint32(msg);
231 s = bio_get_string16(msg, &len);
232 if ((len != (sizeof(svcmgr_id) / 2)) ||
233 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
234 fprintf(stderr,"invalid id %s\n", str8(s));
235 return -1;
236 }
237
238 switch(txn->code) {
239 case SVC_MGR_GET_SERVICE:
240 case SVC_MGR_CHECK_SERVICE:
241 s = bio_get_string16(msg, &len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000242 handle = do_find_service(bs, s, len, txn->sender_euid);
243 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700244 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000245 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700246 return 0;
247
248 case SVC_MGR_ADD_SERVICE:
249 s = bio_get_string16(msg, &len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000250 handle = bio_get_ref(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700251 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000252 if (do_add_service(bs, s, len, handle, txn->sender_euid, allow_isolated))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700253 return -1;
254 break;
255
256 case SVC_MGR_LIST_SERVICES: {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000257 uint32_t n = bio_get_uint32(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700258
259 si = svclist;
260 while ((n-- > 0) && si)
261 si = si->next;
262 if (si) {
263 bio_put_string16(reply, si->name);
264 return 0;
265 }
266 return -1;
267 }
268 default:
269 ALOGE("unknown code %d\n", txn->code);
270 return -1;
271 }
272
273 bio_put_uint32(reply, 0);
274 return 0;
275}
276
277int main(int argc, char **argv)
278{
279 struct binder_state *bs;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700280
281 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000282 if (!bs) {
283 ALOGE("failed to open binder driver\n");
284 return -1;
285 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700286
287 if (binder_become_context_manager(bs)) {
288 ALOGE("cannot become context manager (%s)\n", strerror(errno));
289 return -1;
290 }
291
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000292 svcmgr_handle = BINDER_SERVICE_MANAGER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700293 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000294
Mike Lockwood94afecf2012-10-24 10:45:23 -0700295 return 0;
296}