Skip to contentSkip to conversation

wireguard-nt 1.1: 0xD1 in PubkeyHashtableLookup (double evaluation of PubkeyBucket)

DISCUSSION 丁梓豪 1 participant 1 message Oct 26

lore
Share & tools
lore

丁梓豪

author
#0 lore raw

Hi,

I would like to report a 0xD1 in wireguard.sys 1.1.0.0 (wireguard-nt 1.1, tag fef7bc4) and a root cause I believe is in PubkeyHashtableLookup().

Two crash dumps from the same machine (10 days apart) show the same instruction and the same arguments, so this is deterministic code and not random memory corruption.

== Environment ==
- Windows 11 Pro 26200 (10.0.26200.9168), x64, 8 logical CPUs
- wireguard.sys 1.1.0.0, timestamp Thu May 7 04:35:56 2026 (0x69FBA62C), checksum 0x887D0, PDB stripped (CodeView signature zero)
- loaded through the embeddable wireguard.dll 1.1
- the application is a WireGuard-based mesh VPN that frequently removes and re-adds peers to force an immediate handshake after an endpoint change (updating the endpoint does not reset the handshake retransmit timer). Traffic is continuous while this happens. Any application that removes peers while handshakes are in flight could hit the same race.

== Crashes ==
Both are DRIVER_IRQL_NOT_LESS_OR_EQUAL (0xD1):

  2026-10-09 17:26:09 +08:00
    0xD1 (0xfffffffffffffea8, 0x0000000000000002,
          0x0000000000000000, 0xfffff8039ce1f8d3)
    wireguard base 0xfffff8039ce00000 -> offset 0x1f8d3
  2026-10-19 09:10:30 +08:00
    0xD1 (0xfffffffffffffea8, 0x0000000000000002,
          0x0000000000000000, 0xfffff801669df8d3)
    wireguard base 0xfffff801669c0000 -> offset 0x1f8d3

Arg1 = -0x158, Arg2 = 2 (DISPATCH_LEVEL), Arg3 = 0 (read).

== Crash site ==
!analyze -v:

FAULTING_IP:
wireguard+1f8d3
fffff801`669df8d3 483b0411 cmp rax,qword ptr [rcx+rdx]

Surrounding code (inlined memcmp):

  wireguard+0x1f8d0  488b01      mov rax,[rcx]
  wireguard+0x1f8d3  483b0411    cmp rax,[rcx+rdx]

...

Raw stack return addresses (innermost first):

wireguard+0x1f8d3
wireguard+0x174a5
wireguard+0x15463
wireguard+0x18fdf
tcpip!UdpDeliverDatagrams

0x174a5 is inside PubkeyHashtableLookup(). Disassembly there:

call siphash ; first evaluation of PubkeyBucket()
mov r9,[bucket]
test r9,r9

  je   done                 ; first load is checked for NULL
  call siphash              ; second evaluation of PubkeyBucket()
  mov  rbx,[bucket]         ; second load is NOT checked
  add  rbx,-5E0h            ; CONTAINING_RECORD(..., WG_PEER, PubkeyHash)
  lea  rdx,[rbx+488h]       ; &peer->Handshake.RemoteStatic

mov r8d,20h
call memcmp

== Root cause ==
driver/peerlookup.c:

HLIST_FOR_EACH_ENTRY_RCU (IterPeer, PubkeyBucket(Table, Pubkey), PubkeyHash)

driver/containers.h:

  #define HlistEntry(Ptr, Type, Member) CONTAINING_RECORD(Ptr, Type, Member)
  #define HlistEntrySafe(Ptr, Type, Member) ((Ptr) ? HlistEntry(Ptr, Type, Member) : NULL)
  #define HLIST_FOR_EACH_ENTRY_RCU(Pos, Head, Member) \
      for (Pos = HlistEntrySafe(RcuDereference(HlistFirstRcu(Head)), ...); Pos; \
           Pos = HlistEntrySafe(RcuDereference(HlistNextRcu(&(Pos)->Member)), ...))

HlistEntrySafe() evaluates Ptr twice. Here Ptr contains PubkeyBucket(Table, Pubkey), which calls Siphash() and indexes the table, so the complete bucket lookup (SipHash + bucket read) is evaluated twice. The first result is checked for NULL, the second is used unchecked.

If a concurrent PeerRemove() empties the bucket between the two evaluations, the second read returns NULL, CONTAINING_RECORD(NULL, WG_PEER, PubkeyHash) becomes -0x5E0, and &peer->Handshake.RemoteStatic becomes -0x5E0 + 0x488 = -0x158. memcmp() then reads -0x158 at DISPATCH_LEVEL, which matches Arg1/Arg2/Arg3.

Racing paths:
PubkeyHashtableLookup()
<- NoiseHandshakeConsumeInitiation() (receive.c:87, noise.c:644)
<- NoiseHandshakeConsumeResponse() (receive.c:107, noise.c:740)
PubkeyHashtableRemove()
<- PeerRemove()
<- WG_IOCTL_PEER_REMOVE (ioctl.c)

The lookup is RCU/lock-free and is not serialized against PeerRemove(). Data packets use IndexHashtableLookup(), not the public-key table.

The PDB is stripped (CodeView signature zero), so the offsets above come from the disassembly; 0x5E0 and 0x488 are consistent across both dumps and explain Arg1 exactly.

== Suggested fix ==
Evaluate the bucket only once:

  HLIST_HEAD *Bucket = PubkeyBucket(Table, Pubkey);
  HLIST_FOR_EACH_ENTRY_RCU (IterPeer, Bucket, PubkeyHash) { ... }

MSVC has no statement expressions, so a local variable at the call site is the simplest safe form. This keeps the lookup lock-free.

== Suggested API improvement (separate issue) ==
Applications that need a forced re-handshake currently have to remove and re-add the peer, because SetConfiguration(endpoint=...) does not reset TimerRetransmitHandshake/TimerHandshakeAttempts. A WIREGUARD_PEER_FLAG_RESET (clear keypairs and handshake timers, keep the peer in PeerList/PeerHashtable/AllowedIPs) would allow forced re-handshake without touching the hash table, and would avoid this race class.

== Evidence ==
I can provide on request:
- both minidumps (~3.4 MB and ~3.6 MB),
- full !analyze -v output,
- disassembly of +0x174a5 and +0x1f8d3,
- WER reports.

The list may strip large attachments; please tell me where to send them and I will send them off-list, or provide a temporary link.

I am happy to test a patched driver/DLL build on Windows 11 26200 with continuous traffic and repeated peer add/remove.

Thanks,
Ding Zihao