comparison src/testdir/test_substitute.vim @ 33811:06219b3bdaf3 v9.0.2121

patch 9.0.2121: [security]: use-after-free in ex_substitute Commit: https://github.com/vim/vim/commit/26c11c56888d01e298cd8044caf860f3c26f57bb Author: Christian Brabandt <cb@256bit.org> Date: Wed Nov 22 21:26:41 2023 +0100 patch 9.0.2121: [security]: use-after-free in ex_substitute Problem: [security]: use-after-free in ex_substitute Solution: always allocate memory closes: #13552 A recursive :substitute command could cause a heap-use-after free in Vim (CVE-2023-48706). The whole reproducible test is a bit tricky, I can only reproduce this reliably when no previous substitution command has been used yet (which is the reason, the test needs to run as first one in the test_substitute.vim file) and as a combination of the `:~` command together with a :s command that contains the special substitution atom `~\=` which will make use of a sub-replace special atom and calls a vim script function. There was a comment in the existing :s code, that already makes the `sub` variable allocate memory so that a recursive :s call won't be able to cause any issues here, so this was known as a potential problem already. But for the current test-case that one does not work, because the substitution does not start with `\=` but with `~\=` (and since there does not yet exist a previous substitution atom, Vim will simply increment the `sub` pointer (which then was not allocated dynamically) and later one happily use a sub-replace special expression (which could then free the `sub` var). The following commit fixes this, by making the sub var always using allocated memory, which also means we need to free the pointer whenever we leave the function. Since sub is now always an allocated variable, we also do no longer need the sub_copy variable anymore, since this one was used to indicated when sub pointed to allocated memory (and had therefore to be freed on exit) and when not. Github Security Advisory: https://github.com/vim/vim/security/advisories/GHSA-c8qm-x72m-q53q Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Wed, 22 Nov 2023 22:15:05 +0100
parents 9503dc55b5ed
children
comparison
equal deleted inserted replaced
33810:bb31f05f6b57 33811:06219b3bdaf3
1 " Tests for the substitute (:s) command 1 " Tests for the substitute (:s) command
2 2
3 source shared.vim 3 source shared.vim
4 source check.vim 4 source check.vim
5 source screendump.vim 5 source screendump.vim
6
7 " NOTE: This needs to be the first test to be
8 " run in the file, since it depends on
9 " that the previous substitution atom
10 " was not yet set.
11 "
12 " recursive call of :s and sub-replace special
13 " (did cause heap-use-after free in < v9.0.2121)
14 func Test_aaaa_substitute_expr_recursive_special()
15 func R()
16 " FIXME: leaving out the 'n' flag leaks memory, why?
17 %s/./\='.'/gn
18 endfunc
19 new Xfoobar_UAF
20 put ='abcdef'
21 let bufnr = bufnr('%')
22 try
23 silent! :s/./~\=R()/0
24 "call assert_fails(':s/./~\=R()/0', 'E939:')
25 let @/='.'
26 ~g
27 catch /^Vim\%((\a\+)\)\=:E565:/
28 endtry
29 delfunc R
30 exe bufnr .. "bw!"
31 endfunc
6 32
7 func Test_multiline_subst() 33 func Test_multiline_subst()
8 enew! 34 enew!
9 call append(0, ["1 aa", 35 call append(0, ["1 aa",
10 \ "bb", 36 \ "bb",
145 split Xsubfile 171 split Xsubfile
146 s/^/x 172 s/^/x
147 call feedkeys("Qsc\<CR>y", 'tx') 173 call feedkeys("Qsc\<CR>y", 'tx')
148 bwipe! 174 bwipe!
149 endfunc 175 endfunc
150
151 " Test %s/\n// which is implemented as a special case to use a 176 " Test %s/\n// which is implemented as a special case to use a
152 " more efficient join rather than doing a regular substitution. 177 " more efficient join rather than doing a regular substitution.
153 func Test_substitute_join() 178 func Test_substitute_join()
154 new 179 new
155 180
1445 wincmd x 1470 wincmd x
1446 return 'XXXX' 1471 return 'XXXX'
1447 endfunc 1472 endfunc
1448 new Xfoobar 1473 new Xfoobar
1449 let bufnr = bufnr('%') 1474 let bufnr = bufnr('%')
1450 put ="abcdef" 1475 put ='abcdef'
1451 silent! s/\%')/\=R() 1476 silent! s/\%')/\=R()
1452 call assert_fails(':%s/./\=R()/g', 'E565:') 1477 call assert_fails(':%s/./\=R()/g', 'E565:')
1453 delfunc R 1478 delfunc R
1454 exe bufnr .. "bw!" 1479 exe bufnr .. "bw!"
1455 endfunc 1480 endfunc
1456 1481
1482 " recursive call of :s using test-replace special
1483 func Test_substitute_expr_recursive()
1484 func Q()
1485 %s/./\='foobar'/gn
1486 return "foobar"
1487 endfunc
1488 func R()
1489 %s/./\=Q()/g
1490 endfunc
1491 new Xfoobar_UAF
1492 let bufnr = bufnr('%')
1493 put ='abcdef'
1494 silent! s/./\=R()/g
1495 call assert_fails(':%s/./\=R()/g', 'E565:')
1496 delfunc R
1497 delfunc Q
1498 exe bufnr .. "bw!"
1499 endfunc
1500
1457 " vim: shiftwidth=2 sts=2 expandtab 1501 " vim: shiftwidth=2 sts=2 expandtab