Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 184 additions & 0 deletions app/src/main/cpp/Core/ChatFlagFix/ChatFlagFix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
#pragma once

#include <cstddef>
#include <cstdint>
#include <cstring>

namespace ChatFlagFix {

struct Site {
std::uintptr_t address = 0;
std::uint32_t original = 0;
std::uint32_t replacement = 0;
std::uint32_t flagOffset = 0;
};

enum class Decision { Write, AlreadyApplied, Foreign };

inline Decision Decide(std::uint32_t current, const Site &site) {
if (current == site.original)
return Decision::Write;
if (current == site.replacement)
return Decision::AlreadyApplied;
return Decision::Foreign;
}

namespace Detail {

constexpr std::uint32_t kAndClearBit1 = 0x121e7800u;
constexpr std::uint32_t kAndKeepBits5To7 = 0x121b7000u;
constexpr std::uint32_t kAndKeepBits0To1 = 0x12000400u;
constexpr int kLoadWindow = 4;
constexpr int kStepWindow = 4;
constexpr int kCallWindow = 6;
constexpr int kAddWindow = 12;
constexpr int kAdrpWindow = 5;

inline std::uint32_t Rd(std::uint32_t w) { return w & 0x1fu; }
inline std::uint32_t Rn(std::uint32_t w) { return (w >> 5) & 0x1fu; }

inline bool AndImmediate(std::uint32_t w, std::uint32_t base, std::uint32_t reg) {
return (w & 0xfffffc00u) == base && Rd(w) == reg && Rn(w) == reg;
}

inline bool OrrShifted(std::uint32_t w, std::uint32_t reg, std::uint32_t shift) {
return (w & 0xffe0fc00u) == (0x2a000000u | (shift << 10)) && Rd(w) == reg && Rn(w) == reg;
}

inline bool ByteAccess(std::uint32_t w, bool load, std::uint32_t &reg, std::uint32_t &base,
std::uint32_t &offset) {
if ((w & 0xffc00000u) != (load ? 0x39400000u : 0x39000000u))
return false;
reg = Rd(w);
base = Rn(w);
offset = (w >> 10) & 0xfffu;
return true;
}

inline bool IsCall(std::uint32_t w) { return (w >> 26) == 0x25u; }

inline std::uintptr_t AdrpTarget(std::uint32_t w, std::uintptr_t pc) {
std::int64_t page = (static_cast<std::int64_t>((w >> 5) & 0x7ffffu) << 2) | ((w >> 29) & 3u);
if (page & (std::int64_t{1} << 20))
page -= std::int64_t{1} << 21;
return (pc & ~static_cast<std::uintptr_t>(0xfffu)) + static_cast<std::uintptr_t>(page * 4096);
}

template <class Read>
bool Word(Read &read, std::uintptr_t at, std::uint32_t &w) {
return read(at, &w, sizeof(w));
}

template <class Read>
bool PassesChat(Read &read, std::uintptr_t call) {
for (int k = 1; k <= kAddWindow; ++k) {
std::uint32_t add = 0;
if (!Word(read, call - 4u * k, add))
return false;
if ((add & 0xffc003ffu) != 0x91000021u)
continue;
for (int j = 1; j <= kAdrpWindow; ++j) {
const std::uintptr_t at = call - 4u * (k + j);
std::uint32_t adrp = 0;
if (!Word(read, at, adrp))
return false;
if ((adrp & 0x9f00001fu) != 0x90000001u)
continue;
char text[5] = {};
const std::uintptr_t string = AdrpTarget(adrp, at) + ((add >> 10) & 0xfffu);
return read(string, text, sizeof(text)) && std::memcmp(text, "chat", sizeof(text)) == 0;
}
return false;
}
return false;
}

template <class Read>
bool Candidate(Read &read, std::uintptr_t at, Site &out) {
std::uint32_t w = 0;
if (!Word(read, at, w) || (w & 0xfffffc00u) != kAndClearBit1 || Rd(w) != Rn(w))
return false;
const std::uint32_t reg = Rd(w);

std::uintptr_t load = 0;
std::uint32_t record = 0, offset = 0;
for (int k = 1; k <= kLoadWindow && load == 0; ++k) {
std::uint32_t x = 0, t = 0, n = 0, imm = 0;
if (!Word(read, at - 4u * k, x))
return false;
if (ByteAccess(x, true, t, n, imm) && t == reg) {
load = at - 4u * k;
record = n;
offset = imm;
}
}
if (load == 0)
return false;

std::uintptr_t cursor = at, site = 0;
for (int step = 0; step < 5; ++step) {
bool matched = false;
for (int k = 1; k <= kStepWindow && !matched; ++k) {
std::uint32_t x = 0, t = 0, n = 0, imm = 0;
if (!Word(read, cursor + 4u * k, x))
return false;
switch (step) {
case 0:
matched = OrrShifted(x, reg, 1);
break;
case 1:
case 4:
matched = ByteAccess(x, false, t, n, imm) && t == reg && n == record && imm == offset;
break;
case 2:
matched = AndImmediate(x, kAndKeepBits5To7, reg);
break;
case 3:
matched = OrrShifted(x, reg, 2);
break;
}
if (matched) {
cursor += 4u * k;
if (step == 2)
site = cursor;
}
}
if (!matched)
return false;
}

for (int k = 1; k <= kCallWindow; ++k) {
std::uint32_t x = 0;
if (!Word(read, load - 4u * k, x))
return false;
if (!IsCall(x))
continue;
std::uint32_t original = 0;
if (!PassesChat(read, load - 4u * k) || !Word(read, site, original))
return false;
out.address = site;
out.original = original;
out.replacement = kAndKeepBits0To1 | (reg << 5) | reg;
out.flagOffset = offset;
return true;
}
return false;
}

}

template <class Read>
int Find(Read &&read, std::uintptr_t begin, std::size_t size, Site &out) {
int found = 0;
for (std::size_t i = 0; i + 4 <= size; i += 4) {
std::uint32_t w = 0;
if (!Detail::Word(read, begin + i, w) || (w & 0xfffffc00u) != Detail::kAndClearBit1)
continue;
Site site;
if (Detail::Candidate(read, begin + i, site) && ++found == 1)
out = site;
}
return found;
}

}
133 changes: 133 additions & 0 deletions app/src/main/cpp/Core/ChatFlagFix/ChatFlagFixInstall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "ChatFlagFixInstall.h"

