| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1 | /* Copyright 2008 The Android Open Source Project | 
|  | 2 | */ | 
|  | 3 |  | 
|  | 4 | #include <stdio.h> | 
|  | 5 | #include <stdlib.h> | 
|  | 6 | #include <errno.h> | 
|  | 7 | #include <fcntl.h> | 
|  | 8 |  | 
|  | 9 | #include <private/android_filesystem_config.h> | 
|  | 10 |  | 
| Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 11 | #include <selinux/android.h> | 
|  | 12 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 13 | #include "binder.h" | 
|  | 14 |  | 
|  | 15 | #if 0 | 
|  | 16 | #define ALOGI(x...) fprintf(stderr, "svcmgr: " x) | 
|  | 17 | #define ALOGE(x...) fprintf(stderr, "svcmgr: " x) | 
|  | 18 | #else | 
|  | 19 | #define LOG_TAG "ServiceManager" | 
|  | 20 | #include <cutils/log.h> | 
|  | 21 | #endif | 
|  | 22 |  | 
|  | 23 | /* TODO: | 
|  | 24 | * These should come from a config file or perhaps be | 
|  | 25 | * based on some namespace rules of some sort (media | 
|  | 26 | * uid can register media.*, etc) | 
|  | 27 | */ | 
|  | 28 | static struct { | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 29 | uid_t uid; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 30 | const char *name; | 
|  | 31 | } allowed[] = { | 
|  | 32 | { AID_MEDIA, "media.audio_flinger" }, | 
| Glenn Kasten | 64c8be0 | 2013-01-16 12:06:39 -0800 | [diff] [blame] | 33 | { AID_MEDIA, "media.log" }, | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 34 | { AID_MEDIA, "media.player" }, | 
|  | 35 | { AID_MEDIA, "media.camera" }, | 
|  | 36 | { AID_MEDIA, "media.audio_policy" }, | 
| Eric Laurent | 9882be8 | 2014-04-18 17:52:57 -0700 | [diff] [blame] | 37 | { AID_MEDIA, "media.sound_trigger_hw" }, | 
| Mike Lockwood | 2a5fd8b | 2013-09-11 08:10:11 -0700 | [diff] [blame] | 38 | { AID_AUDIO, "audio" }, | 
| Mike Lockwood | 30a7d5a | 2013-09-13 07:26:05 -0700 | [diff] [blame] | 39 | { AID_INPUT, "input" }, | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 40 | { AID_DRM,   "drm.drmManager" }, | 
|  | 41 | { AID_NFC,   "nfc" }, | 
|  | 42 | { AID_BLUETOOTH, "bluetooth" }, | 
|  | 43 | { AID_RADIO, "radio.phone" }, | 
|  | 44 | { AID_RADIO, "radio.sms" }, | 
|  | 45 | { AID_RADIO, "radio.phonesubinfo" }, | 
|  | 46 | { AID_RADIO, "radio.simphonebook" }, | 
|  | 47 | /* TODO: remove after phone services are updated: */ | 
|  | 48 | { AID_RADIO, "phone" }, | 
|  | 49 | { AID_RADIO, "sip" }, | 
|  | 50 | { AID_RADIO, "isms" }, | 
|  | 51 | { AID_RADIO, "iphonesubinfo" }, | 
|  | 52 | { AID_RADIO, "simphonebook" }, | 
|  | 53 | { AID_MEDIA, "common_time.clock" }, | 
|  | 54 | { AID_MEDIA, "common_time.config" }, | 
| Kenny Root | 2444087 | 2012-11-14 15:42:38 -0800 | [diff] [blame] | 55 | { AID_KEYSTORE, "android.security.keystore" }, | 
| Santos Cordon | 3fb4de7 | 2014-05-21 22:58:55 -0700 | [diff] [blame] | 56 | { AID_RADIO, "telecomm" }, | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 57 | }; | 
|  | 58 |  | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 59 | uint32_t svcmgr_handle; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 60 |  | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 61 | const char *str8(const uint16_t *x) | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 62 | { | 
|  | 63 | static char buf[128]; | 
|  | 64 | unsigned max = 127; | 
|  | 65 | char *p = buf; | 
|  | 66 |  | 
|  | 67 | if (x) { | 
|  | 68 | while (*x && max--) { | 
|  | 69 | *p++ = *x++; | 
|  | 70 | } | 
|  | 71 | } | 
|  | 72 | *p++ = 0; | 
|  | 73 | return buf; | 
|  | 74 | } | 
|  | 75 |  | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 76 | int str16eq(const uint16_t *a, const char *b) | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 77 | { | 
|  | 78 | while (*a && *b) | 
|  | 79 | if (*a++ != *b++) return 0; | 
|  | 80 | if (*a || *b) | 
|  | 81 | return 0; | 
|  | 82 | return 1; | 
|  | 83 | } | 
|  | 84 |  | 
| Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 85 | static struct selabel_handle* sehandle; | 
|  | 86 |  | 
|  | 87 | static bool check_mac_perms(const char *name, pid_t spid) | 
|  | 88 | { | 
|  | 89 | if (is_selinux_enabled() <= 0) { | 
|  | 90 | return true; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | bool allowed = false; | 
|  | 94 |  | 
|  | 95 | const char *class = "service_manager"; | 
|  | 96 | const char *perm = "add"; | 
|  | 97 |  | 
|  | 98 | char *tctx = NULL; | 
|  | 99 | char *sctx = NULL; | 
|  | 100 |  | 
|  | 101 | if (!sehandle) { | 
|  | 102 | ALOGE("SELinux: Failed to find sehandle %s.\n", name); | 
|  | 103 | return false; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | if (getpidcon(spid, &sctx) < 0) { | 
|  | 107 | ALOGE("SELinux: getpidcon failed to retrieve pid context.\n"); | 
|  | 108 | return false; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | if (!sctx) { | 
|  | 112 | ALOGE("SELinux: Failed to find sctx for %s.\n", name); | 
|  | 113 | return false; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | if (selabel_lookup(sehandle, &tctx, name, 1) != 0) { | 
|  | 117 | ALOGE("SELinux: selabel_lookup failed to set tctx for %s.\n", name); | 
|  | 118 | freecon(sctx); | 
|  | 119 | return false; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | if (!tctx) { | 
|  | 123 | ALOGE("SELinux: Failed to find tctx for %s.\n", name); | 
|  | 124 | freecon(sctx); | 
|  | 125 | return false; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | int result = selinux_check_access(sctx, tctx, class, perm, (void *) name); | 
|  | 129 | allowed = (result == 0); | 
|  | 130 |  | 
|  | 131 | freecon(sctx); | 
|  | 132 | freecon(tctx); | 
|  | 133 | return allowed; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | static int svc_can_register(uid_t uid, const uint16_t *name, pid_t spid) | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 137 | { | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 138 | size_t n; | 
|  | 139 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 140 | if ((uid == 0) || (uid == AID_SYSTEM)) | 
| Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 141 | return check_mac_perms(str8(name), spid) ? 1 : 0; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 142 |  | 
|  | 143 | for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++) | 
|  | 144 | if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name)) | 
| Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 145 | return check_mac_perms(str8(name), spid) ? 1 : 0; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 146 |  | 
|  | 147 | return 0; | 
|  | 148 | } | 
|  | 149 |  | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 150 | struct svcinfo | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 151 | { | 
|  | 152 | struct svcinfo *next; | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 153 | uint32_t handle; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 154 | struct binder_death death; | 
|  | 155 | int allow_isolated; | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 156 | size_t len; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 157 | uint16_t name[0]; | 
|  | 158 | }; | 
|  | 159 |  | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 160 | struct svcinfo *svclist = NULL; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 161 |  | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 162 | struct svcinfo *find_svc(const uint16_t *s16, size_t len) | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 163 | { | 
|  | 164 | struct svcinfo *si; | 
|  | 165 |  | 
|  | 166 | for (si = svclist; si; si = si->next) { | 
|  | 167 | if ((len == si->len) && | 
|  | 168 | !memcmp(s16, si->name, len * sizeof(uint16_t))) { | 
|  | 169 | return si; | 
|  | 170 | } | 
|  | 171 | } | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 172 | return NULL; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 173 | } | 
|  | 174 |  | 
|  | 175 | void svcinfo_death(struct binder_state *bs, void *ptr) | 
|  | 176 | { | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 177 | struct svcinfo *si = (struct svcinfo* ) ptr; | 
|  | 178 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 179 | ALOGI("service '%s' died\n", str8(si->name)); | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 180 | if (si->handle) { | 
|  | 181 | binder_release(bs, si->handle); | 
|  | 182 | si->handle = 0; | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 183 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 184 | } | 
|  | 185 |  | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 186 | uint16_t svcmgr_id[] = { | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 187 | 'a','n','d','r','o','i','d','.','o','s','.', | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 188 | 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r' | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 189 | }; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 190 |  | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 191 |  | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 192 | uint32_t do_find_service(struct binder_state *bs, const uint16_t *s, size_t len, uid_t uid) | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 193 | { | 
|  | 194 | struct svcinfo *si; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 195 |  | 
| Serban Constantinescu | 3a345f0 | 2013-12-19 09:11:41 +0000 | [diff] [blame] | 196 | si = find_svc(s, len); | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 197 | //ALOGI("check_service('%s') handle = %x\n", str8(s), si ? si->handle : 0); | 
|  | 198 | if (si && si->handle) { | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 199 | if (!si->allow_isolated) { | 
|  | 200 | // If this service doesn't allow access from isolated processes, | 
|  | 201 | // then check the uid to see if it is isolated. | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 202 | uid_t appid = uid % AID_USER; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 203 | if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) { | 
|  | 204 | return 0; | 
|  | 205 | } | 
|  | 206 | } | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 207 | return si->handle; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 208 | } else { | 
|  | 209 | return 0; | 
|  | 210 | } | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | int do_add_service(struct binder_state *bs, | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 214 | const uint16_t *s, size_t len, | 
| Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 215 | uint32_t handle, uid_t uid, int allow_isolated, | 
|  | 216 | pid_t spid) | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 217 | { | 
|  | 218 | struct svcinfo *si; | 
| Serban Constantinescu | 3a345f0 | 2013-12-19 09:11:41 +0000 | [diff] [blame] | 219 |  | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 220 | //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s), handle, | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 221 | //        allow_isolated ? "allow_isolated" : "!allow_isolated", uid); | 
|  | 222 |  | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 223 | if (!handle || (len == 0) || (len > 127)) | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 224 | return -1; | 
|  | 225 |  | 
| Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 226 | if (!svc_can_register(uid, s, spid)) { | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 227 | ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n", | 
|  | 228 | str8(s), handle, uid); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 229 | return -1; | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | si = find_svc(s, len); | 
|  | 233 | if (si) { | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 234 | if (si->handle) { | 
|  | 235 | ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n", | 
|  | 236 | str8(s), handle, uid); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 237 | svcinfo_death(bs, si); | 
|  | 238 | } | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 239 | si->handle = handle; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 240 | } else { | 
|  | 241 | si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t)); | 
|  | 242 | if (!si) { | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 243 | ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n", | 
|  | 244 | str8(s), handle, uid); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 245 | return -1; | 
|  | 246 | } | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 247 | si->handle = handle; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 248 | si->len = len; | 
|  | 249 | memcpy(si->name, s, (len + 1) * sizeof(uint16_t)); | 
|  | 250 | si->name[len] = '\0'; | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 251 | si->death.func = (void*) svcinfo_death; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 252 | si->death.ptr = si; | 
|  | 253 | si->allow_isolated = allow_isolated; | 
|  | 254 | si->next = svclist; | 
|  | 255 | svclist = si; | 
|  | 256 | } | 
|  | 257 |  | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 258 | binder_acquire(bs, handle); | 
|  | 259 | binder_link_to_death(bs, handle, &si->death); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 260 | return 0; | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | int svcmgr_handler(struct binder_state *bs, | 
| Serban Constantinescu | bcf3888 | 2014-01-10 13:56:27 +0000 | [diff] [blame] | 264 | struct binder_transaction_data *txn, | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 265 | struct binder_io *msg, | 
|  | 266 | struct binder_io *reply) | 
|  | 267 | { | 
|  | 268 | struct svcinfo *si; | 
|  | 269 | uint16_t *s; | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 270 | size_t len; | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 271 | uint32_t handle; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 272 | uint32_t strict_policy; | 
|  | 273 | int allow_isolated; | 
|  | 274 |  | 
| Serban Constantinescu | 3a345f0 | 2013-12-19 09:11:41 +0000 | [diff] [blame] | 275 | //ALOGI("target=%x code=%d pid=%d uid=%d\n", | 
|  | 276 | //  txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 277 |  | 
| Serban Constantinescu | bcf3888 | 2014-01-10 13:56:27 +0000 | [diff] [blame] | 278 | if (txn->target.handle != svcmgr_handle) | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 279 | return -1; | 
|  | 280 |  | 
| Arve Hjønnevåg | e5245cb | 2014-01-28 21:35:03 -0800 | [diff] [blame] | 281 | if (txn->code == PING_TRANSACTION) | 
|  | 282 | return 0; | 
|  | 283 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 284 | // Equivalent to Parcel::enforceInterface(), reading the RPC | 
|  | 285 | // header with the strict mode policy mask and the interface name. | 
|  | 286 | // Note that we ignore the strict_policy and don't propagate it | 
|  | 287 | // further (since we do no outbound RPCs anyway). | 
|  | 288 | strict_policy = bio_get_uint32(msg); | 
|  | 289 | s = bio_get_string16(msg, &len); | 
|  | 290 | if ((len != (sizeof(svcmgr_id) / 2)) || | 
|  | 291 | memcmp(svcmgr_id, s, sizeof(svcmgr_id))) { | 
|  | 292 | fprintf(stderr,"invalid id %s\n", str8(s)); | 
|  | 293 | return -1; | 
|  | 294 | } | 
|  | 295 |  | 
| Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 296 | if (sehandle && selinux_status_updated() > 0) { | 
|  | 297 | struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle(); | 
|  | 298 | if (tmp_sehandle) { | 
|  | 299 | selabel_close(sehandle); | 
|  | 300 | sehandle = tmp_sehandle; | 
|  | 301 | } | 
|  | 302 | } | 
|  | 303 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 304 | switch(txn->code) { | 
|  | 305 | case SVC_MGR_GET_SERVICE: | 
|  | 306 | case SVC_MGR_CHECK_SERVICE: | 
|  | 307 | s = bio_get_string16(msg, &len); | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 308 | handle = do_find_service(bs, s, len, txn->sender_euid); | 
|  | 309 | if (!handle) | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 310 | break; | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 311 | bio_put_ref(reply, handle); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 312 | return 0; | 
|  | 313 |  | 
|  | 314 | case SVC_MGR_ADD_SERVICE: | 
|  | 315 | s = bio_get_string16(msg, &len); | 
| Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 316 | handle = bio_get_ref(msg); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 317 | allow_isolated = bio_get_uint32(msg) ? 1 : 0; | 
| Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 318 | if (do_add_service(bs, s, len, handle, txn->sender_euid, | 
|  | 319 | allow_isolated, txn->sender_pid)) | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 320 | return -1; | 
|  | 321 | break; | 
|  | 322 |  | 
|  | 323 | case SVC_MGR_LIST_SERVICES: { | 
| Serban Constantinescu | 3a345f0 | 2013-12-19 09:11:41 +0000 | [diff] [blame] | 324 | uint32_t n = bio_get_uint32(msg); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 325 |  | 
|  | 326 | si = svclist; | 
|  | 327 | while ((n-- > 0) && si) | 
|  | 328 | si = si->next; | 
|  | 329 | if (si) { | 
|  | 330 | bio_put_string16(reply, si->name); | 
|  | 331 | return 0; | 
|  | 332 | } | 
|  | 333 | return -1; | 
|  | 334 | } | 
|  | 335 | default: | 
|  | 336 | ALOGE("unknown code %d\n", txn->code); | 
|  | 337 | return -1; | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 | bio_put_uint32(reply, 0); | 
|  | 341 | return 0; | 
|  | 342 | } | 
|  | 343 |  | 
| Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 344 |  | 
|  | 345 | static int audit_callback(void *data, security_class_t cls, char *buf, size_t len) | 
|  | 346 | { | 
|  | 347 | snprintf(buf, len, "service=%s", !data ? "NULL" : (char *)data); | 
|  | 348 | return 0; | 
|  | 349 | } | 
|  | 350 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 351 | int main(int argc, char **argv) | 
|  | 352 | { | 
|  | 353 | struct binder_state *bs; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 354 |  | 
|  | 355 | bs = binder_open(128*1024); | 
| Serban Constantinescu | a44542c | 2014-01-30 15:16:45 +0000 | [diff] [blame] | 356 | if (!bs) { | 
|  | 357 | ALOGE("failed to open binder driver\n"); | 
|  | 358 | return -1; | 
|  | 359 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 360 |  | 
|  | 361 | if (binder_become_context_manager(bs)) { | 
|  | 362 | ALOGE("cannot become context manager (%s)\n", strerror(errno)); | 
|  | 363 | return -1; | 
|  | 364 | } | 
|  | 365 |  | 
| Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 366 | sehandle = selinux_android_service_context_handle(); | 
|  | 367 |  | 
|  | 368 | union selinux_callback cb; | 
|  | 369 | cb.func_audit = audit_callback; | 
|  | 370 | selinux_set_callback(SELINUX_CB_AUDIT, cb); | 
|  | 371 | cb.func_log = selinux_log_callback; | 
|  | 372 | selinux_set_callback(SELINUX_CB_LOG, cb); | 
|  | 373 |  | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 374 | svcmgr_handle = BINDER_SERVICE_MANAGER; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 375 | binder_loop(bs, svcmgr_handler); | 
| Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 376 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 377 | return 0; | 
|  | 378 | } |