vim-patch:9.1.1361: [security]: possible use-after-free when closing a buffer (#33820)

Problem:  [security]: Possible to open more windows into a closing
          buffer without splitting, bypassing existing "b_locked_split"
          checks and triggering use-after-free
Solution: Disallow switching to a closing buffer. Editing a closing
          buffer (via ":edit", etc.) was fixed in v9.1.0764, but add an
          error message and check just "b_locked_split", as "b_locked"
          is necessary only when the buffer shouldn't be wiped, and may
          be set for buffers that are in-use but not actually closing.
          (Sean Dewar)

closes: vim/vim#17246

6cb1c82840
(cherry picked from commit 627c648252)
This commit is contained in:
Sean Dewar
2025-05-04 03:15:51 +01:00
committed by github-actions[bot]
parent bdd8498ed7
commit 9965cfb84c
7 changed files with 56 additions and 16 deletions

View File

@@ -4174,7 +4174,8 @@ func Test_autocmd_BufWinLeave_with_vsp()
exe "e " fname
vsp
augroup testing
exe "au BufWinLeave " .. fname .. " :e " dummy .. "| vsp " .. fname
exe 'au BufWinLeave' fname 'e' dummy
\ '| call assert_fails(''vsp' fname ''', ''E1546:'')'
augroup END
bw
call CleanUpTestAuGroup()

View File

@@ -563,4 +563,39 @@ func Test_buflist_alloc_failure()
call assert_fails('cexpr "XallocFail6:10:Line10"', 'E342:')
endfunc
func Test_closed_buffer_still_in_window()
%bw!
let s:w = win_getid()
new
let s:b = bufnr()
setl bufhidden=wipe
augroup ViewClosedBuffer
autocmd!
autocmd BufUnload * ++once call assert_fails(
\ 'call win_execute(s:w, "' .. s:b .. 'b")', 'E1546:')
augroup END
quit!
" Previously resulted in s:b being curbuf while unloaded (no memfile).
call assert_equal(1, bufloaded(bufnr()))
call assert_equal(0, bufexists(s:b))
let s:w = win_getid()
split
new
let s:b = bufnr()
augroup ViewClosedBuffer
autocmd!
autocmd BufWipeout * ++once call win_gotoid(s:w)
\| call assert_fails(s:b .. 'b', 'E1546:') | wincmd p
augroup END
bw! " Close only this buffer first; used to be a heap UAF.
unlet! s:w s:b
autocmd! ViewClosedBuffer
%bw!
endfunc
" vim: shiftwidth=2 sts=2 expandtab