#include "ChatFlagFix.h"

#include <link.h>

#include <cstdio>
#include <cstring>
#include <mutex>
#include <vector>

#include "../Cipher/Cipher.h"
#include "../../include/misc/Logger.h"

namespace {

struct Range {
std::uintptr_t begin;
std::size_t size;
bool executable;
};

struct Search {
const char *libName;
std::uintptr_t base;
std::vector<Range> ranges;
};

bool EndsWith(const char *text, const char *suffix) {
const std::size_t a = std::strlen(text), b = std::strlen(suffix);
return a >= b && std::strcmp(text + a - b, suffix) == 0;
}

int CollectRanges(dl_phdr_info *info, std::size_t, void *data) {
auto *search = static_cast<Search *>(data);
if (info->dlpi_name == nullptr || !EndsWith(info->dlpi_name, search->libName))
return 0;
search->base = info->dlpi_addr;
for (int i = 0; i < info->dlpi_phnum; ++i) {
const ElfW(Phdr) &ph = info->dlpi_phdr[i];
if (ph.p_type != PT_LOAD || (ph.p_flags & PF_R) == 0)
continue;
search->ranges.push_back({info->dlpi_addr + ph.p_vaddr, static_cast<std::size_t>(ph.p_filesz),
(ph.p_flags & PF_X) != 0});
}
return 1;
}

std::mutex g_installMutex;
bool g_finished = false;

}

