-
-
Notifications
You must be signed in to change notification settings - Fork 959
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
Fix potential buffer overflows when calling sprintf #1742
Conversation
Build size and comparison to main:
|
Welcome to InfiniTime! 😊 I think it would be helpful if you write a little description about the motivations of the changes you made. |
These aren't overflows, truncations at best and modulo is relatively quite expensive. The other parts, would be very nice if you would explain them. |
a4a5693
to
fdf24db
Compare
A recent commit introduces a real one-byte buffer overflow. InfiniTime/src/displayapp/screens/StopWatch.cpp Lines 201 to 206 in 40f7e1c
The format str "#%2d %2d:%02d:%02d.%02d\n" requires 17 bytes, but the buffer length is only 16. This is fixed in this PR.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that replacing sprintf
with snprintf
is necessarily a good idea. I believe that because it adds an overhead and that, with the current configuration, I don't think it does anything if it detects an overflow, we shouldn't use it.
I agree that That said, if you insist on using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After looking into the overhead that snprintf adds, I've come to the conclusion that it is fine to use it over sprintf as an added layer of safety. We should of course still make sure that our values can never be higher than what we expect and that our buffers are big enough.
For some reason changing the format specifier macro to PRIu16 fixes the issue I mentioned before. Once that is fixed, I'm willing to get this merged.
1. Replace sprintf with snprintf, which is safer 2. An unsigned int or unsigned long int requires 11 bytes to print (including the null terminator) 3. Use PRIu16 macro to print uint16_t 4. Format string "#%2d %2d:%02d:%02d.%02d\n" in StopWatch::stopLapBtnEventHandler() requires at least 17 bytes. The 16-byte buffer would clearly be overrun if sprintf were used.
Given that 2^16 / 1000 is 65, we can make the buffer only 3 chars.
so that it's just as long as with the hour.
442d875
to
8e17945
Compare
@FintasticMan Rebase done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Hello @FintasticMan, @Avamander, @NeroBurner , My name is Aravind Machiry, Assistant Professor at Purdue's ECE Department. Thank you for considering this pull request. This pull request was the result of our on-going research work (along with @szsam) to improve the security and quality of open-source embedded projects. In addition to scanning codebases with CodeQL, we are also doing a short (~4 minutes) survey to understand the use of static analysis tools like It would greatly benefit our research if you could fill this anonymous survey: https://purdue.ca1.qualtrics.com/jfe/form/SV_0OnXfr5plPe1QCa Thank you, |
No description provided.