Skip to content

Conversation

@felickz
Copy link
Contributor

@felickz felickz commented Jun 10, 2025

This pull request improves the detection of buffer overflow issues in the OverflowCalculated.ql query.

Improvements to buffer overflow detection:

  • Enhanced query description: The description of the query has been updated to include scenarios involving incorrect size calculations for wide character functions, improving clarity and scope. Additionally, the precision level was set to "medium" to better reflect the query's behavior. (cpp/ql/src/Critical/OverflowCalculated.ql, cpp/ql/src/Critical/OverflowCalculated.qlL2-R5)

  • New predicate for wide character size issues: Added the wideCharSizeofProblem predicate to detect cases where the sizeof operator is incorrectly used with wcsftime, leading to potential buffer overflows. The predicate checks for miscalculations in the count parameter by ensuring that the size is expressed in terms of wchar_t elements rather than bytes. (cpp/ql/src/Critical/OverflowCalculated.ql, cpp/ql/src/Critical/OverflowCalculated.qlR44-R64)

  • Updated query logic: Modified the where clause in the query to include the new wideCharSizeofProblem predicate, enabling the detection of both traditional space problems and wide character size issues. (cpp/ql/src/Critical/OverflowCalculated.ql, cpp/ql/src/Critical/OverflowCalculated.qlR44-R64)

@github-actions github-actions bot added the C++ label Jun 10, 2025
felickz added 2 commits June 11, 2025 13:03
- Pointer case: sizeof(pointer) gives pointer size (e.g., 8 bytes), which is completely wrong
@@ -0,0 +1,4 @@
---
category: newQuery
Copy link
Contributor Author

@felickz felickz Jun 11, 2025

Choose a reason for hiding this comment

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

This query did exist previously but did not have any precision so it was not included in any suite.

Also enhanced the query to find additional scenarios - what would be best category?

* @name Buffer overflow from insufficient space or incorrect size calculation
* @description A buffer allocated using 'malloc' may not have enough space for a string being copied into it, or wide character functions may receive incorrect size parameters causing buffer overrun. Make sure that buffers contain enough room for strings (including zero terminator) and that size parameters are correctly calculated.
* @kind problem
* @precision medium
Copy link
Contributor Author

@felickz felickz Jun 11, 2025

Choose a reason for hiding this comment

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

This will add to security-extended suite. Review findings in DCA to ensure FP rate is low. FN's have not been evaluated by me - only added coverage for the above scenario only.

ql/cpp/ql/src/Critical/IncorrectCheckScanf.ql
ql/cpp/ql/src/Critical/MissingCheckScanf.ql
ql/cpp/ql/src/Critical/NewFreeMismatch.ql
ql/cpp/ql/src/Critical/OverflowCalculated.ql
Copy link
Contributor Author

@felickz felickz Jun 12, 2025

Choose a reason for hiding this comment

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

Hand rolled this, security-and-quality, and not_included_in

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant