Skip to content

Commit

Permalink
Handle very large -f files by rejecting them.
Browse files Browse the repository at this point in the history
_read(), on Windows, has a 32-bit size argument and a 32-bit return
value, so reject -f files that have more than 2^31-1 characters.

Add some #defines so that, on Windows, we use _fstati64 to get the size
of that file, to handle large files.

Don't assume that our definition for ssize_t is the same size as size_t;
by the time we want to print the return value of the read, we know it'll
fit into an int, so just cast it to int and print it with %d.
  • Loading branch information
guyharris committed Sep 30, 2020
1 parent 7810dd3 commit faf8fb7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
16 changes: 14 additions & 2 deletions netdissect-stdinc.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,17 @@
#define stat _stat
#define strdup _strdup
#define open _open
#define fstat _fstat
#define read _read
#define close _close
#define O_RDONLY _O_RDONLY

/*
* We define our_fstat64 as _fstati64, and define our_statb as
* struct _stati64, so we get 64-bit file sizes.
*/
#define our_fstat _fstati64
#define our_statb struct _stati64

/*
* If <crtdbg.h> has been included, and _DEBUG is defined, and
* __STDC__ is zero, <crtdbg.h> will define strdup() to call
Expand Down Expand Up @@ -232,6 +238,13 @@ typedef char* caddr_t;

#include <arpa/inet.h>

/*
* We should have large file support enabled, if it's available,
* so just use fstat as our_fstat and struct stat as our_statb.
*/
#define our_fstat fstat
#define our_statb struct stat

/*
* Assume all UN*Xes have strtoll(), and use it for strtoint64_t().
*/
Expand All @@ -241,7 +254,6 @@ typedef char* caddr_t;
* Assume LL works.
*/
#define INT64_T_CONSTANT(constant) (constant##LL)

#endif /* _WIN32 */

/*
Expand Down
15 changes: 12 additions & 3 deletions tcpdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ The Regents of the University of California. All rights reserved.\n";
#include <sys/sysctl.h>
#endif /* __FreeBSD__ */

#include "netdissect-stdinc.h"
#include "netdissect.h"
#include "interface.h"
#include "addrtoname.h"
Expand Down Expand Up @@ -1065,15 +1066,22 @@ read_infile(char *fname)
int i, fd;
ssize_t cc;
char *cp;
struct stat buf;
our_statb buf;

fd = open(fname, O_RDONLY|O_BINARY);
if (fd < 0)
error("can't open %s: %s", fname, pcap_strerror(errno));

if (fstat(fd, &buf) < 0)
if (our_fstat(fd, &buf) < 0)
error("can't stat %s: %s", fname, pcap_strerror(errno));

/*
* Reject files whose size doesn't fit into an int; a filter
* *that* large will probably be too big.
*/
if (buf.st_size > INT_MAX)
error("%s is too large", fname);

cp = malloc((u_int)buf.st_size + 1);
if (cp == NULL)
error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
Expand All @@ -1082,7 +1090,8 @@ read_infile(char *fname)
if (cc < 0)
error("read %s: %s", fname, pcap_strerror(errno));
if (cc != buf.st_size)
error("short read %s (%zd != %d)", fname, cc, (int)buf.st_size);
error("short read %s (%d != %d)", fname, (int) cc,
(int)buf.st_size);

close(fd);
/* replace "# comment" with spaces */
Expand Down

0 comments on commit faf8fb7

Please sign in to comment.