]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.2035: [security] use-after-free with wildmenu v9.0.2035
authorYee Cheng Chin <ychin.git@gmail.com>
Tue, 17 Oct 2023 08:06:56 +0000 (10:06 +0200)
committerChristian Brabandt <cb@256bit.org>
Tue, 17 Oct 2023 08:06:56 +0000 (10:06 +0200)
Problem:  [security] use-after-free with wildmenu
Solution: properly clean up the wildmenu when exiting

Fix wildchar/wildmenu/pum memory corruption with special wildchar's

Currently, using `wildchar=<Esc>` or `wildchar=<C-\>` can lead to a
memory corruption if using wildmenu+pum, or wrong states if only using
wildmenu. This is due to the code only using one single place inside the
cmdline process loop to perform wild menu clean up (by checking
`end_wildmenu`) but there are other odd situations where the loop could
have exited and we need a post-loop clean up just to be sure. If the
clean up was not done you would have a stale popup menu referring to
invalid memory, or if not using popup menu, incorrect status line (if
`laststatus=0`).

For example, if you hit `<Esc>` two times when it's wildchar, there's a
hard-coded behavior to exit command-line as a failsafe for user, and if
you hit `<C-\><C-\><C-N>` it will also exit command-line, but the clean
up code would not have hit because of specialized `<C-\>` handling.

