Skip to content

Commit 482a2b9

Browse files
Merge pull request #1282 from TheBlueMatt/2022-01-fuzz-overflow
Avoid overflow in addition when checking counterparty feerates
2 parents 457e48e + b54fe5f commit 482a2b9

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

lightning/src/ln/channel.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -942,14 +942,6 @@ impl<Signer: Sign> Channel<Signer> {
942942
fn check_remote_fee<F: Deref>(fee_estimator: &F, feerate_per_kw: u32) -> Result<(), ChannelError>
943943
where F::Target: FeeEstimator
944944
{
945-
let lower_limit = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
946-
// Some fee estimators round up to the next full sat/vbyte (ie 250 sats per kw), causing
947-
// occasional issues with feerate disagreements between an initiator that wants a feerate
948-
// of 1.1 sat/vbyte and a receiver that wants 1.1 rounded up to 2. Thus, we always add 250
949-
// sat/kw before the comparison here.
950-
if feerate_per_kw + 250 < lower_limit {
951-
return Err(ChannelError::Close(format!("Peer's feerate much too low. Actual: {}. Our expected lower limit: {} (- 250)", feerate_per_kw, lower_limit)));
952-
}
953945
// We only bound the fee updates on the upper side to prevent completely absurd feerates,
954946
// always accepting up to 25 sat/vByte or 10x our fee estimator's "High Priority" fee.
955947
// We generally don't care too much if they set the feerate to something very high, but it
@@ -959,6 +951,14 @@ impl<Signer: Sign> Channel<Signer> {
959951
if feerate_per_kw as u64 > upper_limit {
960952
return Err(ChannelError::Close(format!("Peer's feerate much too high. Actual: {}. Our expected upper limit: {}", feerate_per_kw, upper_limit)));
961953
}
954+
let lower_limit = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
955+
// Some fee estimators round up to the next full sat/vbyte (ie 250 sats per kw), causing
956+
// occasional issues with feerate disagreements between an initiator that wants a feerate
957+
// of 1.1 sat/vbyte and a receiver that wants 1.1 rounded up to 2. Thus, we always add 250
958+
// sat/kw before the comparison here.
959+
if feerate_per_kw + 250 < lower_limit {
960+
return Err(ChannelError::Close(format!("Peer's feerate much too low. Actual: {}. Our expected lower limit: {} (- 250)", feerate_per_kw, lower_limit)));
961+
}
962962
Ok(())
963963
}
964964

@@ -6154,6 +6154,13 @@ mod tests {
61546154
"MAX_FUNDING_SATOSHIS is greater than all satoshis in existence");
61556155
}
61566156

6157+
#[test]
6158+
fn test_no_fee_check_overflow() {
6159+
// Previously, calling `check_remote_fee` with a fee of 0xffffffff would overflow in
6160+
// arithmetic, causing a panic with debug assertions enabled.
6161+
assert!(Channel::<InMemorySigner>::check_remote_fee(&&TestFeeEstimator { fee_est: 42 }, u32::max_value()).is_err());
6162+
}
6163+
61576164
struct Keys {
61586165
signer: InMemorySigner,
61596166
}

0 commit comments

Comments
 (0)