Skip to content

Commit 7d7f067

Browse files
committed
Use 64-bit file sizes when resuming scans. See nmap#1742
1 parent 7ce92c6 commit 7d7f067

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

nmap.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -2335,14 +2335,14 @@ int nmap_main(int argc, char *argv[]) {
23352335

23362336
int gather_logfile_resumption_state(char *fname, int *myargc, char ***myargv) {
23372337
char *filestr;
2338-
int filelen;
2338+
s64 filelen;
23392339
char nmap_arg_buffer[4096]; /* roughly aligned with arg_parse limit */
23402340
struct in_addr lastip;
23412341
char *p, *q, *found, *lastipstr; /* I love C! */
23422342
/* We mmap it read/write since we will change the last char to a newline if it is not already */
23432343
filestr = mmapfile(fname, &filelen, O_RDWR);
23442344
if (!filestr) {
2345-
fatal("Could not mmap() %s file. Make sure you have enough rights and the file really exists.", fname);
2345+
pfatal("Could not mmap() %s file", fname);
23462346
}
23472347

23482348
if (filelen < 20) {
@@ -2486,7 +2486,7 @@ int gather_logfile_resumption_state(char *fname, int *myargc, char ***myargv) {
24862486
/* Ensure the log file ends with a newline */
24872487
filestr[filelen - 1] = '\n';
24882488
if (munmap(filestr, filelen) != 0)
2489-
gh_perror("%s: error in munmap(%p, %u)", __func__, filestr, filelen);
2489+
gh_perror("%s: error in munmap(%p, %ld)", __func__, filestr, filelen);
24902490

24912491
return 0;
24922492
}

utils.cc

+13-5
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ static int open2mmap_flags(int open_flags)
605605
undefined, and errno is set to something appropriate. The user is responsible
606606
for doing an munmap(ptr, length) when finished with it. openflags should be
607607
O_RDONLY or O_RDWR, or O_WRONLY. */
608-
char *mmapfile(char *fname, int *length, int openflags) {
608+
char *mmapfile(char *fname, s64 *length, int openflags) {
609609
struct stat st;
610610
int fd;
611611
int mmap_flags;
@@ -654,9 +654,10 @@ char *mmapfile(char *fname, int *length, int openflags) {
654654

655655
static HANDLE gmap = NULL;
656656

657-
char *mmapfile(char *fname, int *length, int openflags) {
657+
char *mmapfile(char *fname, s64 *length, int openflags) {
658658
HANDLE fd;
659659
DWORD mflags, oflags;
660+
DWORD lowsize, highsize;
660661
char *fileptr;
661662

662663
if (!length || !fname) {
@@ -683,11 +684,18 @@ char *mmapfile(char *fname, int *length, int openflags) {
683684
if (!fd)
684685
pfatal ("%s(%u): CreateFile()", __FILE__, __LINE__);
685686

686-
*length = (int) GetFileSize (fd, NULL);
687+
lowsize = GetFileSize (fd, &highsize);
688+
if (lowsize == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) {
689+
pfatal("%s(%u): GetFileSize(), file '%s'", __FILE__, __LINE__, fname);
690+
}
691+
*length = lowsize + highsize << sizeof(DWORD);
692+
if (*length < 0) {
693+
fatal("%s(%u): size too large, file '%s'", __FILE__, __LINE__, fname);
694+
}
687695

688696
gmap = CreateFileMapping (fd, NULL, mflags, 0, 0, NULL);
689697
if (!gmap) {
690-
pfatal("%s(%u): CreateFileMapping(), file '%s', length %d, mflags %08lX",
698+
pfatal("%s(%u): CreateFileMapping(), file '%s', length %I64d, mflags %08lX",
691699
__FILE__, __LINE__, fname, *length, mflags);
692700
}
693701

@@ -696,7 +704,7 @@ char *mmapfile(char *fname, int *length, int openflags) {
696704
pfatal ("%s(%u): MapViewOfFile()", __FILE__, __LINE__);
697705

698706
if (o.debugging > 2) {
699-
log_write(LOG_PLAIN, "%s(): fd %08lX, gmap %08lX, fileptr %08lX, length %d\n",
707+
log_write(LOG_PLAIN, "%s(): fd %08lX, gmap %08lX, fileptr %08lX, length %I64d\n",
700708
__func__, (DWORD)fd, (DWORD)gmap, (DWORD)fileptr, *length);
701709
}
702710

utils.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ u8 *parse_hex_string(char *str, size_t *outlen);
189189

190190
int cpe_get_part(const char *cpe);
191191

192-
char *mmapfile(char *fname, int *length, int openflags);
192+
char *mmapfile(char *fname, s64 *length, int openflags);
193193

194194
#ifdef WIN32
195195
int win32_munmap(char *filestr, int filelen);

0 commit comments

Comments
 (0)