Skip to content

Commit 712d97d

Browse files
committed
Add DecodeError::DangerousValue for decoding invalid channel managers
This would help distinguish different types of errors when deserialzing a channel manager. InvalidValue was used previously but this could be because it is an old serialization format, whereas DangerousValue is a lot more clear on why the deserialization failed.
1 parent 5e41425 commit 712d97d

File tree

5 files changed

+16
-3
lines changed

5 files changed

+16
-3
lines changed

fuzz/src/router.rs

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
157157
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
158158
msgs::DecodeError::Io(e) => panic!("{:?}", e),
159159
msgs::DecodeError::UnsupportedCompression => return,
160+
msgs::DecodeError::DangerousValue => return,
160161
}
161162
}
162163
}}

lightning/src/ln/channelmanager.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10918,7 +10918,7 @@ where
1091810918
}
1091910919
}
1092010920
if chan.get_latest_unblocked_monitor_update_id() > max_in_flight_update_id {
10921-
// If the channel is ahead of the monitor, return InvalidValue:
10921+
// If the channel is ahead of the monitor, return DangerousValue:
1092210922
log_error!(logger, "A ChannelMonitor is stale compared to the current ChannelManager! This indicates a potentially-critical violation of the chain::Watch API!");
1092310923
log_error!(logger, " The ChannelMonitor for channel {} is at update_id {} with update_id through {} in-flight",
1092410924
chan.context.channel_id(), monitor.get_latest_update_id(), max_in_flight_update_id);
@@ -10927,7 +10927,7 @@ where
1092710927
log_error!(logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
1092810928
log_error!(logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");
1092910929
log_error!(logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
10930-
return Err(DecodeError::InvalidValue);
10930+
return Err(DecodeError::DangerousValue);
1093110931
}
1093210932
} else {
1093310933
// We shouldn't have persisted (or read) any unfunded channel types so none should have been

lightning/src/ln/msgs.rs

+11
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ pub enum DecodeError {
9191
Io(io::ErrorKind),
9292
/// The message included zlib-compressed values, which we don't support.
9393
UnsupportedCompression,
94+
/// Value is validly encoded but is dangerous to use.
95+
///
96+
/// This is used for things like [`ChannelManager`] deserialization where we want to ensure
97+
/// that we don't use a [`ChannelManager`] which is in out of sync with the [`ChannelMonitor`].
98+
/// This indicates that there is a critical implementation flaw in the storage implementation
99+
/// and it's unsafe to continue.
100+
///
101+
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
102+
/// [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
103+
DangerousValue,
94104
}
95105

96106
/// An [`init`] message to be sent to or received from a peer.
@@ -1796,6 +1806,7 @@ impl fmt::Display for DecodeError {
17961806
DecodeError::BadLengthDescriptor => f.write_str("A length descriptor in the packet didn't describe the later data correctly"),
17971807
DecodeError::Io(ref e) => fmt::Debug::fmt(e, f),
17981808
DecodeError::UnsupportedCompression => f.write_str("We don't support receiving messages with zlib-compressed fields"),
1809+
DecodeError::DangerousValue => f.write_str("Value would be dangerous to continue execution with"),
17991810
}
18001811
}
18011812
}

lightning/src/ln/peer_handler.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
15511551
}
15521552
(msgs::DecodeError::BadLengthDescriptor, _) => return Err(PeerHandleError { }),
15531553
(msgs::DecodeError::Io(_), _) => return Err(PeerHandleError { }),
1554+
(msgs::DecodeError::DangerousValue, _) => return Err(PeerHandleError { }),
15541555
}
15551556
}
15561557
};

lightning/src/ln/reload_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
412412
}
413413

414414
let mut nodes_0_read = &nodes_0_serialized[..];
415-
if let Err(msgs::DecodeError::InvalidValue) =
415+
if let Err(msgs::DecodeError::DangerousValue) =
416416
<(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestRouter, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
417417
default_config: UserConfig::default(),
418418
entropy_source: keys_manager,

0 commit comments

Comments
 (0)