|
| 1 | +/** |
| 2 | + * @name Linux kernel double-fetch vulnerability detection |
| 3 | + * @description Double-fetch is a very common vulnerability pattern |
| 4 | + * in linux kernel, attacker can exploit double-fetch |
| 5 | + * issues to obatain root privilege. |
| 6 | + * Double-fetch is caused by fetching data from user |
| 7 | + * mode by calling copy_from_user twice, CVE-2016-6480 |
| 8 | + * is quite a good example for your information. |
| 9 | + * @kind problem |
| 10 | + * @id cpp/linux-kernel-double-fetch-vulnerability |
| 11 | + * @problem.severity warning |
| 12 | + * @security-severity 7.5 |
| 13 | + * @tags security |
| 14 | + * external/cwe/cwe-362 |
| 15 | + */ |
| 16 | + |
| 17 | +import cpp |
| 18 | +import semmle.code.cpp.valuenumbering.GlobalValueNumbering |
| 19 | + |
| 20 | +class CopyFromUserFunctionCall extends FunctionCall { |
| 21 | + CopyFromUserFunctionCall() { |
| 22 | + this.getTarget().getName() = "copy_from_user" and |
| 23 | + not this.getArgument(1) instanceof AddressOfExpr |
| 24 | + } |
| 25 | + |
| 26 | + //root cause of double-fetech issue is read from |
| 27 | + //the same user mode memory twice, so it makes |
| 28 | + //sense that only check user mode pointer |
| 29 | + predicate readFromSameUserModePointer(CopyFromUserFunctionCall another) { |
| 30 | + globalValueNumber(this.getArgument(1)) = globalValueNumber(another.getArgument(1)) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +from CopyFromUserFunctionCall p1, CopyFromUserFunctionCall p2 |
| 35 | +where |
| 36 | + not p1 = p2 and |
| 37 | + p1.readFromSameUserModePointer(p2) and |
| 38 | + exists(IfStmt ifStmt | |
| 39 | + p1.getBasicBlock().getAFalseSuccessor*() = ifStmt.getBasicBlock() and |
| 40 | + ifStmt.getBasicBlock().getAFalseSuccessor*() = p2.getBasicBlock() |
| 41 | + ) and |
| 42 | + not exists(AssignPointerAddExpr assignPtrAdd | |
| 43 | + globalValueNumber(p1.getArgument(1)) = globalValueNumber(assignPtrAdd.getLValue()) and |
| 44 | + p1.getBasicBlock().getAFalseSuccessor*() = assignPtrAdd.getBasicBlock() |
| 45 | + ) |
| 46 | +select p2, "Double fetch vulnerability. First fetch was $@.", p1, p1.toString() |
0 commit comments