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 | |
| 11 | #include "binder.h" |
| 12 | |
| 13 | #if 0 |
| 14 | #define ALOGI(x...) fprintf(stderr, "svcmgr: " x) |
| 15 | #define ALOGE(x...) fprintf(stderr, "svcmgr: " x) |
| 16 | #else |
| 17 | #define LOG_TAG "ServiceManager" |
| 18 | #include <cutils/log.h> |
| 19 | #endif |
| 20 | |
| 21 | /* TODO: |
| 22 | * These should come from a config file or perhaps be |
| 23 | * based on some namespace rules of some sort (media |
| 24 | * uid can register media.*, etc) |
| 25 | */ |
| 26 | static struct { |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 27 | uid_t uid; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 28 | const char *name; |
| 29 | } allowed[] = { |
| 30 | { AID_MEDIA, "media.audio_flinger" }, |
Glenn Kasten | 64c8be0 | 2013-01-16 12:06:39 -0800 | [diff] [blame] | 31 | { AID_MEDIA, "media.log" }, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 32 | { AID_MEDIA, "media.player" }, |
| 33 | { AID_MEDIA, "media.camera" }, |
| 34 | { AID_MEDIA, "media.audio_policy" }, |
Mike Lockwood | 2a5fd8b | 2013-09-11 08:10:11 -0700 | [diff] [blame] | 35 | { AID_AUDIO, "audio" }, |
Mike Lockwood | 30a7d5a | 2013-09-13 07:26:05 -0700 | [diff] [blame] | 36 | { AID_INPUT, "input" }, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 37 | { AID_DRM, "drm.drmManager" }, |
| 38 | { AID_NFC, "nfc" }, |
| 39 | { AID_BLUETOOTH, "bluetooth" }, |
| 40 | { AID_RADIO, "radio.phone" }, |
| 41 | { AID_RADIO, "radio.sms" }, |
| 42 | { AID_RADIO, "radio.phonesubinfo" }, |
| 43 | { AID_RADIO, "radio.simphonebook" }, |
| 44 | /* TODO: remove after phone services are updated: */ |
| 45 | { AID_RADIO, "phone" }, |
| 46 | { AID_RADIO, "sip" }, |
| 47 | { AID_RADIO, "isms" }, |
| 48 | { AID_RADIO, "iphonesubinfo" }, |
| 49 | { AID_RADIO, "simphonebook" }, |
| 50 | { AID_MEDIA, "common_time.clock" }, |
| 51 | { AID_MEDIA, "common_time.config" }, |
Kenny Root | 2444087 | 2012-11-14 15:42:38 -0800 | [diff] [blame] | 52 | { AID_KEYSTORE, "android.security.keystore" }, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 55 | uint32_t svcmgr_handle; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 56 | |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 57 | const char *str8(const uint16_t *x) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 58 | { |
| 59 | static char buf[128]; |
| 60 | unsigned max = 127; |
| 61 | char *p = buf; |
| 62 | |
| 63 | if (x) { |
| 64 | while (*x && max--) { |
| 65 | *p++ = *x++; |
| 66 | } |
| 67 | } |
| 68 | *p++ = 0; |
| 69 | return buf; |
| 70 | } |
| 71 | |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 72 | int str16eq(const uint16_t *a, const char *b) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 73 | { |
| 74 | while (*a && *b) |
| 75 | if (*a++ != *b++) return 0; |
| 76 | if (*a || *b) |
| 77 | return 0; |
| 78 | return 1; |
| 79 | } |
| 80 | |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 81 | int svc_can_register(uid_t uid, const uint16_t *name) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 82 | { |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 83 | size_t n; |
| 84 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 85 | if ((uid == 0) || (uid == AID_SYSTEM)) |
| 86 | return 1; |
| 87 | |
| 88 | for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++) |
| 89 | if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name)) |
| 90 | return 1; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 95 | struct svcinfo |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 96 | { |
| 97 | struct svcinfo *next; |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 98 | uint32_t handle; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 99 | struct binder_death death; |
| 100 | int allow_isolated; |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 101 | size_t len; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 102 | uint16_t name[0]; |
| 103 | }; |
| 104 | |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 105 | struct svcinfo *svclist = NULL; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 106 | |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 107 | struct svcinfo *find_svc(const uint16_t *s16, size_t len) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 108 | { |
| 109 | struct svcinfo *si; |
| 110 | |
| 111 | for (si = svclist; si; si = si->next) { |
| 112 | if ((len == si->len) && |
| 113 | !memcmp(s16, si->name, len * sizeof(uint16_t))) { |
| 114 | return si; |
| 115 | } |
| 116 | } |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 117 | return NULL; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void svcinfo_death(struct binder_state *bs, void *ptr) |
| 121 | { |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 122 | struct svcinfo *si = (struct svcinfo* ) ptr; |
| 123 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 124 | ALOGI("service '%s' died\n", str8(si->name)); |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 125 | if (si->handle) { |
| 126 | binder_release(bs, si->handle); |
| 127 | si->handle = 0; |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 128 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 131 | uint16_t svcmgr_id[] = { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 132 | 'a','n','d','r','o','i','d','.','o','s','.', |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 133 | '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] | 134 | }; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 135 | |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 136 | |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 137 | 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] | 138 | { |
| 139 | struct svcinfo *si; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 140 | |
Serban Constantinescu | 3a345f0 | 2013-12-19 09:11:41 +0000 | [diff] [blame] | 141 | si = find_svc(s, len); |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 142 | //ALOGI("check_service('%s') handle = %x\n", str8(s), si ? si->handle : 0); |
| 143 | if (si && si->handle) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 144 | if (!si->allow_isolated) { |
| 145 | // If this service doesn't allow access from isolated processes, |
| 146 | // then check the uid to see if it is isolated. |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 147 | uid_t appid = uid % AID_USER; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 148 | if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) { |
| 149 | return 0; |
| 150 | } |
| 151 | } |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 152 | return si->handle; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 153 | } else { |
| 154 | return 0; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | int do_add_service(struct binder_state *bs, |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 159 | const uint16_t *s, size_t len, |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 160 | uint32_t handle, uid_t uid, int allow_isolated) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 161 | { |
| 162 | struct svcinfo *si; |
Serban Constantinescu | 3a345f0 | 2013-12-19 09:11:41 +0000 | [diff] [blame] | 163 | |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 164 | //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s), handle, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 165 | // allow_isolated ? "allow_isolated" : "!allow_isolated", uid); |
| 166 | |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 167 | if (!handle || (len == 0) || (len > 127)) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 168 | return -1; |
| 169 | |
| 170 | if (!svc_can_register(uid, s)) { |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 171 | ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n", |
| 172 | str8(s), handle, uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | si = find_svc(s, len); |
| 177 | if (si) { |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 178 | if (si->handle) { |
| 179 | ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n", |
| 180 | str8(s), handle, uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 181 | svcinfo_death(bs, si); |
| 182 | } |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 183 | si->handle = handle; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 184 | } else { |
| 185 | si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t)); |
| 186 | if (!si) { |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 187 | ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n", |
| 188 | str8(s), handle, uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 189 | return -1; |
| 190 | } |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 191 | si->handle = handle; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 192 | si->len = len; |
| 193 | memcpy(si->name, s, (len + 1) * sizeof(uint16_t)); |
| 194 | si->name[len] = '\0'; |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 195 | si->death.func = (void*) svcinfo_death; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 196 | si->death.ptr = si; |
| 197 | si->allow_isolated = allow_isolated; |
| 198 | si->next = svclist; |
| 199 | svclist = si; |
| 200 | } |
| 201 | |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 202 | binder_acquire(bs, handle); |
| 203 | binder_link_to_death(bs, handle, &si->death); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | int svcmgr_handler(struct binder_state *bs, |
Serban Constantinescu | bcf3888 | 2014-01-10 13:56:27 +0000 | [diff] [blame] | 208 | struct binder_transaction_data *txn, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 209 | struct binder_io *msg, |
| 210 | struct binder_io *reply) |
| 211 | { |
| 212 | struct svcinfo *si; |
| 213 | uint16_t *s; |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 214 | size_t len; |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 215 | uint32_t handle; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 216 | uint32_t strict_policy; |
| 217 | int allow_isolated; |
| 218 | |
Serban Constantinescu | 3a345f0 | 2013-12-19 09:11:41 +0000 | [diff] [blame] | 219 | //ALOGI("target=%x code=%d pid=%d uid=%d\n", |
| 220 | // txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 221 | |
Serban Constantinescu | bcf3888 | 2014-01-10 13:56:27 +0000 | [diff] [blame] | 222 | if (txn->target.handle != svcmgr_handle) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 223 | return -1; |
| 224 | |
Arve Hjønnevåg | e5245cb | 2014-01-28 21:35:03 -0800 | [diff] [blame] | 225 | if (txn->code == PING_TRANSACTION) |
| 226 | return 0; |
| 227 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 228 | // Equivalent to Parcel::enforceInterface(), reading the RPC |
| 229 | // header with the strict mode policy mask and the interface name. |
| 230 | // Note that we ignore the strict_policy and don't propagate it |
| 231 | // further (since we do no outbound RPCs anyway). |
| 232 | strict_policy = bio_get_uint32(msg); |
| 233 | s = bio_get_string16(msg, &len); |
| 234 | if ((len != (sizeof(svcmgr_id) / 2)) || |
| 235 | memcmp(svcmgr_id, s, sizeof(svcmgr_id))) { |
| 236 | fprintf(stderr,"invalid id %s\n", str8(s)); |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | switch(txn->code) { |
| 241 | case SVC_MGR_GET_SERVICE: |
| 242 | case SVC_MGR_CHECK_SERVICE: |
| 243 | s = bio_get_string16(msg, &len); |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 244 | handle = do_find_service(bs, s, len, txn->sender_euid); |
| 245 | if (!handle) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 246 | break; |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 247 | bio_put_ref(reply, handle); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 248 | return 0; |
| 249 | |
| 250 | case SVC_MGR_ADD_SERVICE: |
| 251 | s = bio_get_string16(msg, &len); |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 252 | handle = bio_get_ref(msg); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 253 | allow_isolated = bio_get_uint32(msg) ? 1 : 0; |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 254 | if (do_add_service(bs, s, len, handle, txn->sender_euid, allow_isolated)) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 255 | return -1; |
| 256 | break; |
| 257 | |
| 258 | case SVC_MGR_LIST_SERVICES: { |
Serban Constantinescu | 3a345f0 | 2013-12-19 09:11:41 +0000 | [diff] [blame] | 259 | uint32_t n = bio_get_uint32(msg); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 260 | |
| 261 | si = svclist; |
| 262 | while ((n-- > 0) && si) |
| 263 | si = si->next; |
| 264 | if (si) { |
| 265 | bio_put_string16(reply, si->name); |
| 266 | return 0; |
| 267 | } |
| 268 | return -1; |
| 269 | } |
| 270 | default: |
| 271 | ALOGE("unknown code %d\n", txn->code); |
| 272 | return -1; |
| 273 | } |
| 274 | |
| 275 | bio_put_uint32(reply, 0); |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | int main(int argc, char **argv) |
| 280 | { |
| 281 | struct binder_state *bs; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 282 | |
| 283 | bs = binder_open(128*1024); |
Serban Constantinescu | a44542c | 2014-01-30 15:16:45 +0000 | [diff] [blame] | 284 | if (!bs) { |
| 285 | ALOGE("failed to open binder driver\n"); |
| 286 | return -1; |
| 287 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 288 | |
| 289 | if (binder_become_context_manager(bs)) { |
| 290 | ALOGE("cannot become context manager (%s)\n", strerror(errno)); |
| 291 | return -1; |
| 292 | } |
| 293 | |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 294 | svcmgr_handle = BINDER_SERVICE_MANAGER; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 295 | binder_loop(bs, svcmgr_handler); |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 296 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 297 | return 0; |
| 298 | } |