Browse Source

Fail if file is too large to mmap.

If tor_mmap_file is called with a file which is larger than SIZE_MAX,
only a small part of the file will be memory-mapped due to integer
truncation.

This can only realistically happen on 32 bit architectures with large
file support.
junglefowl 7 years ago
parent
commit
373d9aff7a
2 changed files with 11 additions and 0 deletions
  1. 5 0
      changes/bug21134
  2. 6 0
      src/common/compat.c

+ 5 - 0
changes/bug21134

@@ -0,0 +1,5 @@
+  o Minor bugfixes (portability):
+    - Do not silently truncate content of files if they are larger
+      than SIZE_MAX bytes. This issue could occur on 32 bit systems
+      with large file support and files which are larger than 4 GB.
+      Fixes bug 21134; bugfix on 0.3.0.1-alpha.

+ 6 - 0
src/common/compat.c

@@ -258,6 +258,12 @@ tor_mmap_file(const char *filename)
   page_size = getpagesize();
   size += (size%page_size) ? page_size-(size%page_size) : 0;
 
+  if (st.st_size > SSIZE_T_CEILING || size < st.st_size) {
+    log_warn(LD_FS, "File \"%s\" is too large. Ignoring.",filename);
+    errno = EFBIG;
+    close(fd);
+    return NULL;
+  }
   if (!size) {
     /* Zero-length file. If we call mmap on it, it will succeed but
      * return NULL, and bad things will happen. So just fail. */