Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 232 additions & 45 deletions lightning/src/ln/onion_utils.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -931,15 +931,11 @@ pub(crate) struct DecodedOnionFailure {
pub(crate) onion_error_data: Option<Vec<u8>>,
}

/// Note that we always decrypt `packet` in-place here even if the deserialization into
/// [`msgs::DecodedOnionErrorPacket`] ultimately fails.
fn decrypt_onion_error_packet(
packet: &mut Vec<u8>, shared_secret: SharedSecret,
) -> Result<msgs::DecodedOnionErrorPacket, msgs::DecodeError> {
/// Decrypt the error packet in-place.
fn decrypt_onion_error_packet(packet: &mut Vec<u8>, shared_secret: SharedSecret) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost wonder whether it's one of those instances where in-place processing is what largely contributes to confusion. Getting a result back with the decrypted packet or with the decryption error, without worrying about the input argument getting changed, seems preferable.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The confusion was mainly that this used to be a function that returned a value and still had a side effect too. A side effect that was also needed, because the decrypted packet needs to be processed for the next hop.

Could have returned a decrypted copy in addition to the decoded failure to make it a pure function, probably with some performance implications. But I think now that this function has no return value anymore and only decrypts in-place, it's already a lot better.

let ammag = gen_ammag_from_shared_secret(shared_secret.as_ref());
let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
chacha.process_in_place(packet);
msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(packet))
}

/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
Expand DownExpand Up@@ -1021,9 +1017,11 @@ where
{
// Actually parse the onion error data in tests so we can check that blinded hops fail
// back correctly.
let err_packet =
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret)
.unwrap();
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);
let err_packet = msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(
&encrypted_packet,
))
.unwrap();
error_code_ret = Some(u16::from_be_bytes(
err_packet.failuremsg.get(0..2).unwrap().try_into().unwrap(),
));
Expand All@@ -1044,22 +1042,44 @@ where
let amt_to_forward = htlc_msat - route_hop.fee_msat;
htlc_msat = amt_to_forward;

let err_packet = match decrypt_onion_error_packet(&mut encrypted_packet, shared_secret) {
Ok(p) => p,
Err(_) => return,
};
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);

let um = gen_um_from_shared_secret(shared_secret.as_ref());
let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&err_packet.encode()[32..]);
hmac.input(&encrypted_packet[32..]);

if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &err_packet.hmac) {
if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &encrypted_packet[..32]) {
return;
}

let err_packet =
match msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(&encrypted_packet)) {
Ok(p) => p,
Err(_) => {
log_warn!(logger, "Unreadable failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
});
let short_channel_id = Some(route_hop.short_channel_id);
res = Some(FailureLearnings {
network_update,
short_channel_id,
payment_failed_permanently: is_from_final_node,
failed_within_blinded_path: false,
});
return;
},
};

let error_code_slice = match err_packet.failuremsg.get(0..2) {
Some(s) => s,
None => {
// Useless packet that we can't use but it passed HMAC, so it definitely came from the peer
// in question
log_warn!(logger, "Missing error code in failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
Expand DownExpand Up@@ -1219,6 +1239,12 @@ where
} else {
// only not set either packet unparseable or hmac does not match with any
// payment not retryable only when garbage is from the final node
log_warn!(
logger,
"Non-attributable failure encountered on route {}",
path.hops.iter().map(|h| h.pubkey.to_string()).collect::<Vec<_>>().join("->")
);

DecodedOnionFailure {
network_update: None,
short_channel_id: None,
Expand DownExpand Up@@ -1764,7 +1790,10 @@ fn decode_next_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(

#[cfg(test)]
mod tests {
use std::sync::Arc;

use crate::io;
use crate::ln::channelmanager::PaymentId;
use crate::ln::msgs;
use crate::routing::router::{Path, PaymentParameters, Route, RouteHop};
use crate::types::features::{ChannelFeatures, NodeFeatures};
Expand All@@ -1773,6 +1802,7 @@ mod tests {

#[allow(unused_imports)]
use crate::prelude::*;
use crate::util::test_utils::TestLogger;

use bitcoin::hex::FromHex;
use bitcoin::secp256k1::Secp256k1;
Expand All@@ -1785,40 +1815,95 @@ mod tests {
SecretKey::from_slice(&<Vec<u8>>::from_hex(hex).unwrap()[..]).unwrap()
}

fn build_test_path() -> Path {
Path {
hops: vec![
RouteHop {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt wanted this

pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 0,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 1,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a move, but just these short_channel_id's made unique to make it easier to test things for attr errors

fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 2,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 3,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 4,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
],
blinded_tail: None,
}
}

fn build_test_onion_keys() -> Vec<OnionKeys> {
// Keys from BOLT 4, used in both test vector tests
let secp_ctx = Secp256k1::new();

let route = Route {
paths: vec![Path { hops: vec![
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
], blinded_tail: None }],
route_params: None,
};
let path = build_test_path();
let route = Route { paths: vec![path], route_params: None };

let onion_keys =
super::construct_onion_keys(&secp_ctx, &route.paths[0], &get_test_session_key())
Expand DownExpand Up@@ -2078,6 +2163,108 @@ mod tests {
);
let hex = "9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d";
assert_eq!(onion_packet_5.data, <Vec<u8>>::from_hex(hex).unwrap());

let logger: Arc<TestLogger> = Arc::new(TestLogger::new());
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

// Assert that the original failure can be retrieved and that all hmacs check out.
let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, onion_packet_5.data);

assert_eq!(decrypted_failure.onion_error_code, Some(0x2002));
}

#[test]
fn test_non_attributable_failure_packet_onion() {
// Create a failure packet with bogus data.
let packet = vec![1u8; 292];

// In the current protocol, it is unfortunately not possible to identify the failure source.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet);
assert_eq!(decrypted_failure.short_channel_id, None);

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Non-attributable failure encountered",
1,
);
}

#[test]
fn test_unreadable_failure_packet_onion() {
// Create a failure packet with a valid hmac but unreadable failure message.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

// The failure message is a single 0 byte.
let mut packet = [0u8; 33];

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet[32..]);
let hmac = Hmac::from_engine(hmac).to_byte_array();
packet[..32].copy_from_slice(&hmac);

let packet = encrypt_failure_packet(shared_secret, &packet);

// For the unreadable failure, it is still expected that the failing channel can be identified.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains("lightning::ln::onion_utils", "Unreadable failure", 1);
}

#[test]
fn test_missing_error_code() {
// Create a failure packet with a valid hmac and structure, but no error code.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

let failuremsg = vec![1];
let pad = Vec::new();
let mut packet = msgs::DecodedOnionErrorPacket { hmac: [0; 32], failuremsg, pad };

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet.encode()[32..]);
packet.hmac = Hmac::from_engine(hmac).to_byte_array();

let packet = encrypt_failure_packet(shared_secret, &packet.encode()[..]);

let logger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Missing error code in failure",
1,
);
}

fn test_failure_attribution(logger: &TestLogger, packet: &[u8]) -> DecodedOnionFailure {
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, packet.into());

decrypted_failure
}

struct RawOnionHopData {
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Log cases where an onion failure cannot be attributed or interpreted by joostjager · Pull Request #3629 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 232 additions & 45 deletions lightning/src/ln/onion_utils.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -931,15 +931,11 @@ pub(crate) struct DecodedOnionFailure {
pub(crate) onion_error_data: Option<Vec<u8>>,
}

/// Note that we always decrypt `packet` in-place here even if the deserialization into
/// [`msgs::DecodedOnionErrorPacket`] ultimately fails.
fn decrypt_onion_error_packet(
packet: &mut Vec<u8>, shared_secret: SharedSecret,
) -> Result<msgs::DecodedOnionErrorPacket, msgs::DecodeError> {
/// Decrypt the error packet in-place.
fn decrypt_onion_error_packet(packet: &mut Vec<u8>, shared_secret: SharedSecret) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost wonder whether it's one of those instances where in-place processing is what largely contributes to confusion. Getting a result back with the decrypted packet or with the decryption error, without worrying about the input argument getting changed, seems preferable.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The confusion was mainly that this used to be a function that returned a value and still had a side effect too. A side effect that was also needed, because the decrypted packet needs to be processed for the next hop.

Could have returned a decrypted copy in addition to the decoded failure to make it a pure function, probably with some performance implications. But I think now that this function has no return value anymore and only decrypts in-place, it's already a lot better.

let ammag = gen_ammag_from_shared_secret(shared_secret.as_ref());
let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
chacha.process_in_place(packet);
msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(packet))
}

/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
Expand DownExpand Up@@ -1021,9 +1017,11 @@ where
{
// Actually parse the onion error data in tests so we can check that blinded hops fail
// back correctly.
let err_packet =
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret)
.unwrap();
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);
let err_packet = msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(
&encrypted_packet,
))
.unwrap();
error_code_ret = Some(u16::from_be_bytes(
err_packet.failuremsg.get(0..2).unwrap().try_into().unwrap(),
));
Expand All@@ -1044,22 +1042,44 @@ where
let amt_to_forward = htlc_msat - route_hop.fee_msat;
htlc_msat = amt_to_forward;

