service_manager.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* Copyright 2008 The Android Open Source Project
  2. */
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <inttypes.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <cutils/multiuser.h>
  10. #include <private/android_filesystem_config.h>
  11. #include <selinux/android.h>
  12. #include <selinux/avc.h>
  13. #include "binder.h"
  14. #if 0
  15. #define ALOGI(x...) fprintf(stderr, "svcmgr: " x)
  16. #define ALOGE(x...) fprintf(stderr, "svcmgr: " x)
  17. #else
  18. #define LOG_TAG "ServiceManager"
  19. #include <cutils/log.h>
  20. #endif
  21. const char *str8(const uint16_t *x, size_t x_len)
  22. {
  23. static char buf[128];
  24. size_t max = 127;
  25. char *p = buf;
  26. if (x_len < max) {
  27. max = x_len;
  28. }
  29. if (x) {
  30. while ((max > 0) && (*x != '\0')) {
  31. *p++ = *x++;
  32. max--;
  33. }
  34. }
  35. *p++ = 0;
  36. return buf;
  37. }
  38. int str16eq(const uint16_t *a, const char *b)
  39. {
  40. while (*a && *b)
  41. if (*a++ != *b++) return 0;
  42. if (*a || *b)
  43. return 0;
  44. return 1;
  45. }
  46. static int selinux_enabled;
  47. static char *service_manager_context;
  48. static struct selabel_handle* sehandle;
  49. static bool check_mac_perms(pid_t spid, const char *tctx, const char *perm, const char *name)
  50. {
  51. char *sctx = NULL;
  52. const char *class = "service_manager";
  53. bool allowed;
  54. if (getpidcon(spid, &sctx) < 0) {
  55. ALOGE("SELinux: getpidcon(pid=%d) failed to retrieve pid context.\n", spid);
  56. return false;
  57. }
  58. int result = selinux_check_access(sctx, tctx, class, perm, (void *) name);
  59. allowed = (result == 0);
  60. freecon(sctx);
  61. return allowed;
  62. }
  63. static bool check_mac_perms_from_getcon(pid_t spid, const char *perm)
  64. {
  65. if (selinux_enabled <= 0) {
  66. return true;
  67. }
  68. return check_mac_perms(spid, service_manager_context, perm, NULL);
  69. }
  70. static bool check_mac_perms_from_lookup(pid_t spid, const char *perm, const char *name)
  71. {
  72. bool allowed;
  73. char *tctx = NULL;
  74. if (selinux_enabled <= 0) {
  75. return true;
  76. }
  77. if (!sehandle) {
  78. ALOGE("SELinux: Failed to find sehandle. Aborting service_manager.\n");
  79. abort();
  80. }
  81. if (selabel_lookup(sehandle, &tctx, name, 0) != 0) {
  82. ALOGE("SELinux: No match for %s in service_contexts.\n", name);
  83. return false;
  84. }
  85. allowed = check_mac_perms(spid, tctx, perm, name);
  86. freecon(tctx);
  87. return allowed;
  88. }
  89. static int svc_can_register(const uint16_t *name, size_t name_len, pid_t spid, uid_t uid)
  90. {
  91. const char *perm = "add";
  92. if (multiuser_get_app_id(uid) >= AID_APP) {
  93. return 0; /* Don't allow apps to register services */
  94. }
  95. return check_mac_perms_from_lookup(spid, perm, str8(name, name_len)) ? 1 : 0;
  96. }
  97. static int svc_can_list(pid_t spid)
  98. {
  99. const char *perm = "list";
  100. return check_mac_perms_from_getcon(spid, perm) ? 1 : 0;
  101. }
  102. static int svc_can_find(const uint16_t *name, size_t name_len, pid_t spid)
  103. {
  104. const char *perm = "find";
  105. return check_mac_perms_from_lookup(spid, perm, str8(name, name_len)) ? 1 : 0;
  106. }
  107. struct svcinfo
  108. {
  109. struct svcinfo *next;
  110. uint32_t handle;
  111. struct binder_death death;
  112. int allow_isolated;
  113. size_t len;
  114. uint16_t name[0];
  115. };
  116. struct svcinfo *svclist = NULL;
  117. struct svcinfo *find_svc(const uint16_t *s16, size_t len)
  118. {
  119. struct svcinfo *si;
  120. for (si = svclist; si; si = si->next) {
  121. if ((len == si->len) &&
  122. !memcmp(s16, si->name, len * sizeof(uint16_t))) {
  123. return si;
  124. }
  125. }
  126. return NULL;
  127. }
  128. void svcinfo_death(struct binder_state *bs, void *ptr)
  129. {
  130. struct svcinfo *si = (struct svcinfo* ) ptr;
  131. ALOGI("service '%s' died\n", str8(si->name, si->len));
  132. if (si->handle) {
  133. binder_release(bs, si->handle);
  134. si->handle = 0;
  135. }
  136. }
  137. uint16_t svcmgr_id[] = {
  138. 'a','n','d','r','o','i','d','.','o','s','.',
  139. 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
  140. };
  141. uint32_t do_find_service(struct binder_state *bs, const uint16_t *s, size_t len, uid_t uid, pid_t spid)
  142. {
  143. struct svcinfo *si = find_svc(s, len);
  144. if (!si || !si->handle) {
  145. return 0;
  146. }
  147. if (!si->allow_isolated) {
  148. // If this service doesn't allow access from isolated processes,
  149. // then check the uid to see if it is isolated.
  150. uid_t appid = uid % AID_USER;
  151. if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
  152. return 0;
  153. }
  154. }
  155. if (!svc_can_find(s, len, spid)) {
  156. return 0;
  157. }
  158. return si->handle;
  159. }
  160. int do_add_service(struct binder_state *bs,
  161. const uint16_t *s, size_t len,
  162. uint32_t handle, uid_t uid, int allow_isolated,
  163. pid_t spid)
  164. {
  165. struct svcinfo *si;
  166. //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s, len), handle,
  167. // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
  168. if (!handle || (len == 0) || (len > 127))
  169. return -1;
  170. if (!svc_can_register(s, len, spid, uid)) {
  171. ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
  172. str8(s, len), handle, uid);
  173. return -1;
  174. }
  175. si = find_svc(s, len);
  176. if (si) {
  177. if (si->handle) {
  178. ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
  179. str8(s, len), handle, uid);
  180. svcinfo_death(bs, si);
  181. }
  182. si->handle = handle;
  183. } else {
  184. si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
  185. if (!si) {
  186. ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
  187. str8(s, len), handle, uid);
  188. return -1;
  189. }
  190. si->handle = handle;
  191. si->len = len;
  192. memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
  193. si->name[len] = '\0';
  194. si->death.func = (void*) svcinfo_death;
  195. si->death.ptr = si;
  196. si->allow_isolated = allow_isolated;
  197. si->next = svclist;
  198. svclist = si;
  199. }
  200. binder_acquire(bs, handle);
  201. binder_link_to_death(bs, handle, &si->death);
  202. return 0;
  203. }
  204. int svcmgr_handler(struct binder_state *bs,
  205. struct binder_transaction_data *txn,
  206. struct binder_io *msg,
  207. struct binder_io *reply)
  208. {
  209. struct svcinfo *si;
  210. uint16_t *s;
  211. size_t len;
  212. uint32_t handle;
  213. uint32_t strict_policy;
  214. int allow_isolated;
  215. //ALOGI("target=%p code=%d pid=%d uid=%d\n",
  216. // (void*) txn->target.ptr, txn->code, txn->sender_pid, txn->sender_euid);
  217. if (txn->target.ptr != BINDER_SERVICE_MANAGER)
  218. return -1;
  219. if (txn->code == PING_TRANSACTION)
  220. return 0;
  221. // Equivalent to Parcel::enforceInterface(), reading the RPC
  222. // header with the strict mode policy mask and the interface name.
  223. // Note that we ignore the strict_policy and don't propagate it
  224. // further (since we do no outbound RPCs anyway).
  225. strict_policy = bio_get_uint32(msg);
  226. s = bio_get_string16(msg, &len);
  227. if (s == NULL) {
  228. return -1;
  229. }
  230. if ((len != (sizeof(svcmgr_id) / 2)) ||
  231. memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
  232. fprintf(stderr,"invalid id %s\n", str8(s, len));
  233. return -1;
  234. }
  235. if (sehandle && selinux_status_updated() > 0) {
  236. struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
  237. if (tmp_sehandle) {
  238. selabel_close(sehandle);
  239. sehandle = tmp_sehandle;
  240. }
  241. }
  242. switch(txn->code) {
  243. case SVC_MGR_GET_SERVICE:
  244. case SVC_MGR_CHECK_SERVICE:
  245. s = bio_get_string16(msg, &len);
  246. if (s == NULL) {
  247. return -1;
  248. }
  249. handle = do_find_service(bs, s, len, txn->sender_euid, txn->sender_pid);
  250. if (!handle)
  251. break;
  252. bio_put_ref(reply, handle);
  253. return 0;
  254. case SVC_MGR_ADD_SERVICE:
  255. s = bio_get_string16(msg, &len);
  256. if (s == NULL) {
  257. return -1;
  258. }
  259. handle = bio_get_ref(msg);
  260. allow_isolated = bio_get_uint32(msg) ? 1 : 0;
  261. if (do_add_service(bs, s, len, handle, txn->sender_euid,
  262. allow_isolated, txn->sender_pid))
  263. return -1;
  264. break;
  265. case SVC_MGR_LIST_SERVICES: {
  266. uint32_t n = bio_get_uint32(msg);
  267. if (!svc_can_list(txn->sender_pid)) {
  268. ALOGE("list_service() uid=%d - PERMISSION DENIED\n",
  269. txn->sender_euid);
  270. return -1;
  271. }
  272. si = svclist;
  273. while ((n-- > 0) && si)
  274. si = si->next;
  275. if (si) {
  276. bio_put_string16(reply, si->name);
  277. return 0;
  278. }
  279. return -1;
  280. }
  281. default:
  282. ALOGE("unknown code %d\n", txn->code);
  283. return -1;
  284. }
  285. bio_put_uint32(reply, 0);
  286. return 0;
  287. }
  288. static int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
  289. {
  290. snprintf(buf, len, "service=%s", !data ? "NULL" : (char *)data);
  291. return 0;
  292. }
  293. int main(int argc, char **argv)
  294. {
  295. struct binder_state *bs;
  296. bs = binder_open(128*1024);
  297. if (!bs) {
  298. ALOGE("failed to open binder driver\n");
  299. return -1;
  300. }
  301. if (binder_become_context_manager(bs)) {
  302. ALOGE("cannot become context manager (%s)\n", strerror(errno));
  303. return -1;
  304. }
  305. selinux_enabled = is_selinux_enabled();
  306. sehandle = selinux_android_service_context_handle();
  307. selinux_status_open(true);
  308. if (selinux_enabled > 0) {
  309. if (sehandle == NULL) {
  310. ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n");
  311. abort();
  312. }
  313. if (getcon(&service_manager_context) != 0) {
  314. ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n");
  315. abort();
  316. }
  317. }
  318. union selinux_callback cb;
  319. cb.func_audit = audit_callback;
  320. selinux_set_callback(SELINUX_CB_AUDIT, cb);
  321. cb.func_log = selinux_log_callback;
  322. selinux_set_callback(SELINUX_CB_LOG, cb);
  323. binder_loop(bs, svcmgr_handler);
  324. return 0;
  325. }