X86 memory access segmentation error

I am learning x86 build out of curiosity. I am currently using Linux based OS with NASM assembler. I find it hard to understand why

SECTION .text

global _start

_start:

    nop
    mov ebx, 25
    mov [0xFFF], ebx

   ;Exit the program
   mov eax, 1
   mov ebx, 0
   int 0x80

will result in a segmentation error (when moving the contents of the ebx register to memory 0xFFF). I thought that creating a program in pure asm would give me unlimited access to the virtual address space of my process. Is that not so?

How would you implement something like a heap in an assembly?

+4
source share
2 answers

Linux (x86) - 4gb , . 1 - , , , . 0xfff ( ), segfault.

, . , - sys_brk. int 0x80 EAX = 45. EBX, . , , ( ). , sys_break EBX, 0. , EAX . , .

( ), , :

SECTION .data
heap_base: dd 0          ; Memory address for base of our heap

SECTION .text
global _start
_start:
    ; Use `brk` syscall to get current memory address
    ; For the bottom of our heap This can be achieved
    ; by calling brk with an address (EBX) of 0
    mov eax, 45          ; brk system call
    xor ebx, ebx         ; don't request additional space, we just want to 
                         ; get the memory address for the base of our processes heap area.
    int 0x80
    mov [heap_base], eax ; Save the heap base

    ;Now allocate some space (8192 bytes)
    mov eax, 45          ; brk system call
    mov ebx, [heap_base] ; ebx = address for base of heap
    add ebx, 0x2000      ; increase heap by 8192 bytes
    int 0x80

    ; Example usage
    mov eax, [heap_base]      ; Get pointer to the heap base
    mov dword [eax+0xFFF], 25 ; mov value 25 to DWORD at heapbase+0xFFF

    ;Exit the program
    mov eax, 1
    xor ebx, ebx
    int 0x80
+5

. , , . . ring-3, .

+1

Source: https://habr.com/ru/post/1617207/


All Articles