view src/testdir/test_shortpathname.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 ae10b91ac6b3
children
line wrap: on
line source

" Test for shortpathname ':8' extension.
" Only for use on Win32 systems!

set encoding=utf-8
scriptencoding utf-8

source check.vim
CheckMSWindows

func TestIt(file, bits, expected)
  let res = fnamemodify(a:file, a:bits)
  if a:expected != ''
    call assert_equal(substitute(a:expected, '/', '\\', 'g'),
		\ substitute(res, '/', '\\', 'g'),
		\ "'" . a:file . "'->(" . a:bits . ")->'" . res . "'")
  endif
endfunc

func Test_ColonEight()
  let save_dir = getcwd()

  " This could change for CygWin to //cygdrive/c .
  let dir1 = 'c:/x.x.y'
  let trycount = 5
  while 1
    if !filereadable(dir1) && !isdirectory(dir1)
      break
    endif
    if trycount == 1
      call assert_report("Fatal: '" . dir1 . "' exists, cannot run this test")
      return
    endif
    " When tests run in parallel the directory may exist, wait a bit until it
    " is gone.
    sleep 5
    let trycount -= 1
  endwhile

  let file1 = dir1 . '/zz.y.txt'
  let nofile1 = dir1 . '/z.y.txt'
  let dir2 = dir1 . '/VimIsTheGreatestSinceSlicedBread'
  let file2 = dir2 . '/z.txt'
  let nofile2 = dir2 . '/zz.txt'

  call mkdir(dir1, 'D')
  let resdir1 = substitute(fnamemodify(dir1, ':p:8'), '/$', '', '')
  call assert_match('\V\^c:/XX\x\x\x\x~1.Y\$', resdir1)

  let resfile1 = resdir1 . '/ZZY~1.TXT'
  let resnofile1 = resdir1 . '/z.y.txt'
  let resdir2 = resdir1 . '/VIMIST~1'
  let resfile2 = resdir2 . '/z.txt'
  let resnofile2 = resdir2 . '/zz.txt'

  call mkdir(dir2, 'D')
  call writefile([], file1, 'D')
  call writefile([], file2, 'D')

  call TestIt(file1, ':p:8', resfile1)
  call TestIt(nofile1, ':p:8', resnofile1)
  call TestIt(file2, ':p:8', resfile2)
  call TestIt(nofile2, ':p:8', resnofile2)
  call TestIt(nofile2, ':p:8:h', fnamemodify(resnofile2, ':h'))
  call chdir(dir1)
  call TestIt(file1, ':.:8', strpart(resfile1, strlen(resdir1)+1))
  call TestIt(nofile1, ':.:8', strpart(resnofile1, strlen(resdir1)+1))
  call TestIt(file2, ':.:8', strpart(resfile2, strlen(resdir1)+1))
  call TestIt(nofile2, ':.:8', strpart(resnofile2, strlen(resdir1)+1))
  let $HOME=dir1
  call TestIt(file1, ':~:8', '~' . strpart(resfile1, strlen(resdir1)))
  call TestIt(nofile1, ':~:8', '~' . strpart(resnofile1, strlen(resdir1)))
  call TestIt(file2, ':~:8', '~' . strpart(resfile2, strlen(resdir1)))
  call TestIt(nofile2, ':~:8', '~' . strpart(resnofile2, strlen(resdir1)))

  cd c:/

  call chdir(save_dir)
endfunc

func Test_ColonEight_MultiByte()
  let dir = 'Xtest'

  let file = dir . '/日本語のファイル.txt'

  call mkdir(dir, 'D')
  call writefile([], file, 'D')

  let sfile = fnamemodify(file, ':8')

  call assert_notequal(file, sfile)
  call assert_match('\~', sfile)
endfunc

func Test_ColonEight_notexists()
  let non_exists='C:\windows\newfile.txt'
  call assert_equal(non_exists, fnamemodify(non_exists, ':p:8'))
endfunc

" vim: shiftwidth=2 sts=2 expandtab