let err_packet = match decrypt_onion_error_packet(&mut encrypted_packet, shared_secret) {
Ok(p) => p,
Err(_) => return,
};
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);

let um = gen_um_from_shared_secret(shared_secret.as_ref());
let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&err_packet.encode()[32..]);
hmac.input(&encrypted_packet[32..]);

if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &err_packet.hmac) {
if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &encrypted_packet[..32]) {
return;
}

let err_packet =
match msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(&encrypted_packet)) {
Ok(p) => p,
Err(_) => {
log_warn!(logger, "Unreadable failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
});
let short_channel_id = Some(route_hop.short_channel_id);
res = Some(FailureLearnings {
network_update,
short_channel_id,
payment_failed_permanently: is_from_final_node,
failed_within_blinded_path: false,
});
return;
},
};

let error_code_slice = match err_packet.failuremsg.get(0..2) {
Some(s) => s,
None => {
// Useless packet that we can't use but it passed HMAC, so it definitely came from the peer
// in question
log_warn!(logger, "Missing error code in failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
Expand DownExpand Up@@ -1219,6 +1239,12 @@ where
} else {
// only not set either packet unparseable or hmac does not match with any
// payment not retryable only when garbage is from the final node
log_warn!(
logger,
"Non-attributable failure encountered on route {}",
path.hops.iter().map(|h| h.pubkey.to_string()).collect::<Vec<_>>().join("->")
);

DecodedOnionFailure {
network_update: None,
short_channel_id: None,
Expand DownExpand Up@@ -1764,7 +1790,10 @@ fn decode_next_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(

#[cfg(test)]
mod tests {
use std::sync::Arc;

use crate::io;
use crate::ln::channelmanager::PaymentId;
use crate::ln::msgs;
use crate::routing::router::{Path, PaymentParameters, Route, RouteHop};
use crate::types::features::{ChannelFeatures, NodeFeatures};
Expand All@@ -1773,6 +1802,7 @@ mod tests {

#[allow(unused_imports)]
use crate::prelude::*;
use crate::util::test_utils::TestLogger;

use bitcoin::hex::FromHex;
use bitcoin::secp256k1::Secp256k1;
Expand All@@ -1785,40 +1815,95 @@ mod tests {
SecretKey::from_slice(&<Vec<u8>>::from_hex(hex).unwrap()[..]).unwrap()
}

fn build_test_path() -> Path {
Path {
hops: vec![
RouteHop {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt wanted this

pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 0,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 1,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a move, but just these short_channel_id's made unique to make it easier to test things for attr errors

fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 2,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 3,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 4,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
],
blinded_tail: None,
}
}

fn build_test_onion_keys() -> Vec<OnionKeys> {
// Keys from BOLT 4, used in both test vector tests
let secp_ctx = Secp256k1::new();

let route = Route {
paths: vec![Path { hops: vec![
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
], blinded_tail: None }],
route_params: None,
};
let path = build_test_path();
let route = Route { paths: vec![path], route_params: None };

let onion_keys =
super::construct_onion_keys(&secp_ctx, &route.paths[0], &get_test_session_key())
Expand DownExpand Up@@ -2078,6 +2163,108 @@ mod tests {
);
let hex = "9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d";
assert_eq!(onion_packet_5.data, <Vec<u8>>::from_hex(hex).unwrap());

let logger: Arc<TestLogger> = Arc::new(TestLogger::new());
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

// Assert that the original failure can be retrieved and that all hmacs check out.
let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, onion_packet_5.data);

assert_eq!(decrypted_failure.onion_error_code, Some(0x2002));
}

#[test]
fn test_non_attributable_failure_packet_onion() {
// Create a failure packet with bogus data.
let packet = vec![1u8; 292];

// In the current protocol, it is unfortunately not possible to identify the failure source.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet);
assert_eq!(decrypted_failure.short_channel_id, None);

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Non-attributable failure encountered",
1,
);
}

#[test]
fn test_unreadable_failure_packet_onion() {
// Create a failure packet with a valid hmac but unreadable failure message.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

// The failure message is a single 0 byte.
let mut packet = [0u8; 33];

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet[32..]);
let hmac = Hmac::from_engine(hmac).to_byte_array();
packet[..32].copy_from_slice(&hmac);

let packet = encrypt_failure_packet(shared_secret, &packet);

// For the unreadable failure, it is still expected that the failing channel can be identified.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains("lightning::ln::onion_utils", "Unreadable failure", 1);
}

#[test]
fn test_missing_error_code() {
// Create a failure packet with a valid hmac and structure, but no error code.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

let failuremsg = vec![1];
let pad = Vec::new();
let mut packet = msgs::DecodedOnionErrorPacket { hmac: [0; 32], failuremsg, pad };

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet.encode()[32..]);
packet.hmac = Hmac::from_engine(hmac).to_byte_array();

let packet = encrypt_failure_packet(shared_secret, &packet.encode()[..]);

let logger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Missing error code in failure",
1,
);
}

fn test_failure_attribution(logger: &TestLogger, packet: &[u8]) -> DecodedOnionFailure {
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, packet.into());

decrypted_failure
}

struct RawOnionHopData {
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Log cases where an onion failure cannot be attributed or interpreted by joostjager · Pull Request #3629 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 232 additions & 45 deletions lightning/src/ln/onion_utils.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -931,15 +931,11 @@ pub(crate) struct DecodedOnionFailure {
pub(crate) onion_error_data: Option<Vec<u8>>,
}

/// Note that we always decrypt `packet` in-place here even if the deserialization into
/// [`msgs::DecodedOnionErrorPacket`] ultimately fails.
fn decrypt_onion_error_packet(
packet: &mut Vec<u8>, shared_secret: SharedSecret,
) -> Result<msgs::DecodedOnionErrorPacket, msgs::DecodeError> {
/// Decrypt the error packet in-place.
fn decrypt_onion_error_packet(packet: &mut Vec<u8>, shared_secret: SharedSecret) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost wonder whether it's one of those instances where in-place processing is what largely contributes to confusion. Getting a result back with the decrypted packet or with the decryption error, without worrying about the input argument getting changed, seems preferable.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The confusion was mainly that this used to be a function that returned a value and still had a side effect too. A side effect that was also needed, because the decrypted packet needs to be processed for the next hop.

Could have returned a decrypted copy in addition to the decoded failure to make it a pure function, probably with some performance implications. But I think now that this function has no return value anymore and only decrypts in-place, it's already a lot better.

let ammag = gen_ammag_from_shared_secret(shared_secret.as_ref());
let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
chacha.process_in_place(packet);
msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(packet))
}

/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
Expand DownExpand Up@@ -1021,9 +1017,11 @@ where
{
// Actually parse the onion error data in tests so we can check that blinded hops fail
// back correctly.
let err_packet =
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret)
.unwrap();
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);
let err_packet = msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(
&encrypted_packet,
))
.unwrap();
error_code_ret = Some(u16::from_be_bytes(
err_packet.failuremsg.get(0..2).unwrap().try_into().unwrap(),
));
Expand All@@ -1044,22 +1042,44 @@ where
let amt_to_forward = htlc_msat - route_hop.fee_msat;
htlc_msat = amt_to_forward;

