From 6967c08840bedfecc54884af815b75ff7ab7af7b Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 3 Aug 2024 08:13:20 +0800 Subject: [PATCH] vim-patch:9.1.0648: [security] double-free in dialog_changed() Problem: [security] double-free in dialog_changed() (SuyueGuo) Solution: Only clear pointer b_sfname pointer, if it is different than the b_ffname pointer. Don't try to free b_fname, set it to NULL instead. fixes: vim/vim#15403 Github Advisory: https://github.com/vim/vim/security/advisories/GHSA-46pw-v7qw-xc2f https://github.com/vim/vim/commit/b29f4abcd4b3382fa746edd1d0562b7b48c9de60 Co-authored-by: Christian Brabandt --- src/nvim/ex_cmds2.c | 7 +++++-- test/functional/legacy/crash_spec.lua | 12 ++++++++++++ test/old/testdir/crash/dialog_changed_uaf | Bin 0 -> 552 bytes test/old/testdir/test_crash.vim | 6 ++++++ 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 test/old/testdir/crash/dialog_changed_uaf diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index a602719f6d..b62bb8b42a 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -227,9 +227,12 @@ void dialog_changed(buf_T *buf, bool checkall) // restore to empty when write failed if (empty_bufname) { - XFREE_CLEAR(buf->b_fname); + // prevent double free + if (buf->b_sfname != buf->b_ffname) { + XFREE_CLEAR(buf->b_sfname); + } + buf->b_fname = NULL; XFREE_CLEAR(buf->b_ffname); - XFREE_CLEAR(buf->b_sfname); unchanged(buf, true, false); } } else if (ret == VIM_NO) { diff --git a/test/functional/legacy/crash_spec.lua b/test/functional/legacy/crash_spec.lua index e72c3a512a..b63b3146f4 100644 --- a/test/functional/legacy/crash_spec.lua +++ b/test/functional/legacy/crash_spec.lua @@ -8,6 +8,7 @@ local eq = t.eq local eval = n.eval local exec = n.exec local feed = n.feed +local pcall_err = t.pcall_err before_each(clear) @@ -51,3 +52,14 @@ it('no crash when closing window with tag in loclist', function() eq(0, eval('bufexists(g:qf_bufnr)')) assert_alive() end) + +it('no crash when writing "Untitled" file fails', function() + t.mkdir('Untitled') + finally(function() + vim.uv.fs_rmdir('Untitled') + end) + feed('ifoobar') + command('set bufhidden=unload') + eq('Vim(enew):E502: "Untitled" is a directory', pcall_err(command, 'confirm enew')) + assert_alive() +end) diff --git a/test/old/testdir/crash/dialog_changed_uaf b/test/old/testdir/crash/dialog_changed_uaf new file mode 100644 index 0000000000000000000000000000000000000000..e37d18d6daca107697bd19934b34054aa09e48a4 GIT binary patch literal 552 zcmYc+($>~iD7MN^NMK}0&O73%gaWL>A%NlkCv`6EkO;24{G!~%90@}|D}PS09D#fZ zF~{ys9dHSAYKv{{H?xT<)QsPVNZtoMf)@Vy>KG1`tqS zVBjh>bmrt_G&j%7%gf5ki_s`aOe*FwOK?>xvgS(749w%o%V$X8VruyRU%igYG%PHv zR8fJE!50L!W^%Fq|IfgHuuSf#{h6#s9? zGiGS0%rDB7D$&<+)HTfE=V#*Q%J)go)m3mgWR{Si#KpjsRIH_y%B7_=Jw-v0k%2*> zIPaiRo3ttL@iiVz^3kySPffi?ed1kpuW=U#Z zep-G}wyH`|3|Ao+!@vLk|MPxiU`VYfC{kcxNMwlN%Ihk^7v?z zMrI}#