Skip to content

Commit af2fbd8

Browse files
authored
Merge pull request #20929 from owen-mc/go/fix-data-flow-consistency-checks
Go: fix small issues highlighted by data flow consistency checks
2 parents 3ea92ea + a20c8cf commit af2fbd8

File tree

30 files changed

+19
-268
lines changed

30 files changed

+19
-268
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
22
category: breaking
33
---
4-
* The query `go/unexpected-frontend-error` has been moved from the `codeql/go-queries` query to the `codeql-go-consistency-queries` query pack.
4+
* The query `go/unexpected-frontend-error` has been moved from the `codeql/go-queries` query to the `codeql-go-consistency-queries` query pack.

go/ql/lib/semmle/go/controlflow/IR.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,4 +1588,9 @@ module IR {
15881588
* in a field/method access, element access, or slice expression.
15891589
*/
15901590
EvalImplicitDerefInstruction implicitDerefInstruction(Expr e) { result = MkImplicitDeref(e) }
1591+
1592+
/** Gets the base of `insn`, if `insn` is an implicit field read. */
1593+
Instruction lookThroughImplicitFieldRead(Instruction insn) {
1594+
result = insn.(ImplicitFieldReadInstruction).getBaseInstruction()
1595+
}
15911596
}

go/ql/lib/semmle/go/dataflow/internal/DataFlowNodes.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,11 @@ module Public {
838838
exists(IR::MethodReadInstruction mri |
839839
ce.getTarget() instanceof Method and
840840
mri = IR::evalExprInstruction(ce.getCalleeExpr()) and
841-
insn = mri.getReceiver()
841+
// If a.x is reading a promoted field, and it's equivalent to a.b.c.x,
842+
// then mri.getReceiver() will give us the implicit field read a.b.c
843+
// and we want to have post-update nodes for a, the implicit field
844+
// read a.b and the implicit field read a.b.c.
845+
insn = IR::lookThroughImplicitFieldRead*(mri.getReceiver())
842846
)
843847
) and
844848
mutableType(insn.getResultType())

go/ql/lib/semmle/go/dataflow/internal/DataFlowPrivate.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ predicate basicLocalFlowStep(Node nodeFrom, Node nodeTo) {
8989
nodeFrom = instructionNode(pred) or
9090
nodeFrom.(PostUpdateNode).getPreUpdateNode() = instructionNode(pred)
9191
) and
92-
nodeTo = instructionNode(succ)
92+
nodeTo = instructionNode(succ) and
93+
nodeTo != nodeFrom
9394
)
9495
or
9596
// GlobalFunctionNode -> use

go/ql/lib/semmle/go/dataflow/internal/FlowSummaryImpl.qll

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,17 +397,13 @@ module SourceSinkInterpretationInput implements
397397
}
398398

399399
private DataFlow::Node skipImplicitFieldReads(DataFlow::Node n) {
400-
not exists(lookThroughImplicitFieldRead(n)) and result = n
400+
not exists(IR::lookThroughImplicitFieldRead(n.asInstruction())) and result = n
401401
or
402-
result = skipImplicitFieldReads(lookThroughImplicitFieldRead(n))
403-
}
404-
405-
private DataFlow::Node lookThroughImplicitFieldRead(DataFlow::Node n) {
406-
result.asInstruction() =
407-
n.(DataFlow::InstructionNode)
408-
.asInstruction()
409-
.(IR::ImplicitFieldReadInstruction)
410-
.getBaseInstruction()
402+
exists(DataFlow::Node mid |
403+
mid.asInstruction() = IR::lookThroughImplicitFieldRead(n.asInstruction())
404+
|
405+
result = skipImplicitFieldReads(mid)
406+
)
411407
}
412408

413409
/** Provides additional sink specification logic. */

go/ql/test/example-tests/snippets/CONSISTENCY/DataFlowConsistency.expected

Lines changed: 0 additions & 3 deletions
This file was deleted.

go/ql/test/experimental/CWE-400/CONSISTENCY/DataFlowConsistency.expected

Lines changed: 0 additions & 4 deletions
This file was deleted.

