From c02c494ce4018c6eb8377128e747eafe4fb187e2 Mon Sep 17 00:00:00 2001 From: ningyu Date: Wed, 5 Jun 2024 03:08:47 +0000 Subject: [PATCH] all: add c & rust compilation options Signed-off-by: ningyu --- CMakeLists.txt | 13 ++- upatch-diff/create-diff-object.c | 38 +++---- upatch-diff/elf-common.c | 8 +- upatch-diff/elf-common.h | 14 +-- upatch-diff/elf-compare.c | 10 +- upatch-diff/elf-correlate.c | 16 +-- upatch-diff/elf-create.c | 22 ++--- upatch-diff/elf-create.h | 4 +- upatch-diff/elf-debug.c | 14 +-- upatch-diff/elf-insn.c | 4 +- upatch-diff/insn/asm/inat.h | 4 +- upatch-diff/insn/asm/insn.h | 6 +- upatch-diff/insn/insn.c | 4 +- upatch-diff/list.h | 2 +- upatch-diff/running-elf.c | 11 +-- upatch-diff/upatch-elf.c | 6 +- upatch-diff/upatch-elf.h | 2 +- upatch-hijacker/hijacker/gnu-as-hijacker.c | 10 +- .../hijacker/gnu-compiler-hijacker.c | 4 +- upatch-hijacker/hijacker/hijacker.h | 4 +- upatch-manage/arch/aarch64/insn.c | 12 +-- upatch-manage/arch/aarch64/insn.h | 4 +- upatch-manage/arch/aarch64/ptrace.c | 53 +++++----- upatch-manage/arch/aarch64/relocation.c | 98 +++++++++---------- upatch-manage/arch/x86_64/ptrace.c | 41 ++++---- upatch-manage/arch/x86_64/relocation.c | 5 +- upatch-manage/arch/x86_64/resolve.c | 3 +- upatch-manage/upatch-common.h | 4 +- upatch-manage/upatch-elf.c | 16 +-- upatch-manage/upatch-elf.h | 12 +-- upatch-manage/upatch-manage.c | 24 ++--- upatch-manage/upatch-patch.c | 79 +++++++-------- upatch-manage/upatch-process.c | 33 ++++--- upatch-manage/upatch-process.h | 2 +- upatch-manage/upatch-ptrace.c | 45 ++++----- upatch-manage/upatch-ptrace.h | 25 ++--- upatch-manage/upatch-resolve.c | 17 ++-- upatch-manage/upatch-resolve.h | 2 +- 38 files changed, 324 insertions(+), 347 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c7838a8..4858ba5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,8 +54,15 @@ message("---------------------------------------------------------") # Compile options add_compile_options(-DBUILD_VERSION="${BUILD_VERSION}") -add_compile_options(-g -Wall -O2 -fPIE) - +add_compile_options(-std=gnu99 -g -Wall -D_FORTIFY_SOURCE=2 -O2 -Werror -Wextra + -Wtrampolines -Wformat=2 -Wstrict-prototypes -Wdate-time -Wstack-usage=8192 + -Wfloat-equal -Wswitch-default -Wshadow -Wconversion -Wcast-qual -Wcast-align + -Wunused -Wundef -funsigned-char -fstack-protector-all -fpic -fpie -ftrapv + -fstack-check -freg-struct-return -fno-canonical-system-headers -pipe + -fdebug-prefix-map=old=new) +set(LINK_FLAGS "-pie -Wl,-z,relro,-z,now -Wl,-z,noexecstack -rdynamic -Wl,-Bsymbolic -Wl,-no-undefined") +set(CMAKE_SHARED_LINKER_FLAGS "${LINK_FLAGS}") +set(CMAKE_EXE_LINKER_FLAGS "${LINK_FLAGS}") # Subdirectories add_subdirectory(upatch-diff) add_subdirectory(upatch-manage) @@ -67,7 +74,7 @@ add_custom_target(rust-executables ALL COMMENT "Building rust executables..." COMMAND ${CMAKE_COMMAND} -E env "BUILD_VERSION=${BUILD_VERSION}" - "RUSTFLAGS=--cfg unsound_local_offset" + "RUSTFLAGS=--cfg unsound_local_offset -C relocation_model=pic -D warnings -C link-arg=-s -C overflow_checks -W rust_2021_incompatible_closure_captures" cargo build --release --target-dir ${CMAKE_CURRENT_BINARY_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) diff --git a/upatch-diff/create-diff-object.c b/upatch-diff/create-diff-object.c index 1a05869..6474b22 100644 --- a/upatch-diff/create-diff-object.c +++ b/upatch-diff/create-diff-object.c @@ -76,11 +76,11 @@ struct arguments { }; static struct argp_option options[] = { - {"debug", 'd', NULL, 0, "Show debug output"}, - {"source", 's', "source", 0, "Source object"}, - {"patched", 'p', "patched", 0, "Patched object"}, - {"running", 'r', "running", 0, "Running binary file"}, - {"output", 'o', "output", 0, "Output object"}, + {"debug", 'd', NULL, 0, "Show debug output", 0}, + {"source", 's', "source", 0, "Source object", 0}, + {"patched", 'p', "patched", 0, "Patched object", 0}, + {"running", 'r', "running", 0, "Running binary file", 0}, + {"output", 'o', "output", 0, "Output object", 0}, {NULL} }; @@ -136,7 +136,7 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) return 0; } -static struct argp argp = {options, parse_opt, args_doc, program_doc}; +static struct argp argp = {options, parse_opt, args_doc, program_doc, NULL, NULL, NULL}; /* * Key point for chreate-diff-object: @@ -266,7 +266,7 @@ static void bundle_symbols(struct upatch_elf *uelf) list_for_each_entry(sym, &uelf->symbols, list) { if (is_bundleable(sym)) { if (sym->sym.st_value != 0 && - is_gcc6_localentry_bundled_sym(uelf, sym)) { + is_gcc6_localentry_bundled_sym(uelf)) { ERROR("Symbol '%s' at offset %lu within section '%s', expected 0.", sym->name, sym->sym.st_value, sym->sec->name); } @@ -301,7 +301,7 @@ static void detect_child_functions(struct upatch_elf *uelf) if (!childstr) continue; - pname = strndup(sym->name, childstr - sym->name); + pname = strndup(sym->name, (size_t)(childstr - sym->name)); log_debug("symbol '%s', pname: '%s'\n", sym->name, pname); if (!pname) ERROR("detect_child_functions strndup failed."); @@ -576,8 +576,8 @@ static void replace_section_syms(struct upatch_elf *uelf) if (sym->type == STT_SECTION || sym->sec != rela->sym->sec) continue; - start = sym->sym.st_value; - end = sym->sym.st_value + sym->sym.st_size; + start = (long)sym->sym.st_value; + end = (long)(sym->sym.st_value + sym->sym.st_size); /* text section refer other sections */ if (is_text_section(relasec->base) && @@ -638,7 +638,7 @@ static void replace_section_syms(struct upatch_elf *uelf) if (!found && !is_string_literal_section(rela->sym->sec) && strncmp(rela->sym->name, ".rodata", strlen(".rodata")) && strncmp(rela->sym->name, ".data", strlen(".data"))) { - ERROR("%s+0x%x: Cannot find replacement symbol for '%s+%ld' reference.", + ERROR("%s+0x%lx: Cannot find replacement symbol for '%s+%ld' reference.", relasec->base->name, rela->offset, rela->sym->name, rela->addend); } } @@ -662,8 +662,8 @@ static void mark_ignored_sections(struct upatch_elf *uelf) } /* TODO: we do not handle it now */ -static void mark_ignored_functions_same(struct upatch_elf *uelf) {} -static void mark_ignored_sections_same(struct upatch_elf *uelf) {} +static void mark_ignored_functions_same(void) {} +static void mark_ignored_sections_same(void) {} /* * For a local symbol referenced in the rela list of a changing function, @@ -845,7 +845,7 @@ static void include_debug_sections(struct upatch_elf *uelf) } /* currently, there si no special section need to be handled */ -static void process_special_sections(struct upatch_elf *uelf) {} +static void process_special_sections(void) {} static void verify_patchability(struct upatch_elf *uelf) { @@ -973,8 +973,8 @@ int main(int argc, char*argv[]) mark_file_symbols(&uelf_source); find_debug_symbol(&uelf_source, &relf); - mark_ignored_functions_same(&uelf_patched); - mark_ignored_sections_same(&uelf_patched); + mark_ignored_functions_same(); + mark_ignored_sections_same(); upatch_elf_teardown(&uelf_source); upatch_elf_free(&uelf_source); @@ -990,7 +990,7 @@ int main(int argc, char*argv[]) include_debug_sections(&uelf_patched); - process_special_sections(&uelf_patched); + process_special_sections(); upatch_print_changes(&uelf_patched); @@ -1011,7 +1011,7 @@ int main(int argc, char*argv[]) upatch_create_intermediate_sections(&uelf_out, &relf); - create_kpatch_arch_section(&uelf_out); + create_kpatch_arch_section(); upatch_build_strings_section_data(&uelf_out); @@ -1029,7 +1029,7 @@ int main(int argc, char*argv[]) upatch_rebuild_relocations(&uelf_out); - upatch_check_relocations(&uelf_out); + upatch_check_relocations(); upatch_create_shstrtab(&uelf_out); diff --git a/upatch-diff/elf-common.c b/upatch-diff/elf-common.c index f895e9b..a74da3a 100644 --- a/upatch-diff/elf-common.c +++ b/upatch-diff/elf-common.c @@ -74,8 +74,10 @@ bool is_normal_static_local(struct symbol *sym) if (!strchr(sym->name, '.')) return false; - if (is_special_static(sym)) - return false; + /* + * TODO: Special static local variables should never be correlated and should always + * be included if they are referenced by an included function. + */ return true; } @@ -97,7 +99,7 @@ int offset_of_string(struct list_head *list, char *name) } // no need for X86 -bool is_gcc6_localentry_bundled_sym(struct upatch_elf *uelf, struct symbol *sym) +bool is_gcc6_localentry_bundled_sym(struct upatch_elf *uelf) { switch(uelf->arch) { case AARCH64: diff --git a/upatch-diff/elf-common.h b/upatch-diff/elf-common.h index 9e44a7c..f3d4308 100644 --- a/upatch-diff/elf-common.h +++ b/upatch-diff/elf-common.h @@ -103,7 +103,7 @@ static inline bool is_debug_section(struct section *sec) !strncmp(name, ".eh_frame", 9); } -static inline struct symbol *find_symbol_by_index(struct list_head *list, size_t index) +static inline struct symbol *find_symbol_by_index(struct list_head *list, unsigned int index) { struct symbol *sym; @@ -174,16 +174,6 @@ static inline bool has_digit_tail(char *tail) */ int mangled_strcmp(char *, char *); - -/* - * TODO: Special static local variables should never be correlated and should always - * be included if they are referenced by an included function. - */ -static inline bool is_special_static(struct symbol *sym){ - /* Not need it now. */ - return false; -} - bool is_normal_static_local(struct symbol *); static inline char *section_function_name(struct section *sec) @@ -243,7 +233,7 @@ static inline bool is_local_sym(struct symbol *sym) return sym->bind == STB_LOCAL; } -bool is_gcc6_localentry_bundled_sym(struct upatch_elf *, struct symbol *); +bool is_gcc6_localentry_bundled_sym(struct upatch_elf *); /* * Mapping symbols are used to mark and label the transitions between code and diff --git a/upatch-diff/elf-compare.c b/upatch-diff/elf-compare.c index ef8dd23..851c25f 100644 --- a/upatch-diff/elf-compare.c +++ b/upatch-diff/elf-compare.c @@ -175,7 +175,7 @@ bool upatch_handle_redis_line(const char *symname) } /* TODO: let user support this list or generate by the compiler ? */ -bool check_line_func(struct upatch_elf *uelf, const char *symname) +bool check_line_func(const char *symname) { if (!strncmp(basename(g_relf_name), "redis-server", 12)) return upatch_handle_redis_line(symname); @@ -247,7 +247,7 @@ static bool _line_macro_change_only(struct upatch_elf *uelf, struct section *sec continue; /* TODO: we may need black list ? */ - if (check_line_func(uelf, rela->sym->name)) { + if (check_line_func(rela->sym->name)) { found = true; break; } @@ -292,8 +292,8 @@ static bool _line_macro_change_only_aarch64(struct upatch_elf *uelf, struct sect continue; /* verify it's a mov immediate to w1 */ - if ((*(int *)(start1 + offset) & ~mov_imm_mask) != - (*(int *)(start2 + offset) & ~mov_imm_mask)) + if ((*(unsigned int *)(start1 + offset) & ~mov_imm_mask) != + (*(unsigned int *)(start2 + offset) & ~mov_imm_mask)) return false; found = false; @@ -304,7 +304,7 @@ static bool _line_macro_change_only_aarch64(struct upatch_elf *uelf, struct sect continue; /* TODO: we may need black list ? */ - if (check_line_func(uelf, rela->sym->name)) { + if (check_line_func(rela->sym->name)) { found = true; break; } diff --git a/upatch-diff/elf-correlate.c b/upatch-diff/elf-correlate.c index a0fe669..3e3a536 100644 --- a/upatch-diff/elf-correlate.c +++ b/upatch-diff/elf-correlate.c @@ -58,9 +58,10 @@ void upatch_correlate_symbols(struct upatch_elf *uelf_source, struct upatch_elf sym_orig->type != sym_patched->type || sym_patched->twin) continue; - if (is_special_static(sym_orig)) - continue; - + /* + * TODO: Special static local variables should never be correlated and should always + * be included if they are referenced by an included function. + */ /* * The .LCx symbols point to string literals in * '.rodata..str1.*' sections. They get included @@ -140,11 +141,10 @@ void upatch_correlate_sections(struct upatch_elf *uelf_source, struct upatch_elf sec_patched->twin) continue; - if (is_special_static(is_rela_section(sec_orig) ? - sec_orig->base->secsym : - sec_orig->secsym)) - continue; - + /* + * TODO: Special static local variables should never be correlated and should always + * be included if they are referenced by an included function. + */ /* * Group sections must match exactly to be correlated. */ diff --git a/upatch-diff/elf-create.c b/upatch-diff/elf-create.c index 873b3a9..8ac212a 100644 --- a/upatch-diff/elf-create.c +++ b/upatch-diff/elf-create.c @@ -37,7 +37,7 @@ /* create text and relocation sections */ static struct section *create_section_pair(struct upatch_elf *uelf, char *name, - int entsize, int nr) + unsigned int entsize, unsigned int nr) { char *relaname; struct section *sec, *relasec; @@ -131,7 +131,7 @@ void upatch_create_patches_sections(struct upatch_elf *uelf, struct running_elf struct upatch_patch_func *funcs; struct rela *rela; struct lookup_result symbol; - int nr = 0, index = 0; + unsigned int nr = 0, index = 0; /* find changed func */ list_for_each_entry(sym, &uelf->symbols, list) { @@ -197,7 +197,7 @@ void upatch_create_patches_sections(struct upatch_elf *uelf, struct running_elf ERROR("sanity check failed in funcs sections. \n"); } -static bool need_dynrela(struct upatch_elf *uelf, struct running_elf *relf, +static bool need_dynrela(struct running_elf *relf, struct section *relasec, struct rela *rela) { struct lookup_result symbol; @@ -234,7 +234,7 @@ void upatch_create_intermediate_sections(struct upatch_elf *uelf, struct running struct upatch_symbol *usyms; struct upatch_relocation *urelas; struct symbol *strsym, *usym_sec_sym; - int nr = 0, index = 0; + unsigned int nr = 0, index = 0; list_for_each_entry(relasec, &uelf->sections, list) { if (!is_rela_section(relasec)) @@ -245,7 +245,7 @@ void upatch_create_intermediate_sections(struct upatch_elf *uelf, struct running list_for_each_entry(rela, &relasec->relas, list) { nr++; - if (need_dynrela(uelf, relf, relasec, rela)){ + if (need_dynrela(relf, relasec, rela)){ rela->need_dynrela = 1; } } @@ -401,7 +401,7 @@ static void rebuild_rela_section_data(struct section *sec) struct rela *rela; GElf_Rela *relas; size_t size; - int nr = 0, index = 0; + unsigned int nr = 0, index = 0; list_for_each_entry(rela, &sec->relas, list) nr++; @@ -438,13 +438,13 @@ void upatch_rebuild_relocations(struct upatch_elf *uelf) list_for_each_entry(relasec, &uelf->sections, list) { if (!is_rela_section(relasec)) continue; - relasec->sh.sh_link = symtab->index; - relasec->sh.sh_info = relasec->base->index; + relasec->sh.sh_link = (Elf64_Word)symtab->index; + relasec->sh.sh_info = (Elf64_Word)relasec->base->index; rebuild_rela_section_data(relasec); } } -void upatch_check_relocations(struct upatch_elf *uelf) +void upatch_check_relocations(void) { log_debug("upatch_check_relocations does not work now.\n"); return; @@ -558,7 +558,7 @@ void upatch_create_symtab(struct upatch_elf *uelf) struct symbol *sym; size_t size; char *buf; - int nr = 0, nr_local = 0; + unsigned int nr = 0, nr_local = 0; unsigned long offset = 0; symtab = find_section_by_name(&uelf->sections, ".symtab"); @@ -591,7 +591,7 @@ void upatch_create_symtab(struct upatch_elf *uelf) if (!strtab) ERROR("missing .strtab section in create symtab."); - symtab->sh.sh_link = strtab->index; + symtab->sh.sh_link = (Elf64_Word)strtab->index; symtab->sh.sh_info = nr_local; } diff --git a/upatch-diff/elf-create.h b/upatch-diff/elf-create.h index 1b4dc6c..ce7f263 100644 --- a/upatch-diff/elf-create.h +++ b/upatch-diff/elf-create.h @@ -33,7 +33,7 @@ void upatch_create_patches_sections(struct upatch_elf *, struct running_elf *); void upatch_create_intermediate_sections(struct upatch_elf *, struct running_elf *); -static inline void create_kpatch_arch_section(struct upatch_elf *uelf) {} +static inline void create_kpatch_arch_section(void) {} void upatch_build_strings_section_data(struct upatch_elf *); @@ -45,7 +45,7 @@ void upatch_reindex_elements(struct upatch_elf *); void upatch_rebuild_relocations(struct upatch_elf *); -void upatch_check_relocations(struct upatch_elf *); +void upatch_check_relocations(void); void upatch_create_shstrtab(struct upatch_elf *); diff --git a/upatch-diff/elf-debug.c b/upatch-diff/elf-debug.c index f9c5327..eaabfa1 100644 --- a/upatch-diff/elf-debug.c +++ b/upatch-diff/elf-debug.c @@ -60,7 +60,7 @@ void upatch_dump_kelf(struct upatch_elf *uelf) goto next; log_debug("rela section expansion\n"); list_for_each_entry(rela, &sec->relas, list) { - log_debug("sym %d, offset %d, type %d, %s %s %ld \n", + log_debug("sym %d, offset %ld, type %d, %s %s %ld \n", rela->sym->index, rela->offset, rela->type, rela->sym->name, (rela->addend < 0) ? "-" : "+", @@ -107,7 +107,7 @@ void upatch_rebuild_eh_frame(struct section *sec) struct rela *rela; unsigned char *data, *data_end; unsigned int hdr_length, hdr_id; - unsigned int current_offset; + unsigned long current_offset; unsigned int count = 0; /* sanity check */ @@ -136,13 +136,13 @@ void upatch_rebuild_eh_frame(struct section *sec) /* 8 is the offset of PC begin */ current_offset = 8; list_for_each_entry(rela, &sec->rela->relas, list) { - unsigned int offset = rela->offset; + unsigned long offset = rela->offset; bool found_rela = false; - log_debug("handle relocaton offset at 0x%x \n", offset); + log_debug("handle relocaton offset at 0x%lx \n", offset); while (data != data_end) { void *__src = data; - log_debug("current handle offset is 0x%x \n", current_offset); + log_debug("current handle offset is 0x%lx \n", current_offset); REQUIRE(skip_bytes(&data, data_end, 4), "no length to be read"); hdr_length = *(unsigned int *)(data - 4); @@ -166,13 +166,13 @@ void upatch_rebuild_eh_frame(struct section *sec) /* update rela offset to point to new offset, and also hdr_id */ if (found_rela) { /* 4 is the offset of hdr_id and 8 is the offset of PC begin */ - *(unsigned int *)(eh_frame + frame_size + 4) = frame_size + 4; + *(unsigned long *)(eh_frame + frame_size + 4) = frame_size + 4; rela->offset = frame_size + 8; } frame_size += (hdr_length + 4); } else { - log_debug("remove FDE at 0x%x \n", current_offset); + log_debug("remove FDE at 0x%lx \n", current_offset); } /* hdr_length(value) + hdr_length(body) */ diff --git a/upatch-diff/elf-insn.c b/upatch-diff/elf-insn.c index 41252fe..11380d0 100644 --- a/upatch-diff/elf-insn.c +++ b/upatch-diff/elf-insn.c @@ -50,7 +50,7 @@ void rela_insn(const struct section *sec, const struct rela *rela, struct insn * return; } - ERROR("can't find instruction for rela at %s+0x%x", + ERROR("can't find instruction for rela at %s+0x%lx", sec->name, rela->offset); } @@ -75,7 +75,7 @@ long rela_target_offset(struct upatch_elf *uelf, struct section *relasec, struct rela_insn(sec, rela, &insn); add_off = (long)insn.next_byte - (long)sec->data->d_buf - - rela->offset; + (long)rela->offset; } else { ERROR("unable to handle rela type %d \n", rela->type); } diff --git a/upatch-diff/insn/asm/inat.h b/upatch-diff/insn/asm/inat.h index 95811f3..f446ad8 100644 --- a/upatch-diff/insn/asm/inat.h +++ b/upatch-diff/insn/asm/inat.h @@ -171,9 +171,9 @@ static inline int inat_group_id(insn_attr_t attr) return (attr & INAT_GRP_MASK) >> INAT_GRP_OFFS; } -static inline int inat_group_common_attribute(insn_attr_t attr) +static inline insn_attr_t inat_group_common_attribute(insn_attr_t attr) { - return attr & ~INAT_GRP_MASK; + return attr & ~(insn_attr_t)INAT_GRP_MASK; } static inline int inat_has_immediate(insn_attr_t attr) diff --git a/upatch-diff/insn/asm/insn.h b/upatch-diff/insn/asm/insn.h index 041b351..fc4ae40 100644 --- a/upatch-diff/insn/asm/insn.h +++ b/upatch-diff/insn/asm/insn.h @@ -66,7 +66,7 @@ struct insn { unsigned char x86_64; const insn_byte_t *kaddr; /* kernel address of insn to analyze */ - const insn_byte_t *next_byte; + insn_byte_t *next_byte; }; #define MAX_INSN_SIZE 16 @@ -97,7 +97,7 @@ struct insn { #define X86_VEX_P(vex) ((vex) & 0x03) /* VEX3 Byte2, VEX2 Byte1 */ #define X86_VEX_M_MAX 0x1f /* VEX3.M Maximum value */ -extern void insn_init(struct insn *insn, const void *kaddr, int x86_64); +extern void insn_init(struct insn *insn, void *kaddr, int x86_64); extern void insn_get_prefixes(struct insn *insn); extern void insn_get_opcode(struct insn *insn); extern void insn_get_modrm(struct insn *insn); @@ -116,7 +116,7 @@ static inline void insn_get_attribute(struct insn *insn) extern int insn_rip_relative(struct insn *insn); /* Init insn for kernel text */ -static inline void kernel_insn_init(struct insn *insn, const void *kaddr) +static inline void kernel_insn_init(struct insn *insn, void *kaddr) { #ifdef CONFIG_X86_64 insn_init(insn, kaddr, 1); diff --git a/upatch-diff/insn/insn.c b/upatch-diff/insn/insn.c index 6dfca32..d9a356b 100644 --- a/upatch-diff/insn/insn.c +++ b/upatch-diff/insn/insn.c @@ -49,7 +49,7 @@ * @kaddr: address (in kernel memory) of instruction (or copy thereof) * @x86_64: !0 for 64-bit kernel or 64-bit app */ -void insn_init(struct insn *insn, const void *kaddr, int x86_64) +void insn_init(struct insn *insn, void *kaddr, int x86_64) { memset(insn, 0, sizeof(*insn)); insn->kaddr = kaddr; @@ -250,7 +250,7 @@ void insn_get_modrm(struct insn *insn) modrm->value = mod; modrm->nbytes = 1; if (inat_is_group(insn->attr)) { - pfx_id = insn_last_prefix_id(insn); + pfx_id = (insn_byte_t)insn_last_prefix_id(insn); insn->attr = inat_get_group_attribute(mod, pfx_id, insn->attr); if (insn_is_avx(insn) && !inat_accept_vex(insn->attr)) diff --git a/upatch-diff/list.h b/upatch-diff/list.h index 6205a72..b3b28e1 100644 --- a/upatch-diff/list.h +++ b/upatch-diff/list.h @@ -49,7 +49,7 @@ * */ #define container_of(ptr, type, member) ({ \ - const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) /** diff --git a/upatch-diff/running-elf.c b/upatch-diff/running-elf.c index 676880f..25b72b7 100644 --- a/upatch-diff/running-elf.c +++ b/upatch-diff/running-elf.c @@ -38,7 +38,7 @@ /* TODO: need to judge whether running_elf is a Position-Independent Executable file * https://github.com/bminor/binutils-gdb/blob/master/binutils/readelf.c */ -static bool is_pie(struct Elf *elf) +static bool is_pie(void) { return true; } @@ -50,7 +50,7 @@ static bool is_exec(struct Elf *elf) if (!gelf_getehdr(elf, &ehdr)) ERROR("gelf_getehdr running_file failed for %s.", elf_errmsg(0)); - return ehdr.e_type == ET_EXEC || (ehdr.e_type == ET_DYN && is_pie(elf)); + return ehdr.e_type == ET_EXEC || (ehdr.e_type == ET_DYN && is_pie()); } void relf_init(char *elf_name, struct running_elf *relf) @@ -59,7 +59,6 @@ void relf_init(char *elf_name, struct running_elf *relf) Elf_Scn *scn = NULL; Elf_Data *data; GElf_Sym sym; - unsigned int i; relf->fd = open(elf_name, O_RDONLY); if (relf->fd == -1) @@ -83,12 +82,12 @@ void relf_init(char *elf_name, struct running_elf *relf) if (!data) ERROR("elf_getdata with error %s", elf_errmsg(0)); - relf->obj_nr = shdr.sh_size / shdr.sh_entsize; - relf->obj_syms = calloc(relf->obj_nr, sizeof(struct debug_symbol)); + relf->obj_nr = (int)(shdr.sh_size / shdr.sh_entsize); + relf->obj_syms = calloc((size_t)relf->obj_nr, sizeof(struct debug_symbol)); if (!relf->obj_syms) ERROR("calloc with errno = %d", errno); - for (i = 0; i < relf->obj_nr; i ++) { + for (int i = 0; i < relf->obj_nr; i ++) { if (!gelf_getsym(data, i, &sym)) ERROR("gelf_getsym with error %s", elf_errmsg(0)); relf->obj_syms[i].name = elf_strptr(relf->elf, shdr.sh_link, sym.st_name); diff --git a/upatch-diff/upatch-elf.c b/upatch-diff/upatch-elf.c index fc4396a..ee38efc 100644 --- a/upatch-diff/upatch-elf.c +++ b/upatch-diff/upatch-elf.c @@ -106,7 +106,7 @@ static void create_symbol_list(struct upatch_elf *uelf) INIT_LIST_HEAD(&sym->children); sym->index = index; - if (!gelf_getsym(symtab->data, index, &sym->sym)) + if (!gelf_getsym(symtab->data, (int)index, &sym->sym)) ERROR("gelf_getsym with error %s", elf_errmsg(0)); index ++; @@ -122,7 +122,7 @@ static void create_symbol_list(struct upatch_elf *uelf) /* releated section located in extended header */ if (shndx == SHN_XINDEX && !gelf_getsymshndx(symtab->data, uelf->symtab_shndx, - sym->index, &sym->sym, &shndx)) + (int)sym->index, &sym->sym, &shndx)) ERROR("gelf_getsymshndx with error %s", elf_errmsg(0)); if ((sym->sym.st_shndx > SHN_UNDEF && sym->sym.st_shndx < SHN_LORESERVE) || @@ -206,7 +206,7 @@ static void create_rela_list(struct upatch_elf *uelf, struct section *relasec) if (skip) continue; - log_debug("offset %d, type %d, %s %s %ld", rela->offset, + log_debug("offset %ld, type %d, %s %s %ld", rela->offset, rela->type, rela->sym->name, (rela->addend < 0) ? "-" : "+", labs(rela->addend)); if (rela->string) // rela->string is not utf8 diff --git a/upatch-diff/upatch-elf.h b/upatch-diff/upatch-elf.h index b2d038b..3cbb59b 100644 --- a/upatch-diff/upatch-elf.h +++ b/upatch-diff/upatch-elf.h @@ -87,7 +87,7 @@ struct rela { GElf_Rela rela; struct symbol *sym; unsigned int type; - unsigned int offset; + unsigned long offset; long addend; char *string; bool need_dynrela; diff --git a/upatch-hijacker/hijacker/gnu-as-hijacker.c b/upatch-hijacker/hijacker/gnu-as-hijacker.c index 789ddc2..860a84f 100644 --- a/upatch-hijacker/hijacker/gnu-as-hijacker.c +++ b/upatch-hijacker/hijacker/gnu-as-hijacker.c @@ -28,11 +28,9 @@ #define DEFSYM_MAX 64 -static const char *DEFSYM_FLAG = "--defsym"; -static const char *DEFSYM_VALUE = ".upatch_0x%x="; +static char *DEFSYM_FLAG = "--defsym"; static const int APPEND_ARG_LEN = 2; -static const char *OUTPUT_PATH = "%s/0x%x.o"; static const char *NULL_DEV_PATH = "/dev/null"; static char g_defsym[DEFSYM_MAX] = { 0 }; @@ -81,7 +79,7 @@ int main(int argc, char *argv[], char *envp[]) } int new_argc = argc + APPEND_ARG_LEN + 1; // include terminator NULL - const char **new_argv = calloc(1, new_argc * sizeof(char *)); + char **new_argv = calloc(1, (unsigned long)new_argc * sizeof(char *)); if (new_argv == NULL) { return execve(filename, argv, envp); } @@ -100,13 +98,13 @@ int main(int argc, char *argv[], char *envp[]) char *defsym_value = (char *)g_defsym; char *new_output_file = (char *)g_new_output_file; - snprintf(defsym_value, DEFSYM_MAX, DEFSYM_VALUE, tid); + snprintf(defsym_value, DEFSYM_MAX, ".upatch_0x%x=", tid); new_argv[new_argc++] = DEFSYM_FLAG; new_argv[new_argc++] = defsym_value; new_argv[new_argc] = NULL; // Handle output file - snprintf(new_output_file, PATH_MAX, OUTPUT_PATH, output_dir, tid); + snprintf(new_output_file, PATH_MAX, "%s/0x%x.o", output_dir, tid); new_argv[output_index] = new_output_file; if (access(output_file, F_OK) == 0) { diff --git a/upatch-hijacker/hijacker/gnu-compiler-hijacker.c b/upatch-hijacker/hijacker/gnu-compiler-hijacker.c index d0410a2..d868467 100644 --- a/upatch-hijacker/hijacker/gnu-compiler-hijacker.c +++ b/upatch-hijacker/hijacker/gnu-compiler-hijacker.c @@ -16,7 +16,7 @@ #include "hijacker.h" -static const char* APPEND_ARGS[] = { +static char* APPEND_ARGS[] = { "-gdwarf", /* obatain debug information */ "-ffunction-sections", "-fdata-sections", @@ -52,7 +52,7 @@ int main(int argc, char *argv[], char *envp[]) } int new_argc = argc + APPEND_ARG_LEN + 1; // include terminator NULL - const char **new_argv = calloc(1, new_argc * sizeof(char *)); + char **new_argv = calloc(1, (unsigned long)new_argc * sizeof(char *)); if (new_argv == NULL) { return execve(filename, argv, envp); } diff --git a/upatch-hijacker/hijacker/hijacker.h b/upatch-hijacker/hijacker/hijacker.h index 2a41ab2..cc820ee 100644 --- a/upatch-hijacker/hijacker/hijacker.h +++ b/upatch-hijacker/hijacker/hijacker.h @@ -28,7 +28,7 @@ static const char *OUTPUT_FLAG_NAME = "-o"; static char g_filename[PATH_MAX] = { 0 }; -static inline char* get_current_exec() +static inline char* get_current_exec(void) { ssize_t path_len = readlink(EXEC_SELF_PATH, (char *)g_filename, PATH_MAX); if (path_len == -1) { @@ -39,7 +39,7 @@ static inline char* get_current_exec() return (char *)g_filename; } -static inline const char* get_hijacker_env() +static inline const char* get_hijacker_env(void) { return getenv(UPATCH_ENV_NAME); } diff --git a/upatch-manage/arch/aarch64/insn.c b/upatch-manage/arch/aarch64/insn.c index 8f78ae1..bb61f77 100644 --- a/upatch-manage/arch/aarch64/insn.c +++ b/upatch-manage/arch/aarch64/insn.c @@ -97,17 +97,17 @@ u32 aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type, u32 insn, /* Update the immediate field. */ insn &= ~(mask << shift); - insn |= (imm & mask) << shift; + insn |= (u32)(imm & mask) << shift; return insn; } -u64 extract_insn_imm(s64 sval, int len, int lsb) +s64 extract_insn_imm(s64 sval, int len, int lsb) { - u64 imm, imm_mask; + s64 imm, imm_mask; imm = sval >> lsb; - imm_mask = (BIT(lsb + len) - 1) >> lsb; + imm_mask = (s64)((BIT(lsb + len) - 1) >> lsb); imm = imm & imm_mask; log_debug("upatch: extract imm, X=0x%lx, X[%d:%d]=0x%lx\n", sval, @@ -115,7 +115,7 @@ u64 extract_insn_imm(s64 sval, int len, int lsb) return imm; } -u32 insert_insn_imm(enum aarch64_insn_imm_type imm_type, void *place, u64 imm) +s32 insert_insn_imm(enum aarch64_insn_imm_type imm_type, void *place, u64 imm) { u32 insn, new_insn; @@ -126,5 +126,5 @@ u32 insert_insn_imm(enum aarch64_insn_imm_type imm_type, void *place, u64 imm) "upatch: insert imm, P=0x%lx, insn=0x%x, imm_type=%d, imm=0x%lx, " "new_insn=0x%x\n", (u64)place, insn, imm_type, imm, new_insn); - return new_insn; + return (s32)new_insn; } diff --git a/upatch-manage/arch/aarch64/insn.h b/upatch-manage/arch/aarch64/insn.h index 2b97e8d..76a9689 100644 --- a/upatch-manage/arch/aarch64/insn.h +++ b/upatch-manage/arch/aarch64/insn.h @@ -64,8 +64,8 @@ enum aarch64_insn_imm_type { u32 aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type, u32 insn, u64 imm); -u64 extract_insn_imm(s64, int, int); +s64 extract_insn_imm(s64, int, int); -u32 insert_insn_imm(enum aarch64_insn_imm_type, void *, u64); +s32 insert_insn_imm(enum aarch64_insn_imm_type, void *, u64); #endif /* _ARCH_AARCH64_INSN_H */ diff --git a/upatch-manage/arch/aarch64/ptrace.c b/upatch-manage/arch/aarch64/ptrace.c index c51a236..fdc7695 100644 --- a/upatch-manage/arch/aarch64/ptrace.c +++ b/upatch-manage/arch/aarch64/ptrace.c @@ -21,13 +21,14 @@ #include #include #include +#include #include "insn.h" #include "upatch-ptrace.h" #define ORIGIN_INSN_LEN 16 -int upatch_arch_syscall_remote(struct upatch_ptrace_ctx *pctx, int nr, +long upatch_arch_syscall_remote(struct upatch_ptrace_ctx *pctx, int nr, unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6, @@ -38,10 +39,10 @@ int upatch_arch_syscall_remote(struct upatch_ptrace_ctx *pctx, int nr, 0x01, 0x00, 0x00, 0xd4, // 0xd4000001 svc #0 = syscall 0xa0, 0x00, 0x20, 0xd4, // 0xd42000a0 brk #5 = int3 }; - int ret; + long ret; log_debug("Executing syscall %d (pid %d)...\n", nr, pctx->pid); - regs.regs[8] = (unsigned long)nr; + regs.regs[8] = (unsigned long long)nr; regs.regs[0] = arg1; regs.regs[1] = arg2; regs.regs[2] = arg3; @@ -56,39 +57,45 @@ int upatch_arch_syscall_remote(struct upatch_ptrace_ctx *pctx, int nr, return ret; } -int upatch_arch_execute_remote_func(struct upatch_ptrace_ctx *pctx, +long upatch_arch_execute_remote_func(struct upatch_ptrace_ctx *pctx, const unsigned char *code, size_t codelen, struct user_regs_struct *pregs, int (*func)(struct upatch_ptrace_ctx *pctx, const void *data), const void *data) { + long ret; struct user_regs_struct orig_regs, regs; struct iovec orig_regs_iov, regs_iov; + struct upatch_process *proc = pctx->proc; + unsigned long libc_base = proc->libc_base; + unsigned char *orig_code = (unsigned char *)malloc(sizeof(*orig_code) * codelen); + + if (orig_code == NULL) { + log_error("Malloc orig_code failed\n"); + return -1; + } orig_regs_iov.iov_base = &orig_regs; orig_regs_iov.iov_len = sizeof(orig_regs); regs_iov.iov_base = ®s; regs_iov.iov_len = sizeof(regs); - unsigned char orig_code[codelen]; - int ret; - struct upatch_process *proc = pctx->proc; - unsigned long libc_base = proc->libc_base; - ret = ptrace(PTRACE_GETREGSET, pctx->pid, (void *)NT_PRSTATUS, (void *)&orig_regs_iov); if (ret < 0) { log_error("can't get regs - %d\n", pctx->pid); + free(orig_code); return -1; } ret = upatch_process_mem_read(proc, libc_base, (unsigned long *)orig_code, codelen); if (ret < 0) { log_error("can't peek original code - %d\n", pctx->pid); + free(orig_code); return -1; } - ret = upatch_process_mem_write(proc, (unsigned long *)code, libc_base, + ret = upatch_process_mem_write(proc, code, libc_base, codelen); if (ret < 0) { log_error("can't poke syscall code - %d\n", pctx->pid); @@ -132,6 +139,7 @@ int upatch_arch_execute_remote_func(struct upatch_ptrace_ctx *pctx, poke_back: upatch_process_mem_write(proc, (unsigned long *)orig_code, libc_base, codelen); + free(orig_code); return ret; } @@ -162,41 +170,26 @@ void copy_regs(struct user_regs_struct *dst, struct user_regs_struct *src) #undef COPY_REG } -size_t get_origin_insn_len() +size_t get_origin_insn_len(void) { return ORIGIN_INSN_LEN; } #define UPATCH_INSN_LEN 8 #define UPATCH_ADDR_LEN 8 -size_t get_upatch_insn_len() +size_t get_upatch_insn_len(void) { return UPATCH_INSN_LEN; } -size_t get_upatch_addr_len() +size_t get_upatch_addr_len(void) { return UPATCH_ADDR_LEN; } // for long jumper -unsigned long get_new_insn(struct object_file *obj, unsigned long old_addr, - unsigned long new_addr) +unsigned long get_new_insn(void) { unsigned int insn0 = 0x58000051; // ldr x17, #8 unsigned int insn4 = 0xd61f0220; // br x17 return (unsigned long)(insn0 | ((unsigned long)insn4 << 32)); -} - -#if 0 -unsigned long get_new_insn(struct object_file *obj, unsigned long old_addr, - unsigned long new_addr) -{ - unsigned char b_insn[] = { 0x00, 0x00, 0x00, 0x00 }; /* ins: b IMM */ - - *(unsigned int *)(b_insn) = (unsigned int)(new_addr - old_addr) / 4; - b_insn[3] &= 0x3; - b_insn[3] |= 0x14; - - return *(unsigned int *)b_insn; -} -#endif +} \ No newline at end of file diff --git a/upatch-manage/arch/aarch64/relocation.c b/upatch-manage/arch/aarch64/relocation.c index 0019388..3951135 100644 --- a/upatch-manage/arch/aarch64/relocation.c +++ b/upatch-manage/arch/aarch64/relocation.c @@ -39,15 +39,15 @@ static inline s64 calc_reloc(enum aarch64_reloc_op op, void *place, u64 val) switch (op) { case RELOC_OP_ABS: // S + A - sval = val; + sval = (s64)val; break; case RELOC_OP_PREL: // S + A - P - sval = val - (u64)place; + sval = (s64)(val - (u64)place); break; case RELOC_OP_PAGE: // Page(S + A) - Page(P) - sval = (val & ~0xfff) - ((u64)place & ~0xfff); + sval = (s64)((val & ~(u64)0xfff) - ((u64)place & ~(u64)0xfff)); break; default: log_error("upatch: unknown relocation operation %d\n", op); @@ -92,7 +92,7 @@ int apply_relocate_add(struct upatch_elf *uelf, unsigned int symindex, sym_name = uelf->strtab + sym->st_name; /* val corresponds to (S + A) */ - val = (s64)(sym->st_value + rel[i].r_addend); + val = (unsigned long)sym->st_value + (unsigned long)rel[i].r_addend; log_debug( "upatch: reloc symbol, name=%s, k_addr=0x%lx, u_addr=0x%lx, " "r_offset=0x%lx, st_value=0x%lx, r_addend=0x%lx \n", @@ -113,13 +113,13 @@ int apply_relocate_add(struct upatch_elf *uelf, unsigned int symindex, result = calc_reloc(RELOC_OP_ABS, uloc, val); if (result < -(s64)BIT(31) || result >= (s64)BIT(32)) goto overflow; - *(s32 *)loc = result; + *(s32 *)loc = (s32)result; break; case R_AARCH64_ABS16: result = calc_reloc(RELOC_OP_ABS, uloc, val); if (result < -(s64)BIT(15) || result >= (s64)BIT(16)) goto overflow; - *(s16 *)loc = result; + *(s16 *)loc = (s16)result; break; case R_AARCH64_PREL64: result = calc_reloc(RELOC_OP_PREL, uloc, val); @@ -129,13 +129,13 @@ int apply_relocate_add(struct upatch_elf *uelf, unsigned int symindex, result = calc_reloc(RELOC_OP_PREL, uloc, val); if (result < -(s64)BIT(31) || result >= (s64)BIT(32)) goto overflow; - *(s32 *)loc = result; + *(s32 *)loc = (s32)result; break; case R_AARCH64_PREL16: result = calc_reloc(RELOC_OP_PREL, uloc, val); if (result < -(s64)BIT(15) || result >= (s64)BIT(16)) goto overflow; - *(s16 *)loc = result; + *(s16 *)loc = (s16)result; break; /* Immediate instruction relocations. */ case R_AARCH64_LD_PREL_LO19: @@ -144,8 +144,8 @@ int apply_relocate_add(struct upatch_elf *uelf, unsigned int symindex, goto overflow; result = extract_insn_imm(result, 19, 2); result = insert_insn_imm(AARCH64_INSN_IMM_19, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_ADR_PREL_LO21: result = calc_reloc(RELOC_OP_PREL, uloc, val); @@ -153,8 +153,8 @@ int apply_relocate_add(struct upatch_elf *uelf, unsigned int symindex, goto overflow; result = extract_insn_imm(result, 21, 0); result = insert_insn_imm(AARCH64_INSN_IMM_ADR, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_ADR_PREL_PG_HI21: result = calc_reloc(RELOC_OP_PAGE, uloc, val); @@ -162,51 +162,51 @@ int apply_relocate_add(struct upatch_elf *uelf, unsigned int symindex, goto overflow; result = extract_insn_imm(result, 21, 12); result = insert_insn_imm(AARCH64_INSN_IMM_ADR, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_ADR_PREL_PG_HI21_NC: result = calc_reloc(RELOC_OP_PAGE, uloc, val); result = extract_insn_imm(result, 21, 12); result = insert_insn_imm(AARCH64_INSN_IMM_ADR, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_ADD_ABS_LO12_NC: case R_AARCH64_LDST8_ABS_LO12_NC: result = calc_reloc(RELOC_OP_ABS, uloc, val); result = extract_insn_imm(result, 12, 0); result = insert_insn_imm(AARCH64_INSN_IMM_12, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_LDST16_ABS_LO12_NC: result = calc_reloc(RELOC_OP_ABS, uloc, val); result = extract_insn_imm(result, 11, 1); result = insert_insn_imm(AARCH64_INSN_IMM_12, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_LDST32_ABS_LO12_NC: result = calc_reloc(RELOC_OP_ABS, uloc, val); result = extract_insn_imm(result, 10, 2); result = insert_insn_imm(AARCH64_INSN_IMM_12, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_LDST64_ABS_LO12_NC: result = calc_reloc(RELOC_OP_ABS, uloc, val); result = extract_insn_imm(result, 9, 3); result = insert_insn_imm(AARCH64_INSN_IMM_12, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_LDST128_ABS_LO12_NC: result = calc_reloc(RELOC_OP_ABS, uloc, val); result = extract_insn_imm(result, 8, 4); result = insert_insn_imm(AARCH64_INSN_IMM_12, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_TSTBR14: result = calc_reloc(RELOC_OP_PREL, uloc, val); @@ -214,15 +214,15 @@ int apply_relocate_add(struct upatch_elf *uelf, unsigned int symindex, goto overflow; result = extract_insn_imm(result, 14, 2); result = insert_insn_imm(AARCH64_INSN_IMM_14, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_CONDBR19: result = calc_reloc(RELOC_OP_PREL, uloc, val); result = extract_insn_imm(result, 19, 2); result = insert_insn_imm(AARCH64_INSN_IMM_19, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_JUMP26: case R_AARCH64_CALL26: @@ -243,8 +243,8 @@ int apply_relocate_add(struct upatch_elf *uelf, unsigned int symindex, } result = extract_insn_imm(result, 26, 2); result = insert_insn_imm(AARCH64_INSN_IMM_26, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_ADR_GOT_PAGE: result = calc_reloc(RELOC_OP_PAGE, uloc, val); @@ -252,8 +252,8 @@ int apply_relocate_add(struct upatch_elf *uelf, unsigned int symindex, goto overflow; result = extract_insn_imm(result, 21, 12); result = insert_insn_imm(AARCH64_INSN_IMM_ADR, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_LD64_GOT_LO12_NC: result = calc_reloc(RELOC_OP_ABS, uloc, val); @@ -261,24 +261,24 @@ int apply_relocate_add(struct upatch_elf *uelf, unsigned int symindex, // sometimes, result & 7 != 0, it works fine. result = extract_insn_imm(result, 9, 3); result = insert_insn_imm(AARCH64_INSN_IMM_12, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_TLSLE_ADD_TPREL_HI12: - result = ALIGN(TCB_SIZE, uelf->relf->tls_align) + val; - if (result < 0 || result >= BIT(24)) + result = (long)(ALIGN(TCB_SIZE, uelf->relf->tls_align) + val); + if (result < 0 || result >= (s64)BIT(24)) goto overflow; result = extract_insn_imm(result, 12, 12); result = insert_insn_imm(AARCH64_INSN_IMM_12, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: - result = ALIGN(TCB_SIZE, uelf->relf->tls_align) + val; + result = (long)(ALIGN(TCB_SIZE, uelf->relf->tls_align) + val); result = extract_insn_imm(result, 12, 0); result = insert_insn_imm(AARCH64_INSN_IMM_12, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_TLSDESC_ADR_PAGE21: result = calc_reloc(RELOC_OP_PAGE, uloc, val); @@ -286,23 +286,23 @@ int apply_relocate_add(struct upatch_elf *uelf, unsigned int symindex, goto overflow; result = extract_insn_imm(result, 21, 12); result = insert_insn_imm(AARCH64_INSN_IMM_ADR, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_TLSDESC_LD64_LO12: result = calc_reloc(RELOC_OP_ABS, uloc, val); // don't check result & 7 == 0. result = extract_insn_imm(result, 9, 3); result = insert_insn_imm(AARCH64_INSN_IMM_12, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_TLSDESC_ADD_LO12: result = calc_reloc(RELOC_OP_ABS, uloc, val); result = extract_insn_imm(result, 12, 0); result = insert_insn_imm(AARCH64_INSN_IMM_12, loc, - result); - *(__le32 *)loc = cpu_to_le32(result); + (unsigned long)result); + *(__le32 *)loc = cpu_to_le32((__le32)result); break; case R_AARCH64_TLSDESC_CALL: // this is a blr instruction, don't need to modify diff --git a/upatch-manage/arch/x86_64/ptrace.c b/upatch-manage/arch/x86_64/ptrace.c index d824d92..3d6dd72 100644 --- a/upatch-manage/arch/x86_64/ptrace.c +++ b/upatch-manage/arch/x86_64/ptrace.c @@ -19,6 +19,7 @@ */ #include +#include #include #include @@ -26,7 +27,7 @@ #include "upatch-ptrace.h" -int upatch_arch_syscall_remote(struct upatch_ptrace_ctx *pctx, int nr, +long upatch_arch_syscall_remote(struct upatch_ptrace_ctx *pctx, int nr, unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6, @@ -38,11 +39,11 @@ int upatch_arch_syscall_remote(struct upatch_ptrace_ctx *pctx, int nr, 0x0f, 0x05, /* syscall */ 0xcc, /* int3 */ }; - int ret; + long ret; memset(®s, 0, sizeof(struct user_regs_struct)); log_debug("Executing syscall %d (pid %d)...\n", nr, pctx->pid); - regs.rax = (unsigned long)nr; + regs.rax = (unsigned long long)nr; regs.rdi = arg1; regs.rsi = arg2; regs.rdx = arg3; @@ -57,7 +58,7 @@ int upatch_arch_syscall_remote(struct upatch_ptrace_ctx *pctx, int nr, return ret; } -int upatch_arch_execute_remote_func(struct upatch_ptrace_ctx *pctx, +long upatch_arch_execute_remote_func(struct upatch_ptrace_ctx *pctx, const unsigned char *code, size_t codelen, struct user_regs_struct *pregs, int (*func)(struct upatch_ptrace_ctx *pctx, @@ -65,23 +66,30 @@ int upatch_arch_execute_remote_func(struct upatch_ptrace_ctx *pctx, const void *data) { struct user_regs_struct orig_regs, regs; - unsigned char orig_code[codelen]; - int ret; + long ret; struct upatch_process *proc = pctx->proc; unsigned long libc_base = proc->libc_base; + unsigned char *orig_code = (unsigned char *)malloc(sizeof(*orig_code) * codelen); + + if (orig_code == NULL) { + log_error("Malloc orig_code failed\n"); + return -1; + } ret = ptrace(PTRACE_GETREGS, pctx->pid, NULL, &orig_regs); if (ret < 0) { log_error("can't get regs - %d\n", pctx->pid); + free(orig_code); return -1; } ret = upatch_process_mem_read(proc, libc_base, (unsigned long *)orig_code, codelen); if (ret < 0) { log_error("can't peek original code - %d\n", pctx->pid); + free(orig_code); return -1; } - ret = upatch_process_mem_write(proc, (unsigned long *)code, libc_base, + ret = upatch_process_mem_write(proc, code, libc_base, codelen); if (ret < 0) { log_error("can't poke syscall code - %d\n", pctx->pid); @@ -122,6 +130,7 @@ int upatch_arch_execute_remote_func(struct upatch_ptrace_ctx *pctx, poke_back: upatch_process_mem_write(proc, (unsigned long *)orig_code, libc_base, codelen); + free(orig_code); return ret; } @@ -165,22 +174,8 @@ size_t get_upatch_addr_len() } -unsigned long get_new_insn(struct object_file *obj, unsigned long old_addr, - unsigned long new_addr) +unsigned long get_new_insn(void) { char jmp_insn[] = { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00}; return *(unsigned long *)jmp_insn; -} - -#if 0 -unsigned long get_new_insn(struct object_file *obj, unsigned long old_addr, - unsigned long new_addr) -{ - char jmp_insn[] = { 0xe9, 0x00, 0x00, 0x00, 0x00 }; /* jmp IMM */ - - *(unsigned int *)(jmp_insn + 1) = - (unsigned int)(new_addr - old_addr - 5); - - return *(unsigned long *)jmp_insn; -} -#endif +} \ No newline at end of file diff --git a/upatch-manage/arch/x86_64/relocation.c b/upatch-manage/arch/x86_64/relocation.c index bb9cf32..657f014 100644 --- a/upatch-manage/arch/x86_64/relocation.c +++ b/upatch-manage/arch/x86_64/relocation.c @@ -63,7 +63,7 @@ int apply_relocate_add(struct upatch_elf *uelf, unsigned int symindex, (int)GELF_R_TYPE(rel[i].r_info), sym->st_value, rel[i].r_addend, (u64)loc); - val = sym->st_value + rel[i].r_addend; + val = sym->st_value + (unsigned long)rel[i].r_addend; switch (GELF_R_TYPE(rel[i].r_info)) { case R_X86_64_NONE: break; @@ -95,7 +95,8 @@ int apply_relocate_add(struct upatch_elf *uelf, unsigned int symindex, if (sym->st_value == 0) goto overflow; /* G + GOT + A*/ - val = sym->st_value + rel[i].r_addend; + val = sym->st_value + (unsigned long)rel[i].r_addend; + /* fallthrough*/ case R_X86_64_PC32: case R_X86_64_PLT32: if (*(u32 *)loc != 0) diff --git a/upatch-manage/arch/x86_64/resolve.c b/upatch-manage/arch/x86_64/resolve.c index 5432b20..0f8623d 100644 --- a/upatch-manage/arch/x86_64/resolve.c +++ b/upatch-manage/arch/x86_64/resolve.c @@ -30,7 +30,7 @@ struct upatch_jmp_table_entry { unsigned long addr; }; -unsigned int get_jmp_table_entry() +unsigned int get_jmp_table_entry(void) { return sizeof(struct upatch_jmp_table_entry); } @@ -84,6 +84,7 @@ unsigned long insert_plt_table(struct upatch_elf *uelf, struct object_file *obj, unsigned long jmp_addr; unsigned long elf_addr = 0; + (void)r_type; if (upatch_process_mem_read(obj->proc, addr, &jmp_addr, sizeof(jmp_addr))) { log_error("copy address failed\n"); diff --git a/upatch-manage/upatch-common.h b/upatch-manage/upatch-common.h index ab4084a..bc694d6 100644 --- a/upatch-manage/upatch-common.h +++ b/upatch-manage/upatch-common.h @@ -33,7 +33,7 @@ list_add(&(_new)->list, (_list)); \ } -static inline int page_shift(int n) +static inline int page_shift(long n) { int res = -1; @@ -52,7 +52,7 @@ static inline int page_shift(int n) #endif #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define ALIGN(x, a) (((x) + (a)-1) & (~((a)-1))) -#define PAGE_ALIGN(x) ALIGN((x), PAGE_SIZE) +#define PAGE_ALIGN(x) ALIGN((x), (unsigned long)PAGE_SIZE) #define ROUND_DOWN(x, m) ((x) & ~((m)-1)) #define ROUND_UP(x, m) (((x) + (m)-1) & ~((m)-1)) diff --git a/upatch-manage/upatch-elf.c b/upatch-manage/upatch-elf.c index 78c7fd7..1038ef9 100644 --- a/upatch-manage/upatch-elf.c +++ b/upatch-manage/upatch-elf.c @@ -30,14 +30,14 @@ #include "upatch-elf.h" #include "upatch-ptrace.h" -static int read_from_offset(int fd, void **buf, int len, off_t offset) +static int read_from_offset(int fd, void **buf, unsigned long len, off_t offset) { *buf = malloc(len); if (*buf == NULL) { return -errno; } - int size = pread(fd, *buf, len, offset); + ssize_t size = pread(fd, *buf, len, offset); if (size == -1) { return -errno; } @@ -47,7 +47,7 @@ static int read_from_offset(int fd, void **buf, int len, off_t offset) static int open_elf(struct elf_info *einfo, const char *name) { - int ret = 0, fd = -1, i; + int ret = 0, fd = -1; char *sec_name; struct stat st; @@ -65,7 +65,7 @@ static int open_elf(struct elf_info *einfo, const char *name) goto out; } - ret = read_from_offset(fd, (void **)&einfo->patch_buff, st.st_size, 0); + ret = read_from_offset(fd, (void **)&einfo->patch_buff, (unsigned long)st.st_size, 0); if (ret != 0) { log_error("Failed to read file '%s'\n", name); goto out; @@ -73,7 +73,7 @@ static int open_elf(struct elf_info *einfo, const char *name) einfo->name = name; einfo->inode = st.st_ino; - einfo->patch_size = st.st_size; + einfo->patch_size = (unsigned long)st.st_size; einfo->hdr = (void *)einfo->patch_buff; einfo->shdrs = (void *)einfo->hdr + einfo->hdr->e_shoff; einfo->shstrtab = (void *)einfo->hdr + einfo->shdrs[einfo->hdr->e_shstrndx].sh_offset; @@ -85,7 +85,7 @@ static int open_elf(struct elf_info *einfo, const char *name) goto out; } - for (i = 0; i < einfo->hdr->e_shnum; ++i) { + for (unsigned int i = 0; i < einfo->hdr->e_shnum; ++i) { sec_name = einfo->shstrtab + einfo->shdrs[i].sh_name; if (streql(sec_name, BUILD_ID_NAME) && einfo->shdrs[i].sh_type == SHT_NOTE) { einfo->num_build_id = i; @@ -116,7 +116,7 @@ int upatch_init(struct upatch_elf *uelf, const char *name) return ret; } - for (int i = 1; i < uelf->info.hdr->e_shnum; ++i) { + for (unsigned int i = 1; i < uelf->info.hdr->e_shnum; ++i) { char *sec_name = uelf->info.shstrtab + uelf->info.shdrs[i].sh_name; if (uelf->info.shdrs[i].sh_type == SHT_SYMTAB) { uelf->num_syms = uelf->info.shdrs[i].sh_size / sizeof(GElf_Sym); @@ -165,7 +165,7 @@ int binary_init(struct running_elf *relf, const char *name) return ret; } - for (int i = 1; i < relf->info.hdr->e_shnum; i++) { + for (unsigned int i = 1; i < relf->info.hdr->e_shnum; i++) { char *sec_name = relf->info.shstrtab + relf->info.shdrs[i].sh_name; if (relf->info.shdrs[i].sh_type == SHT_SYMTAB) { log_debug("Found section '%s', idx=%d\n", SYMTAB_NAME, i); diff --git a/upatch-manage/upatch-elf.h b/upatch-manage/upatch-elf.h index 481fec9..499287a 100644 --- a/upatch-manage/upatch-elf.h +++ b/upatch-manage/upatch-elf.h @@ -56,7 +56,7 @@ struct upatch_info { unsigned long size; // upatch_info and upatch_info_func size unsigned long start; // upatch vma start unsigned long end; // upatch vma end - unsigned int changed_func_num; + unsigned long changed_func_num; // upatch_header_func }; @@ -65,15 +65,15 @@ struct upatch_layout { void *kbase; void *base; /* Total size. */ - unsigned int size; + unsigned long size; /* The size of the executable code. */ - unsigned int text_size; + unsigned long text_size; /* Size of RO section of the module (text+rodata) */ - unsigned int ro_size; + unsigned long ro_size; /* Size of RO after init section, not use it now */ - unsigned int ro_after_init_size; + unsigned long ro_after_init_size; /* The size of the info. */ - unsigned int info_size; + unsigned long info_size; }; struct upatch_patch_func { diff --git a/upatch-manage/upatch-manage.c b/upatch-manage/upatch-manage.c index 8a7ba60..965af35 100644 --- a/upatch-manage/upatch-manage.c +++ b/upatch-manage/upatch-manage.c @@ -54,14 +54,14 @@ struct arguments { }; static struct argp_option options[] = { - { "verbose", 'v', NULL, 0, "Show verbose output" }, - { "uuid", 'U', "uuid", 0, "the uuid of the upatch" }, - { "pid", 'p', "pid", 0, "the pid of the user-space process" }, - { "upatch", 'u', "upatch", 0, "the upatch file" }, - { "binary", 'b', "binary", 0, "the binary file" }, - { "cmd", 0, "patch", 0, "Apply a upatch file to a user-space process" }, + { "verbose", 'v', NULL, 0, "Show verbose output", 0 }, + { "uuid", 'U', "uuid", 0, "the uuid of the upatch", 0 }, + { "pid", 'p', "pid", 0, "the pid of the user-space process", 0 }, + { "upatch", 'u', "upatch", 0, "the upatch file", 0 }, + { "binary", 'b', "binary", 0, "the binary file", 0 }, + { "cmd", 0, "patch", 0, "Apply a upatch file to a user-space process", 0 }, { "cmd", 0, "unpatch", 0, - "Unapply a upatch file to a user-space process" }, + "Unapply a upatch file to a user-space process", 0 }, { NULL } }; @@ -135,7 +135,7 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) return 0; } -static struct argp argp = { options, parse_opt, args_doc, program_doc }; +static struct argp argp = { options, parse_opt, args_doc, program_doc, NULL, NULL, NULL }; int patch_upatch(const char *uuid, const char *binary_path, const char *upatch_path, int pid) { @@ -163,7 +163,7 @@ out: return ret; } -int unpatch_upatch(const char *uuid, const char *binary_path, const char *upatch_path, int pid) +int unpatch_upatch(const char *uuid, int pid) { int ret = 0; @@ -176,7 +176,7 @@ int unpatch_upatch(const char *uuid, const char *binary_path, const char *upatch return 0; } -int info_upatch(const char *binary_path, const char *upatch_path, int pid) +int info_upatch(int pid) { int ret = process_info(pid); if (ret != 0) { @@ -210,10 +210,10 @@ int main(int argc, char *argv[]) ret = patch_upatch(args.uuid, args.binary, args.upatch, args.pid); break; case UNPATCH: - ret = unpatch_upatch(args.uuid, args.binary, args.upatch, args.pid); + ret = unpatch_upatch(args.uuid, args.pid); break; case INFO: - ret = info_upatch(args.binary, args.upatch, args.pid); + ret = info_upatch(args.pid); break; default: ERROR("Unknown command"); diff --git a/upatch-manage/upatch-patch.c b/upatch-manage/upatch-patch.c index ab972ac..cbdbbe1 100644 --- a/upatch-manage/upatch-patch.c +++ b/upatch-manage/upatch-patch.c @@ -51,7 +51,7 @@ static GElf_Off calculate_load_address(struct running_elf *relf, bool check_code) { int i; - GElf_Off min_addr = -1; + GElf_Off min_addr = (unsigned long)-1; /* TODO: for ET_DYN, consider check PIE */ if (relf->info.hdr->e_type != ET_EXEC && @@ -78,7 +78,7 @@ out: static unsigned long calculate_mem_load(struct object_file *obj) { struct obj_vm_area *ovma; - unsigned long load_addr = -1; + unsigned long load_addr = (unsigned long)-1; list_for_each_entry(ovma, &obj->vma, list) { if (ovma->inmem.prot & PROT_EXEC) { @@ -120,29 +120,20 @@ static int rewrite_section_headers(struct upatch_elf *uelf) return 0; } -/* Additional bytes needed by arch in front of individual sections */ -unsigned int arch_mod_section_prepend(struct upatch_elf *uelf, - unsigned int section) +static unsigned long get_offset(unsigned long *size, + GElf_Shdr *sechdr) { - /* default implementation just returns zero */ - return 0; -} + unsigned long ret; -static long get_offset(struct upatch_elf *uelf, unsigned int *size, - GElf_Shdr *sechdr, unsigned int section) -{ - long ret; - - *size += arch_mod_section_prepend(uelf, section); - ret = ALIGN(*size, sechdr->sh_addralign ?: 1); - *size = ret + sechdr->sh_size; + ret = ALIGN(*size, (unsigned long)(sechdr->sh_addralign ?: 1)); + *size = (unsigned long)ret + sechdr->sh_size; return ret; } static void layout_upatch_info(struct upatch_elf *uelf) { GElf_Shdr *upatch_func = uelf->info.shdrs + uelf->index.upatch_funcs; - int num = upatch_func->sh_size / sizeof(struct upatch_patch_func); + unsigned long num = upatch_func->sh_size / sizeof(struct upatch_patch_func); uelf->core_layout.info_size = uelf->core_layout.size; uelf->core_layout.size += sizeof(struct upatch_info) + @@ -189,7 +180,7 @@ static void layout_sections(struct upatch_elf *uelf) continue; s->sh_entsize = - get_offset(uelf, &uelf->core_layout.size, s, i); + get_offset(&uelf->core_layout.size, s); log_debug("\tm = %d; %s: sh_entsize: 0x%lx\n", m, sname, s->sh_entsize); } @@ -214,13 +205,14 @@ static void layout_sections(struct upatch_elf *uelf) uelf->core_layout.size = PAGE_ALIGN(uelf->core_layout.size); break; + default: + break; } } } /* TODO: only included used symbol */ -static bool is_upatch_symbol(const GElf_Sym *src, const GElf_Shdr *sechdrs, - unsigned int shnum) +static bool is_upatch_symbol(void) { return true; } @@ -237,12 +229,11 @@ static void layout_symtab(struct upatch_elf *uelf) GElf_Shdr *strsect = uelf->info.shdrs + uelf->index.str; /* TODO: only support same arch as kernel now */ const GElf_Sym *src; - unsigned int i, nsrc, ndst, strtab_size = 0; + unsigned long i, nsrc, ndst, strtab_size = 0; /* Put symbol section at end of init part of module. */ symsect->sh_flags |= SHF_ALLOC; - symsect->sh_entsize = get_offset(uelf, &uelf->core_layout.size, symsect, - uelf->index.sym); + symsect->sh_entsize = get_offset(&uelf->core_layout.size, symsect); log_debug("\t%s\n", uelf->info.shstrtab + symsect->sh_name); src = (void *)uelf->info.hdr + symsect->sh_offset; @@ -250,8 +241,7 @@ static void layout_symtab(struct upatch_elf *uelf) /* Compute total space required for the symbols' strtab. */ for (ndst = i = 0; i < nsrc; i++) { - if (i == 0 || is_upatch_symbol(src + i, uelf->info.shdrs, - uelf->info.hdr->e_shnum)) { + if (i == 0 || is_upatch_symbol()) { strtab_size += strlen(&uelf->strtab[src[i].st_name]) + 1; ndst++; @@ -270,8 +260,7 @@ static void layout_symtab(struct upatch_elf *uelf) /* Put string table section at end of init part of module. */ strsect->sh_flags |= SHF_ALLOC; - strsect->sh_entsize = get_offset(uelf, &uelf->core_layout.size, strsect, - uelf->index.str); + strsect->sh_entsize = get_offset(&uelf->core_layout.size, strsect); uelf->core_layout.size = PAGE_ALIGN(uelf->core_layout.size); log_debug("\t%s\n", uelf->info.shstrtab + strsect->sh_name); } @@ -288,8 +277,8 @@ static void *upatch_alloc(struct object_file *obj, size_t sz) addr = upatch_mmap_remote(proc2pctx(obj->proc), addr, sz, PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, - 0); + MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, + (unsigned long)-1, 0); if (addr == 0) { return NULL; } @@ -308,7 +297,7 @@ static void *upatch_alloc(struct object_file *obj, size_t sz) } static void upatch_free(struct object_file *obj, void *base, - unsigned int size) + unsigned long size) { log_debug("Free patch memory %p\n", base); if (upatch_munmap_remote(proc2pctx(obj->proc), (unsigned long)base, size)) { @@ -378,7 +367,7 @@ static int post_memory(struct upatch_elf *uelf, struct object_file *obj) { int ret = 0; - log_debug("Post kbase %lx(%x) to base %lx\n", + log_debug("Post kbase %lx(%lx) to base %lx\n", (unsigned long)uelf->core_layout.kbase, uelf->core_layout.size, (unsigned long)uelf->core_layout.base); @@ -396,7 +385,7 @@ out: static int complete_info(struct upatch_elf *uelf, struct object_file *obj, const char *uuid) { - int ret = 0, i; + int ret = 0; struct upatch_info *uinfo = (void *)uelf->core_layout.kbase + uelf->core_layout.info_size; struct upatch_patch_func *upatch_funcs_addr = @@ -414,7 +403,7 @@ static int complete_info(struct upatch_elf *uelf, struct object_file *obj, const memcpy(uinfo->id, uuid, strlen(uuid)); log_normal("Changed insn:\n"); - for (i = 0; i < uinfo->changed_func_num; ++i) { + for (unsigned int i = 0; i < uinfo->changed_func_num; ++i) { struct upatch_info_func *upatch_func = (void *)uelf->core_layout.kbase + uelf->core_layout.info_size + @@ -433,8 +422,7 @@ static int complete_info(struct upatch_elf *uelf, struct object_file *obj, const goto out; } - upatch_func->new_insn = get_new_insn(obj, upatch_func->old_addr, - upatch_func->new_addr); + upatch_func->new_insn = get_new_insn(); log_normal("\t0x%lx(0x%lx -> 0x%lx)\n", upatch_func->old_addr, upatch_func->old_insn[0], upatch_func->new_insn); @@ -446,10 +434,10 @@ out: static int unapply_patch(struct object_file *obj, struct upatch_info_func *funcs, - unsigned int changed_func_num) + unsigned long changed_func_num) { log_normal("Changed insn:\n"); - for (int i = 0; i < changed_func_num; ++i) { + for (unsigned int i = 0; i < changed_func_num; ++i) { log_normal("\t0x%lx(0x%lx -> 0x%lx)\n", funcs[i].old_addr, funcs[i].new_insn, funcs[i].old_insn[0]); @@ -467,7 +455,8 @@ static int unapply_patch(struct object_file *obj, static int apply_patch(struct upatch_elf *uelf, struct object_file *obj) { - int ret = 0, i; + int ret = 0; + unsigned int i; struct upatch_info *uinfo = (void *)uelf->core_layout.kbase + uelf->core_layout.info_size; @@ -617,10 +606,10 @@ static int upatch_apply_patches(struct upatch_process *proc, layout_symtab(uelf); layout_upatch_info(uelf); - log_debug("calculate core layout = %x\n", uelf->core_layout.size); + log_debug("calculate core layout = %lx\n", uelf->core_layout.size); log_debug( - "Core layout: text_size = %x, ro_size = %x, ro_after_init_size = " - "%x, info = %x, size = %x\n", + "Core layout: text_size = %lx, ro_size = %lx, ro_after_init_size = " + "%lx, info = %lx, size = %lx\n", uelf->core_layout.text_size, uelf->core_layout.ro_size, uelf->core_layout.ro_after_init_size, uelf->core_layout.info_size, uelf->core_layout.size); @@ -699,7 +688,7 @@ static void upatch_time_tick(int pid) { return; } - unsigned long frozen_time = GET_MICROSECONDS(end_tv, start_tv); + long frozen_time = GET_MICROSECONDS(end_tv, start_tv); log_normal("Process %d frozen time is %ld microsecond(s)\n", pid, frozen_time); } @@ -751,7 +740,7 @@ int process_patch(int pid, struct upatch_elf *uelf, struct running_elf *relf, co * stored in the patch are valid for the original object. */ // 解析process的mem-maps,获得各个块的内存映射以及phdr - ret = upatch_process_map_object_files(&proc, NULL); + ret = upatch_process_map_object_files(&proc); if (ret < 0) { log_error("Failed to read process memory mapping\n"); goto out_free; @@ -871,7 +860,7 @@ int process_unpatch(int pid, const char *uuid) * stored in the patch are valid for the original object. */ // 解析process的mem-maps,获得各个块的内存映射以及phdr - ret = upatch_process_map_object_files(&proc, NULL); + ret = upatch_process_map_object_files(&proc); if (ret < 0) { log_error("Failed to read process memory mapping\n"); goto out_free; @@ -950,7 +939,7 @@ int process_info(int pid) goto out_free; } - ret = upatch_process_map_object_files(&proc, NULL); + ret = upatch_process_map_object_files(&proc); if (ret < 0) { log_error("Failed to read process memory mapping\n"); goto out_free; diff --git a/upatch-manage/upatch-process.c b/upatch-manage/upatch-process.c index c368165..3b8db3b 100644 --- a/upatch-manage/upatch-process.c +++ b/upatch-manage/upatch-process.c @@ -65,7 +65,7 @@ static int lock_process(int pid) return fd; } -static void unlock_process(int pid, int fdmaps) +static void unlock_process(int fdmaps) { int errsv = errno; close(fdmaps); @@ -141,7 +141,7 @@ int upatch_process_init(struct upatch_process *proc, int pid) return 0; out_unlock: - unlock_process(pid, fdmaps); + unlock_process(fdmaps); out_err: return -1; } @@ -192,7 +192,7 @@ static void upatch_process_memfree(struct upatch_process *proc) void upatch_process_destroy(struct upatch_process *proc) { - unlock_process(proc->pid, proc->fdmaps); + unlock_process(proc->fdmaps); upatch_process_memfree(proc); } @@ -345,12 +345,12 @@ static int object_add_vm_area(struct object_file *o, struct vm_area *vma, } static struct object_file * -process_new_object(struct upatch_process *proc, dev_t dev, int inode, +process_new_object(struct upatch_process *proc, dev_t dev, ino_t inode, const char *name, struct vm_area *vma, struct vm_hole *hole) { struct object_file *o; - log_debug("Creating object file '%s' for %lx:%d...", name, dev, inode); + log_debug("Creating object file '%s' for %lx:%lu...", name, dev, inode); o = malloc(sizeof(*o)); if (!o) { @@ -389,7 +389,7 @@ process_new_object(struct upatch_process *proc, dev_t dev, int inode, * Returns: 0 if everything is ok, -1 on error. */ static int process_add_object_vma(struct upatch_process *proc, dev_t dev, - int inode, char *name, struct vm_area *vma, + ino_t inode, char *name, struct vm_area *vma, struct vm_hole *hole) { int object_type; @@ -407,7 +407,7 @@ static int process_add_object_vma(struct upatch_process *proc, dev_t dev, */ list_for_each_entry_reverse(o, &proc->objs, list) { if ((dev && inode && o->dev == dev && - o->inode == inode) || + o->inode == (ino_t)inode) || (dev == 0 && !strcmp(o->name, name))) { return object_add_vm_area(o, vma, hole); } @@ -512,9 +512,9 @@ int upatch_process_parse_proc_maps(struct upatch_process *proc) vma.prot = perms2prot(perms); /* Hole must be at least 2 pages for guardians */ - if (start - hole_start > 2 * PAGE_SIZE) { - hole = process_add_vm_hole(proc, hole_start + PAGE_SIZE, - start - PAGE_SIZE); + if (start - hole_start > (unsigned long)(2 * PAGE_SIZE)) { + hole = process_add_vm_hole(proc, hole_start + (unsigned long)PAGE_SIZE, + start - (unsigned long)PAGE_SIZE); if (hole == NULL) { log_error("Failed to add vma hole"); goto error; @@ -557,8 +557,7 @@ error: return -1; } -int upatch_process_map_object_files(struct upatch_process *proc, - const char *patch_id) +int upatch_process_map_object_files(struct upatch_process *proc) { // we can get plt/got table from mem's elf_segments // Now we read them from the running file @@ -618,7 +617,7 @@ static int process_list_threads(struct upatch_process *proc, int **ppids, *ppids = pids; - return *npids; + return (int)*npids; dealloc: if (dir) { @@ -761,8 +760,10 @@ static inline unsigned long hole_size(struct vm_hole *hole) int vm_hole_split(struct vm_hole *hole, unsigned long alloc_start, unsigned long alloc_end) { - alloc_start = ROUND_DOWN(alloc_start, PAGE_SIZE) - PAGE_SIZE; - alloc_end = ROUND_UP(alloc_end, PAGE_SIZE) + PAGE_SIZE; + unsigned long page_size = (unsigned long)PAGE_SIZE; + + alloc_start = ROUND_DOWN(alloc_start, page_size) - page_size; + alloc_end = ROUND_UP(alloc_end, page_size) + page_size; if (alloc_start > hole->start) { struct vm_hole *left = NULL; @@ -856,7 +857,7 @@ unsigned long object_find_patch_region(struct object_file *obj, size_t memsize, return -1UL; } - region_start = (region_start >> PAGE_SHIFT) << PAGE_SHIFT; + region_start = (region_start >> (unsigned long)PAGE_SHIFT) << (unsigned long)PAGE_SHIFT; log_debug("Found patch region for '%s' at 0x%lx\n", obj->name, region_start); diff --git a/upatch-manage/upatch-process.h b/upatch-manage/upatch-process.h index 39909db..be44cb5 100644 --- a/upatch-manage/upatch-process.h +++ b/upatch-manage/upatch-process.h @@ -133,7 +133,7 @@ void upatch_process_print_short(struct upatch_process *); int upatch_process_mem_open(struct upatch_process *, int); -int upatch_process_map_object_files(struct upatch_process *, const char *); +int upatch_process_map_object_files(struct upatch_process *); int upatch_process_attach(struct upatch_process *); diff --git a/upatch-manage/upatch-ptrace.c b/upatch-manage/upatch-ptrace.c index 1309a6e..03296bc 100644 --- a/upatch-manage/upatch-ptrace.c +++ b/upatch-manage/upatch-ptrace.c @@ -37,16 +37,16 @@ int upatch_process_mem_read(struct upatch_process *proc, unsigned long src, { ssize_t r = pread(proc->memfd, dst, size, (off_t)src); - return r != size ? -1 : 0; + return r != (ssize_t)size ? -1 : 0; } static int upatch_process_mem_write_ptrace(struct upatch_process *proc, - void *src, unsigned long dst, size_t size) + const void *src, unsigned long dst, size_t size) { - int ret; + long ret; while (ROUND_DOWN(size, sizeof(long)) != 0) { - ret = ptrace(PTRACE_POKEDATA, proc->pid, dst, *(unsigned long *)src); + ret = ptrace(PTRACE_POKEDATA, proc->pid, dst, *(const unsigned long *)src); if (ret) { return -1; } @@ -56,10 +56,10 @@ static int upatch_process_mem_write_ptrace(struct upatch_process *proc, } if (size) { - unsigned long tmp; + long tmp; tmp = ptrace(PTRACE_PEEKDATA, proc->pid, dst, NULL); - if (tmp == (unsigned long)-1 && errno) { + if (tmp == -1 && errno) { return -1; } memcpy(&tmp, src, size); @@ -73,7 +73,7 @@ static int upatch_process_mem_write_ptrace(struct upatch_process *proc, return 0; } -int upatch_process_mem_write(struct upatch_process *proc, void *src, +int upatch_process_mem_write(struct upatch_process *proc, const void *src, unsigned long dst, size_t size) { static int use_pwrite = 1; @@ -87,7 +87,7 @@ int upatch_process_mem_write(struct upatch_process *proc, void *src, return upatch_process_mem_write_ptrace(proc, src, dst, size); } - return w != size ? -1 : 0; + return w != (ssize_t)size ? -1 : 0; } static struct upatch_ptrace_ctx* upatch_ptrace_ctx_alloc( @@ -168,19 +168,20 @@ int upatch_ptrace_attach_thread(struct upatch_process *proc, int tid) int wait_for_stop(struct upatch_ptrace_ctx *pctx, const void *data) { - int ret, status = 0, pid = (int)(uintptr_t)data ?: pctx->pid; + long ret; + int status = 0, pid = (int)(uintptr_t)data ?: pctx->pid; log_debug("wait_for_stop(pctx->pid=%d, pid=%d)\n", pctx->pid, pid); while (1) { ret = ptrace(PTRACE_CONT, pctx->pid, NULL, (void *)(uintptr_t)status); if (ret < 0) { - log_error("Cannot start tracee %d, ret=%d\n", pctx->pid, ret); + log_error("Cannot start tracee %d, ret=%ld\n", pctx->pid, ret); return -1; } ret = waitpid(pid, &status, __WALL); if (ret < 0) { - log_error("Cannot wait tracee %d, ret=%d\n", pid, ret); + log_error("Cannot wait tracee %d, ret=%ld\n", pid, ret); return -1; } @@ -217,7 +218,7 @@ int upatch_ptrace_detach(struct upatch_ptrace_ctx *pctx) return 0; } -int upatch_execute_remote(struct upatch_ptrace_ctx *pctx, +long upatch_execute_remote(struct upatch_ptrace_ctx *pctx, const unsigned char *code, size_t codelen, struct user_regs_struct *pregs) { @@ -226,13 +227,13 @@ int upatch_execute_remote(struct upatch_ptrace_ctx *pctx, } unsigned long upatch_mmap_remote(struct upatch_ptrace_ctx *pctx, - unsigned long addr, size_t length, int prot, - int flags, int fd, off_t offset) + unsigned long addr, size_t length, unsigned long prot, + unsigned long flags, unsigned long fd, unsigned long offset) { - int ret; + long ret; unsigned long res = 0; - log_debug("mmap_remote: 0x%lx+%lx, %x, %x, %d, %lx\n", addr, length, + log_debug("mmap_remote: 0x%lx+%lx, %lx, %lx, %lu, %lx\n", addr, length, prot, flags, fd, offset); ret = upatch_arch_syscall_remote(pctx, __NR_mmap, (unsigned long)addr, length, prot, flags, fd, offset, &res); @@ -240,16 +241,16 @@ unsigned long upatch_mmap_remote(struct upatch_ptrace_ctx *pctx, return 0; } if (ret == 0 && res >= (unsigned long)-MAX_ERRNO) { - errno = -(long)res; + errno = -(int)res; return 0; } return res; } int upatch_mprotect_remote(struct upatch_ptrace_ctx *pctx, unsigned long addr, - size_t length, int prot) + size_t length, unsigned long prot) { - int ret; + long ret; unsigned long res; log_debug("mprotect_remote: 0x%lx+%lx\n", addr, length); @@ -259,7 +260,7 @@ int upatch_mprotect_remote(struct upatch_ptrace_ctx *pctx, unsigned long addr, if (ret < 0) return -1; if (ret == 0 && res >= (unsigned long)-MAX_ERRNO) { - errno = -(long)res; + errno = -(int)res; return -1; } @@ -269,7 +270,7 @@ int upatch_mprotect_remote(struct upatch_ptrace_ctx *pctx, unsigned long addr, int upatch_munmap_remote(struct upatch_ptrace_ctx *pctx, unsigned long addr, size_t length) { - int ret; + long ret; unsigned long res; log_debug("munmap_remote: 0x%lx+%lx\n", addr, length); @@ -278,7 +279,7 @@ int upatch_munmap_remote(struct upatch_ptrace_ctx *pctx, unsigned long addr, if (ret < 0) return -1; if (ret == 0 && res >= (unsigned long)-MAX_ERRNO) { - errno = -(long)res; + errno = -(int)res; return -1; } return 0; diff --git a/upatch-manage/upatch-ptrace.h b/upatch-manage/upatch-ptrace.h index 0c88434..b88c656 100644 --- a/upatch-manage/upatch-ptrace.h +++ b/upatch-manage/upatch-ptrace.h @@ -43,7 +43,7 @@ struct upatch_ptrace_ctx { int upatch_process_mem_read(struct upatch_process *proc, unsigned long src, void *dst, size_t size); -int upatch_process_mem_write(struct upatch_process *, void *, unsigned long, +int upatch_process_mem_write(struct upatch_process *, const void *, unsigned long, size_t); int upatch_ptrace_attach_thread(struct upatch_process *, int); @@ -54,31 +54,32 @@ int wait_for_stop(struct upatch_ptrace_ctx *, const void *); void copy_regs(struct user_regs_struct *, struct user_regs_struct *); -int upatch_arch_execute_remote_func(struct upatch_ptrace_ctx *pctx, +long upatch_arch_execute_remote_func(struct upatch_ptrace_ctx *pctx, const unsigned char *code, size_t codelen, struct user_regs_struct *pregs, int (*func)(struct upatch_ptrace_ctx *pctx, const void *data), const void *data); -int upatch_arch_syscall_remote(struct upatch_ptrace_ctx *, int, unsigned long, +long upatch_arch_syscall_remote(struct upatch_ptrace_ctx *, int, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long *); -unsigned long upatch_mmap_remote(struct upatch_ptrace_ctx *, unsigned long, - size_t, int, int, int, off_t); +unsigned long upatch_mmap_remote(struct upatch_ptrace_ctx *pctx, + unsigned long addr, size_t length, unsigned long prot, + unsigned long flags, unsigned long fd, unsigned long offset); -int upatch_mprotect_remote(struct upatch_ptrace_ctx *, unsigned long, size_t, - int); +int upatch_mprotect_remote(struct upatch_ptrace_ctx *pctx, unsigned long addr, + size_t length, unsigned long prot); int upatch_munmap_remote(struct upatch_ptrace_ctx *, unsigned long, size_t); -int upatch_execute_remote(struct upatch_ptrace_ctx *, const unsigned char *, +long upatch_execute_remote(struct upatch_ptrace_ctx *, const unsigned char *, size_t, struct user_regs_struct *); -size_t get_origin_insn_len(); -size_t get_upatch_insn_len(); -size_t get_upatch_addr_len(); -unsigned long get_new_insn(struct object_file *, unsigned long, unsigned long); +size_t get_origin_insn_len(void); +size_t get_upatch_insn_len(void); +size_t get_upatch_addr_len(void); +unsigned long get_new_insn(void); #endif diff --git a/upatch-manage/upatch-resolve.c b/upatch-manage/upatch-resolve.c index 0e992da..197ea2f 100644 --- a/upatch-manage/upatch-resolve.c +++ b/upatch-manage/upatch-resolve.c @@ -50,7 +50,7 @@ static unsigned long resolve_rela_dyn(struct upatch_elf *uelf, * some rela don't have the symbol index, use the symbol's value and * rela's addend to find the symbol. for example, R_X86_64_IRELATIVE. */ - if (rela_dyn[i].r_addend != patch_sym->st_value) { + if (rela_dyn[i].r_addend != (long)patch_sym->st_value) { continue; } } @@ -110,7 +110,7 @@ static unsigned long resolve_rela_plt(struct upatch_elf *uelf, * some rela don't have the symbol index, use the symbol's value and * rela's addend to find the symbol. for example, R_X86_64_IRELATIVE. */ - if (rela_plt[i].r_addend != patch_sym->st_value) { + if (rela_plt[i].r_addend != (long)patch_sym->st_value) { continue; } } else { @@ -140,7 +140,7 @@ static unsigned long resolve_rela_plt(struct upatch_elf *uelf, } static unsigned long resolve_dynsym(struct upatch_elf *uelf, - struct object_file *obj, const char *name, GElf_Sym *patch_sym) + struct object_file *obj, const char *name) { unsigned long elf_addr = 0; struct running_elf *relf = uelf->relf; @@ -178,8 +178,7 @@ static unsigned long resolve_dynsym(struct upatch_elf *uelf, return elf_addr; } -static unsigned long resolve_sym(struct upatch_elf *uelf, - struct object_file *obj, const char *name, GElf_Sym *patch_sym) +static unsigned long resolve_sym(struct upatch_elf *uelf, const char *name) { unsigned long elf_addr = 0; struct running_elf *relf = uelf->relf; @@ -217,7 +216,7 @@ static unsigned long resolve_sym(struct upatch_elf *uelf, } static unsigned long resolve_patch_sym(struct upatch_elf *uelf, - struct object_file *obj, const char *name, GElf_Sym *patch_sym) + const char *name, GElf_Sym *patch_sym) { unsigned long elf_addr = 0; struct running_elf *relf = uelf->relf; @@ -265,17 +264,17 @@ static unsigned long resolve_symbol(struct upatch_elf *uelf, /* resolve from dynsym */ if (!elf_addr) { - elf_addr = resolve_dynsym(uelf, obj, name, &patch_sym); + elf_addr = resolve_dynsym(uelf, obj, name); } /* resolve from sym */ if (!elf_addr) { - elf_addr = resolve_sym(uelf, obj, name, &patch_sym); + elf_addr = resolve_sym(uelf, name); } /* resolve from patch sym */ if (!elf_addr) { - elf_addr = resolve_patch_sym(uelf, obj, name, &patch_sym); + elf_addr = resolve_patch_sym(uelf, name, &patch_sym); } if (!elf_addr) { diff --git a/upatch-manage/upatch-resolve.h b/upatch-manage/upatch-resolve.h index 324c49e..9b31dce 100644 --- a/upatch-manage/upatch-resolve.h +++ b/upatch-manage/upatch-resolve.h @@ -29,7 +29,7 @@ /* jmp table, solve limit for the jmp instruction, Used for both PLT/GOT */ struct upatch_jmp_table_entry; -unsigned int get_jmp_table_entry(); +unsigned int get_jmp_table_entry(void); unsigned long insert_plt_table(struct upatch_elf *, struct object_file *, unsigned long, unsigned long); -- 2.34.1