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