CVE-2023-5344: Heap-based Buffer Overflow (CWE-122)
value: Heap-based Buffer Overflow in GitHub repository vim/vim prior to 9.0.1969.
CWE description: A heap overflow condition is a buffer overflow, where the buffer that can be overwritten is allocated in the heap portion of memory, generally meaning that the buffer was allocated using a routine such as malloc().
CVSS v3 base score: 7.5
Published: 2023-10-02T20:15Z
Repository: https://github.com/vim/vim
Language: C
Files changed: src/message.c, src/testdir/test_crash.vim, src/version.c
Commit date: 2023-10-02 20:59:47 +0200
Commit message: patch 9.0.1969: [security] buffer-overflow in trunc_string()

Problem:  buffer-overflow in trunc_string()
Solution: Add NULL at end of buffer

Currently trunc_string() assumes that when the string is too long,
buf[e-1] will always be writeable. But that assumption may not always be
true. The condition currently looks like this

    else if (e + 3 < buflen)
    [...]
    else
    {
	// can't fit in the "...", just truncate it
	buf[e - 1] = NUL;
    }

but this means, we may run into the last else clause with e still being
larger than buflen. So a buffer overflow occurs.

So instead of using `buf[e - 1]`, let's just always
truncate at `buf[buflen - 1]` which should always be writable.

Signed-off-by: Christian Brabandt <cb@256bit.org>

Diff: diff --git a/src/message.c b/src/message.c
index 2fc6cefa9ddf..83c8b4f4a05f 100644
--- a/src/message.c
+++ b/src/message.c
@@ -351,11 +351,11 @@ trunc_string(
 	buf[e + 3 + len - 1] = NUL;
     }
     else
     {
 	// can't fit in the "...", just truncate it
-	buf[e - 1] = NUL;
+	buf[buflen - 1] = NUL;
     }
 }
 
 /*
  * Automatic prototype generation does not understand this function.
diff --git a/src/testdir/test_crash.vim b/src/testdir/test_crash.vim
index a1da68ed4c7a..5c83e3a2f5ff 100644
--- a/src/testdir/test_crash.vim
+++ b/src/testdir/test_crash.vim
@@ -64,10 +64,16 @@ func Test_crash1()
   let args = printf(cmn_args, vim, file)
   call term_sendkeys(buf, args ..
     \ '  && echo "crash 7: [OK]" >> X_crash1_result.txt' .. "\<cr>")
   call TermWait(buf, 3000)
 
+  let file = 'crash/vim_msg_trunc_poc'
+  let args = printf(cmn_args, vim, file)
+  call term_sendkeys(buf, args ..
+    \ '  || echo "crash 8: [OK]" >> X_crash1_result.txt' .. "\<cr>")
+  call TermWait(buf, 3000)
+
   " clean up
   exe buf .. "bw!"
 
   sp X_crash1_result.txt
 
@@ -77,10 +83,11 @@ func Test_crash1()
       \ 'crash 3: [OK]',
       \ 'crash 4: [OK]',
       \ 'crash 5: [OK]',
       \ 'crash 6: [OK]',
       \ 'crash 7: [OK]',
+      \ 'crash 8: [OK]',
       \ ]
 
   call assert_equal(expected, getline(1, '$'))
   bw!
 
diff --git a/src/version.c b/src/version.c
index 1be14627684c..3056f35d7fe7 100644
--- a/src/version.c
+++ b/src/version.c
@@ -702,10 +702,12 @@ static char *(features[]) =
 	NULL
 };
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1969,
 /**/
     1968,
 /**/
     1967,
 /**/

Vulnerable code: 	buf[e - 1] = NUL;
Fixed code: 	buf[buflen - 1] = NUL;
  let file = 'crash/vim_msg_trunc_poc'
  let args = printf(cmn_args, vim, file)
  call term_sendkeys(buf, args ..
    \ '  || echo "crash 8: [OK]" >> X_crash1_result.txt' .. "\<cr>")
  call TermWait(buf, 3000)

      \ 'crash 8: [OK]',
/**/
    1969,