let err_packet = match decrypt_onion_error_packet(&mut encrypted_packet, shared_secret) {
Ok(p) => p,
Err(_) => return,
};
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);

let um = gen_um_from_shared_secret(shared_secret.as_ref());
let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&err_packet.encode()[32..]);
hmac.input(&encrypted_packet[32..]);

if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &err_packet.hmac) {
if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &encrypted_packet[..32]) {
return;
}

let err_packet =
match msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(&encrypted_packet)) {
Ok(p) => p,
Err(_) => {
log_warn!(logger, "Unreadable failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
});
let short_channel_id = Some(route_hop.short_channel_id);
res = Some(FailureLearnings {
network_update,
short_channel_id,
payment_failed_permanently: is_from_final_node,
failed_within_blinded_path: false,
});
return;
},
};

let error_code_slice = match err_packet.failuremsg.get(0..2) {
Some(s) => s,
None => {
// Useless packet that we can't use but it passed HMAC, so it definitely came from the peer
// in question
log_warn!(logger, "Missing error code in failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
Expand DownExpand Up@@ -1219,6 +1239,12 @@ where
} else {
// only not set either packet unparseable or hmac does not match with any
// payment not retryable only when garbage is from the final node
log_warn!(
logger,
"Non-attributable failure encountered on route {}",
path.hops.iter().map(|h| h.pubkey.to_string()).collect::<Vec<_>>().join("->")
);

DecodedOnionFailure {
network_update: None,
short_channel_id: None,
Expand DownExpand Up@@ -1764,7 +1790,10 @@ fn decode_next_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(

#[cfg(test)]
mod tests {
use std::sync::Arc;

use crate::io;
use crate::ln::channelmanager::PaymentId;
use crate::ln::msgs;
use crate::routing::router::{Path, PaymentParameters, Route, RouteHop};
use crate::types::features::{ChannelFeatures, NodeFeatures};
Expand All@@ -1773,6 +1802,7 @@ mod tests {

#[allow(unused_imports)]
use crate::prelude::*;
use crate::util::test_utils::TestLogger;

use bitcoin::hex::FromHex;
use bitcoin::secp256k1::Secp256k1;
Expand All@@ -1785,40 +1815,95 @@ mod tests {
SecretKey::from_slice(&<Vec<u8>>::from_hex(hex).unwrap()[..]).unwrap()
}

fn build_test_path() -> Path {
Path {
hops: vec![
RouteHop {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt wanted this

pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 0,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 1,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a move, but just these short_channel_id's made unique to make it easier to test things for attr errors

fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 2,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 3,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 4,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
],
blinded_tail: None,
}
}

fn build_test_onion_keys() -> Vec<OnionKeys> {
// Keys from BOLT 4, used in both test vector tests
let secp_ctx = Secp256k1::new();

let route = Route {
paths: vec![Path { hops: vec![
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
], blinded_tail: None }],
route_params: None,
};
let path = build_test_path();
let route = Route { paths: vec![path], route_params: None };

let onion_keys =
super::construct_onion_keys(&secp_ctx, &route.paths[0], &get_test_session_key())
Expand DownExpand Up@@ -2078,6 +2163,108 @@ mod tests {
);
let hex = "9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d";
assert_eq!(onion_packet_5.data, <Vec<u8>>::from_hex(hex).unwrap());

let logger: Arc<TestLogger> = Arc::new(TestLogger::new());
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

// Assert that the original failure can be retrieved and that all hmacs check out.
let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, onion_packet_5.data);

assert_eq!(decrypted_failure.onion_error_code, Some(0x2002));
}

#[test]
fn test_non_attributable_failure_packet_onion() {
// Create a failure packet with bogus data.
let packet = vec![1u8; 292];

// In the current protocol, it is unfortunately not possible to identify the failure source.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet);
assert_eq!(decrypted_failure.short_channel_id, None);

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Non-attributable failure encountered",
1,
);
}

#[test]
fn test_unreadable_failure_packet_onion() {
// Create a failure packet with a valid hmac but unreadable failure message.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

// The failure message is a single 0 byte.
let mut packet = [0u8; 33];

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet[32..]);
let hmac = Hmac::from_engine(hmac).to_byte_array();
packet[..32].copy_from_slice(&hmac);

let packet = encrypt_failure_packet(shared_secret, &packet);

// For the unreadable failure, it is still expected that the failing channel can be identified.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains("lightning::ln::onion_utils", "Unreadable failure", 1);
}

#[test]
fn test_missing_error_code() {
// Create a failure packet with a valid hmac and structure, but no error code.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

let failuremsg = vec![1];
let pad = Vec::new();
let mut packet = msgs::DecodedOnionErrorPacket { hmac: [0; 32], failuremsg, pad };

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet.encode()[32..]);
packet.hmac = Hmac::from_engine(hmac).to_byte_array();

let packet = encrypt_failure_packet(shared_secret, &packet.encode()[..]);

let logger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Missing error code in failure",
1,
);
}

fn test_failure_attribution(logger: &TestLogger, packet: &[u8]) -> DecodedOnionFailure {
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, packet.into());

decrypted_failure
}

struct RawOnionHopData {
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Log cases where an onion failure cannot be attributed or interpreted by joostjager · Pull Request #3629 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 232 additions & 45 deletions lightning/src/ln/onion_utils.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -931,15 +931,11 @@ pub(crate) struct DecodedOnionFailure {
pub(crate) onion_error_data: Option<Vec<u8>>,
}

/// Note that we always decrypt `packet` in-place here even if the deserialization into
/// [`msgs::DecodedOnionErrorPacket`] ultimately fails.
fn decrypt_onion_error_packet(
packet: &mut Vec<u8>, shared_secret: SharedSecret,
) -> Result<msgs::DecodedOnionErrorPacket, msgs::DecodeError> {
/// Decrypt the error packet in-place.
fn decrypt_onion_error_packet(packet: &mut Vec<u8>, shared_secret: SharedSecret) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost wonder whether it's one of those instances where in-place processing is what largely contributes to confusion. Getting a result back with the decrypted packet or with the decryption error, without worrying about the input argument getting changed, seems preferable.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The confusion was mainly that this used to be a function that returned a value and still had a side effect too. A side effect that was also needed, because the decrypted packet needs to be processed for the next hop.

Could have returned a decrypted copy in addition to the decoded failure to make it a pure function, probably with some performance implications. But I think now that this function has no return value anymore and only decrypts in-place, it's already a lot better.

let ammag = gen_ammag_from_shared_secret(shared_secret.as_ref());
let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
chacha.process_in_place(packet);
msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(packet))
}

/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
Expand DownExpand Up@@ -1021,9 +1017,11 @@ where
{
// Actually parse the onion error data in tests so we can check that blinded hops fail
// back correctly.
let err_packet =
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret)
.unwrap();
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);
let err_packet = msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(
&encrypted_packet,
))
.unwrap();
error_code_ret = Some(u16::from_be_bytes(
err_packet.failuremsg.get(0..2).unwrap().try_into().unwrap(),
));
Expand All@@ -1044,22 +1042,44 @@ where
let amt_to_forward = htlc_msat - route_hop.fee_msat;
htlc_msat = amt_to_forward;

