Skip to content

Commit f8b25ce

Browse files
author
Sam Lanning
authored
Merge pull request #3 from kevinbackhouse/demos
QL Demos
2 parents 23a3060 + 5c553ea commit f8b25ce

105 files changed

Lines changed: 3901 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.DS_Store
1+
*~
2+
/.metadata/

ql_demos/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.cache

ql_demos/cpp/.project

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ql-demos-cpp</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
<nature>com.semmle.plugin.qdt.core.qlnature</nature>
11+
</natures>
12+
</projectDescription>

ql_demos/cpp/.qlpath

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<ns2:qlpath xmlns:ns2="https://semmle.com/schemas/qlpath">
3+
<librarypath>
4+
<path kind="PLUGIN">com.semmle.code.cpp.library</path>
5+
</librarypath>
6+
<dbscheme kind="PLUGIN">com.semmle.code.cpp.dbscheme</dbscheme>
7+
<defaultImports>
8+
<defaultImport>cpp</defaultImport>
9+
</defaultImports>
10+
</ns2:qlpath>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import cpp
2+
3+
predicate isSmall(Expr e) {
4+
e.getType().getSize() < 4
5+
}
6+
7+
from AddExpr a, Variable v, RelationalOperation cmp
8+
where a.getAnOperand() = v.getAnAccess()
9+
and cmp.getAnOperand() = a
10+
and cmp.getAnOperand() = v.getAnAccess()
11+
and forall(Expr op | op = a.getAnOperand() | isSmall(op))
12+
and not isSmall(a.getExplicitlyConverted())
13+
select cmp, "Bad overflow check"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Use [this snapshot](https://downloads.lgtm.com/snapshots/cpp/microsoft/chakracore/ChakraCore-revision-2017-April-12--18-13-26.zip)
2+
3+
We now also have this query in our default suite: https://lgtm.com/rules/2156560627/
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import cpp
2+
3+
/** Matches `var < var + ???`. */
4+
predicate overflowCheck(LocalScopeVariable var, AddExpr add, RelationalOperation compare) {
5+
compare.getAnOperand() = var.getAnAccess() and
6+
compare.getAnOperand() = add and
7+
add.getAnOperand() = var.getAnAccess()
8+
}
9+
10+
from LocalScopeVariable var, AddExpr add
11+
where overflowCheck(var, add, _)
12+
select add, "Overflow check on variable of type " + var.getUnderlyingType()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import cpp
2+
3+
/** Matches `var < var + ???`. */
4+
predicate overflowCheck(LocalScopeVariable var, AddExpr add, RelationalOperation compare) {
5+
compare.getAnOperand() = var.getAnAccess() and
6+
compare.getAnOperand() = add and
7+
add.getAnOperand() = var.getAnAccess()
8+
}
9+
10+
from LocalScopeVariable var, AddExpr add
11+
where overflowCheck(var, add, _)
12+
and var.getType().getSize() < 4
13+
select add, "Overflow check on variable of type " + var.getUnderlyingType()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import cpp
2+
3+
/** Matches `var < var + ???`. */
4+
predicate overflowCheck(LocalScopeVariable var, AddExpr add, RelationalOperation compare) {
5+
compare.getAnOperand() = var.getAnAccess() and
6+
compare.getAnOperand() = add and
7+
add.getAnOperand() = var.getAnAccess()
8+
}
9+
10+
from LocalScopeVariable var, AddExpr add
11+
where overflowCheck(var, add, _)
12+
and var.getType().getSize() < 4
13+
and not add.getConversion+().getType().getSize() < 4
14+
select add, "Bad overflow check on variable of type " + var.getUnderlyingType()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @name Fizz Overflow
3+
* @description Narrowing conversions on untrusted data could enable
4+
* an attacker to trigger an integer overflow.
5+
* @kind path-problem
6+
* @problem.severity warning
7+
*/
8+
9+
import cpp
10+
import semmle.code.cpp.ir.dataflow.TaintTracking
11+
import semmle.code.cpp.ir.IR
12+
import DataFlow::PathGraph
13+
14+
/**
15+
* The endianness conversion function `Endian::big()`.
16+
* It is Folly's replacement for `ntohs` and `ntohl`.
17+
*/
18+
class EndianConvert extends Function {
19+
EndianConvert() {
20+
this.getName() = "big" and
21+
this.getDeclaringType().getName().matches("Endian")
22+
}
23+
}
24+
25+
class Cfg extends TaintTracking::Configuration {
26+
Cfg() { this = "FizzOverflowIR" }
27+
28+
/** Holds if `source` is a call to `Endian::big()`. */
29+
override predicate isSource(DataFlow::Node source) {
30+
source
31+
.asInstruction()
32+
.(CallInstruction)
33+
.getCallTarget()
34+
.(FunctionInstruction)
35+
.getFunctionSymbol() instanceof EndianConvert
36+
}
37+
38+
/** Holds if `sink` is a narrowing conversion. */
39+
override predicate isSink(DataFlow::Node sink) {
40+
sink.asInstruction().getResultSize() < sink
41+
.asInstruction()
42+
.(ConvertInstruction)
43+
.getUnary()
44+
.getResultSize()
45+
}
46+
}
47+
48+
from
49+
Cfg cfg, DataFlow::PathNode source, DataFlow::PathNode sink, ConvertInstruction conv,
50+
Type inputType, Type outputType
51+
where
52+
cfg.hasFlowPath(source, sink) and
53+
conv = sink.getNode().asInstruction() and
54+
inputType = conv.getUnary().getResultType() and
55+
outputType = conv.getResultType()
56+
select sink, source, sink,
57+
"Conversion of untrusted data from " + inputType + " to " + outputType + "."

0 commit comments

Comments
 (0)