namespace ChatFlagFix {

bool Install(const char *libName) {
std::lock_guard<std::mutex> lock(g_installMutex);
if (g_finished)
return true;

Search search{libName, 0, {}};
dl_iterate_phdr(CollectRanges, &search);
if (search.ranges.empty()) {
LOGD("Chat fix: %s not loaded yet", libName);
return false;
}

auto read = [&search](std::uintptr_t at, void *out, std::size_t size) {
for (const Range &range : search.ranges) {
if (at >= range.begin && size <= range.size && at - range.begin <= range.size - size) {
std::memcpy(out, reinterpret_cast<const void *>(at), size);
return true;
}
}
return false;
};

int found = 0;
Site site;
for (const Range &range : search.ranges) {
if (!range.executable)
continue;
Site candidate;
const int count = Find(read, range.begin, range.size, candidate);
if (count > 0 && found == 0)
site = candidate;
found += count;
}

g_finished = true;
if (found != 1) {
LOGW("Chat fix: %d candidate sites in %s, not patching", found, libName);
return false;
}

std::uint32_t current = 0;
if (!read(site.address, &current, sizeof(current))) {
LOGW("Chat fix: site %p unreadable, not patching", reinterpret_cast<void *>(site.address));
return false;
}

switch (Decide(current, site)) {
case Decision::AlreadyApplied:
LOGI("Chat fix: already applied at %p", reinterpret_cast<void *>(site.address));
return true;
case Decision::Foreign:
LOGW("Chat fix: %p holds %08x, expected %08x, not patching",
reinterpret_cast<void *>(site.address), current, site.original);
return false;
case Decision::Write:
break;
}

char hex[9] = {};
const auto *bytes = reinterpret_cast<const unsigned char *>(&site.replacement);
std::snprintf(hex, sizeof(hex), "%02x%02x%02x%02x", bytes[0], bytes[1], bytes[2], bytes[3]);
auto *patch = new CipherPatch();
patch->set_Address(site.address, false);
patch->set_Opcode(hex);
patch->Fire();
__builtin___clear_cache(reinterpret_cast<char *>(site.address), reinterpret_cast<char *>(site.address + 4));

std::uint32_t written = 0;
if (!read(site.address, &written, sizeof(written)) || written != site.replacement) {
LOGW("Chat fix: patch at %p did not take (%08x)", reinterpret_cast<void *>(site.address), written);
return false;
}
LOGI("Chat fix: flag mask patched at %p (RVA 0x%lx, flag byte +0x%x)", reinterpret_cast<void *>(site.address),
static_cast<unsigned long>(site.address - search.base), site.flagOffset);
return true;
}

}
7 changes: 7 additions & 0 deletions app/src/main/cpp/Core/ChatFlagFix/ChatFlagFixInstall.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

namespace ChatFlagFix {

bool Install(const char *libName);

}
52 changes: 2 additions & 50 deletions app/src/main/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "fileselector/fileselector.h"
#include <android/asset_manager_jni.h>
#include "shadowhook.h"
#include "ChatFlagFix/ChatFlagFixInstall.h"

extern "C" {
#include "Core/ceserver/ceserver.h"
Expand Down Expand Up @@ -217,57 +218,8 @@ PRIVATE_API void Canvas::CanvasMenu() {
//SystemsTest();
}

// Chat Record Constructor Hook (Sky 0.34.5) to fix blue messages bug without breaking permissions
static void* (*orig_ChatRecordConstructor)(void*, void*, void*, void*, void*, void*, void*, void*) = nullptr;
static void* g_chatRecordStub = nullptr;

static void* hooked_ChatRecordConstructor(void* self, void* a2, void* a3, void* a4, void* a5, void* a6, void* a7, void* a8) {
if (self != nullptr) {
// Strip only the broadcast/pending bits (0xE0)
// Preserve lower permission/channel bits (0x1F)
uint8_t* pFlags = (uint8_t*)self + 20;
*pFlags &= ~0xe0;
}
void* ret = orig_ChatRecordConstructor ? orig_ChatRecordConstructor(self, a2, a3, a4, a5, a6, a7, a8) : self;
if (self != nullptr) {
uint8_t* pFlags = (uint8_t*)self + 20;
*pFlags &= ~0xe0;
}
return ret;
}

static void install_chat_fix() {
if (g_chatRecordStub != nullptr) return;

uintptr_t base = Canvas::findLib(Canvas::libName ? Canvas::libName : "libBootloader.so");
if (base == 0) {
LOGD("libBootloader.so not loaded yet for chat fix");
return;
}

uintptr_t targetAddr = 0;

// Try known version RVA (Sky 0.34.5)
const uintptr_t CHAT_RECORD_CONSTRUCTOR_RVA = 0x13f23b8;
targetAddr = base + CHAT_RECORD_CONSTRUCTOR_RVA;

// Safety check: Ensure target address is 4-byte aligned (ARM64 requirement)
if ((targetAddr & 3) != 0) {
LOGW("Chat fix: Target address %p is misaligned, safely skipping hook", (void*)targetAddr);
return;
}

g_chatRecordStub = shadowhook_hook_func_addr(
(void*)targetAddr,
(void*)hooked_ChatRecordConstructor,
(void**)&orig_ChatRecordConstructor
);
if (g_chatRecordStub != nullptr) {
LOGI("Chat record constructor hook installed at %p (RVA 0x%lx)", (void*)targetAddr, targetAddr - base);
} else {
int err = shadowhook_get_errno();
LOGW("Chat record constructor hook skipped (err %d: %s)", err, shadowhook_to_errmsg(err));
}
ChatFlagFix::Install(Canvas::libName ? Canvas::libName : "libBootloader.so");
}

__unused __attribute__((constructor))
Expand Down
Loading
Loading