Skip to content

Commit 73a4333

Browse files
committed
Split to LCKProcessInterface, fix CocoaTop not showing in itself
1 parent a328cca commit 73a4333

6 files changed

Lines changed: 73 additions & 49 deletions

File tree

LCKXPCServer.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@
88

99
@protocol LCKXPCServiceProtocol
1010
- (void)proc_pidpath:(pid_t)pid reply:(void (^)(NSString *))reply;
11+
- (void)allRunningProcessesWithReply:(void (^)(NSArray<NSNumber *> *))reply;
1112

12-
- (void)pid:(pid_t)pid checkinWithInfo:(NSDictionary *)info;
13+
- (void)checkinWithInfo:(NSDictionary *)info;
1314
- (void)testShowAlert:(NSString *)msg;
1415
@end
1516

16-
@interface LCKXPCService : NSObject<NSXPCListenerDelegate, LCKXPCServiceProtocol>
17+
@interface LCKProcessInterface : NSObject<LCKXPCServiceProtocol>
18+
@property(nonatomic) NSXPCConnection *connection;
19+
@property(nonatomic) NSDictionary *info;
20+
- (instancetype)initWithConnection:(NSXPCConnection *)connection;
21+
@end
22+
23+
@interface LCKXPCService : NSObject<NSXPCListenerDelegate>
1724
@property(nonatomic) NSXPCListener *listener;
18-
@property(nonatomic) NSMutableDictionary *processList;
25+
@property(nonatomic) NSMutableDictionary<NSNumber *, LCKProcessInterface *> *processList;
26+
+ (instancetype)sharedInstanceIfExists;
1927
@end
2028

2129
@interface LCKXPCService(Client)

LCKXPCServer.m

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,49 @@
77
#import "LCKXPCServer.h"
88
#import "LCTaskForPIDTweak.h"
99

