Skip to content

Commit

Permalink
Confirm I can see loaded is included correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewQuijano committed Nov 16, 2024
1 parent 0b3ee96 commit 46765b5
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions panda/plugins/loaded/loaded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,36 +130,43 @@ void linux_mmap_pgoff_return(CPUState *cpu,target_ulong pc,uint32_t addr,uint32_
// https://man7.org/linux/man-pages/man2/mmap.2.html
// https://github.com/panda-re/panda/blob/dev/panda/plugins/syscalls2/generated/syscalls_ext_typedefs_x64.h#L7405-L7412
#elif defined(TARGET_X86_64)

void linux_mprotect_return(CPUState* cpu, target_ulong pc,
uint64_t start, uint32_t len,
uint64_t prot, int32_t pkey) {
if (debug) {
printf("[loaded] mprotect()\n");
}
}

void linux_mmap_return(CPUState *cpu, target_ulong pc,
uint64_t addr, uint64_t len, uint64_t prot,
uint64_t flags, uint64_t fd, uint64_t offset) {

CPUArchState *env = (CPUArchState*)cpu->env_ptr;
target_ulong asid = panda_current_asid(cpu);
if (running_procs.count(asid) == 0) {
//printf ("linux_mmap_pgoff_enter for asid=0x%x fd=%d -- dont know about that asid. discarding \n", (unsigned int) asid, (int) fd);
return;
}
if ((int32_t) fd == -1) {
//printf ("linux_mmap_pgoff_enter for asid=0x%x fd=%d flags=%x -- not valid fd . . . \n", (unsigned int) asid, (int) fd, flags);
return;
}
OsiProc proc = running_procs[asid];
char *filename = osi_linux_fd_to_filename(cpu, &proc, fd);
char * filename = osi_linux_fd_to_filename(cpu, &proc, fd);
// gets us offset into the file. could be useful
// uint64_t pos = osi_linux_fd_to_pos(env, &proc, fd);
// if a filename exists and permission is executable
if (filename != NULL && ((prot & PROT_EXEC) == PROT_EXEC)) {
if (debug) {
printf("[loaded] linux_mmap_pgoff(fd=%lu filename=[%s] len=%lu prot=%lx flags=%lx pgoff=%lu)=%lx\n",
fd, filename, len, prot, flags, offset, (unsigned long)env->regs[R_EAX]);
printf("[loaded] linux_mmap(fd=%lu filename=[%s] len=%lu prot=%lx flags=%lx pgoff=%lu)=%lx\n",
fd, filename, len, prot, flags, offset, (unsigned long) env->regs[R_EAX]);
}
PPP_RUN_CB(on_library_load, cpu, pc, filename, env->regs[R_EAX], len);
}
else if ((prot & PROT_EXEC) == PROT_EXEC) {
printf("[loaded] mapped executable section without a filename!\n");
printf("[loaded] linux_mmap_pgoff(fd=%lu len=%lu prot=%lx flags=%lx pgoff=%lu)=%lx\n",
fd, len, prot, flags, offset, (unsigned long)env->regs[R_EAX]);
printf("[loaded] linux_mmap(fd=%lu len=%lu prot=%lx flags=%lx pgoff=%lu)=%lx\n",
fd, len, prot, flags, offset, (unsigned long) env->regs[R_EAX]);
}
}
#endif
Expand Down Expand Up @@ -201,7 +208,7 @@ void osi_foo(CPUState *cpu, TranslationBlock *tb) {
return;
}
bool init_plugin(void *self) {
//panda_arg_list *args = panda_get_args("loaded");
// panda_arg_list *args = panda_get_args("loaded");

panda_require("osi");
assert(init_osi_api());
Expand All @@ -218,8 +225,9 @@ bool init_plugin(void *self) {

PPP_REG_CB("syscalls2", on_sys_mmap_pgoff_return, linux_mmap_pgoff_return);
// don't use these at them moment
//PPP_REG_CB("syscalls2", on_sys_old_mmap_return, linux_old_mmap_return);
//PPP_REG_CB("syscalls2", on_sys_mprotect_return, linux_mprotect_return);
// PPP_REG_CB("syscalls2", on_sys_old_mmap_return, linux_old_mmap_return);
// PPP_REG_CB("syscalls2", on_sys_mprotect_return, linux_mprotect_return);
printf("The loaded plugin is supported on i386\n");
#elif defined(TARGET_X86_64)
{
panda_cb pcb;
Expand All @@ -229,6 +237,8 @@ bool init_plugin(void *self) {
// Tell Plugin 'syscall2', that if a systemcall 'mmap' occurs, then run the code in ;'linux_mmap_return'
// https://www.linuxquestions.org/questions/linux-general-1/difference-between-mmap2-syscall-and-mmap_pgoff-syscall-for-32-bit-linux-4175622986/
PPP_REG_CB("syscalls2", on_sys_mmap_return, linux_mmap_return);
PPP_REG_CB("syscalls2", on_sys_mprotect_return, linux_mprotect_return);
printf("The loaded plugin is supported on x86-64\n");
#else
fprintf(stderr, "The loaded plugin is not currently supported on this platform.\n");
return false;
Expand Down

0 comments on commit 46765b5

Please sign in to comment.