Skip to content

Instantly share code, notes, and snippets.

@j0hn
Created September 18, 2011 04:47
Show Gist options
  • Save j0hn/1224739 to your computer and use it in GitHub Desktop.
Save j0hn/1224739 to your computer and use it in GitHub Desktop.
Kernelroll LD_PRELOAD version
/***********************************************************************
* KernelRoll, LD_PRELOAD version. *
* =============================== *
* *
* Compile with: gcc -o kernelroll.so kernelroll.c -shared -fPIC -ldl *
* Run with: LD_PRELOAD=./kernelroll.so some_file.mp3 *
* *
* Remember to change SONG constant in this file *
* *
* Authors: Roger Duran *
* Contributors: j0hn *
***********************************************************************/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <stdarg.h>
#include <assert.h>
#if defined(RTLD_NEXT)
#define REAL_LIBC RTLD_NEXT
#else
#define REAL_LIBC ((void*) -1L)
#endif
/* CHANG ME */
#define SONG "/path/to/roll/file"
static int lib_start = 0;
static int (*real_open)(const char *path, int flags, ...);
static int (*real_open64)(const char *path, int flags, ...);
void _start_lib()
{
if (!lib_start){
assert(real_open = dlsym(REAL_LIBC, "open"));
assert(real_open64 = dlsym(REAL_LIBC, "open64"));
lib_start++;
}
}
int _open(int type, const char *path, int flags, va_list arg)
{
char *ext = NULL;
mode_t mode;
_start_lib();
if(flags & O_CREAT) {
mode = va_arg(arg, mode_t);
}
ext = (char *)(path + strlen(path) - 4);
if(strcmp(ext, ".mp3") == 0){
if(type == 64){
return real_open64(SONG, flags, mode);
} else {
return real_open(SONG, flags, mode);
}
}
if(type == 64){
return real_open64(path, flags, mode);
} else {
return real_open(path, flags, mode);
}
}
int open(const char *path, int flags, ...)
{
int result = 0;
va_list arg;
va_start(arg, flags);
result = _open(32, path, flags, arg);
va_end(arg);
return result;
}
int open64(const char* path, int flags, ...)
{
int result = 0;
va_list arg;
va_start(arg, flags);
result = _open(64, path, flags, arg);
va_end(arg);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment