0

I am learning NASM and couldn't figure out why the following snippet runs without crashing.

2 bytes were reserved for sinput buffer and yet, it seems that we can write 3 bytes into it. How is it possible ?

            global      _start

            section     .text
_start:
            mov         eax, 3        ; invoke SYS_READ (opcode 3)
            mov         ebx, 0        ; write to STDIN
            mov         ecx, sinput   ; reserved space to store our input (known as a buffer)
            mov         edx, 3        ; number of bytes to read
            int         80h
            ; WHY DOESNT IT CRASH ? Yet, we wrote 3 bytes into our sinput buffer but initially reserved 2 bytes.

            mov         ebx, [ecx]
            mov         ebx, [ecx + 1]
            mov         ebx, [ecx + 2]

            mov     eax, 1            ; SYS_EXIT
            mov     ebx, 0
            int     80h

            section     .bss
sinput:     resb        2            ; reserve a 2 byte space in memory for the users input string
Ferdinand Mom
  • 59
  • 1
  • 5
  • 1
    _"How is it possible ?"_ Because the BSS section is larger than 2 bytes? Have you looked at the section headers of your executable using something like `readelf`? – Michael Mar 24 '22 at 14:52
  • Thank you for taking your time. The size of the bss section for my snippet is 4 bytes. So I tried to change the number of bytes to read to > 4 to see what happens and it seems to still work. Am i misunderstanding something ? – Ferdinand Mom Mar 24 '22 at 15:18
  • 1
    A `write` or `read` system call will never crash, just return `-EFAULT`. If anything was going to crash, it would be your first load from `[ecx]`, which loads 4 bytes. [Why didn't I get segmentation fault when storing past the end of the BSS?](https://stackoverflow.com/q/47180548) – Peter Cordes Mar 24 '22 at 15:49
  • Thank you for linking this similar SO post ! – Ferdinand Mom Mar 25 '22 at 09:45

0 Answers0