@@ -50,11 +50,17 @@ mod channel_held_info {
50
50
pub ( super ) outgoing_cltv_value : u32 ,
51
51
}
52
52
53
+ #[ derive( Clone ) ] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
54
+ pub enum HTLCFailureMsg {
55
+ Relay ( msgs:: UpdateFailHTLC ) ,
56
+ Malformed ( msgs:: UpdateFailMalformedHTLC ) ,
57
+ }
58
+
53
59
/// Stores whether we can't forward an HTLC or relevant forwarding info
54
60
#[ derive( Clone ) ] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
55
61
pub enum PendingHTLCStatus {
56
62
Forward ( PendingForwardHTLCInfo ) ,
57
- Fail ( msgs :: UpdateFailHTLC ) ,
63
+ Fail ( HTLCFailureMsg ) ,
58
64
}
59
65
60
66
#[ cfg( feature = "fuzztarget" ) ]
@@ -619,7 +625,7 @@ impl ChannelManager {
619
625
620
626
Ok ( msgs:: OnionPacket {
621
627
version : 0 ,
622
- public_key : onion_keys. first ( ) . unwrap ( ) . ephemeral_pubkey ,
628
+ public_key : Ok ( onion_keys. first ( ) . unwrap ( ) . ephemeral_pubkey ) ,
623
629
hop_data : packet_data,
624
630
hmac : hmac_res,
625
631
} )
@@ -675,10 +681,7 @@ impl ChannelManager {
675
681
ChannelManager :: encrypt_failure_packet ( shared_secret, & failure_packet. encode ( ) [ ..] )
676
682
}
677
683
678
- fn decode_update_add_htlc_onion ( & self , msg : & msgs:: UpdateAddHTLC ) -> ( PendingHTLCStatus , SharedSecret , MutexGuard < ChannelHolder > ) {
679
- let shared_secret = SharedSecret :: new ( & self . secp_ctx , & msg. onion_routing_packet . public_key , & self . our_network_key ) ;
680
- let ( rho, mu) = ChannelManager :: gen_rho_mu_from_shared_secret ( & shared_secret) ;
681
-
684
+ fn decode_update_add_htlc_onion ( & self , msg : & msgs:: UpdateAddHTLC ) -> ( PendingHTLCStatus , Option < SharedSecret > , MutexGuard < ChannelHolder > ) {
682
685
macro_rules! get_onion_hash {
683
686
( ) => {
684
687
{
@@ -691,6 +694,19 @@ impl ChannelManager {
691
694
}
692
695
}
693
696
697
+ if let Err ( _) = msg. onion_routing_packet . public_key {
698
+ log_info ! ( self , "Failed to accept/forward incoming HTLC with invalid ephemeral pubkey" ) ;
699
+ return ( PendingHTLCStatus :: Fail ( HTLCFailureMsg :: Malformed ( msgs:: UpdateFailMalformedHTLC {
700
+ channel_id : msg. channel_id ,
701
+ htlc_id : msg. htlc_id ,
702
+ sha256_of_onion : get_onion_hash ! ( ) ,
703
+ failure_code : 0x8000 | 0x4000 | 6 ,
704
+ } ) ) , None , self . channel_state . lock ( ) . unwrap ( ) ) ;
705
+ }
706
+
707
+ let shared_secret = SharedSecret :: new ( & self . secp_ctx , & msg. onion_routing_packet . public_key . unwrap ( ) , & self . our_network_key ) ;
708
+ let ( rho, mu) = ChannelManager :: gen_rho_mu_from_shared_secret ( & shared_secret) ;
709
+
694
710
let mut channel_state = None ;
695
711
macro_rules! return_err {
696
712
( $msg: expr, $err_code: expr, $data: expr) => {
@@ -699,11 +715,11 @@ impl ChannelManager {
699
715
if channel_state. is_none( ) {
700
716
channel_state = Some ( self . channel_state. lock( ) . unwrap( ) ) ;
701
717
}
702
- return ( PendingHTLCStatus :: Fail ( msgs:: UpdateFailHTLC {
718
+ return ( PendingHTLCStatus :: Fail ( HTLCFailureMsg :: Relay ( msgs:: UpdateFailHTLC {
703
719
channel_id: msg. channel_id,
704
720
htlc_id: msg. htlc_id,
705
721
reason: ChannelManager :: build_first_hop_failure_packet( & shared_secret, $err_code, $data) ,
706
- } ) , shared_secret, channel_state. unwrap( ) ) ;
722
+ } ) ) , Some ( shared_secret) , channel_state. unwrap( ) ) ;
707
723
}
708
724
}
709
725
}
@@ -770,7 +786,7 @@ impl ChannelManager {
770
786
chacha. process ( & msg. onion_routing_packet . hop_data [ 65 ..] , & mut new_packet_data[ 0 ..19 * 65 ] ) ;
771
787
chacha. process ( & ChannelManager :: ZERO [ 0 ..65 ] , & mut new_packet_data[ 19 * 65 ..] ) ;
772
788
773
- let mut new_pubkey = msg. onion_routing_packet . public_key . clone ( ) ;
789
+ let mut new_pubkey = msg. onion_routing_packet . public_key . unwrap ( ) ;
774
790
775
791
let blinding_factor = {
776
792
let mut sha = Sha256 :: new ( ) ;
@@ -780,26 +796,19 @@ impl ChannelManager {
780
796
sha. result ( & mut res) ;
781
797
match SecretKey :: from_slice ( & self . secp_ctx , & res) {
782
798
Err ( _) => {
783
- // Return temporary node failure as its technically our issue, not the
784
- // channel's issue.
785
- return_err ! ( "Blinding factor is an invalid private key" , 0x2000 | 2 , & [ 0 ; 0 ] ) ;
799
+ return_err ! ( "Blinding factor is an invalid private key" , 0x8000 | 0x4000 | 6 , & get_onion_hash!( ) ) ;
786
800
} ,
787
801
Ok ( key) => key
788
802
}
789
803
} ;
790
804
791
- match new_pubkey. mul_assign ( & self . secp_ctx , & blinding_factor) {
792
- Err ( _) => {
793
- // Return temporary node failure as its technically our issue, not the
794
- // channel's issue.
795
- return_err ! ( "New blinding factor is an invalid private key" , 0x2000 | 2 , & [ 0 ; 0 ] ) ;
796
- } ,
797
- Ok ( _) => { }
798
- } ;
805
+ if let Err ( _) = new_pubkey. mul_assign ( & self . secp_ctx , & blinding_factor) {
806
+ return_err ! ( "New blinding factor is an invalid private key" , 0x8000 | 0x4000 | 6 , & get_onion_hash!( ) ) ;
807
+ }
799
808
800
809
let outgoing_packet = msgs:: OnionPacket {
801
810
version : 0 ,
802
- public_key : new_pubkey,
811
+ public_key : Ok ( new_pubkey) ,
803
812
hop_data : new_packet_data,
804
813
hmac : next_hop_data. hmac . clone ( ) ,
805
814
} ;
@@ -846,7 +855,7 @@ impl ChannelManager {
846
855
}
847
856
}
848
857
849
- ( pending_forward_info, shared_secret, channel_state. unwrap ( ) )
858
+ ( pending_forward_info, Some ( shared_secret) , channel_state. unwrap ( ) )
850
859
}
851
860
852
861
/// only fails if the channel does not yet have an assigned short_id
@@ -958,6 +967,7 @@ impl ChannelManager {
958
967
update_add_htlcs : vec ! [ update_add] ,
959
968
update_fulfill_htlcs : Vec :: new ( ) ,
960
969
update_fail_htlcs : Vec :: new ( ) ,
970
+ update_fail_malformed_htlcs : Vec :: new ( ) ,
961
971
commitment_signed,
962
972
} ,
963
973
} ) ;
@@ -1102,6 +1112,7 @@ impl ChannelManager {
1102
1112
update_add_htlcs : add_htlc_msgs,
1103
1113
update_fulfill_htlcs : Vec :: new ( ) ,
1104
1114
update_fail_htlcs : Vec :: new ( ) ,
1115
+ update_fail_malformed_htlcs : Vec :: new ( ) ,
1105
1116
commitment_signed : commitment_msg,
1106
1117
} ,
1107
1118
} ) ) ;
@@ -1225,6 +1236,7 @@ impl ChannelManager {
1225
1236
update_add_htlcs : Vec :: new ( ) ,
1226
1237
update_fulfill_htlcs : Vec :: new ( ) ,
1227
1238
update_fail_htlcs : vec ! [ msg] ,
1239
+ update_fail_malformed_htlcs : Vec :: new ( ) ,
1228
1240
commitment_signed : commitment_msg,
1229
1241
} ,
1230
1242
} ) ;
@@ -1324,6 +1336,7 @@ impl ChannelManager {
1324
1336
update_add_htlcs : Vec :: new ( ) ,
1325
1337
update_fulfill_htlcs : vec ! [ msg] ,
1326
1338
update_fail_htlcs : Vec :: new ( ) ,
1339
+ update_fail_malformed_htlcs : Vec :: new ( ) ,
1327
1340
commitment_signed : commitment_msg,
1328
1341
}
1329
1342
} ) ;
@@ -1722,11 +1735,11 @@ impl ChannelMessageHandler for ChannelManager {
1722
1735
}
1723
1736
if !acceptable_cycle {
1724
1737
log_info ! ( self , "Failed to accept incoming HTLC: Payment looped through us twice" ) ;
1725
- pending_forward_info = PendingHTLCStatus :: Fail ( msgs:: UpdateFailHTLC {
1738
+ pending_forward_info = PendingHTLCStatus :: Fail ( HTLCFailureMsg :: Relay ( msgs:: UpdateFailHTLC {
1726
1739
channel_id : msg. channel_id ,
1727
1740
htlc_id : msg. htlc_id ,
1728
- reason : ChannelManager :: build_first_hop_failure_packet ( & shared_secret, 0x4000 | 0x2000 | 2 , & [ 0 ; 0 ] ) ,
1729
- } ) ;
1741
+ reason : ChannelManager :: build_first_hop_failure_packet ( & shared_secret. unwrap ( ) , 0x4000 | 0x2000 | 2 , & [ 0 ; 0 ] ) ,
1742
+ } ) ) ;
1730
1743
} else {
1731
1744
will_forward = true ;
1732
1745
}
@@ -1764,15 +1777,15 @@ impl ChannelMessageHandler for ChannelManager {
1764
1777
} ;
1765
1778
* outbound_route = PendingOutboundHTLC :: CycledRoute {
1766
1779
source_short_channel_id,
1767
- incoming_packet_shared_secret : shared_secret,
1780
+ incoming_packet_shared_secret : shared_secret. unwrap ( ) ,
1768
1781
route,
1769
1782
session_priv,
1770
1783
} ;
1771
1784
} ,
1772
1785
hash_map:: Entry :: Vacant ( e) => {
1773
1786
e. insert ( PendingOutboundHTLC :: IntermediaryHopData {
1774
1787
source_short_channel_id,
1775
- incoming_packet_shared_secret : shared_secret,
1788
+ incoming_packet_shared_secret : shared_secret. unwrap ( ) ,
1776
1789
} ) ;
1777
1790
}
1778
1791
}
@@ -2487,9 +2500,10 @@ mod tests {
2487
2500
impl SendEvent {
2488
2501
fn from_event ( event : Event ) -> SendEvent {
2489
2502
match event {
2490
- Event :: UpdateHTLCs { node_id, updates : msgs:: CommitmentUpdate { update_add_htlcs, update_fulfill_htlcs, update_fail_htlcs, commitment_signed } } => {
2503
+ Event :: UpdateHTLCs { node_id, updates : msgs:: CommitmentUpdate { update_add_htlcs, update_fulfill_htlcs, update_fail_htlcs, update_fail_malformed_htlcs , commitment_signed } } => {
2491
2504
assert ! ( update_fulfill_htlcs. is_empty( ) ) ;
2492
2505
assert ! ( update_fail_htlcs. is_empty( ) ) ;
2506
+ assert ! ( update_fail_malformed_htlcs. is_empty( ) ) ;
2493
2507
SendEvent { node_id : node_id, msgs : update_add_htlcs, commitment_msg : commitment_signed }
2494
2508
} ,
2495
2509
_ => panic ! ( "Unexpected event type!" ) ,
@@ -2646,10 +2660,11 @@ mod tests {
2646
2660
let events = node. node . get_and_clear_pending_events ( ) ;
2647
2661
assert_eq ! ( events. len( ) , 1 ) ;
2648
2662
match events[ 0 ] {
2649
- Event :: UpdateHTLCs { ref node_id, updates : msgs:: CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref commitment_signed } } => {
2663
+ Event :: UpdateHTLCs { ref node_id, updates : msgs:: CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs , ref commitment_signed } } => {
2650
2664
assert ! ( update_add_htlcs. is_empty( ) ) ;
2651
2665
assert_eq ! ( update_fulfill_htlcs. len( ) , 1 ) ;
2652
2666
assert ! ( update_fail_htlcs. is_empty( ) ) ;
2667
+ assert ! ( update_fail_malformed_htlcs. is_empty( ) ) ;
2653
2668
expected_next_node = node_id. clone ( ) ;
2654
2669
next_msgs = Some ( ( update_fulfill_htlcs[ 0 ] . clone ( ) , commitment_signed. clone ( ) ) ) ;
2655
2670
} ,
@@ -2770,10 +2785,11 @@ mod tests {
2770
2785
let events = node. node . get_and_clear_pending_events ( ) ;
2771
2786
assert_eq ! ( events. len( ) , 1 ) ;
2772
2787
match events[ 0 ] {
2773
- Event :: UpdateHTLCs { ref node_id, updates : msgs:: CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref commitment_signed } } => {
2788
+ Event :: UpdateHTLCs { ref node_id, updates : msgs:: CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs , ref commitment_signed } } => {
2774
2789
assert ! ( update_add_htlcs. is_empty( ) ) ;
2775
2790
assert ! ( update_fulfill_htlcs. is_empty( ) ) ;
2776
2791
assert_eq ! ( update_fail_htlcs. len( ) , 1 ) ;
2792
+ assert ! ( update_fail_malformed_htlcs. is_empty( ) ) ;
2777
2793
expected_next_node = node_id. clone ( ) ;
2778
2794
next_msgs = Some ( ( update_fail_htlcs[ 0 ] . clone ( ) , commitment_signed. clone ( ) ) ) ;
2779
2795
} ,
0 commit comments