|
2 | 2 | * @name Buffer overflow from insufficient space or incorrect size calculation |
3 | 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 |
6 | 5 | * @id cpp/overflow-calculated |
7 | 6 | * @problem.severity warning |
8 | 7 | * @security-severity 9.8 |
@@ -43,20 +42,33 @@ predicate spaceProblem(FunctionCall append, string msg) { |
43 | 42 |
|
44 | 43 | predicate wideCharSizeofProblem(FunctionCall call, string msg) { |
45 | 44 | exists( |
46 | | - Variable buffer, SizeofExprOperator sizeofOp, ArrayType arrayType |
| 45 | + Variable buffer, SizeofExprOperator sizeofOp |
47 | 46 | | |
48 | 47 | // Function call is to wcsftime |
49 | 48 | call.getTarget().hasGlobalOrStdName("wcsftime") and |
50 | 49 | // Second argument (count parameter) is a sizeof operation |
51 | 50 | call.getArgument(1) = sizeofOp and |
52 | 51 | // The sizeof is applied to a buffer variable |
53 | 52 | 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." |
| 53 | + ( |
| 54 | + // Case 1: Array of wchar_t - sizeof gives bytes instead of element count |
| 55 | + exists(ArrayType arrayType | |
| 56 | + arrayType = buffer.getType() and |
| 57 | + arrayType.getBaseType().hasName("wchar_t") and |
| 58 | + msg = |
| 59 | + "Using sizeof(" + buffer.getName() + ") passes byte count instead of wchar_t element count to wcsftime. " + |
| 60 | + "Use sizeof(" + buffer.getName() + ")/sizeof(wchar_t) or array length instead." |
| 61 | + ) |
| 62 | + or |
| 63 | + // Case 2: Pointer to wchar_t - sizeof gives pointer size, which is completely wrong |
| 64 | + exists(PointerType ptrType | |
| 65 | + ptrType = buffer.getType() and |
| 66 | + ptrType.getBaseType().hasName("wchar_t") and |
| 67 | + msg = |
| 68 | + "Using sizeof(" + buffer.getName() + ") passes pointer size instead of buffer size to wcsftime. " + |
| 69 | + "Pass the actual element count or use a length variable instead." |
| 70 | + ) |
| 71 | + ) |
60 | 72 | ) |
61 | 73 | } |
62 | 74 |
|
|
0 commit comments