Title: [288020] trunk/Source/WTF
Revision
288020
Author
[email protected]
Date
2022-01-14 10:10:50 -0800 (Fri, 14 Jan 2022)

Log Message

Fix WTF::UUID's potential collision with empty and deleted values
https://bugs.webkit.org/show_bug.cgi?id=235177

Reviewed by Chris Dumez.

Since cryptographicallyRandomValues generates random values, it is possible that it produces 0 or 1.
In that case, it conflicts with empty and deleted values of UUID for hash table. When it is generated
and used, it can break hash tables. This patch avoids that by picking 2 when we generate 0 or 1.

We also avoid using UInt128Impl and instead using UInt128. UInt128Impl is implementation, so we should
always use UInt128. UInt128 can be native __uint128_t when it is supported by the compiler: clang and
GCC on 64bit support it.

In this patch, we do not use strict UUID v4. This patch focuses on fixing the existing hash table corruption
bug, and we defer discussion about UUID v4 since it reduces usable bits from 128 to 122, which we need to
investigate the existing usage of UUID (and UUID string) in our tree.

* wtf/UUID.cpp:
(WTF::UUID::UUID):
* wtf/UUID.h:
(WTF::UUID::UUID):
(WTF::UUID::isHashTableDeletedValue const):
(WTF::UUID::encode const):
(WTF::UUID::decode):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (288019 => 288020)


--- trunk/Source/WTF/ChangeLog	2022-01-14 17:10:44 UTC (rev 288019)
+++ trunk/Source/WTF/ChangeLog	2022-01-14 18:10:50 UTC (rev 288020)
@@ -1,3 +1,30 @@
+2022-01-14  Yusuke Suzuki  <[email protected]>
+
+        Fix WTF::UUID's potential collision with empty and deleted values
+        https://bugs.webkit.org/show_bug.cgi?id=235177
+
+        Reviewed by Chris Dumez.
+
+        Since cryptographicallyRandomValues generates random values, it is possible that it produces 0 or 1.
+        In that case, it conflicts with empty and deleted values of UUID for hash table. When it is generated
+        and used, it can break hash tables. This patch avoids that by picking 2 when we generate 0 or 1.
+
+        We also avoid using UInt128Impl and instead using UInt128. UInt128Impl is implementation, so we should
+        always use UInt128. UInt128 can be native __uint128_t when it is supported by the compiler: clang and
+        GCC on 64bit support it.
+
+        In this patch, we do not use strict UUID v4. This patch focuses on fixing the existing hash table corruption
+        bug, and we defer discussion about UUID v4 since it reduces usable bits from 128 to 122, which we need to
+        investigate the existing usage of UUID (and UUID string) in our tree.
+
+        * wtf/UUID.cpp:
+        (WTF::UUID::UUID):
+        * wtf/UUID.h:
+        (WTF::UUID::UUID):
+        (WTF::UUID::isHashTableDeletedValue const):
+        (WTF::UUID::encode const):
+        (WTF::UUID::decode):
+
 2022-01-13  Elliott Williams  <[email protected]>
 
         [XCBuild] Add "product dependencies" which influence workspace build order

Modified: trunk/Source/WTF/wtf/UUID.cpp (288019 => 288020)


--- trunk/Source/WTF/wtf/UUID.cpp	2022-01-14 17:10:44 UTC (rev 288019)
+++ trunk/Source/WTF/wtf/UUID.cpp	2022-01-14 18:10:50 UTC (rev 288020)
@@ -45,7 +45,9 @@
 UUID::UUID()
 {
     static_assert(sizeof(m_data) == 16);
-    cryptographicallyRandomValues(reinterpret_cast<unsigned char*>(&m_data), 16);
+    do {
+        cryptographicallyRandomValues(reinterpret_cast<unsigned char*>(&m_data), 16);
+    } while (m_data == emptyValue || m_data == deletedValue);
 }
 
 unsigned UUID::hash() const

Modified: trunk/Source/WTF/wtf/UUID.h (288019 => 288020)


--- trunk/Source/WTF/wtf/UUID.h	2022-01-14 17:10:44 UTC (rev 288019)
+++ trunk/Source/WTF/wtf/UUID.h	2022-01-14 18:10:50 UTC (rev 288020)
@@ -40,6 +40,9 @@
 class UUID {
 WTF_MAKE_FAST_ALLOCATED;
 public:
+    static constexpr UInt128 emptyValue = 0;
+    static constexpr UInt128 deletedValue = 1;
+
     static UUID create()
     {
         return UUID { };
@@ -50,41 +53,38 @@
         memcpy(&m_data, span.data(), 16);
     }
 
-    explicit UUID(UInt128Impl&& data)
+    explicit constexpr UUID(UInt128 data)
         : m_data(data)
     {
     }
 
-    UUID(const UUID&) = default;
-
     Span<const uint8_t, 16> toSpan() const
     {
         return Span<const uint8_t, 16> { reinterpret_cast<const uint8_t*>(&m_data), 16 };
     }
 
-    UUID& operator=(const UUID&) = default;
     bool operator==(const UUID& other) const { return m_data == other.m_data; }
 
     template<class Encoder> void encode(Encoder&) const;
     template<class Decoder> static std::optional<UUID> decode(Decoder&);
 
-    explicit UUID(HashTableDeletedValueType)
-        : m_data(1)
+    explicit constexpr UUID(HashTableDeletedValueType)
+        : m_data(deletedValue)
     {
     }
 
-    explicit UUID(HashTableEmptyValueType)
-        : m_data(0)
+    explicit constexpr UUID(HashTableEmptyValueType)
+        : m_data(emptyValue)
     {
     }
 
-    bool isHashTableDeletedValue() const { return m_data == 1; }
+    bool isHashTableDeletedValue() const { return m_data == deletedValue; }
     WTF_EXPORT_PRIVATE unsigned hash() const;
 
 private:
     WTF_EXPORT_PRIVATE UUID();
 
-    UInt128Impl m_data;
+    UInt128 m_data;
 };
 
 struct UUIDHash {
@@ -103,7 +103,7 @@
 template<class Encoder>
 void UUID::encode(Encoder& encoder) const
 {
-    encoder << UInt128High64(m_data) << UInt128Low64(m_data);
+    encoder << static_cast<uint64_t>(m_data >> 64) << static_cast<uint64_t>(m_data);
 }
 
 template<class Decoder>
@@ -119,9 +119,7 @@
     if (!low)
         return std::nullopt;
 
-    return { UUID {
-        MakeUInt128(*high, *low),
-    } };
+    return UUID { (static_cast<UInt128>(*high) << 64) | *low };
 }
 
 // Creates a UUID that consists of 32 hexadecimal digits and returns its canonical form.
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to