Conversation
…nlarge stack buffers to 64 bytes and use safe fread bounds in FileIsX64ELF, FileIsX86ELF, FileIsX64X86ELF, FileIsShell, and FileIsPython. Prevents __fread_chk fortify aborts and stack smashing on ARM64 during execve.
|
Should fix #4382 |
|
I'm sorry, but I don't understand were is the fix? the fread change you do is ... strange. The form used in box64 is perfectly legal and correct, so please expand (and please, don't use AI to explain, it's your PR, not an AI one) |
| char head[64] = {0}; | ||
| size_t sz = fread(head, 1, 20, f); | ||
|
|
||
| char head[sizeof(x64lib)] = {0}; |
There was a problem hiding this comment.
what was wrong with the 64-bytes size of the array before, because I think that was the onyl fix there
There was a problem hiding this comment.
I thought it would be good to implement dynamic head buffer size
To reduce the number of magic numbers
| int sz = fread(head, 20, 1, f); | ||
|
|
||
| char head[sizeof(x64lib)] = {0}; | ||
| int sz = fread(head, sizeof(x64lib) - 1, 1, f); |
There was a problem hiding this comment.
x64lib contains a trailling 0 and should be kept in (it's data, not a C string)
There was a problem hiding this comment.
Compiler adds an extra 0 on string literals (without -1 it will be 21 instead of 20). Can leave as it or change to unsigned char
ex:
static const unsigned char x64lib[] = {
0x7f, 'E', 'L', 'F', 0x02, 0x01, 0x01, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0x00, 0x3e, 0x00
};
in that case sizeof is 20
| int sz = fread(head, SHELL_MAX_LEN - 1, 1, f); | ||
| fclose(f); | ||
| if(sz < strlen(shsign)) | ||
| if(sz != 1) |
There was a problem hiding this comment.
this is not the same code. sz has a differnt meaning here than in the original code.
|
I must apologize, at least for ai description and unnecessary changes. The issue persist on termux, because of patches that change system prefix, but they forgot to modify head size. I should have edited their patch, but I thought it would be good to implement dynamic head buffer size. |
Summary
Fixes an issue where
execve(insidemy_execve) crashes with*** buffer overflow detected ***or*** stack smashing detected ***: terminated(SIGABRT) when callingFileIsX64ELF,FileIsX86ELF,FileIsShell, orFileIsPython.Problem Description
In
src/utils/fileutils.c, the file-checking functions used tightly sized stack buffers:char head[20]inFileIsX64ELF,FileIsX86ELF, andFileIsShellchar head[25]inFileIsPythonThis caused two issues:
_FORTIFY_SOURCE/__fread_chkenabled,fread(head, size, 1, f)triggered:FileIsShellandFileIsPython, strings were read directly to the end of the buffer without leaving room for a trailing\0null-terminator for subsequentstrncmpcalls.Backtrace:
Changes
headbuffer size to64bytes inFileIsX64ELF,FileIsX86ELF,FileIsX64X86ELF,FileIsShell, andFileIsPython.freadcall convention fromfread(head, size, 1, f)tofread(head, 1, count, f)to prevent buffer overflows and ensure safe bounds.FileIsShellandFileIsPython, limit reading tosizeof(head) - 1to guarantee the buffer is always null-terminated.sz < expected_length) rather than strictsz != 1.Testing
-fstack-protector-strong.execvecalls withinbox64-bash(running commands and subshells) now execute cleanly without triggering fortify or stack corruption.