Skip to content

Conversation

maflcko
Copy link

@maflcko maflcko commented Jan 14, 2025

It is UB to exceed the bounds of the buffer when doing pointer arithemetic. That means the following is not a valid bounds check:

if (start + 4 <= limit)

Because if we were at the end of the buffer, we wouldn't be allowed to add 4 anyway. Instead, this must be written as:

if (limit - start >= 4)

Basic forms of this issue are flagged by UBSan. If building with -fsanitize=undefined, the following test trips an error:

[ RUN      ] HASH.SignedUnsignedIssue
.../leveldb/util/hash.cc:30:15: runtime error: applying non-zero offset 4 to null pointer
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/local/google/home/davidben/leveldb/util/hash.cc:30:15 in
[       OK ] HASH.SignedUnsignedIssue (1 ms)

(cherry picked from commit 578eeb7)

It is UB to exceed the bounds of the buffer when doing pointer
arithemetic. That means the following is not a valid bounds check:

    if (start + 4 <= limit)

Because if we were at the end of the buffer, we wouldn't be
allowed to add 4 anyway. Instead, this must be written as:

    if (limit - start >= 4)

Basic forms of this issue are flagged by UBSan. If building with
-fsanitize=undefined, the following test trips an error:

    [ RUN      ] HASH.SignedUnsignedIssue
    .../leveldb/util/hash.cc:30:15: runtime error: applying non-zero offset 4 to null pointer
    SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/local/google/home/davidben/leveldb/util/hash.cc:30:15 in
    [       OK ] HASH.SignedUnsignedIssue (1 ms)

(cherry picked from commit 578eeb7)
@l0rinc
Copy link

l0rinc commented Jan 14, 2025

ACK a8844b2

@maflcko
Copy link
Author

maflcko commented Jan 14, 2025

For reference, I haven't run the leveldb unit tests under ubsan to test this.

I have tried to run the Bitcoin Core msan CI to reproduce this similar to bitcoin/bitcoin#31655 (comment) with a diff:

diff --git a/src/leveldb/util/hash.cc b/src/leveldb/util/hash.cc
index dd47c110ee..41b3e8734a 100644
--- a/src/leveldb/util/hash.cc
+++ b/src/leveldb/util/hash.cc
@@ -5,6 +5,7 @@
 #include "util/hash.h"
 
 #include <string.h>
+#include <span>
 
 #include "util/coding.h"
 
@@ -19,7 +20,8 @@
 
 namespace leveldb {
 
-uint32_t Hash(const char* data, size_t n, uint32_t seed) {
+uint32_t Hash(const char* old, size_t n, uint32_t seed) {
+auto data{std::span{old,n}.data()};
   // Similar to murmur hash
   const uint32_t m = 0xc6a4a793;
   const uint32_t r = 24;

However, the CI passed.

Copy link
Member

@fanquake fanquake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK a8844b2

@fanquake fanquake merged commit 04b5790 into bitcoin-core:bitcoin-fork Jan 16, 2025
fanquake added a commit to fanquake/bitcoin that referenced this pull request Jan 16, 2025
04b5790928 Merge bitcoin-core/leveldb-subtree#46: Fix invalid pointer arithmetic in Hash (bitcoin#1222)
59669817c5 Merge bitcoin-core/leveldb-subtree#40: cherry-pick: Remove leveldb::port::kLittleEndian.
73013d1a37 Merge bitcoin-core/leveldb-subtree#45: [jumbo] Add begin()/end() to Slice.
a8844b23ab Fix invalid pointer arithmetic in Hash (bitcoin#1222)
be4dfc94b3 [jumbo] Add begin()/end() to Slice.
2e3c0131d3 Remove leveldb::port::kLittleEndian.

git-subtree-dir: src/leveldb
git-subtree-split: 04b57909285c7335c1908d53bcde9b90fe0439be
@maflcko maflcko deleted the 2501-less-ub-hash branch January 16, 2025 11:32
fanquake added a commit to bitcoin/bitcoin that referenced this pull request Jan 21, 2025
910a11f build: remove LEVELDB_IS_BIG_ENDIAN (fanquake)
d336b7a Squashed 'src/leveldb/' changes from 688561cba8..04b5790928 (fanquake)

Pull request description:

  Includes:
  * bitcoin-core/leveldb-subtree#40 (used in #29852)
  * bitcoin-core/leveldb-subtree#45
  * bitcoin-core/leveldb-subtree#46

ACKs for top commit:
  kevkevinpal:
    Concept ACK [910a11f](910a11f)
  l0rinc:
    ACK 910a11f
  hebasto:
    ACK 910a11f, I've performed a subtree update locally and got the same changes.
  theuni:
    utACK 910a11f

Tree-SHA512: c5a2224c67d3fd598bc682589b805c324abf91003032a85764766048030285f56154779f29d3f0b3673c8f7f497ae62de5fc6b95ef0b022c873750053c7d27d5
janus pushed a commit to BitgesellOfficial/bitgesell that referenced this pull request Sep 1, 2025
04b5790928 Merge bitcoin-core/leveldb-subtree#46: Fix invalid pointer arithmetic in Hash (#1222)
59669817c5 Merge bitcoin-core/leveldb-subtree#40: cherry-pick: Remove leveldb::port::kLittleEndian.
73013d1a37 Merge bitcoin-core/leveldb-subtree#45: [jumbo] Add begin()/end() to Slice.
a8844b23ab Fix invalid pointer arithmetic in Hash (#1222)
be4dfc94b3 [jumbo] Add begin()/end() to Slice.
2e3c0131d3 Remove leveldb::port::kLittleEndian.

git-subtree-dir: src/leveldb
git-subtree-split: 04b57909285c7335c1908d53bcde9b90fe0439be
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants