Re: Executing from dynamically allocated memory

Jens Kallup <jkallup@web.de>
Sun, 13 Oct 2013 10:44:09 +0200

          From comp.compilers

Related articles
Executing from dynamically allocated memory news@fx32.iad.highwinds-media.com (news) (2013-10-12)
Re: Executing from dynamically allocated memory james.harris.1@gmail.com (James Harris) (2013-10-12)
Re: Executing from dynamically allocated memory sebastien.fricker@gmail.com (=?UTF-8?B?U8OpYmFzdGllbiBGcmlja2Vy?=) (2013-10-12)
Re: Executing from dynamically allocated memory chakaram@auth.gr (2013-10-12)
Re: Executing from dynamically allocated memory blog@rivadpm.com (Alex McDonald) (2013-10-12)
Re: Executing from dynamically allocated memory sandmann@cs.au.dk (2013-10-12)
Re: Executing from dynamically allocated memory jkallup@web.de (Jens Kallup) (2013-10-13)
| List of all articles for this month |
From: Jens Kallup <jkallup@web.de>
Newsgroups: comp.compilers
Date: Sun, 13 Oct 2013 10:44:09 +0200
Organization: 1&1 Internet AG
References: 13-10-004
Keywords: code, storage
Posted-Date: 14 Oct 2013 20:02:59 EDT

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



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.