Merge pull request #10931 from hvitved/ruby/fix-flow-into-phis

Ruby: Fix flow steps into phi nodes
This commit is contained in:
Tom Hvitved 2022-11-02 21:07:06 +01:00 коммит произвёл GitHub
Родитель 8eee450c65 ee9163aa40
Коммит 46631d6eaf
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 106 добавлений и 13 удалений

Просмотреть файл

@ -76,19 +76,29 @@ module LocalFlow {
private import codeql.ruby.dataflow.internal.SsaImpl
/**
* Holds if `nodeFrom` is a last node referencing SSA definition `def`, which
* Holds if `nodeFrom` is a node for SSA definition `def`, which can reach `next`.
*/
private predicate localFlowSsaInputFromDef(
SsaDefinitionNode nodeFrom, Ssa::Definition def, Ssa::Definition next
) {
exists(BasicBlock bb, int i |
lastRefBeforeRedef(def, bb, i, next) and
def = nodeFrom.getDefinition() and
def.definesAt(_, bb, i)
)
}
/**
* Holds if `exprFrom` is a last read of SSA definition `def`, which
* can reach `next`.
*/
private predicate localFlowSsaInput(Node nodeFrom, Ssa::Definition def, Ssa::Definition next) {
exists(BasicBlock bb, int i | lastRefBeforeRedef(def, bb, i, next) |
def = nodeFrom.(SsaDefinitionNode).getDefinition() and
def.definesAt(_, bb, i)
or
exists(CfgNodes::ExprCfgNode e |
e = nodeFrom.asExpr() and
e = bb.getNode(i) and
e.getExpr() instanceof VariableReadAccess
)
predicate localFlowSsaInputFromExpr(
CfgNodes::ExprCfgNode exprFrom, Ssa::Definition def, Ssa::Definition next
) {
exists(BasicBlock bb, int i |
lastRefBeforeRedef(def, bb, i, next) and
exprFrom = bb.getNode(i) and
exprFrom.getExpr() instanceof VariableReadAccess
)
}
@ -139,9 +149,9 @@ module LocalFlow {
// Flow from read to next read
localSsaFlowStepUseUse(def, nodeFrom.(PostUpdateNode).getPreUpdateNode(), nodeTo)
or
// Flow into phi node
// Flow into phi node from definition
exists(Ssa::PhiNode phi |
localFlowSsaInput(nodeFrom, def, phi) and
localFlowSsaInputFromDef(nodeFrom, def, phi) and
phi = nodeTo.(SsaDefinitionNode).getDefinition() and
def = phi.getAnInput()
)
@ -308,6 +318,18 @@ private module Cached {
LocalFlow::localSsaFlowStepUseUse(_, nodeFrom, nodeTo) and
not FlowSummaryImpl::Private::Steps::prohibitsUseUseFlow(nodeFrom, _)
or
// Flow into phi node from read
exists(Ssa::Definition def, Ssa::PhiNode phi, CfgNodes::ExprCfgNode exprFrom |
LocalFlow::localFlowSsaInputFromExpr(exprFrom, def, phi) and
phi = nodeTo.(SsaDefinitionNode).getDefinition() and
def = phi.getAnInput()
|
exprFrom = nodeFrom.asExpr() and
not FlowSummaryImpl::Private::Steps::prohibitsUseUseFlow(nodeFrom, _)
or
exprFrom = nodeFrom.(PostUpdateNode).getPreUpdateNode().asExpr()
)
or
FlowSummaryImpl::Private::Steps::summaryLocalStep(nodeFrom, nodeTo, true)
}
@ -338,6 +360,14 @@ private module Cached {
)
or
LocalFlow::localSsaFlowStepUseUse(_, nodeFrom, nodeTo)
or
// Flow into phi node from read
exists(Ssa::Definition def, Ssa::PhiNode phi, CfgNodes::ExprCfgNode exprFrom |
LocalFlow::localFlowSsaInputFromExpr(exprFrom, def, phi) and
phi = nodeTo.(SsaDefinitionNode).getDefinition() and
def = phi.getAnInput() and
exprFrom = [nodeFrom.asExpr(), nodeFrom.(PostUpdateNode).getPreUpdateNode().asExpr()]
)
}
private predicate entrySsaDefinition(SsaDefinitionNode n) {

Просмотреть файл

@ -0,0 +1,20 @@
failures
edges
| ssa_flow.rb:12:9:12:9 | [post] a [element 0] : | ssa_flow.rb:16:10:16:10 | a [element 0] : |
| ssa_flow.rb:12:9:12:9 | [post] a [element 0] : | ssa_flow.rb:16:10:16:10 | a [element 0] : |
| ssa_flow.rb:12:16:12:23 | call to taint : | ssa_flow.rb:12:9:12:9 | [post] a [element 0] : |
| ssa_flow.rb:12:16:12:23 | call to taint : | ssa_flow.rb:12:9:12:9 | [post] a [element 0] : |
| ssa_flow.rb:16:10:16:10 | a [element 0] : | ssa_flow.rb:16:10:16:13 | ...[...] |
| ssa_flow.rb:16:10:16:10 | a [element 0] : | ssa_flow.rb:16:10:16:13 | ...[...] |
nodes
| ssa_flow.rb:12:9:12:9 | [post] a [element 0] : | semmle.label | [post] a [element 0] : |
| ssa_flow.rb:12:9:12:9 | [post] a [element 0] : | semmle.label | [post] a [element 0] : |
| ssa_flow.rb:12:16:12:23 | call to taint : | semmle.label | call to taint : |
| ssa_flow.rb:12:16:12:23 | call to taint : | semmle.label | call to taint : |
| ssa_flow.rb:16:10:16:10 | a [element 0] : | semmle.label | a [element 0] : |
| ssa_flow.rb:16:10:16:10 | a [element 0] : | semmle.label | a [element 0] : |
| ssa_flow.rb:16:10:16:13 | ...[...] | semmle.label | ...[...] |
| ssa_flow.rb:16:10:16:13 | ...[...] | semmle.label | ...[...] |
subpaths
#select
| ssa_flow.rb:16:10:16:13 | ...[...] | ssa_flow.rb:12:16:12:23 | call to taint : | ssa_flow.rb:16:10:16:13 | ...[...] | $@ | ssa_flow.rb:12:16:12:23 | call to taint : | call to taint : |

Просмотреть файл

@ -0,0 +1,11 @@
/**
* @kind path-problem
*/
import codeql.ruby.AST
import TestUtilities.InlineFlowTest
import PathGraph
from DataFlow::PathNode source, DataFlow::PathNode sink, DefaultValueFlowConf conf
where conf.hasFlowPath(source, sink)
select sink, source, sink, "$@", source, source.toString()

Просмотреть файл

@ -0,0 +1,32 @@
def taint x
x
end
def sink x
puts "SINK: #{x}"
end
def m1
a = Array.new
if rand() > 0 then
a[0] = taint(1)
else
a = nil
end
sink(a[0]) # $ hasValueFlow=1
end
m1
def m2
a = Array.new
if rand() > 0 then
a[0] = taint(2)
a.clear
else
a = nil
end
sink(a[0])
end
m2