Shims.x 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #import "rocketbootstrap_internal.h"
  2. #import <CaptainHook/CaptainHook.h>
  3. #import <libkern/OSAtomic.h>
  4. #import <substrate.h>
  5. static OSSpinLock spin_lock;
  6. kern_return_t bootstrap_look_up3(mach_port_t bp, const name_t service_name, mach_port_t *sp, pid_t target_pid, const uuid_t instance_id, uint64_t flags) __attribute__((weak_import));
  7. kern_return_t (*_bootstrap_look_up3)(mach_port_t bp, const name_t service_name, mach_port_t *sp, pid_t target_pid, const uuid_t instance_id, uint64_t flags);
  8. kern_return_t $bootstrap_look_up3(mach_port_t bp, const name_t service_name, mach_port_t *sp, pid_t target_pid, const uuid_t instance_id, uint64_t flags)
  9. {
  10. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  11. NSMutableDictionary *threadDictionary = [NSThread currentThread].threadDictionary;
  12. id obj = [threadDictionary objectForKey:@"rocketbootstrap_intercept_next_lookup"];
  13. if (obj) {
  14. [threadDictionary removeObjectForKey:@"rocketbootstrap_intercept_next_lookup"];
  15. [pool drain];
  16. return rocketbootstrap_look_up(bp, service_name, sp);
  17. }
  18. [pool drain];
  19. return _bootstrap_look_up3(bp, service_name, sp, target_pid, instance_id, flags);
  20. }
  21. static void hook_bootstrap_lookup(void)
  22. {
  23. static bool hooked_bootstrap_look_up;
  24. OSSpinLockLock(&spin_lock);
  25. if (!hooked_bootstrap_look_up) {
  26. MSHookFunction(bootstrap_look_up3, $bootstrap_look_up3, (void **)&_bootstrap_look_up3);
  27. hooked_bootstrap_look_up = true;
  28. }
  29. OSSpinLockUnlock(&spin_lock);
  30. }
  31. CFMessagePortRef rocketbootstrap_cfmessageportcreateremote(CFAllocatorRef allocator, CFStringRef name)
  32. {
  33. if (rocketbootstrap_is_passthrough())
  34. return CFMessagePortCreateRemote(allocator, name);
  35. hook_bootstrap_lookup();
  36. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  37. NSMutableDictionary *threadDictionary = [NSThread currentThread].threadDictionary;
  38. [threadDictionary setObject:(id)kCFBooleanTrue forKey:@"rocketbootstrap_intercept_next_lookup"];
  39. CFMessagePortRef result = CFMessagePortCreateRemote(allocator, name);
  40. [threadDictionary removeObjectForKey:@"rocketbootstrap_intercept_next_lookup"];
  41. [pool drain];
  42. return result;
  43. }
  44. kern_return_t rocketbootstrap_cfmessageportexposelocal(CFMessagePortRef messagePort)
  45. {
  46. if (rocketbootstrap_is_passthrough())
  47. return 0;
  48. CFStringRef name = CFMessagePortGetName(messagePort);
  49. if (!name)
  50. return -1;
  51. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  52. kern_return_t result = rocketbootstrap_unlock([(NSString *)name UTF8String]);
  53. [pool drain];
  54. return result;
  55. }
  56. @interface CPDistributedMessagingCenter : NSObject
  57. - (void)_setupInvalidationSource;
  58. @end
  59. %group messaging_center
  60. static bool has_hooked_messaging_center;
  61. %hook CPDistributedMessagingCenter
  62. - (mach_port_t)_sendPort
  63. {
  64. if (objc_getAssociatedObject(self, &has_hooked_messaging_center)) {
  65. mach_port_t *_sendPort = CHIvarRef(self, _sendPort, mach_port_t);
  66. NSLock **_lock = CHIvarRef(self, _lock, NSLock *);
  67. if (_sendPort && _lock) {
  68. [*_lock lock];
  69. mach_port_t result = *_sendPort;
  70. if (result == MACH_PORT_NULL) {
  71. NSString **_centerName = CHIvarRef(self, _centerName, NSString *);
  72. if (_centerName && *_centerName && [self respondsToSelector:@selector(_setupInvalidationSource)]) {
  73. mach_port_t bootstrap = MACH_PORT_NULL;
  74. task_get_bootstrap_port(mach_task_self(), &bootstrap);
  75. rocketbootstrap_look_up(bootstrap, [*_centerName UTF8String], _sendPort);
  76. [self _setupInvalidationSource];
  77. result = *_sendPort;
  78. }
  79. }
  80. [*_lock unlock];
  81. return result;
  82. }
  83. }
  84. return %orig();
  85. }
  86. - (void)runServerOnCurrentThreadProtectedByEntitlement:(id)entitlement
  87. {
  88. if (objc_getAssociatedObject(self, &has_hooked_messaging_center)) {
  89. NSString **_centerName = CHIvarRef(self, _centerName, NSString *);
  90. if (_centerName && *_centerName) {
  91. rocketbootstrap_unlock([*_centerName UTF8String]);
  92. }
  93. }
  94. %orig();
  95. }
  96. %end
  97. %end
  98. void rocketbootstrap_distributedmessagingcenter_apply(CPDistributedMessagingCenter *messaging_center)
  99. {
  100. if (rocketbootstrap_is_passthrough())
  101. return;
  102. OSSpinLockLock(&spin_lock);
  103. if (!has_hooked_messaging_center) {
  104. has_hooked_messaging_center = true;
  105. %init(messaging_center);
  106. }
  107. OSSpinLockUnlock(&spin_lock);
  108. objc_setAssociatedObject(messaging_center, &has_hooked_messaging_center, (id)kCFBooleanTrue, OBJC_ASSOCIATION_ASSIGN);
  109. }