|
1 | 1 | /** |
2 | | - * @name Buffer not sufficient for string |
3 | | - * @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). |
| 2 | + * @name Buffer overflow from insufficient space or incorrect size calculation |
| 3 | + * @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. |
4 | 4 | * @kind problem |
| 5 | + * @precision medium |
5 | 6 | * @id cpp/overflow-calculated |
6 | 7 | * @problem.severity warning |
7 | 8 | * @security-severity 9.8 |
@@ -40,6 +41,25 @@ predicate spaceProblem(FunctionCall append, string msg) { |
40 | 41 | ) |
41 | 42 | } |
42 | 43 |
|
| 44 | +predicate wideCharSizeofProblem(FunctionCall call, string msg) { |
| 45 | + exists( |
| 46 | + Variable buffer, SizeofExprOperator sizeofOp, ArrayType arrayType |
| 47 | + | |
| 48 | + // Function call is to wcsftime |
| 49 | + call.getTarget().hasGlobalOrStdName("wcsftime") and |
| 50 | + // Second argument (count parameter) is a sizeof operation |
| 51 | + call.getArgument(1) = sizeofOp and |
| 52 | + // The sizeof is applied to a buffer variable |
| 53 | + sizeofOp.getExprOperand() = buffer.getAnAccess() and |
| 54 | + // The buffer is an array of wchar_t |
| 55 | + arrayType = buffer.getType() and |
| 56 | + arrayType.getBaseType().hasName("wchar_t") and |
| 57 | + msg = |
| 58 | + "Using sizeof(" + buffer.getName() + ") passes byte count instead of wchar_t element count to wcsftime. " + |
| 59 | + "Use sizeof(" + buffer.getName() + ")/sizeof(wchar_t) or array length instead." |
| 60 | + ) |
| 61 | +} |
| 62 | + |
43 | 63 | from Expr problem, string msg |
44 | | -where spaceProblem(problem, msg) |
| 64 | +where spaceProblem(problem, msg) or wideCharSizeofProblem(problem, msg) |
45 | 65 | select problem, msg |
0 commit comments