blob: 939920a3113e2440f2ebae91b7448a782c4f6a4f [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
Riley Spahn69154df2014-06-05 11:07:18 -070011#include <selinux/android.h>
12
Mike Lockwood94afecf2012-10-24 10:45:23 -070013#include "binder.h"
14
15#if 0
16#define ALOGI(x...) fprintf(stderr, "svcmgr: " x)
17#define ALOGE(x...) fprintf(stderr, "svcmgr: " x)
18#else
19#define LOG_TAG "ServiceManager"
20#include <cutils/log.h>
21#endif
22
23/* TODO:
24 * These should come from a config file or perhaps be
25 * based on some namespace rules of some sort (media
26 * uid can register media.*, etc)
27 */
28static struct {
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000029 uid_t uid;
Mike Lockwood94afecf2012-10-24 10:45:23 -070030 const char *name;
31} allowed[] = {
32 { AID_MEDIA, "media.audio_flinger" },
Glenn Kasten64c8be02013-01-16 12:06:39 -080033 { AID_MEDIA, "media.log" },
Mike Lockwood94afecf2012-10-24 10:45:23 -070034 { AID_MEDIA, "media.player" },
35 { AID_MEDIA, "media.camera" },
36 { AID_MEDIA, "media.audio_policy" },
37 { 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
Riley Spahn69154df2014-06-05 11:07:18 -070081static struct selabel_handle* sehandle;
82
83static bool check_mac_perms(const char *name, pid_t spid)
84{
85 if (is_selinux_enabled() <= 0) {
86 return true;
87 }
88
89 bool allowed = false;
90
91 const char *class = "service_manager";
92 const char *perm = "add";
93
94 char *tctx = NULL;
95 char *sctx = NULL;
96
97 if (!sehandle) {
98 ALOGE("SELinux: Failed to find sehandle %s.\n", name);
99 return false;
100 }
101
102 if (getpidcon(spid, &sctx) < 0) {
103 ALOGE("SELinux: getpidcon failed to retrieve pid context.\n");
104 return false;
105 }
106
107 if (!sctx) {
108 ALOGE("SELinux: Failed to find sctx for %s.\n", name);
109 return false;
110 }
111
112 if (selabel_lookup(sehandle, &tctx, name, 1) != 0) {
113 ALOGE("SELinux: selabel_lookup failed to set tctx for %s.\n", name);
114 freecon(sctx);
115 return false;
116 }
117
118 if (!tctx) {
119 ALOGE("SELinux: Failed to find tctx for %s.\n", name);
120 freecon(sctx);
121 return false;
122 }
123
124 int result = selinux_check_access(sctx, tctx, class, perm, (void *) name);
125 allowed = (result == 0);
126
127 freecon(sctx);
128 freecon(tctx);
129 return allowed;
130}
131
132static int svc_can_register(uid_t uid, const uint16_t *name, pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700133{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000134 size_t n;
135
Mike Lockwood94afecf2012-10-24 10:45:23 -0700136 if ((uid == 0) || (uid == AID_SYSTEM))
Riley Spahn69154df2014-06-05 11:07:18 -0700137 return check_mac_perms(str8(name), spid) ? 1 : 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700138
139 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
140 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
Riley Spahn69154df2014-06-05 11:07:18 -0700141 return check_mac_perms(str8(name), spid) ? 1 : 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700142
143 return 0;
144}
145
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000146struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -0700147{
148 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000149 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700150 struct binder_death death;
151 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000152 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700153 uint16_t name[0];
154};
155
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000156struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700157
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000158struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700159{
160 struct svcinfo *si;
161
162 for (si = svclist; si; si = si->next) {
163 if ((len == si->len) &&
164 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
165 return si;
166 }
167 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000168 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700169}
170
171void svcinfo_death(struct binder_state *bs, void *ptr)
172{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000173 struct svcinfo *si = (struct svcinfo* ) ptr;
174
Mike Lockwood94afecf2012-10-24 10:45:23 -0700175 ALOGI("service '%s' died\n", str8(si->name));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000176 if (si->handle) {
177 binder_release(bs, si->handle);
178 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000179 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700180}
181
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000182uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700183 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000184 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700185};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700186
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000187
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000188uint32_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 -0700189{
190 struct svcinfo *si;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700191
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000192 si = find_svc(s, len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000193 //ALOGI("check_service('%s') handle = %x\n", str8(s), si ? si->handle : 0);
194 if (si && si->handle) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700195 if (!si->allow_isolated) {
196 // If this service doesn't allow access from isolated processes,
197 // then check the uid to see if it is isolated.
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000198 uid_t appid = uid % AID_USER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700199 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
200 return 0;
201 }
202 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000203 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700204 } else {
205 return 0;
206 }
207}
208
209int do_add_service(struct binder_state *bs,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000210 const uint16_t *s, size_t len,
Riley Spahn69154df2014-06-05 11:07:18 -0700211 uint32_t handle, uid_t uid, int allow_isolated,
212 pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700213{
214 struct svcinfo *si;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000215
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000216 //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s), handle,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700217 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
218
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000219 if (!handle || (len == 0) || (len > 127))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700220 return -1;
221
Riley Spahn69154df2014-06-05 11:07:18 -0700222 if (!svc_can_register(uid, s, spid)) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000223 ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
224 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700225 return -1;
226 }
227
228 si = find_svc(s, len);
229 if (si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000230 if (si->handle) {
231 ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
232 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700233 svcinfo_death(bs, si);
234 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000235 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700236 } else {
237 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
238 if (!si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000239 ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
240 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700241 return -1;
242 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000243 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700244 si->len = len;
245 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
246 si->name[len] = '\0';
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000247 si->death.func = (void*) svcinfo_death;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700248 si->death.ptr = si;
249 si->allow_isolated = allow_isolated;
250 si->next = svclist;
251 svclist = si;
252 }
253
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000254 binder_acquire(bs, handle);
255 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700256 return 0;
257}
258
259int svcmgr_handler(struct binder_state *bs,
Serban Constantinescubcf38882014-01-10 13:56:27 +0000260 struct binder_transaction_data *txn,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700261 struct binder_io *msg,
262 struct binder_io *reply)
263{
264 struct svcinfo *si;
265 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000266 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000267 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700268 uint32_t strict_policy;
269 int allow_isolated;
270
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000271 //ALOGI("target=%x code=%d pid=%d uid=%d\n",
272 // txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700273
Serban Constantinescubcf38882014-01-10 13:56:27 +0000274 if (txn->target.handle != svcmgr_handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700275 return -1;
276
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800277 if (txn->code == PING_TRANSACTION)
278 return 0;
279
Mike Lockwood94afecf2012-10-24 10:45:23 -0700280 // Equivalent to Parcel::enforceInterface(), reading the RPC
281 // header with the strict mode policy mask and the interface name.
282 // Note that we ignore the strict_policy and don't propagate it
283 // further (since we do no outbound RPCs anyway).
284 strict_policy = bio_get_uint32(msg);
285 s = bio_get_string16(msg, &len);
286 if ((len != (sizeof(svcmgr_id) / 2)) ||
287 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
288 fprintf(stderr,"invalid id %s\n", str8(s));
289 return -1;
290 }
291
Riley Spahn69154df2014-06-05 11:07:18 -0700292 if (sehandle && selinux_status_updated() > 0) {
293 struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
294 if (tmp_sehandle) {
295 selabel_close(sehandle);
296 sehandle = tmp_sehandle;
297 }
298 }
299
Mike Lockwood94afecf2012-10-24 10:45:23 -0700300 switch(txn->code) {
301 case SVC_MGR_GET_SERVICE:
302 case SVC_MGR_CHECK_SERVICE:
303 s = bio_get_string16(msg, &len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000304 handle = do_find_service(bs, s, len, txn->sender_euid);
305 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700306 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000307 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700308 return 0;
309
310 case SVC_MGR_ADD_SERVICE:
311 s = bio_get_string16(msg, &len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000312 handle = bio_get_ref(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700313 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
Riley Spahn69154df2014-06-05 11:07:18 -0700314 if (do_add_service(bs, s, len, handle, txn->sender_euid,
315 allow_isolated, txn->sender_pid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700316 return -1;
317 break;
318
319 case SVC_MGR_LIST_SERVICES: {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000320 uint32_t n = bio_get_uint32(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700321
322 si = svclist;
323 while ((n-- > 0) && si)
324 si = si->next;
325 if (si) {
326 bio_put_string16(reply, si->name);
327 return 0;
328 }
329 return -1;
330 }
331 default:
332 ALOGE("unknown code %d\n", txn->code);
333 return -1;
334 }
335
336 bio_put_uint32(reply, 0);
337 return 0;
338}
339
Riley Spahn69154df2014-06-05 11:07:18 -0700340
341static int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
342{
343 snprintf(buf, len, "service=%s", !data ? "NULL" : (char *)data);
344 return 0;
345}
346
Mike Lockwood94afecf2012-10-24 10:45:23 -0700347int main(int argc, char **argv)
348{
349 struct binder_state *bs;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700350
351 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000352 if (!bs) {
353 ALOGE("failed to open binder driver\n");
354 return -1;
355 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700356
357 if (binder_become_context_manager(bs)) {
358 ALOGE("cannot become context manager (%s)\n", strerror(errno));
359 return -1;
360 }
361
Riley Spahn69154df2014-06-05 11:07:18 -0700362 sehandle = selinux_android_service_context_handle();
363
364 union selinux_callback cb;
365 cb.func_audit = audit_callback;
366 selinux_set_callback(SELINUX_CB_AUDIT, cb);
367 cb.func_log = selinux_log_callback;
368 selinux_set_callback(SELINUX_CB_LOG, cb);
369
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000370 svcmgr_handle = BINDER_SERVICE_MANAGER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700371 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000372
Mike Lockwood94afecf2012-10-24 10:45:23 -0700373 return 0;
374}