Skip to content

Commit bceca1e

Browse files
committed
Fix reachable unwrap on non-channel_type manual channel acceptance
If we receive an `OpenChannel` message without a `channel_type` with `manually_accept_inbound_channels` set, we will `unwrap()` `None`. This is uncommon these days as most nodes support `channel_type`, but sadly is rather trivial for a peer to hit for those with manual channel acceptance enabled. Reported in and fixes #2804. Luckily, the updated `full_stack_target` has no issue reaching this issue quickly.
1 parent 3b6a361 commit bceca1e

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

lightning/src/ln/channel.rs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6845,6 +6845,41 @@ pub(super) struct InboundV1Channel<SP: Deref> where SP::Target: SignerProvider {
68456845
pub unfunded_context: UnfundedChannelContext,
68466846
}
68476847

6848+
/// Fetches the [`ChannelTypeFeatures`] that will be used for a channel built from a given
6849+
/// [`msgs::OpenChannel`].
6850+
pub(super) fn channel_type_from_open_channel(
6851+
msg: &msgs::OpenChannel, their_features: &InitFeatures,
6852+
our_supported_features: &ChannelTypeFeatures
6853+
) -> Result<ChannelTypeFeatures, ChannelError> {
6854+
if let Some(channel_type) = &msg.channel_type {
6855+
if channel_type.supports_any_optional_bits() {
6856+
return Err(ChannelError::Close("Channel Type field contained optional bits - this is not allowed".to_owned()));
6857+
}
6858+
6859+
// We only support the channel types defined by the `ChannelManager` in
6860+
// `provided_channel_type_features`. The channel type must always support
6861+
// `static_remote_key`.
6862+
if !channel_type.requires_static_remote_key() {
6863+
return Err(ChannelError::Close("Channel Type was not understood - we require static remote key".to_owned()));
6864+
}
6865+
// Make sure we support all of the features behind the channel type.
6866+
if !channel_type.is_subset(our_supported_features) {
6867+
return Err(ChannelError::Close("Channel Type contains unsupported features".to_owned()));
6868+
}
6869+
let announced_channel = if (msg.channel_flags & 1) == 1 { true } else { false };
6870+
if channel_type.requires_scid_privacy() && announced_channel {
6871+
return Err(ChannelError::Close("SCID Alias/Privacy Channel Type cannot be set on a public channel".to_owned()));
6872+
}
6873+
Ok(channel_type.clone())
6874+
} else {
6875+
let channel_type = ChannelTypeFeatures::from_init(&their_features);
6876+
if channel_type != ChannelTypeFeatures::only_static_remote_key() {
6877+
return Err(ChannelError::Close("Only static_remote_key is supported for non-negotiated channel types".to_owned()));
6878+
}
6879+
Ok(channel_type)
6880+
}
6881+
}
6882+
68486883
impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
68496884
/// Creates a new channel from a remote sides' request for one.
68506885
/// Assumes chain_hash has already been checked and corresponds with what we expect!
@@ -6863,32 +6898,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
68636898

68646899
// First check the channel type is known, failing before we do anything else if we don't
68656900
// support this channel type.
6866-
let channel_type = if let Some(channel_type) = &msg.channel_type {
6867-
if channel_type.supports_any_optional_bits() {
6868-
return Err(ChannelError::Close("Channel Type field contained optional bits - this is not allowed".to_owned()));
6869-
}
6870-
6871-
// We only support the channel types defined by the `ChannelManager` in
6872-
// `provided_channel_type_features`. The channel type must always support
6873-
// `static_remote_key`.
6874-
if !channel_type.requires_static_remote_key() {
6875-
return Err(ChannelError::Close("Channel Type was not understood - we require static remote key".to_owned()));
6876-
}
6877-
// Make sure we support all of the features behind the channel type.
6878-
if !channel_type.is_subset(our_supported_features) {
6879-
return Err(ChannelError::Close("Channel Type contains unsupported features".to_owned()));
6880-
}
6881-
if channel_type.requires_scid_privacy() && announced_channel {
6882-
return Err(ChannelError::Close("SCID Alias/Privacy Channel Type cannot be set on a public channel".to_owned()));
6883-
}
6884-
channel_type.clone()
6885-
} else {
6886-
let channel_type = ChannelTypeFeatures::from_init(&their_features);
6887-
if channel_type != ChannelTypeFeatures::only_static_remote_key() {
6888-
return Err(ChannelError::Close("Only static_remote_key is supported for non-negotiated channel types".to_owned()));
6889-
}
6890-
channel_type
6891-
};
6901+
let channel_type = channel_type_from_open_channel(msg, their_features, our_supported_features)?;
68926902

68936903
let channel_keys_id = signer_provider.generate_channel_keys_id(true, msg.funding_satoshis, user_id);
68946904
let holder_signer = signer_provider.derive_channel_signer(msg.funding_satoshis, channel_keys_id);

lightning/src/ln/channelmanager.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, Messa
4343
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
4444
// construct one themselves.
4545
use crate::ln::{inbound_payment, ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
46-
use crate::ln::channel::{Channel, ChannelPhase, ChannelContext, ChannelError, ChannelUpdateStatus, ShutdownResult, UnfundedChannelContext, UpdateFulfillCommitFetch, OutboundV1Channel, InboundV1Channel, WithChannelContext};
46+
use crate::ln::channel::{self, Channel, ChannelPhase, ChannelContext, ChannelError, ChannelUpdateStatus, ShutdownResult, UnfundedChannelContext, UpdateFulfillCommitFetch, OutboundV1Channel, InboundV1Channel, WithChannelContext};
4747
use crate::ln::features::{Bolt12InvoiceFeatures, ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
4848
#[cfg(any(feature = "_test_utils", test))]
4949
use crate::ln::features::Bolt11InvoiceFeatures;
@@ -6170,13 +6170,21 @@ where
61706170

61716171
// If we're doing manual acceptance checks on the channel, then defer creation until we're sure we want to accept.
61726172
if self.default_configuration.manually_accept_inbound_channels {
6173+
let channel_type_res = channel::channel_type_from_open_channel(
6174+
&msg, &peer_state.latest_features, &self.channel_type_features()
6175+
);
6176+
let channel_type = match channel_type_res {
6177+
Ok(channel_type) => channel_type,
6178+
Err(e) =>
6179+
return Err(MsgHandleErrInternal::from_chan_no_close(e, msg.temporary_channel_id)),
6180+
};
61736181
let mut pending_events = self.pending_events.lock().unwrap();
61746182
pending_events.push_back((events::Event::OpenChannelRequest {
61756183
temporary_channel_id: msg.temporary_channel_id.clone(),
61766184
counterparty_node_id: counterparty_node_id.clone(),
61776185
funding_satoshis: msg.funding_satoshis,
61786186
push_msat: msg.push_msat,
6179-
channel_type: msg.channel_type.clone().unwrap(),
6187+
channel_type,
61806188
}, None));
61816189
peer_state.inbound_channel_request_by_id.insert(channel_id, InboundChannelRequest {
61826190
open_channel_msg: msg.clone(),

0 commit comments

Comments
 (0)