blob: ec3fac538dcadb3cf09ec1b35dd891a8b5414ce2 [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 if (sid == NULL) {
81 android_errorWriteLog(0x534e4554, "121035042");
82 }
Steven Moreland3d795472019-01-04 17:51:30 -080083
84 int result = selinux_check_access(sid ? sid : lookup_sid, tctx, class, perm, (void *) &ad);
Riley Spahn69154df2014-06-05 11:07:18 -070085 allowed = (result == 0);
86
Steven Moreland3d795472019-01-04 17:51:30 -080087 freecon(lookup_sid);
Riley Spahnc67e6302014-07-08 09:03:00 -070088 return allowed;
89}
90
Steven Moreland3d795472019-01-04 17:51:30 -080091static 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 -070092{
Steven Moreland3d795472019-01-04 17:51:30 -080093 return check_mac_perms(spid, sid, uid, service_manager_context, perm, NULL);
Riley Spahnc67e6302014-07-08 09:03:00 -070094}
95
Steven Moreland3d795472019-01-04 17:51:30 -080096static 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 -070097{
98 bool allowed;
99 char *tctx = NULL;
100
Riley Spahnc67e6302014-07-08 09:03:00 -0700101 if (!sehandle) {
102 ALOGE("SELinux: Failed to find sehandle. Aborting service_manager.\n");
103 abort();
104 }
105
106 if (selabel_lookup(sehandle, &tctx, name, 0) != 0) {
107 ALOGE("SELinux: No match for %s in service_contexts.\n", name);
108 return false;
109 }
110
Steven Moreland3d795472019-01-04 17:51:30 -0800111 allowed = check_mac_perms(spid, sid, uid, tctx, perm, name);
Riley Spahn69154df2014-06-05 11:07:18 -0700112 freecon(tctx);
113 return allowed;
114}
115
Steven Moreland3d795472019-01-04 17:51:30 -0800116static 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 -0700117{
Riley Spahnc67e6302014-07-08 09:03:00 -0700118 const char *perm = "add";
Arve Hjønnevåg5fa90a02016-08-01 16:05:17 -0700119
Arve Hjønnevåg6b9c6d22016-08-18 15:42:35 -0700120 if (multiuser_get_app_id(uid) >= AID_APP) {
Arve Hjønnevåg5fa90a02016-08-01 16:05:17 -0700121 return 0; /* Don't allow apps to register services */
122 }
123
Steven Moreland3d795472019-01-04 17:51:30 -0800124 return check_mac_perms_from_lookup(spid, sid, uid, perm, str8(name, name_len)) ? 1 : 0;
Riley Spahnc67e6302014-07-08 09:03:00 -0700125}
126
Steven Moreland3d795472019-01-04 17:51:30 -0800127static int svc_can_list(pid_t spid, const char* sid, uid_t uid)
Riley Spahnc67e6302014-07-08 09:03:00 -0700128{
129 const char *perm = "list";
Steven Moreland3d795472019-01-04 17:51:30 -0800130 return check_mac_perms_from_getcon(spid, sid, uid, perm) ? 1 : 0;
Riley Spahnc67e6302014-07-08 09:03:00 -0700131}
132
Steven Moreland3d795472019-01-04 17:51:30 -0800133static 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 -0700134{
135 const char *perm = "find";
Steven Moreland3d795472019-01-04 17:51:30 -0800136 return check_mac_perms_from_lookup(spid, sid, 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;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700145 uint32_t dumpsys_priority;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000146 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700147 uint16_t name[0];
148};
149
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000150struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700151
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000152struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700153{
154 struct svcinfo *si;
155
156 for (si = svclist; si; si = si->next) {
157 if ((len == si->len) &&
158 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
159 return si;
160 }
161 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000162 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700163}
164
165void svcinfo_death(struct binder_state *bs, void *ptr)
166{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000167 struct svcinfo *si = (struct svcinfo* ) ptr;
168
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700169 ALOGI("service '%s' died\n", str8(si->name, si->len));
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000170 if (si->handle) {
171 binder_release(bs, si->handle);
172 si->handle = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000173 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700174}
175
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000176uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700177 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000178 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700179};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700180
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000181
Steven Moreland3d795472019-01-04 17:51:30 -0800182uint32_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 -0700183{
Nick Kralevichb27bbd12015-03-05 10:58:40 -0800184 struct svcinfo *si = find_svc(s, len);
185
186 if (!si || !si->handle) {
187 return 0;
188 }
189
190 if (!si->allow_isolated) {
191 // If this service doesn't allow access from isolated processes,
192 // then check the uid to see if it is isolated.
193 uid_t appid = uid % AID_USER;
194 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
195 return 0;
196 }
197 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700198
Steven Moreland3d795472019-01-04 17:51:30 -0800199 if (!svc_can_find(s, len, spid, sid, uid)) {
Riley Spahnc67e6302014-07-08 09:03:00 -0700200 return 0;
201 }
Nick Kralevichb27bbd12015-03-05 10:58:40 -0800202
203 return si->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700204}
205
Vishnu Nairf56042d2017-09-19 15:25:10 -0700206int do_add_service(struct binder_state *bs, const uint16_t *s, size_t len, uint32_t handle,
Steven Moreland3d795472019-01-04 17:51:30 -0800207 uid_t uid, int allow_isolated, uint32_t dumpsys_priority, pid_t spid, const char* sid) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700208 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
Steven Moreland3d795472019-01-04 17:51:30 -0800216 if (!svc_can_register(s, len, spid, sid, 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;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700244 si->dumpsys_priority = dumpsys_priority;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700245 si->next = svclist;
246 svclist = si;
247 }
248
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000249 binder_acquire(bs, handle);
250 binder_link_to_death(bs, handle, &si->death);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700251 return 0;
252}
253
254int svcmgr_handler(struct binder_state *bs,
Steven Moreland3d795472019-01-04 17:51:30 -0800255 struct binder_transaction_data_secctx *txn_secctx,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700256 struct binder_io *msg,
257 struct binder_io *reply)
258{
259 struct svcinfo *si;
260 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000261 size_t len;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000262 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700263 uint32_t strict_policy;
264 int allow_isolated;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700265 uint32_t dumpsys_priority;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700266
Steven Moreland3d795472019-01-04 17:51:30 -0800267 struct binder_transaction_data *txn = &txn_secctx->transaction_data;
268
Elliott Hughes0b41ad52015-04-03 16:51:18 -0700269 //ALOGI("target=%p code=%d pid=%d uid=%d\n",
270 // (void*) txn->target.ptr, txn->code, txn->sender_pid, txn->sender_euid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700271
Elliott Hughes0b41ad52015-04-03 16:51:18 -0700272 if (txn->target.ptr != BINDER_SERVICE_MANAGER)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700273 return -1;
274
Arve Hjønnevåge5245cb2014-01-28 21:35:03 -0800275 if (txn->code == PING_TRANSACTION)
276 return 0;
277
Mike Lockwood94afecf2012-10-24 10:45:23 -0700278 // Equivalent to Parcel::enforceInterface(), reading the RPC
279 // header with the strict mode policy mask and the interface name.
280 // Note that we ignore the strict_policy and don't propagate it
281 // further (since we do no outbound RPCs anyway).
282 strict_policy = bio_get_uint32(msg);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100283 bio_get_uint32(msg); // Ignore worksource header.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700284 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700285 if (s == NULL) {
286 return -1;
287 }
288
Mike Lockwood94afecf2012-10-24 10:45:23 -0700289 if ((len != (sizeof(svcmgr_id) / 2)) ||
290 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700291 fprintf(stderr,"invalid id %s\n", str8(s, len));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700292 return -1;
293 }
294
Riley Spahn69154df2014-06-05 11:07:18 -0700295 if (sehandle && selinux_status_updated() > 0) {
Kouji Shiotani61f8dfa2017-06-13 15:40:54 +0900296#ifdef VENDORSERVICEMANAGER
297 struct selabel_handle *tmp_sehandle = selinux_android_vendor_service_context_handle();
298#else
Riley Spahn69154df2014-06-05 11:07:18 -0700299 struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
Kouji Shiotani61f8dfa2017-06-13 15:40:54 +0900300#endif
Riley Spahn69154df2014-06-05 11:07:18 -0700301 if (tmp_sehandle) {
302 selabel_close(sehandle);
303 sehandle = tmp_sehandle;
304 }
305 }
306
Mike Lockwood94afecf2012-10-24 10:45:23 -0700307 switch(txn->code) {
308 case SVC_MGR_GET_SERVICE:
309 case SVC_MGR_CHECK_SERVICE:
310 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700311 if (s == NULL) {
312 return -1;
313 }
Steven Moreland3d795472019-01-04 17:51:30 -0800314 handle = do_find_service(s, len, txn->sender_euid, txn->sender_pid,
315 (const char*) txn_secctx->secctx);
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000316 if (!handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700317 break;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000318 bio_put_ref(reply, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700319 return 0;
320
321 case SVC_MGR_ADD_SERVICE:
322 s = bio_get_string16(msg, &len);
Nick Kralevich7d42a3c2014-07-12 16:34:01 -0700323 if (s == NULL) {
324 return -1;
325 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000326 handle = bio_get_ref(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700327 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700328 dumpsys_priority = bio_get_uint32(msg);
329 if (do_add_service(bs, s, len, handle, txn->sender_euid, allow_isolated, dumpsys_priority,
Steven Moreland3d795472019-01-04 17:51:30 -0800330 txn->sender_pid, (const char*) txn_secctx->secctx))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700331 return -1;
332 break;
333
334 case SVC_MGR_LIST_SERVICES: {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000335 uint32_t n = bio_get_uint32(msg);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700336 uint32_t req_dumpsys_priority = bio_get_uint32(msg);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700337
Steven Moreland3d795472019-01-04 17:51:30 -0800338 if (!svc_can_list(txn->sender_pid, (const char*) txn_secctx->secctx, txn->sender_euid)) {
Riley Spahnc67e6302014-07-08 09:03:00 -0700339 ALOGE("list_service() uid=%d - PERMISSION DENIED\n",
340 txn->sender_euid);
341 return -1;
342 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700343 si = svclist;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700344 // walk through the list of services n times skipping services that
345 // do not support the requested priority
346 while (si) {
347 if (si->dumpsys_priority & req_dumpsys_priority) {
348 if (n == 0) break;
349 n--;
350 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700351 si = si->next;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700352 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700353 if (si) {
354 bio_put_string16(reply, si->name);
355 return 0;
356 }
357 return -1;
358 }
359 default:
360 ALOGE("unknown code %d\n", txn->code);
361 return -1;
362 }
363
364 bio_put_uint32(reply, 0);
365 return 0;
366}
367
Riley Spahn69154df2014-06-05 11:07:18 -0700368
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000369static int audit_callback(void *data, __unused security_class_t cls, char *buf, size_t len)
Riley Spahn69154df2014-06-05 11:07:18 -0700370{
William Roberts8fb0f922015-10-01 22:02:15 -0700371 struct audit_data *ad = (struct audit_data *)data;
372
373 if (!ad || !ad->name) {
374 ALOGE("No service manager audit data");
375 return 0;
376 }
377
378 snprintf(buf, len, "service=%s pid=%d uid=%d", ad->name, ad->pid, ad->uid);
Riley Spahn69154df2014-06-05 11:07:18 -0700379 return 0;
380}
381
Martijn Coenen69b05152017-03-21 10:00:38 -0700382int main(int argc, char** argv)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700383{
384 struct binder_state *bs;
Sandeep Patil93ba7012016-12-27 12:40:45 -0800385 union selinux_callback cb;
Martijn Coenen69b05152017-03-21 10:00:38 -0700386 char *driver;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700387
Martijn Coenen69b05152017-03-21 10:00:38 -0700388 if (argc > 1) {
389 driver = argv[1];
390 } else {
391 driver = "/dev/binder";
392 }
393
394 bs = binder_open(driver, 128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000395 if (!bs) {
Martijn Coenen31361232017-03-31 16:12:12 -0700396#ifdef VENDORSERVICEMANAGER
397 ALOGW("failed to open binder driver %s\n", driver);
398 while (true) {
399 sleep(UINT_MAX);
400 }
401#else
Martijn Coenen69b05152017-03-21 10:00:38 -0700402 ALOGE("failed to open binder driver %s\n", driver);
Martijn Coenen31361232017-03-31 16:12:12 -0700403#endif
Serban Constantinescua44542c2014-01-30 15:16:45 +0000404 return -1;
405 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700406
407 if (binder_become_context_manager(bs)) {
408 ALOGE("cannot become context manager (%s)\n", strerror(errno));
409 return -1;
410 }
411
Sandeep Patil93ba7012016-12-27 12:40:45 -0800412 cb.func_audit = audit_callback;
413 selinux_set_callback(SELINUX_CB_AUDIT, cb);
Steven Moreland41b69be2018-09-06 16:52:07 -0700414#ifdef VENDORSERVICEMANAGER
415 cb.func_log = selinux_vendor_log_callback;
416#else
Sandeep Patil93ba7012016-12-27 12:40:45 -0800417 cb.func_log = selinux_log_callback;
Steven Moreland41b69be2018-09-06 16:52:07 -0700418#endif
Sandeep Patil93ba7012016-12-27 12:40:45 -0800419 selinux_set_callback(SELINUX_CB_LOG, cb);
420
Martijn Coenen31361232017-03-31 16:12:12 -0700421#ifdef VENDORSERVICEMANAGER
422 sehandle = selinux_android_vendor_service_context_handle();
423#else
Riley Spahn69154df2014-06-05 11:07:18 -0700424 sehandle = selinux_android_service_context_handle();
Martijn Coenen31361232017-03-31 16:12:12 -0700425#endif
Stephen Smalleybea07462015-06-03 09:25:37 -0400426 selinux_status_open(true);
Riley Spahn69154df2014-06-05 11:07:18 -0700427
Nick Kralevicheb4d5cb2016-12-09 17:05:09 -0800428 if (sehandle == NULL) {
429 ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n");
430 abort();
431 }
Riley Spahnc67e6302014-07-08 09:03:00 -0700432
Nick Kralevicheb4d5cb2016-12-09 17:05:09 -0800433 if (getcon(&service_manager_context) != 0) {
434 ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n");
435 abort();
Riley Spahnc67e6302014-07-08 09:03:00 -0700436 }
437
Riley Spahn69154df2014-06-05 11:07:18 -0700438
Mike Lockwood94afecf2012-10-24 10:45:23 -0700439 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000440
Mike Lockwood94afecf2012-10-24 10:45:23 -0700441 return 0;
442}