Skip to content

net: signed-integer-overflow in LocalServiceInfo #24049

Closed

Description

@Crypt-iQ

LocalServiceInfo is defined as:

bitcoin/src/net.h

Lines 228 to 231 in 16781e1

struct LocalServiceInfo {
int nScore;
uint16_t nPort;
};

nScore is of type int which is signed and is 4 bytes wide on my machine (clang-12). nScore is incremented by SeenLocal during the version handshake here:

bitcoin/src/net_processing.cpp

Lines 2603 to 2606 in 16781e1

if (pfrom.IsInboundConn() && addrMe.IsRoutable())
{
SeenLocal(addrMe);
}

So I believe this UB is peer-triggerable (I don't think the compiler can detect this), but I don't think it's a big deal unless there's a node out there compiled with UBSAN or ftrapv. It seems like nScore can just be changed to int64_t.

Activity

  1. fanquake commented on Aug 8, 2022

    @fanquake
    Member

    #24090 was closed with no comment. @Crypt-iQ do you want to follow up here?

  2. Crypt-iQ commented on Aug 8, 2022

    @Crypt-iQ
    ContributorAuthor

    I won't have time to work on this unfortunately

  3. fanquake commented on Aug 9, 2022

    @fanquake
    Member

    Ok. Labelling this "Up for grabs" for now.

  4. ViralTaco commented on Sep 29, 2022

    @ViralTaco

    I have a potential fix… It's ugly, though. And it looks like it should be UB, but I can't find a reason why.
    The function which may invoke UB is (currently) defined like so:

    bitcoin/src/net.cpp

    Lines 344 to 352 in 5b6f0f3

    /** vote for a local address */
    bool SeenLocal(const CService& addr)
    {
    LOCK(g_maplocalhost_mutex);
    const auto it = mapLocalHost.find(addr);
    if (it == mapLocalHost.end()) return false;
    ++it->second.nScore;
    return true;
    }

    ++reinterpret_cast <unsigned&> (it->second.nScore);

    It's ugly, I know… But it leaves everything else in this repo untouched.

  5. added a commit that references this issue on Oct 27, 2022
    474504f
  6. ViralTaco commented on Nov 24, 2022

    @ViralTaco

    Replying to #26399 (comment):

    The burden to […] motivate a change […] is on the author.

    The patch makes the behavior of the code defined. (See: [intro.compliance.general], [intro.abstract])
    It should do so, without any conversions.

    I spent over a hundred hours trying to prove it couldn't be done.
    First, I asked more senior engineers, chatted llvm devs to know it could [be an asan bug], then finally posted on stackoverflow.

    You […] ignored previous discussions, ignored two review comments

    I did? I'll have to look into that. I agree with the rest of your comment, though.

    I have very little free time, and this isn't how I want to spend it.
    Please don't mistake my frustration for arrogance.

  7. DevAgrawal1112 commented on Apr 1, 2023

    @DevAgrawal1112

    Since I am new to open source, I would like to know if I need to change int to int64_t for nScore or if I have to work on something else.

  8. added a commit that references this issue on Apr 1, 2023
  9. added a commit that references this issue on Jun 10, 2026
    53b836c
  10. added a commit that references this issue on Jun 12, 2026
    f8ed7ac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions