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)
{
char_u *fname;
struct stat st;
char_u *type = NULL;
char *t;
fname = get_tv_string(&argvars[0]);
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
if (S_ISREG(st.st_mode))
if (S_ISREG(mode))
t = "file";
else if (S_ISDIR(st.st_mode))
else if (S_ISDIR(mode))
t = "dir";
# ifdef S_ISLNK
else if (S_ISLNK(st.st_mode))
else if (S_ISLNK(mode))
t = "link";
# endif
# ifdef S_ISBLK
else if (S_ISBLK(st.st_mode))
else if (S_ISBLK(mode))
t = "bdev";
# endif
# ifdef S_ISCHR
else if (S_ISCHR(st.st_mode))
else if (S_ISCHR(mode))
t = "cdev";
# endif
# ifdef S_ISFIFO
else if (S_ISFIFO(st.st_mode))
else if (S_ISFIFO(mode))
t = "fifo";
# endif
# ifdef S_ISSOCK
else if (S_ISSOCK(st.st_mode))
else if (S_ISSOCK(mode))
t = "fifo";
# endif
else
t = "other";
#else
# ifdef S_IFMT
switch (st.st_mode & S_IFMT) {
switch (mode & S_IFMT) {
case S_IFREG: t = "file"; break;
case S_IFDIR: t = "dir"; break;
# ifdef S_IFLNK

View File

@@ -1041,10 +1041,6 @@ mf_do_open (
int flags /* flags for open() */
)
{
#ifdef HAVE_LSTAT
struct stat sb;
#endif
mfp->mf_fname = fname;
/*
@@ -1054,17 +1050,16 @@ mf_do_open (
*/
mf_set_ffname(mfp);
#ifdef HAVE_LSTAT
/*
* 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.
*/
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;
EMSG(_("E300: Swap file already exists (symlink attack?)"));
} else
#endif
{
} else {
/*
* try to open the file
*/

View File

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

View File

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