vim-patch:9.0.2106: [security]: Use-after-free in win_close()

Problem:  [security]: Use-after-free in win_close()
Solution: Check window is valid, before accessing it

If the current window structure is no longer valid (because a previous
autocommand has already freed this window), fail and return before
attempting to set win->w_closing variable.

Add a test to trigger ASAN in CI

25aabc2b8e

Co-authored-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
zeertzjq
2023-11-17 09:47:04 +08:00
parent d49be1cd28
commit 790bd4d585
7 changed files with 36 additions and 0 deletions

View File

@@ -2661,6 +2661,9 @@ int win_close(win_T *win, bool free_buf, bool force)
reset_VIsual_and_resel(); // stop Visual mode
other_buffer = true;
if (!win_valid(win)) {
return FAIL;
}
win->w_closing = true;
apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, false, curbuf);
if (!win_valid(win)) {

BIN
test/old/testdir/crash/poc1 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -110,6 +110,39 @@ func Test_crash1()
call delete('X_crash1_result.txt')
endfunc
func Test_crash1_2()
CheckNotBSD
CheckExecutable dash
" The following used to crash Vim
let opts = #{cmd: 'sh'}
let vim = GetVimProg()
let result = 'X_crash1_1_result.txt'
let buf = RunVimInTerminal('sh', opts)
let file = 'crash/poc1'
let cmn_args = "%s -u NONE -i NONE -n -e -s -S %s -c ':qa!'"
let args = printf(cmn_args, vim, file)
call term_sendkeys(buf, args ..
\ ' && echo "crash 1: [OK]" > '.. result .. "\<cr>")
call TermWait(buf, 150)
" clean up
exe buf .. "bw!"
exe "sp " .. result
let expected = [
\ 'crash 1: [OK]',
\ ]
call assert_equal(expected, getline(1, '$'))
bw!
call delete(result)
endfunc
func Test_crash2()
" The following used to crash Vim
let opts = #{wait_for_ruler: 0, rows: 20}