Attackathon _ Fuel Network 33175 - [Smart Contract - High] Sway-lib Subtract i Logic Vulnerability
Description
Vulnerability Details
impl core::ops::Subtract for I8 {
/// Subtract a I8 from a I8. Panics of overflow.
fn subtract(self, other: Self) -> Self {
let mut res = Self::new();
if self.underlying >= Self::indent()
&& other.underlying >= Self::indent()
{
if self.underlying > other.underlying {
res = Self::from_uint(self.underlying - other.underlying + Self::indent());
} else {
res = Self::from_uint(self.underlying - (other.underlying - Self::indent()));
}
} else if self.underlying >= Self::indent()
&& other.underlying < Self::indent()
{
res = Self::from_uint(self.underlying - Self::indent() + other.underlying);
} else if self.underlying < Self::indent()
&& other.underlying >= Self::indent()
{
res = Self::from_uint(self.underlying - (other.underlying - Self::indent()));
} else if self.underlying < Self::indent()
&& other.underlying < Self::indent()
{
if self.underlying < other.underlying {
res = Self::from_uint(other.underlying - self.underlying + Self::indent());
} else {
res = Self::from_uint(self.underlying + other.underlying - Self::indent());
}
}
res
}
}The issue is in the last branch of the conditional statement:
As example to trigger the bug
Impact is loss of funds because of
Recommendation
Proof of concept
Add this PoC test in the end of the i8.sw
PreviousAttackathon _ Fuel Network 33172 - [Smart Contract - Insight] OOB in type_check_analyze of ImplTraitNextAttackathon _ Fuel Network 33181 - [Smart Contract - Insight] users messages might encode incorrect
Last updated
Was this helpful?