Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strcat overflow?

Could someone explain why is my strcat doing this?

I can't seem to find out why I am rewriting on a part of the source string.

The output is like this: New String: HelloThis shall be after my backslash 0 in str1h 0 in str1

    global  strcat
            extern  strlen

strcat:

    push    ebp
    mov     ebp, esp
    push    ecx
    push    esi
    push    edi
    push    ebx
    push    edx
    xor     edx, edx
    xor     edi, edi
    xor     ebx, ebx
    xor     esi, esi
    xor     ecx, ecx
    mov     edi, [ebp + 8]
    mov     esi, [ebp + 12]
    push    edi
    call    strlen
    pop     edi
    mov     ecx, eax
    xor     eax, eax
    push    esi
    call    strlen
    pop     esi
    mov     ebx, eax
    xor     eax, eax
    cmp     [edi + ecx], byte 0b
    je      PUT_LINE
    jmp     FINALIZE_END

PUT_LINE:

    cmp     ebx, eax
    je      END
    mov     dl, [esi + eax]
    mov     [edi + ecx], dl
    xor     edx, edx
    inc     eax
    inc     ecx
    jmp     PUT_LINE

END:

    mov     eax, [ebp + 8]
    jmp     FINALIZE_END

FINALIZE_END:

    pop     edx
    pop     ebx
    pop     edi
    pop     esi
    pop     ecx
    mov     esp, ebp
    pop     ebp
    ret

~

~

int     main(int argc, char** argv)
{
        (void)argc;
        (void)argv;
        char*   str1;   
        char*   str2;

        str1 = strdup("Hello");

        str2 = strdup("This shall be after my backslash 0 in str1");
        printf("New String : %s\n", strcat(str1, str2));
        return (0);
}

~

like image 937
Abdellah IDRISSI Avatar asked Aug 11 '26 05:08

Abdellah IDRISSI


1 Answers

strcat() appends the characters from one string to another string. The target string is modified. So strcat(str1, str2) modifies str1 to also contain the contents of str2.

Since not enough memory is allocated for str1 to contain the characters from both strings, this leads to an overflow.

like image 127
sth Avatar answered Aug 14 '26 07:08

sth