|
| 1 | +/** |
| 2 | + * @name Year field changed using an arithmetic operation without checking for leap year (AntiPattern 1) |
| 3 | + * @description A field that represents a year is being modified by an arithmetic operation, but no proper check for leap years can be detected afterwards. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity error |
| 6 | + * @id cpp/leap-year/unchecked-after-arithmetic-year-modification |
| 7 | + * @precision medium |
| 8 | + * @tags leap-year |
| 9 | + * correctness |
| 10 | + * security |
| 11 | + */ |
| 12 | + |
| 13 | +import cpp |
| 14 | +import LeapYear |
| 15 | + |
| 16 | +/** |
| 17 | + * Holds if there is no known leap-year verification for the given `YearWriteOp`. |
| 18 | + * Binds the `var` argument to the qualifier of the `ywo` argument. |
| 19 | + */ |
| 20 | +bindingset[ywo] |
| 21 | +predicate isYearModifedWithoutExplicitLeapYearCheck(Variable var, YearWriteOp ywo) { |
| 22 | + exists(VariableAccess va, YearFieldAccess yfa | |
| 23 | + yfa = ywo.getYearAccess() and |
| 24 | + yfa.getQualifier() = va and |
| 25 | + var.getAnAccess() = va and |
| 26 | + // Avoid false positives |
| 27 | + not ( |
| 28 | + // If there is a local check for leap year after the modification |
| 29 | + exists(LeapYearFieldAccess yfacheck | |
| 30 | + yfacheck.getQualifier() = var.getAnAccess() and |
| 31 | + yfacheck.isUsedInCorrectLeapYearCheck() and |
| 32 | + yfacheck.getBasicBlock() = yfa.getBasicBlock().getASuccessor*() |
| 33 | + ) |
| 34 | + or |
| 35 | + // If there is a data flow from the variable that was modified to a function that seems to check for leap year |
| 36 | + exists(VariableAccess source, ChecksForLeapYearFunctionCall fc | |
| 37 | + source = var.getAnAccess() and |
| 38 | + LeapYearCheckFlow::flow(DataFlow::exprNode(source), DataFlow::exprNode(fc.getAnArgument())) |
| 39 | + ) |
| 40 | + or |
| 41 | + // If there is a data flow from the field that was modified to a function that seems to check for leap year |
| 42 | + exists(VariableAccess vacheck, YearFieldAccess yfacheck, ChecksForLeapYearFunctionCall fc | |
| 43 | + vacheck = var.getAnAccess() and |
| 44 | + yfacheck.getQualifier() = vacheck and |
| 45 | + LeapYearCheckFlow::flow(DataFlow::exprNode(yfacheck), DataFlow::exprNode(fc.getAnArgument())) |
| 46 | + ) |
| 47 | + or |
| 48 | + // If there is a successor or predecessor that sets the month or day to a fixed value |
| 49 | + exists(FieldAccess mfa, AssignExpr ae, int val | |
| 50 | + mfa instanceof MonthFieldAccess or mfa instanceof DayFieldAccess |
| 51 | + | |
| 52 | + mfa.getQualifier() = var.getAnAccess() and |
| 53 | + mfa.isModified() and |
| 54 | + ( |
| 55 | + mfa.getBasicBlock() = yfa.getBasicBlock().getASuccessor*() or |
| 56 | + yfa.getBasicBlock() = mfa.getBasicBlock().getASuccessor+() |
| 57 | + ) and |
| 58 | + ae = mfa.getEnclosingElement() and |
| 59 | + ae.getAnOperand().getValue().toInt() = val |
| 60 | + ) |
| 61 | + ) |
| 62 | + ) |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * The set of all write operations to the Year field of a date struct. |
| 67 | + */ |
| 68 | +abstract class YearWriteOp extends Operation { |
| 69 | + /** Extracts the access to the Year field */ |
| 70 | + abstract YearFieldAccess getYearAccess(); |
| 71 | + |
| 72 | + /** Get the expression which represents the new value. */ |
| 73 | + abstract Expr getMutationExpr(); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * A unary operation (Crement) performed on a Year field. |
| 78 | + */ |
| 79 | +class YearWriteOpUnary extends YearWriteOp, UnaryOperation { |
| 80 | + YearWriteOpUnary() { this.getOperand() instanceof YearFieldAccess } |
| 81 | + |
| 82 | + override YearFieldAccess getYearAccess() { result = this.getOperand() } |
| 83 | + |
| 84 | + override Expr getMutationExpr() { result = this } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * An assignment operation or mutation on the Year field of a date object. |
| 89 | + */ |
| 90 | +class YearWriteOpAssignment extends YearWriteOp, Assignment { |
| 91 | + YearWriteOpAssignment() { this.getLValue() instanceof YearFieldAccess } |
| 92 | + |
| 93 | + override YearFieldAccess getYearAccess() { result = this.getLValue() } |
| 94 | + |
| 95 | + override Expr getMutationExpr() { |
| 96 | + // Note: may need to use DF analysis to pull out the original value, |
| 97 | + // if there is excessive false positives. |
| 98 | + if this.getOperator() = "=" |
| 99 | + then |
| 100 | + exists(DataFlow::Node source, DataFlow::Node sink | |
| 101 | + sink.asExpr() = this.getRValue() and |
| 102 | + OperationToYearAssignmentFlow::flow(source, sink) and |
| 103 | + result = source.asExpr() |
| 104 | + ) |
| 105 | + else result = this |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * A DataFlow configuration for identifying flows from some non trivial access or literal |
| 111 | + * to the Year field of a date object. |
| 112 | + */ |
| 113 | +module OperationToYearAssignmentConfig implements DataFlow::ConfigSig { |
| 114 | + predicate isSource(DataFlow::Node n) { |
| 115 | + not n.asExpr() instanceof Access and |
| 116 | + not n.asExpr() instanceof Literal |
| 117 | + } |
| 118 | + |
| 119 | + predicate isSink(DataFlow::Node n) { |
| 120 | + exists(Assignment a | |
| 121 | + a.getLValue() instanceof YearFieldAccess and |
| 122 | + a.getRValue() = n.asExpr() |
| 123 | + ) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +module OperationToYearAssignmentFlow = DataFlow::Global<OperationToYearAssignmentConfig>; |
| 128 | + |
| 129 | +from Variable var, YearWriteOp ywo, Expr mutationExpr |
| 130 | +where |
| 131 | + mutationExpr = ywo.getMutationExpr() and |
| 132 | + isYearModifedWithoutExplicitLeapYearCheck(var, ywo) and |
| 133 | + not isNormalizationOperation(mutationExpr) and |
| 134 | + not ywo instanceof AddressOfExpr and |
| 135 | + not exists(Call c, TimeConversionFunction f | f.getACallToThisFunction() = c | |
| 136 | + c.getAnArgument().getAChild*() = var.getAnAccess() and |
| 137 | + ywo.getASuccessor*() = c |
| 138 | + ) |
| 139 | +select ywo, |
| 140 | + "$@: Field $@ on variable $@ has been modified, but no appropriate check for LeapYear was found.", |
| 141 | + ywo.getEnclosingFunction(), ywo.getEnclosingFunction().toString(), |
| 142 | + ywo.getYearAccess().getTarget(), ywo.getYearAccess().getTarget().toString(), var, var.toString() |
0 commit comments