194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Copyright 2008 The Android Open Source Project
294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <errno.h>
594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <fcntl.h>
60b41ad5d6ec86cd2d481969dcff7e88f2805324fElliott Hughes#include <inttypes.h>
713df5f5f8fbc5a3fdfdd5d1ba5dc853cf3f017f0Mark Salyzyn#include <stdio.h>
813df5f5f8fbc5a3fdfdd5d1ba5dc853cf3f017f0Mark Salyzyn#include <stdlib.h>
913df5f5f8fbc5a3fdfdd5d1ba5dc853cf3f017f0Mark Salyzyn#include <string.h>
1094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <private/android_filesystem_config.h>
1294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1369154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn#include <selinux/android.h>
147d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich#include <selinux/avc.h>
1569154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
1694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include "binder.h"
1794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#if 0
1994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define ALOGI(x...) fprintf(stderr, "svcmgr: " x)
2094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define ALOGE(x...) fprintf(stderr, "svcmgr: " x)
2194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#else
2294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define LOG_TAG "ServiceManager"
2394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <cutils/log.h>
2494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#endif
2594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
268fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Robertsstruct audit_data {
278fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    pid_t pid;
288fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    uid_t uid;
298fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    const char *name;
308fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts};
318fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts
327d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevichconst char *str8(const uint16_t *x, size_t x_len)
3394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
3494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    static char buf[128];
357d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich    size_t max = 127;
3694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *p = buf;
3794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
387d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich    if (x_len < max) {
397d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich        max = x_len;
407d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich    }
417d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich
4294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (x) {
437d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich        while ((max > 0) && (*x != '\0')) {
4494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *p++ = *x++;
457d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich            max--;
4694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
4794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
4894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *p++ = 0;
4994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return buf;
5094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
5194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
529b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescuint str16eq(const uint16_t *a, const char *b)
5394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
5494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (*a && *b)
5594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (*a++ != *b++) return 0;
5694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (*a || *b)
5794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return 0;
5894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 1;
5994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
6094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
61c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahnstatic int selinux_enabled;
62c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahnstatic char *service_manager_context;
6369154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahnstatic struct selabel_handle* sehandle;
6469154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
658fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Robertsstatic bool check_mac_perms(pid_t spid, uid_t uid, const char *tctx, const char *perm, const char *name)
6669154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn{
6769154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    char *sctx = NULL;
68c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    const char *class = "service_manager";
69c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    bool allowed;
708fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    struct audit_data ad;
7169154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
72c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    if (getpidcon(spid, &sctx) < 0) {
73c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn        ALOGE("SELinux: getpidcon(pid=%d) failed to retrieve pid context.\n", spid);
7469154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn        return false;
7569154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    }
7669154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
778fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    ad.pid = spid;
788fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    ad.uid = uid;
798fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    ad.name = name;
808fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts
818fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    int result = selinux_check_access(sctx, tctx, class, perm, (void *) &ad);
82c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    allowed = (result == 0);
83c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn
84c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    freecon(sctx);
85c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    return allowed;
86c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn}
87c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn
888fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Robertsstatic bool check_mac_perms_from_getcon(pid_t spid, uid_t uid, const char *perm)
89c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn{
90c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    if (selinux_enabled <= 0) {
91c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn        return true;
9269154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    }
9369154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
948fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    return check_mac_perms(spid, uid, service_manager_context, perm, NULL);
95c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn}
96c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn
978fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Robertsstatic bool check_mac_perms_from_lookup(pid_t spid, uid_t uid, const char *perm, const char *name)
98c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn{
99c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    bool allowed;
100c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    char *tctx = NULL;
101c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn
102c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    if (selinux_enabled <= 0) {
103c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn        return true;
10469154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    }
10569154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
106c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    if (!sehandle) {
107c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn        ALOGE("SELinux: Failed to find sehandle. Aborting service_manager.\n");
108c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn        abort();
10969154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    }
11069154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
111c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    if (selabel_lookup(sehandle, &tctx, name, 0) != 0) {
112c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn        ALOGE("SELinux: No match for %s in service_contexts.\n", name);
11369154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn        return false;
11469154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    }
11569154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
1168fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    allowed = check_mac_perms(spid, uid, tctx, perm, name);
11769154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    freecon(tctx);
11869154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    return allowed;
11969154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn}
12069154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
1218fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Robertsstatic int svc_can_register(const uint16_t *name, size_t name_len, pid_t spid, uid_t uid)
122c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn{
123c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    const char *perm = "add";
1248fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    return check_mac_perms_from_lookup(spid, uid, perm, str8(name, name_len)) ? 1 : 0;
125c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn}
126c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn
1278fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Robertsstatic int svc_can_list(pid_t spid, uid_t uid)
12894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
129c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    const char *perm = "list";
1308fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    return check_mac_perms_from_getcon(spid, uid, perm) ? 1 : 0;
131c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn}
132c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn
1338fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Robertsstatic int svc_can_find(const uint16_t *name, size_t name_len, pid_t spid, uid_t uid)
134c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn{
135c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    const char *perm = "find";
1368fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    return check_mac_perms_from_lookup(spid, uid, perm, str8(name, name_len)) ? 1 : 0;
13794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
13894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1399b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescustruct svcinfo
14094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
14194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct svcinfo *next;
1425fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu    uint32_t handle;
14394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct binder_death death;
14494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int allow_isolated;
1459b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    size_t len;
14694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    uint16_t name[0];
14794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood};
14894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1499b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescustruct svcinfo *svclist = NULL;
15094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1519b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescustruct svcinfo *find_svc(const uint16_t *s16, size_t len)
15294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
15394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct svcinfo *si;
15494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
15594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (si = svclist; si; si = si->next) {
15694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if ((len == si->len) &&
15794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            !memcmp(s16, si->name, len * sizeof(uint16_t))) {
15894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return si;
15994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
16094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
1619b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    return NULL;
16294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
16394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
16494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid svcinfo_death(struct binder_state *bs, void *ptr)
16594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
1669b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    struct svcinfo *si = (struct svcinfo* ) ptr;
1679b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu
1687d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich    ALOGI("service '%s' died\n", str8(si->name, si->len));
1695fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu    if (si->handle) {
1705fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu        binder_release(bs, si->handle);
1715fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu        si->handle = 0;
1729b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    }
17394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
17494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1759b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescuuint16_t svcmgr_id[] = {
17694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    'a','n','d','r','o','i','d','.','o','s','.',
1779b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
17894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood};
17994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1809b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu
181d57d9b900da83b1b5431d90e250f86c0047c618aIan Pedowitzuint32_t do_find_service(const uint16_t *s, size_t len, uid_t uid, pid_t spid)
18294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
183b27bbd18bb65b3744ae066fcd6826285dec8b469Nick Kralevich    struct svcinfo *si = find_svc(s, len);
18494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
185b27bbd18bb65b3744ae066fcd6826285dec8b469Nick Kralevich    if (!si || !si->handle) {
186c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn        return 0;
187c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    }
188b27bbd18bb65b3744ae066fcd6826285dec8b469Nick Kralevich
189b27bbd18bb65b3744ae066fcd6826285dec8b469Nick Kralevich    if (!si->allow_isolated) {
190b27bbd18bb65b3744ae066fcd6826285dec8b469Nick Kralevich        // If this service doesn't allow access from isolated processes,
191b27bbd18bb65b3744ae066fcd6826285dec8b469Nick Kralevich        // then check the uid to see if it is isolated.
192b27bbd18bb65b3744ae066fcd6826285dec8b469Nick Kralevich        uid_t appid = uid % AID_USER;
193b27bbd18bb65b3744ae066fcd6826285dec8b469Nick Kralevich        if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
194b27bbd18bb65b3744ae066fcd6826285dec8b469Nick Kralevich            return 0;
19594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
196b27bbd18bb65b3744ae066fcd6826285dec8b469Nick Kralevich    }
197b27bbd18bb65b3744ae066fcd6826285dec8b469Nick Kralevich
1988fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    if (!svc_can_find(s, len, spid, uid)) {
19994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return 0;
20094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
201b27bbd18bb65b3744ae066fcd6826285dec8b469Nick Kralevich
202b27bbd18bb65b3744ae066fcd6826285dec8b469Nick Kralevich    return si->handle;
20394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
20494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
20594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint do_add_service(struct binder_state *bs,
2069b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu                   const uint16_t *s, size_t len,
20769154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn                   uint32_t handle, uid_t uid, int allow_isolated,
20869154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn                   pid_t spid)
20994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
21094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct svcinfo *si;
2113a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu
2127d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich    //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s, len), handle,
21394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    //        allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
21494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2155fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu    if (!handle || (len == 0) || (len > 127))
21694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
21794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2188fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    if (!svc_can_register(s, len, spid, uid)) {
2195fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu        ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
2207d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich             str8(s, len), handle, uid);
22194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
22294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
22394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
22494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    si = find_svc(s, len);
22594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (si) {
2265fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu        if (si->handle) {
2275fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu            ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
2287d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich                 str8(s, len), handle, uid);
22994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            svcinfo_death(bs, si);
23094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
2315fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu        si->handle = handle;
23294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
23394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
23494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (!si) {
2355fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu            ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
2367d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich                 str8(s, len), handle, uid);
23794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
23894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
2395fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu        si->handle = handle;
24094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        si->len = len;
24194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
24294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        si->name[len] = '\0';
2439b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu        si->death.func = (void*) svcinfo_death;
24494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        si->death.ptr = si;
24594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        si->allow_isolated = allow_isolated;
24694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        si->next = svclist;
24794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        svclist = si;
24894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
24994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2505fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu    binder_acquire(bs, handle);
2515fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu    binder_link_to_death(bs, handle, &si->death);
25294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
25394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
25494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
25594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint svcmgr_handler(struct binder_state *bs,
256bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu                   struct binder_transaction_data *txn,
25794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                   struct binder_io *msg,
25894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                   struct binder_io *reply)
25994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
26094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct svcinfo *si;
26194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    uint16_t *s;
2629b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    size_t len;
2635fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu    uint32_t handle;
26494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    uint32_t strict_policy;
26594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int allow_isolated;
26694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2670b41ad5d6ec86cd2d481969dcff7e88f2805324fElliott Hughes    //ALOGI("target=%p code=%d pid=%d uid=%d\n",
2680b41ad5d6ec86cd2d481969dcff7e88f2805324fElliott Hughes    //      (void*) txn->target.ptr, txn->code, txn->sender_pid, txn->sender_euid);
26994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2700b41ad5d6ec86cd2d481969dcff7e88f2805324fElliott Hughes    if (txn->target.ptr != BINDER_SERVICE_MANAGER)
27194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
27294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
273e5245cbf5d4e830cf605ef07f5d284d7c5d2867eArve Hjønnevåg    if (txn->code == PING_TRANSACTION)
274e5245cbf5d4e830cf605ef07f5d284d7c5d2867eArve Hjønnevåg        return 0;
275e5245cbf5d4e830cf605ef07f5d284d7c5d2867eArve Hjønnevåg
27694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Equivalent to Parcel::enforceInterface(), reading the RPC
27794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // header with the strict mode policy mask and the interface name.
27894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Note that we ignore the strict_policy and don't propagate it
27994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // further (since we do no outbound RPCs anyway).
28094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strict_policy = bio_get_uint32(msg);
28194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    s = bio_get_string16(msg, &len);
2827d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich    if (s == NULL) {
2837d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich        return -1;
2847d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich    }
2857d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich
28694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((len != (sizeof(svcmgr_id) / 2)) ||
28794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
2887d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich        fprintf(stderr,"invalid id %s\n", str8(s, len));
28994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
29094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
29194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
29269154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    if (sehandle && selinux_status_updated() > 0) {
29369154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn        struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
29469154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn        if (tmp_sehandle) {
29569154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn            selabel_close(sehandle);
29669154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn            sehandle = tmp_sehandle;
29769154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn        }
29869154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    }
29969154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
30094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    switch(txn->code) {
30194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    case SVC_MGR_GET_SERVICE:
30294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    case SVC_MGR_CHECK_SERVICE:
30394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        s = bio_get_string16(msg, &len);
3047d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich        if (s == NULL) {
3057d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich            return -1;
3067d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich        }
307d57d9b900da83b1b5431d90e250f86c0047c618aIan Pedowitz        handle = do_find_service(s, len, txn->sender_euid, txn->sender_pid);
3085fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu        if (!handle)
30994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            break;
3105fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu        bio_put_ref(reply, handle);
31194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return 0;
31294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
31394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    case SVC_MGR_ADD_SERVICE:
31494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        s = bio_get_string16(msg, &len);
3157d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich        if (s == NULL) {
3167d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich            return -1;
3177d42a3c31ba78a418f9bdde0e0ab951469f321b5Nick Kralevich        }
3185fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu        handle = bio_get_ref(msg);
31994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        allow_isolated = bio_get_uint32(msg) ? 1 : 0;
32069154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn        if (do_add_service(bs, s, len, handle, txn->sender_euid,
32169154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn            allow_isolated, txn->sender_pid))
32294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
32394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        break;
32494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    case SVC_MGR_LIST_SERVICES: {
3263a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        uint32_t n = bio_get_uint32(msg);
32794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3288fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts        if (!svc_can_list(txn->sender_pid, txn->sender_euid)) {
329c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn            ALOGE("list_service() uid=%d - PERMISSION DENIED\n",
330c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn                    txn->sender_euid);
331c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn            return -1;
332c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn        }
33394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        si = svclist;
33494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        while ((n-- > 0) && si)
33594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            si = si->next;
33694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (si) {
33794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            bio_put_string16(reply, si->name);
33894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return 0;
33994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
34094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
34194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
34294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    default:
34394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("unknown code %d\n", txn->code);
34494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
34594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
34694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
34794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bio_put_uint32(reply, 0);
34894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
34994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
35094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
35169154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
352d57d9b900da83b1b5431d90e250f86c0047c618aIan Pedowitzstatic int audit_callback(void *data, __unused security_class_t cls, char *buf, size_t len)
35369154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn{
3548fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    struct audit_data *ad = (struct audit_data *)data;
3558fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts
3568fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    if (!ad || !ad->name) {
3578fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts        ALOGE("No service manager audit data");
3588fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts        return 0;
3598fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    }
3608fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts
3618fb0f92e0fe3c286ebb120bae9e1570ee109fa12William Roberts    snprintf(buf, len, "service=%s pid=%d uid=%d", ad->name, ad->pid, ad->uid);
36269154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    return 0;
36369154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn}
36469154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
365d57d9b900da83b1b5431d90e250f86c0047c618aIan Pedowitzint main()
36694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
36794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct binder_state *bs;
36894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
36994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bs = binder_open(128*1024);
370a44542ca74b7da5b44ba30c205c3244805bb0600Serban Constantinescu    if (!bs) {
371a44542ca74b7da5b44ba30c205c3244805bb0600Serban Constantinescu        ALOGE("failed to open binder driver\n");
372a44542ca74b7da5b44ba30c205c3244805bb0600Serban Constantinescu        return -1;
373a44542ca74b7da5b44ba30c205c3244805bb0600Serban Constantinescu    }
37494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
37594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (binder_become_context_manager(bs)) {
37694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("cannot become context manager (%s)\n", strerror(errno));
37794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
37894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
37994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
380c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    selinux_enabled = is_selinux_enabled();
38169154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    sehandle = selinux_android_service_context_handle();
382bea0746b241d15626cf0a56828efc1d4640dbda7Stephen Smalley    selinux_status_open(true);
38369154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
384c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    if (selinux_enabled > 0) {
385c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn        if (sehandle == NULL) {
386c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn            ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n");
387c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn            abort();
388c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn        }
389c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn
390c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn        if (getcon(&service_manager_context) != 0) {
391c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn            ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n");
392c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn            abort();
393c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn        }
394c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn    }
395c67e6307cadb1f2cd876907c42d39b8374b93acdRiley Spahn
39669154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    union selinux_callback cb;
39769154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    cb.func_audit = audit_callback;
39869154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    selinux_set_callback(SELINUX_CB_AUDIT, cb);
39969154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    cb.func_log = selinux_log_callback;
40069154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn    selinux_set_callback(SELINUX_CB_LOG, cb);
40169154df9efd3ffb7580b72a0138f58a2f5443db6Riley Spahn
40294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    binder_loop(bs, svcmgr_handler);
4039b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu
40494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
40594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
406