Skip to content

Commit f5e5df1

Browse files
[AUTO-CHERRYPICK] rapidjson: fix CVE-2024-38517 and CVE-2024-39684 - branch main (#9897)
Co-authored-by: xiaohong <Xiaohong-Deng@users.noreply.github.com>
1 parent 2dd2769 commit f5e5df1

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
From 8269bc2bc289e9d343bae51cdf6d23ef0950e001 Mon Sep 17 00:00:00 2001
2+
From: Florin Malita <fmalita@gmail.com>
3+
Date: Tue, 15 May 2018 22:48:07 -0400
4+
Subject: [PATCH] Prevent int underflow when parsing exponents
5+
6+
When parsing negative exponents, the current implementation takes
7+
precautions for |exp| to not underflow int.
8+
9+
But that is not sufficient: later on [1], |exp + expFrac| is also
10+
stored to an int - so we must ensure that the sum stays within int
11+
representable values.
12+
13+
Update the exp clamping logic to take expFrac into account.
14+
15+
[1] https://github.com/Tencent/rapidjson/blob/master/include/rapidjson/reader.h#L1690
16+
---
17+
include/rapidjson/reader.h | 11 ++++++++++-
18+
test/unittest/readertest.cpp | 1 +
19+
2 files changed, 11 insertions(+), 1 deletion(-)
20+
21+
diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h
22+
index 7441eda4..f95aef42 100644
23+
--- a/include/rapidjson/reader.h
24+
+++ b/include/rapidjson/reader.h
25+
@@ -1632,9 +1632,18 @@ private:
26+
if (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) {
27+
exp = static_cast<int>(s.Take() - '0');
28+
if (expMinus) {
29+
+ // (exp + expFrac) must not underflow int => we're detecting when -exp gets
30+
+ // dangerously close to INT_MIN (a pessimistic next digit 9 would push it into
31+
+ // underflow territory):
32+
+ //
33+
+ // -(exp * 10 + 9) + expFrac >= INT_MIN
34+
+ // <=> exp <= (expFrac - INT_MIN - 9) / 10
35+
+ RAPIDJSON_ASSERT(expFrac <= 0);
36+
+ int maxExp = (expFrac + 2147483639) / 10;
37+
+
38+
while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) {
39+
exp = exp * 10 + static_cast<int>(s.Take() - '0');
40+
- if (exp >= 214748364) { // Issue #313: prevent overflow exponent
41+
+ if (RAPIDJSON_UNLIKELY(exp > maxExp)) {
42+
while (RAPIDJSON_UNLIKELY(s.Peek() >= '0' && s.Peek() <= '9')) // Consume the rest of exponent
43+
s.Take();
44+
}
45+
diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp
46+
index e5308019..c4800b93 100644
47+
--- a/test/unittest/readertest.cpp
48+
+++ b/test/unittest/readertest.cpp
49+
@@ -242,6 +242,7 @@ static void TestParseDouble() {
50+
TEST_DOUBLE(fullPrecision, "1e-214748363", 0.0); // Maximum supported negative exponent
51+
TEST_DOUBLE(fullPrecision, "1e-214748364", 0.0);
52+
TEST_DOUBLE(fullPrecision, "1e-21474836311", 0.0);
53+
+ TEST_DOUBLE(fullPrecision, "1.00000000001e-2147483638", 0.0);
54+
TEST_DOUBLE(fullPrecision, "0.017976931348623157e+310", 1.7976931348623157e+308); // Max double in another form
55+
56+
// Since
57+
--
58+
2.34.1
59+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CVE-2024-39684 is a duplicate of CVE-2024-38517

SPECS/rapidjson/rapidjson.spec

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Summary: A fast JSON parser/generator for C++ with both SAX/DOM style API
22
Name: rapidjson
33
Version: 1.1.0
4-
Release: 7%{?dist}
4+
Release: 8%{?dist}
55
License: BSD and JSON and MIT
66
Vendor: Microsoft Corporation
77
Distribution: Mariner
@@ -11,6 +11,7 @@ Source0: %{url}/archive/v%{version}.tar.gz#/%{name}-%{version}.tar.gz
1111
Patch0: 0000-Supress-implicit-fallthrough-in-GCC.patch
1212
Patch1: 0001-Onley-apply-to-GCC-7.patch
1313
Patch2: 0002-Correct-object-copying-in-document_h.patch
14+
Patch3: CVE-2024-38517.patch
1415
%global debug_package %{nil}
1516
BuildRequires: cmake
1617
BuildRequires: gcc
@@ -52,6 +53,9 @@ make test
5253
%{_datadir}
5354

5455
%changelog
56+
* Wed Jul 17 2024 Xiaohong Deng <xiaohongdeng@microsoft.com> - 1.1.0-8
57+
- Patch CVE-2024-38517
58+
5559
* Mon Apr 11 2022 Pawel Winogrodzki <pawelwi@microsoft.com> - 1.1.0-7
5660
- Fixing invalid source URL.
5761
- License verified.

0 commit comments

Comments
 (0)