Fix Ctrl-E / Ctrl-Y to not cancel/accept wildmenu if they are also
used for 'wildchar'/'wildcharm'. Currently they don't behave properly,
and also have potentially memory unsafe behavior as the logic is
currently not accounting for this situation and try to do both.
(Previous patch that addressed this: #11677)

Also, correctly document Escape key behavior (double-hit it to escape)
in wildchar docs as it's previously undocumented.

In addition, block known invalid chars to be set in `wildchar` option,
such as Ctrl-C and `<CR>`. This is just to make it clear to the user
they shouldn't be set, and is not required for this bug fix.

closes: #13361

Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yee Cheng Chin <ychin.git@gmail.com>
13 files changed:
runtime/doc/options.txt
src/ex_getln.c
src/option.c
src/optiondefs.h
src/proto/option.pro
src/testdir/dumps/Test_wildmenu_5.dump [new file with mode: 0644]
src/testdir/dumps/Test_wildmenu_6.dump [new file with mode: 0644]
src/testdir/dumps/Test_wildmenu_pum_odd_wildchar_1.dump [moved from src/testdir/dumps/Test_wildmenu_pum_clear_entries_1.dump with 100% similarity]
src/testdir/dumps/Test_wildmenu_pum_odd_wildchar_2.dump [new file with mode: 0644]
src/testdir/dumps/Test_wildmenu_pum_odd_wildchar_3.dump [new file with mode: 0644]
src/testdir/test_cmdline.vim
src/testdir/test_options.vim
src/version.c

index 6f38bc02bedbe852b3f1e2eb288d6bd81e09117f..86a734f6e6dadcb24b04d8d0d7b33cffc42074db 100644 (file)
@@ -9253,6 +9253,8 @@ A jump table for the options with a short description can be found at |Q_op|.
        The character is not recognized when used inside a macro.  See
        'wildcharm' for that.
        Some keys will not work, such as CTRL-C, <CR> and Enter.
+       <Esc> can be used, but hitting it twice in a row will still exit
+       command-line as a failsafe measure.
        Although 'wc' is a number option, you can set it to a special key: >
                :set wc=<Tab>
 <      NOTE: This option is set to the Vi default value when 'compatible' is
index 5baffa77fb162f44fdb8b29e7dfa3c3b18d76326..711e08e8867f16c70cf3c83623be304fdcc02623 100644 (file)
@@ -1877,7 +1877,8 @@ getcmdline_int(
        if (p_wmnu)
            c = wildmenu_translate_key(&ccline, c, &xpc, did_wild_list);
 
-       if (cmdline_pum_active())
+       int key_is_wc = (c == p_wc && KeyTyped) || c == p_wcm;
+       if (cmdline_pum_active() && !key_is_wc)
        {
            // Ctrl-Y: Accept the current selection and close the popup menu.
            // Ctrl-E: cancel the cmdline popup menu and return the original
@@ -1897,7 +1898,7 @@ getcmdline_int(
        // 'wildcharm' or Ctrl-N or Ctrl-P or Ctrl-A or Ctrl-L).
        // If the popup menu is displayed, then PageDown and PageUp keys are
        // also used to navigate the menu.
-       end_wildmenu = (!(c == p_wc && KeyTyped) && c != p_wcm
+       end_wildmenu = (!key_is_wc
                && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A && c != Ctrl_L);
        end_wildmenu = end_wildmenu && (!cmdline_pum_active() ||
                            (c != K_PAGEDOWN && c != K_PAGEUP
@@ -2504,6 +2505,15 @@ returncmd:
     cmdmsg_rl = FALSE;
 #endif
 
+    // We could have reached here without having a chance to clean up wild menu
+    // if certain special keys like <Esc> or <C-\> were used as wildchar. Make
+    // sure to still clean up to avoid memory corruption.
+    if (cmdline_pum_active())
+       cmdline_pum_remove();
+    wildmenu_cleanup(&ccline);
+    did_wild_list = FALSE;
+    wim_index = 0;
+
     ExpandCleanup(&xpc);
     ccline.xpc = NULL;
 
index 53e956c8b125963bfaf349706abf17d00dff7784..180aca221d0d2050a1511d9b37c6f0e4ac9bdc83 100644 (file)
@@ -4392,6 +4392,21 @@ did_set_weirdinvert(optset_T *args)
     return NULL;
 }
 
+/*
+ * Process the new 'wildchar' / 'wildcharm' option value.
+ */
+    char *
+did_set_wildchar(optset_T *args)
+{
+    long c = *(long *)args->os_varp;
+
+    // Don't allow key values that wouldn't work as wildchar.
+    if (c == Ctrl_C || c == '\n' || c == '\r' || c == K_KENTER)
+       return e_invalid_argument;
+
+    return NULL;
+}
+
 /*
  * Process the new 'window' option value.
  */
index c4810e7d5d201235aa3e1a3c2a91cda235e12a8f..d5887e1d41edcf3b700279ecb02fd48059504ad2 100644 (file)
@@ -2812,11 +2812,11 @@ static struct vimoption options[] =
                            (char_u *)&p_ww, PV_NONE, did_set_whichwrap, expand_set_whichwrap,
                            {(char_u *)"", (char_u *)"b,s"} SCTX_INIT},
     {"wildchar",    "wc",   P_NUM|P_VIM,
-                           (char_u *)&p_wc, PV_NONE, NULL, NULL,
+                           (char_u *)&p_wc, PV_NONE, did_set_wildchar, NULL,
                            {(char_u *)(long)Ctrl_E, (char_u *)(long)TAB}
                            SCTX_INIT},
     {"wildcharm",   "wcm",  P_NUM|P_VI_DEF,
-                           (char_u *)&p_wcm, PV_NONE, NULL, NULL,
+                           (char_u *)&p_wcm, PV_NONE, did_set_wildchar, NULL,
                            {(char_u *)0L, (char_u *)0L} SCTX_INIT},
     {"wildignore",  "wig",  P_STRING|P_VI_DEF|P_ONECOMMA|P_NODUP,
                            (char_u *)&p_wig, PV_NONE, NULL, NULL,
index ec0013dbfa5c48629fedbf07f5fad67be1f5002c..effa81315b176919953d7721c20e7f79fcd865f7 100644 (file)
@@ -81,6 +81,7 @@ char *did_set_undofile(optset_T *args);
 char *did_set_undolevels(optset_T *args);
 char *did_set_updatecount(optset_T *args);
 char *did_set_weirdinvert(optset_T *args);
+char *did_set_wildchar(optset_T *args);
 char *did_set_window(optset_T *args);
 char *did_set_winheight_helpheight(optset_T *args);
 char *did_set_winminheight(optset_T *args);
diff --git a/src/testdir/dumps/Test_wildmenu_5.dump b/src/testdir/dumps/Test_wildmenu_5.dump
new file mode 100644 (file)
index 0000000..ca9c5be
--- /dev/null
@@ -0,0 +1,8 @@
+| +0&#ffffff0@74
+|~+0#4040ff13&| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|:+0#0000000&|v|i|m| > @69
diff --git a/src/testdir/dumps/Test_wildmenu_6.dump b/src/testdir/dumps/Test_wildmenu_6.dump
new file mode 100644 (file)
index 0000000..6b7ef83
--- /dev/null
@@ -0,0 +1,8 @@
+> +0&#ffffff0@74
+|~+0#4040ff13&| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+| +0#0000000&@56|0|,|0|-|1| @8|A|l@1| 
diff --git a/src/testdir/dumps/Test_wildmenu_pum_odd_wildchar_2.dump b/src/testdir/dumps/Test_wildmenu_pum_odd_wildchar_2.dump
new file mode 100644 (file)
index 0000000..2887895
--- /dev/null
@@ -0,0 +1,10 @@
+| +0#0000001#ffd7ff255|!| @14| +0#0000000#0000001| +0&#ffffff0@56
+| +0#0000001#e0e0e08|#| @14| +0#0000000#a8a8a8255| +0#4040ff13#ffffff0@56
+| +0#0000001#ffd7ff255|&| @14| +0#0000000#a8a8a8255| +0#4040ff13#ffffff0@56
+| +0#0000001#ffd7ff255|*| @14| +0#0000000#a8a8a8255| +0#4040ff13#ffffff0@56
+| +0#0000001#ffd7ff255|+@1| @13| +0#0000000#a8a8a8255| +0#4040ff13#ffffff0@56
+| +0#0000001#ffd7ff255|-@1| @13| +0#0000000#a8a8a8255| +0#4040ff13#ffffff0@56
+| +0#0000001#ffd7ff255|<| @14| +0#0000000#a8a8a8255| +0#4040ff13#ffffff0@56
+| +0#0000001#ffd7ff255|=| @14| +0#0000000#a8a8a8255| +0#4040ff13#ffffff0@56
+| +0#0000001#ffd7ff255|>| @14| +0#0000000#a8a8a8255| +0#4040ff13#ffffff0@56
+|:+0#0000000&|#> @72
diff --git a/src/testdir/dumps/Test_wildmenu_pum_odd_wildchar_3.dump b/src/testdir/dumps/Test_wildmenu_pum_odd_wildchar_3.dump
new file mode 100644 (file)
index 0000000..270a233
--- /dev/null
@@ -0,0 +1,10 @@
+> +0&#ffffff0@74
+|~+0#4040ff13&| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+| +0#0000000&@56|0|,|0|-|1| @8|A|l@1| 
index ddfeba28ff878bae3c34a6ed9c972174ff2aeda6..c285b087a4afb9b54b595d2501a2a671aad27e7d 100644 (file)
@@ -171,6 +171,7 @@ func Test_wildmenu_screendump()
   [SCRIPT]
   call writefile(lines, 'XTest_wildmenu', 'D')
 
+  " Test simple wildmenu
   let buf = RunVimInTerminal('-S XTest_wildmenu', {'rows': 8})
   call term_sendkeys(buf, ":vim\<Tab>")
   call VerifyScreenDump(buf, 'Test_wildmenu_1', {})
@@ -181,9 +182,23 @@ func Test_wildmenu_screendump()
   call term_sendkeys(buf, "\<Tab>")
   call VerifyScreenDump(buf, 'Test_wildmenu_3', {})
 
+  " Looped back to the original value
   call term_sendkeys(buf, "\<Tab>\<Tab>")
   call VerifyScreenDump(buf, 'Test_wildmenu_4', {})
+
+  " Test that the wild menu is cleared properly
+  call term_sendkeys(buf, " ")
+  call VerifyScreenDump(buf, 'Test_wildmenu_5', {})
+
+  " Test that a different wildchar still works
+  call term_sendkeys(buf, "\<Esc>:set wildchar=<Esc>\<CR>")
+  call term_sendkeys(buf, ":vim\<Esc>")
+  call VerifyScreenDump(buf, 'Test_wildmenu_1', {})
+
+  " Double-<Esc> is a hard-coded method to escape while wildchar=<Esc>. Make
+  " sure clean up is properly done in edge case like this.
   call term_sendkeys(buf, "\<Esc>")
+  call VerifyScreenDump(buf, 'Test_wildmenu_6', {})
 
   " clean up
   call StopVimInTerminal(buf)
@@ -2636,10 +2651,11 @@ func Test_wildmenu_pum_from_terminal()
   call StopVimInTerminal(buf)
 endfunc
 
-func Test_wildmenu_pum_clear_entries()
+func Test_wildmenu_pum_odd_wildchar()
   CheckRunVimInTerminal
 
-  " This was using freed memory.  Run in a terminal to get the pum to update.
+  " Test odd wildchar interactions with pum. Make sure they behave properly
+  " and don't lead to memory corruption due to improperly cleaned up memory.
   let lines =<< trim END
     set wildoptions=pum
     set wildchar=<C-E>
@@ -2647,10 +2663,35 @@ func Test_wildmenu_pum_clear_entries()
   call writefile(lines, 'XwildmenuTest', 'D')
   let buf = RunVimInTerminal('-S XwildmenuTest', #{rows: 10})
 
-  call term_sendkeys(buf, ":\<C-E>\<C-E>")
-  call VerifyScreenDump(buf, 'Test_wildmenu_pum_clear_entries_1', {})
+  call term_sendkeys(buf, ":\<C-E>")
+  call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_1', {})
+
+  " <C-E> being a wildchar takes priority over its original functionality
+  call term_sendkeys(buf, "\<C-E>")
+  call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_2', {})
+
+  call term_sendkeys(buf, "\<Esc>")
+  call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_3', {})
 
-  set wildoptions& wildchar&
+  " Escape key can be wildchar too. Double-<Esc> is hard-coded to escape
+  " command-line, and we need to make sure to clean up properly.
+  call term_sendkeys(buf, ":set wildchar=<Esc>\<CR>")
+  call term_sendkeys(buf, ":\<Esc>")
+  call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_1', {})
+
+  call term_sendkeys(buf, "\<Esc>")
+  call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_3', {})
+
+  " <C-\> can also be wildchar. <C-\><C-N> however will still escape cmdline
+  " and we again need to make sure we clean up properly.
+  call term_sendkeys(buf, ":set wildchar=<C-\\>\<CR>")
+  call term_sendkeys(buf, ":\<C-\>\<C-\>")
+  call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_1', {})
+
+  call term_sendkeys(buf, "\<C-N>")
+  call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_3', {})
+
+  call StopVimInTerminal(buf)
 endfunc
 
 " Test for completion after a :substitute command followed by a pipe (|)
index ba0808956da0455147bfd008bdb6bd372e1d5170..6b2b7482a7e560e1ea90d21cde9f5d0e30bd2336 100644 (file)
@@ -228,6 +228,11 @@ func Test_keymap_valid()
   call assert_fails(":set kmp=trunc\x00name", "trunc")
 endfunc
 
+func Test_wildchar_valid()
+  call assert_fails("set wildchar=<CR>", "E474:")
+  call assert_fails("set wildcharm=<C-C>", "E474:")
+endfunc
+
 func Check_dir_option(name)
   " Check that it's possible to set the option.
   exe 'set ' . a:name . '=/usr/share/dict/words'
index 695b0cd56ef61ee183f8a8ef32761523e60e9382..6f39f950f802a371d6a4ba43d325501e2d1dd1a1 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2035,
 /**/
     2034,
 /**/