patch 9.2.0278: viminfo: heap buffer overflow when reading viminfo file

Problem:  Reading a crafted viminfo file can cause a heap buffer
          overflow because the length value from getdigits() is cast to
          int, truncating large size_t values
Solution: Remove the (int) cast when calling alloc() (sentinel404)

Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Christian Brabandt
2026-04-01 15:03:58 +00:00
parent 3e60f03d94
commit b2e55ed1d6
3 changed files with 23 additions and 1 deletions
+20
View File
@@ -1371,4 +1371,24 @@ func Test_viminfo_len_one()
let &viminfofile = _viminfofile
endfunc
func Test_viminfo_len_overflow()
let _viminfofile = &viminfofile
let &viminfofile=''
let viminfo_file = tempname()
defer delete(viminfo_file)
" Craft a viminfo entry with size_t length overflow
call writefile(['# Viminfo',
\ '|1,4', '|2,>4294967311',
\ '|<"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
\ '|<BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB',
\ '|<CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC',
\ '|<DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD'], viminfo_file, 'b')
" Should not crash or cause memory errors
exe 'rviminfo! ' .. viminfo_file
let &viminfofile = _viminfofile
endfunc
" vim: shiftwidth=2 sts=2 expandtab
+2
View File
@@ -734,6 +734,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
278,
/**/
277,
/**/
+1 -1
View File
@@ -1054,7 +1054,7 @@ barline_parse(vir_T *virp, char_u *text, garray_T *values)
// Length includes the quotes.
++p;
len = getdigits(&p);
buf = alloc((int)(len + 1));
buf = alloc(len + 1);
if (buf == NULL)
return TRUE;
p = buf;