Skip to content

Conversation

MathiasVP
Copy link
Contributor

@MathiasVP MathiasVP commented May 21, 2021

(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 the cpp/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?

@MathiasVP MathiasVP added C++ ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. labels May 21, 2021
@MathiasVP MathiasVP requested a review from a team as a code owner May 21, 2021 08:46
@docs-bot
Copy link
Contributor

:octocat:📚 Thanks for the docs ping! 🛎️ This was added to our docs first-responder project board. A team member will be along shortly to respond. To request changes to the docs you can also open a CodeQL docs issue.

@felicitymay
Copy link
Contributor

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.

Copy link
Contributor

@geoffw0 geoffw0 left a 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
}
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@geoffw0
Copy link
Contributor

geoffw0 commented May 24, 2021

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.

This is a weird case - I think their code is safe but buggy, as an overflow causes it to put + and a terminator at the end of the buffer but not to write the characters leading up to that, so possibly some garbage gets displayed. They'd be better off relying on strncat to do it's job (but fixing the character limit).

Results LGTM. This is clearly a fairly common error.

@mchammer01 mchammer01 self-assigned this May 25, 2021
geoffw0
geoffw0 previously approved these changes May 25, 2021
Copy link
Contributor

@geoffw0 geoffw0 left a 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.

mchammer01
mchammer01 previously approved these changes May 25, 2021
Copy link
Contributor

@mchammer01 mchammer01 left a 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.
Copy link
Contributor

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

Copy link
Contributor Author

@MathiasVP MathiasVP May 25, 2021

Choose a reason for hiding this comment

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

Thanks! Fixed in 78cc8f0.

<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.
Copy link
Contributor

Choose a reason for hiding this comment

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

All these sentences are in the same paragraph. Perhaps consider breaking this content into 2 or 3 paragraph to improve readability? Feel free to ignore if you don't agree.

one-para

Copy link
Contributor Author

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.

@MathiasVP MathiasVP dismissed stale reviews from mchammer01 and geoffw0 via f842d09 May 25, 2021 11:16
@MathiasVP
Copy link
Contributor Author

@geoffw0 I believe all comments (including documentation) have been addressed now.

Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

Changes LGTM.

@geoffw0 geoffw0 merged commit 2fd461e into github:main May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C++ documentation ready-for-doc-review This PR requires and is ready for review from the GitHub docs team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants