blob: 8a8e6885cc1016fc7c44850bc3ca3cce191b200d [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";
Arve Hjønnevåg2b74d2c2016-08-01 16:05:17 -0700124
125 if (uid >= AID_APP) {
126 return 0; /* Don't allow apps to register services */
127 }
128
William Roberts8fb0f922015-10-01 22:02:15 -0700129 return check_mac_perms_from_lookup(spid, uid, perm, str8(name, name_len)) ? 1 : 0;
Riley Spahnc67e6302014-07-08 09:03:00 -0700130}
131
William Roberts8fb0f922015-10-01 22:02:15 -0700132static int svc_can_list(pid_t spid, uid_t uid)
Riley Spahnc67e6302014-07-08 09:03:00 -0700133{
134 const char *perm = "list";
William Roberts8fb0f922015-10-01 22:02:15 -0700135 return check_mac_perms_from_getcon(spid, uid, perm) ? 1 : 0;
Riley Spahnc67e6302014-07-08 09:03:00 -0700136}
137
William Roberts8fb0f922015-10-01 22:02:15 -0700138static 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 -0700139{
140 const char *perm = "find";
William Roberts8fb0f922015-10-01 22:02:15 -0700141 return check_mac_perms_from_lookup(spid, uid, perm, str8(name, name_len)) ? 1 : 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700142}
143
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000144struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -0700145{
146 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000147 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700148 struct binder_death death;
149 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000150 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700151 uint16_t name[0];
152};
153
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000154struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700155
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000156struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700157{
158 struct svcinfo *si;
159
160 for (si = svclist; si; si = si->next) {
161 if ((len == si->len) &&
162 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
163 return si;
164 }
165 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000166 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700167}
168
169void svcinfo_death(struct binder_state *bs, void *ptr)
170{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000171 struct svcinfo *si = (struct svcinfo* ) ptr;
172
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700173 ALOGI("service '%s' died\n", str8(si->name, si->len));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000174 if (si->handle) {
175 binder_release(bs, si->handle);
176 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000177 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700178}
179
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000180uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700181 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000182 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700183};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700184
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000185
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000186uint32_t do_find_service(const uint16_t *s, size_t len, uid_t uid, pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700187{
Nick Kralevichb27bbd12015-03-05 10:58:40 -0800188 struct svcinfo *si = find_svc(s, len);
189
190 if (!si || !si->handle) {
191 return 0;
192 }
193
194 if (!si->allow_isolated) {
195 // If this service doesn't allow access from isolated processes,
196 // then check the uid to see if it is isolated.
197 uid_t appid = uid % AID_USER;
198 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
199 return 0;
200 }
201 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700202
William Roberts8fb0f922015-10-01 22:02:15 -0700203 if (!svc_can_find(s, len, spid, uid)) {
Riley Spahnc67e6302014-07-08 09:03:00 -0700204 return 0;
205 }
Nick Kralevichb27bbd12015-03-05 10:58:40 -0800206
207 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700208}
209
210int do_add_service(struct binder_state *bs,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000211 const uint16_t *s, size_t len,
Riley Spahn69154df2014-06-05 11:07:18 -0700212 uint32_t handle, uid_t uid, int allow_isolated,
213 pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700214{
215 struct svcinfo *si;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000216
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700217 //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s, len), handle,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700218 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
219
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000220 if (!handle || (len == 0) || (len > 127))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700221 return -1;
222
William Roberts8fb0f922015-10-01 22:02:15 -0700223 if (!svc_can_register(s, len, spid, uid)) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000224 ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\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 }
228
229 si = find_svc(s, len);
230 if (si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000231 if (si->handle) {
232 ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700233 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700234 svcinfo_death(bs, si);
235 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000236 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700237 } else {
238 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
239 if (!si) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000240 ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700241 str8(s, len), handle, uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700242 return -1;
243 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000244 si->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700245 si->len = len;
246 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
247 si->name[len] = '\0';
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000248 si->death.func = (void*) svcinfo_death;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700249 si->death.ptr = si;
250 si->allow_isolated = allow_isolated;
251 si->next = svclist;
252 svclist = si;
253 }
254
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000255 binder_acquire(bs, handle);
256 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700257 return 0;
258}
259
260int svcmgr_handler(struct binder_state *bs,
Serban Constantinescubcf38882014-01-10 13:56:27 +0000261 struct binder_transaction_data *txn,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700262 struct binder_io *msg,
263 struct binder_io *reply)
264{
265 struct svcinfo *si;
266 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000267 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000268 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700269 uint32_t strict_policy;
270 int allow_isolated;
271
Elliott Hughes0b41ad52015-04-03 16:51:18 -0700272 //ALOGI("target=%p code=%d pid=%d uid=%d\n",
273 // (void*) txn->target.ptr, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700274
Elliott Hughes0b41ad52015-04-03 16:51:18 -0700275 if (txn->target.ptr != BINDER_SERVICE_MANAGER)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700276 return -1;
277
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800278 if (txn->code == PING_TRANSACTION)
279 return 0;
280
Mike Lockwood94afecf2012-10-24 10:45:23 -0700281 // Equivalent to Parcel::enforceInterface(), reading the RPC
282 // header with the strict mode policy mask and the interface name.
283 // Note that we ignore the strict_policy and don't propagate it
284 // further (since we do no outbound RPCs anyway).
285 strict_policy = bio_get_uint32(msg);
286 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700287 if (s == NULL) {
288 return -1;
289 }
290
Mike Lockwood94afecf2012-10-24 10:45:23 -0700291 if ((len != (sizeof(svcmgr_id) / 2)) ||
292 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700293 fprintf(stderr,"invalid id %s\n", str8(s, len));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700294 return -1;
295 }
296
Riley Spahn69154df2014-06-05 11:07:18 -0700297 if (sehandle && selinux_status_updated() > 0) {
298 struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
299 if (tmp_sehandle) {
300 selabel_close(sehandle);
301 sehandle = tmp_sehandle;
302 }
303 }
304
Mike Lockwood94afecf2012-10-24 10:45:23 -0700305 switch(txn->code) {
306 case SVC_MGR_GET_SERVICE:
307 case SVC_MGR_CHECK_SERVICE:
308 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700309 if (s == NULL) {
310 return -1;
311 }
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000312 handle = do_find_service(s, len, txn->sender_euid, txn->sender_pid);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000313 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700314 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000315 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700316 return 0;
317
318 case SVC_MGR_ADD_SERVICE:
319 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700320 if (s == NULL) {
321 return -1;
322 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000323 handle = bio_get_ref(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700324 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
Riley Spahn69154df2014-06-05 11:07:18 -0700325 if (do_add_service(bs, s, len, handle, txn->sender_euid,
326 allow_isolated, txn->sender_pid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700327 return -1;
328 break;
329
330 case SVC_MGR_LIST_SERVICES: {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000331 uint32_t n = bio_get_uint32(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700332
William Roberts8fb0f922015-10-01 22:02:15 -0700333 if (!svc_can_list(txn->sender_pid, txn->sender_euid)) {
Riley Spahnc67e6302014-07-08 09:03:00 -0700334 ALOGE("list_service() uid=%d - PERMISSION DENIED\n",
335 txn->sender_euid);
336 return -1;
337 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700338 si = svclist;
339 while ((n-- > 0) && si)
340 si = si->next;
341 if (si) {
342 bio_put_string16(reply, si->name);
343 return 0;
344 }
345 return -1;
346 }
347 default:
348 ALOGE("unknown code %d\n", txn->code);
349 return -1;
350 }
351
352 bio_put_uint32(reply, 0);
353 return 0;
354}
355
Riley Spahn69154df2014-06-05 11:07:18 -0700356
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000357static int audit_callback(void *data, __unused security_class_t cls, char *buf, size_t len)
Riley Spahn69154df2014-06-05 11:07:18 -0700358{
William Roberts8fb0f922015-10-01 22:02:15 -0700359 struct audit_data *ad = (struct audit_data *)data;
360
361 if (!ad || !ad->name) {
362 ALOGE("No service manager audit data");
363 return 0;
364 }
365
366 snprintf(buf, len, "service=%s pid=%d uid=%d", ad->name, ad->pid, ad->uid);
Riley Spahn69154df2014-06-05 11:07:18 -0700367 return 0;
368}
369
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000370int main()
Mike Lockwood94afecf2012-10-24 10:45:23 -0700371{
372 struct binder_state *bs;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700373
374 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000375 if (!bs) {
376 ALOGE("failed to open binder driver\n");
377 return -1;
378 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700379
380 if (binder_become_context_manager(bs)) {
381 ALOGE("cannot become context manager (%s)\n", strerror(errno));
382 return -1;
383 }
384
Riley Spahnc67e6302014-07-08 09:03:00 -0700385 selinux_enabled = is_selinux_enabled();
Riley Spahn69154df2014-06-05 11:07:18 -0700386 sehandle = selinux_android_service_context_handle();
Stephen Smalleybea07462015-06-03 09:25:37 -0400387 selinux_status_open(true);
Riley Spahn69154df2014-06-05 11:07:18 -0700388
Riley Spahnc67e6302014-07-08 09:03:00 -0700389 if (selinux_enabled > 0) {
390 if (sehandle == NULL) {
391 ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n");
392 abort();
393 }
394
395 if (getcon(&service_manager_context) != 0) {
396 ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n");
397 abort();
398 }
399 }
400
Riley Spahn69154df2014-06-05 11:07:18 -0700401 union selinux_callback cb;
402 cb.func_audit = audit_callback;
403 selinux_set_callback(SELINUX_CB_AUDIT, cb);
404 cb.func_log = selinux_log_callback;
405 selinux_set_callback(SELINUX_CB_LOG, cb);
406
Mike Lockwood94afecf2012-10-24 10:45:23 -0700407 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000408
Mike Lockwood94afecf2012-10-24 10:45:23 -0700409 return 0;
410}