2012-10-24 17:45:23 +00:00
|
|
|
/* Copyright 2008 The Android Open Source Project
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
2014-06-05 18:07:18 +00:00
|
|
|
#include <selinux/android.h>
|
2014-07-12 23:34:01 +00:00
|
|
|
#include <selinux/avc.h>
|
2014-06-05 18:07:18 +00:00
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
#include "binder.h"
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#define ALOGI(x...) fprintf(stderr, "svcmgr: " x)
|
|
|
|
#define ALOGE(x...) fprintf(stderr, "svcmgr: " x)
|
|
|
|
#else
|
|
|
|
#define LOG_TAG "ServiceManager"
|
|
|
|
#include <cutils/log.h>
|
|
|
|
#endif
|
|
|
|
|
2014-01-30 14:07:34 +00:00
|
|
|
uint32_t svcmgr_handle;
|
2012-10-24 17:45:23 +00:00
|
|
|
|
2014-07-12 23:34:01 +00:00
|
|
|
const char *str8(const uint16_t *x, size_t x_len)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
|
|
|
static char buf[128];
|
2014-07-12 23:34:01 +00:00
|
|
|
size_t max = 127;
|
2012-10-24 17:45:23 +00:00
|
|
|
char *p = buf;
|
|
|
|
|
2014-07-12 23:34:01 +00:00
|
|
|
if (x_len < max) {
|
|
|
|
max = x_len;
|
|
|
|
}
|
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
if (x) {
|
2014-07-12 23:34:01 +00:00
|
|
|
while ((max > 0) && (*x != '\0')) {
|
2012-10-24 17:45:23 +00:00
|
|
|
*p++ = *x++;
|
2014-07-12 23:34:01 +00:00
|
|
|
max--;
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*p++ = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2014-01-10 15:48:29 +00:00
|
|
|
int str16eq(const uint16_t *a, const char *b)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
|
|
|
while (*a && *b)
|
|
|
|
if (*a++ != *b++) return 0;
|
|
|
|
if (*a || *b)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
static int selinux_enabled;
|
|
|
|
static char *service_manager_context;
|
2014-06-05 18:07:18 +00:00
|
|
|
static struct selabel_handle* sehandle;
|
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
static bool check_mac_perms(pid_t spid, const char *tctx, const char *perm, const char *name)
|
2014-06-05 18:07:18 +00:00
|
|
|
{
|
|
|
|
char *sctx = NULL;
|
2014-07-08 16:03:00 +00:00
|
|
|
const char *class = "service_manager";
|
|
|
|
bool allowed;
|
2014-06-05 18:07:18 +00:00
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
if (getpidcon(spid, &sctx) < 0) {
|
|
|
|
ALOGE("SELinux: getpidcon(pid=%d) failed to retrieve pid context.\n", spid);
|
2014-06-05 18:07:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
int result = selinux_check_access(sctx, tctx, class, perm, (void *) name);
|
|
|
|
allowed = (result == 0);
|
|
|
|
|
|
|
|
freecon(sctx);
|
|
|
|
return allowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_mac_perms_from_getcon(pid_t spid, const char *perm)
|
|
|
|
{
|
|
|
|
if (selinux_enabled <= 0) {
|
|
|
|
return true;
|
2014-06-05 18:07:18 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
return check_mac_perms(spid, service_manager_context, perm, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_mac_perms_from_lookup(pid_t spid, const char *perm, const char *name)
|
|
|
|
{
|
|
|
|
bool allowed;
|
|
|
|
char *tctx = NULL;
|
|
|
|
|
|
|
|
if (selinux_enabled <= 0) {
|
|
|
|
return true;
|
2014-06-05 18:07:18 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
if (!sehandle) {
|
|
|
|
ALOGE("SELinux: Failed to find sehandle. Aborting service_manager.\n");
|
|
|
|
abort();
|
2014-06-05 18:07:18 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
if (selabel_lookup(sehandle, &tctx, name, 0) != 0) {
|
|
|
|
ALOGE("SELinux: No match for %s in service_contexts.\n", name);
|
2014-06-05 18:07:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
allowed = check_mac_perms(spid, tctx, perm, name);
|
2014-06-05 18:07:18 +00:00
|
|
|
freecon(tctx);
|
|
|
|
return allowed;
|
|
|
|
}
|
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
static int svc_can_register(const uint16_t *name, size_t name_len, pid_t spid)
|
|
|
|
{
|
|
|
|
const char *perm = "add";
|
|
|
|
return check_mac_perms_from_lookup(spid, perm, str8(name, name_len)) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int svc_can_list(pid_t spid)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
2014-07-08 16:03:00 +00:00
|
|
|
const char *perm = "list";
|
|
|
|
return check_mac_perms_from_getcon(spid, perm) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int svc_can_find(const uint16_t *name, size_t name_len, pid_t spid)
|
|
|
|
{
|
|
|
|
const char *perm = "find";
|
|
|
|
return check_mac_perms_from_lookup(spid, perm, str8(name, name_len)) ? 1 : 0;
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 15:48:29 +00:00
|
|
|
struct svcinfo
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
|
|
|
struct svcinfo *next;
|
2014-01-30 14:07:34 +00:00
|
|
|
uint32_t handle;
|
2012-10-24 17:45:23 +00:00
|
|
|
struct binder_death death;
|
|
|
|
int allow_isolated;
|
2014-01-10 15:48:29 +00:00
|
|
|
size_t len;
|
2012-10-24 17:45:23 +00:00
|
|
|
uint16_t name[0];
|
|
|
|
};
|
|
|
|
|
2014-01-10 15:48:29 +00:00
|
|
|
struct svcinfo *svclist = NULL;
|
2012-10-24 17:45:23 +00:00
|
|
|
|
2014-01-10 15:48:29 +00:00
|
|
|
struct svcinfo *find_svc(const uint16_t *s16, size_t len)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
|
|
|
struct svcinfo *si;
|
|
|
|
|
|
|
|
for (si = svclist; si; si = si->next) {
|
|
|
|
if ((len == si->len) &&
|
|
|
|
!memcmp(s16, si->name, len * sizeof(uint16_t))) {
|
|
|
|
return si;
|
|
|
|
}
|
|
|
|
}
|
2014-01-10 15:48:29 +00:00
|
|
|
return NULL;
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void svcinfo_death(struct binder_state *bs, void *ptr)
|
|
|
|
{
|
2014-01-10 15:48:29 +00:00
|
|
|
struct svcinfo *si = (struct svcinfo* ) ptr;
|
|
|
|
|
2014-07-12 23:34:01 +00:00
|
|
|
ALOGI("service '%s' died\n", str8(si->name, si->len));
|
2014-01-30 14:07:34 +00:00
|
|
|
if (si->handle) {
|
|
|
|
binder_release(bs, si->handle);
|
|
|
|
si->handle = 0;
|
2014-01-10 15:48:29 +00:00
|
|
|
}
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 15:48:29 +00:00
|
|
|
uint16_t svcmgr_id[] = {
|
2012-10-24 17:45:23 +00:00
|
|
|
'a','n','d','r','o','i','d','.','o','s','.',
|
2014-01-10 15:48:29 +00:00
|
|
|
'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
|
2012-10-24 17:45:23 +00:00
|
|
|
};
|
|
|
|
|
2014-01-10 15:48:29 +00:00
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
uint32_t do_find_service(struct binder_state *bs, const uint16_t *s, size_t len, uid_t uid, pid_t spid)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
2015-03-05 18:58:40 +00:00
|
|
|
struct svcinfo *si = find_svc(s, len);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
2015-03-05 18:58:40 +00:00
|
|
|
if (!si || !si->handle) {
|
2014-07-08 16:03:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-03-05 18:58:40 +00:00
|
|
|
|
|
|
|
if (!si->allow_isolated) {
|
|
|
|
// If this service doesn't allow access from isolated processes,
|
|
|
|
// then check the uid to see if it is isolated.
|
|
|
|
uid_t appid = uid % AID_USER;
|
|
|
|
if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
|
|
|
|
return 0;
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
2015-03-05 18:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!svc_can_find(s, len, spid)) {
|
2012-10-24 17:45:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-03-05 18:58:40 +00:00
|
|
|
|
|
|
|
return si->handle;
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int do_add_service(struct binder_state *bs,
|
2014-01-10 15:48:29 +00:00
|
|
|
const uint16_t *s, size_t len,
|
2014-06-05 18:07:18 +00:00
|
|
|
uint32_t handle, uid_t uid, int allow_isolated,
|
|
|
|
pid_t spid)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
|
|
|
struct svcinfo *si;
|
2013-12-19 09:11:41 +00:00
|
|
|
|
2014-07-12 23:34:01 +00:00
|
|
|
//ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s, len), handle,
|
2012-10-24 17:45:23 +00:00
|
|
|
// allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
|
|
|
|
|
2014-01-30 14:07:34 +00:00
|
|
|
if (!handle || (len == 0) || (len > 127))
|
2012-10-24 17:45:23 +00:00
|
|
|
return -1;
|
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
if (!svc_can_register(s, len, spid)) {
|
2014-01-30 14:07:34 +00:00
|
|
|
ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
|
2014-07-12 23:34:01 +00:00
|
|
|
str8(s, len), handle, uid);
|
2012-10-24 17:45:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
si = find_svc(s, len);
|
|
|
|
if (si) {
|
2014-01-30 14:07:34 +00:00
|
|
|
if (si->handle) {
|
|
|
|
ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
|
2014-07-12 23:34:01 +00:00
|
|
|
str8(s, len), handle, uid);
|
2012-10-24 17:45:23 +00:00
|
|
|
svcinfo_death(bs, si);
|
|
|
|
}
|
2014-01-30 14:07:34 +00:00
|
|
|
si->handle = handle;
|
2012-10-24 17:45:23 +00:00
|
|
|
} else {
|
|
|
|
si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
|
|
|
|
if (!si) {
|
2014-01-30 14:07:34 +00:00
|
|
|
ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
|
2014-07-12 23:34:01 +00:00
|
|
|
str8(s, len), handle, uid);
|
2012-10-24 17:45:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-01-30 14:07:34 +00:00
|
|
|
si->handle = handle;
|
2012-10-24 17:45:23 +00:00
|
|
|
si->len = len;
|
|
|
|
memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
|
|
|
|
si->name[len] = '\0';
|
2014-01-10 15:48:29 +00:00
|
|
|
si->death.func = (void*) svcinfo_death;
|
2012-10-24 17:45:23 +00:00
|
|
|
si->death.ptr = si;
|
|
|
|
si->allow_isolated = allow_isolated;
|
|
|
|
si->next = svclist;
|
|
|
|
svclist = si;
|
|
|
|
}
|
|
|
|
|
2014-01-30 14:07:34 +00:00
|
|
|
binder_acquire(bs, handle);
|
|
|
|
binder_link_to_death(bs, handle, &si->death);
|
2012-10-24 17:45:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int svcmgr_handler(struct binder_state *bs,
|
2014-01-10 13:56:27 +00:00
|
|
|
struct binder_transaction_data *txn,
|
2012-10-24 17:45:23 +00:00
|
|
|
struct binder_io *msg,
|
|
|
|
struct binder_io *reply)
|
|
|
|
{
|
|
|
|
struct svcinfo *si;
|
|
|
|
uint16_t *s;
|
2014-01-10 15:48:29 +00:00
|
|
|
size_t len;
|
2014-01-30 14:07:34 +00:00
|
|
|
uint32_t handle;
|
2012-10-24 17:45:23 +00:00
|
|
|
uint32_t strict_policy;
|
|
|
|
int allow_isolated;
|
|
|
|
|
2013-12-19 09:11:41 +00:00
|
|
|
//ALOGI("target=%x code=%d pid=%d uid=%d\n",
|
|
|
|
// txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
2014-01-10 13:56:27 +00:00
|
|
|
if (txn->target.handle != svcmgr_handle)
|
2012-10-24 17:45:23 +00:00
|
|
|
return -1;
|
|
|
|
|
2014-01-29 05:35:03 +00:00
|
|
|
if (txn->code == PING_TRANSACTION)
|
|
|
|
return 0;
|
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
// Equivalent to Parcel::enforceInterface(), reading the RPC
|
|
|
|
// header with the strict mode policy mask and the interface name.
|
|
|
|
// Note that we ignore the strict_policy and don't propagate it
|
|
|
|
// further (since we do no outbound RPCs anyway).
|
|
|
|
strict_policy = bio_get_uint32(msg);
|
|
|
|
s = bio_get_string16(msg, &len);
|
2014-07-12 23:34:01 +00:00
|
|
|
if (s == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
if ((len != (sizeof(svcmgr_id) / 2)) ||
|
|
|
|
memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
|
2014-07-12 23:34:01 +00:00
|
|
|
fprintf(stderr,"invalid id %s\n", str8(s, len));
|
2012-10-24 17:45:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-06-05 18:07:18 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
switch(txn->code) {
|
|
|
|
case SVC_MGR_GET_SERVICE:
|
|
|
|
case SVC_MGR_CHECK_SERVICE:
|
|
|
|
s = bio_get_string16(msg, &len);
|
2014-07-12 23:34:01 +00:00
|
|
|
if (s == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-08 16:03:00 +00:00
|
|
|
handle = do_find_service(bs, s, len, txn->sender_euid, txn->sender_pid);
|
2014-01-30 14:07:34 +00:00
|
|
|
if (!handle)
|
2012-10-24 17:45:23 +00:00
|
|
|
break;
|
2014-01-30 14:07:34 +00:00
|
|
|
bio_put_ref(reply, handle);
|
2012-10-24 17:45:23 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SVC_MGR_ADD_SERVICE:
|
|
|
|
s = bio_get_string16(msg, &len);
|
2014-07-12 23:34:01 +00:00
|
|
|
if (s == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-01-30 14:07:34 +00:00
|
|
|
handle = bio_get_ref(msg);
|
2012-10-24 17:45:23 +00:00
|
|
|
allow_isolated = bio_get_uint32(msg) ? 1 : 0;
|
2014-06-05 18:07:18 +00:00
|
|
|
if (do_add_service(bs, s, len, handle, txn->sender_euid,
|
|
|
|
allow_isolated, txn->sender_pid))
|
2012-10-24 17:45:23 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SVC_MGR_LIST_SERVICES: {
|
2013-12-19 09:11:41 +00:00
|
|
|
uint32_t n = bio_get_uint32(msg);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
if (!svc_can_list(txn->sender_pid)) {
|
|
|
|
ALOGE("list_service() uid=%d - PERMISSION DENIED\n",
|
|
|
|
txn->sender_euid);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-24 17:45:23 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-06-05 18:07:18 +00:00
|
|
|
|
|
|
|
static int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
snprintf(buf, len, "service=%s", !data ? "NULL" : (char *)data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct binder_state *bs;
|
|
|
|
|
|
|
|
bs = binder_open(128*1024);
|
2014-01-30 15:16:45 +00:00
|
|
|
if (!bs) {
|
|
|
|
ALOGE("failed to open binder driver\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
if (binder_become_context_manager(bs)) {
|
|
|
|
ALOGE("cannot become context manager (%s)\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
selinux_enabled = is_selinux_enabled();
|
2014-06-05 18:07:18 +00:00
|
|
|
sehandle = selinux_android_service_context_handle();
|
|
|
|
|
2014-07-08 16:03:00 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 18:07:18 +00:00
|
|
|
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);
|
|
|
|
|
2014-01-10 15:48:29 +00:00
|
|
|
svcmgr_handle = BINDER_SERVICE_MANAGER;
|
2012-10-24 17:45:23 +00:00
|
|
|
binder_loop(bs, svcmgr_handler);
|
2014-01-10 15:48:29 +00:00
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|