Headline
CVE-2021-33388: Heap-based Buffer Overflow in the makevar() function (#8) · Issues · Dwight Aplevich / dpic · GitLab
dpic 2021.04.10 has a Heap Buffer Overflow in themakevar() function in dpic.y
Skip to content
Heap-based Buffer Overflow in the makevar() function
Hi,
I found a heap buffer overflow write of size 1 in the makevar() function, in dpic.y on dpic 2021.04.10 (commit: d317e406)
Heap buffer overflow issue: for loop in https://gitlab.com/aplevich/dpic/-/blob/master/dpic.y#L5769 as given below:
void
makevar(char *s, int ln, double varval)
{
nametype *vn, *lastvar, *namptr;
int j, tstval;
for (j = 0; j < ln; j++) { chbuf[chbufi + j] = s[j]; }
...
And, as per the code, https://gitlab.com/aplevich/dpic/-/blob/master/main.c#L1777
chbuf = malloc (sizeof (chbufarray)); if (chbuf==NULL){ fatal(9); } the sizeof chbufarray of char datatype is: https://gitlab.com/aplevich/dpic/-/blob/master/dpic.h#L78
typedef Char chbufarray[CHBUFSIZ + 1];
and upper limit of chbuf buffers is CHBUFSIZ which is of 4095 limit so sizeof (chbufarray) becomes 4096 and while running the executable, the value of chbufi can be seen as 4089 for this test file, so it means any value which is greater than 6 will cause heap buffer overflow write in the makevar() function for loop, in this case.
if ln (second input parameter of makevar()) is 7:
chbuf[4089 + 6] = s[6];
chbuf[4095] = s[6];
if ln is greater than 7 like 8 or 9:
chbuf[4089 + 7] = s[6];
which is, chbuf[4096] = s[6]; // heap buffer overflow of write 1 byte
And, as per the code: https://gitlab.com/aplevich/dpic/-/blob/master/dpic.y#L5735 as given below, it can be seen as many makevar() function has second input parameter greater than 7
void
mkOptionVars(void)
{
makevar("dpicopt", 7, drawmode);
if (safemode) { i = 1; } else { i = 0; }
makevar("optsafe", 7, i);
makevar("optMFpic", 8, MFpic);
makevar("optMpost", 8, MPost);
makevar("optPDF", 6, PDF);
makevar("optPGF", 6, PGF);
makevar("optPict2e", 9, Pict2e);
makevar("optPS", 5, PS);
makevar("optPSfrag", 9, PSfrag);
makevar("optPSTricks", 11, PSTricks);
makevar("optSVG", 6, SVG);
makevar("optTeX", 6, TeX);
makevar("opttTeX", 7, tTeX);
makevar("optxfig", 7, xfig);
...
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.2 LTS"
Attaching a reproducer: heap_bof_write_test
the issue can be reproduced by running:
dpic heap_bof_write_test
With ASAN report
=================================================================
==582741==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x621000001100 at pc 0x000000538ae3 bp 0x7ffead55dc90 sp 0x7ffead55dc88
WRITE of size 1 at 0x621000001100 thread T0
#0 0x538ae2 in makevar /home/bsdboy/projects/again/dpic/dpic.y:5769:48
#1 0x511042 in mkOptionVars /home/bsdboy/projects/again/dpic/dpic.y:5748:5
#2 0x4de391 in yyparse /home/bsdboy/projects/again/dpic/dpic.y:495:19
#3 0x4dbcb4 in main /home/bsdboy/projects/again/dpic/main.c:1813:13
#4 0x7f9a899740b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16
#5 0x41c65d in _start (/home/bsdboy/projects/again/dpic/dpic+0x41c65d)
0x621000001100 is located 0 bytes to the right of 4096-byte region [0x621000000100,0x621000001100)
allocated by thread T0 here:
#0 0x4978bd in malloc (/home/bsdboy/projects/again/dpic/dpic+0x4978bd)
#1 0x4dbb9d in main /home/bsdboy/projects/again/dpic/main.c:1779:11
#2 0x7f9a899740b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/bsdboy/projects/again/dpic/dpic.y:5769:48 in makevar
Shadow bytes around the buggy address:
0x0c427fff81d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c427fff81e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c427fff81f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c427fff8200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c427fff8210: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c427fff8220:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c427fff8230: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c427fff8240: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c427fff8250: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c427fff8260: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c427fff8270: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
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
Shadow gap: cc
==582741==ABORTING
Edited May 09, 2021 by Neeraj Pal