Skip to content

Commit ed22149

Browse files
committed
CR feedback
1 parent e75a694 commit ed22149

5 files changed

Lines changed: 34 additions & 13 deletions

File tree

patcher-darwin/src/main.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <chrono>
55
#include <cstdio>
6+
#include <cstring>
67
#include <string>
78
#include <thread>
89
#include <unistd.h>
@@ -17,9 +18,9 @@ static void print_usage() {
1718
printf(" --headless Patch once and exit\n");
1819
printf(" --domain <url> Server domain (default: "
1920
"127.0.0.1)\n");
20-
printf(" --use-http Use HTTP instead of HTTPS\n");
21+
printf(" --dont-use-http Use HTTPS instead of HTTP\n");
2122
printf(
22-
" --optional-dynamic-resources Allow optional dynamic resources\n");
23+
" --non-optional-dynamic-resources Allow optional dynamic resources\n");
2324
printf(" --help Show this help message\n");
2425
}
2526

@@ -50,8 +51,8 @@ int main(int argc, char *argv[]) {
5051
}
5152
} else if (strcmp(argv[i], "--dont-use-http") == 0) {
5253
use_http = false;
53-
} else if (strcmp(argv[i], "--optional-dynamic-resources") == 0) {
54-
optional_dynres = true;
54+
} else if (strcmp(argv[i], "--non-optional-dynamic-resources") == 0) {
55+
optional_dynres = false;
5556
} else if (strcmp(argv[i], "--help") == 0) {
5657
print_usage();
5758
return 0;

patcher-darwin/src/memory.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ namespace peacock {
7575

7676
if (kr == KERN_SUCCESS) {
7777
*old_prot = info.protection;
78+
if (object_name != MACH_PORT_NULL)
79+
mach_port_deallocate(mach_task_self(), object_name);
7880
} else {
7981
*old_prot = VM_PROT_NONE;
8082
}

patcher-darwin/src/patcher.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ namespace peacock {
3232
if (!scanned)
3333
return nullptr;
3434

35-
PatchRegistry::instance().add_version(identifier, std::move(*scanned));
36-
return PatchRegistry::instance().find_version(identifier);
35+
if (identifier != 0) {
36+
PatchRegistry::instance().add_version(identifier, std::move(*scanned));
37+
return PatchRegistry::instance().find_version(identifier);
38+
}
39+
40+
return new HitmanVersion(std::move(*scanned));
3741
}
3842

3943
/// Collect patches to apply based on user options.
@@ -91,6 +95,7 @@ namespace peacock {
9195
if (!Memory::write(task, addr, data_to_write.data(),
9296
data_to_write.size())) {
9397
log("Failed to write at offset 0x" + to_hex(patch->offset));
98+
Memory::protect(task, addr, data_to_write.size(), old_prot, nullptr);
9499
return false;
95100
}
96101

@@ -159,8 +164,15 @@ namespace peacock {
159164

160165
const auto patches = collect_patches(*version, options);
161166

162-
// Build the custom URL bytes
167+
// Build the custom URL bytes (must fit in the configdomain buffer)
168+
static constexpr size_t kMaxConfigDomainLen = 256;
163169
const std::string &url = options.custom_config_domain;
170+
if (url.size() >= kMaxConfigDomainLen) {
171+
log("Custom config domain too long (" + std::to_string(url.size()) +
172+
" bytes, max " + std::to_string(kMaxConfigDomainLen - 1) + ")");
173+
Memory::close_task(task);
174+
return false;
175+
}
164176
std::vector<uint8_t> url_bytes(url.begin(), url.end());
165177
url_bytes.push_back(0x00);
166178

patcher-darwin/src/process.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "process.h"
22

3+
#include <cstring>
34
#include <libproc.h>
45
#include <mach-o/dyld_images.h>
56
#include <mach-o/loader.h>
@@ -89,9 +90,11 @@ namespace peacock {
8990
if (lc.cmd == LC_SEGMENT_64) {
9091
segment_command_64 seg{};
9192
out_size = sizeof(seg);
92-
mach_vm_read_overwrite(
93+
kr = mach_vm_read_overwrite(
9394
task, cmd_offset, sizeof(seg),
9495
reinterpret_cast<mach_vm_address_t>(&seg), &out_size);
96+
if (kr != KERN_SUCCESS)
97+
return false;
9598

9699
// skip __PAGEZERO — it's a non-readable guard region
97100
if (strcmp(seg.segname, SEG_PAGEZERO) == 0) {
@@ -173,9 +176,9 @@ namespace peacock {
173176
return false;
174177
}
175178

176-
compute_image_size(task, base_address, image_size);
179+
const bool ok = compute_image_size(task, base_address, image_size);
177180

178181
mach_port_deallocate(mach_task_self(), task);
179-
return true;
182+
return ok;
180183
}
181184
}

patcher-darwin/src/scanner.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "scanner.h"
22
#include "log.h"
3+
#include <cstring>
34

45
namespace peacock {
56
// ARM64 instructions are 4-byte aligned; use step of 4 instead of 16
@@ -63,9 +64,11 @@ namespace peacock {
6364
const uint8_t effective_alignment = alignment + leading_wildcards;
6465

6566
const size_t pat_len = byte_pattern.size();
66-
const size_t search_end = data.size() - pat_len;
6767

68-
for (size_t i = effective_alignment; i < search_end; i += kArm64InstructionAlignment) {
68+
if (data.size() < pat_len)
69+
return results;
70+
71+
for (size_t i = effective_alignment; i + pat_len <= data.size(); i += kArm64InstructionAlignment) {
6972
if (data[i] != byte_pattern[0])
7073
continue;
7174

@@ -203,7 +206,7 @@ namespace peacock {
203206
// Verify the cbz: byte 3 should be 0x34 (CBZ w-register)
204207
if (auth1_orig[3] != 0x34) {
205208
log("AOB scan failed: expected CBZ at authheader patch 1 "
206-
"(got 0x" + std::to_string(auth1_orig[3]) + ")");
209+
"(got 0x" + to_hex(auth1_orig[3]) + ")");
207210
return std::nullopt;
208211
}
209212

0 commit comments

Comments
 (0)