Commit message: None
CVE: CVE-2016-3900
CWE: CWE-264
Vulnerability description: cmds/servicemanager/service_manager.c in ServiceManager in Android 5.0.x before 5.0.2, 5.1.x before 5.1.1, 6.x before 2016-10-01, and 7.0 before 2016-10-01 does not properly restrict service registration, which allows attackers to gain privileges via a crafted application, aka internal bug 29431260.

Project: Android

static int audit_callback(void *data, __unused security_class_t cls, char *buf, size_t len)
{
 struct audit_data *ad = (struct audit_data *)data;

 if (!ad || !ad->name) {
        ALOGE("No service manager audit data");
 return 0;
 }

    snprintf(buf, len, "service=%s pid=%d uid=%d", ad->name, ad->pid, ad->uid);
 return 0;
}

Project: Android

static bool check_mac_perms_from_getcon(pid_t spid, uid_t uid, const char *perm)
{
 if (selinux_enabled <= 0) {
 return true;
 }

 return check_mac_perms(spid, uid, service_manager_context, perm, NULL);
}

Project: Android

static bool check_mac_perms_from_lookup(pid_t spid, uid_t uid, const char *perm, const char *name)
{
 bool allowed;
 char *tctx = NULL;

 if (selinux_enabled <= 0) {
 return true;
 }

 if (!sehandle) {
        ALOGE("SELinux: Failed to find sehandle. Aborting service_manager.\n");
        abort();
 }

 if (selabel_lookup(sehandle, &tctx, name, 0) != 0) {
        ALOGE("SELinux: No match for %s in service_contexts.\n", name);
 return false;
 }

    allowed = check_mac_perms(spid, uid, tctx, perm, name);
    freecon(tctx);
 return allowed;
}

Project: Android

int do_add_service(struct binder_state *bs,
 const uint16_t *s, size_t len,
 uint32_t handle, uid_t uid, int allow_isolated,
 pid_t spid)
{
 struct svcinfo *si;


 if (!handle || (len == 0) || (len > 127))
 return -1;

 if (!svc_can_register(s, len, spid, uid)) {
        ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
             str8(s, len), handle, uid);
 return -1;
 }

    si = find_svc(s, len);
 if (si) {
 if (si->handle) {
            ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
                 str8(s, len), handle, uid);
            svcinfo_death(bs, si);
 }
        si->handle = handle;
 } else {
        si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
 if (!si) {
            ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
                 str8(s, len), handle, uid);
 return -1;
 }
        si->handle = handle;
        si->len = len;
        memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
        si->name[len] = '\0';
        si->death.func = (void*) svcinfo_death;
        si->death.ptr = si;
        si->allow_isolated = allow_isolated;
        si->next = svclist;
        svclist = si;
 }

    binder_acquire(bs, handle);
    binder_link_to_death(bs, handle, &si->death);
 return 0;
}

Project: Android

uint32_t do_find_service(const uint16_t *s, size_t len, uid_t uid, pid_t spid)
{
 struct svcinfo *si = find_svc(s, len);

 if (!si || !si->handle) {
 return 0;
 }

 if (!si->allow_isolated) {
 uid_t appid = uid % AID_USER;
 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
 return 0;
 }
 }

 if (!svc_can_find(s, len, spid, uid)) {
 return 0;
 }

 return si->handle;
}

Project: Android

struct svcinfo *find_svc(const uint16_t *s16, size_t len)
{
 struct svcinfo *si;

 for (si = svclist; si; si = si->next) {
 if ((len == si->len) &&
 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
 return si;
 }
 }
 return NULL;
}

Project: Android

int main()
{
 struct binder_state *bs;

    bs = binder_open(128*1024);
 if (!bs) {
        ALOGE("failed to open binder driver\n");
 return -1;
 }

 if (binder_become_context_manager(bs)) {
        ALOGE("cannot become context manager (%s)\n", strerror(errno));
 return -1;
 }

    selinux_enabled = is_selinux_enabled();
    sehandle = selinux_android_service_context_handle();
    selinux_status_open(true);

 if (selinux_enabled > 0) {
 if (sehandle == NULL) {
            ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n");
            abort();
 }

 if (getcon(&service_manager_context) != 0) {
            ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n");
            abort();
 }
 }

 union selinux_callback cb;
    cb.func_audit = audit_callback;
    selinux_set_callback(SELINUX_CB_AUDIT, cb);
    cb.func_log = selinux_log_callback;
    selinux_set_callback(SELINUX_CB_LOG, cb);

    binder_loop(bs, svcmgr_handler);

 return 0;
}

Project: Android

int str16eq(const uint16_t *a, const char *b)
{
 while (*a && *b)
 if (*a++ != *b++) return 0;
 if (*a || *b)
 return 0;
 return 1;
}

Project: Android

const char *str8(const uint16_t *x, size_t x_len)
{
 static char buf[128];
 size_t max = 127;
 char *p = buf;

 if (x_len < max) {
        max = x_len;
 }

 if (x) {
 while ((max > 0) && (*x != '\0')) {
 *p++ = *x++;
            max--;
 }
 }
 *p++ = 0;
 return buf;
}

Project: Android

static int svc_can_find(const uint16_t *name, size_t name_len, pid_t spid, uid_t uid)
{
 const char *perm = "find";
 return check_mac_perms_from_lookup(spid, uid, perm, str8(name, name_len)) ? 1 : 0;
}

Project: Android

static int svc_can_list(pid_t spid, uid_t uid)
{
 const char *perm = "list";
 return check_mac_perms_from_getcon(spid, uid, perm) ? 1 : 0;
}

Project: Android

int svcmgr_handler(struct binder_state *bs,
 struct binder_transaction_data *txn,
 struct binder_io *msg,
 struct binder_io *reply)
{
 struct svcinfo *si;
 uint16_t *s;
 size_t len;
 uint32_t handle;
 uint32_t strict_policy;
 int allow_isolated;


 if (txn->target.ptr != BINDER_SERVICE_MANAGER)
 return -1;

 if (txn->code == PING_TRANSACTION)
 return 0;

    strict_policy = bio_get_uint32(msg);
    s = bio_get_string16(msg, &len);
 if (s == NULL) {
 return -1;
 }

 if ((len != (sizeof(svcmgr_id) / 2)) ||
        memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
        fprintf(stderr,"invalid id %s\n", str8(s, len));
 return -1;
 }

 if (sehandle && selinux_status_updated() > 0) {
 struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
 if (tmp_sehandle) {
            selabel_close(sehandle);
            sehandle = tmp_sehandle;
 }
 }

 switch(txn->code) {
 case SVC_MGR_GET_SERVICE:
 case SVC_MGR_CHECK_SERVICE:
        s = bio_get_string16(msg, &len);
 if (s == NULL) {
 return -1;
 }
        handle = do_find_service(s, len, txn->sender_euid, txn->sender_pid);
 if (!handle)
 break;
        bio_put_ref(reply, handle);
 return 0;

 case SVC_MGR_ADD_SERVICE:
        s = bio_get_string16(msg, &len);
 if (s == NULL) {
 return -1;
 }
        handle = bio_get_ref(msg);
        allow_isolated = bio_get_uint32(msg) ? 1 : 0;
 if (do_add_service(bs, s, len, handle, txn->sender_euid,
            allow_isolated, txn->sender_pid))
 return -1;
 break;

 case SVC_MGR_LIST_SERVICES: {
 uint32_t n = bio_get_uint32(msg);

 if (!svc_can_list(txn->sender_pid, txn->sender_euid)) {
            ALOGE("list_service() uid=%d - PERMISSION DENIED\n",
                    txn->sender_euid);
 return -1;
 }
        si = svclist;
 while ((n-- > 0) && si)
            si = si->next;
 if (si) {
            bio_put_string16(reply, si->name);
 return 0;
 }
 return -1;
 }
 default:
        ALOGE("unknown code %d\n", txn->code);
 return -1;
 }

    bio_put_uint32(reply, 0);
 return 0;
}