Skip to content

Commit b2c28a6

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

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

lightning/src/util/persist.rs

+8
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,12 @@ where
216218
for stored_key in kv_store.list(
217219
CHANNEL_MONITOR_PERSISTENCE_NAMESPACE, CHANNEL_MONITOR_PERSISTENCE_SUB_NAMESPACE)?
218220
{
221+
if stored_key.len() < 65 {
222+
return Err(io::Error::new(
223+
io::ErrorKind::InvalidData,
224+
"Stored key has invalid length"));
225+
}
226+
219227
let txid = Txid::from_hex(stored_key.split_at(64).0).map_err(|_| {
220228
io::Error::new(io::ErrorKind::InvalidData, "Invalid tx ID in stored key")
221229
})?;

0 commit comments

Comments
 (0)