blob: ea4155848bb82d9ddc1a17083a15a0c222e472a3 [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
Arve Hjønnevåg6b9c6d22016-08-18 15:42:35 -070011#include <cutils/multiuser.h>
12
Mike Lockwood94afecf2012-10-24 10:45:23 -070013#include <private/android_filesystem_config.h>
14
Riley Spahn69154df2014-06-05 11:07:18 -070015#include <selinux/android.h>
Nick Kralevich7d42a3c2014-07-12 16:34:01 -070016#include <selinux/avc.h>
Riley Spahn69154df2014-06-05 11:07:18 -070017
Mike Lockwood94afecf2012-10-24 10:45:23 -070018#include "binder.h"
19
20#if 0
21#define ALOGI(x...) fprintf(stderr, "svcmgr: " x)
22#define ALOGE(x...) fprintf(stderr, "svcmgr: " x)
23#else
24#define LOG_TAG "ServiceManager"
25#include <cutils/log.h>
26#endif
27
William Roberts8fb0f922015-10-01 22:02:15 -070028struct audit_data {
29 pid_t pid;
30 uid_t uid;
31 const char *name;
32};
33
Nick Kralevich7d42a3c2014-07-12 16:34:01 -070034const char *str8(const uint16_t *x, size_t x_len)
Mike Lockwood94afecf2012-10-24 10:45:23 -070035{
36 static char buf[128];
Nick Kralevich7d42a3c2014-07-12 16:34:01 -070037 size_t max = 127;
Mike Lockwood94afecf2012-10-24 10:45:23 -070038 char *p = buf;
39
Nick Kralevich7d42a3c2014-07-12 16:34:01 -070040 if (x_len < max) {
41 max = x_len;
42 }
43
Mike Lockwood94afecf2012-10-24 10:45:23 -070044 if (x) {
Nick Kralevich7d42a3c2014-07-12 16:34:01 -070045 while ((max > 0) && (*x != '\0')) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070046 *p++ = *x++;
Nick Kralevich7d42a3c2014-07-12 16:34:01 -070047 max--;
Mike Lockwood94afecf2012-10-24 10:45:23 -070048 }
49 }
50 *p++ = 0;
51 return buf;
52}
53
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000054int str16eq(const uint16_t *a, const char *b)
Mike Lockwood94afecf2012-10-24 10:45:23 -070055{
56 while (*a && *b)
57 if (*a++ != *b++) return 0;
58 if (*a || *b)
59 return 0;
60 return 1;
61}
62
Riley Spahnc67e6302014-07-08 09:03:00 -070063static char *service_manager_context;
Riley Spahn69154df2014-06-05 11:07:18 -070064static struct selabel_handle* sehandle;
65
William Roberts8fb0f922015-10-01 22:02:15 -070066static 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 -070067{
Riley Spahn69154df2014-06-05 11:07:18 -070068 char *sctx = NULL;
Riley Spahnc67e6302014-07-08 09:03:00 -070069 const char *class = "service_manager";
70 bool allowed;
William Roberts8fb0f922015-10-01 22:02:15 -070071 struct audit_data ad;
Riley Spahn69154df2014-06-05 11:07:18 -070072
73 if (getpidcon(spid, &sctx) < 0) {
Riley Spahnc67e6302014-07-08 09:03:00 -070074 ALOGE("SELinux: getpidcon(pid=%d) failed to retrieve pid context.\n", spid);
Riley Spahn69154df2014-06-05 11:07:18 -070075 return false;
76 }
77
William Roberts8fb0f922015-10-01 22:02:15 -070078 ad.pid = spid;
79 ad.uid = uid;
80 ad.name = name;
81
82 int result = selinux_check_access(sctx, tctx, class, perm, (void *) &ad);
Riley Spahn69154df2014-06-05 11:07:18 -070083 allowed = (result == 0);
84
85 freecon(sctx);
Riley Spahnc67e6302014-07-08 09:03:00 -070086 return allowed;
87}
88
William Roberts8fb0f922015-10-01 22:02:15 -070089static bool check_mac_perms_from_getcon(pid_t spid, uid_t uid, const char *perm)
Riley Spahnc67e6302014-07-08 09:03:00 -070090{
William Roberts8fb0f922015-10-01 22:02:15 -070091 return check_mac_perms(spid, uid, service_manager_context, perm, NULL);
Riley Spahnc67e6302014-07-08 09:03:00 -070092}
93
William Roberts8fb0f922015-10-01 22:02:15 -070094static 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 -070095{
96 bool allowed;
97 char *tctx = NULL;
98
Riley Spahnc67e6302014-07-08 09:03:00 -070099 if (!sehandle) {
100 ALOGE("SELinux: Failed to find sehandle. Aborting service_manager.\n");
101 abort();
102 }
103
104 if (selabel_lookup(sehandle, &tctx, name, 0) != 0) {
105 ALOGE("SELinux: No match for %s in service_contexts.\n", name);
106 return false;
107 }
108
William Roberts8fb0f922015-10-01 22:02:15 -0700109 allowed = check_mac_perms(spid, uid, tctx, perm, name);
Riley Spahn69154df2014-06-05 11:07:18 -0700110 freecon(tctx);
111 return allowed;
112}
113
William Roberts8fb0f922015-10-01 22:02:15 -0700114static 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 -0700115{
Riley Spahnc67e6302014-07-08 09:03:00 -0700116 const char *perm = "add";
Arve Hjønnevåg5fa90a02016-08-01 16:05:17 -0700117
Arve Hjønnevåg6b9c6d22016-08-18 15:42:35 -0700118 if (multiuser_get_app_id(uid) >= AID_APP) {
Arve Hjønnevåg5fa90a02016-08-01 16:05:17 -0700119 return 0; /* Don't allow apps to register services */
120 }
121
William Roberts8fb0f922015-10-01 22:02:15 -0700122 return check_mac_perms_from_lookup(spid, uid, perm, str8(name, name_len)) ? 1 : 0;
Riley Spahnc67e6302014-07-08 09:03:00 -0700123}
124
William Roberts8fb0f922015-10-01 22:02:15 -0700125static int svc_can_list(pid_t spid, uid_t uid)
Riley Spahnc67e6302014-07-08 09:03:00 -0700126{
127 const char *perm = "list";
William Roberts8fb0f922015-10-01 22:02:15 -0700128 return check_mac_perms_from_getcon(spid, uid, perm) ? 1 : 0;
Riley Spahnc67e6302014-07-08 09:03:00 -0700129}
130
William Roberts8fb0f922015-10-01 22:02:15 -0700131static 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 -0700132{
133 const char *perm = "find";
William Roberts8fb0f922015-10-01 22:02:15 -0700134 return check_mac_perms_from_lookup(spid, uid, perm, str8(name, name_len)) ? 1 : 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700135}
136
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000137struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -0700138{
139 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000140 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700141 struct binder_death death;
142 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000143 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700144 uint16_t name[0];
145};
146
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000147struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700148
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000149struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700150{
151 struct svcinfo *si;
152
153 for (si = svclist; si; si = si->next) {
154 if ((len == si->len) &&
155 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
156 return si;
157 }
158 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000159 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700160}
161
162void svcinfo_death(struct binder_state *bs, void *ptr)
163{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000164 struct svcinfo *si = (struct svcinfo* ) ptr;
165
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700166 ALOGI("service '%s' died\n", str8(si->name, si->len));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000167 if (si->handle) {
168 binder_release(bs, si->handle);
169 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000170 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700171}
172
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000173uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700174 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000175 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700176};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700177
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000178
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000179uint32_t do_find_service(const uint16_t *s, size_t len, uid_t uid, pid_t spid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700180{
Nick Kralevichb27bbd12015-03-05 10:58:40 -0800181 struct svcinfo *si = find_svc(s, len);
182
183 if (!si || !si->handle) {
184 return 0;
185 }
186
187 if (!si->allow_isolated) {
188 // If this service doesn't allow access from isolated processes,
189 // then check the uid to see if it is isolated.
190 uid_t appid = uid % AID_USER;
191 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
192 return 0;
193 }
194 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700195
William Roberts8fb0f922015-10-01 22:02:15 -0700196 if (!svc_can_find(s, len, spid, uid)) {
Riley Spahnc67e6302014-07-08 09:03:00 -0700197 return 0;
198 }
Nick Kralevichb27bbd12015-03-05 10:58:40 -0800199
200 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700201}
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 Kralevich7d42a3c2014-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
William Roberts8fb0f922015-10-01 22:02:15 -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 Kralevich7d42a3c2014-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 Kralevich7d42a3c2014-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 Kralevich7d42a3c2014-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
Elliott Hughes0b41ad52015-04-03 16:51:18 -0700265 //ALOGI("target=%p code=%d pid=%d uid=%d\n",
266 // (void*) txn->target.ptr, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700267
Elliott Hughes0b41ad52015-04-03 16:51:18 -0700268 if (txn->target.ptr != BINDER_SERVICE_MANAGER)
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 Kralevich7d42a3c2014-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 Kralevich7d42a3c2014-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 Kralevich7d42a3c2014-07-12 16:34:01 -0700302 if (s == NULL) {
303 return -1;
304 }
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000305 handle = do_find_service(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 Kralevich7d42a3c2014-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
William Roberts8fb0f922015-10-01 22:02:15 -0700326 if (!svc_can_list(txn->sender_pid, txn->sender_euid)) {
Riley Spahnc67e6302014-07-08 09:03:00 -0700327 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
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000350static int audit_callback(void *data, __unused security_class_t cls, char *buf, size_t len)
Riley Spahn69154df2014-06-05 11:07:18 -0700351{
William Roberts8fb0f922015-10-01 22:02:15 -0700352 struct audit_data *ad = (struct audit_data *)data;
353
354 if (!ad || !ad->name) {
355 ALOGE("No service manager audit data");
356 return 0;
357 }
358
359 snprintf(buf, len, "service=%s pid=%d uid=%d", ad->name, ad->pid, ad->uid);
Riley Spahn69154df2014-06-05 11:07:18 -0700360 return 0;
361}
362
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000363int main()
Mike Lockwood94afecf2012-10-24 10:45:23 -0700364{
365 struct binder_state *bs;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700366
367 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000368 if (!bs) {
369 ALOGE("failed to open binder driver\n");
370 return -1;
371 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700372
373 if (binder_become_context_manager(bs)) {
374 ALOGE("cannot become context manager (%s)\n", strerror(errno));
375 return -1;
376 }
377
Riley Spahn69154df2014-06-05 11:07:18 -0700378 sehandle = selinux_android_service_context_handle();
Stephen Smalleybea07462015-06-03 09:25:37 -0400379 selinux_status_open(true);
Riley Spahn69154df2014-06-05 11:07:18 -0700380
Nick Kralevicheb4d5cb2016-12-09 17:05:09 -0800381 if (sehandle == NULL) {
382 ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n");
383 abort();
384 }
Riley Spahnc67e6302014-07-08 09:03:00 -0700385
Nick Kralevicheb4d5cb2016-12-09 17:05:09 -0800386 if (getcon(&service_manager_context) != 0) {
387 ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n");
388 abort();
Riley Spahnc67e6302014-07-08 09:03:00 -0700389 }
390
Riley Spahn69154df2014-06-05 11:07:18 -0700391 union selinux_callback cb;
392 cb.func_audit = audit_callback;
393 selinux_set_callback(SELINUX_CB_AUDIT, cb);
394 cb.func_log = selinux_log_callback;
395 selinux_set_callback(SELINUX_CB_LOG, cb);
396
Mike Lockwood94afecf2012-10-24 10:45:23 -0700397 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000398
Mike Lockwood94afecf2012-10-24 10:45:23 -0700399 return 0;
400}