Skip to content

Commit 9ea0d03

Browse files
committed
Add length check for read ChannelMonitor keys
1 parent 9abe4bb commit 9ea0d03

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

lightning/src/util/persist.rs

+9
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStore> Persist<ChannelSign
179179

180180
fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
181181
let key = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
182+
debug_assert!(key.len() > 65);
182183
match self.write(
183184
CHANNEL_MONITOR_PERSISTENCE_NAMESPACE,
184185
CHANNEL_MONITOR_PERSISTENCE_SUB_NAMESPACE,
@@ -191,6 +192,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStore> Persist<ChannelSign
191192

192193
fn update_persisted_channel(&self, funding_txo: OutPoint, _update: Option<&ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
193194
let key = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
195+
debug_assert!(key.len() > 65);
194196
match self.write(
195197
CHANNEL_MONITOR_PERSISTENCE_NAMESPACE,
196198
CHANNEL_MONITOR_PERSISTENCE_SUB_NAMESPACE,
@@ -216,6 +218,13 @@ where
216218
for stored_key in kv_store.list(
217219
CHANNEL_MONITOR_PERSISTENCE_NAMESPACE, CHANNEL_MONITOR_PERSISTENCE_SUB_NAMESPACE)?
218220
{
221+
debug_assert!(stored_key.len() > 65);
222+
if stored_key.len() < 65 {
223+
return Err(io::Error::new(
224+
io::ErrorKind::InvalidData,
225+
"Stored key has invalid length"));
226+
}
227+
219228
let txid = Txid::from_hex(stored_key.split_at(64).0).map_err(|_| {
220229
io::Error::new(io::ErrorKind::InvalidData, "Invalid tx ID in stored key")
221230
})?;

0 commit comments

Comments
 (0)