| 1 |
From a36b97ccf34e2b7e420655e806b9bc591f93e57d Mon Sep 17 00:00:00 2001 |
| 2 |
From: Kaylee Lubick <[email protected]> |
| 3 |
Date: Tue, 23 Jun 2026 13:35:54 +0000 |
| 4 |
Subject: [PATCH] Fix salt value used for expat in parsing XML |
| 5 |
|
| 6 |
We started setting the salt in [1] to avoid using the |
| 7 |
secure PRNG generator on Windows [2][3] but the choice of |
| 8 |
using a pointer was undesirable (see linked bug). |
| 9 |
|
| 10 |
This uses SkRandom and the time to avoid leaking info while |
| 11 |
using a hard-to-guess salt to mitigate the original |
| 12 |
hash-flooding DOS attack without causing additional problems. |
| 13 |
SkRandom is portable and doesn't use any external sources of |
| 14 |
entropy, avoiding the original problem. |
| 15 |
|
| 16 |
[1] https://review.skia.org/730076 |
| 17 |
|
| 18 |
Bug: https://issues.chromium.org/issues/506377118 |
| 19 |
Fixed: 506377118 |
| 20 |
Change-Id: Ib059d2e4da6bca5e5c4c2a284d4de5693f307890 |
| 21 |
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1268116 |
| 22 |
Auto-Submit: Kaylee Lubick <[email protected]> |
| 23 |
Commit-Queue: Florin Malita <[email protected]> |
| 24 |
Reviewed-by: Florin Malita <[email protected]> |
| 25 |
--- |
| 26 |
src/xml/SkXMLParser.cpp | 37 +++++++++++++++++++++++++++++-------- |
| 27 |
1 file changed, 29 insertions(+), 8 deletions(-) |
| 28 |
|
| 29 |
--- a/src/xml/SkXMLParser.cpp |
| 30 |
+++ b/src/xml/SkXMLParser.cpp |
| 31 |
@@ -11,6 +11,9 @@ |
| 32 |
#include "include/private/base/SkTemplates.h" |
| 33 |
#include "include/private/base/SkTo.h" |
| 34 |
#include "src/xml/SkXMLParser.h" |
| 35 |
+#include "src/base/SkRandom.h" |
| 36 |
+#include "src/base/SkTime.h" |
| 37 |
+#include "src/base/SkUtils.h" |
| 38 |
|
| 39 |
#include <expat.h> |
| 40 |
|
| 41 |
@@ -59,7 +62,24 @@ |
| 42 |
|
| 43 |
namespace { |
| 44 |
|
| 45 |
-constexpr const void* kHashSeed = &kHashSeed; |
| 46 |
+// Return a random number based on the clock to be consistent |
| 47 |
+// across platforms yet hard to guess and not leaking information. |
| 48 |
+static uint32_t get_hash_salt() { |
| 49 |
+ static const uint32_t s_salt = []() { |
| 50 |
+ double nsecs = SkTime::GetNSecs(); |
| 51 |
+ uint64_t timeVal = sk_bit_cast<uint64_t>(nsecs); |
| 52 |
+ uint32_t seed = static_cast<uint32_t>(timeVal ^ timeVal >> 32); |
| 53 |
+ |
| 54 |
+ SkRandom rand(seed); |
| 55 |
+ uint32_t salt = rand.nextU(); |
| 56 |
+ while (salt == 0) [[unlikely]] { |
| 57 |
+ // Expat hash seed must be non-zero or the default cPRNG will be used. |
| 58 |
+ salt = rand.nextU(); |
| 59 |
+ } |
| 60 |
+ return salt; |
| 61 |
+ }(); |
| 62 |
+ return s_salt; |
| 63 |
+} |
| 64 |
|
| 65 |
const XML_Memory_Handling_Suite sk_XML_alloc = { |
| 66 |
sk_malloc_throw, |
| 67 |
@@ -149,12 +169,13 @@ |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
- // Avoid calls to rand_s if this is not set. This seed helps prevent DOS |
| 72 |
- // with a known hash sequence so an address is sufficient. The provided |
| 73 |
- // seed should not be zero as that results in a call to rand_s. |
| 74 |
- unsigned long seed = static_cast<unsigned long>( |
| 75 |
- reinterpret_cast<size_t>(kHashSeed) & 0xFFFFFFFF); |
| 76 |
- XML_SetHashSalt(ctx.fXMLParser, seed ? seed : 1); |
| 77 |
+ // Expat calls to rand_s if no salt is set, which is not allowed on Windows. |
| 78 |
+ // https://crbug.com/40088338 |
| 79 |
+ // Setting the salt helps prevent DOS with a known hash sequence. Generating a |
| 80 |
+ // random salt once per process oursevles ensures the salt is non-deterministic |
| 81 |
+ // and unguessable, while preventing calls to rand_s inside sandboxed processes |
| 82 |
+ // on Windows. |
| 83 |
+ XML_SetHashSalt(ctx.fXMLParser, get_hash_salt()); |
| 84 |
|
| 85 |
XML_SetUserData(ctx.fXMLParser, &ctx); |
| 86 |
XML_SetElementHandler(ctx.fXMLParser, start_element_handler, end_element_handler); |