2014-12-20 6 views
0

mprotect()을 사용하고 텍스트 세그먼트를 쓰기 가능하게 만들려면 elf의 텍스트 세그먼트가 들어있는 첫 번째 페이지의 주소를 정적으로 계산해야합니다.주소의 정적 페이지 계산

Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al .. [14] .text PROGBITS 08048380 000380 0002e0 00 AX 0 0 128

어떤 아이디어가?

답변

1

정상적으로 컴파일되고 충돌하지 않는이 프로그램은 어떻습니까?

#include <stdio.h> 
#include <stdlib.h> 
#include <stdint.h> 
#include <unistd.h> 
#include <sys/mman.h> 

extern char __executable_start; 
extern char __etext; 

int 
main (int argc, char **argv) 
{ 
    int pagesize = sysconf (_SC_PAGE_SIZE); 
    char *start = 
    (char *) (((uintptr_t) & __executable_start) & ~(pagesize - 1)); 
    char *end = 
    (char *) (((uintptr_t) & __etext + pagesize - 1) & ~(pagesize - 1)); 
    mprotect (start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC); 
    printf ("Hello world\n"); 
    void *m = main; 
    *((char *) m) = 0; 
    exit (0); 
} 

내가 __executable_start__etext 사용했지만, 당신은 적어도 맨 페이지에 설명되어있는 이러한 작업을 얻을 수 있다면 당신이보고 더 좋을 수 있습니다

NAME

`etext`, `edata`, `end` - end of program segments 

개요

extern etext; 
    extern edata; 
    extern end; 

설명

The addresses of these symbols indicate the end of various program segments: 

    `etext` This is the first address past the end of the text segment (the program 
      code). 

    `edata` This is the first address past the end of the initialized data segment. 

    `end` This is the first address past the end of the uninitialized data 
      segment (also known as the BSS segment). 

Although these symbols have long been provided on most UNIX systems, they are 
    not standardized; use with caution. 
에 부합