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