libjailbreak_mig.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "libjailbreak_mig.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <mach/mach.h>
  7. #include "mach/jailbreak_daemonUser.h"
  8. #include <dispatch/dispatch.h>
  9. kern_return_t bootstrap_look_up(mach_port_t port, const char *service, mach_port_t *server_port);
  10. #define JAILBREAKD_COMMAND_ENTITLE 1
  11. #define JAILBREAKD_COMMAND_ENTITLE_AND_SIGCONT 2
  12. #define JAILBREAKD_COMMAND_ENTITLE_AND_SIGCONT_FROM_XPCPROXY 3
  13. #define JAILBREAKD_COMMAND_FIXUP_SETUID 4
  14. struct __attribute__((__packed__)) jb_connection {
  15. mach_port_t jbd_port;
  16. };
  17. typedef void *jb_connection_t;
  18. jb_connection_t jb_connect(void) {
  19. mach_port_t jbd_port;
  20. if (bootstrap_look_up(bootstrap_port, "org.coolstar.jailbreakd", &jbd_port) == 0) {
  21. struct jb_connection *conn = malloc(sizeof(struct jb_connection));
  22. conn->jbd_port = jbd_port;
  23. return (jb_connection_t)conn;
  24. }
  25. return NULL;
  26. }
  27. void jb_disconnect(jb_connection_t connection) {
  28. struct jb_connection *conn = (struct jb_connection *)connection;
  29. mach_port_deallocate(mach_task_self(), conn->jbd_port);
  30. free(conn);
  31. }
  32. void jb_entitle(jb_connection_t connection, pid_t pid, uint32_t what, jb_callback_t done) {
  33. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0UL), ^{
  34. struct jb_connection *conn = (struct jb_connection *)connection;
  35. int response = jbd_call(conn->jbd_port, JAILBREAKD_COMMAND_ENTITLE, pid);
  36. done(response);
  37. });
  38. }
  39. void jb_fix_setuid(jb_connection_t connection, pid_t pid, jb_callback_t done) {
  40. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0UL), ^{
  41. struct jb_connection *conn = (struct jb_connection *)connection;
  42. int response = jbd_call(conn->jbd_port, JAILBREAKD_COMMAND_FIXUP_SETUID, pid);
  43. done(response);
  44. });
  45. }
  46. int jb_entitle_now(jb_connection_t connection, pid_t pid, uint32_t what) {
  47. struct jb_connection *conn = (struct jb_connection *)connection;
  48. return jbd_call(conn->jbd_port, JAILBREAKD_COMMAND_ENTITLE, pid);
  49. }
  50. int jb_fix_setuid_now(jb_connection_t connection, pid_t pid) {
  51. struct jb_connection *conn = (struct jb_connection *)connection;
  52. return jbd_call(conn->jbd_port, JAILBREAKD_COMMAND_FIXUP_SETUID, pid);
  53. }
  54. void jb_oneshot_entitle_now(pid_t pid, uint32_t what) {
  55. jb_connection_t c = jb_connect();
  56. jb_entitle_now(c, pid, what);
  57. jb_disconnect(c);
  58. }
  59. void jb_oneshot_fix_setuid_now(pid_t pid) {
  60. jb_connection_t c = jb_connect();
  61. jb_fix_setuid_now(c, pid);
  62. jb_disconnect(c);
  63. }