let err_packet = match decrypt_onion_error_packet(&mut encrypted_packet, shared_secret) {
Ok(p) => p,
Err(_) => return,
};
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);

let um = gen_um_from_shared_secret(shared_secret.as_ref());
let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&err_packet.encode()[32..]);
hmac.input(&encrypted_packet[32..]);

if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &err_packet.hmac) {
if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &encrypted_packet[..32]) {
return;
}

let err_packet =
match msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(&encrypted_packet)) {
Ok(p) => p,
Err(_) => {
log_warn!(logger, "Unreadable failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
});
let short_channel_id = Some(route_hop.short_channel_id);
res = Some(FailureLearnings {
network_update,
short_channel_id,
payment_failed_permanently: is_from_final_node,
failed_within_blinded_path: false,
});
return;
},
};

let error_code_slice = match err_packet.failuremsg.get(0..2) {
Some(s) => s,
None => {
// Useless packet that we can't use but it passed HMAC, so it definitely came from the peer
// in question
log_warn!(logger, "Missing error code in failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
Expand DownExpand Up@@ -1219,6 +1239,12 @@ where
} else {
// only not set either packet unparseable or hmac does not match with any
// payment not retryable only when garbage is from the final node
log_warn!(
logger,
"Non-attributable failure encountered on route {}",
path.hops.iter().map(|h| h.pubkey.to_string()).collect::<Vec<_>>().join("->")
);

DecodedOnionFailure {
network_update: None,
short_channel_id: None,
Expand DownExpand Up@@ -1764,7 +1790,10 @@ fn decode_next_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(

#[cfg(test)]
mod tests {
use std::sync::Arc;

use crate::io;
use crate::ln::channelmanager::PaymentId;
use crate::ln::msgs;
use crate::routing::router::{Path, PaymentParameters, Route, RouteHop};
use crate::types::features::{ChannelFeatures, NodeFeatures};
Expand All@@ -1773,6 +1802,7 @@ mod tests {

#[allow(unused_imports)]
use crate::prelude::*;
use crate::util::test_utils::TestLogger;

use bitcoin::hex::FromHex;
use bitcoin::secp256k1::Secp256k1;
Expand All@@ -1785,40 +1815,95 @@ mod tests {
SecretKey::from_slice(&<Vec<u8>>::from_hex(hex).unwrap()[..]).unwrap()
}

fn build_test_path() -> Path {
Path {
hops: vec![
RouteHop {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt wanted this

pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 0,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 1,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a move, but just these short_channel_id's made unique to make it easier to test things for attr errors

fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 2,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 3,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 4,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
],
blinded_tail: None,
}
}

fn build_test_onion_keys() -> Vec<OnionKeys> {
// Keys from BOLT 4, used in both test vector tests
let secp_ctx = Secp256k1::new();

let route = Route {
paths: vec![Path { hops: vec![
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
], blinded_tail: None }],
route_params: None,
};
let path = build_test_path();
let route = Route { paths: vec![path], route_params: None };

let onion_keys =
super::construct_onion_keys(&secp_ctx, &route.paths[0], &get_test_session_key())
Expand DownExpand Up@@ -2078,6 +2163,108 @@ mod tests {
);
let hex = "9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d";
assert_eq!(onion_packet_5.data, <Vec<u8>>::from_hex(hex).unwrap());

let logger: Arc<TestLogger> = Arc::new(TestLogger::new());
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

// Assert that the original failure can be retrieved and that all hmacs check out.
let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, onion_packet_5.data);

assert_eq!(decrypted_failure.onion_error_code, Some(0x2002));
}

#[test]
fn test_non_attributable_failure_packet_onion() {
// Create a failure packet with bogus data.
let packet = vec![1u8; 292];

// In the current protocol, it is unfortunately not possible to identify the failure source.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet);
assert_eq!(decrypted_failure.short_channel_id, None);

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Non-attributable failure encountered",
1,
);
}

#[test]
fn test_unreadable_failure_packet_onion() {
// Create a failure packet with a valid hmac but unreadable failure message.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

// The failure message is a single 0 byte.
let mut packet = [0u8; 33];

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet[32..]);
let hmac = Hmac::from_engine(hmac).to_byte_array();
packet[..32].copy_from_slice(&hmac);

let packet = encrypt_failure_packet(shared_secret, &packet);

// For the unreadable failure, it is still expected that the failing channel can be identified.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains("lightning::ln::onion_utils", "Unreadable failure", 1);
}

#[test]
fn test_missing_error_code() {
// Create a failure packet with a valid hmac and structure, but no error code.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

let failuremsg = vec![1];
let pad = Vec::new();
let mut packet = msgs::DecodedOnionErrorPacket { hmac: [0; 32], failuremsg, pad };

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet.encode()[32..]);
packet.hmac = Hmac::from_engine(hmac).to_byte_array();

let packet = encrypt_failure_packet(shared_secret, &packet.encode()[..]);

let logger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Missing error code in failure",
1,
);
}

fn test_failure_attribution(logger: &TestLogger, packet: &[u8]) -> DecodedOnionFailure {
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, packet.into());

decrypted_failure
}

struct RawOnionHopData {
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' Log cases where an onion failure cannot be attributed or interpreted by joostjager · Pull Request #3629 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 232 additions & 45 deletions lightning/src/ln/onion_utils.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -931,15 +931,11 @@ pub(crate) struct DecodedOnionFailure {
pub(crate) onion_error_data: Option<Vec<u8>>,
}

/// Note that we always decrypt `packet` in-place here even if the deserialization into
/// [`msgs::DecodedOnionErrorPacket`] ultimately fails.
fn decrypt_onion_error_packet(
packet: &mut Vec<u8>, shared_secret: SharedSecret,
) -> Result<msgs::DecodedOnionErrorPacket, msgs::DecodeError> {
/// Decrypt the error packet in-place.
fn decrypt_onion_error_packet(packet: &mut Vec<u8>, shared_secret: SharedSecret) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost wonder whether it's one of those instances where in-place processing is what largely contributes to confusion. Getting a result back with the decrypted packet or with the decryption error, without worrying about the input argument getting changed, seems preferable.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The confusion was mainly that this used to be a function that returned a value and still had a side effect too. A side effect that was also needed, because the decrypted packet needs to be processed for the next hop.

Could have returned a decrypted copy in addition to the decoded failure to make it a pure function, probably with some performance implications. But I think now that this function has no return value anymore and only decrypts in-place, it's already a lot better.

let ammag = gen_ammag_from_shared_secret(shared_secret.as_ref());
let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
chacha.process_in_place(packet);
msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(packet))
}

/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
Expand DownExpand Up@@ -1021,9 +1017,11 @@ where
{
// Actually parse the onion error data in tests so we can check that blinded hops fail
// back correctly.
let err_packet =
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret)
.unwrap();
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);
let err_packet = msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(
&encrypted_packet,
))
.unwrap();
error_code_ret = Some(u16::from_be_bytes(
err_packet.failuremsg.get(0..2).unwrap().try_into().unwrap(),
));
Expand All@@ -1044,22 +1042,44 @@ where
let amt_to_forward = htlc_msat - route_hop.fee_msat;
htlc_msat = amt_to_forward;

