Skip to content

Commit 5a4a63f

Browse files
authored
Create IfStatementAdditionOverflow.ql
1 parent cd59640 commit 5a4a63f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @name Integer addition may overflow inside if statement
3+
* @description Detects "if (a+b>c) a=c-b", which is incorrect if a+b overflows.
4+
* Should be replaced by "if (a>c-b) a=c-b", which correctly
5+
* implements a = min(a,c-b)". This integer overflow is the root
6+
* cause of the buffer overflow in the SHA-3 reference implementation
7+
* (CVE-2022-37454).
8+
* @kind problem
9+
* @problem.severity warning
10+
* @id cpp/if-statement-addition-overflow
11+
* @tags: experimental
12+
* correctness
13+
* security
14+
* external/cwe/cwe-190
15+
*/
16+
17+
import cpp
18+
19+
from IfStmt ifstmt, GTExpr gtexpr, ExprStmt exprstmt, AssignExpr assignexpr, AddExpr addexpr, SubExpr subexpr
20+
where ifstmt.getCondition() = gtexpr and
21+
gtexpr.getLeftOperand() = addexpr and
22+
ifstmt.getThen() = exprstmt and
23+
exprstmt.getExpr() = assignexpr and
24+
assignexpr.getRValue() = subexpr and
25+
addexpr.getLeftOperand().toString() = assignexpr.getLValue().toString() and
26+
addexpr.getRightOperand().toString() = subexpr.getRightOperand().toString() and
27+
gtexpr.getRightOperand().toString() = subexpr.getLeftOperand().toString()
28+
select ifstmt, "Integer addition may overflow inside if statement."

0 commit comments

Comments
 (0)