Skip to content

Commit 2b8149a

Browse files
flichtenheldcron2
authored andcommitted
buffer: Change limits for array_mult_safe
- Lower the limit to 1GB on 32bit systems. The limit of 4GB-1 makes no sense on systems that usually don't allow a single process to allocate anything near to this limit. - Increate the limit from 4GB-1 to 4GB on other systems. It makes no difference in protection but makes it much easier to use the limit in other contexts, e.g. if dividing it. Change-Id: I4f95edd7ce2098180aa620a231727217f333a12d Signed-off-by: Frank Lichtenheld <[email protected]> Acked-by: Gert Doering <[email protected]> Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1436 Message-Id: <[email protected]> URL: https://www.mail-archive.com/[email protected]/msg35044.html Signed-off-by: Gert Doering <[email protected]>
1 parent e5ff824 commit 2b8149a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/openvpn/buffer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
size_t
4040
array_mult_safe(const size_t m1, const size_t m2, const size_t extra)
4141
{
42-
const size_t limit = 0xFFFFFFFF;
42+
const size_t limit = ALLOC_SIZE_MAX;
4343
unsigned long long res =
4444
(unsigned long long)m1 * (unsigned long long)m2 + (unsigned long long)extra;
4545
if (unlikely(m1 > limit) || unlikely(m2 > limit) || unlikely(extra > limit)

src/openvpn/buffer.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,18 @@ gc_reset(struct gc_arena *a)
10441044
* Allocate memory to hold a structure
10451045
*/
10461046

1047+
/* When allocating arrays make sure we do not use a excessive amount
1048+
* of memory.
1049+
*/
1050+
#if UINTPTR_MAX <= UINT32_MAX
1051+
/* 1 GB on 32bit systems, they usually can only allocate 2 GB for the
1052+
* whole process.
1053+
*/
1054+
#define ALLOC_SIZE_MAX (1u << 30)
1055+
#else
1056+
#define ALLOC_SIZE_MAX ((size_t)1 << 32) /* 4 GB */
1057+
#endif
1058+
10471059
#define ALLOC_OBJ(dptr, type) \
10481060
{ \
10491061
check_malloc_return((dptr) = (type *)malloc(sizeof(type))); \

0 commit comments

Comments
 (0)