replaced some mch_lstat()

This commit is contained in:
Stefan Hoffmann
2014-05-02 20:08:36 +02:00
parent 902ad8d94d
commit 8e8dae71da
4 changed files with 32 additions and 46 deletions

View File

@@ -9729,44 +9729,45 @@ static void f_getftime(typval_T *argvars, typval_T *rettv)
static void f_getftype(typval_T *argvars, typval_T *rettv) static void f_getftype(typval_T *argvars, typval_T *rettv)
{ {
char_u *fname; char_u *fname;
struct stat st;
char_u *type = NULL; char_u *type = NULL;
char *t; char *t;
fname = get_tv_string(&argvars[0]); fname = get_tv_string(&argvars[0]);
rettv->v_type = VAR_STRING; rettv->v_type = VAR_STRING;
if (mch_lstat((char *)fname, &st) >= 0) { FileInfo file_info;
if (os_get_file_info_link((char *)fname, &file_info)) {
uint64_t mode = file_info.stat.st_mode;
#ifdef S_ISREG #ifdef S_ISREG
if (S_ISREG(st.st_mode)) if (S_ISREG(mode))
t = "file"; t = "file";
else if (S_ISDIR(st.st_mode)) else if (S_ISDIR(mode))
t = "dir"; t = "dir";
# ifdef S_ISLNK # ifdef S_ISLNK
else if (S_ISLNK(st.st_mode)) else if (S_ISLNK(mode))
t = "link"; t = "link";
# endif # endif
# ifdef S_ISBLK # ifdef S_ISBLK
else if (S_ISBLK(st.st_mode)) else if (S_ISBLK(mode))
t = "bdev"; t = "bdev";
# endif # endif
# ifdef S_ISCHR # ifdef S_ISCHR
else if (S_ISCHR(st.st_mode)) else if (S_ISCHR(mode))
t = "cdev"; t = "cdev";
# endif # endif
# ifdef S_ISFIFO # ifdef S_ISFIFO
else if (S_ISFIFO(st.st_mode)) else if (S_ISFIFO(mode))
t = "fifo"; t = "fifo";
# endif # endif
# ifdef S_ISSOCK # ifdef S_ISSOCK
else if (S_ISSOCK(st.st_mode)) else if (S_ISSOCK(mode))
t = "fifo"; t = "fifo";
# endif # endif
else else
t = "other"; t = "other";
#else #else
# ifdef S_IFMT # ifdef S_IFMT
switch (st.st_mode & S_IFMT) { switch (mode & S_IFMT) {
case S_IFREG: t = "file"; break; case S_IFREG: t = "file"; break;
case S_IFDIR: t = "dir"; break; case S_IFDIR: t = "dir"; break;
# ifdef S_IFLNK # ifdef S_IFLNK

View File

@@ -1041,10 +1041,6 @@ mf_do_open (
int flags /* flags for open() */ int flags /* flags for open() */
) )
{ {
#ifdef HAVE_LSTAT
struct stat sb;
#endif
mfp->mf_fname = fname; mfp->mf_fname = fname;
/* /*
@@ -1054,17 +1050,16 @@ mf_do_open (
*/ */
mf_set_ffname(mfp); mf_set_ffname(mfp);
#ifdef HAVE_LSTAT
/* /*
* Extra security check: When creating a swap file it really shouldn't * Extra security check: When creating a swap file it really shouldn't
* exist yet. If there is a symbolic link, this is most likely an attack. * exist yet. If there is a symbolic link, this is most likely an attack.
*/ */
if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0) { FileInfo file_info;
if ((flags & O_CREAT)
&& os_get_file_info_link((char *)mfp->mf_fname, &file_info)) {
mfp->mf_fd = -1; mfp->mf_fd = -1;
EMSG(_("E300: Swap file already exists (symlink attack?)")); EMSG(_("E300: Swap file already exists (symlink attack?)"));
} else } else {
#endif
{
/* /*
* try to open the file * try to open the file
*/ */

View File

@@ -1285,7 +1285,6 @@ void simplify_filename(char_u *filename)
if (components > 0) { /* strip one preceding component */ if (components > 0) { /* strip one preceding component */
int do_strip = FALSE; int do_strip = FALSE;
char_u saved_char; char_u saved_char;
struct stat st;
/* Don't strip for an erroneous file name. */ /* Don't strip for an erroneous file name. */
if (!stripping_disabled) { if (!stripping_disabled) {
@@ -1294,12 +1293,10 @@ void simplify_filename(char_u *filename)
* link that refers to a non-existent file. */ * link that refers to a non-existent file. */
saved_char = p[-1]; saved_char = p[-1];
p[-1] = NUL; p[-1] = NUL;
#ifdef UNIX FileInfo file_info;
if (mch_lstat((char *)filename, &st) < 0) if (!os_get_file_info_link((char *)filename, &file_info)) {
#else
if (mch_stat((char *)filename, &st) < 0)
#endif
do_strip = TRUE; do_strip = TRUE;
}
p[-1] = saved_char; p[-1] = saved_char;
--p; --p;
@@ -1320,40 +1317,37 @@ void simplify_filename(char_u *filename)
* components. */ * components. */
saved_char = *tail; saved_char = *tail;
*tail = NUL; *tail = NUL;
if (mch_stat((char *)filename, &st) >= 0) if (os_get_file_info((char *)filename, &file_info)) {
do_strip = TRUE; do_strip = TRUE;
}
else else
stripping_disabled = TRUE; stripping_disabled = TRUE;
*tail = saved_char; *tail = saved_char;
#ifdef UNIX
if (do_strip) { if (do_strip) {
struct stat new_st; /* The check for the unstripped file name
/* On Unix, the check for the unstripped file name
* above works also for a symbolic link pointing to * above works also for a symbolic link pointing to
* a searchable directory. But then the parent of * a searchable directory. But then the parent of
* the directory pointed to by the link must be the * the directory pointed to by the link must be the
* same as the stripped file name. (The latter * same as the stripped file name. (The latter
* exists in the file system since it is the * exists in the file system since it is the
* component's parent directory.) */ * component's parent directory.) */
if (p == start && relative) FileInfo new_file_info;
(void)mch_stat(".", &new_st); if (p == start && relative) {
else { os_get_file_info(".", &new_file_info);
} else {
saved_char = *p; saved_char = *p;
*p = NUL; *p = NUL;
(void)mch_stat((char *)filename, &new_st); os_get_file_info((char *)filename, &new_file_info);
*p = saved_char; *p = saved_char;
} }
if (new_st.st_ino != st.st_ino || if (!os_file_info_id_equal(&file_info, &new_file_info)) {
new_st.st_dev != st.st_dev) {
do_strip = FALSE; do_strip = FALSE;
/* We don't disable stripping of later /* We don't disable stripping of later
* components since the unstripped path name is * components since the unstripped path name is
* still valid. */ * still valid. */
} }
} }
#endif
} }
} }

View File

@@ -2573,9 +2573,6 @@ static char_u *get_mef_name(void)
char_u *name; char_u *name;
static int start = -1; static int start = -1;
static int off = 0; static int off = 0;
#ifdef HAVE_LSTAT
struct stat sb;
#endif
if (*p_mef == NUL) { if (*p_mef == NUL) {
name = vim_tempname('e'); name = vim_tempname('e');
@@ -2602,13 +2599,12 @@ static char_u *get_mef_name(void)
STRCPY(name, p_mef); STRCPY(name, p_mef);
sprintf((char *)name + (p - p_mef), "%d%d", start, off); sprintf((char *)name + (p - p_mef), "%d%d", start, off);
STRCAT(name, p + 2); STRCAT(name, p + 2);
if (!os_file_exists(name) // Don't accept a symbolic link, its a security risk.
#ifdef HAVE_LSTAT FileInfo file_info;
/* Don't accept a symbolic link, its a security risk. */ bool file_or_link_found = os_get_file_info_link((char *)name, &file_info);
&& mch_lstat((char *)name, &sb) < 0 if (!file_or_link_found) {
#endif
)
break; break;
}
free(name); free(name);
} }
return name; return name;