let err_packet = match decrypt_onion_error_packet(&mut encrypted_packet, shared_secret) {
Ok(p) => p,
Err(_) => return,
};
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);

let um = gen_um_from_shared_secret(shared_secret.as_ref());
let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&err_packet.encode()[32..]);
hmac.input(&encrypted_packet[32..]);

if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &err_packet.hmac) {
if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &encrypted_packet[..32]) {
return;
}

let err_packet =
match msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(&encrypted_packet)) {
Ok(p) => p,
Err(_) => {
log_warn!(logger, "Unreadable failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
});
let short_channel_id = Some(route_hop.short_channel_id);
res = Some(FailureLearnings {
network_update,
short_channel_id,
payment_failed_permanently: is_from_final_node,
failed_within_blinded_path: false,
});
return;
},
};

let error_code_slice = match err_packet.failuremsg.get(0..2) {
Some(s) => s,
None => {
// Useless packet that we can't use but it passed HMAC, so it definitely came from the peer
// in question
log_warn!(logger, "Missing error code in failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
Expand DownExpand Up@@ -1219,6 +1239,12 @@ where
} else {
// only not set either packet unparseable or hmac does not match with any
// payment not retryable only when garbage is from the final node
log_warn!(
logger,
"Non-attributable failure encountered on route {}",
path.hops.iter().map(|h| h.pubkey.to_string()).collect::<Vec<_>>().join("->")
);

DecodedOnionFailure {
network_update: None,
short_channel_id: None,
Expand DownExpand Up@@ -1764,7 +1790,10 @@ fn decode_next_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(

#[cfg(test)]
mod tests {
use std::sync::Arc;

use crate::io;
use crate::ln::channelmanager::PaymentId;
use crate::ln::msgs;
use crate::routing::router::{Path, PaymentParameters, Route, RouteHop};
use crate::types::features::{ChannelFeatures, NodeFeatures};
Expand All@@ -1773,6 +1802,7 @@ mod tests {

#[allow(unused_imports)]
use crate::prelude::*;
use crate::util::test_utils::TestLogger;

use bitcoin::hex::FromHex;
use bitcoin::secp256k1::Secp256k1;
Expand All@@ -1785,40 +1815,95 @@ mod tests {
SecretKey::from_slice(&<Vec<u8>>::from_hex(hex).unwrap()[..]).unwrap()
}

fn build_test_path() -> Path {
Path {
hops: vec![
RouteHop {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt wanted this

pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 0,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 1,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a move, but just these short_channel_id's made unique to make it easier to test things for attr errors

fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 2,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 3,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 4,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
],
blinded_tail: None,
}
}

fn build_test_onion_keys() -> Vec<OnionKeys> {
// Keys from BOLT 4, used in both test vector tests
let secp_ctx = Secp256k1::new();

let route = Route {
paths: vec![Path { hops: vec![
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
], blinded_tail: None }],
route_params: None,
};
let path = build_test_path();
let route = Route { paths: vec![path], route_params: None };

let onion_keys =
super::construct_onion_keys(&secp_ctx, &route.paths[0], &get_test_session_key())
Expand DownExpand Up@@ -2078,6 +2163,108 @@ mod tests {
);
let hex = "9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d";
assert_eq!(onion_packet_5.data, <Vec<u8>>::from_hex(hex).unwrap());

let logger: Arc<TestLogger> = Arc::new(TestLogger::new());
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

// Assert that the original failure can be retrieved and that all hmacs check out.
let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, onion_packet_5.data);

assert_eq!(decrypted_failure.onion_error_code, Some(0x2002));
}

#[test]
fn test_non_attributable_failure_packet_onion() {
// Create a failure packet with bogus data.
let packet = vec![1u8; 292];

// In the current protocol, it is unfortunately not possible to identify the failure source.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet);
assert_eq!(decrypted_failure.short_channel_id, None);

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Non-attributable failure encountered",
1,
);
}

#[test]
fn test_unreadable_failure_packet_onion() {
// Create a failure packet with a valid hmac but unreadable failure message.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

// The failure message is a single 0 byte.
let mut packet = [0u8; 33];

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet[32..]);
let hmac = Hmac::from_engine(hmac).to_byte_array();
packet[..32].copy_from_slice(&hmac);

let packet = encrypt_failure_packet(shared_secret, &packet);

// For the unreadable failure, it is still expected that the failing channel can be identified.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains("lightning::ln::onion_utils", "Unreadable failure", 1);
}

#[test]
fn test_missing_error_code() {
// Create a failure packet with a valid hmac and structure, but no error code.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

let failuremsg = vec![1];
let pad = Vec::new();
let mut packet = msgs::DecodedOnionErrorPacket { hmac: [0; 32], failuremsg, pad };

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet.encode()[32..]);
packet.hmac = Hmac::from_engine(hmac).to_byte_array();

let packet = encrypt_failure_packet(shared_secret, &packet.encode()[..]);

let logger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Missing error code in failure",
1,
);
}

fn test_failure_attribution(logger: &TestLogger, packet: &[u8]) -> DecodedOnionFailure {
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, packet.into());

decrypted_failure
}

struct RawOnionHopData {
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Log cases where an onion failure cannot be attributed or interpreted by joostjager · Pull Request #3629 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 232 additions & 45 deletions lightning/src/ln/onion_utils.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -931,15 +931,11 @@ pub(crate) struct DecodedOnionFailure {
pub(crate) onion_error_data: Option<Vec<u8>>,
}

/// Note that we always decrypt `packet` in-place here even if the deserialization into
/// [`msgs::DecodedOnionErrorPacket`] ultimately fails.
fn decrypt_onion_error_packet(
packet: &mut Vec<u8>, shared_secret: SharedSecret,
) -> Result<msgs::DecodedOnionErrorPacket, msgs::DecodeError> {
/// Decrypt the error packet in-place.
fn decrypt_onion_error_packet(packet: &mut Vec<u8>, shared_secret: SharedSecret) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost wonder whether it's one of those instances where in-place processing is what largely contributes to confusion. Getting a result back with the decrypted packet or with the decryption error, without worrying about the input argument getting changed, seems preferable.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The confusion was mainly that this used to be a function that returned a value and still had a side effect too. A side effect that was also needed, because the decrypted packet needs to be processed for the next hop.

Could have returned a decrypted copy in addition to the decoded failure to make it a pure function, probably with some performance implications. But I think now that this function has no return value anymore and only decrypts in-place, it's already a lot better.

let ammag = gen_ammag_from_shared_secret(shared_secret.as_ref());
let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
chacha.process_in_place(packet);
msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(packet))
}

/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
Expand DownExpand Up@@ -1021,9 +1017,11 @@ where
{
// Actually parse the onion error data in tests so we can check that blinded hops fail
// back correctly.
let err_packet =
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret)
.unwrap();
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);
let err_packet = msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(
&encrypted_packet,
))
.unwrap();
error_code_ret = Some(u16::from_be_bytes(
err_packet.failuremsg.get(0..2).unwrap().try_into().unwrap(),
));
Expand All@@ -1044,22 +1042,44 @@ where
let amt_to_forward = htlc_msat - route_hop.fee_msat;
htlc_msat = amt_to_forward;

