Skip to content

lightningd/peer_control: initialize error pointer in handle_peer_spoke#9001

Open
ThomsenDrake wants to merge 1 commit intoElementsProject:masterfrom
ThomsenDrake:fix/peer-control-uninitialized-ptr-8849
Open

lightningd/peer_control: initialize error pointer in handle_peer_spoke#9001
ThomsenDrake wants to merge 1 commit intoElementsProject:masterfrom
ThomsenDrake:fix/peer-control-uninitialized-ptr-8849

Conversation

@ThomsenDrake
Copy link
Copy Markdown

Summary

Fixes #8849

The Bug

In lightningd/peer_control.c, the handle_peer_spoke() function declares a local pointer variable error without initialization:

const u8 *error;

Several error paths jump to the send_error label where error is dereferenced — specifically passed to tal_hex(tmpctx, error) and towire_connectd_peer_send_msg(). If error is not initialized before reaching send_error, this is undefined behavior (likely a segfault).

The affected goto send_error paths are all on sockpair() failure:

  • Line ~2019 (CLOSINGD_COMPLETE state, reestablish path)
  • Line ~2063 (DUALOPEND_AWAITING_LOCKIN restart path)
  • Line ~2100 (WIRE_OPEN_CHANNEL new channel path)
  • Line ~2119 (WIRE_OPEN_CHANNEL2 dual-fund path)

While sockpair() currently does set the error pointer via its output parameter on failure, the uninitialized declaration is still undefined behavior and fragile against future code changes.

The Fix

Initialize error to NULL at declaration:

const u8 *error = NULL;

This is the minimal, defensive fix that prevents undefined behavior regardless of which path reaches send_error.

The local variable `error` in handle_peer_spoke() is declared as a
pointer type with no initialization. Several error paths jump to the
`send_error` label where `error` is dereferenced (passed to
tal_hex() and towire_connectd_peer_send_msg()). While sockpair()
currently sets the `error` pointer via the output parameter on
failure, the declaration should be initialized to NULL as a defensive
measure and to avoid undefined behavior if code paths change.

Fixes ElementsProject#8849
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.

Error paths in handle_peer_spoke(…) in lightningd/peer_control.c dereference an uninitialized pointer

1 participant