blob: 39dfbd73539bc7be5d59450f16dc93b905edc381 [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" },
Eric Laurent9882be82014-04-18 17:52:57 -070037 { AID_MEDIA, "media.sound_trigger_hw" },
Mike Lockwood2a5fd8b2013-09-11 08:10:11 -070038 { AID_AUDIO, "audio" },
Mike Lockwood30a7d5a2013-09-13 07:26:05 -070039 { AID_INPUT, "input" },
Mike Lockwood94afecf2012-10-24 10:45:23 -070040 { AID_DRM, "drm.drmManager" },
41 { AID_NFC, "nfc" },
42 { AID_BLUETOOTH, "bluetooth" },
43 { AID_RADIO, "radio.phone" },
44 { AID_RADIO, "radio.sms" },
45 { AID_RADIO, "radio.phonesubinfo" },
46 { AID_RADIO, "radio.simphonebook" },
47/* TODO: remove after phone services are updated: */
48 { AID_RADIO, "phone" },
49 { AID_RADIO, "sip" },
50 { AID_RADIO, "isms" },
51 { AID_RADIO, "iphonesubinfo" },
52 { AID_RADIO, "simphonebook" },
53 { AID_MEDIA, "common_time.clock" },
54 { AID_MEDIA, "common_time.config" },
Kenny Root24440872012-11-14 15:42:38 -080055 { AID_KEYSTORE, "android.security.keystore" },
Santos Cordon3fb4de72014-05-21 22:58:55 -070056 { AID_RADIO, "telecomm" },
Mike Lockwood94afecf2012-10-24 10:45:23 -070057};
58
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000059uint32_t svcmgr_handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -070060
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000061const char *str8(const uint16_t *x)
Mike Lockwood94afecf2012-10-24 10:45:23 -070062{
63 static char buf[128];
64 unsigned max = 127;
65 char *p = buf;
66
67 if (x) {
68 while (*x && max--) {
69 *p++ = *x++;
70 }
71 }
72 *p++ = 0;
73 return buf;
74}
75
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000076int str16eq(const uint16_t *a, const char *b)
Mike Lockwood94afecf2012-10-24 10:45:23 -070077{
78 while (*a && *b)
79 if (*a++ != *b++) return 0;
80 if (*a || *b)
81 return 0;
82 return 1;
83}
84
Riley Spahn69154df2014-06-05 11:07:18 -070085static struct selabel_handle* sehandle;
86
87static bool check_mac_perms(const char *name, pid_t spid)
88{
89 if (is_selinux_enabled() <= 0) {
90 return true;
91 }
92
93 bool allowed = false;
94
95 const char *class = "service_manager";
96 const char *perm = "add";
97
98 char *tctx = NULL;
99 char *sctx = NULL;
100
101 if (!sehandle) {
102 ALOGE("SELinux: Failed to find sehandle %s.\n", name);
103 return false;
104 }
105
106 if (getpidcon(spid, &sctx) < 0) {
107 ALOGE("SELinux: getpidcon failed to retrieve pid context.\n");
108 return false;
109 }
110
111 if (!sctx) {
112 ALOGE("SELinux: Failed to find sctx for %s.\n", name);
113 return false;
114 }
115
116 if (selabel_lookup(sehandle, &tctx, name, 1) != 0) {
117 ALOGE("SELinux: selabel_lookup failed to set tctx for %s.\n", name);
118 freecon(sctx);
119 return false;
120 }
121
122 if (!tctx) {
123 ALOGE("SELinux: Failed to find tctx for %s.\n", name);
124 freecon(sctx);
125 return false;
126 }
127
128 int result = selinux_check_access(sctx, tctx, class, perm, (void *) name);
129 allowed = (result == 0);
130
131 freecon(sctx);
132 freecon(tctx);
133 return allowed;
134}
135
136static int svc_can_register(uid_t uid, const uint16_t *name, pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700137{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000138 size_t n;
139
Mike Lockwood94afecf2012-10-24 10:45:23 -0700140 if ((uid == 0) || (uid == AID_SYSTEM))
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 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
144 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
Riley Spahn69154df2014-06-05 11:07:18 -0700145 return check_mac_perms(str8(name), spid) ? 1 : 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700146
147 return 0;
148}
149
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000150struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -0700151{
152 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000153 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700154 struct binder_death death;
155 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000156 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700157 uint16_t name[0];
158};
159
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000160struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700161
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000162struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700163{
164 struct svcinfo *si;
165
166 for (si = svclist; si; si = si->next) {
167 if ((len == si->len) &&
168 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
169 return si;
170 }
171 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000172 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700173}
174
175void svcinfo_death(struct binder_state *bs, void *ptr)
176{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000177 struct svcinfo *si = (struct svcinfo* ) ptr;
178
Mike Lockwood94afecf2012-10-24 10:45:23 -0700179 ALOGI("service '%s' died\n", str8(si->name));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000180 if (si->handle) {
181 binder_release(bs, si->handle);
182 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000183 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700184}
185
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000186uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700187 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000188 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700189};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700190
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000191
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000192uint32_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 -0700193{
194 struct svcinfo *si;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700195
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000196 si = find_svc(s, len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000197 //ALOGI("check_service('%s') handle = %x\n", str8(s), si ? si->handle : 0);
198 if (si && si->handle) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700199 if (!si->allow_isolated) {
200 // If this service doesn't allow access from isolated processes,
201 // then check the uid to see if it is isolated.
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000202 uid_t appid = uid % AID_USER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700203 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
204 return 0;
205 }
206 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000207 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700208 } else {
209 return 0;
210 }
211}
212
213int do_add_service(struct binder_state *bs,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000214 const uint16_t *s, size_t len,
Riley Spahn69154df2014-06-05 11:07:18 -0700215 uint32_t handle, uid_t uid, int allow_isolated,
216 pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700217{
218 struct svcinfo *si;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000219
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000220 //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s), handle,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700221 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
222
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000223 if (!handle || (len == 0) || (len > 127))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700224 return -1;
225
Riley Spahn69154df2014-06-05 11:07:18 -0700226 if (!svc_can_register(uid, s, spid)) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000227 ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
228 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700229 return -1;
230 }
231
232 si = find_svc(s, len);
233 if (si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000234 if (si->handle) {
235 ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
236 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700237 svcinfo_death(bs, si);
238 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000239 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700240 } else {
241 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
242 if (!si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000243 ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
244 str8(s), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700245 return -1;
246 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000247 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700248 si->len = len;
249 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
250 si->name[len] = '\0';
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000251 si->death.func = (void*) svcinfo_death;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700252 si->death.ptr = si;
253 si->allow_isolated = allow_isolated;
254 si->next = svclist;
255 svclist = si;
256 }
257
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000258 binder_acquire(bs, handle);
259 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700260 return 0;
261}
262
263int svcmgr_handler(struct binder_state *bs,
Serban Constantinescubcf38882014-01-10 13:56:27 +0000264 struct binder_transaction_data *txn,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700265 struct binder_io *msg,
266 struct binder_io *reply)
267{
268 struct svcinfo *si;
269 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000270 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000271 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700272 uint32_t strict_policy;
273 int allow_isolated;
274
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000275 //ALOGI("target=%x code=%d pid=%d uid=%d\n",
276 // txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700277
Serban Constantinescubcf38882014-01-10 13:56:27 +0000278 if (txn->target.handle != svcmgr_handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700279 return -1;
280
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800281 if (txn->code == PING_TRANSACTION)
282 return 0;
283
Mike Lockwood94afecf2012-10-24 10:45:23 -0700284 // Equivalent to Parcel::enforceInterface(), reading the RPC
285 // header with the strict mode policy mask and the interface name.
286 // Note that we ignore the strict_policy and don't propagate it
287 // further (since we do no outbound RPCs anyway).
288 strict_policy = bio_get_uint32(msg);
289 s = bio_get_string16(msg, &len);
290 if ((len != (sizeof(svcmgr_id) / 2)) ||
291 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
292 fprintf(stderr,"invalid id %s\n", str8(s));
293 return -1;
294 }
295
Riley Spahn69154df2014-06-05 11:07:18 -0700296 if (sehandle && selinux_status_updated() > 0) {
297 struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
298 if (tmp_sehandle) {
299 selabel_close(sehandle);
300 sehandle = tmp_sehandle;
301 }
302 }
303
Mike Lockwood94afecf2012-10-24 10:45:23 -0700304 switch(txn->code) {
305 case SVC_MGR_GET_SERVICE:
306 case SVC_MGR_CHECK_SERVICE:
307 s = bio_get_string16(msg, &len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000308 handle = do_find_service(bs, s, len, txn->sender_euid);
309 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700310 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000311 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700312 return 0;
313
314 case SVC_MGR_ADD_SERVICE:
315 s = bio_get_string16(msg, &len);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000316 handle = bio_get_ref(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700317 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
Riley Spahn69154df2014-06-05 11:07:18 -0700318 if (do_add_service(bs, s, len, handle, txn->sender_euid,
319 allow_isolated, txn->sender_pid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700320 return -1;
321 break;
322
323 case SVC_MGR_LIST_SERVICES: {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000324 uint32_t n = bio_get_uint32(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700325
326 si = svclist;
327 while ((n-- > 0) && si)
328 si = si->next;
329 if (si) {
330 bio_put_string16(reply, si->name);
331 return 0;
332 }
333 return -1;
334 }
335 default:
336 ALOGE("unknown code %d\n", txn->code);
337 return -1;
338 }
339
340 bio_put_uint32(reply, 0);
341 return 0;
342}
343
Riley Spahn69154df2014-06-05 11:07:18 -0700344
345static int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
346{
347 snprintf(buf, len, "service=%s", !data ? "NULL" : (char *)data);
348 return 0;
349}
350
Mike Lockwood94afecf2012-10-24 10:45:23 -0700351int main(int argc, char **argv)
352{
353 struct binder_state *bs;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700354
355 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000356 if (!bs) {
357 ALOGE("failed to open binder driver\n");
358 return -1;
359 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700360
361 if (binder_become_context_manager(bs)) {
362 ALOGE("cannot become context manager (%s)\n", strerror(errno));
363 return -1;
364 }
365
Riley Spahn69154df2014-06-05 11:07:18 -0700366 sehandle = selinux_android_service_context_handle();
367
368 union selinux_callback cb;
369 cb.func_audit = audit_callback;
370 selinux_set_callback(SELINUX_CB_AUDIT, cb);
371 cb.func_log = selinux_log_callback;
372 selinux_set_callback(SELINUX_CB_LOG, cb);
373
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000374 svcmgr_handle = BINDER_SERVICE_MANAGER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700375 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000376
Mike Lockwood94afecf2012-10-24 10:45:23 -0700377 return 0;
378}