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