service_manager.c 12 KB

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