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