Skip to content

Commit 0352d93

Browse files
committed
fix: few UBs; fix: missing error handler for malloc
This commit fixes some few UBs (Undefined Behaviors) based on numerous sanitizers, and also adds the missing error handling for a "malloc" call.
1 parent e11db94 commit 0352d93

5 files changed

Lines changed: 27 additions & 6 deletions

File tree

loader/src/ptracer/ptracer.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ bool inject_on_main(int pid, const char *lib_path) {
100100
For arm32 compatibility, we set the last bit to the same as the entry address
101101
*/
102102

103-
uintptr_t break_addr = (-0x05ec1cff & ~1) | ((uintptr_t)entry_addr & 1);
103+
/* INFO: (-0x0F & ~1) is a value below zero, while the one after "|"
104+
is an unsigned (must be 0 or greater) value, so we must
105+
cast the second value to signed long (intptr_t) to avoid
106+
undefined behavior.
107+
*/
108+
uintptr_t break_addr = (uintptr_t)((intptr_t)(-0x0F & ~1) | (intptr_t)((uintptr_t)entry_addr & 1));
104109
if (!write_proc(pid, (uintptr_t)addr_of_entry_addr, &break_addr, sizeof(break_addr))) return false;
105110

106111
ptrace(PTRACE_CONT, pid, 0, 0);
@@ -110,7 +115,7 @@ bool inject_on_main(int pid, const char *lib_path) {
110115
if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGSEGV) {
111116
if (!get_regs(pid, regs)) return false;
112117

113-
if (static_cast<uintptr_t>(regs.REG_IP & ~1) != (break_addr & ~1)) {
118+
if (((int)regs.REG_IP & ~1) != ((int)break_addr & ~1)) {
114119
LOGE("stopped at unknown addr %p", (void *) regs.REG_IP);
115120

116121
return false;
@@ -184,8 +189,14 @@ bool inject_on_main(int pid, const char *lib_path) {
184189
}
185190

186191
/* NOTICE: C++ -> C */
187-
char *err = (char *)malloc(dlerror_len + 1);
188-
read_proc(pid, (uintptr_t) dlerror_str_addr, err, dlerror_len);
192+
char *err = (char *)malloc((dlerror_len + 1) * sizeof(char));
193+
if (err == NULL) {
194+
LOGE("malloc err");
195+
196+
return false;
197+
}
198+
199+
read_proc(pid, dlerror_str_addr, err, dlerror_len + 1);
189200

190201
LOGE("dlerror info %s", err);
191202

loader/src/ptracer/utils.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,11 @@ void *find_func_addr(std::vector<MapInfo> &local_info, std::vector<MapInfo> &rem
313313

314314
/* WARNING: C++ keyword */
315315
void align_stack(struct user_regs_struct &regs, long preserve) {
316-
regs.REG_SP = (regs.REG_SP - preserve) & ~0xf;
316+
/* INFO: ~0xf is a negative value, and REG_SP is unsigned,
317+
so we must cast REG_SP to signed type before subtracting
318+
then cast back to unsigned type.
319+
*/
320+
regs.REG_SP = (uintptr_t)((intptr_t)(regs.REG_SP - preserve) & ~0xf);
317321
}
318322

319323
/* WARNING: C++ keyword */

zygiskd/src/companion.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void *entry_thread(void *arg) {
5454
pthread_exit(NULL);
5555
}
5656

57+
/* WARNING: Dynamic memory based */
5758
void entry(int fd) {
5859
LOGI("New companion entry.\n - Client fd: %d\n", fd);
5960

zygiskd/src/root_impl/kernelsu.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
#include "kernelsu.h"
1111

12-
#define KERNEL_SU_OPTION 0xdeadbeef
12+
/* INFO: It would be presumed it is a unsigned int,
13+
so we need to cast it to signed int to
14+
avoid any potential UB.
15+
*/
16+
#define KERNEL_SU_OPTION (signed int)0xdeadbeef
1317

1418
#define CMD_GET_VERSION 2
1519
#define CMD_UID_GRANTED_ROOT 12

zygiskd/src/zygiskd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ struct __attribute__((__packed__)) MsgHead {
360360
char data[0];
361361
};
362362

363+
/* WARNING: Dynamic memory based */
363364
void zygiskd_start(char *restrict argv[]) {
364365
LOGI("Welcome to ReZygisk %s Zygiskd!\n", ZKSU_VERSION);
365366

0 commit comments

Comments
 (0)