blob: 44f4f6dac5d4bbd3c4f9108d264b57cd5ec4f5ce [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 Kralevich7d42a3c2014-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 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 Spahn69154df2014-06-05 11:07:18 -070055static struct selabel_handle* sehandle;
56
57static bool check_mac_perms(const char *name, pid_t spid)
58{
59 if (is_selinux_enabled() <= 0) {
60 return true;
61 }
62
63 bool allowed = false;
64
65 const char *class = "service_manager";
66 const char *perm = "add";
67
68 char *tctx = NULL;
69 char *sctx = NULL;
70
71 if (!sehandle) {
72 ALOGE("SELinux: Failed to find sehandle %s.\n", name);
73 return false;
74 }
75
76 if (getpidcon(spid, &sctx) < 0) {
77 ALOGE("SELinux: getpidcon failed to retrieve pid context.\n");
78 return false;
79 }
80
81 if (!sctx) {
82 ALOGE("SELinux: Failed to find sctx for %s.\n", name);
83 return false;
84 }
85
86 if (selabel_lookup(sehandle, &tctx, name, 1) != 0) {
87 ALOGE("SELinux: selabel_lookup failed to set tctx for %s.\n", name);
88 freecon(sctx);
89 return false;
90 }
91
92 if (!tctx) {
93 ALOGE("SELinux: Failed to find tctx for %s.\n", name);
94 freecon(sctx);
95 return false;
96 }
97
98 int result = selinux_check_access(sctx, tctx, class, perm, (void *) name);
99 allowed = (result == 0);
100
101 freecon(sctx);
102 freecon(tctx);
103 return allowed;
104}
105
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700106static int svc_can_register(uid_t uid, const uint16_t *name, size_t name_len, pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700107{
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700108 return check_mac_perms(str8(name, name_len), spid) ? 1 : 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700109}
110
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000111struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -0700112{
113 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000114 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700115 struct binder_death death;
116 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000117 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700118 uint16_t name[0];
119};
120
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000121struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700122
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000123struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700124{
125 struct svcinfo *si;
126
127 for (si = svclist; si; si = si->next) {
128 if ((len == si->len) &&
129 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
130 return si;
131 }
132 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000133 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700134}
135
136void svcinfo_death(struct binder_state *bs, void *ptr)
137{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000138 struct svcinfo *si = (struct svcinfo* ) ptr;
139
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700140 ALOGI("service '%s' died\n", str8(si->name, si->len));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000141 if (si->handle) {
142 binder_release(bs, si->handle);
143 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000144 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700145}
146
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000147uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700148 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000149 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700150};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700151
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000152
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000153uint32_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 -0700154{
155 struct svcinfo *si;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700156
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000157 si = find_svc(s, len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700158 //ALOGI("check_service('%s') handle = %x\n", str8(s, len), si ? si->handle : 0);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000159 if (si && si->handle) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700160 if (!si->allow_isolated) {
161 // If this service doesn't allow access from isolated processes,
162 // then check the uid to see if it is isolated.
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000163 uid_t appid = uid % AID_USER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700164 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
165 return 0;
166 }
167 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000168 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700169 } else {
170 return 0;
171 }
172}
173
174int do_add_service(struct binder_state *bs,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000175 const uint16_t *s, size_t len,
Riley Spahn69154df2014-06-05 11:07:18 -0700176 uint32_t handle, uid_t uid, int allow_isolated,
177 pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700178{
179 struct svcinfo *si;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000180
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700181 //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s, len), handle,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700182 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
183
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000184 if (!handle || (len == 0) || (len > 127))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700185 return -1;
186
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700187 if (!svc_can_register(uid, s, len, spid)) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000188 ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700189 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700190 return -1;
191 }
192
193 si = find_svc(s, len);
194 if (si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000195 if (si->handle) {
196 ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700197 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700198 svcinfo_death(bs, si);
199 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000200 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700201 } else {
202 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
203 if (!si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000204 ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700205 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700206 return -1;
207 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000208 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700209 si->len = len;
210 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
211 si->name[len] = '\0';
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000212 si->death.func = (void*) svcinfo_death;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700213 si->death.ptr = si;
214 si->allow_isolated = allow_isolated;
215 si->next = svclist;
216 svclist = si;
217 }
218
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000219 binder_acquire(bs, handle);
220 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700221 return 0;
222}
223
224int svcmgr_handler(struct binder_state *bs,
Serban Constantinescubcf38882014-01-10 13:56:27 +0000225 struct binder_transaction_data *txn,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700226 struct binder_io *msg,
227 struct binder_io *reply)
228{
229 struct svcinfo *si;
230 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000231 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000232 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700233 uint32_t strict_policy;
234 int allow_isolated;
235
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000236 //ALOGI("target=%x code=%d pid=%d uid=%d\n",
237 // txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700238
Serban Constantinescubcf38882014-01-10 13:56:27 +0000239 if (txn->target.handle != svcmgr_handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700240 return -1;
241
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800242 if (txn->code == PING_TRANSACTION)
243 return 0;
244
Mike Lockwood94afecf2012-10-24 10:45:23 -0700245 // Equivalent to Parcel::enforceInterface(), reading the RPC
246 // header with the strict mode policy mask and the interface name.
247 // Note that we ignore the strict_policy and don't propagate it
248 // further (since we do no outbound RPCs anyway).
249 strict_policy = bio_get_uint32(msg);
250 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700251 if (s == NULL) {
252 return -1;
253 }
254
Mike Lockwood94afecf2012-10-24 10:45:23 -0700255 if ((len != (sizeof(svcmgr_id) / 2)) ||
256 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700257 fprintf(stderr,"invalid id %s\n", str8(s, len));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700258 return -1;
259 }
260
Riley Spahn69154df2014-06-05 11:07:18 -0700261 if (sehandle && selinux_status_updated() > 0) {
262 struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
263 if (tmp_sehandle) {
264 selabel_close(sehandle);
265 sehandle = tmp_sehandle;
266 }
267 }
268
Mike Lockwood94afecf2012-10-24 10:45:23 -0700269 switch(txn->code) {
270 case SVC_MGR_GET_SERVICE:
271 case SVC_MGR_CHECK_SERVICE:
272 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700273 if (s == NULL) {
274 return -1;
275 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000276 handle = do_find_service(bs, s, len, txn->sender_euid);
277 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700278 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000279 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700280 return 0;
281
282 case SVC_MGR_ADD_SERVICE:
283 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700284 if (s == NULL) {
285 return -1;
286 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000287 handle = bio_get_ref(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700288 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
Riley Spahn69154df2014-06-05 11:07:18 -0700289 if (do_add_service(bs, s, len, handle, txn->sender_euid,
290 allow_isolated, txn->sender_pid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700291 return -1;
292 break;
293
294 case SVC_MGR_LIST_SERVICES: {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000295 uint32_t n = bio_get_uint32(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700296
297 si = svclist;
298 while ((n-- > 0) && si)
299 si = si->next;
300 if (si) {
301 bio_put_string16(reply, si->name);
302 return 0;
303 }
304 return -1;
305 }
306 default:
307 ALOGE("unknown code %d\n", txn->code);
308 return -1;
309 }
310
311 bio_put_uint32(reply, 0);
312 return 0;
313}
314
Riley Spahn69154df2014-06-05 11:07:18 -0700315
316static int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
317{
318 snprintf(buf, len, "service=%s", !data ? "NULL" : (char *)data);
319 return 0;
320}
321
Mike Lockwood94afecf2012-10-24 10:45:23 -0700322int main(int argc, char **argv)
323{
324 struct binder_state *bs;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700325
326 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000327 if (!bs) {
328 ALOGE("failed to open binder driver\n");
329 return -1;
330 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700331
332 if (binder_become_context_manager(bs)) {
333 ALOGE("cannot become context manager (%s)\n", strerror(errno));
334 return -1;
335 }
336
Riley Spahn69154df2014-06-05 11:07:18 -0700337 sehandle = selinux_android_service_context_handle();
338
339 union selinux_callback cb;
340 cb.func_audit = audit_callback;
341 selinux_set_callback(SELINUX_CB_AUDIT, cb);
342 cb.func_log = selinux_log_callback;
343 selinux_set_callback(SELINUX_CB_LOG, cb);
344
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000345 svcmgr_handle = BINDER_SERVICE_MANAGER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700346 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000347
Mike Lockwood94afecf2012-10-24 10:45:23 -0700348 return 0;
349}