blob: e20c13fa0bd1f8e79cfbba44daed9a42cc5dcdd1 [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
Arve Hjønnevåge6bbe692016-08-18 15:42:35 -07009#include <cutils/multiuser.h>
10
Mike Lockwood94afecf2012-10-24 10:45:23 -070011#include <private/android_filesystem_config.h>
12
Riley Spahn69154df2014-06-05 11:07:18 -070013#include <selinux/android.h>
Nick Kralevich652c4852014-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
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000026uint32_t svcmgr_handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -070027
Nick Kralevich652c4852014-07-12 16:34:01 -070028const char *str8(const uint16_t *x, size_t x_len)
Mike Lockwood94afecf2012-10-24 10:45:23 -070029{
30 static char buf[128];
Nick Kralevich652c4852014-07-12 16:34:01 -070031 size_t max = 127;
Mike Lockwood94afecf2012-10-24 10:45:23 -070032 char *p = buf;
33
Nick Kralevich652c4852014-07-12 16:34:01 -070034 if (x_len < max) {
35 max = x_len;
36 }
37
Mike Lockwood94afecf2012-10-24 10:45:23 -070038 if (x) {
Nick Kralevich652c4852014-07-12 16:34:01 -070039 while ((max > 0) && (*x != '\0')) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070040 *p++ = *x++;
Nick Kralevich652c4852014-07-12 16:34:01 -070041 max--;
Mike Lockwood94afecf2012-10-24 10:45:23 -070042 }
43 }
44 *p++ = 0;
45 return buf;
46}
47
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000048int str16eq(const uint16_t *a, const char *b)
Mike Lockwood94afecf2012-10-24 10:45:23 -070049{
50 while (*a && *b)
51 if (*a++ != *b++) return 0;
52 if (*a || *b)
53 return 0;
54 return 1;
55}
56
Riley Spahn2a0e4092014-07-08 09:03:00 -070057static int selinux_enabled;
58static char *service_manager_context;
Riley Spahn69154df2014-06-05 11:07:18 -070059static struct selabel_handle* sehandle;
60
Riley Spahn2a0e4092014-07-08 09:03:00 -070061static bool check_mac_perms(pid_t spid, const char *tctx, const char *perm, const char *name)
Riley Spahn69154df2014-06-05 11:07:18 -070062{
Riley Spahn69154df2014-06-05 11:07:18 -070063 char *sctx = NULL;
Riley Spahn2a0e4092014-07-08 09:03:00 -070064 const char *class = "service_manager";
65 bool allowed;
Riley Spahn69154df2014-06-05 11:07:18 -070066
67 if (getpidcon(spid, &sctx) < 0) {
Riley Spahn2a0e4092014-07-08 09:03:00 -070068 ALOGE("SELinux: getpidcon(pid=%d) failed to retrieve pid context.\n", spid);
Riley Spahn69154df2014-06-05 11:07:18 -070069 return false;
70 }
71
72 int result = selinux_check_access(sctx, tctx, class, perm, (void *) name);
73 allowed = (result == 0);
74
75 freecon(sctx);
Riley Spahn2a0e4092014-07-08 09:03:00 -070076 return allowed;
77}
78
79static bool check_mac_perms_from_getcon(pid_t spid, const char *perm)
80{
81 if (selinux_enabled <= 0) {
82 return true;
83 }
84
85 return check_mac_perms(spid, service_manager_context, perm, NULL);
86}
87
88static bool check_mac_perms_from_lookup(pid_t spid, const char *perm, const char *name)
89{
90 bool allowed;
91 char *tctx = NULL;
92
93 if (selinux_enabled <= 0) {
94 return true;
95 }
96
97 if (!sehandle) {
98 ALOGE("SELinux: Failed to find sehandle. Aborting service_manager.\n");
99 abort();
100 }
101
102 if (selabel_lookup(sehandle, &tctx, name, 0) != 0) {
103 ALOGE("SELinux: No match for %s in service_contexts.\n", name);
104 return false;
105 }
106
107 allowed = check_mac_perms(spid, tctx, perm, name);
Riley Spahn69154df2014-06-05 11:07:18 -0700108 freecon(tctx);
109 return allowed;
110}
111
Arve Hjønnevågeb304f02016-08-01 16:05:17 -0700112static 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 -0700113{
Riley Spahn2a0e4092014-07-08 09:03:00 -0700114 const char *perm = "add";
Arve Hjønnevågeb304f02016-08-01 16:05:17 -0700115
Arve Hjønnevåge6bbe692016-08-18 15:42:35 -0700116 if (multiuser_get_app_id(uid) >= AID_APP) {
Arve Hjønnevågeb304f02016-08-01 16:05:17 -0700117 return 0; /* Don't allow apps to register services */
118 }
119
Riley Spahn2a0e4092014-07-08 09:03:00 -0700120 return check_mac_perms_from_lookup(spid, perm, str8(name, name_len)) ? 1 : 0;
121}
122
123static int svc_can_list(pid_t spid)
124{
125 const char *perm = "list";
126 return check_mac_perms_from_getcon(spid, perm) ? 1 : 0;
127}
128
129static int svc_can_find(const uint16_t *name, size_t name_len, pid_t spid)
130{
131 const char *perm = "find";
132 return check_mac_perms_from_lookup(spid, perm, str8(name, name_len)) ? 1 : 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700133}
134
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000135struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -0700136{
137 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000138 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700139 struct binder_death death;
140 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000141 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700142 uint16_t name[0];
143};
144
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000145struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700146
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000147struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700148{
149 struct svcinfo *si;
150
151 for (si = svclist; si; si = si->next) {
152 if ((len == si->len) &&
153 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
154 return si;
155 }
156 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000157 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700158}
159
160void svcinfo_death(struct binder_state *bs, void *ptr)
161{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000162 struct svcinfo *si = (struct svcinfo* ) ptr;
163
Nick Kralevich652c4852014-07-12 16:34:01 -0700164 ALOGI("service '%s' died\n", str8(si->name, si->len));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000165 if (si->handle) {
166 binder_release(bs, si->handle);
167 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000168 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700169}
170
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000171uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700172 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000173 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700174};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700175
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000176
Riley Spahn2a0e4092014-07-08 09:03:00 -0700177uint32_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 -0700178{
179 struct svcinfo *si;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700180
Riley Spahn2a0e4092014-07-08 09:03:00 -0700181 if (!svc_can_find(s, len, spid)) {
182 ALOGE("find_service('%s') uid=%d - PERMISSION DENIED\n",
183 str8(s, len), uid);
184 return 0;
185 }
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000186 si = find_svc(s, len);
Nick Kralevich652c4852014-07-12 16:34:01 -0700187 //ALOGI("check_service('%s') handle = %x\n", str8(s, len), si ? si->handle : 0);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000188 if (si && si->handle) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700189 if (!si->allow_isolated) {
190 // If this service doesn't allow access from isolated processes,
191 // then check the uid to see if it is isolated.
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000192 uid_t appid = uid % AID_USER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700193 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
194 return 0;
195 }
196 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000197 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700198 } else {
199 return 0;
200 }
201}
202
203int do_add_service(struct binder_state *bs,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000204 const uint16_t *s, size_t len,
Riley Spahn69154df2014-06-05 11:07:18 -0700205 uint32_t handle, uid_t uid, int allow_isolated,
206 pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700207{
208 struct svcinfo *si;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000209
Nick Kralevich652c4852014-07-12 16:34:01 -0700210 //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s, len), handle,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700211 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
212
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000213 if (!handle || (len == 0) || (len > 127))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700214 return -1;
215
Arve Hjønnevågeb304f02016-08-01 16:05:17 -0700216 if (!svc_can_register(s, len, spid, uid)) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000217 ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
Nick Kralevich652c4852014-07-12 16:34:01 -0700218 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700219 return -1;
220 }
221
222 si = find_svc(s, len);
223 if (si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000224 if (si->handle) {
225 ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
Nick Kralevich652c4852014-07-12 16:34:01 -0700226 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700227 svcinfo_death(bs, si);
228 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000229 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700230 } else {
231 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
232 if (!si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000233 ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
Nick Kralevich652c4852014-07-12 16:34:01 -0700234 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700235 return -1;
236 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000237 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700238 si->len = len;
239 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
240 si->name[len] = '\0';
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000241 si->death.func = (void*) svcinfo_death;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700242 si->death.ptr = si;
243 si->allow_isolated = allow_isolated;
244 si->next = svclist;
245 svclist = si;
246 }
247
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000248 binder_acquire(bs, handle);
249 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700250 return 0;
251}
252
253int svcmgr_handler(struct binder_state *bs,
Serban Constantinescubcf38882014-01-10 13:56:27 +0000254 struct binder_transaction_data *txn,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700255 struct binder_io *msg,
256 struct binder_io *reply)
257{
258 struct svcinfo *si;
259 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000260 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000261 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700262 uint32_t strict_policy;
263 int allow_isolated;
264
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000265 //ALOGI("target=%x code=%d pid=%d uid=%d\n",
266 // txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700267
Serban Constantinescubcf38882014-01-10 13:56:27 +0000268 if (txn->target.handle != svcmgr_handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700269 return -1;
270
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800271 if (txn->code == PING_TRANSACTION)
272 return 0;
273
Mike Lockwood94afecf2012-10-24 10:45:23 -0700274 // Equivalent to Parcel::enforceInterface(), reading the RPC
275 // header with the strict mode policy mask and the interface name.
276 // Note that we ignore the strict_policy and don't propagate it
277 // further (since we do no outbound RPCs anyway).
278 strict_policy = bio_get_uint32(msg);
279 s = bio_get_string16(msg, &len);
Nick Kralevich652c4852014-07-12 16:34:01 -0700280 if (s == NULL) {
281 return -1;
282 }
283
Mike Lockwood94afecf2012-10-24 10:45:23 -0700284 if ((len != (sizeof(svcmgr_id) / 2)) ||
285 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
Nick Kralevich652c4852014-07-12 16:34:01 -0700286 fprintf(stderr,"invalid id %s\n", str8(s, len));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700287 return -1;
288 }
289
Riley Spahn69154df2014-06-05 11:07:18 -0700290 if (sehandle && selinux_status_updated() > 0) {
291 struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
292 if (tmp_sehandle) {
293 selabel_close(sehandle);
294 sehandle = tmp_sehandle;
295 }
296 }
297
Mike Lockwood94afecf2012-10-24 10:45:23 -0700298 switch(txn->code) {
299 case SVC_MGR_GET_SERVICE:
300 case SVC_MGR_CHECK_SERVICE:
301 s = bio_get_string16(msg, &len);
Nick Kralevich652c4852014-07-12 16:34:01 -0700302 if (s == NULL) {
303 return -1;
304 }
Riley Spahn2a0e4092014-07-08 09:03:00 -0700305 handle = do_find_service(bs, s, len, txn->sender_euid, txn->sender_pid);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000306 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700307 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000308 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700309 return 0;
310
311 case SVC_MGR_ADD_SERVICE:
312 s = bio_get_string16(msg, &len);
Nick Kralevich652c4852014-07-12 16:34:01 -0700313 if (s == NULL) {
314 return -1;
315 }
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
Riley Spahn2a0e4092014-07-08 09:03:00 -0700326 if (!svc_can_list(txn->sender_pid)) {
327 ALOGE("list_service() uid=%d - PERMISSION DENIED\n",
328 txn->sender_euid);
329 return -1;
330 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700331 si = svclist;
332 while ((n-- > 0) && si)
333 si = si->next;
334 if (si) {
335 bio_put_string16(reply, si->name);
336 return 0;
337 }
338 return -1;
339 }
340 default:
341 ALOGE("unknown code %d\n", txn->code);
342 return -1;
343 }
344
345 bio_put_uint32(reply, 0);
346 return 0;
347}
348
Riley Spahn69154df2014-06-05 11:07:18 -0700349
350static int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
351{
352 snprintf(buf, len, "service=%s", !data ? "NULL" : (char *)data);
353 return 0;
354}
355
Mike Lockwood94afecf2012-10-24 10:45:23 -0700356int main(int argc, char **argv)
357{
358 struct binder_state *bs;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700359
360 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000361 if (!bs) {
362 ALOGE("failed to open binder driver\n");
363 return -1;
364 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700365
366 if (binder_become_context_manager(bs)) {
367 ALOGE("cannot become context manager (%s)\n", strerror(errno));
368 return -1;
369 }
370
Riley Spahn2a0e4092014-07-08 09:03:00 -0700371 selinux_enabled = is_selinux_enabled();
Riley Spahn69154df2014-06-05 11:07:18 -0700372 sehandle = selinux_android_service_context_handle();
373
Riley Spahn2a0e4092014-07-08 09:03:00 -0700374 if (selinux_enabled > 0) {
375 if (sehandle == NULL) {
376 ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n");
377 abort();
378 }
379
380 if (getcon(&service_manager_context) != 0) {
381 ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n");
382 abort();
383 }
384 }
385
Riley Spahn69154df2014-06-05 11:07:18 -0700386 union selinux_callback cb;
387 cb.func_audit = audit_callback;
388 selinux_set_callback(SELINUX_CB_AUDIT, cb);
389 cb.func_log = selinux_log_callback;
390 selinux_set_callback(SELINUX_CB_LOG, cb);
391
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000392 svcmgr_handle = BINDER_SERVICE_MANAGER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700393 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000394
Mike Lockwood94afecf2012-10-24 10:45:23 -0700395 return 0;
396}