Skip to content

Commit c793549

Browse files
Fix scid_utils::is_valid* false positive
cargo bench was able to find an scid of 0 as a valid fake scid
1 parent 203394f commit c793549

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

lightning/src/ln/channelmanager.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2313,7 +2313,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
23132313
None => { // unknown_next_peer
23142314
// Note that this is likely a timing oracle for detecting whether an scid is a
23152315
// phantom.
2316-
if fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, *short_channel_id) {
2316+
if fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, *short_channel_id, &self.genesis_hash) {
23172317
None
23182318
} else {
23192319
break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
@@ -3202,7 +3202,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
32023202
}
32033203
if let PendingHTLCRouting::Forward { onion_packet, .. } = routing {
32043204
let phantom_secret_res = self.keys_manager.get_node_secret(Recipient::PhantomNode);
3205-
if phantom_secret_res.is_ok() && fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, short_chan_id) {
3205+
if phantom_secret_res.is_ok() && fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, short_chan_id, &self.genesis_hash) {
32063206
let phantom_shared_secret = SharedSecret::new(&onion_packet.public_key.unwrap(), &phantom_secret_res.unwrap()).secret_bytes();
32073207
let next_hop = match onion_utils::decode_next_payment_hop(phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac, payment_hash) {
32083208
Ok(res) => res,

lightning/src/util/scid_utils.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub(crate) mod fake_scid {
7373
use core::convert::TryInto;
7474
use core::ops::Deref;
7575

76-
const TEST_SEGWIT_ACTIVATION_HEIGHT: u32 = 0;
76+
const TEST_SEGWIT_ACTIVATION_HEIGHT: u32 = 1;
7777
const MAINNET_SEGWIT_ACTIVATION_HEIGHT: u32 = 481_824;
7878
const MAX_TX_INDEX: u32 = 2_500;
7979
const MAX_NAMESPACES: u8 = 8; // We allocate 3 bits for the namespace identifier.
@@ -151,12 +151,13 @@ pub(crate) mod fake_scid {
151151
}
152152

153153
/// Returns whether the given fake scid falls into the given namespace.
154-
pub fn is_valid_phantom(fake_scid_rand_bytes: &[u8; 32], scid: u64) -> bool {
154+
pub fn is_valid_phantom(fake_scid_rand_bytes: &[u8; 32], scid: u64, genesis_hash: &BlockHash) -> bool {
155155
let block_height = scid_utils::block_from_scid(&scid);
156156
let tx_index = scid_utils::tx_index_from_scid(&scid);
157157
let namespace = Namespace::Phantom;
158158
let valid_vout = namespace.get_encrypted_vout(block_height, tx_index, fake_scid_rand_bytes);
159-
valid_vout == scid_utils::vout_from_scid(&scid) as u8
159+
block_height >= segwit_activation_height(genesis_hash)
160+
&& valid_vout == scid_utils::vout_from_scid(&scid) as u8
160161
}
161162

162163
#[cfg(test)]
@@ -194,11 +195,12 @@ pub(crate) mod fake_scid {
194195
fn test_is_valid_phantom() {
195196
let namespace = Namespace::Phantom;
196197
let fake_scid_rand_bytes = [0; 32];
198+
let testnet_genesis = genesis_block(Network::Testnet).header.block_hash();
197199
let valid_encrypted_vout = namespace.get_encrypted_vout(0, 0, &fake_scid_rand_bytes);
198-
let valid_fake_scid = scid_utils::scid_from_parts(0, 0, valid_encrypted_vout as u64).unwrap();
199-
assert!(is_valid_phantom(&fake_scid_rand_bytes, valid_fake_scid));
200-
let invalid_fake_scid = scid_utils::scid_from_parts(0, 0, 12).unwrap();
201-
assert!(!is_valid_phantom(&fake_scid_rand_bytes, invalid_fake_scid));
200+
let valid_fake_scid = scid_utils::scid_from_parts(1, 0, valid_encrypted_vout as u64).unwrap();
201+
assert!(is_valid_phantom(&fake_scid_rand_bytes, valid_fake_scid, &testnet_genesis));
202+
let invalid_fake_scid = scid_utils::scid_from_parts(1, 0, 12).unwrap();
203+
assert!(!is_valid_phantom(&fake_scid_rand_bytes, invalid_fake_scid, &testnet_genesis));
202204
}
203205

204206
#[test]

0 commit comments

Comments
 (0)