Logo Search packages:      
Sourcecode: paxtest version File versions  Download package

anonmap.c

/* anonmap.c - Tests wether code can be executed in anonymous mappings
 *
 * Copyright (c)2003 by Peter Busser <peter@adamantix.org>
 * This file has been released under the GNU Public Licence version 2 or later
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include "body.h"

const char testname[] = "Executable anonymous mapping             ";

#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
#endif

void doit( void )
{
      char *buf;
      fptr func;

      buf = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
      if( buf == MAP_FAILED ) {
            fprintf( stderr, "mmap() returned NULL\n" );
            exit( 1 );
      }

      /* Put a RETN instruction in the buffer */
      *buf = '\xc3';

      /* Convert the pointer to a function pointer */
      func = (fptr)buf;

      /* Call the code in the buffer */
      func();

      /* It worked when the function returns */
      itworked();
}

Generated by  Doxygen 1.6.0   Back to index