Skip to content

Commit

Permalink
test all mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
joostjager committed Feb 24, 2025
1 parent 0f20bca commit 97480b6
Showing 1 changed file with 92 additions and 10 deletions.
102 changes: 92 additions & 10 deletions lightning/src/ln/onion_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,12 +1274,29 @@ where
}


struct AttributableFailure {
failure_index: usize,

result: AttributableFailureResult,
}

struct AttributableFailureSuccess {
message: Vec<u8>,
hold_times: Vec<u32>,
}

enum AttributableFailureResult {
Success(AttributableFailureSuccess),
InvalidPayload,
InvalidHmac
}

/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
/// OutboundRoute).
#[inline]
pub(super) fn process_attributable_onion_failure<T: secp256k1::Signing, L: Deref>(
secp_ctx: &Secp256k1<T>, logger: &L, htlc_source: &HTLCSource, mut encrypted_packet: Vec<u8>,
) -> Option<Vec<u8>>
) -> AttributableFailure
where
L::Target: Logger,
{
Expand All @@ -1293,7 +1310,7 @@ where
};

// Learnings from the HTLC failure to inform future payment retries and scoring.
let mut res: Option<Vec<u8>> = None;
let mut res: Option<AttributableFailure> = None;
let mut is_from_final_node = false;

// Handle packed channel/node updates for passing back for the route handler
Expand Down Expand Up @@ -1381,6 +1398,11 @@ where
let expected_hmac= &Hmac::from_engine(hmac).to_byte_array()[..HMAC_LEN];

if !fixed_time_eq(expected_hmac, actual_hmac) {
res = Some(AttributableFailure {
failure_index: route_hop_idx,
result: AttributableFailureResult::InvalidHmac,
});

log_debug!(logger, "Invalid HMAC in onion failure packet at pos {}", route_hop_idx);

return;
Expand Down Expand Up @@ -1411,10 +1433,25 @@ where
1 => {
// Final payload, parse failure msg.
let cursor = &mut Cursor::new(message);
res = Some(Readable::read(cursor).unwrap());
res = Some(AttributableFailure {
failure_index: route_hop_idx,
result: AttributableFailureResult::Success(AttributableFailureSuccess {
message: Readable::read(cursor).unwrap(),

// TODO: Read hold times
hold_times: Vec::new(),
})
})
}
_ => {
panic!("Got a payload type we don't know how to handle!");
res = Some(AttributableFailure {
failure_index: route_hop_idx,
result: AttributableFailureResult::InvalidPayload,
});

log_debug!(logger, "Invalid payload at pos {}", route_hop_idx);

return;
}
}
};
Expand All @@ -1428,7 +1465,17 @@ where
)
.expect("Route we used spontaneously grew invalid keys in the middle of it?");

res
match res {
Some(res) => res,
None => {
// All hmacs checked out, but none was a final payload. The final hop apparently returned an intermediate
// payload.
AttributableFailure {
failure_index: path.hops.len(),
result: AttributableFailureResult::InvalidPayload,
}
}
}
}

#[derive(Clone)]// See Channel::revoke_and_ack for why, tl;dr: Rust bug
Expand Down Expand Up @@ -2329,6 +2376,28 @@ use crate::util::test_utils::TestLogger;

#[test]
fn test_attributable_failure_packet_onion() {
const EXPECT_FAILURE: &str = "400f0000000000000064000c3500fd84d1fd012c808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080";

for mutating_node in 0..5 {
for mutated_index in 0..1968 {
println!("Testing mutation {} on node {}", mutated_index, mutating_node);

let decrypted_failure = test_attributable_failure_packet_onion_with_mutation(mutating_node, mutated_index);

match decrypted_failure.result {
AttributableFailureResult::Success(success) => {
assert_eq!(success.message.to_lower_hex_string(), EXPECT_FAILURE);
assert_eq!(decrypted_failure.failure_index, 4);
}
AttributableFailureResult::InvalidPayload | AttributableFailureResult::InvalidHmac => {
assert_eq!(decrypted_failure.failure_index, 4-mutating_node);
}
}
}
}
}

fn test_attributable_failure_packet_onion_with_mutation(mutating_node: usize, mutated_index: usize) -> AttributableFailure {
const FAILURE_DATA: &str = "0000000000000064000c3500fd84d1fd012c808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080";
const EXPECTED_MESSAGES: [&str; 5] = [
"e88935bf7c9a374ba64fd9da3aa2a05e6cf9e52cfb1f72698fd33b74b9c79346284931ea3571af54de5df341304833b0825fb7e8817fd82a29c0803f0a0679a6a073c33a6fb8250090a3152eba3f11a85184fa87b67f1b0354d6f48e3b342e332a17b7710f342f342a87cf32eccdf0afc2160808d58abb5e5840d2c760c538e63a6f841970f97d2e6fe5b8739dc45e2f7f5f532f227bcc2988ab0f9cc6d3f12909cd5842c37bc8c7608475a5ebbe10626d5ecc1f3388ad5f645167b44a4d166f87863fe34918cea25c18059b4c4d9cb414b59f6bc50c1cea749c80c43e2344f5d23159122ed4ab9722503b212016470d9610b46c35dbeebaf2e342e09770b38392a803bc9d2e7c8d6d384ffcbeb74943fe3f64afb2a543a6683c7db3088441c531eeb4647518cb41992f8954f1269fb969630944928c2d2b45593731b5da0c4e70d0c84ad72f642fc26919927b347808bade4b1c321b08bc363f20745ba2f97f0ced2996a232f55ba28fe7dfa70a9ab0433a085388f25cce8d53de6a2fbd7546377d6ede9027ad173ba1f95767461a3689ef405ab608a21086165c64b02c1782b04a6dba2361a7784603069124e12f2f6dcb1ec7612a4fbf94c0e14631a2bef6190c3d5f35e0c4b32aa85201f449d830fd8f782ec758b0910428e3ec3ca1dba3b6c7d89f69e1ee1b9df3dfbbf6d361e1463886b38d52e8f43b73a3bd48c6f36f5897f514b93364a31d49d1d506340b1315883d425cb36f4ea553430d538fd6f3596d4afc518db2f317dd051abc0d4bfb0a7870c3db70f19fe78d6604bbf088fcb4613f54e67b038277fedcd9680eb97bdffc3be1ab2cbcbafd625b8a7ac34d8c190f98d3064ecd3b95b8895157c6a37f31ef4de094b2cb9dbf8ff1f419ba0ecacb1bb13df0253b826bec2ccca1e745dd3b3e7cc6277ce284d649e7b8285727735ff4ef6cca6c18e2714f4e2a1ac67b25213d3bb49763b3b94e7ebf72507b71fb2fe0329666477ee7cb7ebd6b88ad5add8b217188b1ca0fa13de1ec09cc674346875105be6e0e0d6c8928eb0df23c39a639e04e4aedf535c4e093f08b2c905a14f25c0c0fe47a5a1535ab9eae0d9d67bdd79de13a08d59ee05385c7ea4af1ad3248e61dd22f8990e9e99897d653dd7b1b1433a6d464ea9f74e377f2d8ce99ba7dbc753297644234d25ecb5bd528e2e2082824681299ac30c05354baaa9c3967d86d7c07736f87fc0f63e5036d47235d7ae12178ced3ae36ee5919c093a02579e4fc9edad2c446c656c790704bfc8e2c491a42500aa1d75c8d4921ce29b753f883e17c79b09ea324f1f32ddf1f3284cd70e847b09d90f6718c42e5c94484cc9cbb0df659d255630a3f5a27e7d5dd14fa6b974d1719aa98f01a20fb4b7b1c77b42d57fab3c724339d459ee4a1c6b5d3bd4e08624c786a257872acc9ad3ff62222f2265a658d9e2a007228a5293b67ec91c84c4b4407c228434bad8a815ca9b256c776bd2c9f92e2cf87a89ed786194104eba6b7973491f04d0b924c10e6fcff336d037b6e53fe763919da946a640960978994e8d0e5a2d555c9a897ce38a324c766fd01e9416acd91f1ea345c12aa1cfa5933c5c1230c5e45efb8c7e8d75bd9dd85ce8228cf80a52c915282375663690d1286ba0e70201af791a25715819dfd1035feb5239e3df7c230956cb3be858395094d3d99cbc2352cb8adc7a8fe8f4755a2f93bef3926f57bbcff17956c4031a2ae8c88d57dd9235b49a0253e86a7f173d96907aa2e162c82f1626dd5c07188e5e01d79724a546bed2b89a2084230b770ff97b2271158569ed7d00f967cdc51d216fa1578a9624f9142d8de1039b5d4f51de09324c91582f830c7730934feffbc7c51d5d87e8760a77e0712d947190ea6f896a4685045a3de3b8187490ee65f68a9c40cf708e03ab5f28a3b7e5e4a164c3cdb3a7a393b120a2306671a3e310419f873e1d978ff08535353a85eb1773e7476f1f102e3f2364036427a633d32cbf1c34ee0a223f696e69d9e296ac4981d64c99e9966d93eef673163ac774b2545e36e64816030b4ebb7775afeb77f88396c565d58bf2f2d07601dfc5338e5a5a71853dac2e42fc2d89a2da7bf913a8d5c1705ef3c869dc6a7d3a6a8ec7cde4e99380c0223b8d766506574f45cafbe96b25ebefd066945bffc1d2262d1e7d1057660ec2916055a493f2930afe8133de9e593b470e2d512ab5bedb363a6c9ac59fd82379f528d02bae1ade38fafe2d7ac7b097bb6fa4e00e43b087d3480f41402a5156379052526827da75486cf8703ed9ba34f38a9b445da35c3e5bd799156f25081e88051d54fe47fd0f7bca364d7cdeb01cb28aadc9b03fdd91712036f1da31a554e230a897e7142c0043c98e1d9a4bd996d26dfbce431bce8a6e29048783a7142c84483895c44df5d65c3bf8ca1bd6069ad305e525df7a4c584299525549bd3d013dcbfaebf18dcd82a9d29618a9b3e564b19cfdeac6eb7a6c4ad42268747fbd162c2f300aaad722d59dde7db179d93468b435724d97078df797d75d1728e75d0687e661fb603fa1b264466b8cedb75482ef042151f2ca045c795683a5e56dfff85a17f82cc5aba7e187784f159a996c3200b8b3f2a91de25d378557c5b33f2a17ec877b15cccc5615836899c30912cb83390e24902cbecb57cb95c0738d798debc43a07b09060347b145d5ccc150fcc46bc0a21f372622f25acae867946346e54498737be61c312e93086748b37633cbd8ab62433e6375914f117b9c1cb802513f",
Expand All @@ -2351,23 +2420,33 @@ use crate::util::test_utils::TestLogger;
let mut packet = onion_error.encode();
let packet_slice = packet.as_mut_slice();

let logger: Arc<TestLogger> = Arc::new(TestLogger::new());
log_info!(logger, "packet len: {}", packet_slice.len());

super::add_hmacs(onion_keys[4].shared_secret.as_ref(), packet_slice);

if mutating_node == 0 {
packet_slice[mutated_index] ^= 1;
}

let mut encrypted_packet = super::encrypt_failure_packet(onion_keys[4].shared_secret.as_ref(), packet_slice);
assert_eq!(encrypted_packet.data.to_lower_hex_string(), EXPECTED_MESSAGES[0]);
// assert_eq!(encrypted_packet.data.to_lower_hex_string(), EXPECTED_MESSAGES[0]);

for idx in 1..5 {

let shared_secret = onion_keys[4 - idx].shared_secret.as_ref();

let payload = [0, 0, 0, (idx + 1) as u8];
let processed_packet = process_failure_packet(&encrypted_packet.data, shared_secret, &payload);
encrypted_packet = super::encrypt_failure_packet(shared_secret, &processed_packet);

assert_eq!(encrypted_packet.data.to_lower_hex_string(), EXPECTED_MESSAGES[idx]);
if idx == mutating_node {
encrypted_packet.data[mutated_index] ^= 1;
}
// assert_eq!(encrypted_packet.data.to_lower_hex_string(), EXPECTED_MESSAGES[idx]);
}

let ctx_full = Secp256k1::new();
let logger = Arc::new(TestLogger::new());

let path = build_test_path();
let session_priv = SecretKey::from_slice(<Vec<u8>>::from_hex(SESSION_KEY).unwrap().as_slice()).unwrap();
Expand All @@ -2378,8 +2457,11 @@ use crate::util::test_utils::TestLogger;
payment_id: PaymentId([1; 32])
, };


// Assert that the original failure can be retrieved and that all hmacs check out.
let decrypted_failure = process_attributable_onion_failure(&ctx_full, &logger, &htlc_source,encrypted_packet.data).unwrap();
assert_eq!(onion_error.failuremsg, decrypted_failure);
let decrypted_failure = process_attributable_onion_failure(&ctx_full, &logger, &htlc_source,encrypted_packet.data);

decrypted_failure

}
}

0 comments on commit 97480b6

Please sign in to comment.