annotate src/testdir/test_cmdmods.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 5acc0d2cf4f7
children cda1a8a06777
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30986
360f286b5869 patch 9.0.0828: various typos
Bram Moolenaar <Bram@vim.org>
parents: 30174
diff changeset
1 " Test for all command modifiers in
30174
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 def Test_cmdmods_array()
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 # Get all the command modifiers from ex_cmds.h.
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 var lines = readfile('../ex_cmds.h')->filter((_, l) => l =~ 'ex_wrongmodifier,')
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6 var cmds = lines->map((_, v) => substitute(v, '.*"\(\k*\)".*', '\1', ''))
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8 # :hide is both a command and a modifier
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9 cmds->extend(['hide'])
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11 # Get the entries of cmdmods[] in ex_docmd.c
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 edit ../ex_docmd.c
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13 var top = search('^} cmdmods[') + 1
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14 var bot = search('^};') - 1
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15 lines = getline(top, bot)
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16 var mods = lines->map((_, v) => substitute(v, '.*"\(\k*\)".*', '\1', ''))
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
18 # Add the other commands that use ex_wrongmodifier.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
19 mods->extend([
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
20 'endclass',
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
21 'endenum',
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
22 'endinterface',
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
23 'public',
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
24 'static',
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
25 'this',
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
26 ])
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
27
30174
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28 # Check the lists are equal. Convert them to a dict to get a clearer error
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 # message.
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30 var cmds_dict = {}
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
31 for v in cmds
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32 cmds_dict[v] = 1
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33 endfor
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34 var mods_dict = {}
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 for v in mods
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36 mods_dict[v] = 1
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37 endfor
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 assert_equal(cmds_dict, mods_dict)
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40 bwipe!
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 enddef
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 " vim: shiftwidth=2 sts=2 expandtab
3564cddda2a5 patch 9.0.0423: "for" and "while" not recognized after :vim9cmd and :legacy
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45