aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2023-11-23 07:11:16 +0800
committerzeertzjq <zeertzjq@outlook.com>2023-11-23 16:17:37 +0800
commit8d8136bfcf9ea6d0b23638b48e99e09bdc8a9d44 (patch)
treeaa87c5a2c7f915ac97844444b310655af5965995
parented8f9ff47bfaff99f121014c1c17ee222b4f7226 (diff)
downloadrneovim-8d8136bfcf9ea6d0b23638b48e99e09bdc8a9d44.tar.gz
rneovim-8d8136bfcf9ea6d0b23638b48e99e09bdc8a9d44.tar.bz2
rneovim-8d8136bfcf9ea6d0b23638b48e99e09bdc8a9d44.zip
vim-patch:9.0.2121: [security]: use-after-free in ex_substitute
Problem: [security]: use-after-free in ex_substitute Solution: always allocate memory closes: vim/vim#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 https://github.com/vim/vim/commit/26c11c56888d01e298cd8044caf860f3c26f57bb Co-authored-by: Christian Brabandt <cb@256bit.org>
-rw-r--r--src/nvim/ex_cmds.c30
-rw-r--r--test/old/testdir/test_substitute.vim48
2 files changed, 64 insertions, 14 deletions
diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c
index e3abb14481..c406364491 100644
--- a/src/nvim/ex_cmds.c
+++ b/src/nvim/ex_cmds.c
@@ -3367,8 +3367,9 @@ static int do_sub(exarg_T *eap, const proftime_T timeout, const int cmdpreview_n
// Small incompatibility: vi sees '\n' as end of the command, but in
// Vim we want to use '\n' to find/substitute a NUL.
- sub = cmd; // remember the start of the substitution
+ char *p = cmd; // remember the start of the substitution
cmd = skip_substitute(cmd, delimiter);
+ sub = xstrdup(p);
if (!eap->skip && cmdpreview_ns <= 0) {
sub_set_replacement((SubReplacementString) {
@@ -3383,7 +3384,7 @@ static int do_sub(exarg_T *eap, const proftime_T timeout, const int cmdpreview_n
return 0;
}
pat = NULL; // search_regcomp() will use previous pattern
- sub = old_sub.sub;
+ sub = xstrdup(old_sub.sub);
// Vi compatibility quirk: repeating with ":s" keeps the cursor in the
// last column after using "$".
@@ -3391,6 +3392,7 @@ static int do_sub(exarg_T *eap, const proftime_T timeout, const int cmdpreview_n
}
if (sub != NULL && sub_joining_lines(eap, pat, sub, cmd, cmdpreview_ns <= 0)) {
+ xfree(sub);
return 0;
}
@@ -3405,11 +3407,13 @@ static int do_sub(exarg_T *eap, const proftime_T timeout, const int cmdpreview_n
i = getdigits_int(&cmd, true, INT_MAX);
if (i <= 0 && !eap->skip && subflags.do_error) {
emsg(_(e_zerocount));
+ xfree(sub);
return 0;
} else if (i >= INT_MAX) {
char buf[20];
vim_snprintf(buf, sizeof(buf), "%d", i);
semsg(_(e_val_too_large), buf);
+ xfree(sub);
return 0;
}
eap->line1 = eap->line2;
@@ -3425,17 +3429,20 @@ static int do_sub(exarg_T *eap, const proftime_T timeout, const int cmdpreview_n
eap->nextcmd = check_nextcmd(cmd);
if (eap->nextcmd == NULL) {
semsg(_(e_trailing_arg), cmd);
+ xfree(sub);
return 0;
}
}
if (eap->skip) { // not executing commands, only parsing
+ xfree(sub);
return 0;
}
if (!subflags.do_count && !MODIFIABLE(curbuf)) {
// Substitution is not allowed in non-'modifiable' buffer
emsg(_(e_modifiable));
+ xfree(sub);
return 0;
}
@@ -3444,6 +3451,7 @@ static int do_sub(exarg_T *eap, const proftime_T timeout, const int cmdpreview_n
if (subflags.do_error) {
emsg(_(e_invcmd));
}
+ xfree(sub);
return 0;
}
@@ -3458,22 +3466,20 @@ static int do_sub(exarg_T *eap, const proftime_T timeout, const int cmdpreview_n
assert(sub != NULL);
- char *sub_copy = NULL;
-
// If the substitute pattern starts with "\=" then it's an expression.
// Make a copy, a recursive function may free it.
// Otherwise, '~' in the substitute pattern is replaced with the old
// pattern. We do it here once to avoid it to be replaced over and over
// again.
if (sub[0] == '\\' && sub[1] == '=') {
- sub = xstrdup(sub);
- sub_copy = sub;
+ char *p = xstrdup(sub);
+ xfree(sub);
+ sub = p;
} else {
- char *newsub = regtilde(sub, magic_isset(), cmdpreview_ns > 0);
- if (newsub != sub) {
- // newsub was allocated, free it later.
- sub_copy = newsub;
- sub = newsub;
+ char *p = regtilde(sub, magic_isset(), cmdpreview_ns > 0);
+ if (p != sub) {
+ xfree(sub);
+ sub = p;
}
}
@@ -4244,7 +4250,7 @@ skip:
}
vim_regfree(regmatch.regprog);
- xfree(sub_copy);
+ xfree(sub);
// Restore the flag values, they can be used for ":&&".
subflags.do_all = save_do_all;
diff --git a/test/old/testdir/test_substitute.vim b/test/old/testdir/test_substitute.vim
index 75062f13aa..a9e317da02 100644
--- a/test/old/testdir/test_substitute.vim
+++ b/test/old/testdir/test_substitute.vim
@@ -4,6 +4,32 @@ source shared.vim
source check.vim
source screendump.vim
+" NOTE: This needs to be the first test to be
+" run in the file, since it depends on
+" that the previous substitution atom
+" was not yet set.
+"
+" recursive call of :s and sub-replace special
+" (did cause heap-use-after free in < v9.0.2121)
+func Test_aaaa_substitute_expr_recursive_special()
+ func R()
+ " FIXME: leaving out the 'n' flag leaks memory, why?
+ %s/./\='.'/gn
+ endfunc
+ new Xfoobar_UAF
+ put ='abcdef'
+ let bufnr = bufnr('%')
+ try
+ silent! :s/./~\=R()/0
+ "call assert_fails(':s/./~\=R()/0', 'E939:')
+ let @/='.'
+ ~g
+ catch /^Vim\%((\a\+)\)\=:E565:/
+ endtry
+ delfunc R
+ exe bufnr .. "bw!"
+endfunc
+
func Test_multiline_subst()
enew!
call append(0, ["1 aa",
@@ -147,7 +173,6 @@ func Test_substitute_repeat()
call feedkeys("Qsc\<CR>y", 'tx')
bwipe!
endfunc
-
" Test %s/\n// which is implemented as a special case to use a
" more efficient join rather than doing a regular substitution.
func Test_substitute_join()
@@ -1448,11 +1473,30 @@ func Test_substitute_expr_switch_win()
endfunc
new Xfoobar
let bufnr = bufnr('%')
- put ="abcdef"
+ put ='abcdef'
silent! s/\%')/\=R()
call assert_fails(':%s/./\=R()/g', 'E565:')
delfunc R
exe bufnr .. "bw!"
endfunc
+" recursive call of :s using test-replace special
+func Test_substitute_expr_recursive()
+ func Q()
+ %s/./\='foobar'/gn
+ return "foobar"
+ endfunc
+ func R()
+ %s/./\=Q()/g
+ endfunc
+ new Xfoobar_UAF
+ let bufnr = bufnr('%')
+ put ='abcdef'
+ silent! s/./\=R()/g
+ call assert_fails(':%s/./\=R()/g', 'E565:')
+ delfunc R
+ delfunc Q
+ exe bufnr .. "bw!"
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab