Skip to content

base64: fix out-of-bounds write in htp_base64_decode#461

Open
zhangjy1014 wants to merge 1 commit intoOISF:0.5.xfrom
zhangjy1014:fix-issue-458
Open

base64: fix out-of-bounds write in htp_base64_decode#461
zhangjy1014 wants to merge 1 commit intoOISF:0.5.xfrom
zhangjy1014:fix-issue-458

Conversation

@zhangjy1014
Copy link

The htp_base64_decode function previously performed a write to the output buffer (*plainchar) to store the partial result of the next byte before verifying if the output buffer had sufficient space. This resulted in a heap-buffer-overflow when the provided output buffer was undersized or exactly full.

This patch fixes the issue by:

  1. Moving the partial byte assignment after the buffer length check.
  2. Correctly updating the decoder state (decoder->step and decoder->plainchar) when the buffer limit is reached, ensuring the state is preserved without writing out-of-bounds.

Fixes #458

This patch is generated by ASKRepair, an agentic automated vulnerability repair framework

@zhangjy1014
Copy link
Author

Hi @catenacyber, please take a look

@catenacyber
Copy link
Contributor

Thanks for the work.
Could we have some tests for the different cases ?

@zhangjy1014
Copy link
Author

Hi @catenacyber, thanks for your review, I've added several tests. The patched version passes all of them, while the unpatched version triggers an ASan error on each of them.

@catenacyber
Copy link
Contributor

Could you squash together all the commits ?
Otherwise, looks good to me

…tput buffer (*plainchar) to store the partial result of the next byte before verifying if the output buffer had sufficient space. This resulted in a heap-buffer-overflow when the provided output buffer was undersized or exactly full.

This patch fixes the issue by:

Moving the partial byte assignment after the buffer length check.
Correctly updating the decoder state (decoder->step and decoder->plainchar) when the buffer limit is reached, ensuring the state is preserved without writing out-of-bounds.
Fixes OISF#458

This patch is generated by ASKRepair, an agentic automated vulnerability repair framework
@zhangjy1014
Copy link
Author

Could you squash together all the commits ? Otherwise, looks good to me

squashed, thanks!

@zhangjy1014
Copy link
Author

Since this fixes an out-of-bounds write issue, could you help merge it when convenient?
Thanks again @catenacyber !

@catenacyber
Copy link
Contributor

The commit message should be updated : the first line should be much shorter: like base64: fix bounds checks
(sorry I forgot to review this earlier)

As for merging, I do not know, you can see I have #460 pending as well ;-)

Copy link
Contributor

@catenacyber catenacyber left a comment

Choose a reason for hiding this comment

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

This is good for me @victorjulien

Copy link
Contributor

@jufajardini jufajardini left a comment

Choose a reason for hiding this comment

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

Please see my inline comments, and also please consider Victor (#463 (comment)) and Philippe's feedback on the commit format, it's not following our standard. (you can see c990dc3 for an example)

bstr_free(out);
}

// Tests for issue #458: htp_base64_decode out-of-bounds write with undersized output buffer
Copy link
Contributor

Choose a reason for hiding this comment

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

If we have this comment, we should also have one indicating where do such tests end. :P

Comment on lines +73 to +74
int ret = htp_base64_decode(&dec, in, 4, out, 1);
EXPECT_EQ(1, ret);
Copy link
Contributor

Choose a reason for hiding this comment

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

Here and in similar calls, can't we simply call htp_base64_decode from EXPECT_EQ as it's done for other tests?

@jufajardini
Copy link
Contributor

Since this fixes an out-of-bounds write issue, could you help merge it when convenient? Thanks again @catenacyber !

Thanks for your work, but we need some further improvements to make merging possible :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Out-of-bounds write in htp_base64_decode

4 participants