Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong gas price calculation in eth_send_raw_unsigned_tx could lead to suboptimal transaction pricing #133

Closed
howlbot-integration bot opened this issue Nov 9, 2024 · 5 comments
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working edited-by-warden primary issue Highest quality submission among a set of duplicates 🤖_47_group AI based duplicate group recommendation sufficient quality report This report is of sufficient quality unsatisfactory does not satisfy C4 submission criteria; not eligible for awards

Comments

@howlbot-integration
Copy link

Lines of code

https://github.com/kkrt-labs/kakarot/blob/7411a5520e8a00be6f5243a50c160e66ad285563/src/kakarot/eth_rpc.cairo#L304-L307

Vulnerability details

Proof of Concept

eth_rpc.cairo::eth_send_raw_unsigned_tx underprices transactions, which could lead to delayed or failed transactions in congested network conditions.

Take a look at this part of the code:

https://github.com/kkrt-labs/kakarot/blob/7411a5520e8a00be6f5243a50c160e66ad285563/src/kakarot/eth_rpc.cairo#L304-L307

let possible_priority_fee = tx.max_fee_per_gas - block_base_fee;
let priority_fee_is_max_priority_fee = is_nn(
    possible_priority_fee - tx.max_priority_fee_per_gas
);
let priority_fee_per_gas = priority_fee_is_max_priority_fee * tx.max_priority_fee_per_gas + (
    1 - priority_fee_is_max_priority_fee
) * possible_priority_fee;
let effective_gas_price = priority_fee_per_gas + block_base_fee;

The problem is in the calculation of priority_fee_per_gas. The logic attempts to choose between tx.max_priority_fee_per_gas and possible_priority_fee, but it does so incorrectly.

The issue is that priority_fee_is_max_priority_fee is set to 1 when possible_priority_fee is greater than or equal to tx.max_priority_fee_per_gas, which is the opposite of what we actually want. This means that when the possible_priority_fee is larger, the function will choose tx.max_priority_fee_per_gas instead of the smaller value.

As a result, in some cases, the priority_fee_per_gas could be set higher/lower than intended leading to users paying more/less in gas fees than necessary.

In most cases, transactions may be underpriced relative to what the user is willing to pay. In congested network conditions, transactions may end up being delayed or failing to be processed cos they're not utilizing the full gas price the user was willing to pay.

Short Note:

I discussed this issue with the sponsors and they confirmed this bug case

Recommended Mitigation Steps

The calculation of priority_fee_is_max_priority_fee should be inverted.

- let priority_fee_is_max_priority_fee = is_nn(
-     possible_priority_fee - tx.max_priority_fee_per_gas
- );
+ let priority_fee_is_max_priority_fee = is_nn(
+     tx.max_priority_fee_per_gas - possible_priority_fee
+ );

Assessed type

Error

@howlbot-integration howlbot-integration bot added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value 🤖_47_group AI based duplicate group recommendation bug Something isn't working edited-by-warden primary issue Highest quality submission among a set of duplicates sufficient quality report This report is of sufficient quality labels Nov 9, 2024
howlbot-integration bot added a commit that referenced this issue Nov 9, 2024
@c4-judge
Copy link
Contributor

c4-judge commented Nov 9, 2024

dmvt marked the issue as selected for report

@c4-judge c4-judge added the selected for report This submission will be included/highlighted in the audit report label Nov 9, 2024
@dmvt
Copy link

dmvt commented Nov 9, 2024

@obatirou
Copy link

This means that when the possible_priority_fee is larger, the function will choose tx.max_priority_fee_per_gas instead of the smaller value.

This is a wrong interpretation of our discord answer. The EIP1559 says that one should pick the smaller between possible and given max, which is the implemented behavior.

There is no wrong calculation here. This finding is invalid

@dmvt
Copy link

dmvt commented Nov 16, 2024

This is a wrong interpretation of our discord answer. The EIP1559 says that one should pick the smaller between possible and given max, which is the implemented behavior.

I missed this on my first review of the issue. The sponsor is correct here. The "max" is actually the variable defined in the first line highlighted by the warden let possible_priority_fee = tx.max_fee_per_gas - block_base_fee;, making the second line correct.

@c4-judge c4-judge removed the selected for report This submission will be included/highlighted in the audit report label Nov 16, 2024
@c4-judge
Copy link
Contributor

dmvt marked the issue as unsatisfactory:
Invalid

@c4-judge c4-judge added the unsatisfactory does not satisfy C4 submission criteria; not eligible for awards label Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working edited-by-warden primary issue Highest quality submission among a set of duplicates 🤖_47_group AI based duplicate group recommendation sufficient quality report This report is of sufficient quality unsatisfactory does not satisfy C4 submission criteria; not eligible for awards
Projects
None yet
Development

No branches or pull requests

3 participants