robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqvim-patch:9.2.0738: ml_recover() may write beyond block buffer (#40446)f3fbb3928c

master
15.3 KiB554 linesraw
1" Test :recover
2
3func SetUp()
4  edit XSetUp
5  preserve
6  let sn = swapname('')
7  let b = readblob(sn)
8  bw!
9
10  " Not all fields are written in a system-independent manner.  Detect whether
11  " the test is running on a little or big-endian system.
12  " The B0_MAGIC_LONG field may be 32-bit or 64-bit, depending on the system,
13  " even though the value stored is only 32-bits.  Therefore, need to check
14  " both the high and low 32-bits to compute these values.
15  let s:little_endian = (b[1008:1011] == 0z33323130) || (b[1012:1015] == 0z33323130)
16  let s:system_64bit = s:little_endian ? (b[1012:1015] == 0z00000000) : (b[1008:1011] == 0z00000000)
17endfunc
18
19func Test_recover_root_dir()
20  " This used to access invalid memory.
21  split Xtest
22  set dir=/
23  call assert_fails('recover', 'E305:')
24  close!
25
26  if has('win32')
27    " can write in / directory on MS-Windows
28    let &directory = 'F:\\'
29  elseif filewritable('/') == 2
30    set dir=/notexist/
31  endif
32  call assert_fails('split Xtest', 'E303:')
33
34  " No error with empty 'directory' setting.
35  set directory=
36  split XtestOK
37  close!
38
39  set dir&
40endfunc
41
42" Make a copy of the current swap file to "Xswap".
43" Return the name of the swap file.
44func CopySwapfile()
45  preserve
46  " get the name of the swap file
47  let swname = split(execute("swapname"))[0]
48  let swname = substitute(swname, '[[:blank:][:cntrl:]]*\(.\{-}\)[[:blank:][:cntrl:]]*$', '\1', '')
49  " make a copy of the swap file in Xswap
50  set binary
51  exe 'sp ' . swname
52  w! Xswap
53  set nobinary
54  return swname
55endfunc
56
57" Inserts 10000 lines with text to fill the swap file with two levels of pointer
58" blocks.  Then recovers from the swap file and checks all text is restored.
59"
60" We need about 10000 lines of 100 characters to get two levels of pointer
61" blocks.
62func Test_swap_file()
63  set directory=.
64  set fileformat=unix undolevels=-1
65  edit! Xtest
66  let text = "\tabcdefghijklmnoparstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnoparstuvwxyz0123456789"
67  let i = 1
68  let linecount = 10000
69  while i <= linecount
70    call append(i - 1, i . text)
71    let i += 1
72  endwhile
73  $delete
74
75  let swname = CopySwapfile()
76
77  new
78  only!
79  bwipe! Xtest
80  call rename('Xswap', swname)
81  recover Xtest
82  call delete(swname)
83  let linedollar = line('$')
84  call assert_equal(linecount, linedollar)
85  if linedollar < linecount
86    let linecount = linedollar
87  endif
88  let i = 1
89  while i <= linecount
90    call assert_equal(i . text, getline(i))
91    let i += 1
92  endwhile
93
94  set undolevels&
95  enew! | only
96endfunc
97
98func Test_nocatch_process_still_running()
99  let g:skipped_reason = 'test_override() is N/A'
100  return
101  " sysinfo.uptime probably only works on Linux
102  if !has('linux')
103    let g:skipped_reason = 'only works on Linux'
104    return
105  endif
106  " the GUI dialog can't be handled
107  if has('gui_running')
108    let g:skipped_reason = 'only works in the terminal'
109    return
110  endif
111
112  " don't intercept existing swap file here
113  au! SwapExists
114
115  " Edit a file and grab its swapfile.
116  edit Xswaptest
117  call setline(1, ['a', 'b', 'c'])
118  let swname = CopySwapfile()
119
120  " Forget we edited this file
121  new
122  only!
123  bwipe! Xswaptest
124
125  call rename('Xswap', swname)
126  call feedkeys('e', 'tL')
127  redir => editOutput
128  edit Xswaptest
129  redir END
130  call assert_match('E325: ATTENTION', editOutput)
131  call assert_match('file name: .*Xswaptest', editOutput)
132  call assert_match('process ID: \d* (STILL RUNNING)', editOutput)
133
134  " Forget we edited this file
135  new
136  only!
137  bwipe! Xswaptest
138
139  " pretend we rebooted
140  call test_override("uptime", 0)
141  sleep 1
142
143  call feedkeys('e', 'tL')
144  redir => editOutput
145  edit Xswaptest
146  redir END
147  call assert_match('E325: ATTENTION', editOutput)
148  call assert_notmatch('(STILL RUNNING)', editOutput)
149
150  call test_override("ALL", 0)
151  call delete(swname)
152endfunc
153
154" Test for :recover with multiple swap files
155func Test_recover_multiple_swap_files()
156  CheckUnix
157  new Xfile1
158  call setline(1, ['a', 'b', 'c'])
159  preserve
160  let b = readblob(swapname(''))
161  call writefile(b, '.Xfile1.swm', 'D')
162  call writefile(b, '.Xfile1.swn', 'D')
163  call writefile(b, '.Xfile1.swo', 'D')
164  %bw!
165  call feedkeys(":recover Xfile1\<CR>3\<CR>q", 'xt')
166  call assert_equal(['a', 'b', 'c'], getline(1, '$'))
167  " try using out-of-range number to select a swap file
168  bw!
169  call feedkeys(":recover Xfile1\<CR>4\<CR>q", 'xt')
170  call assert_equal('Xfile1', @%)
171  call assert_equal([''], getline(1, '$'))
172  bw!
173  call feedkeys(":recover Xfile1\<CR>0\<CR>q", 'xt')
174  call assert_equal('Xfile1', @%)
175  call assert_equal([''], getline(1, '$'))
176  bw!
177endfunc
178
179" Test for :recover using an empty swap file
180func Test_recover_empty_swap_file()
181  CheckUnix
182  call writefile([], '.Xfile1.swp', 'D')
183  set dir=.
184  let msg = execute('recover Xfile1')
185  call assert_match('Unable to read block 0 from .Xfile1.swp', msg)
186  call assert_equal('Xfile1', @%)
187  bw!
188
189  " make sure there are no old swap files laying around
190  for f in glob('.sw?', 0, 1)
191    call delete(f)
192  endfor
193
194  " :recover from an empty buffer
195  call assert_fails('recover', 'E305:')
196  set dir&vim
197endfunc
198
199" Test for :recover using a corrupted swap file
200" Refer to the comments in the memline.c file for the swap file headers
201" definition.
202func Test_recover_corrupted_swap_file()
203  CheckUnix
204
205  " recover using a partial swap file
206  call writefile(0z1234, '.Xfile1.swp')
207  call assert_fails('recover Xfile1', 'E295:')
208  bw!
209
210  " recover using invalid content in the swap file
211  call writefile([repeat('1', 2*1024)], '.Xfile1.swp')
212  call assert_fails('recover Xfile1', 'E307:')
213  call delete('.Xfile1.swp')
214
215  " :recover using a swap file with a corrupted header
216  edit Xfile1
217  preserve
218  let sn = swapname('')
219  let b = readblob(sn)
220  let save_b = copy(b)
221  bw!
222
223  " clear the B0_MAGIC_LONG field
224  if s:system_64bit
225    let b[1008:1015] = 0z00000000.00000000
226  else
227    let b[1008:1011] = 0z00000000
228  endif
229  call writefile(b, sn)
230  let msg = execute('recover Xfile1')
231  call assert_match('the file has been damaged', msg)
232  call assert_equal('Xfile1', @%)
233  call assert_equal([''], getline(1, '$'))
234  bw!
235
236  " reduce the page size
237  let b = copy(save_b)
238  let b[12:15] = 0z00010000
239  call writefile(b, sn)
240  let msg = execute('recover Xfile1')
241  call assert_match('page size is smaller than minimum value', msg)
242  call assert_equal('Xfile1', @%)
243  call assert_equal([''], getline(1, '$'))
244  bw!
245
246  " clear the pointer ID
247  let b = copy(save_b)
248  let b[4096:4097] = 0z0000
249  call writefile(b, sn)
250  call assert_fails('recover Xfile1', 'E310:')
251  call assert_equal('Xfile1', @%)
252  call assert_equal([''], getline(1, '$'))
253  bw!
254
255  " set the number of pointers in a pointer block to zero
256  let b = copy(save_b)
257  let b[4098:4099] = 0z0000
258  call writefile(b, sn)
259  call assert_fails('recover Xfile1', 'E312:')
260  call assert_equal('Xfile1', @%)
261  call assert_equal(['???EMPTY BLOCK'], getline(1, '$'))
262  bw!
263
264  " set the number of pointers in a pointer block to a large value
265  let b = copy(save_b)
266  let b[4098:4099] = 0zFFFF
267  call writefile(b, sn)
268  call assert_fails('recover Xfile1', 'E1364:')
269  call assert_equal('Xfile1', @%)
270  bw!
271
272  " set the block number in a pointer entry to a negative number
273  let b = copy(save_b)
274  if v:true  " Nvim changed this field from a long to an int64_t
275    let b[4104:4111] = s:little_endian ? 0z00000000.00000080 : 0z80000000.00000000
276  else
277    let b[4104:4107] = s:little_endian ? 0z00000080 : 0z80000000
278  endif
279  call writefile(b, sn)
280  call assert_fails('recover Xfile1', 'E312:')
281  call assert_equal('Xfile1', @%)
282  call assert_equal(['???LINES MISSING'], getline(1, '$'))
283  bw!
284
285  " clear the data block ID
286  let b = copy(save_b)
287  let b[8192:8193] = 0z0000
288  call writefile(b, sn)
289  call assert_fails('recover Xfile1', 'E312:')
290  call assert_equal('Xfile1', @%)
291  call assert_equal(['???BLOCK MISSING'], getline(1, '$'))
292  bw!
293
294  " set the number of lines in the data block to zero
295  let b = copy(save_b)
296  if s:system_64bit
297    let b[8208:8215] = 0z00000000.00000000
298  else
299    let b[8208:8211] = 0z00000000
300  endif
301  call writefile(b, sn)
302  call assert_fails('recover Xfile1', 'E312:')
303  call assert_equal('Xfile1', @%)
304  call assert_equal(['??? from here until ???END lines may have been inserted/deleted',
305        \ '???END'], getline(1, '$'))
306  bw!
307
308  " set the number of lines in the data block to a large value
309  let b = copy(save_b)
310  if s:system_64bit
311    let b[8208:8215] = 0z00FFFFFF.FFFFFF00
312  else
313    let b[8208:8211] = 0z00FFFF00
314  endif
315  call writefile(b, sn)
316  call assert_fails('recover Xfile1', 'E312:')
317  call assert_equal('Xfile1', @%)
318  call assert_equal(['??? from here until ???END lines may have been inserted/deleted',
319        \ '', '???', '??? lines may be missing',
320        \ '???END'], getline(1, '$'))
321  bw!
322
323  " use an invalid text start for the lines in a data block
324  let b = copy(save_b)
325  if s:system_64bit
326    let b[8216:8219] = 0z00000000
327  else
328    let b[8212:8215] = 0z00000000
329  endif
330  call writefile(b, sn)
331  call assert_fails('recover Xfile1', 'E312:')
332  call assert_equal('Xfile1', @%)
333  call assert_equal(['???'], getline(1, '$'))
334  bw!
335
336  " use an incorrect text end (db_txt_end) for the data block
337  let b = copy(save_b)
338  let b[8204:8207] = s:little_endian ? 0z80000000 : 0z00000080
339  call writefile(b, sn)
340  call assert_fails('recover Xfile1', 'E312:')
341  call assert_equal('Xfile1', @%)
342  call assert_equal(['??? from here until ???END lines may be messed up', '',
343        \ '???END'], getline(1, '$'))
344  bw!
345
346  " remove the data block
347  let b = copy(save_b)
348  call writefile(b[:8191], sn)
349  call assert_fails('recover Xfile1', 'E312:')
350  call assert_equal('Xfile1', @%)
351  call assert_equal(['???MANY LINES MISSING'], getline(1, '$'))
352  bw!
353
354  " use an out-of-bounds db_txt_start in the data block (would cause OOB
355  " read past dp->db_index[] in ml_recover() without the bounds check)
356  let b = copy(save_b)
357  let b[8200:8203] = s:little_endian ? 0zFEFFFFFF : 0zFFFFFFFE
358  if s:system_64bit
359    let b[8208:8215] = 0z00FFFFFF.FFFFFF00
360  else
361    let b[8208:8211] = 0z00FFFF00
362  endif
363  call writefile(b, sn)
364  call assert_fails('recover Xfile1', 'E312:')
365  call assert_equal('Xfile1', @%)
366  call assert_match('??? block header corrupted', getline(1))
367  bw!
368  call delete(sn)
369endfunc
370
371" Test for :recover using an encrypted swap file
372func Test_recover_encrypted_swap_file()
373  CheckFeature cryptv
374  CheckUnix
375
376  " Recover an encrypted file from the swap file without the original file
377  new Xfile1
378  call feedkeys(":X\<CR>vim\<CR>vim\<CR>", 'xt')
379  call setline(1, ['aaa', 'bbb', 'ccc'])
380  preserve
381  let b = readblob('.Xfile1.swp')
382  call writefile(b, '.Xfile1.swm')
383  bw!
384  call feedkeys(":recover Xfile1\<CR>vim\<CR>\<CR>", 'xt')
385  call assert_equal(['aaa', 'bbb', 'ccc'], getline(1, '$'))
386  bw!
387  call delete('.Xfile1.swm')
388
389  " Recover an encrypted file from the swap file with the original file
390  new Xfile1
391  call feedkeys(":X\<CR>vim\<CR>vim\<CR>", 'xt')
392  call setline(1, ['aaa', 'bbb', 'ccc'])
393  update
394  call setline(1, ['111', '222', '333'])
395  preserve
396  let b = readblob('.Xfile1.swp')
397  call writefile(b, '.Xfile1.swm')
398  bw!
399  call feedkeys(":recover Xfile1\<CR>vim\<CR>\<CR>", 'xt')
400  call assert_equal(['111', '222', '333'], getline(1, '$'))
401  call assert_true(&modified)
402  bw!
403  call delete('.Xfile1.swm')
404  call delete('Xfile1')
405endfunc
406
407" Test for :recover using an unreadable swap file
408func Test_recover_unreadable_swap_file()
409  CheckUnix
410  CheckNotRoot
411  new Xfile1
412  let b = readblob('.Xfile1.swp')
413  call writefile(b, '.Xfile1.swm', 'D')
414  bw!
415  call setfperm('.Xfile1.swm', '-w-------')
416  call assert_fails('recover Xfile1', 'E306:')
417endfunc
418
419" Test for using :recover when the original file and the swap file have the
420" same contents.
421func Test_recover_unmodified_file()
422  CheckUnix
423  call writefile(['aaa', 'bbb', 'ccc'], 'Xfile1')
424  edit Xfile1
425  preserve
426  let b = readblob('.Xfile1.swp')
427  %bw!
428  call writefile(b, '.Xfile1.swz', 'D')
429  let msg = execute('recover Xfile1')
430  call assert_equal(['aaa', 'bbb', 'ccc'], getline(1, '$'))
431  call assert_false(&modified)
432  call assert_match('Buffer contents equals file contents', msg)
433  bw!
434  call delete('Xfile1')
435endfunc
436
437" Test for recovering a file when editing a symbolically linked file
438func Test_recover_symbolic_link()
439  CheckUnix
440  call writefile(['aaa', 'bbb', 'ccc'], 'Xfile1', 'D')
441  silent !ln -s Xfile1 Xfile2
442  edit Xfile2
443  call assert_equal('.Xfile1.swp', fnamemodify(swapname(''), ':t'))
444  preserve
445  let b = readblob('.Xfile1.swp')
446  %bw!
447  call writefile([], 'Xfile1')
448  call writefile(b, '.Xfile1.swp')
449  silent! recover Xfile2
450  call assert_equal(['aaa', 'bbb', 'ccc'], getline(1, '$'))
451  call assert_true(&modified)
452  update
453  %bw!
454  call assert_equal(['aaa', 'bbb', 'ccc'], readfile('Xfile1'))
455  call delete('Xfile2')
456  call delete('.Xfile1.swp')
457endfunc
458
459" Test for recovering a file when an autocmd moves the cursor to an invalid
460" line. This used to result in an internal error (E315) which is fixed
461" by 8.2.2966.
462func Test_recover_invalid_cursor_pos()
463  call writefile([], 'Xfile1', 'D')
464  edit Xfile1
465  preserve
466  let b = readblob('.Xfile1.swp')
467  bw!
468  augroup Test
469    au!
470    au BufReadPost Xfile1 normal! 3G
471  augroup END
472  call writefile(range(1, 3), 'Xfile1')
473  call writefile(b, '.Xfile1.swp', 'D')
474  try
475    recover Xfile1
476  catch /E308:/
477    " this test is for the :E315 internal error.
478    " ignore the 'E308: Original file may have been changed' error
479  endtry
480  redraw!
481  augroup Test
482    au!
483  augroup END
484  augroup! Test
485endfunc
486
487" Test for recovering a buffer without a name
488func Test_noname_buffer()
489  new
490  call setline(1, ['one', 'two'])
491  preserve
492  let sn = swapname('')
493  let b = readblob(sn)
494  bw!
495  call writefile(b, sn, 'D')
496  exe "recover " .. sn
497  call assert_equal(['one', 'two'], getline(1, '$'))
498endfunc
499
500" Test for recovering a corrupted swap file, those caused a crash
501func Test_recover_corrupted_swap_file1()
502  CheckUnix
503  " only works correctly on 64bit Unix systems:
504  if !(s:system_64bit && s:little_endian && has('unix'))
505    throw 'Skipped: Corrupt Swap file sample requires a little-endian 64bit Unix build'
506  endif
507  " Test 1: Heap buffer-overflow
508  new
509  let sample = 'samples/recover-crash1.swp'
510  let target = '.Xpoc1.swp'  " Xpoc1.swp (non-hidden) doesn't work in Nvim
511  " In an out-of-source-tree build the sample may be a symlink, this copies the
512  " data into a real file.
513  call writefile(readblob(sample), target, 'D')
514  try
515    sil recover! Xpoc1
516  catch /^Vim\%((\S\+)\)\=:E1364:/
517  endtry
518  let content = getline(1, '$')->join()
519  call assert_match('???ILLEGAL BLOCK NUMBER', content)
520  call delete(target)
521  bw!
522
523  " Test 2: Segfault
524  new
525  let sample = 'samples/recover-crash2.swp'
526  let target = '.Xpoc2.swp'  " Xpoc1.swp (non-hidden) doesn't work in Nvim
527  " In an out-of-source-tree build the sample may be a symlink, this copies the
528  " data into a real file.
529  call writefile(readblob(sample), target, 'D')
530  try
531    sil recover! Xpoc2
532  catch /^Vim\%((\S\+)\)\=:E1364:/
533  endtry
534  let content = getline(1, '$')->join()
535  call assert_match('???ILLEGAL BLOCK NUMBER', content)
536  call assert_match('???LINES MISSING', content)
537  call delete(target)
538  bw!
539
540  " Test 3: wrong page_count header
541  new
542  let sample = 'samples/recover-mismatch-pc.swp'
543  let target = 'Xmismatch-pc.swp'
544  call writefile(readblob(sample), target, 'bD')
545  try
546    sil noa recover! Xmismatch-pc.swp
547  catch
548  endtry
549  " Verifies no crash occurs. The OOB write is only reliably triggered
550  " interactively due to memory pressure evicting blocks in the test runner.
551  bw!
552endfunc
553
554" vim: shiftwidth=2 sts=2 expandtab