Skip to content
Business logic Audit finding Intermediate

Incorrect decoding in decodeLockTwpTapDstMsg

The decoding function for a message used incorrect slice lengths, allowing multiple byte sequences to decode to the same values. An attacker could craft a malformed message that decodes to a valid-looking position, potentially bypassing validation.

GalloDaSballo Follow with KIntern_NA

Originally disclosed on Code4rena

Submitted by GalloDaSballo, also found by KIntern_NA

The decoding applied in decodeLockTwpTapDstMsg is incorrect as there are more than one combination of bytes that would result in the same result.

This is due to: uint96 duration = BytesLib.toUint96(BytesLib.slice(_msg, userOffset_, durationOffset_), 0); , which uses length == durationOffset_ which is 32 instead of 12.

uint256 amount = BytesLib.toUint256(BytesLib.slice(_msg, durationOffset_, _msg.length - durationOffset_), 0); uses the length of the message, instead of 32 which would be the maximum size of a u256.

POC

This was found with Medusa, using Recon.

The test is as follows:

    function malformedTokenTwTapPositionMsg(bytes memory encoded) public {
        LockTwTapPositionMsg memory decoded = TapTokenCodec.decodeLockTwpTapDstMsg(encoded);
        bytes memory ReEncoded = TapTokenCodec.buildLockTwTapPositionMsg(decoded);

        emit DebugBytes(encoded);
        emit DebugBytes(ReEncoded);
        t(BytesLib.equal(encoded, ReEncoded), "tokenTwTapPositionMsg");
    }

And a repro case is:

function test_malformedTokenTwTapPositionMsg_codec() public {
    malformedTokenTwTapPositionMsg(624640ab9f2104f27610f40e02a5184fc6e28e66473292539dccd55a499f6ce9e6103ab8afd0ef7ebd66e9b36707fb0a70bd2db5189ae20d11df6743e19fd653638b193c3e611e038a16fcc61179eebc1d4c);
}

Mitigation

Change:

uint96 duration = BytesLib.toUint96(BytesLib.slice(_msg, userOffset_, durationOffset_), 0);

to

uint96 duration = BytesLib.toUint96(BytesLib.slice(_msg, userOffset_, 12), 0);, which will prevent reading the wrong area of memory.

Change:

uint256 amount = BytesLib.toUint256(BytesLib.slice(_msg, durationOffset_, _msg.length - durationOffset_), 0);

to

uint256 amount = BytesLib.toUint256(BytesLib.slice(_msg, durationOffset_, 32), 0);, which will ensure that the bytes being read are the length of the message, instead of 32 which would be the maximum size of a u256.

Assessed type

en/de-code

cryptotechmaker (Tapioca) confirmed via duplicate Issue #144


The original report as published on Code4rena, by GalloDaSballo. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the slice lengths passed to the byte-to-integer conversion functions were wrong: 32 instead of 12 for the duration field, and the remaining message length instead of 32 for the amount field.

Impact

An attacker could craft a malformed message that decodes to the same values as a valid one, potentially bypassing validation or causing unexpected behaviour.

What the attacker needed

Ability to submit a crafted message

The fix

The slice lengths should be corrected to 12 for the duration field and 32 for the amount field.

How it was found

  1. Call the malformed message test function with a crafted byte sequence.
  2. The function decodes the message using decodeLockTwpTapDstMsg.
  3. It re-encodes the decoded message using buildLockTwTapPositionMsg.
  4. It checks whether the original and re-encoded bytes are equal.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A decoder turns bytes into data. Here, the code read the wrong number of bytes for two fields, so different byte strings could produce the same decoded values. This means a crafted message might be accepted as valid even though it is not the original.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by GalloDaSballo

View all

30 more disclosed reports, best first.

More from Tapioca Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.