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

[apps/speed] Added checking for buflen overflow due to MAX_MISALIGNMENT. #17646

Closed
wants to merge 4 commits into from

Conversation

heavycrystal
Copy link
Contributor

If a large enough value is given to the "-bytes" option, a signed integer overflow occurs as MAX_ALIGNMENT + 1 is added to buflen. This negative value is interpreted as unsigned by malloc, which then attempts to allocate an extremely large buffer.

For now, I'm just ignoring the addition and printing a warning. I'm not sure about the semantics of the "-misalign" option, so if there is a better solution I will implement that instead.

This also might be a consideration for backporting to the 1.1.1 branch.

apps/speed.c Outdated
@@ -1778,7 +1778,12 @@ int speed_main(int argc, char **argv)
buflen = lengths[size_num - 1];
if (buflen < 36) /* size of random vector in RSA benchmark */
buflen = 36;
buflen += MAX_MISALIGNMENT + 1;
if (0x7fffffff - (MAX_MISALIGNMENT + 1) < buflen)
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be INT_MAX not 0x7fffffff.

Copy link
Member

Choose a reason for hiding this comment

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

What about using buflen + MAX_MISALIGNMENT + 1 < buflen?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@paulidale fixed. Also changed in a couple of other places where 0x7fffffff is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@t8m C doesn't actually mandate signed integer overflow to be wraparound. I'm pretty sure the platforms OpenSSL supports will indeed wraparound, but I did this just to be safe.

Copy link
Contributor

Choose a reason for hiding this comment

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

C specifies that signed integer overflow is undefined. We don't want to rely on undefined behaviour.

Copy link
Contributor

@kaduk kaduk Feb 8, 2022

Choose a reason for hiding this comment

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

[deleted duplicate comment to paulidale's point]

apps/speed.c Outdated
if (0x7fffffff - (MAX_MISALIGNMENT + 1) < buflen)
{
BIO_printf(bio_err, "Warning: ignoring -misalign option.\n");
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Because each branch of the if is but a single statement, our coding style would suggest omitting the {} on both.

I think better might be changing the warning to an error and failing. In which case, the {} should be retained.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@paulidale Made it an error, also updated help with the maximum possible buffer size for clarity.

Copy link
Contributor

Choose a reason for hiding this comment

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

Even better.

apps/speed.c Outdated Show resolved Hide resolved
@paulidale paulidale added approval: review pending This pull request needs review by a committer branch: master Merge to master branch labels Feb 8, 2022
apps/speed.c Outdated
@@ -1778,7 +1778,13 @@ int speed_main(int argc, char **argv)
buflen = lengths[size_num - 1];
if (buflen < 36) /* size of random vector in RSA benchmark */
buflen = 36;
buflen += MAX_MISALIGNMENT + 1;
if (INT_MAX - (MAX_MISALIGNMENT + 1) < buflen)
{
Copy link
Member

Choose a reason for hiding this comment

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

Placement of { should be on the end of the preceding line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed this.

apps/speed.c Outdated
{
BIO_printf(bio_err, "Error: buffer size too large\n");
goto end;
} else {
Copy link
Member

Choose a reason for hiding this comment

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

This could actually be not an "else" at all and just unconditional since the main branch ends in a goto so will never get here anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed this as well.

@t8m t8m added triaged: bug The issue/pr is/fixes a bug branch: 3.0 Merge to openssl-3.0 branch labels Feb 9, 2022
Copy link
Member

@t8m t8m left a comment

Choose a reason for hiding this comment

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

OK for master and 3.0. @paulidale ok for 3.0 too?

@t8m t8m added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Feb 9, 2022
@paulidale
Copy link
Contributor

Good for 3.0 too, I wasn't sure if it was a bug fix or not.

@openssl-machine
Copy link
Collaborator

24 hours has passed since 'approval: done' was set, but as this PR has been updated in that time the label 'approval: ready to merge' is not being automatically set. Please review the updates and set the label manually.

@paulidale
Copy link
Contributor

Merged to master and 3.0 after end of line whitespace removal.

@paulidale paulidale closed this Feb 11, 2022
openssl-machine pushed a commit that referenced this pull request Feb 11, 2022
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from #17646)
openssl-machine pushed a commit that referenced this pull request Feb 11, 2022
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from #17646)

(cherry picked from commit 4b3777c9ad4a2058a9b87afb26289039ebf4a6c1)
@heavycrystal heavycrystal deleted the fix-misalign branch February 11, 2022 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approval: done This pull request has the required number of approvals branch: master Merge to master branch branch: 3.0 Merge to openssl-3.0 branch triaged: bug The issue/pr is/fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants