Skip to content

Commit e17a169

Browse files
authored
Merge pull request #180 from microsoft/powershell-ast-modernization-follow-up
PS: Fix PowerShell dataflow/taint-tracking failures
2 parents cea435c + 7102ebb commit e17a169

File tree

18 files changed

+589
-345
lines changed

18 files changed

+589
-345
lines changed

powershell/ql/lib/semmle/code/powershell/ast/internal/If.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,13 @@ class If extends Expr, TIf {
5353
)
5454
}
5555

56+
StmtBlock getABranch(boolean b) {
57+
b = true and result = this.getAThen()
58+
or
59+
b = false and result = this.getElse()
60+
}
61+
62+
StmtBlock getABranch() { result = this.getAThen() or result = this.getElse() }
63+
5664
predicate hasElse() { exists(this.getElse()) }
5765
}

powershell/ql/lib/semmle/code/powershell/ast/internal/ScriptBlock.qll

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ class ScriptBlock extends Ast, TScriptBlock {
7979
result = this.getParameter(index)
8080
)
8181
or
82+
i = ThisVar() and
83+
result = this.getThisParameter()
84+
or
8285
exists(int index |
8386
i = scriptBlockUsing(index) and
8487
result = this.getUsingStmt(index)
@@ -90,13 +93,14 @@ class ScriptBlock extends Ast, TScriptBlock {
9093
or
9194
any(Synthesis s).pipelineParameterHasIndex(this, i) and
9295
synthChild(getRawAst(this), PipelineParamVar(), result)
93-
or
94-
i = -1 and
95-
synthChild(getRawAst(this), ThisVar(), result)
9696
}
9797

98+
Parameter getThisParameter() { synthChild(getRawAst(this), ThisVar(), result) }
99+
98100
/**
99101
* Gets a parameter of this block.
102+
*
103+
* Note: This does not include the `this` parameter, but it does include pipeline parameters.
100104
*/
101105
Parameter getAParameter() { result = this.getParameter(_) }
102106

powershell/ql/lib/semmle/code/powershell/ast/internal/Synthesis.qll

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class Synthesis extends TSynthesis {
8484

8585
predicate functionName(FunctionBase f, string name) { none() }
8686

87+
predicate getAnAccess(VarAccessSynth va, Variable v) { none() }
88+
8789
predicate memberName(Member m, string name) { none() }
8890

8991
predicate typeName(Type t, string name) { none() }
@@ -116,13 +118,26 @@ Raw::Ast getRawAst(Ast r) { r = getResultAst(result) }
116118

117119
private module ThisSynthesis {
118120
private class ThisSynthesis extends Synthesis {
121+
private predicate thisAccess(Raw::Ast parent, ChildIndex i, Child child, Raw::Scope scope) {
122+
scope = parent.getScope() and
123+
parent.getChild(toRawChildIndex(i)).(Raw::VarAccess).getUserPath().toLowerCase() = "this" and
124+
child = SynthChild(VarAccessSynthKind(TVariableSynth(scope, ThisVar())))
125+
}
126+
119127
override predicate child(Raw::Ast parent, ChildIndex i, Child child) {
120128
parent instanceof Raw::MethodScriptBlock and
121129
i = ThisVar() and
122130
child = SynthChild(VarSynthKind(ThisVarKind()))
123131
or
124-
parent.getChild(toRawChildIndex(i)).(Raw::VarAccess).getUserPath().toLowerCase() = "this" and
125-
child = SynthChild(VarAccessSynthKind(TVariableSynth(parent.getScope(), ThisVar())))
132+
this.thisAccess(parent, i, child, _)
133+
}
134+
135+
final override predicate getAnAccess(VarAccessSynth va, Variable v) {
136+
exists(Raw::Ast parent, Raw::Scope scope, ChildIndex i |
137+
this.thisAccess(parent, i, _, scope) and
138+
v = TVariableSynth(scope, ThisVar()) and
139+
va = TVarAccessSynth(parent, i)
140+
)
126141
}
127142

128143
override predicate variableSynthName(VariableSynth v, string name) {
@@ -731,18 +746,26 @@ private module IteratorAccessSynth {
731746
)
732747
}
733748

749+
final override predicate getAnAccess(VarAccessSynth va, Variable v) {
750+
exists(Raw::Ast parent, ChildIndex i, Raw::VarAccess r |
751+
this.expr(parent, i, r, _) and
752+
va = TVarAccessSynth(parent, i) and
753+
v = this.varAccess(r)
754+
)
755+
}
756+
734757
override predicate exprStmtExpr(ExprStmt e, Expr expr) {
735758
exists(Raw::Ast p, Raw::VarAccess va, Raw::CmdExpr cmdExpr, ChildIndex i1, ChildIndex i2 |
736759
this.stmt(p, i1, _, _) and
737760
this.expr(cmdExpr, i2, va, _) and
738761
e = TExprStmtSynth(p, i1) and
739-
expr = TVarAccessSynth(cmdExpr, i2, this.varAccess(va))
762+
expr = TVarAccessSynth(cmdExpr, i2)
740763
)
741764
}
742765

743766
final override Expr getResultAstImpl(Raw::Ast r) {
744767
exists(Raw::Ast parent, ChildIndex i | this.expr(parent, i, r, _) |
745-
result = TVarAccessSynth(parent, i, this.varAccess(r))
768+
result = TVarAccessSynth(parent, i)
746769
)
747770
}
748771

powershell/ql/lib/semmle/code/powershell/ast/internal/TAst.qll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private predicate hasScopeAndName(VariableImpl variable, Scope::Range scope, str
6161
scope = variable.getDeclaringScopeImpl()
6262
}
6363

64-
private predicate access(Raw::VarAccess va, VariableImpl v) {
64+
predicate access(Raw::VarAccess va, VariableImpl v) {
6565
exists(string name, Scope::Range scope |
6666
pragma[only_bind_into](name) = variableNameInScope(va, scope)
6767
|
@@ -150,11 +150,11 @@ private module Cached {
150150
)
151151
} or
152152
TVariableSynth(Raw::Ast scope, ChildIndex i) { mkSynthChild(VarSynthKind(_), scope, i) } or
153-
TVarAccessReal(Raw::VarAccess va, Variable v) { access(va, v) } or
154-
TVarAccessSynth(Raw::Ast parent, ChildIndex i, Variable v) {
155-
mkSynthChild(VarAccessRealKind(v), parent, i)
153+
TVarAccessReal(Raw::VarAccess va) { access(va, _) } or
154+
TVarAccessSynth(Raw::Ast parent, ChildIndex i) {
155+
mkSynthChild(VarAccessRealKind(_), parent, i)
156156
or
157-
mkSynthChild(VarAccessSynthKind(v), parent, i)
157+
mkSynthChild(VarAccessSynthKind(_), parent, i)
158158
} or
159159
TWhileStmt(Raw::WhileStmt w) or
160160
TTypeNameExpr(Raw::TypeNameExpr t) or
@@ -277,7 +277,7 @@ private module Cached {
277277
n = TTypeConstraint(result) or
278278
n = TUnaryExpr(result) or
279279
n = TUsingStmt(result) or
280-
n = TVarAccessReal(result, _) or
280+
n = TVarAccessReal(result) or
281281
n = TWhileStmt(result) or
282282
n = TFunctionDefinitionStmt(result) or
283283
n = TExpandableSubExpr(result) or
@@ -308,7 +308,7 @@ private module Cached {
308308
result = TFunctionSynth(parent, i) or
309309
result = TBoolLiteral(parent, i) or
310310
result = TNullLiteral(parent, i) or
311-
result = TVarAccessSynth(parent, i, _) or
311+
result = TVarAccessSynth(parent, i) or
312312
result = TEnvVariable(parent, i) or
313313
result = TTypeSynth(parent, i) or
314314
result = TAutomaticVariable(parent, i) or

powershell/ql/lib/semmle/code/powershell/ast/internal/Variable.qll

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,23 @@ module Private {
9595

9696
class VarAccessReal extends VarAccessImpl, TVarAccessReal {
9797
Raw::VarAccess va;
98-
Variable v;
9998

100-
VarAccessReal() { this = TVarAccessReal(va, v) }
99+
VarAccessReal() { this = TVarAccessReal(va) }
101100

102-
final override Variable getVariableImpl() { result = v }
101+
final override Variable getVariableImpl() { access(va, result) }
103102

104-
final override string toString() { result = v.getName() }
103+
final override string toString() { result = va.getUserPath() }
105104
}
106105

107106
class VarAccessSynth extends VarAccessImpl, TVarAccessSynth {
108107
Raw::Ast parent;
109108
ChildIndex i;
110-
Variable v;
111109

112-
VarAccessSynth() { this = TVarAccessSynth(parent, i, v) }
110+
VarAccessSynth() { this = TVarAccessSynth(parent, i) }
113111

114-
final override Variable getVariableImpl() { result = v }
112+
final override Variable getVariableImpl() { any(Synthesis s).getAnAccess(this, result) }
115113

116-
final override string toString() { result = v.getName() }
114+
final override string toString() { result = this.getVariableImpl().getName() }
117115

118116
final override Location getLocation() { result = parent.getLocation() }
119117
}

0 commit comments

Comments
 (0)