blob: 32000127dec3ba1a45d178b118396c391c3f5785 [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" },
Eric Laurent9882be82014-04-18 17:52:57 -070035 { AID_MEDIA, "media.sound_trigger_hw" },
Mike Lockwood2a5fd8b2013-09-11 08:10:11 -070036 { AID_AUDIO, "audio" },
Mike Lockwood30a7d5a2013-09-13 07:26:05 -070037 { AID_INPUT, "input" },
Mike Lockwood94afecf2012-10-24 10:45:23 -070038 { AID_DRM, "drm.drmManager" },
39 { AID_NFC, "nfc" },
40 { AID_BLUETOOTH, "bluetooth" },
41 { AID_RADIO, "radio.phone" },
42 { AID_RADIO, "radio.sms" },
43 { AID_RADIO, "radio.phonesubinfo" },
44 { AID_RADIO, "radio.simphonebook" },
45/* TODO: remove after phone services are updated: */
46 { AID_RADIO, "phone" },
47 { AID_RADIO, "sip" },
48 { AID_RADIO, "isms" },
49 { AID_RADIO, "iphonesubinfo" },
50 { AID_RADIO, "simphonebook" },
51 { AID_MEDIA, "common_time.clock" },
52 { AID_MEDIA, "common_time.config" },
Kenny Root24440872012-11-14 15:42:38 -080053 { AID_KEYSTORE, "android.security.keystore" },
Santos Cordon3fb4de72014-05-21 22:58:55 -070054 { AID_RADIO, "telecomm" },
Mike Lockwood94afecf2012-10-24 10:45:23 -070055};
56
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000057uint32_t svcmgr_handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -070058
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000059const char *str8(const uint16_t *x)
Mike Lockwood94afecf2012-10-24 10:45:23 -070060{
61 static char buf[128];
62 unsigned max = 127;
63 char *p = buf;
64
65 if (x) {
66 while (*x && max--) {
67 *p++ = *x++;
68 }
69 }
70 *p++ = 0;
71 return buf;
72}
73
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000074int str16eq(const uint16_t *a, const char *b)
Mike Lockwood94afecf2012-10-24 10:45:23 -070075{
76 while (*a && *b)
77 if (*a++ != *b++) return 0;
78 if (*a || *b)
79 return 0;
80 return 1;
81}
82
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000083int svc_can_register(uid_t uid, const uint16_t *name)
Mike Lockwood94afecf2012-10-24 10:45:23 -070084{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000085 size_t n;
86
Mike Lockwood94afecf2012-10-24 10:45:23 -070087 if ((uid == 0) || (uid == AID_SYSTEM))
88 return 1;
89
90 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
91 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
92 return 1;
93
94 return 0;
95}
96
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000097struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -070098{
99 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000100 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700101 struct binder_death death;
102 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000103 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700104 uint16_t name[0];
105};
106
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000107struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700108
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000109struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700110{
111 struct svcinfo *si;
112
113 for (si = svclist; si; si = si->next) {
114 if ((len == si->len) &&
115 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
116 return si;
117 }
118 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000119 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700120}
121
122void svcinfo_death(struct binder_state *bs, void *ptr)
123{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000124 struct svcinfo *si = (struct svcinfo* ) ptr;
125
Mike Lockwood94afecf2012-10-24 10:45:23 -0700126 ALOGI("service '%s' died\n", str8(si->name));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000127 if (si->handle) {
128 binder_release(bs, si->handle);
129 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000130 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700131}
132
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000133uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700134 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000135 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700136};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700137
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000138
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000139uint32_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 -0700140{
141 struct svcinfo *si;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700142
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000143 si = find_svc(s, len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000144 //ALOGI("check_service('%s') handle = %x\n", str8(s), si ? si->handle : 0);
145 if (si && si->handle) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700146 if (!si->allow_isolated) {
147 // If this service doesn't allow access from isolated processes,
148 // then check the uid to see if it is isolated.
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000149 uid_t appid = uid % AID_USER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700150 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
151 return 0;
152 }
153 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000154 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700155 } else {
156 return 0;
157 }
158}
159
160int do_add_service(struct binder_state *bs,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000161 const uint16_t *s, size_t len,
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000162 uint32_t handle, uid_t uid, int allow_isolated)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700163{
164 struct svcinfo *si;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000165
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000166 //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s), handle,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700167 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
168
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000169 if (!handle || (len == 0) || (len > 127))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700170 return -1;
171
172 if (!svc_can_register(uid, s)) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000173 ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
174 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700175 return -1;
176 }
177
178 si = find_svc(s, len);
179 if (si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000180 if (si->handle) {
181 ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
182 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700183 svcinfo_death(bs, si);
184 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000185 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700186 } else {
187 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
188 if (!si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000189 ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
190 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700191 return -1;
192 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000193 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700194 si->len = len;
195 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
196 si->name[len] = '\0';
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000197 si->death.func = (void*) svcinfo_death;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700198 si->death.ptr = si;
199 si->allow_isolated = allow_isolated;
200 si->next = svclist;
201 svclist = si;
202 }
203
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000204 binder_acquire(bs, handle);
205 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700206 return 0;
207}
208
209int svcmgr_handler(struct binder_state *bs,
Serban Constantinescubcf38882014-01-10 13:56:27 +0000210 struct binder_transaction_data *txn,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700211 struct binder_io *msg,
212 struct binder_io *reply)
213{
214 struct svcinfo *si;
215 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000216 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000217 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700218 uint32_t strict_policy;
219 int allow_isolated;
220
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000221 //ALOGI("target=%x code=%d pid=%d uid=%d\n",
222 // txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700223
Serban Constantinescubcf38882014-01-10 13:56:27 +0000224 if (txn->target.handle != svcmgr_handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700225 return -1;
226
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800227 if (txn->code == PING_TRANSACTION)
228 return 0;
229
Mike Lockwood94afecf2012-10-24 10:45:23 -0700230 // Equivalent to Parcel::enforceInterface(), reading the RPC
231 // header with the strict mode policy mask and the interface name.
232 // Note that we ignore the strict_policy and don't propagate it
233 // further (since we do no outbound RPCs anyway).
234 strict_policy = bio_get_uint32(msg);
235 s = bio_get_string16(msg, &len);
236 if ((len != (sizeof(svcmgr_id) / 2)) ||
237 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
238 fprintf(stderr,"invalid id %s\n", str8(s));
239 return -1;
240 }
241
242 switch(txn->code) {
243 case SVC_MGR_GET_SERVICE:
244 case SVC_MGR_CHECK_SERVICE:
245 s = bio_get_string16(msg, &len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000246 handle = do_find_service(bs, s, len, txn->sender_euid);
247 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700248 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000249 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700250 return 0;
251
252 case SVC_MGR_ADD_SERVICE:
253 s = bio_get_string16(msg, &len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000254 handle = bio_get_ref(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700255 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000256 if (do_add_service(bs, s, len, handle, txn->sender_euid, allow_isolated))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700257 return -1;
258 break;
259
260 case SVC_MGR_LIST_SERVICES: {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000261 uint32_t n = bio_get_uint32(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700262
263 si = svclist;
264 while ((n-- > 0) && si)
265 si = si->next;
266 if (si) {
267 bio_put_string16(reply, si->name);
268 return 0;
269 }
270 return -1;
271 }
272 default:
273 ALOGE("unknown code %d\n", txn->code);
274 return -1;
275 }
276
277 bio_put_uint32(reply, 0);
278 return 0;
279}
280
281int main(int argc, char **argv)
282{
283 struct binder_state *bs;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700284
285 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000286 if (!bs) {
287 ALOGE("failed to open binder driver\n");
288 return -1;
289 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700290
291 if (binder_become_context_manager(bs)) {
292 ALOGE("cannot become context manager (%s)\n", strerror(errno));
293 return -1;
294 }
295
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000296 svcmgr_handle = BINDER_SERVICE_MANAGER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700297 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000298
Mike Lockwood94afecf2012-10-24 10:45:23 -0700299 return 0;
300}