Skip to content

Commit 141c4be

Browse files
committed
Limit unserialization element count more aggressively
This is slightly more aggressive about rejecting obviously incorrect element counts. Previously the number of elements was allowed to match the number of characters. Now it is the number of characters divided by two (this can actually be increased further to at least 4). This doesn't really matter in the grand scheme of things (as it just cuts maximum memory usage by half), but should fix oss-fuzz #29356.
1 parent 21562aa commit 141c4be

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

ext/standard/var_unserializer.re

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
#define VAR_WAKEUP_FLAG 1
3030
#define VAR_UNSERIALIZE_FLAG 2
3131

32+
/* Each element is encoded using at least 2 characters. */
33+
#define IS_FAKE_ELEM_COUNT(num_elems, serialized_len) \
34+
((num_elems) > (serialized_len) / 2)
35+
3236
typedef struct {
3337
zend_long used_slots;
3438
void *next;
@@ -1001,7 +1005,7 @@ use_double:
10011005
*p = YYCURSOR;
10021006
if (!var_hash) return 0;
10031007
1004-
if (elements < 0 || elements >= HT_MAX_SIZE || elements > max - YYCURSOR) {
1008+
if (elements < 0 || elements >= HT_MAX_SIZE || IS_FAKE_ELEM_COUNT(elements, max - YYCURSOR)) {
10051009
return 0;
10061010
}
10071011
@@ -1169,7 +1173,7 @@ object ":" uiv ":" ["] {
11691173
}
11701174

11711175
elements = parse_iv2(*p + 2, p);
1172-
if (elements < 0 || elements > max - YYCURSOR) {
1176+
if (elements < 0 || IS_FAKE_ELEM_COUNT(elements, max - YYCURSOR)) {
11731177
zend_string_release_ex(class_name, 0);
11741178
return 0;
11751179
}

0 commit comments

Comments
 (0)