go/ql/test/experimental/CWE-522-DecompressionBombs/CONSISTENCY/DataFlowConsistency.expected

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,3 @@ reverseRead
3434
| test.go:92:19:92:25 | implicit dereference | Origin of readStep is missing a PostUpdateNode. |
3535
| test.go:93:5:93:11 | implicit dereference | Origin of readStep is missing a PostUpdateNode. |
3636
| test.go:94:9:94:15 | implicit dereference | Origin of readStep is missing a PostUpdateNode. |
37-
identityLocalStep
38-
| test.go:114:37:114:38 | rc | Node steps to itself |
39-
| test.go:198:27:198:29 | out | Node steps to itself |
40-
| test.go:224:27:224:29 | out | Node steps to itself |
41-
| test.go:249:27:249:29 | out | Node steps to itself |
42-
| test.go:274:27:274:29 | out | Node steps to itself |
43-
| test.go:299:27:299:29 | out | Node steps to itself |
44-
| test.go:324:26:324:28 | out | Node steps to itself |
45-
| test.go:349:26:349:28 | out | Node steps to itself |
46-
| test.go:375:28:375:30 | out | Node steps to itself |
47-
| test.go:403:28:403:30 | out | Node steps to itself |
48-
| test.go:431:24:431:26 | out | Node steps to itself |
49-
| test.go:463:26:463:28 | out | Node steps to itself |
50-
| test.go:490:26:490:28 | out | Node steps to itself |
51-
| test.go:517:27:517:29 | out | Node steps to itself |
52-
| test.go:546:26:546:28 | out | Node steps to itself |
53-
| test.go:571:26:571:28 | out | Node steps to itself |
54-
| test.go:601:24:601:26 | out | Node steps to itself |
55-
| test.go:614:15:614:21 | tarRead | Node steps to itself |
56-
| test.go:622:3:622:7 | files | Node steps to itself |
57-
| test.go:637:10:637:16 | tarRead | Node steps to itself |
58-
| test.go:647:10:647:16 | tarRead | Node steps to itself |

go/ql/test/experimental/CWE-942/CONSISTENCY/DataFlowConsistency.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,3 @@ reverseRead
1313
| CorsMisconfiguration.go:170:14:170:16 | implicit dereference | Origin of readStep is missing a PostUpdateNode. |
1414
| CorsMisconfiguration.go:194:17:194:19 | implicit dereference | Origin of readStep is missing a PostUpdateNode. |
1515
| CorsMisconfiguration.go:206:14:206:16 | implicit dereference | Origin of readStep is missing a PostUpdateNode. |
16-
identityLocalStep
17-
| CorsMisconfiguration.go:208:13:208:18 | origin | Node steps to itself |
18-
| CorsMisconfiguration.go:235:6:235:8 | try | Node steps to itself |
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
reverseRead
2-
| pkg1/embedding.go:56:29:56:39 | implicit dereference | Origin of readStep is missing a PostUpdateNode. |
3-
| pkg1/embedding.go:57:33:57:46 | implicit dereference | Origin of readStep is missing a PostUpdateNode. |
4-
| pkg1/embedding.go:58:14:58:22 | implicit read of field embedder | Origin of readStep is missing a PostUpdateNode. |
5-
| pkg1/embedding.go:58:31:58:42 | implicit read of field embedder | Origin of readStep is missing a PostUpdateNode. |
62
| pkg1/tst.go:43:6:43:6 | t | Origin of readStep is missing a PostUpdateNode. |
73
| pkg1/tst.go:46:6:46:6 | t | Origin of readStep is missing a PostUpdateNode. |
8-
| pkg1/tst.go:50:2:50:2 | t | Origin of readStep is missing a PostUpdateNode. |
94
| pkg1/tst.go:53:6:53:7 | t2 | Origin of readStep is missing a PostUpdateNode. |
105
| pkg1/tst.go:55:6:55:7 | t2 | Origin of readStep is missing a PostUpdateNode. |
11-
| promoted.go:18:6:18:6 | t | Origin of readStep is missing a PostUpdateNode. |

0 commit comments

Comments
 (0)