Every connection callback overwrites the same device reference. Two centrals at once, and notifications meant for the first can land on the second.
What goes wrong
The peripheral holds a single field for the connected peer. Every time a connection callback fires, it writes the new device into that field, no questions asked. There is no check that a peer is already connected, and no binding between the connection and an authenticated session.
Now let two centrals connect close together. The second callback overwrites the reference while the first peer is still around expecting data. When the peripheral pushes a notification, it goes to whatever device sits in that field right now, which may be the wrong one. Application data intended for the first peer is delivered to the second.
Bluetooth makes concurrent and rapid reconnects normal, so this is not a corner case you have to squint to see. The bug is state that is global where it should be per-connection, mutated by a callback that assumes there is only ever one peer.
The pattern
device = None # one field for the whole peripheral
def onConnectionChange(peer, state):
if state == CONNECTED:
device = peer # overwrites any prior peer
def notify(payload):
device.write(CHAR, payload) # goes to the latest peersessions = {} # keyed per connection
def onConnectionChange(peer, state):
if state == CONNECTED:
if activeSessionFor(peer) is None:
sessions[peer.id] = newSession(peer)
def notify(peerId, payload):
s = sessions.get(peerId)
if s and s.isAuthenticated():
s.peer.write(CHAR, payload) # only that peerEvery gate held. The exploit path was reproduced against a negative control, the impact was calibrated, and the finding was signed report-ready.
Why it matters to you
Anywhere a callback stashes 'the current thing' in a shared field, a second event can move it under your feet. On BLE that shared field decides who receives your data. The exposure is bounded by what those notifications carry and by radio range, which keeps this at a medium rather than a fire drill, but proximity attacks are cheap and the data path is real.
Model each connection as its own session with its own state, and address notifications to a specific peer rather than to whoever happens to be current.
Check your own code
- Find connection handlers that write to a shared, class-level field
- Key session state per connection, not one mutable reference
- Reject or isolate a second peer instead of overwriting the first
- Address every notification to a specific, authenticated session