From 3bc7b6af90922cc36f335547c88f67202ab6ec4a Mon Sep 17 00:00:00 2001 From: Jukka Ojanen Date: Sat, 21 Aug 2021 14:29:10 +0300 Subject: [PATCH] Avoid directly accessing file handle --- include/filehandling.h | 3 ++- src/filehandling.c | 52 ++++++++++++++++++++++++++++-------------- src/locking.c | 4 ++-- src/outfile_check.c | 2 +- src/restore.c | 11 +-------- src/shared.c | 4 ++-- src/wordlist.c | 2 +- 7 files changed, 44 insertions(+), 34 deletions(-) diff --git a/include/filehandling.h b/include/filehandling.h index db03ac456..5eea01080 100644 --- a/include/filehandling.h +++ b/include/filehandling.h @@ -22,11 +22,12 @@ int hc_fprintf (HCFILE *fp, const char *format, ...); int hc_vfprintf (HCFILE *fp, const char *format, va_list ap); int hc_fseek (HCFILE *fp, off_t offset, int whence); void hc_rewind (HCFILE *fp); +int hc_fstat (HCFILE *fp, struct stat *buf); off_t hc_ftell (HCFILE *fp); int hc_fgetc (HCFILE *fp); -int hc_fileno (HCFILE *fp); int hc_feof (HCFILE *fp); void hc_fflush (HCFILE *fp); +void hc_fsync (HCFILE *fp); void hc_fclose (HCFILE *fp); int hc_fputc (int c, HCFILE *fp); char *hc_fgets (char *buf, int len, HCFILE *fp); diff --git a/src/filehandling.c b/src/filehandling.c index f23c13024..48c2831b2 100644 --- a/src/filehandling.c +++ b/src/filehandling.c @@ -96,19 +96,19 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode) close (fd_tmp); } - if (is_zip == false) + if (fmode == -1) { - if (fmode == -1) - { - fp->fd = open (path, oflag); - } - else - { - fp->fd = open (path, oflag, fmode); - } + fp->fd = open (path, oflag); + } + else + { + fp->fd = open (path, oflag, fmode); + } - if (fp->fd == -1) return false; + if (fp->fd == -1) return false; + if (is_zip == false) + { if (is_gzip) { if ((fp->gfp = gzdopen (fp->fd, mode)) == NULL) return false; @@ -389,6 +389,13 @@ void hc_rewind (HCFILE *fp) } } +int hc_fstat (HCFILE *fp, struct stat *buf) +{ + if (fp == NULL || buf == NULL || fp->fd == -1) return -1; + + return fstat (fp->fd, buf); +} + off_t hc_ftell (HCFILE *fp) { off_t n = 0; @@ -544,13 +551,6 @@ int hc_fscanf (HCFILE *fp, const char *format, void *ptr) return 1; } -int hc_fileno (HCFILE *fp) -{ - if (fp == NULL) return 1; - - return fp->fd; -} - int hc_feof (HCFILE *fp) { int r = -1; @@ -590,6 +590,22 @@ void hc_fflush (HCFILE *fp) } } +void hc_fsync (HCFILE *fp) +{ + if (fp == NULL) return; + + if (fp->pfp) + { +#if defined (_WIN) + HANDLE h = (HANDLE) _get_osfhandle (fp->fd); + + FlushFileBuffers (h); +#else + fsync (fp->fd); +#endif + } +} + void hc_fclose (HCFILE *fp) { if (fp == NULL) return; @@ -603,6 +619,8 @@ void hc_fclose (HCFILE *fp) unzCloseCurrentFile (fp->ufp); unzClose (fp->ufp); + + close (fp->fd); } else if (fp->pfp) { diff --git a/src/locking.c b/src/locking.c index 411a02178..8918101a2 100644 --- a/src/locking.c +++ b/src/locking.c @@ -21,7 +21,7 @@ int hc_lockfile (HCFILE *fp) lock.l_type = F_WRLCK; /* Needs this loop because a signal may interrupt a wait for lock */ - while (fcntl (hc_fileno (fp), F_SETLKW, &lock)) + while (fcntl (fp->fd, F_SETLKW, &lock)) { if (errno != EINTR) return -1; } @@ -39,7 +39,7 @@ int hc_unlockfile (HCFILE *fp) lock.l_type = F_UNLCK; - if (fcntl (hc_fileno (fp), F_SETLK, &lock)) return -1; + if (fcntl (fp->fd, F_SETLK, &lock)) return -1; return 0; } diff --git a/src/outfile_check.c b/src/outfile_check.c index 359806985..82abfab1f 100644 --- a/src/outfile_check.c +++ b/src/outfile_check.c @@ -163,7 +163,7 @@ static int outfile_remove (hashcat_ctx_t *hashcat_ctx) struct stat outfile_stat; - if (fstat (hc_fileno (&fp), &outfile_stat)) + if (hc_fstat (&fp, &outfile_stat)) { hc_fclose (&fp); diff --git a/src/restore.c b/src/restore.c index 03b7d3125..37832d906 100644 --- a/src/restore.c +++ b/src/restore.c @@ -13,15 +13,6 @@ #include "folder.h" #include "restore.h" -#if defined (_WIN) -static void fsync (int fd) -{ - HANDLE h = (HANDLE) _get_osfhandle (fd); - - FlushFileBuffers (h); -} -#endif - static int init_restore (hashcat_ctx_t *hashcat_ctx) { restore_ctx_t *restore_ctx = hashcat_ctx->restore_ctx; @@ -232,7 +223,7 @@ static int write_restore (hashcat_ctx_t *hashcat_ctx) hc_fflush (&fp); - fsync (hc_fileno (&fp)); + hc_fsync (&fp); hc_fclose (&fp); diff --git a/src/shared.c b/src/shared.c index 919948232..a2b148c76 100644 --- a/src/shared.c +++ b/src/shared.c @@ -693,7 +693,7 @@ bool hc_same_files (char *file1, char *file2) if (hc_fopen (&fp, file1, "r") == true) { - if (fstat (hc_fileno (&fp), &tmpstat_file1)) + if (hc_fstat (&fp, &tmpstat_file1)) { hc_fclose (&fp); @@ -707,7 +707,7 @@ bool hc_same_files (char *file1, char *file2) if (hc_fopen (&fp, file2, "r") == true) { - if (fstat (hc_fileno (&fp), &tmpstat_file2)) + if (hc_fstat (&fp, &tmpstat_file2)) { hc_fclose (&fp); diff --git a/src/wordlist.c b/src/wordlist.c index aa8aaf82e..176974e8c 100644 --- a/src/wordlist.c +++ b/src/wordlist.c @@ -343,7 +343,7 @@ int count_words (hashcat_ctx_t *hashcat_ctx, HCFILE *fp, const char *dictfile, u memset (&d, 0, sizeof (d)); - if (fstat (hc_fileno (fp), &d.stat)) + if (hc_fstat (fp, &d.stat)) { *result = 0;