blob: cacfe1426cbede0e336d3aaff6122731beee6aa1 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/* Copyright 2008 The Android Open Source Project
2 */
3
Mike Lockwood94afecf2012-10-24 10:45:23 -07004#include <errno.h>
5#include <fcntl.h>
Elliott Hughes0b41ad52015-04-03 16:51:18 -07006#include <inttypes.h>
Mark Salyzyn13df5f52015-04-01 07:52:12 -07007#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070010
11#include <private/android_filesystem_config.h>
12
Riley Spahn69154df2014-06-05 11:07:18 -070013#include <selinux/android.h>
Nick Kralevich7d42a3c2014-07-12 16:34:01 -070014#include <selinux/avc.h>
Riley Spahn69154df2014-06-05 11:07:18 -070015
Mike Lockwood94afecf2012-10-24 10:45:23 -070016#include "binder.h"
17
18#if 0
19#define ALOGI(x...) fprintf(stderr, "svcmgr: " x)
20#define ALOGE(x...) fprintf(stderr, "svcmgr: " x)
21#else
22#define LOG_TAG "ServiceManager"
23#include <cutils/log.h>
24#endif
25
Nick Kralevich7d42a3c2014-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 Kralevich7d42a3c2014-07-12 16:34:01 -070029 size_t max = 127;
Mike Lockwood94afecf2012-10-24 10:45:23 -070030 char *p = buf;
31
Nick Kralevich7d42a3c2014-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 Kralevich7d42a3c2014-07-12 16:34:01 -070037 while ((max > 0) && (*x != '\0')) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070038 *p++ = *x++;
Nick Kralevich7d42a3c2014-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 Spahnc67e6302014-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 Spahnc67e6302014-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 Spahnc67e6302014-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 Spahnc67e6302014-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 Spahnc67e6302014-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
Riley Spahnc67e6302014-07-08 09:03:00 -0700110static int svc_can_register(const uint16_t *name, size_t name_len, pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700111{
Riley Spahnc67e6302014-07-08 09:03:00 -0700112 const char *perm = "add";
113 return check_mac_perms_from_lookup(spid, perm, str8(name, name_len)) ? 1 : 0;
114}
115
116static int svc_can_list(pid_t spid)
117{
118 const char *perm = "list";
119 return check_mac_perms_from_getcon(spid, perm) ? 1 : 0;
120}
121
122static int svc_can_find(const uint16_t *name, size_t name_len, pid_t spid)
123{
124 const char *perm = "find";
125 return check_mac_perms_from_lookup(spid, perm, str8(name, name_len)) ? 1 : 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700126}
127
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000128struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -0700129{
130 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000131 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700132 struct binder_death death;
133 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000134 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700135 uint16_t name[0];
136};
137
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000138struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700139
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000140struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700141{
142 struct svcinfo *si;
143
144 for (si = svclist; si; si = si->next) {
145 if ((len == si->len) &&
146 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
147 return si;
148 }
149 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000150 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700151}
152
153void svcinfo_death(struct binder_state *bs, void *ptr)
154{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000155 struct svcinfo *si = (struct svcinfo* ) ptr;
156
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700157 ALOGI("service '%s' died\n", str8(si->name, si->len));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000158 if (si->handle) {
159 binder_release(bs, si->handle);
160 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000161 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700162}
163
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000164uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700165 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000166 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700167};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700168
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000169
Riley Spahnc67e6302014-07-08 09:03:00 -0700170uint32_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 -0700171{
Nick Kralevichb27bbd12015-03-05 10:58:40 -0800172 struct svcinfo *si = find_svc(s, len);
173
174 if (!si || !si->handle) {
175 return 0;
176 }
177
178 if (!si->allow_isolated) {
179 // If this service doesn't allow access from isolated processes,
180 // then check the uid to see if it is isolated.
181 uid_t appid = uid % AID_USER;
182 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
183 return 0;
184 }
185 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700186
Riley Spahnc67e6302014-07-08 09:03:00 -0700187 if (!svc_can_find(s, len, spid)) {
Riley Spahnc67e6302014-07-08 09:03:00 -0700188 return 0;
189 }
Nick Kralevichb27bbd12015-03-05 10:58:40 -0800190
191 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700192}
193
194int do_add_service(struct binder_state *bs,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000195 const uint16_t *s, size_t len,
Riley Spahn69154df2014-06-05 11:07:18 -0700196 uint32_t handle, uid_t uid, int allow_isolated,
197 pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700198{
199 struct svcinfo *si;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000200
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700201 //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s, len), handle,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700202 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
203
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000204 if (!handle || (len == 0) || (len > 127))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700205 return -1;
206
Riley Spahnc67e6302014-07-08 09:03:00 -0700207 if (!svc_can_register(s, len, spid)) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000208 ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700209 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700210 return -1;
211 }
212
213 si = find_svc(s, len);
214 if (si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000215 if (si->handle) {
216 ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700217 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700218 svcinfo_death(bs, si);
219 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000220 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700221 } else {
222 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
223 if (!si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000224 ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700225 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700226 return -1;
227 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000228 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700229 si->len = len;
230 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
231 si->name[len] = '\0';
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000232 si->death.func = (void*) svcinfo_death;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700233 si->death.ptr = si;
234 si->allow_isolated = allow_isolated;
235 si->next = svclist;
236 svclist = si;
237 }
238
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000239 binder_acquire(bs, handle);
240 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700241 return 0;
242}
243
244int svcmgr_handler(struct binder_state *bs,
Serban Constantinescubcf38882014-01-10 13:56:27 +0000245 struct binder_transaction_data *txn,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700246 struct binder_io *msg,
247 struct binder_io *reply)
248{
249 struct svcinfo *si;
250 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000251 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000252 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700253 uint32_t strict_policy;
254 int allow_isolated;
255
Elliott Hughes0b41ad52015-04-03 16:51:18 -0700256 //ALOGI("target=%p code=%d pid=%d uid=%d\n",
257 // (void*) txn->target.ptr, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700258
Elliott Hughes0b41ad52015-04-03 16:51:18 -0700259 if (txn->target.ptr != BINDER_SERVICE_MANAGER)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700260 return -1;
261
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800262 if (txn->code == PING_TRANSACTION)
263 return 0;
264
Mike Lockwood94afecf2012-10-24 10:45:23 -0700265 // Equivalent to Parcel::enforceInterface(), reading the RPC
266 // header with the strict mode policy mask and the interface name.
267 // Note that we ignore the strict_policy and don't propagate it
268 // further (since we do no outbound RPCs anyway).
269 strict_policy = bio_get_uint32(msg);
270 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700271 if (s == NULL) {
272 return -1;
273 }
274
Mike Lockwood94afecf2012-10-24 10:45:23 -0700275 if ((len != (sizeof(svcmgr_id) / 2)) ||
276 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700277 fprintf(stderr,"invalid id %s\n", str8(s, len));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700278 return -1;
279 }
280
Riley Spahn69154df2014-06-05 11:07:18 -0700281 if (sehandle && selinux_status_updated() > 0) {
282 struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
283 if (tmp_sehandle) {
284 selabel_close(sehandle);
285 sehandle = tmp_sehandle;
286 }
287 }
288
Mike Lockwood94afecf2012-10-24 10:45:23 -0700289 switch(txn->code) {
290 case SVC_MGR_GET_SERVICE:
291 case SVC_MGR_CHECK_SERVICE:
292 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700293 if (s == NULL) {
294 return -1;
295 }
Riley Spahnc67e6302014-07-08 09:03:00 -0700296 handle = do_find_service(bs, s, len, txn->sender_euid, txn->sender_pid);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000297 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700298 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000299 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700300 return 0;
301
302 case SVC_MGR_ADD_SERVICE:
303 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700304 if (s == NULL) {
305 return -1;
306 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000307 handle = bio_get_ref(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700308 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
Riley Spahn69154df2014-06-05 11:07:18 -0700309 if (do_add_service(bs, s, len, handle, txn->sender_euid,
310 allow_isolated, txn->sender_pid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700311 return -1;
312 break;
313
314 case SVC_MGR_LIST_SERVICES: {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000315 uint32_t n = bio_get_uint32(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700316
Riley Spahnc67e6302014-07-08 09:03:00 -0700317 if (!svc_can_list(txn->sender_pid)) {
318 ALOGE("list_service() uid=%d - PERMISSION DENIED\n",
319 txn->sender_euid);
320 return -1;
321 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700322 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 Spahnc67e6302014-07-08 09:03:00 -0700362 selinux_enabled = is_selinux_enabled();
Riley Spahn69154df2014-06-05 11:07:18 -0700363 sehandle = selinux_android_service_context_handle();
364
Riley Spahnc67e6302014-07-08 09:03:00 -0700365 if (selinux_enabled > 0) {
366 if (sehandle == NULL) {
367 ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n");
368 abort();
369 }
370
371 if (getcon(&service_manager_context) != 0) {
372 ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n");
373 abort();
374 }
375 }
376
Riley Spahn69154df2014-06-05 11:07:18 -0700377 union selinux_callback cb;
378 cb.func_audit = audit_callback;
379 selinux_set_callback(SELINUX_CB_AUDIT, cb);
380 cb.func_log = selinux_log_callback;
381 selinux_set_callback(SELINUX_CB_LOG, cb);
382
Mike Lockwood94afecf2012-10-24 10:45:23 -0700383 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000384
Mike Lockwood94afecf2012-10-24 10:45:23 -0700385 return 0;
386}