blob: ca004e96a935a6142214bdbe5a6c880b0eab1ad1 [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
Yifan Hong212c8812017-08-01 17:09:07 -070011#include <cutils/android_filesystem_config.h>
Arve Hjønnevåg6b9c6d22016-08-18 15:42:35 -070012#include <cutils/multiuser.h>
13
Riley Spahn69154df2014-06-05 11:07:18 -070014#include <selinux/android.h>
Nick Kralevich7d42a3c2014-07-12 16:34:01 -070015#include <selinux/avc.h>
Riley Spahn69154df2014-06-05 11:07:18 -070016
Mike Lockwood94afecf2012-10-24 10:45:23 -070017#include "binder.h"
18
Martijn Coenen31361232017-03-31 16:12:12 -070019#ifdef VENDORSERVICEMANAGER
20#define LOG_TAG "VendorServiceManager"
Mike Lockwood94afecf2012-10-24 10:45:23 -070021#else
22#define LOG_TAG "ServiceManager"
Mike Lockwood94afecf2012-10-24 10:45:23 -070023#endif
Martijn Coenen31361232017-03-31 16:12:12 -070024#include <log/log.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070025
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 char *service_manager_context;
Riley Spahn69154df2014-06-05 11:07:18 -070062static struct selabel_handle* sehandle;
63
Steven Moreland3d795472019-01-04 17:51:30 -080064static bool check_mac_perms(pid_t spid, const char* sid, uid_t uid, const char *tctx, const char *perm, const char *name)
Riley Spahn69154df2014-06-05 11:07:18 -070065{
Steven Moreland3d795472019-01-04 17:51:30 -080066 char *lookup_sid = NULL;
Riley Spahnc67e6302014-07-08 09:03:00 -070067 const char *class = "service_manager";
68 bool allowed;
William Roberts8fb0f922015-10-01 22:02:15 -070069 struct audit_data ad;
Riley Spahn69154df2014-06-05 11:07:18 -070070
Steven Moreland3d795472019-01-04 17:51:30 -080071 if (sid == NULL && getpidcon(spid, &lookup_sid) < 0) {
Riley Spahnc67e6302014-07-08 09:03:00 -070072 ALOGE("SELinux: getpidcon(pid=%d) failed to retrieve pid context.\n", spid);
Riley Spahn69154df2014-06-05 11:07:18 -070073 return false;
74 }
75
William Roberts8fb0f922015-10-01 22:02:15 -070076 ad.pid = spid;
77 ad.uid = uid;
78 ad.name = name;
79
Steven Moreland3d795472019-01-04 17:51:30 -080080#ifndef VENDORSERVICEMANAGER
81 if (sid == NULL) {
82 android_errorWriteLog(0x534e4554, "121035042");
83 }
84#endif
85
86 int result = selinux_check_access(sid ? sid : lookup_sid, tctx, class, perm, (void *) &ad);
Riley Spahn69154df2014-06-05 11:07:18 -070087 allowed = (result == 0);
88
Steven Moreland3d795472019-01-04 17:51:30 -080089 freecon(lookup_sid);
Riley Spahnc67e6302014-07-08 09:03:00 -070090 return allowed;
91}
92
Steven Moreland3d795472019-01-04 17:51:30 -080093static bool check_mac_perms_from_getcon(pid_t spid, const char* sid, uid_t uid, const char *perm)
Riley Spahnc67e6302014-07-08 09:03:00 -070094{
Steven Moreland3d795472019-01-04 17:51:30 -080095 return check_mac_perms(spid, sid, uid, service_manager_context, perm, NULL);
Riley Spahnc67e6302014-07-08 09:03:00 -070096}
97
Steven Moreland3d795472019-01-04 17:51:30 -080098static bool check_mac_perms_from_lookup(pid_t spid, const char* sid, uid_t uid, const char *perm, const char *name)
Riley Spahnc67e6302014-07-08 09:03:00 -070099{
100 bool allowed;
101 char *tctx = NULL;
102
Riley Spahnc67e6302014-07-08 09:03:00 -0700103 if (!sehandle) {
104 ALOGE("SELinux: Failed to find sehandle. Aborting service_manager.\n");
105 abort();
106 }
107
108 if (selabel_lookup(sehandle, &tctx, name, 0) != 0) {
109 ALOGE("SELinux: No match for %s in service_contexts.\n", name);
110 return false;
111 }
112
Steven Moreland3d795472019-01-04 17:51:30 -0800113 allowed = check_mac_perms(spid, sid, uid, tctx, perm, name);
Riley Spahn69154df2014-06-05 11:07:18 -0700114 freecon(tctx);
115 return allowed;
116}
117
Steven Moreland3d795472019-01-04 17:51:30 -0800118static int svc_can_register(const uint16_t *name, size_t name_len, pid_t spid, const char* sid, uid_t uid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700119{
Riley Spahnc67e6302014-07-08 09:03:00 -0700120 const char *perm = "add";
Arve Hjønnevåg5fa90a02016-08-01 16:05:17 -0700121
Arve Hjønnevåg6b9c6d22016-08-18 15:42:35 -0700122 if (multiuser_get_app_id(uid) >= AID_APP) {
Arve Hjønnevåg5fa90a02016-08-01 16:05:17 -0700123 return 0; /* Don't allow apps to register services */
124 }
125
Steven Moreland3d795472019-01-04 17:51:30 -0800126 return check_mac_perms_from_lookup(spid, sid, uid, perm, str8(name, name_len)) ? 1 : 0;
Riley Spahnc67e6302014-07-08 09:03:00 -0700127}
128
Steven Moreland3d795472019-01-04 17:51:30 -0800129static int svc_can_list(pid_t spid, const char* sid, uid_t uid)
Riley Spahnc67e6302014-07-08 09:03:00 -0700130{
131 const char *perm = "list";
Steven Moreland3d795472019-01-04 17:51:30 -0800132 return check_mac_perms_from_getcon(spid, sid, uid, perm) ? 1 : 0;
Riley Spahnc67e6302014-07-08 09:03:00 -0700133}
134
Steven Moreland3d795472019-01-04 17:51:30 -0800135static int svc_can_find(const uint16_t *name, size_t name_len, pid_t spid, const char* sid, uid_t uid)
Riley Spahnc67e6302014-07-08 09:03:00 -0700136{
137 const char *perm = "find";
Steven Moreland3d795472019-01-04 17:51:30 -0800138 return check_mac_perms_from_lookup(spid, sid, uid, perm, str8(name, name_len)) ? 1 : 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700139}
140
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000141struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -0700142{
143 struct svcinfo *next;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000144 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700145 struct binder_death death;
146 int allow_isolated;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700147 uint32_t dumpsys_priority;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000148 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700149 uint16_t name[0];
150};
151
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000152struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700153
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000154struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700155{
156 struct svcinfo *si;
157
158 for (si = svclist; si; si = si->next) {
159 if ((len == si->len) &&
160 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
161 return si;
162 }
163 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000164 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700165}
166
167void svcinfo_death(struct binder_state *bs, void *ptr)
168{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000169 struct svcinfo *si = (struct svcinfo* ) ptr;
170
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700171 ALOGI("service '%s' died\n", str8(si->name, si->len));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000172 if (si->handle) {
173 binder_release(bs, si->handle);
174 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000175 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700176}
177
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000178uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700179 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000180 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700181};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700182
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000183
Steven Moreland3d795472019-01-04 17:51:30 -0800184uint32_t do_find_service(const uint16_t *s, size_t len, uid_t uid, pid_t spid, const char* sid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700185{
Nick Kralevichb27bbd12015-03-05 10:58:40 -0800186 struct svcinfo *si = find_svc(s, len);
187
188 if (!si || !si->handle) {
189 return 0;
190 }
191
192 if (!si->allow_isolated) {
193 // If this service doesn't allow access from isolated processes,
194 // then check the uid to see if it is isolated.
195 uid_t appid = uid % AID_USER;
196 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
197 return 0;
198 }
199 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700200
Steven Moreland3d795472019-01-04 17:51:30 -0800201 if (!svc_can_find(s, len, spid, sid, uid)) {
Riley Spahnc67e6302014-07-08 09:03:00 -0700202 return 0;
203 }
Nick Kralevichb27bbd12015-03-05 10:58:40 -0800204
205 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700206}
207
Vishnu Nairf56042d2017-09-19 15:25:10 -0700208int do_add_service(struct binder_state *bs, const uint16_t *s, size_t len, uint32_t handle,
Steven Moreland3d795472019-01-04 17:51:30 -0800209 uid_t uid, int allow_isolated, uint32_t dumpsys_priority, pid_t spid, const char* sid) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700210 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
Steven Moreland3d795472019-01-04 17:51:30 -0800218 if (!svc_can_register(s, len, spid, sid, 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;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700246 si->dumpsys_priority = dumpsys_priority;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700247 si->next = svclist;
248 svclist = si;
249 }
250
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000251 binder_acquire(bs, handle);
252 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700253 return 0;
254}
255
256int svcmgr_handler(struct binder_state *bs,
Steven Moreland3d795472019-01-04 17:51:30 -0800257 struct binder_transaction_data_secctx *txn_secctx,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700258 struct binder_io *msg,
259 struct binder_io *reply)
260{
261 struct svcinfo *si;
262 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000263 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000264 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700265 uint32_t strict_policy;
266 int allow_isolated;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700267 uint32_t dumpsys_priority;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700268
Steven Moreland3d795472019-01-04 17:51:30 -0800269 struct binder_transaction_data *txn = &txn_secctx->transaction_data;
270
Elliott Hughes0b41ad52015-04-03 16:51:18 -0700271 //ALOGI("target=%p code=%d pid=%d uid=%d\n",
272 // (void*) txn->target.ptr, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700273
Elliott Hughes0b41ad52015-04-03 16:51:18 -0700274 if (txn->target.ptr != BINDER_SERVICE_MANAGER)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700275 return -1;
276
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800277 if (txn->code == PING_TRANSACTION)
278 return 0;
279
Mike Lockwood94afecf2012-10-24 10:45:23 -0700280 // Equivalent to Parcel::enforceInterface(), reading the RPC
281 // header with the strict mode policy mask and the interface name.
282 // Note that we ignore the strict_policy and don't propagate it
283 // further (since we do no outbound RPCs anyway).
284 strict_policy = bio_get_uint32(msg);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100285 bio_get_uint32(msg); // Ignore worksource header.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700286 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) {
Kouji Shiotani61f8dfa2017-06-13 15:40:54 +0900298#ifdef VENDORSERVICEMANAGER
299 struct selabel_handle *tmp_sehandle = selinux_android_vendor_service_context_handle();
300#else
Riley Spahn69154df2014-06-05 11:07:18 -0700301 struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
Kouji Shiotani61f8dfa2017-06-13 15:40:54 +0900302#endif
Riley Spahn69154df2014-06-05 11:07:18 -0700303 if (tmp_sehandle) {
304 selabel_close(sehandle);
305 sehandle = tmp_sehandle;
306 }
307 }
308
Mike Lockwood94afecf2012-10-24 10:45:23 -0700309 switch(txn->code) {
310 case SVC_MGR_GET_SERVICE:
311 case SVC_MGR_CHECK_SERVICE:
312 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700313 if (s == NULL) {
314 return -1;
315 }
Steven Moreland3d795472019-01-04 17:51:30 -0800316 handle = do_find_service(s, len, txn->sender_euid, txn->sender_pid,
317 (const char*) txn_secctx->secctx);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000318 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700319 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000320 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700321 return 0;
322
323 case SVC_MGR_ADD_SERVICE:
324 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700325 if (s == NULL) {
326 return -1;
327 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000328 handle = bio_get_ref(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700329 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700330 dumpsys_priority = bio_get_uint32(msg);
331 if (do_add_service(bs, s, len, handle, txn->sender_euid, allow_isolated, dumpsys_priority,
Steven Moreland3d795472019-01-04 17:51:30 -0800332 txn->sender_pid, (const char*) txn_secctx->secctx))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700333 return -1;
334 break;
335
336 case SVC_MGR_LIST_SERVICES: {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000337 uint32_t n = bio_get_uint32(msg);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700338 uint32_t req_dumpsys_priority = bio_get_uint32(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700339
Steven Moreland3d795472019-01-04 17:51:30 -0800340 if (!svc_can_list(txn->sender_pid, (const char*) txn_secctx->secctx, txn->sender_euid)) {
Riley Spahnc67e6302014-07-08 09:03:00 -0700341 ALOGE("list_service() uid=%d - PERMISSION DENIED\n",
342 txn->sender_euid);
343 return -1;
344 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700345 si = svclist;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700346 // walk through the list of services n times skipping services that
347 // do not support the requested priority
348 while (si) {
349 if (si->dumpsys_priority & req_dumpsys_priority) {
350 if (n == 0) break;
351 n--;
352 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700353 si = si->next;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700354 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700355 if (si) {
356 bio_put_string16(reply, si->name);
357 return 0;
358 }
359 return -1;
360 }
361 default:
362 ALOGE("unknown code %d\n", txn->code);
363 return -1;
364 }
365
366 bio_put_uint32(reply, 0);
367 return 0;
368}
369
Riley Spahn69154df2014-06-05 11:07:18 -0700370
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000371static int audit_callback(void *data, __unused security_class_t cls, char *buf, size_t len)
Riley Spahn69154df2014-06-05 11:07:18 -0700372{
William Roberts8fb0f922015-10-01 22:02:15 -0700373 struct audit_data *ad = (struct audit_data *)data;
374
375 if (!ad || !ad->name) {
376 ALOGE("No service manager audit data");
377 return 0;
378 }
379
380 snprintf(buf, len, "service=%s pid=%d uid=%d", ad->name, ad->pid, ad->uid);
Riley Spahn69154df2014-06-05 11:07:18 -0700381 return 0;
382}
383
Martijn Coenen69b05152017-03-21 10:00:38 -0700384int main(int argc, char** argv)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700385{
386 struct binder_state *bs;
Sandeep Patil93ba7012016-12-27 12:40:45 -0800387 union selinux_callback cb;
Martijn Coenen69b05152017-03-21 10:00:38 -0700388 char *driver;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700389
Martijn Coenen69b05152017-03-21 10:00:38 -0700390 if (argc > 1) {
391 driver = argv[1];
392 } else {
393 driver = "/dev/binder";
394 }
395
396 bs = binder_open(driver, 128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000397 if (!bs) {
Martijn Coenen31361232017-03-31 16:12:12 -0700398#ifdef VENDORSERVICEMANAGER
399 ALOGW("failed to open binder driver %s\n", driver);
400 while (true) {
401 sleep(UINT_MAX);
402 }
403#else
Martijn Coenen69b05152017-03-21 10:00:38 -0700404 ALOGE("failed to open binder driver %s\n", driver);
Martijn Coenen31361232017-03-31 16:12:12 -0700405#endif
Serban Constantinescua44542c2014-01-30 15:16:45 +0000406 return -1;
407 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700408
409 if (binder_become_context_manager(bs)) {
410 ALOGE("cannot become context manager (%s)\n", strerror(errno));
411 return -1;
412 }
413
Sandeep Patil93ba7012016-12-27 12:40:45 -0800414 cb.func_audit = audit_callback;
415 selinux_set_callback(SELINUX_CB_AUDIT, cb);
Steven Moreland41b69be2018-09-06 16:52:07 -0700416#ifdef VENDORSERVICEMANAGER
417 cb.func_log = selinux_vendor_log_callback;
418#else
Sandeep Patil93ba7012016-12-27 12:40:45 -0800419 cb.func_log = selinux_log_callback;
Steven Moreland41b69be2018-09-06 16:52:07 -0700420#endif
Sandeep Patil93ba7012016-12-27 12:40:45 -0800421 selinux_set_callback(SELINUX_CB_LOG, cb);
422
Martijn Coenen31361232017-03-31 16:12:12 -0700423#ifdef VENDORSERVICEMANAGER
424 sehandle = selinux_android_vendor_service_context_handle();
425#else
Riley Spahn69154df2014-06-05 11:07:18 -0700426 sehandle = selinux_android_service_context_handle();
Martijn Coenen31361232017-03-31 16:12:12 -0700427#endif
Stephen Smalleybea07462015-06-03 09:25:37 -0400428 selinux_status_open(true);
Riley Spahn69154df2014-06-05 11:07:18 -0700429
Nick Kralevicheb4d5cb2016-12-09 17:05:09 -0800430 if (sehandle == NULL) {
431 ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n");
432 abort();
433 }
Riley Spahnc67e6302014-07-08 09:03:00 -0700434
Nick Kralevicheb4d5cb2016-12-09 17:05:09 -0800435 if (getcon(&service_manager_context) != 0) {
436 ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n");
437 abort();
Riley Spahnc67e6302014-07-08 09:03:00 -0700438 }
439
Riley Spahn69154df2014-06-05 11:07:18 -0700440
Mike Lockwood94afecf2012-10-24 10:45:23 -0700441 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000442
Mike Lockwood94afecf2012-10-24 10:45:23 -0700443 return 0;
444}