Skip to content

Commit 5a73e0c

Browse files
committed
patch 8.0.1263: others can read the swap file if a user is careless
Problem: Others can read the swap file if a user is careless with his primary group. Solution: If the group permission allows for reading but the world permissions doesn't, make sure the group is right.
1 parent 7dd88c5 commit 5a73e0c

File tree

4 files changed

+99
-40
lines changed

4 files changed

+99
-40
lines changed

Diff for: src/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,7 @@ test_arglist \
22592259
test_stat \
22602260
test_statusline \
22612261
test_substitute \
2262+
test_swap \
22622263
test_syn_attr \
22632264
test_syntax \
22642265
test_system \

Diff for: src/fileio.c

+23-1
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,29 @@ readfile(
716716
/* Set swap file protection bits after creating it. */
717717
if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
718718
&& curbuf->b_ml.ml_mfp->mf_fname != NULL)
719-
(void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode);
719+
{
720+
char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
721+
722+
/*
723+
* If the group-read bit is set but not the world-read bit, then
724+
* the group must be equal to the group of the original file. If
725+
* we can't make that happen then reset the group-read bit. This
726+
* avoids making the swap file readable to more users when the
727+
* primary group of the user is too permissive.
728+
*/
729+
if ((swap_mode & 044) == 040)
730+
{
731+
stat_T swap_st;
732+
733+
if (mch_stat((char *)swap_fname, &swap_st) >= 0
734+
&& st.st_gid != swap_st.st_gid
735+
&& fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
736+
== -1)
737+
swap_mode &= 0600;
738+
}
739+
740+
(void)mch_setperm(swap_fname, (long)swap_mode);
741+
}
720742
#endif
721743
}
722744

Diff for: src/testdir/test_swap.vim

+73-39
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,82 @@
11
" Tests for the swap feature
22

3-
" Tests for 'directory' option.
4-
func Test_swap_directory()
3+
"" Tests for 'directory' option.
4+
"func Test_swap_directory()
5+
" if !has("unix")
6+
" return
7+
" endif
8+
" let content = ['start of testfile',
9+
" \ 'line 2 Abcdefghij',
10+
" \ 'line 3 Abcdefghij',
11+
" \ 'end of testfile']
12+
" call writefile(content, 'Xtest1')
13+
"
14+
" " '.', swap file in the same directory as file
15+
" set dir=.,~
16+
"
17+
" " Verify that the swap file doesn't exist in the current directory
18+
" call assert_equal([], glob(".Xtest1*.swp", 1, 1, 1))
19+
" edit Xtest1
20+
" let swfname = split(execute("swapname"))[0]
21+
" call assert_equal([swfname], glob(swfname, 1, 1, 1))
22+
"
23+
" " './dir', swap file in a directory relative to the file
24+
" set dir=./Xtest2,.,~
25+
"
26+
" call mkdir("Xtest2")
27+
" edit Xtest1
28+
" call assert_equal([], glob(swfname, 1, 1, 1))
29+
" let swfname = "Xtest2/Xtest1.swp"
30+
" call assert_equal(swfname, split(execute("swapname"))[0])
31+
" call assert_equal([swfname], glob("Xtest2/*", 1, 1, 1))
32+
"
33+
" " 'dir', swap file in directory relative to the current dir
34+
" set dir=Xtest.je,~
35+
"
36+
" call mkdir("Xtest.je")
37+
" call writefile(content, 'Xtest2/Xtest3')
38+
" edit Xtest2/Xtest3
39+
" call assert_equal(["Xtest2/Xtest3"], glob("Xtest2/*", 1, 1, 1))
40+
" let swfname = "Xtest.je/Xtest3.swp"
41+
" call assert_equal(swfname, split(execute("swapname"))[0])
42+
" call assert_equal([swfname], glob("Xtest.je/*", 1, 1, 1))
43+
"
44+
" set dir&
45+
" call delete("Xtest1")
46+
" call delete("Xtest2", "rf")
47+
" call delete("Xtest.je", "rf")
48+
"endfunc
49+
50+
func Test_swap_group()
551
if !has("unix")
652
return
753
endif
8-
let content = ['start of testfile',
9-
\ 'line 2 Abcdefghij',
10-
\ 'line 3 Abcdefghij',
11-
\ 'end of testfile']
12-
call writefile(content, 'Xtest1')
13-
14-
" '.', swap file in the same directory as file
15-
set dir=.,~
16-
17-
" Verify that the swap file doesn't exist in the current directory
18-
call assert_equal([], glob(".Xtest1*.swp", 1, 1, 1))
19-
edit Xtest1
20-
let swfname = split(execute("swapname"))[0]
21-
call assert_equal([swfname], glob(swfname, 1, 1, 1))
22-
23-
" './dir', swap file in a directory relative to the file
24-
set dir=./Xtest2,.,~
25-
26-
call mkdir("Xtest2")
27-
edit Xtest1
28-
call assert_equal([], glob(swfname, 1, 1, 1))
29-
let swfname = "Xtest2/Xtest1.swp"
30-
call assert_equal(swfname, split(execute("swapname"))[0])
31-
call assert_equal([swfname], glob("Xtest2/*", 1, 1, 1))
54+
let groups = split(system('groups'))
55+
if len(groups) <= 1
56+
throw 'Skipped: need at least two groups, got ' . groups
57+
endif
3258

33-
" 'dir', swap file in directory relative to the current dir
34-
set dir=Xtest.je,~
59+
call delete('Xtest')
60+
split Xtest
61+
call setline(1, 'just some text')
62+
wq
63+
if system('ls -l Xtest') !~ ' ' . groups[0] . ' \d'
64+
throw 'Skipped: test file does not have the first group'
65+
else
66+
silent !chmod 640 Xtest
67+
call system('chgrp ' . groups[1] . ' Xtest')
68+
if system('ls -l Xtest') !~ ' ' . groups[1] . ' \d'
69+
throw 'Skipped: cannot set second group on test file'
70+
else
71+
split Xtest
72+
let swapname = substitute(execute('swapname'), '[[:space:]]', '', 'g')
73+
call assert_match('Xtest', swapname)
74+
" Group of swapfile must now match original file.
75+
call assert_match(' ' . groups[1] . ' \d', system('ls -l ' . swapname))
3576

36-
call mkdir("Xtest.je")
37-
call writefile(content, 'Xtest2/Xtest3')
38-
edit Xtest2/Xtest3
39-
call assert_equal(["Xtest2/Xtest3"], glob("Xtest2/*", 1, 1, 1))
40-
let swfname = "Xtest.je/Xtest3.swp"
41-
call assert_equal(swfname, split(execute("swapname"))[0])
42-
call assert_equal([swfname], glob("Xtest.je/*", 1, 1, 1))
77+
bwipe!
78+
endif
79+
endif
4380

44-
set dir&
45-
call delete("Xtest1")
46-
call delete("Xtest2", "rf")
47-
call delete("Xtest.je", "rf")
81+
call delete('Xtest')
4882
endfunc

Diff for: src/version.c

+2
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ static char *(features[]) =
761761

762762
static int included_patches[] =
763763
{ /* Add new patch number below this line */
764+
/**/
765+
1263,
764766
/**/
765767
1262,
766768
/**/

0 commit comments

Comments
 (0)