forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 19
Fix FPs in sizeof queries #305
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c0d29f2
Initial refactor to address false positives in sizeof misuse queries.
bdrodes 91b12ae
Additional FP tweaking, removing redundant test cases, not sure why t…
bdrodes 5262824
Update cpp/ql/src/Microsoft/Likely Bugs/SizeOfMisuse/SizeOfConstIntMa…
bdrodes c6b48c3
updated comment.
bdrodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 74 additions & 36 deletions
110
cpp/ql/src/Microsoft/Likely Bugs/SizeOfMisuse/SizeOfTypeUtils.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,83 @@ | ||
| import cpp | ||
|
|
||
| /** | ||
| * Holds if `type` is a `Type` that typically should not be used for `sizeof` in macros or function return values. | ||
| * Determines if the sizeOfExpr is ignorable. | ||
| */ | ||
| predicate isTypeDangerousForSizeof(Type type) { | ||
| ( | ||
| type instanceof IntegralOrEnumType and | ||
| // ignore string literals | ||
| not type instanceof WideCharType and | ||
| not type instanceof CharType | ||
| predicate ignorableSizeof(SizeofExprOperator sizeofExpr) { | ||
| // a common pattern found is to sizeof a binary operation to check a type | ||
| // to then perfomr an onperaiton for a 32 or 64 bit type. | ||
| // these cases often look like sizeof(x) >=4 | ||
| // more generally we see binary operations frequently used in different type | ||
| // checks, where the sizeof is part of some comparison operation of a switch statement guard. | ||
| // sizeof as an argument is also similarly used, but seemingly less frequently. | ||
| exists(ComparisonOperation comp | comp.getAnOperand() = sizeofExpr) | ||
| or | ||
| exists(ConditionalStmt s | s.getControllingExpr() = sizeofExpr) | ||
| or | ||
| // another common practice is to use bit-wise operations in sizeof to allow the compiler to | ||
| // 'pack' the size appropriate but get the size of the result out of a sizeof operation. | ||
| sizeofExpr.getExprOperand() instanceof BinaryBitwiseOperation | ||
| or | ||
| // Known intentional misuses in corecrt_math.h | ||
| // Windows SDK corecrt_math.h defines a macro _CLASS_ARG that | ||
| // intentionally misuses sizeof to determine the size of a floating point type. | ||
| // Explicitly ignoring any hit in this macro. | ||
| exists(MacroInvocation mi | | ||
| mi.getMacroName() = "_CLASS_ARG" and | ||
| mi.getMacro().getFile().getBaseName() = "corecrt_math.h" and | ||
| mi.getAnExpandedElement() = sizeofExpr | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Holds if `type` is a `Type` that typically should not be used for `sizeof` in macros or function return values. | ||
| * This predicate extends the types detected in exchange of precision. | ||
| * For higher precision, please use `isTypeDangerousForSizeof` | ||
| */ | ||
| predicate isTypeDangerousForSizeofLowPrecision(Type type) { | ||
| ( | ||
| // UINT8/BYTE are typedefs to char, so we treat them separately. | ||
| // WCHAR is sometimes a typedef to UINT16, so we treat it separately too. | ||
| type.getName() = "UINT8" | ||
| or | ||
| type.getName() = "BYTE" | ||
| or | ||
| not type.getName() = "WCHAR" and | ||
| exists(Type ut | | ||
| ut = type.getUnderlyingType() and | ||
| ut instanceof IntegralOrEnumType and | ||
| not ut instanceof WideCharType and | ||
| not ut instanceof CharType | ||
| ) | ||
| or | ||
| // the linux minmax.h header has macros that intentionally miuse sizeof, | ||
| // for type checking, see __typecheck | ||
| // This code has been observed in kernel.h as well. | ||
| // Ignoring cases in linux build_bug.h and bug.h see BUILD_BUG_ON_INVALID | ||
| // Ignoring cases of uses of FP_XSTATE_MAGIC2_SIZE found in sigcontext.h | ||
| // which uses sizeof a constant as a way to get an architecturally agnostic size by | ||
| // using the special magic number constant already defined | ||
| exists(MacroInvocation mi | | ||
| ( | ||
| // Generally ignore anything from these linux headers | ||
| mi.getMacro().getFile().getBaseName() in [ | ||
| "minmax.h", "build_bug.h", "kernel.h", "bug.h", "sigcontext.h" | ||
| ] and | ||
| mi.getMacro().getFile().getRelativePath().toLowerCase().matches("%linux%") | ||
| or | ||
| // Sometimes the same macros are copied into other files, so also check the macro name | ||
| // this is redundant, but the first check above blocks all macros in these headers | ||
| // while this second check blocks any copies of these specific macros if found elsewhere. | ||
| mi.getMacroName() = "FP_XSTATE_MAGIC2_SIZE" | ||
| or | ||
| mi.getMacroName() = "__typecheck" | ||
| ) and | ||
| mi.getAnExpandedElement() = sizeofExpr | ||
| ) | ||
| or | ||
| // if the operand is a macro invocation of something resembling "null" | ||
| // assume sizeof is intended for strings, and ignore as this is a | ||
| // potential null pointer issue, not a misuse of sizeof. | ||
| exists(MacroInvocation mi | | ||
| mi.getAnExpandedElement() = sizeofExpr.getExprOperand() and | ||
| mi.getMacroName().toLowerCase().matches("%null%") | ||
| ) | ||
| or | ||
| // LLVM has known test cases under gcc-torture, ignore any hits under any matching directory | ||
| // see for example 20020226-1.c | ||
| sizeofExpr.getFile().getRelativePath().toLowerCase().matches("%gcc-%torture%") | ||
| or | ||
| // The user seems to be ignoring the output of the sizeof by casting the sizeof to void | ||
| // this has been observed as a common pattern in assert macros (I believe for disabling asserts in release builds). | ||
| // NOTE: having to check the conversion's type rather than the conversion itself | ||
| // i.e., checking if VoidConversion | ||
| // as nesting in parenthesis creats a ParenConversion | ||
| sizeofExpr.getExplicitlyConverted().getUnspecifiedType() instanceof VoidType | ||
| or | ||
| // A common macro seen that gets size of arguments, considered ignorable | ||
| exists(MacroInvocation mi | | ||
| mi.getMacroName() = "_SDT_ARGSIZE" and mi.getAnExpandedElement() = sizeofExpr | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Holds if the `Function` return type is dangerous as input for `sizeof`. | ||
| */ | ||
| class FunctionWithTypeDangerousForSizeofLowPrecision extends Function { | ||
| FunctionWithTypeDangerousForSizeofLowPrecision() { | ||
| exists(Type type | type = this.getType() | isTypeDangerousForSizeofLowPrecision(type)) | ||
| } | ||
| class CandidateSizeofCall extends SizeofExprOperator { | ||
| CandidateSizeofCall() { not ignorableSizeof(this) } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.