Skip to content

Commit d2ee9ef

Browse files
committed
C#: Upgrade libraries and queries to use the new Operation classes.
1 parent 97a487e commit d2ee9ef

File tree

27 files changed

+138
-108
lines changed

27 files changed

+138
-108
lines changed

csharp/ql/campaigns/Solorigate/src/ModifiedFnvFunctionDetection.ql

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,9 @@ import experimental.code.csharp.Cryptography.NonCryptographicHashes
1616
from Variable v, Literal l, LoopStmt loop, Expr additional_xor
1717
where
1818
maybeUsedInFnvFunction(v, _, _, loop) and
19-
(
20-
exists(BitwiseXorExpr xor2 | xor2.getAnOperand() = l and additional_xor = xor2 |
21-
loop.getAControlFlowExitNode().getASuccessor*() = xor2.getAControlFlowNode() and
22-
xor2.getAnOperand() = v.getAnAccess()
23-
)
24-
or
25-
exists(AssignXorExpr xor2 | xor2.getAnOperand() = l and additional_xor = xor2 |
26-
loop.getAControlFlowExitNode().getASuccessor*() = xor2.getAControlFlowNode() and
27-
xor2.getAnOperand() = v.getAnAccess()
28-
)
19+
exists(BitwiseXorOperation xor2 | xor2.getAnOperand() = l and additional_xor = xor2 |
20+
loop.getAControlFlowExitNode().getASuccessor*() = xor2.getAControlFlowNode() and
21+
xor2.getAnOperand() = v.getAnAccess()
2922
)
3023
select l, "This literal is used in an $@ after an FNV-like hash calculation with variable $@.",
3124
additional_xor, "additional xor", v, v.toString()

csharp/ql/lib/experimental/code/csharp/Cryptography/NonCryptographicHashes.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private predicate maybeUsedInElfHashFunction(Variable v, Operation xor, Operatio
4848
Expr e1, Expr e2, AssignExpr addAssign, AssignExpr xorAssign, Operation notOp,
4949
AssignExpr notAssign
5050
|
51-
(add instanceof AddExpr or add instanceof AssignAddExpr) and
51+
add instanceof AddOperation and
5252
e1.getAChild*() = add.getAnOperand() and
5353
e1 instanceof BinaryBitwiseOperation and
5454
e2 = e1.(BinaryBitwiseOperation).getLeftOperand() and

csharp/ql/lib/semmle/code/csharp/commons/Strings.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class ImplicitToStringExpr extends Expr {
4949
this = add.getOtherOperand(o).stripImplicit()
5050
)
5151
or
52+
exists(AssignAddExpr add, Expr o | o = add.getLeftOperand() |
53+
o.stripImplicit().getType() instanceof StringType and
54+
this = add.getRightOperand().stripImplicit()
55+
)
56+
or
5257
this = any(InterpolatedStringExpr ise).getAnInsert().stripImplicit()
5358
}
5459
}

csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@ private module GuardsInput implements
119119
class AndExpr extends BinExpr {
120120
AndExpr() {
121121
this instanceof LogicalAndExpr or
122-
this instanceof BitwiseAndExpr
122+
this instanceof BitwiseAndOperation
123123
}
124124
}
125125

126126
class OrExpr extends BinExpr {
127127
OrExpr() {
128128
this instanceof LogicalOrExpr or
129-
this instanceof BitwiseOrExpr
129+
this instanceof BitwiseOrOperation
130130
}
131131
}
132132

@@ -292,7 +292,7 @@ private module LogicInput implements GuardsImpl::LogicInputSig {
292292
v1.isNonNullValue() and
293293
v2 = v1
294294
or
295-
g2 = g1.(NullCoalescingExpr).getAnOperand() and
295+
g2 = g1.(NullCoalescingOperation).getAnOperand() and
296296
v1.isNullValue() and
297297
v2 = v1
298298
or
@@ -840,14 +840,14 @@ module Internal {
840840
or
841841
e1 = e2.(Cast).getExpr()
842842
or
843-
e2 = e1.(NullCoalescingExpr).getAnOperand()
843+
e2 = e1.(NullCoalescingOperation).getAnOperand()
844844
}
845845

846846
/** Holds if expression `e3` is a `null` value whenever `e1` and `e2` are. */
847847
predicate nullValueImpliedBinary(Expr e1, Expr e2, Expr e3) {
848848
e3 = any(ConditionalExpr ce | e1 = ce.getThen() and e2 = ce.getElse())
849849
or
850-
e3 = any(NullCoalescingExpr nce | e1 = nce.getLeftOperand() and e2 = nce.getRightOperand())
850+
e3 = any(NullCoalescingOperation no | e1 = no.getLeftOperand() and e2 = no.getRightOperand())
851851
}
852852

853853
predicate nullValueImplied(Expr e) {
@@ -907,7 +907,7 @@ module Internal {
907907
or
908908
// "In string concatenation operations, the C# compiler treats a null string the same as an empty string."
909909
// (https://docs.microsoft.com/en-us/dotnet/csharp/how-to/concatenate-multiple-strings)
910-
e instanceof AddExpr and
910+
e instanceof AddOperation and
911911
e.getType() instanceof StringType
912912
or
913913
e.(DefaultValueExpr).getType().isValueType()
@@ -922,11 +922,9 @@ module Internal {
922922

923923
/** Holds if expression `e2` is a non-`null` value whenever `e1` is. */
924924
predicate nonNullValueImpliedUnary(Expr e1, Expr e2) {
925-
e1 = e2.(CastExpr).getExpr()
926-
or
927-
e1 = e2.(AssignExpr).getRValue()
928-
or
929-
e1 = e2.(NullCoalescingExpr).getAnOperand()
925+
e1 = e2.(CastExpr).getExpr() or
926+
e1 = e2.(AssignExpr).getRValue() or
927+
e1 = e2.(NullCoalescingOperation).getAnOperand()
930928
}
931929

932930
/**
@@ -953,10 +951,13 @@ module Internal {
953951
)
954952
or
955953
// In C#, `null + 1` has type `int?` with value `null`
956-
exists(BinaryArithmeticOperation bao, Expr o |
957-
result = bao and
958-
bao.getAnOperand() = e and
959-
bao.getAnOperand() = o and
954+
exists(BinaryOperation bo, Expr o |
955+
bo instanceof BinaryArithmeticOperation or
956+
bo instanceof AssignArithmeticOperation
957+
|
958+
result = bo and
959+
bo.getAnOperand() = e and
960+
bo.getAnOperand() = o and
960961
// The other operand must be provably non-null in order
961962
// for `only if` to hold
962963
nonNullValueImplied(o) and
@@ -972,10 +973,10 @@ module Internal {
972973
any(QualifiableExpr qe |
973974
qe.isConditional() and
974975
result = qe.getQualifier()
975-
)
976-
or
976+
) or
977977
// In C#, `null + 1` has type `int?` with value `null`
978-
e = any(BinaryArithmeticOperation bao | result = bao.getAnOperand())
978+
e = any(BinaryArithmeticOperation bao | result = bao.getAnOperand()) or
979+
e = any(AssignArithmeticOperation aao | result = aao.getAnOperand())
979980
}
980981

981982
deprecated predicate isGuard(Expr e, GuardValue val) {

csharp/ql/lib/semmle/code/csharp/controlflow/internal/Completion.qll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ private class TriedControlFlowElement extends ControlFlowElement {
344344
result instanceof SystemOutOfMemoryExceptionClass
345345
or
346346
this =
347-
any(AddExpr ae |
347+
any(AddOperation ae |
348348
ae.getType() instanceof StringType and
349349
result instanceof SystemOutOfMemoryExceptionClass
350350
or
@@ -353,24 +353,24 @@ private class TriedControlFlowElement extends ControlFlowElement {
353353
)
354354
or
355355
this =
356-
any(SubExpr se |
356+
any(SubOperation se |
357357
se.getType() instanceof IntegralType and
358358
result instanceof SystemOverflowExceptionClass
359359
)
360360
or
361361
this =
362-
any(MulExpr me |
362+
any(MulOperation me |
363363
me.getType() instanceof IntegralType and
364364
result instanceof SystemOverflowExceptionClass
365365
)
366366
or
367367
this =
368-
any(DivExpr de |
368+
any(DivOperation de |
369369
not de.getDenominator().getValue().toFloat() != 0 and
370370
result instanceof SystemDivideByZeroExceptionClass
371371
)
372372
or
373-
this instanceof RemExpr and
373+
this instanceof RemOperation and
374374
result instanceof SystemDivideByZeroExceptionClass
375375
or
376376
this instanceof DynamicExpr and
@@ -447,7 +447,7 @@ private predicate inBooleanContext(Expr e) {
447447
e in [ce.getThen(), ce.getElse()]
448448
)
449449
or
450-
e = any(NullCoalescingExpr nce | inBooleanContext(nce)).getAnOperand()
450+
e = any(NullCoalescingOperation nce | inBooleanContext(nce)).getAnOperand()
451451
or
452452
e = any(SwitchExpr se | inBooleanContext(se)).getACase()
453453
or
@@ -467,13 +467,13 @@ private predicate mustHaveNullnessCompletion(Expr e) {
467467
* that `e` evaluates to determines a `null`/non-`null` branch successor.
468468
*/
469469
private predicate inNullnessContext(Expr e) {
470-
e = any(NullCoalescingExpr nce).getLeftOperand()
470+
e = any(NullCoalescingOperation nce).getLeftOperand()
471471
or
472472
exists(QualifiableExpr qe | qe.isConditional() | e = qe.getChildExpr(-1))
473473
or
474474
exists(ConditionalExpr ce | inNullnessContext(ce) | (e = ce.getThen() or e = ce.getElse()))
475475
or
476-
exists(NullCoalescingExpr nce | inNullnessContext(nce) | e = nce.getRightOperand())
476+
exists(NullCoalescingOperation nce | inNullnessContext(nce) | e = nce.getRightOperand())
477477
or
478478
e = any(SwitchExpr se | inNullnessContext(se)).getACase()
479479
or

csharp/ql/lib/semmle/code/csharp/controlflow/internal/Splitting.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ module ConditionalCompletionSplitting {
9595
child = parent.(SwitchCaseExpr).getBody()
9696
or
9797
parent =
98-
any(NullCoalescingExpr nce |
98+
any(NullCoalescingOperation nce |
9999
if childCompletion instanceof NullnessCompletion
100100
then child = nce.getRightOperand()
101101
else child = nce.getAnOperand()

csharp/ql/lib/semmle/code/csharp/dataflow/Nullness.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private Expr maybeNullExpr(Expr reason) {
4242
ce.getElse() = maybeNullExpr(reason)
4343
)
4444
or
45-
result.(NullCoalescingExpr).getRightOperand() = maybeNullExpr(reason)
45+
result.(NullCoalescingOperation).getRightOperand() = maybeNullExpr(reason)
4646
or
4747
result =
4848
any(QualifiableExpr qe |

csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ module LocalFlow {
512512
predicate localExprStep(Expr e1, Expr e2) {
513513
e1 = e2.(ParenthesizedExpr).getExpr()
514514
or
515-
e1 = e2.(NullCoalescingExpr).getAnOperand()
515+
e1 = e2.(NullCoalescingOperation).getAnOperand()
516516
or
517517
e1 = e2.(SuppressNullableWarningExpr).getExpr()
518518
or
@@ -623,7 +623,7 @@ module LocalFlow {
623623
(
624624
e instanceof ConditionalExpr or
625625
e instanceof Cast or
626-
e instanceof NullCoalescingExpr or
626+
e instanceof NullCoalescingOperation or
627627
e instanceof SwitchExpr or
628628
e instanceof SuppressNullableWarningExpr or
629629
e instanceof AssignExpr

csharp/ql/lib/semmle/code/csharp/dataflow/internal/TaintTrackingPrivate.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ predicate defaultImplicitTaintRead(DataFlow::Node node, DataFlow::ContentSet c)
4747
private predicate localTaintExprStep(Expr e1, Expr e2) {
4848
e1 = e2.(ElementAccess).getQualifier()
4949
or
50-
e1 = e2.(AddExpr).getAnOperand()
50+
e1 = e2.(AddOperation).getAnOperand()
5151
or
5252
// A comparison expression where taint can flow from one of the
5353
// operands if the other operand is a constant value.

csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/ModulusAnalysisSpecific.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ module Private {
2020

2121
class ConditionalExpr = RU::ExprNode::ConditionalExpr;
2222

23-
class AddExpr = RU::ExprNode::AddExpr;
23+
class AddExpr = RU::ExprNode::AddOperation;
2424

25-
class SubExpr = RU::ExprNode::SubExpr;
25+
class SubExpr = RU::ExprNode::SubOperation;
2626

27-
class RemExpr = RU::ExprNode::RemExpr;
27+
class RemExpr = RU::ExprNode::RemOperation;
2828

29-
class BitwiseAndExpr = RU::ExprNode::BitwiseAndExpr;
29+
class BitwiseAndExpr = RU::ExprNode::BitwiseAndOperation;
3030

31-
class MulExpr = RU::ExprNode::MulExpr;
31+
class MulExpr = RU::ExprNode::MulOperation;
3232

33-
class LeftShiftExpr = RU::ExprNode::LeftShiftExpr;
33+
class LeftShiftExpr = RU::ExprNode::LeftShiftOperation;
3434

3535
predicate guardControlsSsaRead = RU::guardControlsSsaRead/3;
3636

0 commit comments

Comments
 (0)