http://www.tjd.phlegethon.org/software/linux/kpoke.c

/*
 *  Hack to frob a value in kernel space, when you can't be 
 *  bothered using sysctl.
 *
 *  Copyright (c) 2002 Tim Deegan <[EMAIL PROTECTED]>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id: kpoke.c,v 1.2 2002/03/26 12:14:46 tjd21 Exp $
 */

#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

int main (int argc, char **argv)
{
    int       fd;
    uintptr_t addr, value;
    char     *ap, *vp;

    errno = 0;
    if (argc != 3) {
	fprintf (stderr, "%s: sets a (%i-byte) word of kernel memory\n", 
		 argv[0], sizeof (value));
	fprintf (stderr, "usage: %s <address> <value>\n", argv[0]);
	exit(-1);
    }
    addr = strtoul(argv[1], &ap, 0);
    value = strtoul(argv[2], &vp, 0);
    if (errno != 0 || ap == argv[1] || vp == argv[2]) {
	fprintf (stderr, "usage: %s <address> <value>\n", argv[0]);
	exit(-1);
    }

    fd = open("/dev/kmem", O_RDWR);
    if (fd == -1) {
	perror ("open() failed:"); 
	exit (-1);
    }
    
    if (lseek (fd, addr, SEEK_SET) == -1) {
	perror ("lseek() failed:");
	exit (-1);	
    }
    if (write (fd, &value, sizeof(value)) == -1) {
	perror ("write() failed:");
	exit (-1);
    }
    if (lseek (fd, addr, SEEK_SET) == -1) {
	perror ("lseek() failed:");
	exit (-1);	
    }
    if (read (fd, &value, sizeof(value)) != sizeof (value)) {
	perror ("read() failed:");
	exit (-1);
    }
    
    printf ("%#x: %#x\n", addr, value);
    exit(0);
}

/*
 *  EOF
 */

Reply via email to