-
Notifications
You must be signed in to change notification settings - Fork 1.8k
C++: Promote cpp/access-memory-location-after-end-buffer-strncat
out of experimental
#5938
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
C++: Promote cpp/access-memory-location-after-end-buffer-strncat
out of experimental
#5938
Conversation
…nd-buffer-strncat' into 'cpp/unsafe-strncat'.
…rncat' into the tests from 'cpp/unsafe-strncat'.
|
Hi @MathiasVP, this week's docs content team's first responder here👋🏻 Thanks for ping. I'll add this to our review board and a writer will pick it up for review in their next reviewing session. |
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 think this case fits very well in the existing query. Good call. 👍
unsigned max_size = sizeof(buffers->array); | ||
unsigned free_size = max_size - len_array; | ||
strncat(buffers->array, s, free_size); // BAD | ||
} |
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.
What about the test cases from experimental
involving "fix"
and MAX_SIZE
and len + 1
?
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've added these in e857ac1. Those tests make the query look better than it really is since they're only GOOD
because we don't handle the malloc
case in the query. It doesn't hurt to have them, though.
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.
Yeah, there are a few obvious cases missing. If we make improvements to support malloc
at some point we'll want to add them.
cpp/ql/src/Likely Bugs/Memory Management/SuspiciousCallToStrncat.ql
Outdated
Show resolved
Hide resolved
This is a weird case - I think their code is safe but buggy, as an overflow causes it to put Results LGTM. This is clearly a fairly common error. |
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'm happy with this. We should merge it after the docs team review.
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.
@MathiasVP - this LGTM ✨
One typo and 2 other comments for your consideration.
Approving this so my review is not blocking you.
* @name Potentially unsafe call to strncat | ||
* @description Calling 'strncat' with the size of the destination buffer | ||
* as the third argument may result in a buffer overflow. | ||
* @description Calling `strncat` with the size of the destination buffer as the third argument may result in a buffer overflow. |
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.
The description is a bit long but happy for you to leave it as is if there's no workaround
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.
Thanks! Fixed in 78cc8f0.
cpp/ql/src/Likely Bugs/Memory Management/SuspiciousCallToStrncat.qhelp
Outdated
Show resolved
Hide resolved
<overview> | ||
<p>The standard library function <code>strncat</code> appends a source string to a target string. | ||
The third argument defines the maximum number of characters to append and should be less than or equal to the remaining space in the destination buffer. Calls of the form <code>strncat(dest, src, strlen(dest))</code> or <code>strncat(dest, src, sizeof(dest))</code> set the third argument to the entire size of the destination buffer. Executing a call of this type may cause a buffer overflow unless the buffer is known to be empty. Buffer overflows can lead to anything from a segmentation fault to a security vulnerability.</p> | ||
The third argument defines the maximum number of characters to append and should be less than or equal to the remaining space in the destination buffer. |
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.
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.
Oh! Good point. Fixed in 5382ef7.
Co-authored-by: mc <[email protected]>
@geoffw0 I believe all comments (including documentation) have been addressed now. |
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.
Changes LGTM.
(Fixes https://github.com/github/codeql-c-team/issues/275.)
This PR promotes
cpp/access-memory-location-after-end-buffer-strncat
out of experimental by moving the bad case identified in #5009 into thecpp/unsafe-strncat
query. The new case contributes to a couple of new results on the usual LGTM projects: https://lgtm.com/query/6385023132115309042/.It could be argued that the result on
pmacct/pmacct
is a false positive as there's a guard that handles the bad case a couple of lines above the place where we raise an alert. I still think it would be in the interest of the programmer to fix it, though.@github/docs-content-codeql, would you please review the
.qhelp
and@description
changes and the change-note in this PR?