Skip to contentSkip to conversation

Limit file size assignment to safe size_t range.

PATCH In review Matthias Andree via Openvpn-devel 1 participant 2 messages Jul 1

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Limit file size assignment to safe size_t range.

Some operating systems (FreeBSD 14 on i386) have sizeof(size_t) == 4 (which is used for object sizes) sizeof(off_t) == 8 (which is used for file sizes), so assigning a file size obtained from stat to a size_t causes justified compiler warnings about a narrowing conversion.

It is safe to assume a 32-bit platform will not want to load a >= 4 GB file there, so let's just ASSERT() that the off_t we are about to assign fits into a size_t object.

---
 src/openvpn/buffer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
src/openvpn/buffer.c +4 -1
index 5f2b2338..4b6acb36 100644
@@ -1391,7 +1391,10 @@ buffer_read_from_file(const char *filename, struct gc_arena *gc)
         return ret;
     }
 
-    const size_t size = file_stat.st_size;
+    /* for some systems, off_t is 63 bits wide + sign bit and size_t is 32 bits
+     * wide, and we need to avoid negative garbage wrapping around */
+    ASSERT(file_stat.st_size >= 0 && file_stat.st_size <= SIZE_MAX);
+    const size_t size = (size_t)file_stat.st_size;
     ret = alloc_buf_gc(size + 1, gc); /* space for trailing \0 */
     size_t read_size = fread(BPTR(&ret), 1, size, fp);
     if (read_size == 0)
Signature · 8 lines
2.54.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#1 lore raw

This is against master and can be cherry-picked to release/2.7 and release/2.6.

Am 01.07.26 um 20:36 schrieb Matthias Andree via Openvpn-devel:

Quoted 23 lines

Some operating systems (FreeBSD 14 on i386) have sizeof(size_t) == 4 (which is used for object sizes) sizeof(off_t) == 8 (which is used for file sizes), so assigning a file size obtained from stat to a size_t causes justified compiler warnings about a narrowing conversion.

It is safe to assume a 32-bit platform will not want to load a >= 4 GB file there, so let's just ASSERT() that the off_t we are about to assign fits into a size_t object.

---
  src/openvpn/buffer.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
src/openvpn/buffer.c +0 -0
index 5f2b2338..4b6acb36 100644
@@ -1391,7 +1391,10 @@ buffer_read_from_file(const char *filename, struct gc_arena *gc)
          return ret;

src/openvpn/buffer.c buffer_read_from_file(const char *filename, struct gc_arena *gc) · line 1400

Quoted 10 lines
src/openvpn/buffer.c +4 -1
      }
 
-    const size_t size = file_stat.st_size;
+    /* for some systems, off_t is 63 bits wide + sign bit and size_t is 32 bits
+     * wide, and we need to avoid negative garbage wrapping around */
+    ASSERT(file_stat.st_size >= 0 && file_stat.st_size <= SIZE_MAX);
+    const size_t size = (size_t)file_stat.st_size;
      ret = alloc_buf_gc(size + 1, gc); /* space for trailing \0 */
      size_t read_size = fread(BPTR(&ret), 1, size, fp);
      if (read_size == 0)

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel