blob: 21fdff0f0f567e81d3a575ac6513de9a5d1c3b21 [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
William Roberts8fb0f922015-10-01 22:02:15 -070026struct audit_data {
27 pid_t pid;
28 uid_t uid;
29 const char *name;
30};
31
Nick Kralevich7d42a3c2014-07-12 16:34:01 -070032const char *str8(const uint16_t *x, size_t x_len)
Mike Lockwood94afecf2012-10-24 10:45:23 -070033{
34 static char buf[128];
Nick Kralevich7d42a3c2014-07-12 16:34:01 -070035 size_t max = 127;
Mike Lockwood94afecf2012-10-24 10:45:23 -070036 char *p = buf;
37
Nick Kralevich7d42a3c2014-07-12 16:34:01 -070038 if (x_len < max) {
39 max = x_len;
40 }
41
Mike Lockwood94afecf2012-10-24 10:45:23 -070042 if (x) {
Nick Kralevich7d42a3c2014-07-12 16:34:01 -070043 while ((max > 0) && (*x != '\0')) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070044 *p++ = *x++;
Nick Kralevich7d42a3c2014-07-12 16:34:01 -070045 max--;
Mike Lockwood94afecf2012-10-24 10:45:23 -070046 }
47 }
48 *p++ = 0;
49 return buf;
50}
51
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000052int str16eq(const uint16_t *a, const char *b)
Mike Lockwood94afecf2012-10-24 10:45:23 -070053{
54 while (*a && *b)
55 if (*a++ != *b++) return 0;
56 if (*a || *b)
57 return 0;
58 return 1;
59}
60
Riley Spahnc67e6302014-07-08 09:03:00 -070061static int selinux_enabled;
62static char *service_manager_context;
Riley Spahn69154df2014-06-05 11:07:18 -070063static struct selabel_handle* sehandle;
64
William Roberts8fb0f922015-10-01 22:02:15 -070065static bool check_mac_perms(pid_t spid, uid_t uid, const char *tctx, const char *perm, const char *name)
Riley Spahn69154df2014-06-05 11:07:18 -070066{
Riley Spahn69154df2014-06-05 11:07:18 -070067 char *sctx = NULL;
Riley Spahnc67e6302014-07-08 09:03:00 -070068 const char *class = "service_manager";
69 bool allowed;
William Roberts8fb0f922015-10-01 22:02:15 -070070 struct audit_data ad;
Riley Spahn69154df2014-06-05 11:07:18 -070071
72 if (getpidcon(spid, &sctx) < 0) {
Riley Spahnc67e6302014-07-08 09:03:00 -070073 ALOGE("SELinux: getpidcon(pid=%d) failed to retrieve pid context.\n", spid);
Riley Spahn69154df2014-06-05 11:07:18 -070074 return false;
75 }
76
William Roberts8fb0f922015-10-01 22:02:15 -070077 ad.pid = spid;
78 ad.uid = uid;
79 ad.name = name;
80
81 int result = selinux_check_access(sctx, tctx, class, perm, (void *) &ad);
Riley Spahn69154df2014-06-05 11:07:18 -070082 allowed = (result == 0);
83
84 freecon(sctx);
Riley Spahnc67e6302014-07-08 09:03:00 -070085 return allowed;
86}
87
William Roberts8fb0f922015-10-01 22:02:15 -070088static bool check_mac_perms_from_getcon(pid_t spid, uid_t uid, const char *perm)
Riley Spahnc67e6302014-07-08 09:03:00 -070089{
90 if (selinux_enabled <= 0) {
91 return true;
92 }
93
William Roberts8fb0f922015-10-01 22:02:15 -070094 return check_mac_perms(spid, uid, service_manager_context, perm, NULL);
Riley Spahnc67e6302014-07-08 09:03:00 -070095}
96
William Roberts8fb0f922015-10-01 22:02:15 -070097static bool check_mac_perms_from_lookup(pid_t spid, uid_t uid, const char *perm, const char *name)
Riley Spahnc67e6302014-07-08 09:03:00 -070098{
99 bool allowed;
100 char *tctx = NULL;
101
102 if (selinux_enabled <= 0) {
103 return true;
104 }
105
106 if (!sehandle) {
107 ALOGE("SELinux: Failed to find sehandle. Aborting service_manager.\n");
108 abort();
109 }
110
111 if (selabel_lookup(sehandle, &tctx, name, 0) != 0) {
112 ALOGE("SELinux: No match for %s in service_contexts.\n", name);
113 return false;
114 }
115
William Roberts8fb0f922015-10-01 22:02:15 -0700116 allowed = check_mac_perms(spid, uid, tctx, perm, name);
Riley Spahn69154df2014-06-05 11:07:18 -0700117 freecon(tctx);
118 return allowed;
119}
120
William Roberts8fb0f922015-10-01 22:02:15 -0700121static 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 -0700122{
Riley Spahnc67e6302014-07-08 09:03:00 -0700123 const char *perm = "add";
William Roberts8fb0f922015-10-01 22:02:15 -0700124 return check_mac_perms_from_lookup(spid, uid, perm, str8(name, name_len)) ? 1 : 0;
Riley Spahnc67e6302014-07-08 09:03:00 -0700125}
126
William Roberts8fb0f922015-10-01 22:02:15 -0700127static int svc_can_list(pid_t spid, uid_t uid)
Riley Spahnc67e6302014-07-08 09:03:00 -0700128{
129 const char *perm = "list";
William Roberts8fb0f922015-10-01 22:02:15 -0700130 return check_mac_perms_from_getcon(spid, uid, perm) ? 1 : 0;
Riley Spahnc67e6302014-07-08 09:03:00 -0700131}
132
William Roberts8fb0f922015-10-01 22:02:15 -0700133static int svc_can_find(const uint16_t *name, size_t name_len, pid_t spid, uid_t uid)
Riley Spahnc67e6302014-07-08 09:03:00 -0700134{
135 const char *perm = "find";
William Roberts8fb0f922015-10-01 22:02:15 -0700136 return check_mac_perms_from_lookup(spid, uid, perm, str8(name, name_len)) ? 1 : 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700137}
138
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000139struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -0700140{
141 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000142 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700143 struct binder_death death;
144 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000145 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700146 uint16_t name[0];
147};
148
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000149struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700150
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000151struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700152{
153 struct svcinfo *si;
154
155 for (si = svclist; si; si = si->next) {
156 if ((len == si->len) &&
157 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
158 return si;
159 }
160 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000161 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700162}
163
164void svcinfo_death(struct binder_state *bs, void *ptr)
165{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000166 struct svcinfo *si = (struct svcinfo* ) ptr;
167
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700168 ALOGI("service '%s' died\n", str8(si->name, si->len));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000169 if (si->handle) {
170 binder_release(bs, si->handle);
171 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000172 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700173}
174
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000175uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700176 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000177 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700178};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700179
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000180
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000181uint32_t do_find_service(const uint16_t *s, size_t len, uid_t uid, pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700182{
Nick Kralevichb27bbd12015-03-05 10:58:40 -0800183 struct svcinfo *si = find_svc(s, len);
184
185 if (!si || !si->handle) {
186 return 0;
187 }
188
189 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.
192 uid_t appid = uid % AID_USER;
193 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
194 return 0;
195 }
196 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700197
William Roberts8fb0f922015-10-01 22:02:15 -0700198 if (!svc_can_find(s, len, spid, uid)) {
Riley Spahnc67e6302014-07-08 09:03:00 -0700199 return 0;
200 }
Nick Kralevichb27bbd12015-03-05 10:58:40 -0800201
202 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700203}
204
205int do_add_service(struct binder_state *bs,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000206 const uint16_t *s, size_t len,
Riley Spahn69154df2014-06-05 11:07:18 -0700207 uint32_t handle, uid_t uid, int allow_isolated,
208 pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700209{
210 struct svcinfo *si;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000211
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700212 //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s, len), handle,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700213 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
214
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000215 if (!handle || (len == 0) || (len > 127))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700216 return -1;
217
William Roberts8fb0f922015-10-01 22:02:15 -0700218 if (!svc_can_register(s, len, spid, uid)) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000219 ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700220 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700221 return -1;
222 }
223
224 si = find_svc(s, len);
225 if (si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000226 if (si->handle) {
227 ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700228 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700229 svcinfo_death(bs, si);
230 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000231 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700232 } else {
233 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
234 if (!si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000235 ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700236 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700237 return -1;
238 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000239 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700240 si->len = len;
241 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
242 si->name[len] = '\0';
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000243 si->death.func = (void*) svcinfo_death;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700244 si->death.ptr = si;
245 si->allow_isolated = allow_isolated;
246 si->next = svclist;
247 svclist = si;
248 }
249
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000250 binder_acquire(bs, handle);
251 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700252 return 0;
253}
254
255int svcmgr_handler(struct binder_state *bs,
Serban Constantinescubcf38882014-01-10 13:56:27 +0000256 struct binder_transaction_data *txn,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700257 struct binder_io *msg,
258 struct binder_io *reply)
259{
260 struct svcinfo *si;
261 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000262 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000263 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700264 uint32_t strict_policy;
265 int allow_isolated;
266
Elliott Hughes0b41ad52015-04-03 16:51:18 -0700267 //ALOGI("target=%p code=%d pid=%d uid=%d\n",
268 // (void*) txn->target.ptr, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700269
Elliott Hughes0b41ad52015-04-03 16:51:18 -0700270 if (txn->target.ptr != BINDER_SERVICE_MANAGER)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700271 return -1;
272
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800273 if (txn->code == PING_TRANSACTION)
274 return 0;
275
Mike Lockwood94afecf2012-10-24 10:45:23 -0700276 // Equivalent to Parcel::enforceInterface(), reading the RPC
277 // header with the strict mode policy mask and the interface name.
278 // Note that we ignore the strict_policy and don't propagate it
279 // further (since we do no outbound RPCs anyway).
280 strict_policy = bio_get_uint32(msg);
281 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700282 if (s == NULL) {
283 return -1;
284 }
285
Mike Lockwood94afecf2012-10-24 10:45:23 -0700286 if ((len != (sizeof(svcmgr_id) / 2)) ||
287 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700288 fprintf(stderr,"invalid id %s\n", str8(s, len));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700289 return -1;
290 }
291
Riley Spahn69154df2014-06-05 11:07:18 -0700292 if (sehandle && selinux_status_updated() > 0) {
293 struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
294 if (tmp_sehandle) {
295 selabel_close(sehandle);
296 sehandle = tmp_sehandle;
297 }
298 }
299
Mike Lockwood94afecf2012-10-24 10:45:23 -0700300 switch(txn->code) {
301 case SVC_MGR_GET_SERVICE:
302 case SVC_MGR_CHECK_SERVICE:
303 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700304 if (s == NULL) {
305 return -1;
306 }
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000307 handle = do_find_service(s, len, txn->sender_euid, txn->sender_pid);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000308 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700309 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000310 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700311 return 0;
312
313 case SVC_MGR_ADD_SERVICE:
314 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700315 if (s == NULL) {
316 return -1;
317 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000318 handle = bio_get_ref(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700319 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
Riley Spahn69154df2014-06-05 11:07:18 -0700320 if (do_add_service(bs, s, len, handle, txn->sender_euid,
321 allow_isolated, txn->sender_pid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700322 return -1;
323 break;
324
325 case SVC_MGR_LIST_SERVICES: {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000326 uint32_t n = bio_get_uint32(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700327
William Roberts8fb0f922015-10-01 22:02:15 -0700328 if (!svc_can_list(txn->sender_pid, txn->sender_euid)) {
Riley Spahnc67e6302014-07-08 09:03:00 -0700329 ALOGE("list_service() uid=%d - PERMISSION DENIED\n",
330 txn->sender_euid);
331 return -1;
332 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700333 si = svclist;
334 while ((n-- > 0) && si)
335 si = si->next;
336 if (si) {
337 bio_put_string16(reply, si->name);
338 return 0;
339 }
340 return -1;
341 }
342 default:
343 ALOGE("unknown code %d\n", txn->code);
344 return -1;
345 }
346
347 bio_put_uint32(reply, 0);
348 return 0;
349}
350
Riley Spahn69154df2014-06-05 11:07:18 -0700351
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000352static int audit_callback(void *data, __unused security_class_t cls, char *buf, size_t len)
Riley Spahn69154df2014-06-05 11:07:18 -0700353{
William Roberts8fb0f922015-10-01 22:02:15 -0700354 struct audit_data *ad = (struct audit_data *)data;
355
356 if (!ad || !ad->name) {
357 ALOGE("No service manager audit data");
358 return 0;
359 }
360
361 snprintf(buf, len, "service=%s pid=%d uid=%d", ad->name, ad->pid, ad->uid);
Riley Spahn69154df2014-06-05 11:07:18 -0700362 return 0;
363}
364
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000365int main()
Mike Lockwood94afecf2012-10-24 10:45:23 -0700366{
367 struct binder_state *bs;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700368
369 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000370 if (!bs) {
371 ALOGE("failed to open binder driver\n");
372 return -1;
373 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700374
375 if (binder_become_context_manager(bs)) {
376 ALOGE("cannot become context manager (%s)\n", strerror(errno));
377 return -1;
378 }
379
Riley Spahnc67e6302014-07-08 09:03:00 -0700380 selinux_enabled = is_selinux_enabled();
Riley Spahn69154df2014-06-05 11:07:18 -0700381 sehandle = selinux_android_service_context_handle();
Stephen Smalleybea07462015-06-03 09:25:37 -0400382 selinux_status_open(true);
Riley Spahn69154df2014-06-05 11:07:18 -0700383
Riley Spahnc67e6302014-07-08 09:03:00 -0700384 if (selinux_enabled > 0) {
385 if (sehandle == NULL) {
386 ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n");
387 abort();
388 }
389
390 if (getcon(&service_manager_context) != 0) {
391 ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n");
392 abort();
393 }
394 }
395
Riley Spahn69154df2014-06-05 11:07:18 -0700396 union selinux_callback cb;
397 cb.func_audit = audit_callback;
398 selinux_set_callback(SELINUX_CB_AUDIT, cb);
399 cb.func_log = selinux_log_callback;
400 selinux_set_callback(SELINUX_CB_LOG, cb);
401
Mike Lockwood94afecf2012-10-24 10:45:23 -0700402 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000403
Mike Lockwood94afecf2012-10-24 10:45:23 -0700404 return 0;
405}