let err_packet = match decrypt_onion_error_packet(&mut encrypted_packet, shared_secret) {
Ok(p) => p,
Err(_) => return,
};
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);

let um = gen_um_from_shared_secret(shared_secret.as_ref());
let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&err_packet.encode()[32..]);
hmac.input(&encrypted_packet[32..]);

if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &err_packet.hmac) {
if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &encrypted_packet[..32]) {
return;
}

let err_packet =
match msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(&encrypted_packet)) {
Ok(p) => p,
Err(_) => {
log_warn!(logger, "Unreadable failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
});
let short_channel_id = Some(route_hop.short_channel_id);
res = Some(FailureLearnings {
network_update,
short_channel_id,
payment_failed_permanently: is_from_final_node,
failed_within_blinded_path: false,
});
return;
},
};

let error_code_slice = match err_packet.failuremsg.get(0..2) {
Some(s) => s,
None => {
// Useless packet that we can't use but it passed HMAC, so it definitely came from the peer
// in question
log_warn!(logger, "Missing error code in failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
Expand DownExpand Up@@ -1219,6 +1239,12 @@ where
} else {
// only not set either packet unparseable or hmac does not match with any
// payment not retryable only when garbage is from the final node
log_warn!(
logger,
"Non-attributable failure encountered on route {}",
path.hops.iter().map(|h| h.pubkey.to_string()).collect::<Vec<_>>().join("->")
);

DecodedOnionFailure {
network_update: None,
short_channel_id: None,
Expand DownExpand Up@@ -1764,7 +1790,10 @@ fn decode_next_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(

#[cfg(test)]
mod tests {
use std::sync::Arc;

use crate::io;
use crate::ln::channelmanager::PaymentId;
use crate::ln::msgs;
use crate::routing::router::{Path, PaymentParameters, Route, RouteHop};
use crate::types::features::{ChannelFeatures, NodeFeatures};
Expand All@@ -1773,6 +1802,7 @@ mod tests {

#[allow(unused_imports)]
use crate::prelude::*;
use crate::util::test_utils::TestLogger;

use bitcoin::hex::FromHex;
use bitcoin::secp256k1::Secp256k1;
Expand All@@ -1785,40 +1815,95 @@ mod tests {
SecretKey::from_slice(&<Vec<u8>>::from_hex(hex).unwrap()[..]).unwrap()
}

fn build_test_path() -> Path {
Path {
hops: vec![
RouteHop {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt wanted this

pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 0,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 1,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a move, but just these short_channel_id's made unique to make it easier to test things for attr errors

fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 2,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 3,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 4,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
],
blinded_tail: None,
}
}

fn build_test_onion_keys() -> Vec<OnionKeys> {
// Keys from BOLT 4, used in both test vector tests
let secp_ctx = Secp256k1::new();

let route = Route {
paths: vec![Path { hops: vec![
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
], blinded_tail: None }],
route_params: None,
};
let path = build_test_path();
let route = Route { paths: vec![path], route_params: None };

let onion_keys =
super::construct_onion_keys(&secp_ctx, &route.paths[0], &get_test_session_key())
Expand DownExpand Up@@ -2078,6 +2163,108 @@ mod tests {
);
let hex = "9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d";
assert_eq!(onion_packet_5.data, <Vec<u8>>::from_hex(hex).unwrap());

let logger: Arc<TestLogger> = Arc::new(TestLogger::new());
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

// Assert that the original failure can be retrieved and that all hmacs check out.
let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, onion_packet_5.data);

assert_eq!(decrypted_failure.onion_error_code, Some(0x2002));
}

#[test]
fn test_non_attributable_failure_packet_onion() {
// Create a failure packet with bogus data.
let packet = vec![1u8; 292];

// In the current protocol, it is unfortunately not possible to identify the failure source.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet);
assert_eq!(decrypted_failure.short_channel_id, None);

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Non-attributable failure encountered",
1,
);
}

#[test]
fn test_unreadable_failure_packet_onion() {
// Create a failure packet with a valid hmac but unreadable failure message.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

// The failure message is a single 0 byte.
let mut packet = [0u8; 33];

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet[32..]);
let hmac = Hmac::from_engine(hmac).to_byte_array();
packet[..32].copy_from_slice(&hmac);

let packet = encrypt_failure_packet(shared_secret, &packet);

// For the unreadable failure, it is still expected that the failing channel can be identified.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains("lightning::ln::onion_utils", "Unreadable failure", 1);
}

#[test]
fn test_missing_error_code() {
// Create a failure packet with a valid hmac and structure, but no error code.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

let failuremsg = vec![1];
let pad = Vec::new();
let mut packet = msgs::DecodedOnionErrorPacket { hmac: [0; 32], failuremsg, pad };

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet.encode()[32..]);
packet.hmac = Hmac::from_engine(hmac).to_byte_array();

let packet = encrypt_failure_packet(shared_secret, &packet.encode()[..]);

let logger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Missing error code in failure",
1,
);
}

fn test_failure_attribution(logger: &TestLogger, packet: &[u8]) -> DecodedOnionFailure {
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, packet.into());

decrypted_failure
}

struct RawOnionHopData {
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Log cases where an onion failure cannot be attributed or interpreted by joostjager · Pull Request #3629 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 232 additions & 45 deletions lightning/src/ln/onion_utils.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -931,15 +931,11 @@ pub(crate) struct DecodedOnionFailure {
pub(crate) onion_error_data: Option<Vec<u8>>,
}

/// Note that we always decrypt `packet` in-place here even if the deserialization into
/// [`msgs::DecodedOnionErrorPacket`] ultimately fails.
fn decrypt_onion_error_packet(
packet: &mut Vec<u8>, shared_secret: SharedSecret,
) -> Result<msgs::DecodedOnionErrorPacket, msgs::DecodeError> {
/// Decrypt the error packet in-place.
fn decrypt_onion_error_packet(packet: &mut Vec<u8>, shared_secret: SharedSecret) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost wonder whether it's one of those instances where in-place processing is what largely contributes to confusion. Getting a result back with the decrypted packet or with the decryption error, without worrying about the input argument getting changed, seems preferable.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The confusion was mainly that this used to be a function that returned a value and still had a side effect too. A side effect that was also needed, because the decrypted packet needs to be processed for the next hop.

Could have returned a decrypted copy in addition to the decoded failure to make it a pure function, probably with some performance implications. But I think now that this function has no return value anymore and only decrypts in-place, it's already a lot better.

let ammag = gen_ammag_from_shared_secret(shared_secret.as_ref());
let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
chacha.process_in_place(packet);
msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(packet))
}

/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
Expand DownExpand Up@@ -1021,9 +1017,11 @@ where
{
// Actually parse the onion error data in tests so we can check that blinded hops fail
// back correctly.
let err_packet =
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret)
.unwrap();
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);
let err_packet = msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(
&encrypted_packet,
))
.unwrap();
error_code_ret = Some(u16::from_be_bytes(
err_packet.failuremsg.get(0..2).unwrap().try_into().unwrap(),
));
Expand All@@ -1044,22 +1042,44 @@ where
let amt_to_forward = htlc_msat - route_hop.fee_msat;
htlc_msat = amt_to_forward;

