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