L. Landgraf
L. Landgraf

Reputation: 31

Why does the shell run remaining input when the char *buf is larger than size_t count for a read system call?

I ran into a little bug or unexpected behaviour in x86-64 assembly on my Linux machine.

In the .bss section I'm reserving 16 bytes in memory which I call name

When the user enters their name, it will be output back onto the screen with a hello message. However when the user types in something that is larger than 16 characters, the rest of the characters will be immediately run in the console. The issue

Is this what people refer to as a buffer overflow? But why is the code after 16 characters being run in the first place? I thought that the application would crash or simply not work. Where is the rest of the message being saved to and how does it happen that my shell which is zsh runs especially that code?

I'm sorry if these questions sound silly, but i just started yesterday with assembly language and im trying to understand what the linux kernel is doing.


The full source code for my program is this:

section .data
    question1   db "What is your name? "
    greeting    db "Hello, "

section .bss
    name        resb 16     ; Reserve 16 bytes in memory for "name"

section .text
    global _start           ; define at which label (address) the main program routine should start

_start:
    call _printQuestion
    call _getName
    call _printGreeting
    call _printName
    
    ; Define the next syscall to end the program
    mov     rax, 60         ; ID for sys_exit syscall
    mov     rdi, 0          ; rdi = errorcode value. 0 = No error
    syscall             ; run the syscall to exit

_getName:
    mov     rax, 0          ; Syscall 0 for sys_read
    mov rdi, 0          ; Standard Input
    mov rsi, name       
    mov rdx, 16
    syscall
    ret

_printQuestion:
    mov     rax, 1
    mov rdi, 1
    mov rsi, question1
    mov rdx, 19
    syscall
    ret

_printGreeting:
    mov     rax, 1
    mov rdi, 1
    mov rsi, greeting
    mov     rdx, 7
    syscall
    ret

_printName:
    mov rax, 1
    mov rdi, 1
    mov rsi, name
    mov rdx, 16
    syscall
    ret

I knew that the program would do unexpected things, but I wasn't expecting this and i want to understand if this is a common issue and how to fix it.

Upvotes: 3

Views: 60

Answers (1)

Barmar
Barmar

Reputation: 782166

This is happening because your program doesn't necessarily read an entire line of input. If the user enters a line longer than 16 characters, you read the first 16 of them. The rest stays in the terminal driver's buffer, and it will be returned in the next call to read(). When your program exits, the shell calls read() to get the next command line, this will return the remainder of the input line, and the shell then executes it.

Upvotes: 5

Related Questions