Repository: llvm/llvm-project
File: clang/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp (C++)

Code before:

  GenericTaintRuleParser::RulesContTy Rules{
      ConfigParser.parseConfiguration(Option, std::move(*Config))};

  DynamicTaintRules.emplace(std::make_move_iterator(Rules.begin()),
                            std::make_move_iterator(Rules.end()));
}

// The incoming parameters of the main function get tainted
// if the program called in an untrusted environment.
void GenericTaintChecker::checkBeginFunction(CheckerContext &C) const {
  if (!C.inTopFrame() || C.getAnalysisManager()
                             .getAnalyzerOptions()
                             .ShouldAssumeControlledEnvironment)
    return;

  const auto *FD = dyn_cast<FunctionDecl>(C.getLocationContext()->getDecl());
  if (!FD || !FD->isMain() || FD->param_size() < 2)
    return;

  ProgramStateRef State = C.getState();
  const MemRegion *ArgvReg =
      State->getRegion(FD->parameters()[1], C.getLocationContext());
  SVal ArgvSval = State->getSVal(ArgvReg);
  // Add taintedness to argv**
  State = addTaint(State, ArgvSval);

  const NoteTag *OriginatingTag =
      C.getNoteTag([ArgvSval](PathSensitiveBugReport &BR) -> std::string {
        // We give diagnostics only for taint related reports
        if (!BR.isInteresting(ArgvSval) ||
            BR.getBugType().getCategory() != categories::TaintedData)
          return "";

        return MsgTaintOrigin;
      });
  C.addTransition(State, OriginatingTag);
}

void GenericTaintChecker::checkPreCall(const CallEvent &Call,
                                       CheckerContext &C) const {

  initTaintRules(C);

  // FIXME: this should be much simpler.
  if (const auto *Rule =
          Call.isGlobalCFunction() ? StaticTaintRules->lookup(Call) : nullptr)
    Rule->process(*this, Call, C);
  else if (const auto *Rule = DynamicTaintRules->lookup(Call))
    Rule->process(*this, Call, C);


Review comment (suggestion):
Please add testcases where:
- The `main` function is defined with no argument.
- A function named `main` is defined in a C++ namespace or as a method of a C++ class.
I expect that these would pass, but it would be good practice to demonstrate this. 

Code after:

  GenericTaintRuleParser::RulesContTy Rules{
      ConfigParser.parseConfiguration(Option, std::move(*Config))};

  DynamicTaintRules.emplace(std::make_move_iterator(Rules.begin()),
                            std::make_move_iterator(Rules.end()));
}

bool isPointerToCharArray(const QualType &QT) {
  if (!QT->isPointerType())
    return false;
  QualType PointeeType = QT->getPointeeType();
  return PointeeType->isPointerType() &&
         PointeeType->getPointeeType()->isCharType();
}

// The incoming parameters of the main function get tainted
// if the program called in an untrusted environment.
void GenericTaintChecker::checkBeginFunction(CheckerContext &C) const {
  if (!C.inTopFrame() || C.getAnalysisManager()
                             .getAnalyzerOptions()
                             .ShouldAssumeControlledEnvironment)
    return;

  const auto *FD = dyn_cast<FunctionDecl>(C.getLocationContext()->getDecl());
  if (!FD || !FD->isMain() || FD->param_size() < 2)
    return;

  if (!FD->parameters()[0]->getType()->isIntegerType())
    return;

  if (!isPointerToCharArray(FD->parameters()[1]->getType()))
    return;
  ProgramStateRef State = C.getState();

  const MemRegion *ArgcReg =
      State->getRegion(FD->parameters()[0], C.getLocationContext());
  SVal ArgcSVal = State->getSVal(ArgcReg);
  State = addTaint(State, ArgcSVal);
  StringRef ArgcName = FD->parameters()[0]->getName();
  if (auto N = ArgcSVal.getAs<NonLoc>()) {
    ConstraintManager &CM = C.getConstraintManager();
    // The upper bound is the ARG_MAX on an arbitrary Linux
    // to model that is is typically smaller than INT_MAX.
    State = CM.assumeInclusiveRange(State, *N, llvm::APSInt::getUnsigned(1),
                                    llvm::APSInt::getUnsigned(2097152), true);
  }

  const MemRegion *ArgvReg =
      State->getRegion(FD->parameters()[1], C.getLocationContext());
  SVal ArgvSVal = State->getSVal(ArgvReg);