10-
@implementation LCKXPCService
11-
- (instancetype)init {
10+
@implementation LCKProcessInterface
11+
- (instancetype)initWithConnection:(NSXPCConnection *)connection {
1212
self = [super init];
13-
self.listener = [NSXPCListener anonymousListener];
14-
self.listener.delegate = self;
15-
[self.listener resume];
16-
17-
self.processList = [NSMutableDictionary dictionary];
13+
self.connection = connection;
1814
return self;
1915
}
16+
- (void)checkinWithInfo:(NSDictionary *)info {
17+
if(!self.info) self.info = info;
18+
}
19+
- (void)testShowAlert:(NSString *)message {
20+
LCShowAlert([NSString stringWithFormat:@"LCTaskForPIDTweak: %@", message]);
21+
}
22+
- (void)allRunningProcessesWithReply:(void (^)(NSArray<NSNumber *> *))reply {
23+
reply(LCKXPCService.sharedInstanceIfExists.processList.allKeys);
24+
}
2025

21-
// Protocol
2226
- (void)proc_pidpath:(pid_t)pid reply:(void (^)(NSString *))reply {
23-
reply(self.processList[@(pid)][@"ProgramArguments"][0]);
27+
reply(LCKXPCService.sharedInstanceIfExists.processList[@(pid)].info[@"ProgramArguments"][0]);
28+
}
29+
@end
30+
31+
@implementation LCKXPCService
32+
static LCKXPCService *sharedInstance;
33+
+ (instancetype)sharedInstanceIfExists {
34+
return sharedInstance;
2435
}
2536

26-
- (void)pid:(pid_t)pid checkinWithInfo:(NSDictionary *)info {
27-
// only allow writing to uninitialized entry
28-
if(self.processList[@(pid)] != NSNull.null) {
29-
//LCShowAlert([NSString stringWithFormat:@"LCKXPCService Violation: guest tried to overwrite exitsing process info"]);
30-
return;
31-
}
37+
- (instancetype)init {
38+
self = sharedInstance = [super init];
39+
self.listener = [NSXPCListener anonymousListener];
40+
self.listener.delegate = self;
41+
[self.listener resume];
3242

33-
self.processList[@(pid)] = info;
34-
}
35-
- (void)testShowAlert:(NSString *)message {
36-
LCShowAlert([NSString stringWithFormat:@"LCTaskForPIDTweak: %@", message]);
43+
self.processList = [NSMutableDictionary dictionary];
44+
return self;
3745
}
3846

3947
// NSXPCListenerDelegate
4048
- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection {
41-
// set process entry to uninitialized
42-
self.processList[@(newConnection.processIdentifier)] = NSNull.null;
43-
49+
LCKProcessInterface *obj = [[LCKProcessInterface alloc] initWithConnection:newConnection];
50+
self.processList[@(newConnection.processIdentifier)] = obj;
4451
newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(LCKXPCServiceProtocol)];
45-
newConnection.exportedObject = self;
52+
newConnection.exportedObject = obj;
4653
[newConnection resume];
4754
return YES;
4855
}

LCTaskForPIDTweak.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void LCShowAlert(NSString *msg);
2727
@end
2828

2929
// private C APIs
30+
int csops(pid_t pid, unsigned int ops, void * useraddr, size_t usersize);
3031
int csops_audittoken(pid_t pid, unsigned int ops, void * useraddr, size_t usersize, audit_token_t * token);
3132
int proc_pidpath(int pid, void *buffer, uint32_t buffersize);
3233
mach_port_t xpc_endpoint_copy_listener_port_4sim(xpc_object_t endpoint);

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Allows CocoaTop and VansonMod(?) to work jailed inside LiveContainer environment
1010

1111
## Known issues
1212
- No entitlement simulation for now, so an app can get task port of any other running apps
13-
- CocoaTop does not show itself in the process list
1413

1514
## Technical details
1615
### Hooked methods

TweakGuestApp.m

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@
77
static int hook_sysctl_CTL_KERN_PROC_ALL(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen) {
88
// TODO: implement via LCKXPCServer
99
struct kinfo_proc *procs = (struct kinfo_proc *)oldp;
10-
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:containerLockPath];
11-
if(!info) return 0;
10+
__block NSArray<NSNumber *> *pids;
11+
[LCKXPCService.sharedClientProxy allRunningProcessesWithReply:^(NSArray<NSNumber *> *p) {
12+
pids = p;
13+
}];
1214
size_t count = 0;
13-
for(NSDictionary *appUsageInfo in info.allValues) {
14-
if(![appUsageInfo isKindOfClass:NSDictionary.class]) continue;
15-
uint64_t val57 = [appUsageInfo[@"auditToken57"] longLongValue];
16-
audit_token_t token;
17-
token.val[5] = val57 >> 32;
18-
token.val[7] = val57 & 0xffffffff;
19-
csops_audittoken(token.val[5], 0, NULL, 0, &token);
15+
for(NSNumber *pid in pids) {
16+
csops(pid.intValue, 0, NULL, 0);
2017
if(errno == ESRCH) continue;
2118
if(oldp) {
2219
procs[count].kp_eproc.e_ucred.cr_uid = 501; // uid=501
2320
procs[count].kp_eproc.e_pcred.p_rgid = 501; // gid=501
2421
procs[count].kp_eproc.e_ppid = 1; // ppid=1
25-
procs[count].kp_proc.p_pid = token.val[5];
22+
procs[count].kp_proc.p_pid = pid.intValue;
2623
snprintf(procs[count].kp_proc.p_comm, sizeof(procs[count].kp_proc.p_comm), "LiveProcess");
2724
}
2825
count++;
@@ -75,10 +72,9 @@ void guest_check_in(void) {
7572
exc_port = 0;
7673

7774
// check in with the kernel server
78-
NSDictionary *info = @{
75+
[LCKXPCService.sharedClientProxy checkinWithInfo:@{
7976
@"ProgramArguments": NSProcessInfo.processInfo.arguments
80-
};
81-
[LCKXPCService.sharedClientProxy pid:getpid() checkinWithInfo:info];
77+
}];
8278
}
8379

8480
void init_guest_apps(void) {

TweakLiveContainer.m

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,28 @@ kern_return_t catch_mach_exception_raise_state_identity(mach_port_t exception_po
1818
mach_msg_server(mach_exc_server, sizeof(union __RequestUnion__catch_mach_exc_subsystem), exc_port, MACH_MSG_OPTION_NONE);
1919
return NULL;
2020
}
21-
static void *kernel_server(void *unused) {
22-
static LCKXPCService *service;
23-
assert(!service);
24-
service = [LCKXPCService new];
21+
22+
static void LCKStartKernelServer(void) {
23+
LCKXPCService *service = [LCKXPCService new];
2524
kern_port = service.listener.machPort;
2625

2726
// Expose the XPC service to the public
2827
kern_return_t kr = bootstrap_register(bootstrap_port, (char*)LCTaskForPIDTweak.kernelPortName.UTF8String, kern_port);
2928
if(kr != KERN_SUCCESS) {
3029
LCShowAlert([NSString stringWithFormat:@"LCTaskForPIDTweak: bootstrap_register(kern_port) failed: %d", kr]);
31-
return NULL;
3230
}
33-
34-
return NULL;
31+
}
32+
static void LCKStartFakeHostTaskPort(void) {
33+
// for now, LiveContainer itself cannot register its real task port since it would need a supporting process to handle the exception
34+
mach_port_t fake_task;
35+
kern_return_t kr;
36+
kr = bootstrap_check_in(bootstrap_port, [LCTaskForPIDTweak taskPortNameForPID:getpid()].UTF8String, &fake_task);
37+
if(kr != KERN_SUCCESS) {
38+
LCShowAlert([NSString stringWithFormat:@"LCTaskForPIDTweak: bootstrap_check_in(fake_task) failed: %d", kr]);
39+
return;
40+
}
41+
mach_port_insert_right(mach_task_self(), fake_task, fake_task, MACH_MSG_TYPE_MAKE_SEND);
42+
// TODO
3543
}
3644
void init_livecontainer(void) {
3745
// in LiveContainer we make a global exception port
@@ -45,6 +53,11 @@ void init_livecontainer(void) {
4553
mach_port_insert_right(mach_task_self(), exc_port, exc_port, MACH_MSG_TYPE_MAKE_SEND);
4654
pthread_t tExc;
4755
pthread_create(&tExc, NULL, exception_server, NULL);
48-
//pthread_create(&tKern, NULL, kernel_server, NULL);
49-
kernel_server(NULL);
56+
LCKStartKernelServer();
57+
//LCKStartFakeHostTaskPort();
58+
59+
// check in myself with the kernel server
60+
[LCKXPCService.sharedClientProxy checkinWithInfo:@{
61+
@"ProgramArguments": NSProcessInfo.processInfo.arguments
62+
}];
5063
}

0 commit comments

Comments
 (0)