|
| 1 | +/** |
| 2 | + * Provides models for `Numeric` and related Swift classes (such as `Int` and `Float`). |
| 3 | + */ |
| 4 | + |
| 5 | +import swift |
| 6 | +private import codeql.swift.dataflow.DataFlow |
| 7 | +private import codeql.swift.dataflow.ExternalFlow |
| 8 | +private import codeql.swift.dataflow.FlowSteps |
| 9 | + |
| 10 | +/** |
| 11 | + * A model for `Numeric` and related class members and functions that permit taint flow. |
| 12 | + */ |
| 13 | +private class NumericSummaries extends SummaryModelCsv { |
| 14 | + override predicate row(string row) { |
| 15 | + row = |
| 16 | + [ |
| 17 | + ";;false;numericCast(_:);;;Argument[0];ReturnValue;taint", |
| 18 | + ";;false;unsafeDowncast(_:to:);;;Argument[0];ReturnValue;taint", |
| 19 | + ";;false;unsafeBitCast(_:to:);;;Argument[0];ReturnValue;taint", |
| 20 | + ";Numeric;true;init(exactly:);;;Argument[0];ReturnValue.OptionalSome;value", |
| 21 | + ";Numeric;true;init(bitPattern:);;;Argument[0];ReturnValue;taint", |
| 22 | + ";BinaryInteger;true;init(_:);;;Argument[0];ReturnValue;taint", |
| 23 | + ";BinaryInteger;true;init(clamping:);;;Argument[0];ReturnValue;taint", |
| 24 | + ";BinaryInteger;true;init(truncatingIfNeeded:);;;Argument[0];ReturnValue;taint", |
| 25 | + ";BinaryInteger;true;init(_:format:lenient:);;;Argument[0];ReturnValue;taint", |
| 26 | + ";BinaryInteger;true;init(_:strategy:);;;Argument[0];ReturnValue;taint", |
| 27 | + ";BinaryInteger;true;formatted();;;Argument[-1];ReturnValue;taint", |
| 28 | + ";BinaryInteger;true;formatted(_:);;;Argument[-1];ReturnValue;taint", |
| 29 | + ";FixedWidthInteger;true;init(_:radix:);;;Argument[0];ReturnValue;taint", |
| 30 | + ";FixedWidthInteger;true;init(littleEndian:);;;Argument[0];ReturnValue;taint", |
| 31 | + ";FixedWidthInteger;true;init(bigEndian:);;;Argument[0];ReturnValue;taint", |
| 32 | + ";FloatingPoint;true;init(_:);;;Argument[0];ReturnValue;taint", |
| 33 | + ";FloatingPoint;true;init(sign:exponent:significand:);;;Argument[1..2];ReturnValue;taint", |
| 34 | + ";FloatingPoint;true;init(signOf:magnitudeOf:);;;Argument[1];ReturnValue;taint", |
| 35 | + ] |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * A content implying that, if a `Numeric` is tainted, then some of its fields are |
| 41 | + * tainted. |
| 42 | + */ |
| 43 | +private class NumericFieldsInheritTaint extends TaintInheritingContent, |
| 44 | + DataFlow::Content::FieldContent |
| 45 | +{ |
| 46 | + NumericFieldsInheritTaint() { |
| 47 | + this.getField().hasQualifiedName("FixedWidthInteger", ["littleEndian", "bigEndian"]) |
| 48 | + or |
| 49 | + this.getField() |
| 50 | + .hasQualifiedName(["Double", "Float", "Float80", "FloatingPoint"], |
| 51 | + ["exponent", "significand"]) |
| 52 | + } |
| 53 | +} |
0 commit comments