Skip to content

Commit

Permalink
[dataflowengineoss] remove overriding operator semantics (#4952)
Browse files Browse the repository at this point in the history
* fix assignmentModulo

* fix assignmentXor

* fix assignmentOr

* fix assignmentAnd

* fix assignmentExponentiation

* fix assignmentShiftLeft

* fix assignment{Logical,Arithmetic}ShiftRight

* scalafmt
  • Loading branch information
xavierpinho authored Sep 25, 2024
1 parent 10e1c33 commit cc7f787
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@ object DefaultSemantics {
F(Operators.preIncrement, List((1, 1), (1, -1))),
F(Operators.sizeOf, List.empty[(Int, Int)]),

// some of those operators have duplicate mappings due to a typo
// - see https://github.com/ShiftLeftSecurity/codepropertygraph/pull/1630

F("<operators>.assignmentExponentiation", List((2, 1), (1, 1))),
F("<operators>.assignmentModulo", List((2, 1), (1, 1))),
F("<operators>.assignmentShiftLeft", List((2, 1), (1, 1))),
F("<operators>.assignmentLogicalShiftRight", List((2, 1), (1, 1))),
F("<operators>.assignmentArithmeticShiftRight", List((2, 1), (1, 1))),
F("<operators>.assignmentAnd", List((2, 1), (1, 1))),
F("<operators>.assignmentOr", List((2, 1), (1, 1))),
F("<operators>.assignmentXor", List((2, 1), (1, 1))),

// Language specific operators
PTF("<operator>.tupleLiteral"),
PTF("<operator>.dictLiteral"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1988,4 +1988,137 @@ class DataFlowTestsWithCallDepth extends DataFlowCodeToCpgSuite {
)
}
}

"DataFlowTest73" should {
val cpg = code("""
|int main(void) {
| int x = 5;
| call1(x%=2);
| call2(x);
|}
|""".stripMargin)

"the literal in x%=2 should taint the outer expression" in {
val source = cpg.literal("2")
val sink = cpg.call("call1")
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(List(("x%=2", 4), ("call1(x%=2)", 4)))
}

"the literal in x%=2 should taint the next occurrence of x" in {
val source = cpg.literal("2")
val sink = cpg.call("call2")
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(List(("x%=2", 4), ("call2(x)", 5)))
}

}

"DataFlowTest74" should {
val cpg = code("""
|int main(void) {
| int x = 5;
| call1(x^=2);
| call2(x);
|}
|""".stripMargin)

"the literal in x^=2 should taint the outer expression" in {
val source = cpg.literal("2")
val sink = cpg.call("call1")
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(List(("x^=2", 4), ("call1(x^=2)", 4)))
}

"the literal in x^=2 should taint the next occurrence of x" in {
val source = cpg.literal("2")
val sink = cpg.call("call2")
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(List(("x^=2", 4), ("call2(x)", 5)))
}
}

"DataFlowTest75" should {
val cpg = code("""
|int main(void) {
| int x = 5;
| call1(x|=2);
| call2(x);
|}
|""".stripMargin)

"the literal in x|=2 should taint the outer expression" in {
val source = cpg.literal("2")
val sink = cpg.call("call1")
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(List(("x|=2", 4), ("call1(x|=2)", 4)))
}

"the literal in x|=2 should taint the next occurrence of x" in {
val source = cpg.literal("2")
val sink = cpg.call("call2")
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(List(("x|=2", 4), ("call2(x)", 5)))
}
}

"DataFlowTest76" should {
val cpg = code("""
|int main(void) {
| int x = 5;
| call1(x&=2);
| call2(x);
|}
|""".stripMargin)

"the literal in x&=2 should taint the outer expression" in {
val source = cpg.literal("2")
val sink = cpg.call("call1")
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(List(("x&=2", 4), ("call1(x&=2)", 4)))
}

"the literal in x&=2 should taint the next occurrence of x" in {
val source = cpg.literal("2")
val sink = cpg.call("call2")
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(List(("x&=2", 4), ("call2(x)", 5)))
}
}

"DataFlowTest77" should {
val cpg = code("""
|int main(void) {
| int x = 5;
| call1(x<<=2);
| call2(x);
|}
|""".stripMargin)

"the literal in x<<=2 should taint the outer expression" in {
val source = cpg.literal("2")
val sink = cpg.call("call1")
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(List(("x<<=2", 4), ("call1(x<<=2)", 4)))
}

"the literal in x<<=2 should taint the next occurrence of x" in {
val source = cpg.literal("2")
val sink = cpg.call("call2")
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(List(("x<<=2", 4), ("call2(x)", 5)))
}
}

"DataFlowTest78" should {
val cpg = code("""
|int main(void) {
| int x = 5;
| call1(x>>=2);
| call2(x);
|}
|""".stripMargin)

"the literal in x>>=2 should taint the outer expression" in {
val source = cpg.literal("2")
val sink = cpg.call("call1")
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(List(("x>>=2", 4), ("call1(x>>=2)", 4)))
}

"the literal in x>>=2 should taint the next occurrence of x" in {
val source = cpg.literal("2")
val sink = cpg.call("call2")
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(List(("x>>=2", 4), ("call2(x)", 5)))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ class SingleAssignmentTests extends RubyCode2CpgFixture(withPostProcessing = tru
sink.reachableByFlows(src).l.size shouldBe 2
}

"flow through **=" in {
val cpg = code("""
|x = 5
|call1(x**=2)
|call2(x)
|""".stripMargin)

val source = cpg.literal("2").l
val call1 = cpg.call("call1")
val call2 = cpg.call("call2")

call1.reachableBy(source).l shouldBe source
call2.reachableBy(source).l shouldBe source
}

"Data flow through grouping expression" in {
val cpg = code("""
|x = 0
Expand Down

0 comments on commit cc7f787

Please sign in to comment.