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 | |
Yifan Hong | 212c881 | 2017-08-01 17:09:07 -0700 | [diff] [blame] | 11 | #include <cutils/android_filesystem_config.h> |
Arve Hjønnevåg | 6b9c6d2 | 2016-08-18 15:42:35 -0700 | [diff] [blame] | 12 | #include <cutils/multiuser.h> |
| 13 | |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 14 | #include <selinux/android.h> |
Nick Kralevich | 7d42a3c | 2014-07-12 16:34:01 -0700 | [diff] [blame] | 15 | #include <selinux/avc.h> |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 16 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 17 | #include "binder.h" |
| 18 | |
Martijn Coenen | 3136123 | 2017-03-31 16:12:12 -0700 | [diff] [blame] | 19 | #ifdef VENDORSERVICEMANAGER |
| 20 | #define LOG_TAG "VendorServiceManager" |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 21 | #else |
| 22 | #define LOG_TAG "ServiceManager" |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 23 | #endif |
Martijn Coenen | 3136123 | 2017-03-31 16:12:12 -0700 | [diff] [blame] | 24 | #include <log/log.h> |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 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 char *service_manager_context; |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 62 | static struct selabel_handle* sehandle; |
| 63 | |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 64 | 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] | 65 | { |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 66 | char *sctx = NULL; |
Riley Spahn | c67e630 | 2014-07-08 09:03:00 -0700 | [diff] [blame] | 67 | const char *class = "service_manager"; |
| 68 | bool allowed; |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 69 | struct audit_data ad; |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 70 | |
| 71 | if (getpidcon(spid, &sctx) < 0) { |
Riley Spahn | c67e630 | 2014-07-08 09:03:00 -0700 | [diff] [blame] | 72 | ALOGE("SELinux: getpidcon(pid=%d) failed to retrieve pid context.\n", spid); |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 76 | ad.pid = spid; |
| 77 | ad.uid = uid; |
| 78 | ad.name = name; |
| 79 | |
| 80 | int result = selinux_check_access(sctx, tctx, class, perm, (void *) &ad); |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 81 | allowed = (result == 0); |
| 82 | |
| 83 | freecon(sctx); |
Riley Spahn | c67e630 | 2014-07-08 09:03:00 -0700 | [diff] [blame] | 84 | return allowed; |
| 85 | } |
| 86 | |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 87 | 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] | 88 | { |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 89 | return check_mac_perms(spid, uid, service_manager_context, perm, NULL); |
Riley Spahn | c67e630 | 2014-07-08 09:03:00 -0700 | [diff] [blame] | 90 | } |
| 91 | |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 92 | 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] | 93 | { |
| 94 | bool allowed; |
| 95 | char *tctx = NULL; |
| 96 | |
Riley Spahn | c67e630 | 2014-07-08 09:03:00 -0700 | [diff] [blame] | 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 | |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 107 | allowed = check_mac_perms(spid, uid, tctx, perm, name); |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 108 | freecon(tctx); |
| 109 | return allowed; |
| 110 | } |
| 111 | |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -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 | 5fa90a0 | 2016-08-01 16:05:17 -0700 | [diff] [blame] | 115 | |
Arve Hjønnevåg | 6b9c6d2 | 2016-08-18 15:42:35 -0700 | [diff] [blame] | 116 | if (multiuser_get_app_id(uid) >= AID_APP) { |
Arve Hjønnevåg | 5fa90a0 | 2016-08-01 16:05:17 -0700 | [diff] [blame] | 117 | return 0; /* Don't allow apps to register services */ |
| 118 | } |
| 119 | |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 120 | 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] | 121 | } |
| 122 | |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 123 | static int svc_can_list(pid_t spid, uid_t uid) |
Riley Spahn | c67e630 | 2014-07-08 09:03:00 -0700 | [diff] [blame] | 124 | { |
| 125 | const char *perm = "list"; |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 126 | return check_mac_perms_from_getcon(spid, uid, perm) ? 1 : 0; |
Riley Spahn | c67e630 | 2014-07-08 09:03:00 -0700 | [diff] [blame] | 127 | } |
| 128 | |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 129 | 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] | 130 | { |
| 131 | const char *perm = "find"; |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 132 | 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] | 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; |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame^] | 141 | uint32_t dumpsys_priority; |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 142 | size_t len; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 143 | uint16_t name[0]; |
| 144 | }; |
| 145 | |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 146 | struct svcinfo *svclist = NULL; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 147 | |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 148 | struct svcinfo *find_svc(const uint16_t *s16, size_t len) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 149 | { |
| 150 | struct svcinfo *si; |
| 151 | |
| 152 | for (si = svclist; si; si = si->next) { |
| 153 | if ((len == si->len) && |
| 154 | !memcmp(s16, si->name, len * sizeof(uint16_t))) { |
| 155 | return si; |
| 156 | } |
| 157 | } |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 158 | return NULL; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void svcinfo_death(struct binder_state *bs, void *ptr) |
| 162 | { |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 163 | struct svcinfo *si = (struct svcinfo* ) ptr; |
| 164 | |
Nick Kralevich | 7d42a3c | 2014-07-12 16:34:01 -0700 | [diff] [blame] | 165 | ALOGI("service '%s' died\n", str8(si->name, si->len)); |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 166 | if (si->handle) { |
| 167 | binder_release(bs, si->handle); |
| 168 | si->handle = 0; |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 169 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 172 | uint16_t svcmgr_id[] = { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 173 | 'a','n','d','r','o','i','d','.','o','s','.', |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 174 | '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] | 175 | }; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 176 | |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 177 | |
Ian Pedowitz | d57d9b9 | 2016-02-19 08:34:43 +0000 | [diff] [blame] | 178 | 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] | 179 | { |
Nick Kralevich | b27bbd1 | 2015-03-05 10:58:40 -0800 | [diff] [blame] | 180 | struct svcinfo *si = find_svc(s, len); |
| 181 | |
| 182 | if (!si || !si->handle) { |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | if (!si->allow_isolated) { |
| 187 | // If this service doesn't allow access from isolated processes, |
| 188 | // then check the uid to see if it is isolated. |
| 189 | uid_t appid = uid % AID_USER; |
| 190 | if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) { |
| 191 | return 0; |
| 192 | } |
| 193 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 194 | |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 195 | if (!svc_can_find(s, len, spid, uid)) { |
Riley Spahn | c67e630 | 2014-07-08 09:03:00 -0700 | [diff] [blame] | 196 | return 0; |
| 197 | } |
Nick Kralevich | b27bbd1 | 2015-03-05 10:58:40 -0800 | [diff] [blame] | 198 | |
| 199 | return si->handle; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame^] | 202 | int do_add_service(struct binder_state *bs, const uint16_t *s, size_t len, uint32_t handle, |
| 203 | uid_t uid, int allow_isolated, uint32_t dumpsys_priority, pid_t spid) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 204 | struct svcinfo *si; |
Serban Constantinescu | 3a345f0 | 2013-12-19 09:11:41 +0000 | [diff] [blame] | 205 | |
Nick Kralevich | 7d42a3c | 2014-07-12 16:34:01 -0700 | [diff] [blame] | 206 | //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] | 207 | // allow_isolated ? "allow_isolated" : "!allow_isolated", uid); |
| 208 | |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 209 | if (!handle || (len == 0) || (len > 127)) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 210 | return -1; |
| 211 | |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 212 | if (!svc_can_register(s, len, spid, uid)) { |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 213 | ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n", |
Nick Kralevich | 7d42a3c | 2014-07-12 16:34:01 -0700 | [diff] [blame] | 214 | str8(s, len), handle, uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | si = find_svc(s, len); |
| 219 | if (si) { |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 220 | if (si->handle) { |
| 221 | ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n", |
Nick Kralevich | 7d42a3c | 2014-07-12 16:34:01 -0700 | [diff] [blame] | 222 | str8(s, len), handle, uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 223 | svcinfo_death(bs, si); |
| 224 | } |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 225 | si->handle = handle; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 226 | } else { |
| 227 | si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t)); |
| 228 | if (!si) { |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 229 | ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n", |
Nick Kralevich | 7d42a3c | 2014-07-12 16:34:01 -0700 | [diff] [blame] | 230 | str8(s, len), handle, uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 231 | return -1; |
| 232 | } |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 233 | si->handle = handle; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 234 | si->len = len; |
| 235 | memcpy(si->name, s, (len + 1) * sizeof(uint16_t)); |
| 236 | si->name[len] = '\0'; |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 237 | si->death.func = (void*) svcinfo_death; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 238 | si->death.ptr = si; |
| 239 | si->allow_isolated = allow_isolated; |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame^] | 240 | si->dumpsys_priority = dumpsys_priority; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 241 | si->next = svclist; |
| 242 | svclist = si; |
| 243 | } |
| 244 | |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 245 | binder_acquire(bs, handle); |
| 246 | binder_link_to_death(bs, handle, &si->death); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | int svcmgr_handler(struct binder_state *bs, |
Serban Constantinescu | bcf3888 | 2014-01-10 13:56:27 +0000 | [diff] [blame] | 251 | struct binder_transaction_data *txn, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 252 | struct binder_io *msg, |
| 253 | struct binder_io *reply) |
| 254 | { |
| 255 | struct svcinfo *si; |
| 256 | uint16_t *s; |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 257 | size_t len; |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 258 | uint32_t handle; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 259 | uint32_t strict_policy; |
| 260 | int allow_isolated; |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame^] | 261 | uint32_t dumpsys_priority; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 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) { |
Kouji Shiotani | 61f8dfa | 2017-06-13 15:40:54 +0900 | [diff] [blame] | 289 | #ifdef VENDORSERVICEMANAGER |
| 290 | struct selabel_handle *tmp_sehandle = selinux_android_vendor_service_context_handle(); |
| 291 | #else |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 292 | struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle(); |
Kouji Shiotani | 61f8dfa | 2017-06-13 15:40:54 +0900 | [diff] [blame] | 293 | #endif |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 294 | if (tmp_sehandle) { |
| 295 | selabel_close(sehandle); |
| 296 | sehandle = tmp_sehandle; |
| 297 | } |
| 298 | } |
| 299 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 300 | switch(txn->code) { |
| 301 | case SVC_MGR_GET_SERVICE: |
| 302 | case SVC_MGR_CHECK_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 | } |
Ian Pedowitz | d57d9b9 | 2016-02-19 08:34:43 +0000 | [diff] [blame] | 307 | handle = do_find_service(s, len, txn->sender_euid, txn->sender_pid); |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 308 | if (!handle) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 309 | break; |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 310 | bio_put_ref(reply, handle); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 311 | return 0; |
| 312 | |
| 313 | case SVC_MGR_ADD_SERVICE: |
| 314 | s = bio_get_string16(msg, &len); |
Nick Kralevich | 7d42a3c | 2014-07-12 16:34:01 -0700 | [diff] [blame] | 315 | if (s == NULL) { |
| 316 | return -1; |
| 317 | } |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 318 | handle = bio_get_ref(msg); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 319 | allow_isolated = bio_get_uint32(msg) ? 1 : 0; |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame^] | 320 | dumpsys_priority = bio_get_uint32(msg); |
| 321 | if (do_add_service(bs, s, len, handle, txn->sender_euid, allow_isolated, dumpsys_priority, |
| 322 | txn->sender_pid)) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 323 | return -1; |
| 324 | break; |
| 325 | |
| 326 | case SVC_MGR_LIST_SERVICES: { |
Serban Constantinescu | 3a345f0 | 2013-12-19 09:11:41 +0000 | [diff] [blame] | 327 | uint32_t n = bio_get_uint32(msg); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame^] | 328 | uint32_t req_dumpsys_priority = bio_get_uint32(msg); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 329 | |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 330 | if (!svc_can_list(txn->sender_pid, txn->sender_euid)) { |
Riley Spahn | c67e630 | 2014-07-08 09:03:00 -0700 | [diff] [blame] | 331 | ALOGE("list_service() uid=%d - PERMISSION DENIED\n", |
| 332 | txn->sender_euid); |
| 333 | return -1; |
| 334 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 335 | si = svclist; |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame^] | 336 | // walk through the list of services n times skipping services that |
| 337 | // do not support the requested priority |
| 338 | while (si) { |
| 339 | if (si->dumpsys_priority & req_dumpsys_priority) { |
| 340 | if (n == 0) break; |
| 341 | n--; |
| 342 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 343 | si = si->next; |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame^] | 344 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 345 | if (si) { |
| 346 | bio_put_string16(reply, si->name); |
| 347 | return 0; |
| 348 | } |
| 349 | return -1; |
| 350 | } |
| 351 | default: |
| 352 | ALOGE("unknown code %d\n", txn->code); |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | bio_put_uint32(reply, 0); |
| 357 | return 0; |
| 358 | } |
| 359 | |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 360 | |
Ian Pedowitz | d57d9b9 | 2016-02-19 08:34:43 +0000 | [diff] [blame] | 361 | 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] | 362 | { |
William Roberts | 8fb0f92 | 2015-10-01 22:02:15 -0700 | [diff] [blame] | 363 | struct audit_data *ad = (struct audit_data *)data; |
| 364 | |
| 365 | if (!ad || !ad->name) { |
| 366 | ALOGE("No service manager audit data"); |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | 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] | 371 | return 0; |
| 372 | } |
| 373 | |
Martijn Coenen | 69b0515 | 2017-03-21 10:00:38 -0700 | [diff] [blame] | 374 | int main(int argc, char** argv) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 375 | { |
| 376 | struct binder_state *bs; |
Sandeep Patil | 93ba701 | 2016-12-27 12:40:45 -0800 | [diff] [blame] | 377 | union selinux_callback cb; |
Martijn Coenen | 69b0515 | 2017-03-21 10:00:38 -0700 | [diff] [blame] | 378 | char *driver; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 379 | |
Martijn Coenen | 69b0515 | 2017-03-21 10:00:38 -0700 | [diff] [blame] | 380 | if (argc > 1) { |
| 381 | driver = argv[1]; |
| 382 | } else { |
| 383 | driver = "/dev/binder"; |
| 384 | } |
| 385 | |
| 386 | bs = binder_open(driver, 128*1024); |
Serban Constantinescu | a44542c | 2014-01-30 15:16:45 +0000 | [diff] [blame] | 387 | if (!bs) { |
Martijn Coenen | 3136123 | 2017-03-31 16:12:12 -0700 | [diff] [blame] | 388 | #ifdef VENDORSERVICEMANAGER |
| 389 | ALOGW("failed to open binder driver %s\n", driver); |
| 390 | while (true) { |
| 391 | sleep(UINT_MAX); |
| 392 | } |
| 393 | #else |
Martijn Coenen | 69b0515 | 2017-03-21 10:00:38 -0700 | [diff] [blame] | 394 | ALOGE("failed to open binder driver %s\n", driver); |
Martijn Coenen | 3136123 | 2017-03-31 16:12:12 -0700 | [diff] [blame] | 395 | #endif |
Serban Constantinescu | a44542c | 2014-01-30 15:16:45 +0000 | [diff] [blame] | 396 | return -1; |
| 397 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 398 | |
| 399 | if (binder_become_context_manager(bs)) { |
| 400 | ALOGE("cannot become context manager (%s)\n", strerror(errno)); |
| 401 | return -1; |
| 402 | } |
| 403 | |
Sandeep Patil | 93ba701 | 2016-12-27 12:40:45 -0800 | [diff] [blame] | 404 | cb.func_audit = audit_callback; |
| 405 | selinux_set_callback(SELINUX_CB_AUDIT, cb); |
| 406 | cb.func_log = selinux_log_callback; |
| 407 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 408 | |
Martijn Coenen | 3136123 | 2017-03-31 16:12:12 -0700 | [diff] [blame] | 409 | #ifdef VENDORSERVICEMANAGER |
| 410 | sehandle = selinux_android_vendor_service_context_handle(); |
| 411 | #else |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 412 | sehandle = selinux_android_service_context_handle(); |
Martijn Coenen | 3136123 | 2017-03-31 16:12:12 -0700 | [diff] [blame] | 413 | #endif |
Stephen Smalley | bea0746 | 2015-06-03 09:25:37 -0400 | [diff] [blame] | 414 | selinux_status_open(true); |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 415 | |
Nick Kralevich | eb4d5cb | 2016-12-09 17:05:09 -0800 | [diff] [blame] | 416 | if (sehandle == NULL) { |
| 417 | ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n"); |
| 418 | abort(); |
| 419 | } |
Riley Spahn | c67e630 | 2014-07-08 09:03:00 -0700 | [diff] [blame] | 420 | |
Nick Kralevich | eb4d5cb | 2016-12-09 17:05:09 -0800 | [diff] [blame] | 421 | if (getcon(&service_manager_context) != 0) { |
| 422 | ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n"); |
| 423 | abort(); |
Riley Spahn | c67e630 | 2014-07-08 09:03:00 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Riley Spahn | 69154df | 2014-06-05 11:07:18 -0700 | [diff] [blame] | 426 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 427 | binder_loop(bs, svcmgr_handler); |
Serban Constantinescu | 9b738bb | 2014-01-10 15:48:29 +0000 | [diff] [blame] | 428 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 429 | return 0; |
| 430 | } |