Initial implementation of io.fd plugin ##io (#17158)

Useful for `self://` and r2frida when injecting the lib inside a target process

Co-authored-by: pancake <pancake@nopcode.org>
This commit is contained in:
pancake 2020-07-08 05:28:08 +02:00 committed by GitHub
parent 57c9fc7dad
commit cccaf55a6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 132 additions and 0 deletions

View File

@ -506,6 +506,7 @@ extern RIOPlugin r_io_plugin_ar;
extern RIOPlugin r_io_plugin_rbuf;
extern RIOPlugin r_io_plugin_winedbg;
extern RIOPlugin r_io_plugin_gprobe;
extern RIOPlugin r_io_plugin_fd;
#if __cplusplus
}

View File

@ -9,6 +9,7 @@ r_io_sources = [
'undo.c',
'p_cache.c',
'p/io_ar.c',
'p/io_fd.c',
'p/io_bfdbg.c',
'p/io_bochs.c',
'p/io_debug.c',

16
libr/io/p/fd.mk Normal file
View File

@ -0,0 +1,16 @@
OBJ_FD=io_fd.o
STATIC_OBJ+=${OBJ_FD}
TARGET_FD=io_fd.${EXT_SO}
ALL_TARGETS+=${TARGET_FD}
ifeq (${WITHPIC},0)
LINKFLAGS+=../../util/libr_util.a
LINKFLAGS+=../../io/libr_io.a
else
LINKFLAGS+=-L../../util -lr_util
LINKFLAGS+=-L.. -lr_io
endif
${TARGET_FD}: ${OBJ_FD}
${CC_LIB} $(call libname,io_fd) ${CFLAGS} -o ${TARGET_FD} ${OBJ_FD} ${LINKFLAGS}

112
libr/io/p/io_fd.c Normal file
View File

@ -0,0 +1,112 @@
/* radare - LGPL - Copyright 2020 - pancake */
#include "r_io.h"
#include "r_lib.h"
#include <stdio.h>
#include <stdlib.h>
#include <r_cons.h>
#include <sys/types.h>
#if __WINDOWS__
#define FDURI "handle://"
#else
#define FDURI "fd://"
#endif
typedef struct {
int fd;
} RIOFdata;
static int __write(RIO *io, RIODesc *desc, const ut8 *buf, int count) {
RIOFdata *fdd = (RIOFdata*)desc->data;
if (fdd) {
return write (fdd->fd, buf, count);
}
return -1;
}
static bool __resize(RIO *io, RIODesc *desc, ut64 count) {
RIOFdata *fdd = (RIOFdata*)desc->data;
if (fdd) {
return ftruncate (fdd->fd, count) == 0;
}
return false;
}
static int __read(RIO *io, RIODesc *desc, ut8 *buf, int count) {
RIOFdata *fdd = (RIOFdata*)desc->data;
if (fdd) {
r_cons_break_push (NULL, NULL);
int res = read (fdd->fd, buf, count);
r_cons_break_pop ();
return res;
}
return -1;
}
static int __close(RIODesc *desc) {
R_FREE (desc->data);
return 0;
}
static ut64 __lseek(RIO* io, RIODesc *desc, ut64 offset, int whence) {
RIOFdata *fdd = (RIOFdata*)desc->data;
if (fdd) {
return lseek (fdd->fd, offset, whence);
}
return 0;
}
static bool __check(RIO *io, const char *pathname, bool many) {
return !strncmp (pathname, FDURI, strlen (FDURI));
}
static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) {
if (r_sandbox_enable (false)) {
eprintf ("Do not permit " FDURI " in sandbox mode.\n");
return NULL;
}
if (!__check (io, pathname, 0)) {
return NULL;
}
RIOFdata *fdd = R_NEW0 (RIOFdata);
if (fdd) {
fdd->fd = r_num_math (NULL, pathname + strlen (FDURI));
#if __WINDOWS__
fdd->fd = _open_osfhandle (fdd->fd, 0);
#endif
if (fdd->fd < 0) {
free (fdd);
eprintf ("Invalid filedescriptor.\n");
return NULL;
}
}
return r_io_desc_new (io, &r_io_plugin_fd, pathname, R_PERM_RW | rw, mode, fdd);
}
RIOPlugin r_io_plugin_fd = {
#if __WINDOWS__
.name = "handle",
.desc = "Local process file handle IO",
#else
.name = "fd",
.desc = "Local process filedescriptor IO",
#endif
.uris = FDURI,
.license = "MIT",
.open = __open,
.close = __close,
.read = __read,
.check = __check,
.lseek = __lseek,
.write = __write,
.resize = __resize,
};
#ifndef R2_PLUGIN_INCORE
R_API RLibStruct radare_plugin = {
.type = R_LIB_TYPE_IO,
.data = &r_io_plugin_fd,
.version = R2_VERSION
};
#endif

View File

@ -264,6 +264,7 @@ fs_plugins = [
io_plugins = [
'ar',
'fd',
'bfdbg',
'bochs',
'debug',

View File

@ -260,6 +260,7 @@ io.winedbg
io.zip
io.r2k
io.ar
io.fd
io.rbuf
lang.spp
lang.vala