Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #993

Re: Executing from dynamically allocated memory

From Jens Kallup <jkallup@web.de>
Newsgroups comp.compilers
Subject Re: Executing from dynamically allocated memory
Date 2013-10-13 10:44 +0200
Organization 1&1 Internet AG
Message-ID <13-10-010@comp.compilers> (permalink)
References <13-10-004@comp.compilers>

Show all headers | View raw


Hello,

this should be work:


#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

extern int errno;
static volatile sig_atomic_t sflag;
static sigset_t signal_neu, signal_alt, signal_leer;
void sigfunc1(int);
void sigfunc2(int);

void signale_mmap(void)
{
   if(signal(SIGSEGV, sigfunc1) == SIG_ERR)
    {
      fprintf(stderr, "Konnte signalhandler fC<r SIGSEGV nicht
einrichten\n");
      exit(0);
     }
   if(signal(SIGBUS, sigfunc2) == SIG_ERR)
     {
       fprintf(stderr, "Konnte signalhandler fC<r SIGBUS nicht
einrichten\n");
       exit(0);
     }

   sigemptyset(&signal_leer);
   sigemptyset(&signal_neu);

   sigaddset(&signal_neu, SIGSEGV);
   sigaddset(&signal_neu, SIGBUS);

   if(sigprocmask(SIG_BLOCK, &signal_neu, &signal_alt) < 0)
      exit(0);
}

void sigfunc1(int sig)
{
   printf("SIGSEGV: Versuch auf einen unerlaubten Speicherbereich zu
schreiben\n");
   exit(0);
}

void sigfunc2(int sig)
{
   printf("SIGBUS: Der Speicherbereich ist nicht mehr gC<ltig\n");
   exit(0);
}

void fun1(int v)
{
   printf("value --> 0x%x\n",v);
}

int main(int argc, char **argv)
{
   int fd,fd1;

   void (*callback1)(int);

   printf("exit-> 0x%x\n",exit);
   printf("puts-> 0x%x\n",puts);
   printf("fun1-> 0x%x\n",fun1);


   struct stat attr;

   signale_mmap();

   if((fd1=open(argv[1],O_RDONLY)) < 0)
   {
       fprintf(stderr,"%s : Konnte %s nicht
C6ffnen\n",strerror(errno),argv[2]);
       exit(0);
   }

   if(fstat(fd1, &attr) == -1)
   {
       fprintf(stderr,"Fehler bei fstat.......\n");
       exit(0);
   }

   callback1 = mmap(0, attr.st_size, PROT_READ | PROT_EXEC, MAP_SHARED,
fd1, 0);
   printf("val: 0x%x\n",callback1);

   if((callback1 == ((caddr_t) -1)) )
     {
       fprintf(stderr, "%s: Fehler bei mmap ...........\n",strerror(errno));
       exit(0);
     }
   close(fd1);

   printf("start...\n");
   callback1((int)callback1);
   printf("ende...\n");

   exit(0);
}

// the following code is compiled with nasm 32 bit

bits 32
org 0x0

;-----------------------------------
; Version 1.0
;-----------------------------------
%define FuncPuts	1
%define FuncFun1	2

segment .text
start:
	push	ebp
	mov	ebp, esp

	mov	eax, [ebp + 8]
	add	eax, LC1
	push	eax

	mov	eax,  [ebp + 8]
	call	dword [eax + func_entry + (FuncPuts * 4)]

	pop	eax

	leave
	ret

segment .data
LC1:	db "Hello You!", 10, 0


func_entry:
	dd 31102011	; version
	dd 0x4015d0	; puts

; cheers and good luck
; Jens

Back to comp.compilers | Previous | NextPrevious in thread | Find similar


Thread

Executing from dynamically allocated memory "news" <news@fx32.iad.highwinds-media.com> - 2013-10-12 00:05 +0000
  Re: Executing from dynamically allocated memory "James Harris" <james.harris.1@gmail.com> - 2013-10-12 10:10 +0100
  Re: Executing from dynamically allocated memory Sébastien Fricker <sebastien.fricker@gmail.com> - 2013-10-12 11:52 +0200
  Re: Executing from dynamically allocated memory chakaram@auth.gr - 2013-10-12 19:20 +0300
  Re: Executing from dynamically allocated memory Alex McDonald <blog@rivadpm.com> - 2013-10-12 11:12 -0700
  Re: Executing from dynamically allocated memory sandmann@cs.au.dk (Søren Sandmann) - 2013-10-12 20:58 +0200
  Re: Executing from dynamically allocated memory Jens Kallup <jkallup@web.de> - 2013-10-13 10:44 +0200

csiph-web