-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Enhance cpp/overflow-calculated
- detect out-of-bounds write caused by passing the buffer size in bytes (using sizeof) instead of the number of elements to wcsftime, allowing the function to overrun the allocated buffer.
#19722
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
base: main
Are you sure you want to change the base?
Changes from all commits
47486a4
11786cd
6d91eff
c728c78
31e7790
7a72c49
0283466
34cc7f2
f40fcf1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
/** | ||
* @name Buffer not sufficient for string | ||
* @description A buffer allocated using 'malloc' may not have enough space for a string that is being copied into it. The operation can cause a buffer overrun. Make sure that the buffer contains enough room for the string (including the zero terminator). | ||
* @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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will add to |
||
* @id cpp/overflow-calculated | ||
* @problem.severity warning | ||
* @security-severity 9.8 | ||
|
@@ -40,6 +41,38 @@ predicate spaceProblem(FunctionCall append, string msg) { | |
) | ||
} | ||
|
||
predicate wideCharSizeofProblem(FunctionCall call, string msg) { | ||
exists(Variable buffer, SizeofExprOperator sizeofOp | | ||
// Function call is to wcsftime | ||
call.getTarget().hasGlobalOrStdName("wcsftime") and | ||
// Second argument (count parameter) is a sizeof operation | ||
call.getArgument(1) = sizeofOp and | ||
// The sizeof is applied to a buffer variable | ||
sizeofOp.getExprOperand() = buffer.getAnAccess() and | ||
( | ||
// Case 1: Array of wchar_t - sizeof gives bytes instead of element count | ||
exists(ArrayType arrayType | | ||
arrayType = buffer.getType() and | ||
arrayType.getBaseType().hasName("wchar_t") and | ||
msg = | ||
"Using sizeof(" + buffer.getName() + | ||
") passes byte count instead of wchar_t element count to wcsftime. " + "Use sizeof(" + | ||
buffer.getName() + ")/sizeof(wchar_t) or array length instead." | ||
) | ||
or | ||
// Case 2: Pointer to wchar_t - sizeof gives pointer size, which is completely wrong | ||
exists(PointerType ptrType | | ||
ptrType = buffer.getType() and | ||
ptrType.getBaseType().hasName("wchar_t") and | ||
msg = | ||
"Using sizeof(" + buffer.getName() + | ||
") passes pointer size instead of buffer size to wcsftime. " + | ||
"Pass the actual element count or use a length variable instead." | ||
) | ||
) | ||
) | ||
} | ||
|
||
from Expr problem, string msg | ||
where spaceProblem(problem, msg) | ||
where spaceProblem(problem, msg) or wideCharSizeofProblem(problem, msg) | ||
select problem, msg |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: newQuery | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This query did exist previously but did not have any Also enhanced the query to find additional scenarios - what would be best |
||
--- | ||
* Adds `cpp/overflow-calculated` to the `cpp-security-extended` query suite. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
| tests2.cpp:34:4:34:9 | call to strcat | This buffer only contains enough room for 'str1' (copied on line 33) | | ||
| tests2.cpp:52:4:52:9 | call to strcat | This buffer only contains enough room for 'str1' (copied on line 51) | | ||
| tests2.cpp:48:4:48:9 | call to strcat | This buffer only contains enough room for 'str1' (copied on line 47) | | ||
| tests2.cpp:66:4:66:9 | call to strcat | This buffer only contains enough room for 'str1' (copied on line 65) | | ||
| tests2.cpp:118:4:118:11 | call to wcsftime | Using sizeof(buf) passes byte count instead of wchar_t element count to wcsftime. Use sizeof(buf)/sizeof(wchar_t) or array length instead. | | ||
| tests2.cpp:142:4:142:11 | call to wcsftime | Using sizeof(smallBuf) passes byte count instead of wchar_t element count to wcsftime. Use sizeof(smallBuf)/sizeof(wchar_t) or array length instead. | | ||
| tests2.cpp:151:5:151:12 | call to wcsftime | Using sizeof(dynamicBuf) passes pointer size instead of buffer size to wcsftime. Pass the actual element count or use a length variable instead. | |
Uh oh!
There was an error while loading. Please reload this page.
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.
Hand rolled this, security-and-quality, and not_included_in