Rizin
unix-like reverse engineering framework and cli tools
io_rzk_linux.h File Reference
#include <rz_io.h>
#include <rz_lib.h>
#include <rz_types.h>
#include <rz_util.h>
#include <rz_util/rz_print.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <errno.h>

Go to the source code of this file.

Classes

struct  io_rzk_linux
 
struct  rzk_data
 
struct  rzk_kernel_map_info
 
struct  rzk_kernel_maps
 
struct  rzk_control_reg
 
struct  rzk_proc_info
 

Macros

#define MAX_PHYS_ADDR   128
 
#define RZ_TYPE   0x69
 
#define READ_KERNEL_MEMORY   0x1
 
#define WRITE_KERNEL_MEMORY   0x2
 
#define READ_PROCESS_ADDR   0x3
 
#define WRITE_PROCESS_ADDR   0X4
 
#define READ_PHYSICAL_ADDR   0x5
 
#define WRITE_PHYSICAL_ADDR   0x6
 
#define GET_KERNEL_MAP   0x7
 
#define READ_CONTROL_REG   0x8
 
#define PRINT_PROC_INFO   0x9
 
#define rzk_data_size   sizeof(struct rzk_data)
 
#define rzk_kernel_maps_size   sizeof(struct rzk_kernel_maps)
 
#define rzk_control_reg_size   sizeof(struct rzk_control_reg)
 
#define rzk_proc_info_size   sizeof(struct rzk_proc_info)
 
#define IOCTL_READ_KERNEL_MEMORY   _IOR(RZ_TYPE, READ_KERNEL_MEMORY, rzk_data_size)
 
#define IOCTL_WRITE_KERNEL_MEMORY   _IOR(RZ_TYPE, WRITE_KERNEL_MEMORY, rzk_data_size)
 
#define IOCTL_READ_PROCESS_ADDR   _IOR(RZ_TYPE, READ_PROCESS_ADDR, rzk_data_size)
 
#define IOCTL_WRITE_PROCESS_ADDR   _IOR(RZ_TYPE, WRITE_PROCESS_ADDR, rzk_data_size)
 
#define IOCTL_READ_PHYSICAL_ADDR   _IOR(RZ_TYPE, READ_PHYSICAL_ADDR, rzk_data_size)
 
#define IOCTL_WRITE_PHYSICAL_ADDR   _IOR(RZ_TYPE, WRITE_PHYSICAL_ADDR, rzk_data_size)
 
#define IOCTL_GET_KERNEL_MAP   _IOR(RZ_TYPE, GET_KERNEL_MAP, rzk_kernel_maps_size)
 
#define IOCTL_READ_CONTROL_REG   _IOR(RZ_TYPE, READ_CONTROL_REG, rzk_control_reg_size)
 
#define IOCTL_PRINT_PROC_INFO   _IOR(RZ_TYPE, PRINT_PROC_INFO, rzk_data_size)
 
#define VM_READ   0x1
 
#define VM_WRITE   0x2
 
#define VM_EXEC   0x4
 
#define VM_MAYSHARE   0x80
 

Functions

int ReadMemory (RzIO *io, RzIODesc *iodesc, int ioctl_n, size_t pid, size_t address, ut8 *buf, int len)
 
int WriteMemory (RzIO *io, RzIODesc *iodesc, int ioctl_n, size_t pid, ut64 address, const ut8 *buf, int len)
 
int run_ioctl_command (RzIO *io, RzIODesc *iodesc, const char *buf)
 

Variables

struct io_rzk_linux rzk_struct
 

Macro Definition Documentation

◆ GET_KERNEL_MAP

#define GET_KERNEL_MAP   0x7

Definition at line 94 of file io_rzk_linux.h.

◆ IOCTL_GET_KERNEL_MAP

#define IOCTL_GET_KERNEL_MAP   _IOR(RZ_TYPE, GET_KERNEL_MAP, rzk_kernel_maps_size)

Definition at line 116 of file io_rzk_linux.h.

◆ IOCTL_PRINT_PROC_INFO

#define IOCTL_PRINT_PROC_INFO   _IOR(RZ_TYPE, PRINT_PROC_INFO, rzk_data_size)

Definition at line 118 of file io_rzk_linux.h.

◆ IOCTL_READ_CONTROL_REG

#define IOCTL_READ_CONTROL_REG   _IOR(RZ_TYPE, READ_CONTROL_REG, rzk_control_reg_size)

Definition at line 117 of file io_rzk_linux.h.

◆ IOCTL_READ_KERNEL_MEMORY

#define IOCTL_READ_KERNEL_MEMORY   _IOR(RZ_TYPE, READ_KERNEL_MEMORY, rzk_data_size)

Definition at line 110 of file io_rzk_linux.h.

◆ IOCTL_READ_PHYSICAL_ADDR

#define IOCTL_READ_PHYSICAL_ADDR   _IOR(RZ_TYPE, READ_PHYSICAL_ADDR, rzk_data_size)

Definition at line 114 of file io_rzk_linux.h.

◆ IOCTL_READ_PROCESS_ADDR

#define IOCTL_READ_PROCESS_ADDR   _IOR(RZ_TYPE, READ_PROCESS_ADDR, rzk_data_size)

Definition at line 112 of file io_rzk_linux.h.

◆ IOCTL_WRITE_KERNEL_MEMORY

#define IOCTL_WRITE_KERNEL_MEMORY   _IOR(RZ_TYPE, WRITE_KERNEL_MEMORY, rzk_data_size)

Definition at line 111 of file io_rzk_linux.h.

◆ IOCTL_WRITE_PHYSICAL_ADDR

#define IOCTL_WRITE_PHYSICAL_ADDR   _IOR(RZ_TYPE, WRITE_PHYSICAL_ADDR, rzk_data_size)

Definition at line 115 of file io_rzk_linux.h.

◆ IOCTL_WRITE_PROCESS_ADDR

#define IOCTL_WRITE_PROCESS_ADDR   _IOR(RZ_TYPE, WRITE_PROCESS_ADDR, rzk_data_size)

Definition at line 113 of file io_rzk_linux.h.

◆ MAX_PHYS_ADDR

#define MAX_PHYS_ADDR   128

Definition at line 19 of file io_rzk_linux.h.

◆ PRINT_PROC_INFO

#define PRINT_PROC_INFO   0x9

Definition at line 96 of file io_rzk_linux.h.

◆ READ_CONTROL_REG

#define READ_CONTROL_REG   0x8

Definition at line 95 of file io_rzk_linux.h.

◆ READ_KERNEL_MEMORY

#define READ_KERNEL_MEMORY   0x1

Definition at line 88 of file io_rzk_linux.h.

◆ READ_PHYSICAL_ADDR

#define READ_PHYSICAL_ADDR   0x5

Definition at line 92 of file io_rzk_linux.h.

◆ READ_PROCESS_ADDR

#define READ_PROCESS_ADDR   0x3

Definition at line 90 of file io_rzk_linux.h.

◆ RZ_TYPE

#define RZ_TYPE   0x69

Definition at line 86 of file io_rzk_linux.h.

◆ rzk_control_reg_size

#define rzk_control_reg_size   sizeof(struct rzk_control_reg)

Definition at line 106 of file io_rzk_linux.h.

◆ rzk_data_size

#define rzk_data_size   sizeof(struct rzk_data)

Definition at line 104 of file io_rzk_linux.h.

◆ rzk_kernel_maps_size

#define rzk_kernel_maps_size   sizeof(struct rzk_kernel_maps)

Definition at line 105 of file io_rzk_linux.h.

◆ rzk_proc_info_size

#define rzk_proc_info_size   sizeof(struct rzk_proc_info)

Definition at line 107 of file io_rzk_linux.h.

◆ VM_EXEC

#define VM_EXEC   0x4

Definition at line 122 of file io_rzk_linux.h.

◆ VM_MAYSHARE

#define VM_MAYSHARE   0x80

Definition at line 123 of file io_rzk_linux.h.

◆ VM_READ

#define VM_READ   0x1

Definition at line 120 of file io_rzk_linux.h.

◆ VM_WRITE

#define VM_WRITE   0x2

Definition at line 121 of file io_rzk_linux.h.

◆ WRITE_KERNEL_MEMORY

#define WRITE_KERNEL_MEMORY   0x2

Definition at line 89 of file io_rzk_linux.h.

◆ WRITE_PHYSICAL_ADDR

#define WRITE_PHYSICAL_ADDR   0x6

Definition at line 93 of file io_rzk_linux.h.

◆ WRITE_PROCESS_ADDR

#define WRITE_PROCESS_ADDR   0X4

Definition at line 91 of file io_rzk_linux.h.

Function Documentation

◆ ReadMemory()

int ReadMemory ( RzIO io,
RzIODesc iodesc,
int  ioctl_n,
size_t  pid,
size_t  address,
ut8 buf,
int  len 
)

Definition at line 272 of file io_rzk_linux.c.

272  {
273  int ret = -1;
274  int pagesize, newlen;
275  ut64 pageaddr, offset;
276  bool flag = 0;
277  ut8 garbage;
278 
279  if (iodesc && iodesc->data > 0 && buf) {
280  struct rzk_data data;
281 
282  data.pid = pid;
283  data.addr = address;
284  data.len = len;
285  data.buff = (ut8 *)calloc(len + 1, 1);
286  if (!data.buff) {
287  return -1;
288  }
289 
290  ret = ioctl((int)(size_t)iodesc->data, ioctl_n, &data);
291  if (!ret) {
292  memcpy(buf, data.buff, len);
293  ret = len;
294  } else {
295  garbage = 0xff;
296  flag = 0;
297  offset = 0;
298  pagesize = getpagesize();
299  pageaddr = address + pagesize;
300  pageaddr -= (pageaddr % pagesize);
301  if ((len - (int)(pageaddr - address)) > 0) {
302  data.len = pageaddr - address;
303  ret = ioctl((int)(size_t)iodesc->data, ioctl_n, &data);
304  if (!ret) {
305  memcpy(buf + offset, data.buff, pageaddr - address);
306  flag = 1;
307  } else {
308  memset(buf + offset, garbage, pageaddr - address);
309  }
310 
311  offset = pageaddr - address;
312  newlen = len - offset;
313  while (newlen >= pagesize) {
314  data.addr = pageaddr;
315  data.len = pagesize;
316 
317  ret = ioctl((int)(size_t)iodesc->data, ioctl_n, &data);
318  if (!ret) {
319  memcpy(buf + offset, data.buff, pagesize);
320  flag = 1;
321  } else {
322  memset(buf + offset, garbage, pagesize);
323  }
324  pageaddr += pagesize;
325  offset += pagesize;
326  newlen -= pagesize;
327  }
328 
329  data.addr = pageaddr;
330  data.len = newlen;
331  ret = ioctl((int)(size_t)iodesc->data, ioctl_n, &data);
332  if (!ret) {
333  memcpy(buf + offset, data.buff, newlen);
334  flag = 1;
335  } else {
336  memset(buf + offset, garbage, newlen);
337  }
338  }
339  ret = flag ? len : -1;
340  }
341 
342  free(data.buff);
343  } else if (!buf) {
344  io->cb_printf("Invalid input buffer.\n");
345  } else {
346  io->cb_printf("IOCTL device not initialized.\n");
347  }
348  return ret;
349 }
size_t len
Definition: 6502dis.c:15
static static sync static getppid static getegid const char static filename ioctl
Definition: sflib.h:62
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc pid
Definition: sflib.h:64
void * data
Definition: rz_io.h:102
PrintfCallback cb_printf
Definition: rz_io.h:91
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References rzk_data::addr, rzk_data::buff, calloc(), rz_io_t::cb_printf, rz_io_desc_t::data, free(), ioctl, len, rzk_data::len, memcpy(), memset(), pid, rzk_data::pid, and ut64().

Referenced by run_old_command(), and rzk__read().

◆ run_ioctl_command()

int run_ioctl_command ( RzIO io,
RzIODesc iodesc,
const char *  buf 
)

Definition at line 842 of file io_rzk_linux.c.

842  {
843  buf = rz_str_ichr((char *)buf, ' ');
844 
845  if (!run_new_command(io, iodesc, buf)) {
846  return run_old_command(io, iodesc, buf);
847  }
848  return 0;
849 }
int run_new_command(RzIO *io, RzIODesc *iodesc, const char *buf)
Definition: io_rzk_linux.c:772
int run_old_command(RzIO *io, RzIODesc *iodesc, const char *buf)
Definition: io_rzk_linux.c:385
RZ_API char * rz_str_ichr(char *str, char chr)
Definition: str.c:660

References run_new_command(), run_old_command(), and rz_str_ichr().

Referenced by rzk__system().

◆ WriteMemory()

int WriteMemory ( RzIO io,
RzIODesc iodesc,
int  ioctl_n,
size_t  pid,
ut64  address,
const ut8 buf,
int  len 
)

Definition at line 351 of file io_rzk_linux.c.

351  {
352  int ret = -1;
353 
354  if (iodesc && iodesc->data > 0 && buf) {
355  struct rzk_data data;
356 
357  data.pid = pid;
358  data.addr = address;
359  data.len = len;
360  data.buff = (ut8 *)calloc(len + 1, 1);
361  data.wp = rzk_struct.wp;
362 
363  if (!data.buff) {
364  return -1;
365  }
366 
367  memcpy(data.buff, buf, len);
368  ret = ioctl((int)(size_t)iodesc->data, ioctl_n, &data);
369  if (!ret) {
370  ret = len;
371  } else {
372  io->cb_printf("Write failed. ioctl err: %s\n", strerror(errno));
373  ret = -1;
374  }
375 
376  free(data.buff);
377  } else if (!buf) {
378  io->cb_printf("Invalid input buffer.\n");
379  } else {
380  io->cb_printf("IOCTL device not initialized.\n");
381  }
382  return ret;
383 }
struct io_rzk_linux rzk_struct

References rzk_data::addr, rzk_data::buff, calloc(), rz_io_t::cb_printf, rz_io_desc_t::data, free(), ioctl, len, rzk_data::len, memcpy(), pid, rzk_data::pid, rzk_struct, io_rzk_linux::wp, and rzk_data::wp.

Referenced by run_old_command(), and rzk__write().

Variable Documentation

◆ rzk_struct