Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit dbf1d24

Browse files
committed
Add new barrier guard for second half of path
1 parent 35e336f commit dbf1d24

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

ql/src/semmle/go/security/AllocationSizeOverflowCustomizations.qll

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ module AllocationSizeOverflow {
7272
}
7373
}
7474

75+
/** A check of the allocation size, acting as a guard to prevent allocation-size overflow. */
76+
class AllocationSizeCheck extends DataFlow::BarrierGuard, DataFlow::RelationalComparisonNode {
77+
override predicate checks(Expr e, boolean branch) {
78+
exists(DataFlow::Node lesser | this.leq(branch, lesser, _, _) |
79+
globalValueNumber(DataFlow::exprNode(e)) = globalValueNumber(lesser)
80+
)
81+
}
82+
}
83+
7584
/**
7685
* An arithmetic operation that might overflow, and whose result is used to compute an
7786
* allocation size.
@@ -81,7 +90,8 @@ module AllocationSizeOverflow {
8190

8291
DefaultSink() {
8392
this instanceof OverflowProneOperand and
84-
localStep*(this, allocsz)
93+
localStep*(this, allocsz) and
94+
not exists(AllocationSizeCheck g | allocsz = g.getAGuardedNode())
8595
}
8696

8797
override DataFlow::Node getAllocationSize() { result = allocsz }

ql/test/query-tests/Security/CWE-190/AllocationSizeOverflow.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ edges
33
| tst2.go:9:2:9:37 | ... := ...[0] : slice type | tst2.go:10:22:10:30 | call to len |
44
| tst2.go:14:2:14:29 | ... := ...[0] : slice type | tst2.go:15:22:15:30 | call to len |
55
| tst3.go:6:2:6:31 | ... := ...[0] : slice type | tst3.go:7:22:7:34 | call to len |
6+
| tst3.go:6:2:6:31 | ... := ...[0] : slice type | tst3.go:24:16:24:28 | call to len |
7+
| tst3.go:6:2:6:31 | ... := ...[0] : slice type | tst3.go:32:16:32:28 | call to len |
68
| tst.go:14:2:14:30 | ... = ...[0] : slice type | tst.go:15:22:15:34 | call to len |
79
| tst.go:20:2:20:31 | ... = ...[0] : slice type | tst.go:21:22:21:34 | call to len |
810
| tst.go:26:2:26:31 | ... = ...[0] : slice type | tst.go:27:26:27:38 | call to len |
@@ -16,6 +18,8 @@ nodes
1618
| tst2.go:15:22:15:30 | call to len | semmle.label | call to len |
1719
| tst3.go:6:2:6:31 | ... := ...[0] : slice type | semmle.label | ... := ...[0] : slice type |
1820
| tst3.go:7:22:7:34 | call to len | semmle.label | call to len |
21+
| tst3.go:24:16:24:28 | call to len | semmle.label | call to len |
22+
| tst3.go:32:16:32:28 | call to len | semmle.label | call to len |
1923
| tst.go:14:2:14:30 | ... = ...[0] : slice type | semmle.label | ... = ...[0] : slice type |
2024
| tst.go:15:22:15:34 | call to len | semmle.label | call to len |
2125
| tst.go:20:2:20:31 | ... = ...[0] : slice type | semmle.label | ... = ...[0] : slice type |
@@ -29,6 +33,8 @@ nodes
2933
| tst2.go:10:22:10:30 | call to len | tst2.go:9:2:9:37 | ... := ...[0] : slice type | tst2.go:10:22:10:30 | call to len | This operation, which is used in an $@, involves a potentially large $@ and might overflow. | tst2.go:10:22:10:32 | ...+... | allocation | tst2.go:9:2:9:37 | ... := ...[0] : slice type | value |
3034
| tst2.go:15:22:15:30 | call to len | tst2.go:14:2:14:29 | ... := ...[0] : slice type | tst2.go:15:22:15:30 | call to len | This operation, which is used in an $@, involves a potentially large $@ and might overflow. | tst2.go:15:22:15:32 | ...+... | allocation | tst2.go:14:2:14:29 | ... := ...[0] : slice type | value |
3135
| tst3.go:7:22:7:34 | call to len | tst3.go:6:2:6:31 | ... := ...[0] : slice type | tst3.go:7:22:7:34 | call to len | This operation, which is used in an $@, involves a potentially large $@ and might overflow. | tst3.go:7:22:7:36 | ...+... | allocation | tst3.go:6:2:6:31 | ... := ...[0] : slice type | value |
36+
| tst3.go:24:16:24:28 | call to len | tst3.go:6:2:6:31 | ... := ...[0] : slice type | tst3.go:24:16:24:28 | call to len | This operation, which is used in an $@, involves a potentially large $@ and might overflow. | tst3.go:27:24:27:32 | newlength | allocation | tst3.go:6:2:6:31 | ... := ...[0] : slice type | value |
37+
| tst3.go:32:16:32:28 | call to len | tst3.go:6:2:6:31 | ... := ...[0] : slice type | tst3.go:32:16:32:28 | call to len | This operation, which is used in an $@, involves a potentially large $@ and might overflow. | tst3.go:36:23:36:31 | newlength | allocation | tst3.go:6:2:6:31 | ... := ...[0] : slice type | value |
3238
| tst.go:15:22:15:34 | call to len | tst.go:14:2:14:30 | ... = ...[0] : slice type | tst.go:15:22:15:34 | call to len | This operation, which is used in an $@, involves a potentially large $@ and might overflow. | tst.go:15:22:15:36 | ...+... | allocation | tst.go:14:2:14:30 | ... = ...[0] : slice type | value |
3339
| tst.go:21:22:21:34 | call to len | tst.go:20:2:20:31 | ... = ...[0] : slice type | tst.go:21:22:21:34 | call to len | This operation, which is used in an $@, involves a potentially large $@ and might overflow. | tst.go:21:22:21:36 | ...+... | allocation | tst.go:20:2:20:31 | ... = ...[0] : slice type | value |
3440
| tst.go:27:26:27:38 | call to len | tst.go:26:2:26:31 | ... = ...[0] : slice type | tst.go:27:26:27:38 | call to len | This operation, which is used in an $@, involves a potentially large $@ and might overflow. | tst.go:27:26:27:40 | ...+... | allocation | tst.go:26:2:26:31 | ... = ...[0] : slice type | value |

ql/test/query-tests/Security/CWE-190/tst3.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,36 @@ func testSanitizers(s string) {
1111
if len(jsonData) < 1000 {
1212
ignore(make([]byte, len(jsonData)+1)) // OK: there is an upper bound check on len(jsonData)
1313
}
14+
15+
{
16+
newlength := len(jsonData) + 2 // OK: there is an upper bound check which dominates `make`
17+
_ := newlength - 1
18+
if newlength < 1000 {
19+
ignore(make([]byte, newlength))
20+
}
21+
}
22+
23+
{
24+
newlength := len(jsonData) + 3 // NOT OK: newlength is changed after the upper bound check (even though it's made smaller)
25+
if newlength < 1000 {
26+
newlength = newlength - 1
27+
ignore(make([]byte, newlength))
28+
}
29+
}
30+
31+
{
32+
newlength := len(jsonData) + 4 // NOT OK: there is an upper bound check but it doesn't dominate `make`
33+
if newlength < 1000 {
34+
_ := newlength + 2
35+
}
36+
ignore(make([]byte, newlength))
37+
}
38+
39+
{
40+
newlength := len(jsonData) + 5 // OK: there is an upper bound check which dominates `make`
41+
if newlength > 1000 {
42+
return
43+
}
44+
ignore(make([]byte, newlength))
45+
}
1446
}

0 commit comments

Comments
 (0)