Skip to content

Commit 286fc27

Browse files
authored
Brodes/nested sizeof or operation in sizeof audit fixes (#310)
* False positive fix regarding common type check idioms. * Simplifying sizeof query output messages, and making both consistent with each other.
1 parent 1dd488b commit 286fc27

File tree

4 files changed

+55
-25
lines changed

4 files changed

+55
-25
lines changed

cpp/ql/src/Microsoft/Likely Bugs/SizeOfMisuse/ArgumentIsSizeofOrOperation.ql

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,48 @@
1111
import cpp
1212
import SizeOfTypeUtils
1313

14-
from CandidateSizeofCall sizeofExpr, string message, Expr op
15-
where
16-
exists(string tmpMsg |
14+
predicate isIgnorableBinaryOperation(BinaryOperation op) {
15+
// FP case: precompilation type checking idiom of the form:
16+
// sizeof((type *)0 == (ptr))
17+
op instanceof EqualityOperation and
18+
exists(Literal zeroOperand, Expr other, Type t |
19+
other = op.getAnOperand() and
20+
other != zeroOperand and
21+
zeroOperand = op.getAnOperand() and
22+
zeroOperand.getValue().toInt() = 0 and
23+
zeroOperand.getImplicitlyConverted().hasExplicitConversion() and
24+
zeroOperand.getExplicitlyConverted().getUnspecifiedType() = t and
25+
// often 'NULL' is defined as (void *)0, ignore these cases
26+
not t instanceof VoidPointerType and
1727
(
18-
op instanceof BinaryOperation and tmpMsg = "binary operator"
28+
// Apparently derived types, eg., functoin pointers, aren't PointerType
29+
// according to codeql, so special casing them out here.
30+
other.getUnspecifiedType() instanceof DerivedType
1931
or
20-
op instanceof SizeofOperator and tmpMsg = "sizeof"
21-
) and
22-
if sizeofExpr.isInMacroExpansion()
23-
then message = tmpMsg + "(in a macro expansion)"
24-
else message = tmpMsg
32+
other.getUnspecifiedType() instanceof PointerType
33+
)
34+
)
35+
}
36+
37+
class CandidateOperation extends Operation {
38+
CandidateOperation() {
39+
// For now only considering binary operations
40+
// TODO: Unary operations may be of interest but need special care
41+
// as pointer deref, and address-of are unary operations.
42+
// It is therefore more likely to get false positives if unary operations are included.
43+
// To be considered in the future.
44+
this instanceof BinaryOperation and
45+
not isIgnorableBinaryOperation(this)
46+
}
47+
}
48+
49+
from CandidateSizeofCall sizeofExpr, string inMacro, string argType, Expr op
50+
where
51+
(
52+
op instanceof CandidateOperation and argType = "binary operator"
53+
or
54+
op instanceof SizeofOperator and argType = "sizeof operation"
2555
) and
56+
(if sizeofExpr.isInMacroExpansion() then inMacro = " (in a macro expansion) " else inMacro = " ") and
2657
op = sizeofExpr.getExprOperand()
27-
select sizeofExpr, "$@: $@ of $@ inside sizeof.", sizeofExpr, message,
28-
sizeofExpr.getEnclosingFunction(), "Usage", op, message
58+
select sizeofExpr, "sizeof" + inMacro + "has a " + argType + " argument: $@.", op, op.toString()

cpp/ql/src/Microsoft/Likely Bugs/SizeOfMisuse/SizeOfConstIntMacro.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ where
7272
isSizeOfExprOperandMacroInvocationAConstInteger(sizeofExpr, mi, _) and
7373
(if sizeofExpr.isInMacroExpansion() then inMacro = " (in a macro expansion) " else inMacro = " ")
7474
select sizeofExpr,
75-
"$@: sizeof" + inMacro +
76-
"of integer macro $@ will always return the size of the underlying integer type.", sizeofExpr,
77-
sizeofExpr.getEnclosingFunction().getName(), mi.getMacro(), mi.getMacro().getName()
75+
"sizeof" + inMacro +
76+
"of integer macro $@ will always return the size of the underlying integer type.",
77+
mi.getMacro(), mi.getMacro().getName()
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
| test.cpp:89:6:89:29 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.cpp:89:6:89:29 | sizeof(<expr>) | binary operator | test.cpp:66:6:66:11 | Test01 | Usage | test.cpp:89:13:89:28 | ... / ... | binary operator |
2-
| test.cpp:96:6:96:30 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.cpp:96:6:96:30 | sizeof(<expr>) | binary operator | test.cpp:66:6:66:11 | Test01 | Usage | test.cpp:96:13:96:29 | ... * ... | binary operator |
3-
| test.cpp:98:6:98:35 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.cpp:98:6:98:35 | sizeof(<expr>) | binary operator | test.cpp:66:6:66:11 | Test01 | Usage | test.cpp:98:13:98:34 | ... * ... | binary operator |
4-
| test.cpp:101:6:101:31 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.cpp:101:6:101:31 | sizeof(<expr>) | sizeof | test.cpp:66:6:66:11 | Test01 | Usage | test.cpp:101:13:101:30 | sizeof(int) | sizeof |
5-
| test.cpp:120:6:120:24 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.cpp:120:6:120:24 | sizeof(<expr>) | sizeof | test.cpp:66:6:66:11 | Test01 | Usage | test.cpp:120:13:120:23 | sizeof(int) | sizeof |
6-
| test.cpp:121:6:121:18 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.cpp:121:6:121:18 | sizeof(<expr>) | binary operator | test.cpp:66:6:66:11 | Test01 | Usage | test.cpp:121:13:121:17 | ... + ... | binary operator |
1+
| test.cpp:89:6:89:29 | sizeof(<expr>) | sizeof has a binary operator argument: $@. | test.cpp:89:13:89:28 | ... / ... | ... / ... |
2+
| test.cpp:96:6:96:30 | sizeof(<expr>) | sizeof has a binary operator argument: $@. | test.cpp:96:13:96:29 | ... * ... | ... * ... |
3+
| test.cpp:98:6:98:35 | sizeof(<expr>) | sizeof has a binary operator argument: $@. | test.cpp:98:13:98:34 | ... * ... | ... * ... |
4+
| test.cpp:101:6:101:31 | sizeof(<expr>) | sizeof has a sizeof operation argument: $@. | test.cpp:101:13:101:30 | sizeof(int) | sizeof(int) |
5+
| test.cpp:120:6:120:24 | sizeof(<expr>) | sizeof has a sizeof operation argument: $@. | test.cpp:120:13:120:23 | sizeof(int) | sizeof(int) |
6+
| test.cpp:121:6:121:18 | sizeof(<expr>) | sizeof has a binary operator argument: $@. | test.cpp:121:13:121:17 | ... + ... | ... + ... |
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| test.cpp:75:6:75:42 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test.cpp:75:6:75:42 | sizeof(<expr>) | Test01 | test.cpp:48:1:48:48 | #define SOMESTRUCT_ERRNO_THAT_MATTERS 0x8000000d | SOMESTRUCT_ERRNO_THAT_MATTERS |
2-
| test.cpp:83:10:83:32 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test.cpp:83:10:83:32 | sizeof(<expr>) | Test01 | test.cpp:2:1:2:26 | #define BAD_MACRO_CONST 5l | BAD_MACRO_CONST |
3-
| test.cpp:84:6:84:29 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test.cpp:84:6:84:29 | sizeof(<expr>) | Test01 | test.cpp:3:1:3:35 | #define BAD_MACRO_CONST2 0x80005001 | BAD_MACRO_CONST2 |
4-
| test.cpp:92:7:92:35 | sizeof(<expr>) | $@: sizeof (in a macro expansion) of integer macro $@ will always return the size of the underlying integer type. | test.cpp:92:7:92:35 | sizeof(<expr>) | Test01 | test.cpp:1:1:1:19 | #define PAGESIZE 64 | PAGESIZE |
5-
| test.cpp:116:6:116:37 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test.cpp:116:6:116:37 | sizeof(<expr>) | Test01 | test.cpp:32:1:32:45 | #define ACE_CONDITION_SIGNATURE2 'xt' | ACE_CONDITION_SIGNATURE2 |
1+
| test.cpp:75:6:75:42 | sizeof(<expr>) | sizeof of integer macro $@ will always return the size of the underlying integer type. | test.cpp:48:1:48:48 | #define SOMESTRUCT_ERRNO_THAT_MATTERS 0x8000000d | SOMESTRUCT_ERRNO_THAT_MATTERS |
2+
| test.cpp:83:10:83:32 | sizeof(<expr>) | sizeof of integer macro $@ will always return the size of the underlying integer type. | test.cpp:2:1:2:26 | #define BAD_MACRO_CONST 5l | BAD_MACRO_CONST |
3+
| test.cpp:84:6:84:29 | sizeof(<expr>) | sizeof of integer macro $@ will always return the size of the underlying integer type. | test.cpp:3:1:3:35 | #define BAD_MACRO_CONST2 0x80005001 | BAD_MACRO_CONST2 |
4+
| test.cpp:92:7:92:35 | sizeof(<expr>) | sizeof (in a macro expansion) of integer macro $@ will always return the size of the underlying integer type. | test.cpp:1:1:1:19 | #define PAGESIZE 64 | PAGESIZE |
5+
| test.cpp:116:6:116:37 | sizeof(<expr>) | sizeof of integer macro $@ will always return the size of the underlying integer type. | test.cpp:32:1:32:45 | #define ACE_CONDITION_SIGNATURE2 'xt' | ACE_CONDITION_SIGNATURE2 |

0 commit comments

Comments
 (0)