From d3b2d1a75c5c62e4d7142124162e2deb8273b32b Mon Sep 17 00:00:00 2001 From: Tomas Mudrunka Date: Tue, 11 Feb 2014 14:28:27 +0100 Subject: [PATCH] Nano LD_PRELOAD wrapper --- c/cano/Makefile | 5 ++ c/cano/cano.c | 71 +++++++++++++++++ c/cano/old/Makefile | 5 ++ c/cano/old/cano.c | 52 ++++++++++++ c/cano/old/cano2.c | 81 +++++++++++++++++++ c/cano/old/cano3.c | 99 +++++++++++++++++++++++ c/cano/old/cano4.c | 128 ++++++++++++++++++++++++++++++ c/ghetto-sound-system/ftdi-midi.c | 17 ++-- 8 files changed, 451 insertions(+), 7 deletions(-) create mode 100644 c/cano/Makefile create mode 100644 c/cano/cano.c create mode 100644 c/cano/old/Makefile create mode 100644 c/cano/old/cano.c create mode 100644 c/cano/old/cano2.c create mode 100644 c/cano/old/cano3.c create mode 100644 c/cano/old/cano4.c diff --git a/c/cano/Makefile b/c/cano/Makefile new file mode 100644 index 0000000..33a5ebd --- /dev/null +++ b/c/cano/Makefile @@ -0,0 +1,5 @@ +proj=cano + +unexport LD_PRELOAD +$(proj).so: $(proj).c + $(CC) -shared -fPIC -ldl $? -o $@ diff --git a/c/cano/cano.c b/c/cano/cano.c new file mode 100644 index 0000000..5ca71bb --- /dev/null +++ b/c/cano/cano.c @@ -0,0 +1,71 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +#include +//#include +#include +#include +#include +#include +#include + +int stat(const char *path, struct stat *buf) { + buf->st_size = (off_t) 5; + return 0; +} + +int pipe_read() { + return fileno(popen("xxd -r /tmp/b", "r")); +} +int pipe_write() { + return fileno(popen("read -n 1 i || exit; (echo -n \"$i\"; cat) | xxd > /tmp/b", "w")); +} +int open(const char *filename, int flags, ...) +{ + static int (*open_orig)(const char *, int, mode_t); + int ret; + va_list ap; + mode_t mode; + + if (!open_orig) { + open_orig = dlsym(RTLD_NEXT, "open"); + } + + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + + if(strcmp(filename,"/tmp/a") != 0) { + ret = open_orig(filename, flags, mode); + //ret = syscall(SYS_open,redirect_name(filename),flags,mode); + } else { + //ret = fileno(popen("echo aaaa", "rb")); + if((flags & O_WRONLY) || (flags & O_RDWR)) { + ret = pipe_write(); + } else { + ret = pipe_read(); + } + //ssize_t write(int fd, const void *buf, size_t count); + } + + //printf("open(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret); + + return ret; +} + +//hackystuff, fixme: +int open64(const char *filename, int flags, ...) { + va_list ap; + mode_t mode; + + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + + return open(filename, flags, mode); +} + diff --git a/c/cano/old/Makefile b/c/cano/old/Makefile new file mode 100644 index 0000000..33a5ebd --- /dev/null +++ b/c/cano/old/Makefile @@ -0,0 +1,5 @@ +proj=cano + +unexport LD_PRELOAD +$(proj).so: $(proj).c + $(CC) -shared -fPIC -ldl $? -o $@ diff --git a/c/cano/old/cano.c b/c/cano/old/cano.c new file mode 100644 index 0000000..1f374b3 --- /dev/null +++ b/c/cano/old/cano.c @@ -0,0 +1,52 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + + +static const char *fd_to_path(int fd) { + static char path[PATH_MAX]; + sprintf(path,"/proc/self/fd/%d",fd); + int l = (int) readlink((char * __restrict__) path, path, PATH_MAX); + path[l]=0; + return path; +} + +int at_close() { + unsetenv("LD_PRELOAD"); + return system(getenv("AT_CLOSE")); + /* + static const char *command; + if(!command) { + command = getenv("AT_CLOSE"); + unsetenv("LD_PRELOAD"); + } + fprintf(stderr,"%s\n",command); + system(command); + return 0; + */ +} + +int close(int fd) { + //fprintf(stderr,"%s(%d,%s);\n",__func__,fd,fd_to_path(fd)); + static int (*close_orig)(int fd); + if (!close_orig) { + close_orig = dlsym(RTLD_NEXT, "close"); + } + int ret = close_orig(fd); + //int ret = syscall(SYS_close,fd); + at_close(); + return ret; +} +int fclose(FILE *fd) { + //fprintf(stderr,"%s(%d,%d,%s);\n",__func__,fileno(fd),fd,fd_to_path(fileno(fd))); + static int (*fclose_orig)(FILE *fd); + if (!fclose_orig) { + fclose_orig = dlsym(RTLD_NEXT, "fclose"); + } + int ret = fclose_orig(fd); + at_close(); + return ret; +} diff --git a/c/cano/old/cano2.c b/c/cano/old/cano2.c new file mode 100644 index 0000000..3483431 --- /dev/null +++ b/c/cano/old/cano2.c @@ -0,0 +1,81 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define i { srand(23); printf("%s();\n",__func__); } +//Catch open() calls (while redirecting filename): +static const char *redirect_name(const char *name) +{ + if( + (strcmp(name,"/dev/random") == 0) || + (strcmp(name,"/dev/urandom") == 0) + ) { + printf("REDIRECT HIT: %s\n", name); + return "/dev/zero"; + } + return name; +} + + +int open(const char *filename, int flags, ...) +{ + //srand(23); + static int (*open_orig)(const char *, int, mode_t); + int ret; + va_list ap; + mode_t mode; + + if (!open_orig) { + open_orig = dlsym(RTLD_NEXT, "open"); + } + + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + + if(strcmp(filename,"/tmp/a") != 0) { + ret = open_orig(redirect_name(filename), flags, mode); + //ret = syscall(SYS_open,redirect_name(filename),flags,mode); + } else { + ret = popen("tee aaaa", "w"); + } + + printf("open(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret); + + return ret; +} + +int open64(const char *filename, int flags, ...) +{ + //srand(23); + static int (*open64_orig)(const char *, int, mode_t); + int ret; + va_list ap; + mode_t mode; + + if (!open64_orig) { + open64_orig = dlsym(RTLD_NEXT, "open64"); + } + + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + + if(strcmp(filename,"/tmp/a") != 0) { + ret = open64_orig(redirect_name(filename), flags, mode); + //ret = syscall(SYS_open64,redirect_name(filename),flags,mode); + } else { + ret = popen("tee aaaa", "w"); + } + + printf("open64(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret); + + return ret; +} diff --git a/c/cano/old/cano3.c b/c/cano/old/cano3.c new file mode 100644 index 0000000..68ebefb --- /dev/null +++ b/c/cano/old/cano3.c @@ -0,0 +1,99 @@ +#define _GNU_SOURCE +/* +#include +#include +#include +#include +#include +#include + + #include + #include + #include +*/ +#include +#include +#include +#include + +#define i { srand(23); fprintf(stderr,"%s();\n",__func__); } +//Catch open() calls (while redirecting filename): + +static const char *fd_to_path(int fd) { + static char path[PATH_MAX]; + sprintf(path,"/proc/self/fd/%d",fd); + int l = (int) readlink((char * __restrict__) path, path, PATH_MAX); + path[l]=0; + return path; +} + +int close(int fd) { + //fputs(fd_to_path(fd),stderr); + fprintf(stderr,"%s(%d,%s);\n",__func__,fd,fd_to_path(fd)); + if(fd>2) return syscall(SYS_close,fd); +} +int fclose(FILE *fd) { + //fputs(fd_to_path(fd),stderr); + fprintf(stderr,"%s(%d,%d,%s);\n",__func__,fileno(fd),fd,fd_to_path(fileno(fd))); + static ssize_t (*fclose_orig)(FILE *fd); + if (!fclose_orig) { + fclose_orig = dlsym(RTLD_NEXT, "fclose"); + } + return fclose_orig(fd); + //return syscall(SYS_close,fileno(fd)); +} +/* +ssize_t write(int fd, const void *buf, size_t count) { + //fputs(fd_to_path(fd),stderr); + printf("%d",fd); + i; + return syscall(SYS_write,fd,buf,count); +} +*/ +/* +static const char *redirect_name(const char *name) +{ + if( + (strcmp(name,"/dev/random") == 0) || + (strcmp(name,"/dev/urandom") == 0) + ) { + printf("REDIRECT HIT: %s\n", name); + return "/dev/zero"; + } + return name; +} + + +int open(const char *filename, int flags, ...) +{ + //srand(23); + static int (*open_orig)(const char *, int, mode_t); + int ret; + va_list ap; + mode_t mode; + + if (!open_orig) { + open_orig = dlsym(RTLD_NEXT, "open"); + } + + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + + if(strcmp(filename,"/tmp/lol.txt.asc") != 0) { + ret = open_orig(redirect_name(filename), flags, mode); + //ret = syscall(SYS_open,redirect_name(filename),flags,mode); + } else { + if(flags & O_RDONLY) { + ret = fileno(popen("cat aaaa", "r")); + } else { + ret = fileno(popen("tee aaaa", "w")); + } + } + + printf("open(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret); + + return ret; +} + +*/ diff --git a/c/cano/old/cano4.c b/c/cano/old/cano4.c new file mode 100644 index 0000000..69d0823 --- /dev/null +++ b/c/cano/old/cano4.c @@ -0,0 +1,128 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +//#include +#include +#include +#include +#include + +#define i { srand(23); fprintf(stderr,"%s();\n",__func__); } +//Catch open() calls (while redirecting filename): + +static const char *fd_to_path(int fd) { + static char path[PATH_MAX]; + sprintf(path,"/proc/self/fd/%d",fd); + int l = (int) readlink((char * __restrict__) path, path, PATH_MAX); + path[l]=0; + return path; +} + + +int pipe_read() { + /* + int pipefd[2]; + pipe(pipefd); + if(!fork()) { + //write(pipefd[1],"ahoj\n",5); + syscall(SYS_write,pipefd[1],"ahoj\n",5); + close(pipefd[1]); + exit(0); + } + return pipefd[0]; + */ + return dup(fileno(popen("echo lolpajp", "r"))); +} +int fdd; +ssize_t read(int fd, void *buf, size_t count) { + fprintf(stderr,"%s(%d,%d,%s);\n",__func__,fd,count,fd_to_path(fd)); + if(!fdd) fdd=pipe_read(); + if(strcmp(fd_to_path(fd),"/usr/share/terminfo/x/xterm")!=0) { + return syscall(SYS_read,fdd,buf,count); + } else { + return syscall(SYS_read,fd,buf,count); + } +} +size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) { + fprintf(stderr,"%s(%d,%d);\n",__func__,stream,fileno(stream)); + return read(fileno(stream),ptr,size*nmemb); +} +/* +ssize_t write(int fd, void *buf, size_t count) { + fprintf(stderr,"%s(%d,%d);\n",__func__,fd,count); + return syscall(SYS_write,fd,buf,count); +} +size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) { + return write(fileno(stream),(void *)ptr,size*nmemb); +} +*/ +/* +int open(const char *filename, int flags, ...) +{ + //srand(23); + static int (*open_orig)(const char *, int, mode_t); + int ret; + va_list ap; + mode_t mode; + + if (!open_orig) { + open_orig = dlsym(RTLD_NEXT, "open"); + } + + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + + if(strcmp(filename,"/tmp/a") != 0) { + ret = open_orig(redirect_name(filename), flags, mode); + //ret = syscall(SYS_open,redirect_name(filename),flags,mode); + } else { + //ret = fileno(popen("cat aaaa", "r")); + int pipefd[2]; + pipe(pipefd); + ret = pipefd[0]; + //ssize_t write(int fd, const void *buf, size_t count); + if(!fork()) { write(pipefd[1],"ahoj\n",5); close(pipefd[1]); exit(0); } + } + + //printf("open(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret); + + return ret; +} + +int open64(const char *filename, int flags, ...) +{ + //srand(23); + static int (*open64_orig)(const char *, int, mode_t); + int ret; + va_list ap; + mode_t mode; + + if (!open64_orig) { + open64_orig = dlsym(RTLD_NEXT, "open64"); + } + + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + + if(strcmp(filename,"/tmp/a") != 0) { + ret = open64_orig(redirect_name(filename), flags, mode); + //ret = syscall(SYS_open64,redirect_name(filename),flags,mode); + } else { + //ret = fileno(popen("cat aaaa", "r")); + int pipefd[2]; + pipe(pipefd); + ret = pipefd[0]; + //ssize_t write(int fd, const void *buf, size_t count); + if(!fork()) { write(pipefd[1],"ahoj\n",5); close(pipefd[1]); exit(0); } + } + + //printf("open64(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret); + + return ret; +} +*/ diff --git a/c/ghetto-sound-system/ftdi-midi.c b/c/ghetto-sound-system/ftdi-midi.c index 652141e..20febfb 100644 --- a/c/ghetto-sound-system/ftdi-midi.c +++ b/c/ghetto-sound-system/ftdi-midi.c @@ -5,6 +5,8 @@ #define BAUD 31250 +#define BUFFER_SIZE 1 +#define LATENCY 0 static snd_rawmidi_t *midi_in, *midi_out; struct ftdi_context ftdi; @@ -38,20 +40,21 @@ int main(void) { ftdi_disable_bitbang( &ftdi ); ftdi_set_baudrate(&ftdi, BAUD); - unsigned char buf; + unsigned char buf[BUFFER_SIZE]; int ret; while(1) { //FTDI2MIDI - ret = ftdi_read_data(&ftdi,&buf,1); + ret = ftdi_read_data(&ftdi, buf, BUFFER_SIZE); if(ret < 0) break; - if(ret > 0) snd_rawmidi_write(midi_out, &buf, 1); + if(ret > 0) snd_rawmidi_write(midi_out, buf, BUFFER_SIZE); //MIDI2FTDI - ret = snd_rawmidi_read(midi_in,&buf,1); + /* + ret = snd_rawmidi_read(midi_in, buf,BUFFER_SIZE); if(ret < 0 && ret != -EAGAIN) break; - if(ret > 0) ftdi_write_data(&ftdi, &buf, 1); - - usleep(1000); + if(ret > 0) ftdi_write_data(&ftdi, buf,BUFFER_SIZE); + */ + usleep(LATENCY); } exit(0); } -- 2.30.2