let err_packet = match decrypt_onion_error_packet(&mut encrypted_packet, shared_secret) {
Ok(p) => p,
Err(_) => return,
};
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);

let um = gen_um_from_shared_secret(shared_secret.as_ref());
let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&err_packet.encode()[32..]);
hmac.input(&encrypted_packet[32..]);

if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &err_packet.hmac) {
if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &encrypted_packet[..32]) {
return;
}

let err_packet =
match msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(&encrypted_packet)) {
Ok(p) => p,
Err(_) => {
log_warn!(logger, "Unreadable failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
});
let short_channel_id = Some(route_hop.short_channel_id);
res = Some(FailureLearnings {
network_update,
short_channel_id,
payment_failed_permanently: is_from_final_node,
failed_within_blinded_path: false,
});
return;
},
};

let error_code_slice = match err_packet.failuremsg.get(0..2) {
Some(s) => s,
None => {
// Useless packet that we can't use but it passed HMAC, so it definitely came from the peer
// in question
log_warn!(logger, "Missing error code in failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
Expand DownExpand Up@@ -1219,6 +1239,12 @@ where
} else {
// only not set either packet unparseable or hmac does not match with any
// payment not retryable only when garbage is from the final node
log_warn!(
logger,
"Non-attributable failure encountered on route {}",
path.hops.iter().map(|h| h.pubkey.to_string()).collect::<Vec<_>>().join("->")
);

DecodedOnionFailure {
network_update: None,
short_channel_id: None,
Expand DownExpand Up@@ -1764,7 +1790,10 @@ fn decode_next_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(

#[cfg(test)]
mod tests {
use std::sync::Arc;

use crate::io;
use crate::ln::channelmanager::PaymentId;
use crate::ln::msgs;
use crate::routing::router::{Path, PaymentParameters, Route, RouteHop};
use crate::types::features::{ChannelFeatures, NodeFeatures};
Expand All@@ -1773,6 +1802,7 @@ mod tests {

#[allow(unused_imports)]
use crate::prelude::*;
use crate::util::test_utils::TestLogger;

use bitcoin::hex::FromHex;
use bitcoin::secp256k1::Secp256k1;
Expand All@@ -1785,40 +1815,95 @@ mod tests {
SecretKey::from_slice(&<Vec<u8>>::from_hex(hex).unwrap()[..]).unwrap()
}

fn build_test_path() -> Path {
Path {
hops: vec![
RouteHop {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt wanted this

pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 0,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 1,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a move, but just these short_channel_id's made unique to make it easier to test things for attr errors

fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 2,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 3,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 4,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
],
blinded_tail: None,
}
}

fn build_test_onion_keys() -> Vec<OnionKeys> {
// Keys from BOLT 4, used in both test vector tests
let secp_ctx = Secp256k1::new();

let route = Route {
paths: vec![Path { hops: vec![
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
], blinded_tail: None }],
route_params: None,
};
let path = build_test_path();
let route = Route { paths: vec![path], route_params: None };

let onion_keys =
super::construct_onion_keys(&secp_ctx, &route.paths[0], &get_test_session_key())
Expand DownExpand Up@@ -2078,6 +2163,108 @@ mod tests {
);
let hex = "9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d";
assert_eq!(onion_packet_5.data, <Vec<u8>>::from_hex(hex).unwrap());

let logger: Arc<TestLogger> = Arc::new(TestLogger::new());
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

// Assert that the original failure can be retrieved and that all hmacs check out.
let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, onion_packet_5.data);

assert_eq!(decrypted_failure.onion_error_code, Some(0x2002));
}

#[test]
fn test_non_attributable_failure_packet_onion() {
// Create a failure packet with bogus data.
let packet = vec![1u8; 292];

// In the current protocol, it is unfortunately not possible to identify the failure source.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet);
assert_eq!(decrypted_failure.short_channel_id, None);

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Non-attributable failure encountered",
1,
);
}

#[test]
fn test_unreadable_failure_packet_onion() {
// Create a failure packet with a valid hmac but unreadable failure message.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

// The failure message is a single 0 byte.
let mut packet = [0u8; 33];

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet[32..]);
let hmac = Hmac::from_engine(hmac).to_byte_array();
packet[..32].copy_from_slice(&hmac);

let packet = encrypt_failure_packet(shared_secret, &packet);

// For the unreadable failure, it is still expected that the failing channel can be identified.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains("lightning::ln::onion_utils", "Unreadable failure", 1);
}

#[test]
fn test_missing_error_code() {
// Create a failure packet with a valid hmac and structure, but no error code.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

let failuremsg = vec![1];
let pad = Vec::new();
let mut packet = msgs::DecodedOnionErrorPacket { hmac: [0; 32], failuremsg, pad };

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet.encode()[32..]);
packet.hmac = Hmac::from_engine(hmac).to_byte_array();

let packet = encrypt_failure_packet(shared_secret, &packet.encode()[..]);

let logger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Missing error code in failure",
1,
);
}

fn test_failure_attribution(logger: &TestLogger, packet: &[u8]) -> DecodedOnionFailure {
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, packet.into());

decrypted_failure
}

struct RawOnionHopData {
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); Log cases where an onion failure cannot be attributed or interpreted by joostjager · Pull Request #3629 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 232 additions & 45 deletions lightning/src/ln/onion_utils.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -931,15 +931,11 @@ pub(crate) struct DecodedOnionFailure {
pub(crate) onion_error_data: Option<Vec<u8>>,
}

/// Note that we always decrypt `packet` in-place here even if the deserialization into
/// [`msgs::DecodedOnionErrorPacket`] ultimately fails.
fn decrypt_onion_error_packet(
packet: &mut Vec<u8>, shared_secret: SharedSecret,
) -> Result<msgs::DecodedOnionErrorPacket, msgs::DecodeError> {
/// Decrypt the error packet in-place.
fn decrypt_onion_error_packet(packet: &mut Vec<u8>, shared_secret: SharedSecret) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost wonder whether it's one of those instances where in-place processing is what largely contributes to confusion. Getting a result back with the decrypted packet or with the decryption error, without worrying about the input argument getting changed, seems preferable.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The confusion was mainly that this used to be a function that returned a value and still had a side effect too. A side effect that was also needed, because the decrypted packet needs to be processed for the next hop.

Could have returned a decrypted copy in addition to the decoded failure to make it a pure function, probably with some performance implications. But I think now that this function has no return value anymore and only decrypts in-place, it's already a lot better.

let ammag = gen_ammag_from_shared_secret(shared_secret.as_ref());
let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
chacha.process_in_place(packet);
msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(packet))
}

