stb_sprintf asan buffer overflow detect
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 52/100
Research direction
The overflow is reported at stb_sprintf.h line 409 inside stbsp_vsprintfcb, reached from stbsp_sprintf at line 1381, so start by reading how the format string is scanned there. Reproduce with the four-line program in the issue under AddressSanitizer (clang -fsanitize=address works too). Done means the global-buffer-overflow no longer appears for the format "%s %d %d" and the library's existing behaviour is unchanged.
Written by the indexing model from the issue text.
Description
I got some overflow detect with very simple test case:
stb_sprintf - v1.10
#define STB_SPRINTF_IMPLEMENTATION
#include "stb_sprintf.h"
int main()
{
char buffer[512] = {};
stbsp_sprintf(buffer, "%s %d %d", "123", 1, 2);
}
Compiled with vs2022: cl /Od /Zi /fsanitize=address test.cpp
ASAN Trace Logs
==26880==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7ff72a28fd88 at pc 0x7ff72a221949 bp 0x00b7e7f2eb80 sp 0x00b7e7f2eb80
READ of size 4 at 0x7ff72a28fd88 thread T0
#0 0x7ff72a221948 in stbsp_vsprintfcb D:\sources\my\tests\stb_printf\stb_sprintf.h:409
nothings/stb#1 0x7ff72a22156e in stbsp_sprintf D:\sources\my\tests\stb_printf\stb_sprintf.h:1381
nothings/stb#2 0x7ff72a22c0a7 in main D:\sources\my\tests\stb_printf\test.cpp:7
nothings/stb#3 0x7ff72a22e793 in invoke_main D:\a_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
nothings/stb#4 0x7ff72a22e793 in __scrt_common_main_seh D:\a_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
nothings/stb#5 0x7ffbe331e8d6 (C:\WINDOWS\System32\KERNEL32.DLL+0x18002e8d6)
nothings/stb#6 0x7ffbe419fbcb (C:\WINDOWS\SYSTEM32\ntdll.dll+0x1800dfbcb)
0x7ff72a28fd89 is located 0 bytes after global variable '<C++ string literal>' defined in 'test.cpp:7:26' (0x7ff72a28fd80) of size 9
'<C++ string literal>' is ascii string '%s %d %d'
SUMMARY: AddressSanitizer: global-buffer-overflow D:\sources\my\tests\stb_printf\stb_sprintf.h:409 in stbsp_vsprintfcb
Shadow bytes around the buggy address:
0x7ff72a28fb00: f9 f9 f9 f9 00 00 00 00 00 00 00 00 00 00 00 00
0x7ff72a28fb80: 00 f9 f9 f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00
0x7ff72a28fc00: 00 00 00 00 00 f9 f9 f9 f9 f9 f9 f9 00 00 00 00
0x7ff72a28fc80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7ff72a28fd00: f9 f9 f9 f9 04 f9 f9 f9 04 f9 f9 f9 04 f9 f9 f9
=>0x7ff72a28fd80: 00[01]f9 f9 00 00 00 00 00 00 00 00 00 00 00 00
0x7ff72a28fe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7ff72a28fe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7ff72a28ff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7ff72a28ff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7ff72a290000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==26880==ABORTING
- Dominant language
- C
- Stars
- 18
- Forks
- 5
- Avg merge
- 1m
- Merged PRs (30d)
- 3
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from nothings/stb_sprintf
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
nothings/stb_sprintf#10 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
nothings/stb_sprintf#8 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
nothings/stb_sprintf#5 ·
-
[SECURITY] stbsp_snprintf with count=0 writes null terminator before bufferPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
nothings/stb_sprintf#4 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
nothings/stb_sprintf#20 ·
All issues in nothings/stb_sprintf
Similar issues
-
bug severity: medium
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
AlessandroSangiuliano/Uros#1014 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
OpenXiangShan/NEMU#1314 ·
Maintainers usually reply within 1 day
-
`get_options()` repeats the first byte when hex-encoding octet stringsPossibly taken @rue19 claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 85/100
OpenPrinting/cups#1830 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
micropython/micropython#20020 ·
Maintainers usually reply within 1 day