Skip to content

Commit 79bf2db

Browse files
ref: use SIZE_MAX in sentry_envelope_deserialize (#1328)
* ref: use SIZE_MAX in sentry_envelope_deserialize * restore payload_len < 0 check * fix length parsing vuln * Update src/sentry_envelope.c Co-authored-by: JoshuaMoelans <[email protected]> --------- Co-authored-by: JoshuaMoelans <[email protected]>
1 parent 98b3891 commit 79bf2db

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/sentry_envelope.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -814,20 +814,22 @@ sentry_envelope_deserialize(const char *buf, size_t buf_len)
814814
payload_end = end;
815815
}
816816
item->payload_len = (size_t)(payload_end - ptr);
817+
} else if (sentry_value_get_type(length) == SENTRY_VALUE_TYPE_UINT64) {
818+
uint64_t payload_len = sentry_value_as_uint64(length);
819+
if (payload_len >= SIZE_MAX) {
820+
goto fail;
821+
}
822+
item->payload_len = (size_t)payload_len;
817823
} else {
818-
// TODO: sentry_value_as_uint64
819-
// https://github.com/getsentry/sentry-native/pull/1301
820-
int payload_len = sentry_value_as_int32(length);
821-
if (payload_len < 0) {
824+
int64_t payload_len = sentry_value_as_int64(length);
825+
if (payload_len < 0 || (uint64_t)payload_len >= SIZE_MAX) {
822826
goto fail;
823827
}
824828
item->payload_len = (size_t)payload_len;
825829
}
826830
if (item->payload_len > 0) {
827-
// TODO: SIZE_MAX
828-
// https://github.com/getsentry/sentry-native/pull/1301
829831
if (ptr + item->payload_len > end
830-
|| item->payload_len > INT32_MAX - 1) {
832+
|| item->payload_len >= SIZE_MAX) {
831833
goto fail;
832834
}
833835
item->payload = sentry_malloc(item->payload_len + 1);

tests/unit/test_envelopes.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,7 @@ SENTRY_TEST(deserialize_envelope_invalid)
671671
TEST_CHECK(!sentry_envelope_deserialize("{}\ninvalid\n", 11));
672672
TEST_CHECK(!sentry_envelope_deserialize("invalid", 7));
673673
TEST_CHECK(!sentry_envelope_deserialize("{}\n{\"length\":-1}\n", 17));
674-
// TODO: SIZE_MAX
675-
// https://github.com/getsentry/sentry-native/pull/1301
676674
char buf[128];
677-
snprintf(buf, sizeof(buf), "{}\n{\"length\":%d}\n", INT32_MAX);
675+
snprintf(buf, sizeof(buf), "{}\n{\"length\":%zu}\n", SIZE_MAX);
678676
TEST_CHECK(!sentry_envelope_deserialize(buf, strlen(buf)));
679677
}

0 commit comments

Comments
 (0)