/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
Expand DownExpand Up@@ -1021,9 +1017,11 @@ where
{
// Actually parse the onion error data in tests so we can check that blinded hops fail
// back correctly.
let err_packet =
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret)
.unwrap();
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);
let err_packet = msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(
&encrypted_packet,
))
.unwrap();
error_code_ret = Some(u16::from_be_bytes(
err_packet.failuremsg.get(0..2).unwrap().try_into().unwrap(),
));
Expand All@@ -1044,22 +1042,44 @@ where
let amt_to_forward = htlc_msat - route_hop.fee_msat;
htlc_msat = amt_to_forward;

let err_packet = match decrypt_onion_error_packet(&mut encrypted_packet, shared_secret) {
Ok(p) => p,
Err(_) => return,
};
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);

let um = gen_um_from_shared_secret(shared_secret.as_ref());
let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&err_packet.encode()[32..]);
hmac.input(&encrypted_packet[32..]);

if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &err_packet.hmac) {
if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &encrypted_packet[..32]) {
return;
}

let err_packet =
match msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(&encrypted_packet)) {
Ok(p) => p,
Err(_) => {
log_warn!(logger, "Unreadable failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
});
let short_channel_id = Some(route_hop.short_channel_id);
res = Some(FailureLearnings {
network_update,
short_channel_id,
payment_failed_permanently: is_from_final_node,
failed_within_blinded_path: false,
});
return;
},
};

let error_code_slice = match err_packet.failuremsg.get(0..2) {
Some(s) => s,
None => {
// Useless packet that we can't use but it passed HMAC, so it definitely came from the peer
// in question
log_warn!(logger, "Missing error code in failure from {}", route_hop.pubkey);

let network_update = Some(NetworkUpdate::NodeFailure {
node_id: route_hop.pubkey,
is_permanent: true,
Expand DownExpand Up@@ -1219,6 +1239,12 @@ where
} else {
// only not set either packet unparseable or hmac does not match with any
// payment not retryable only when garbage is from the final node
log_warn!(
logger,
"Non-attributable failure encountered on route {}",
path.hops.iter().map(|h| h.pubkey.to_string()).collect::<Vec<_>>().join("->")
);

DecodedOnionFailure {
network_update: None,
short_channel_id: None,
Expand DownExpand Up@@ -1764,7 +1790,10 @@ fn decode_next_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(

#[cfg(test)]
mod tests {
use std::sync::Arc;

use crate::io;
use crate::ln::channelmanager::PaymentId;
use crate::ln::msgs;
use crate::routing::router::{Path, PaymentParameters, Route, RouteHop};
use crate::types::features::{ChannelFeatures, NodeFeatures};
Expand All@@ -1773,6 +1802,7 @@ mod tests {

#[allow(unused_imports)]
use crate::prelude::*;
use crate::util::test_utils::TestLogger;

use bitcoin::hex::FromHex;
use bitcoin::secp256k1::Secp256k1;
Expand All@@ -1785,40 +1815,95 @@ mod tests {
SecretKey::from_slice(&<Vec<u8>>::from_hex(hex).unwrap()[..]).unwrap()
}

fn build_test_path() -> Path {
Path {
hops: vec![
RouteHop {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt wanted this

pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 0,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 1,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a move, but just these short_channel_id's made unique to make it easier to test things for attr errors

fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 2,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 3,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(
&<Vec<u8>>::from_hex(
"02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145",
)
.unwrap()[..],
)
.unwrap(),
channel_features: ChannelFeatures::empty(),
node_features: NodeFeatures::empty(),
short_channel_id: 4,
fee_msat: 0,
cltv_expiry_delta: 0,
maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
],
blinded_tail: None,
}
}

fn build_test_onion_keys() -> Vec<OnionKeys> {
// Keys from BOLT 4, used in both test vector tests
let secp_ctx = Secp256k1::new();

let route = Route {
paths: vec![Path { hops: vec![
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
RouteHop {
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
},
], blinded_tail: None }],
route_params: None,
};
let path = build_test_path();
let route = Route { paths: vec![path], route_params: None };

let onion_keys =
super::construct_onion_keys(&secp_ctx, &route.paths[0], &get_test_session_key())
Expand DownExpand Up@@ -2078,6 +2163,108 @@ mod tests {
);
let hex = "9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d";
assert_eq!(onion_packet_5.data, <Vec<u8>>::from_hex(hex).unwrap());

let logger: Arc<TestLogger> = Arc::new(TestLogger::new());
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

// Assert that the original failure can be retrieved and that all hmacs check out.
let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, onion_packet_5.data);

assert_eq!(decrypted_failure.onion_error_code, Some(0x2002));
}

#[test]
fn test_non_attributable_failure_packet_onion() {
// Create a failure packet with bogus data.
let packet = vec![1u8; 292];

// In the current protocol, it is unfortunately not possible to identify the failure source.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet);
assert_eq!(decrypted_failure.short_channel_id, None);

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Non-attributable failure encountered",
1,
);
}

#[test]
fn test_unreadable_failure_packet_onion() {
// Create a failure packet with a valid hmac but unreadable failure message.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

// The failure message is a single 0 byte.
let mut packet = [0u8; 33];

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet[32..]);
let hmac = Hmac::from_engine(hmac).to_byte_array();
packet[..32].copy_from_slice(&hmac);

let packet = encrypt_failure_packet(shared_secret, &packet);

// For the unreadable failure, it is still expected that the failing channel can be identified.
let logger: TestLogger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains("lightning::ln::onion_utils", "Unreadable failure", 1);
}

#[test]
fn test_missing_error_code() {
// Create a failure packet with a valid hmac and structure, but no error code.
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
let shared_secret = onion_keys[0].shared_secret.as_ref();
let um = gen_um_from_shared_secret(&shared_secret);

let failuremsg = vec![1];
let pad = Vec::new();
let mut packet = msgs::DecodedOnionErrorPacket { hmac: [0; 32], failuremsg, pad };

let mut hmac = HmacEngine::<Sha256>::new(&um);
hmac.input(&packet.encode()[32..]);
packet.hmac = Hmac::from_engine(hmac).to_byte_array();

let packet = encrypt_failure_packet(shared_secret, &packet.encode()[..]);

let logger = TestLogger::new();
let decrypted_failure = test_failure_attribution(&logger, &packet.data);
assert_eq!(decrypted_failure.short_channel_id, Some(0));

logger.assert_log_contains(
"lightning::ln::onion_utils",
"Missing error code in failure",
1,
);
}

fn test_failure_attribution(logger: &TestLogger, packet: &[u8]) -> DecodedOnionFailure {
let ctx_full = Secp256k1::new();
let path = build_test_path();
let htlc_source = HTLCSource::OutboundRoute {
path,
session_priv: get_test_session_key(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([1; 32]),
};

let decrypted_failure =
process_onion_failure(&ctx_full, &logger, &htlc_source, packet.into());

decrypted_failure
}

struct RawOnionHopData {
Expand Down