Skip to content

Commit aeeb688

Browse files
committed
patch 8.0.1287: temp file used for viminfo may have wrong permissions
Problem: The temp file used when updating the viminfo file may have the wrong permissions if setting the group fails. Solution: Check if the group matches and reduce permissions if not.
1 parent 5fd8b78 commit aeeb688

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/ex_cmds.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,7 +2007,8 @@ write_viminfo(char_u *file, int forceit)
20072007

20082008
/*
20092009
* If we can't create in the same directory, try creating a
2010-
* "normal" temp file.
2010+
* "normal" temp file. This is just an attempt, renaming the temp
2011+
* file might fail as well.
20112012
*/
20122013
if (fp_out == NULL)
20132014
{
@@ -2018,11 +2019,29 @@ write_viminfo(char_u *file, int forceit)
20182019

20192020
#if defined(UNIX) && defined(HAVE_FCHOWN)
20202021
/*
2021-
* Make sure the owner can read/write it. This only works for
2022-
* root.
2022+
* Make sure the original owner can read/write the tempfile and
2023+
* otherwise preserve permissions, making sure the group matches.
20232024
*/
20242025
if (fp_out != NULL)
2025-
ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid);
2026+
{
2027+
stat_T tmp_st;
2028+
2029+
if (mch_stat((char *)tempname, &tmp_st) >= 0)
2030+
{
2031+
if (st_old.st_uid != tmp_st.st_uid)
2032+
/* Changing the owner might fail, in which case the
2033+
* file will now owned by the current user, oh well. */
2034+
ignored = fchown(fileno(fp_out), st_old.st_uid, -1);
2035+
if (st_old.st_gid != tmp_st.st_gid
2036+
&& fchown(fileno(fp_out), -1, st_old.st_gid) == -1)
2037+
/* can't set the group to what it should be, remove
2038+
* group permissions */
2039+
(void)mch_setperm(tempname, 0600);
2040+
}
2041+
else
2042+
/* can't stat the file, set conservative permissions */
2043+
(void)mch_setperm(tempname, 0600);
2044+
}
20262045
#endif
20272046
}
20282047
}
@@ -7536,7 +7555,7 @@ ex_sign(exarg_T *eap)
75367555
int idx;
75377556
sign_T *sp;
75387557
sign_T *sp_prev;
7539-
buf_T *buf;
7558+
buf_T *buf = NULL;
75407559

75417560
/* Parse the subcommand. */
75427561
p = skiptowhite(arg);

src/version.c

Lines changed: 2 additions & 0 deletions
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+
1287,
764766
/**/
765767
1286,
766768
/**/

0 commit comments

Comments
 (0)