blob: 7c343af3f8803ea0332332b4968215a5d098a4fe [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>
Nick Kralevich652c4852014-07-12 16:34:01 -070012#include <selinux/avc.h>
Riley Spahn69154df2014-06-05 11:07:18 -070013
Mike Lockwood94afecf2012-10-24 10:45:23 -070014#include "binder.h"
15
16#if 0
17#define ALOGI(x...) fprintf(stderr, "svcmgr: " x)
18#define ALOGE(x...) fprintf(stderr, "svcmgr: " x)
19#else
20#define LOG_TAG "ServiceManager"
21#include <cutils/log.h>
22#endif
23
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000024uint32_t svcmgr_handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -070025
Nick Kralevich652c4852014-07-12 16:34:01 -070026const char *str8(const uint16_t *x, size_t x_len)
Mike Lockwood94afecf2012-10-24 10:45:23 -070027{
28 static char buf[128];
Nick Kralevich652c4852014-07-12 16:34:01 -070029 size_t max = 127;
Mike Lockwood94afecf2012-10-24 10:45:23 -070030 char *p = buf;
31
Nick Kralevich652c4852014-07-12 16:34:01 -070032 if (x_len < max) {
33 max = x_len;
34 }
35
Mike Lockwood94afecf2012-10-24 10:45:23 -070036 if (x) {
Nick Kralevich652c4852014-07-12 16:34:01 -070037 while ((max > 0) && (*x != '\0')) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070038 *p++ = *x++;
Nick Kralevich652c4852014-07-12 16:34:01 -070039 max--;
Mike Lockwood94afecf2012-10-24 10:45:23 -070040 }
41 }
42 *p++ = 0;
43 return buf;
44}
45
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000046int str16eq(const uint16_t *a, const char *b)
Mike Lockwood94afecf2012-10-24 10:45:23 -070047{
48 while (*a && *b)
49 if (*a++ != *b++) return 0;
50 if (*a || *b)
51 return 0;
52 return 1;
53}
54
Riley Spahn2a0e4092014-07-08 09:03:00 -070055static int selinux_enabled;
56static char *service_manager_context;
Riley Spahn69154df2014-06-05 11:07:18 -070057static struct selabel_handle* sehandle;
58
Riley Spahn2a0e4092014-07-08 09:03:00 -070059static bool check_mac_perms(pid_t spid, const char *tctx, const char *perm, const char *name)
Riley Spahn69154df2014-06-05 11:07:18 -070060{
Riley Spahn69154df2014-06-05 11:07:18 -070061 char *sctx = NULL;
Riley Spahn2a0e4092014-07-08 09:03:00 -070062 const char *class = "service_manager";
63 bool allowed;
Riley Spahn69154df2014-06-05 11:07:18 -070064
65 if (getpidcon(spid, &sctx) < 0) {
Riley Spahn2a0e4092014-07-08 09:03:00 -070066 ALOGE("SELinux: getpidcon(pid=%d) failed to retrieve pid context.\n", spid);
Riley Spahn69154df2014-06-05 11:07:18 -070067 return false;
68 }
69
70 int result = selinux_check_access(sctx, tctx, class, perm, (void *) name);
71 allowed = (result == 0);
72
73 freecon(sctx);
Riley Spahn2a0e4092014-07-08 09:03:00 -070074 return allowed;
75}
76
77static bool check_mac_perms_from_getcon(pid_t spid, const char *perm)
78{
79 if (selinux_enabled <= 0) {
80 return true;
81 }
82
83 return check_mac_perms(spid, service_manager_context, perm, NULL);
84}
85
86static bool check_mac_perms_from_lookup(pid_t spid, const char *perm, const char *name)
87{
88 bool allowed;
89 char *tctx = NULL;
90
91 if (selinux_enabled <= 0) {
92 return true;
93 }
94
95 if (!sehandle) {
96 ALOGE("SELinux: Failed to find sehandle. Aborting service_manager.\n");
97 abort();
98 }
99
100 if (selabel_lookup(sehandle, &tctx, name, 0) != 0) {
101 ALOGE("SELinux: No match for %s in service_contexts.\n", name);
102 return false;
103 }
104
105 allowed = check_mac_perms(spid, tctx, perm, name);
Riley Spahn69154df2014-06-05 11:07:18 -0700106 freecon(tctx);
107 return allowed;
108}
109
Arve Hjønnevågeb304f02016-08-01 16:05:17 -0700110static int svc_can_register(const uint16_t *name, size_t name_len, pid_t spid, uid_t uid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700111{
Riley Spahn2a0e4092014-07-08 09:03:00 -0700112 const char *perm = "add";
Arve Hjønnevågeb304f02016-08-01 16:05:17 -0700113
114 if (uid >= AID_APP) {
115 return 0; /* Don't allow apps to register services */
116 }
117
Riley Spahn2a0e4092014-07-08 09:03:00 -0700118 return check_mac_perms_from_lookup(spid, perm, str8(name, name_len)) ? 1 : 0;
119}
120
121static int svc_can_list(pid_t spid)
122{
123 const char *perm = "list";
124 return check_mac_perms_from_getcon(spid, perm) ? 1 : 0;
125}
126
127static int svc_can_find(const uint16_t *name, size_t name_len, pid_t spid)
128{
129 const char *perm = "find";
130 return check_mac_perms_from_lookup(spid, perm, str8(name, name_len)) ? 1 : 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700131}
132
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000133struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -0700134{
135 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000136 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700137 struct binder_death death;
138 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000139 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700140 uint16_t name[0];
141};
142
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000143struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700144
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000145struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700146{
147 struct svcinfo *si;
148
149 for (si = svclist; si; si = si->next) {
150 if ((len == si->len) &&
151 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
152 return si;
153 }
154 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000155 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700156}
157
158void svcinfo_death(struct binder_state *bs, void *ptr)
159{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000160 struct svcinfo *si = (struct svcinfo* ) ptr;
161
Nick Kralevich652c4852014-07-12 16:34:01 -0700162 ALOGI("service '%s' died\n", str8(si->name, si->len));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000163 if (si->handle) {
164 binder_release(bs, si->handle);
165 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000166 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700167}
168
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000169uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700170 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000171 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700172};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700173
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000174
Riley Spahn2a0e4092014-07-08 09:03:00 -0700175uint32_t do_find_service(struct binder_state *bs, const uint16_t *s, size_t len, uid_t uid, pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700176{
177 struct svcinfo *si;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700178
Riley Spahn2a0e4092014-07-08 09:03:00 -0700179 if (!svc_can_find(s, len, spid)) {
180 ALOGE("find_service('%s') uid=%d - PERMISSION DENIED\n",
181 str8(s, len), uid);
182 return 0;
183 }
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000184 si = find_svc(s, len);
Nick Kralevich652c4852014-07-12 16:34:01 -0700185 //ALOGI("check_service('%s') handle = %x\n", str8(s, len), si ? si->handle : 0);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000186 if (si && si->handle) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700187 if (!si->allow_isolated) {
188 // If this service doesn't allow access from isolated processes,
189 // then check the uid to see if it is isolated.
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000190 uid_t appid = uid % AID_USER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700191 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
192 return 0;
193 }
194 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000195 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700196 } else {
197 return 0;
198 }
199}
200
201int do_add_service(struct binder_state *bs,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000202 const uint16_t *s, size_t len,
Riley Spahn69154df2014-06-05 11:07:18 -0700203 uint32_t handle, uid_t uid, int allow_isolated,
204 pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700205{
206 struct svcinfo *si;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000207
Nick Kralevich652c4852014-07-12 16:34:01 -0700208 //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s, len), handle,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700209 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
210
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000211 if (!handle || (len == 0) || (len > 127))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700212 return -1;
213
Arve Hjønnevågeb304f02016-08-01 16:05:17 -0700214 if (!svc_can_register(s, len, spid, uid)) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000215 ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
Nick Kralevich652c4852014-07-12 16:34:01 -0700216 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700217 return -1;
218 }
219
220 si = find_svc(s, len);
221 if (si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000222 if (si->handle) {
223 ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
Nick Kralevich652c4852014-07-12 16:34:01 -0700224 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700225 svcinfo_death(bs, si);
226 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000227 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700228 } else {
229 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
230 if (!si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000231 ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
Nick Kralevich652c4852014-07-12 16:34:01 -0700232 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700233 return -1;
234 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000235 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700236 si->len = len;
237 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
238 si->name[len] = '\0';
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000239 si->death.func = (void*) svcinfo_death;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700240 si->death.ptr = si;
241 si->allow_isolated = allow_isolated;
242 si->next = svclist;
243 svclist = si;
244 }
245
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000246 binder_acquire(bs, handle);
247 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700248 return 0;
249}
250
251int svcmgr_handler(struct binder_state *bs,
Serban Constantinescubcf38882014-01-10 13:56:27 +0000252 struct binder_transaction_data *txn,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700253 struct binder_io *msg,
254 struct binder_io *reply)
255{
256 struct svcinfo *si;
257 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000258 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000259 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700260 uint32_t strict_policy;
261 int allow_isolated;
262
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000263 //ALOGI("target=%x code=%d pid=%d uid=%d\n",
264 // txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700265
Serban Constantinescubcf38882014-01-10 13:56:27 +0000266 if (txn->target.handle != svcmgr_handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700267 return -1;
268
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800269 if (txn->code == PING_TRANSACTION)
270 return 0;
271
Mike Lockwood94afecf2012-10-24 10:45:23 -0700272 // Equivalent to Parcel::enforceInterface(), reading the RPC
273 // header with the strict mode policy mask and the interface name.
274 // Note that we ignore the strict_policy and don't propagate it
275 // further (since we do no outbound RPCs anyway).
276 strict_policy = bio_get_uint32(msg);
277 s = bio_get_string16(msg, &len);
Nick Kralevich652c4852014-07-12 16:34:01 -0700278 if (s == NULL) {
279 return -1;
280 }
281
Mike Lockwood94afecf2012-10-24 10:45:23 -0700282 if ((len != (sizeof(svcmgr_id) / 2)) ||
283 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
Nick Kralevich652c4852014-07-12 16:34:01 -0700284 fprintf(stderr,"invalid id %s\n", str8(s, len));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700285 return -1;
286 }
287
Riley Spahn69154df2014-06-05 11:07:18 -0700288 if (sehandle && selinux_status_updated() > 0) {
289 struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
290 if (tmp_sehandle) {
291 selabel_close(sehandle);
292 sehandle = tmp_sehandle;
293 }
294 }
295
Mike Lockwood94afecf2012-10-24 10:45:23 -0700296 switch(txn->code) {
297 case SVC_MGR_GET_SERVICE:
298 case SVC_MGR_CHECK_SERVICE:
299 s = bio_get_string16(msg, &len);
Nick Kralevich652c4852014-07-12 16:34:01 -0700300 if (s == NULL) {
301 return -1;
302 }
Riley Spahn2a0e4092014-07-08 09:03:00 -0700303 handle = do_find_service(bs, s, len, txn->sender_euid, txn->sender_pid);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000304 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700305 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000306 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700307 return 0;
308
309 case SVC_MGR_ADD_SERVICE:
310 s = bio_get_string16(msg, &len);
Nick Kralevich652c4852014-07-12 16:34:01 -0700311 if (s == NULL) {
312 return -1;
313 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000314 handle = bio_get_ref(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700315 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
Riley Spahn69154df2014-06-05 11:07:18 -0700316 if (do_add_service(bs, s, len, handle, txn->sender_euid,
317 allow_isolated, txn->sender_pid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700318 return -1;
319 break;
320
321 case SVC_MGR_LIST_SERVICES: {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000322 uint32_t n = bio_get_uint32(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700323
Riley Spahn2a0e4092014-07-08 09:03:00 -0700324 if (!svc_can_list(txn->sender_pid)) {
325 ALOGE("list_service() uid=%d - PERMISSION DENIED\n",
326 txn->sender_euid);
327 return -1;
328 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700329 si = svclist;
330 while ((n-- > 0) && si)
331 si = si->next;
332 if (si) {
333 bio_put_string16(reply, si->name);
334 return 0;
335 }
336 return -1;
337 }
338 default:
339 ALOGE("unknown code %d\n", txn->code);
340 return -1;
341 }
342
343 bio_put_uint32(reply, 0);
344 return 0;
345}
346
Riley Spahn69154df2014-06-05 11:07:18 -0700347
348static int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
349{
350 snprintf(buf, len, "service=%s", !data ? "NULL" : (char *)data);
351 return 0;
352}
353
Mike Lockwood94afecf2012-10-24 10:45:23 -0700354int main(int argc, char **argv)
355{
356 struct binder_state *bs;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700357
358 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000359 if (!bs) {
360 ALOGE("failed to open binder driver\n");
361 return -1;
362 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700363
364 if (binder_become_context_manager(bs)) {
365 ALOGE("cannot become context manager (%s)\n", strerror(errno));
366 return -1;
367 }
368
Riley Spahn2a0e4092014-07-08 09:03:00 -0700369 selinux_enabled = is_selinux_enabled();
Riley Spahn69154df2014-06-05 11:07:18 -0700370 sehandle = selinux_android_service_context_handle();
371
Riley Spahn2a0e4092014-07-08 09:03:00 -0700372 if (selinux_enabled > 0) {
373 if (sehandle == NULL) {
374 ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n");
375 abort();
376 }
377
378 if (getcon(&service_manager_context) != 0) {
379 ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n");
380 abort();
381 }
382 }
383
Riley Spahn69154df2014-06-05 11:07:18 -0700384 union selinux_callback cb;
385 cb.func_audit = audit_callback;
386 selinux_set_callback(SELINUX_CB_AUDIT, cb);
387 cb.func_log = selinux_log_callback;
388 selinux_set_callback(SELINUX_CB_LOG, cb);
389
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000390 svcmgr_handle = BINDER_SERVICE_MANAGER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700391 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000392
Mike Lockwood94afecf2012-10-24 10:45:23 -0700393 return 0;
394}