1d99bd55aSTed Kremenek //==- CheckSecuritySyntaxOnly.cpp - Basic security checks --------*- C++ -*-==// 2d99bd55aSTed Kremenek // 3d99bd55aSTed Kremenek // The LLVM Compiler Infrastructure 4d99bd55aSTed Kremenek // 5d99bd55aSTed Kremenek // This file is distributed under the University of Illinois Open Source 6d99bd55aSTed Kremenek // License. See LICENSE.TXT for details. 7d99bd55aSTed Kremenek // 8d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 9d99bd55aSTed Kremenek // 10d99bd55aSTed Kremenek // This file defines a set of flow-insensitive security checks. 11d99bd55aSTed Kremenek // 12d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 13d99bd55aSTed Kremenek 14af45aca6SArgyrios Kyrtzidis #include "ClangSACheckers.h" 15c29bed39SAnna Zaks #include "clang/Analysis/AnalysisContext.h" 16c29bed39SAnna Zaks #include "clang/AST/StmtVisitor.h" 17c29bed39SAnna Zaks #include "clang/Basic/TargetInfo.h" 186a5674ffSArgyrios Kyrtzidis #include "clang/StaticAnalyzer/Core/Checker.h" 19f8cbac4bSTed Kremenek #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" 20c29bed39SAnna Zaks #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 21fca2e961SLenny Maiorani #include "llvm/ADT/StringSwitch.h" 22c29bed39SAnna Zaks #include "llvm/Support/raw_ostream.h" 23d99bd55aSTed Kremenek 24d99bd55aSTed Kremenek using namespace clang; 25d99bd55aSTed Kremenek using namespace ento; 26d99bd55aSTed Kremenek 27d99bd55aSTed Kremenek static bool isArc4RandomAvailable(const ASTContext &Ctx) { 28e8bbc121SDouglas Gregor const llvm::Triple &T = Ctx.getTargetInfo().getTriple(); 29d99bd55aSTed Kremenek return T.getVendor() == llvm::Triple::Apple || 3045e84b00SDouglas Gregor T.getOS() == llvm::Triple::FreeBSD || 3145e84b00SDouglas Gregor T.getOS() == llvm::Triple::NetBSD || 3245e84b00SDouglas Gregor T.getOS() == llvm::Triple::OpenBSD || 3345e84b00SDouglas Gregor T.getOS() == llvm::Triple::DragonFly; 34d99bd55aSTed Kremenek } 35d99bd55aSTed Kremenek 36d99bd55aSTed Kremenek namespace { 37d99bd55aSTed Kremenek class WalkAST : public StmtVisitor<WalkAST> { 38d99bd55aSTed Kremenek BugReporter &BR; 39c29bed39SAnna Zaks AnalysisContext* AC; 40d99bd55aSTed Kremenek enum { num_setids = 6 }; 41d99bd55aSTed Kremenek IdentifierInfo *II_setid[num_setids]; 42d99bd55aSTed Kremenek 43d99bd55aSTed Kremenek const bool CheckRand; 44d99bd55aSTed Kremenek 45d99bd55aSTed Kremenek public: 46c29bed39SAnna Zaks WalkAST(BugReporter &br, AnalysisContext* ac) 47c29bed39SAnna Zaks : BR(br), AC(ac), II_setid(), 48d99bd55aSTed Kremenek CheckRand(isArc4RandomAvailable(BR.getContext())) {} 49d99bd55aSTed Kremenek 50d99bd55aSTed Kremenek // Statement visitor methods. 51d99bd55aSTed Kremenek void VisitCallExpr(CallExpr *CE); 52d99bd55aSTed Kremenek void VisitForStmt(ForStmt *S); 53d99bd55aSTed Kremenek void VisitCompoundStmt (CompoundStmt *S); 54d99bd55aSTed Kremenek void VisitStmt(Stmt *S) { VisitChildren(S); } 55d99bd55aSTed Kremenek 56d99bd55aSTed Kremenek void VisitChildren(Stmt *S); 57d99bd55aSTed Kremenek 58d99bd55aSTed Kremenek // Helpers. 59de909e49SLenny Maiorani bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD); 60d99bd55aSTed Kremenek 61fca2e961SLenny Maiorani typedef void (WalkAST::*FnCheck)(const CallExpr *, 62fca2e961SLenny Maiorani const FunctionDecl *); 63fca2e961SLenny Maiorani 64d99bd55aSTed Kremenek // Checker-specific methods. 65de909e49SLenny Maiorani void checkLoopConditionForFloat(const ForStmt *FS); 66de909e49SLenny Maiorani void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD); 67de909e49SLenny Maiorani void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD); 68de909e49SLenny Maiorani void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD); 69de909e49SLenny Maiorani void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD); 70de909e49SLenny Maiorani void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD); 71de909e49SLenny Maiorani void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD); 72de909e49SLenny Maiorani void checkCall_random(const CallExpr *CE, const FunctionDecl *FD); 73*fedf5dfcSAnna Zaks void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD); 74de909e49SLenny Maiorani void checkUncheckedReturnValue(CallExpr *CE); 75d99bd55aSTed Kremenek }; 76d99bd55aSTed Kremenek } // end anonymous namespace 77d99bd55aSTed Kremenek 78d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 79d99bd55aSTed Kremenek // AST walking. 80d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 81d99bd55aSTed Kremenek 82d99bd55aSTed Kremenek void WalkAST::VisitChildren(Stmt *S) { 83d99bd55aSTed Kremenek for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) 84d99bd55aSTed Kremenek if (Stmt *child = *I) 85d99bd55aSTed Kremenek Visit(child); 86d99bd55aSTed Kremenek } 87d99bd55aSTed Kremenek 88d99bd55aSTed Kremenek void WalkAST::VisitCallExpr(CallExpr *CE) { 89fca2e961SLenny Maiorani // Get the callee. 90fca2e961SLenny Maiorani const FunctionDecl *FD = CE->getDirectCallee(); 91fca2e961SLenny Maiorani 92fca2e961SLenny Maiorani if (!FD) 93fca2e961SLenny Maiorani return; 94fca2e961SLenny Maiorani 95fca2e961SLenny Maiorani // Get the name of the callee. If it's a builtin, strip off the prefix. 96fca2e961SLenny Maiorani IdentifierInfo *II = FD->getIdentifier(); 97fca2e961SLenny Maiorani if (!II) // if no identifier, not a simple C function 98fca2e961SLenny Maiorani return; 990e62c1ccSChris Lattner StringRef Name = II->getName(); 100fca2e961SLenny Maiorani if (Name.startswith("__builtin_")) 101fca2e961SLenny Maiorani Name = Name.substr(10); 102fca2e961SLenny Maiorani 103fca2e961SLenny Maiorani // Set the evaluation function by switching on the callee name. 104fca2e961SLenny Maiorani FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name) 105de909e49SLenny Maiorani .Case("gets", &WalkAST::checkCall_gets) 106de909e49SLenny Maiorani .Case("getpw", &WalkAST::checkCall_getpw) 107de909e49SLenny Maiorani .Case("mktemp", &WalkAST::checkCall_mktemp) 108de909e49SLenny Maiorani .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy) 109de909e49SLenny Maiorani .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat) 110de909e49SLenny Maiorani .Case("drand48", &WalkAST::checkCall_rand) 111de909e49SLenny Maiorani .Case("erand48", &WalkAST::checkCall_rand) 112de909e49SLenny Maiorani .Case("jrand48", &WalkAST::checkCall_rand) 113de909e49SLenny Maiorani .Case("lrand48", &WalkAST::checkCall_rand) 114de909e49SLenny Maiorani .Case("mrand48", &WalkAST::checkCall_rand) 115de909e49SLenny Maiorani .Case("nrand48", &WalkAST::checkCall_rand) 116de909e49SLenny Maiorani .Case("lcong48", &WalkAST::checkCall_rand) 117de909e49SLenny Maiorani .Case("rand", &WalkAST::checkCall_rand) 118de909e49SLenny Maiorani .Case("rand_r", &WalkAST::checkCall_rand) 119de909e49SLenny Maiorani .Case("random", &WalkAST::checkCall_random) 120*fedf5dfcSAnna Zaks .Case("vfork", &WalkAST::checkCall_vfork) 121fca2e961SLenny Maiorani .Default(NULL); 122fca2e961SLenny Maiorani 123fca2e961SLenny Maiorani // If the callee isn't defined, it is not of security concern. 124fca2e961SLenny Maiorani // Check and evaluate the call. 125fca2e961SLenny Maiorani if (evalFunction) 126fca2e961SLenny Maiorani (this->*evalFunction)(CE, FD); 127d99bd55aSTed Kremenek 128d99bd55aSTed Kremenek // Recurse and check children. 129d99bd55aSTed Kremenek VisitChildren(CE); 130d99bd55aSTed Kremenek } 131d99bd55aSTed Kremenek 132d99bd55aSTed Kremenek void WalkAST::VisitCompoundStmt(CompoundStmt *S) { 133d99bd55aSTed Kremenek for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) 134d99bd55aSTed Kremenek if (Stmt *child = *I) { 135d99bd55aSTed Kremenek if (CallExpr *CE = dyn_cast<CallExpr>(child)) 136de909e49SLenny Maiorani checkUncheckedReturnValue(CE); 137d99bd55aSTed Kremenek Visit(child); 138d99bd55aSTed Kremenek } 139d99bd55aSTed Kremenek } 140d99bd55aSTed Kremenek 141d99bd55aSTed Kremenek void WalkAST::VisitForStmt(ForStmt *FS) { 142de909e49SLenny Maiorani checkLoopConditionForFloat(FS); 143d99bd55aSTed Kremenek 144d99bd55aSTed Kremenek // Recurse and check children. 145d99bd55aSTed Kremenek VisitChildren(FS); 146d99bd55aSTed Kremenek } 147d99bd55aSTed Kremenek 148d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 149d99bd55aSTed Kremenek // Check: floating poing variable used as loop counter. 150d99bd55aSTed Kremenek // Originally: <rdar://problem/6336718> 151d99bd55aSTed Kremenek // Implements: CERT security coding advisory FLP-30. 152d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 153d99bd55aSTed Kremenek 154d99bd55aSTed Kremenek static const DeclRefExpr* 155de909e49SLenny Maiorani getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) { 156d99bd55aSTed Kremenek expr = expr->IgnoreParenCasts(); 157d99bd55aSTed Kremenek 158d99bd55aSTed Kremenek if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) { 159d99bd55aSTed Kremenek if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() || 160d99bd55aSTed Kremenek B->getOpcode() == BO_Comma)) 161d99bd55aSTed Kremenek return NULL; 162d99bd55aSTed Kremenek 163de909e49SLenny Maiorani if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y)) 164d99bd55aSTed Kremenek return lhs; 165d99bd55aSTed Kremenek 166de909e49SLenny Maiorani if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y)) 167d99bd55aSTed Kremenek return rhs; 168d99bd55aSTed Kremenek 169d99bd55aSTed Kremenek return NULL; 170d99bd55aSTed Kremenek } 171d99bd55aSTed Kremenek 172d99bd55aSTed Kremenek if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) { 173d99bd55aSTed Kremenek const NamedDecl *ND = DR->getDecl(); 174d99bd55aSTed Kremenek return ND == x || ND == y ? DR : NULL; 175d99bd55aSTed Kremenek } 176d99bd55aSTed Kremenek 177d99bd55aSTed Kremenek if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr)) 178d99bd55aSTed Kremenek return U->isIncrementDecrementOp() 179de909e49SLenny Maiorani ? getIncrementedVar(U->getSubExpr(), x, y) : NULL; 180d99bd55aSTed Kremenek 181d99bd55aSTed Kremenek return NULL; 182d99bd55aSTed Kremenek } 183d99bd55aSTed Kremenek 184d99bd55aSTed Kremenek /// CheckLoopConditionForFloat - This check looks for 'for' statements that 185d99bd55aSTed Kremenek /// use a floating point variable as a loop counter. 186d99bd55aSTed Kremenek /// CERT: FLP30-C, FLP30-CPP. 187d99bd55aSTed Kremenek /// 188de909e49SLenny Maiorani void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) { 189d99bd55aSTed Kremenek // Does the loop have a condition? 190d99bd55aSTed Kremenek const Expr *condition = FS->getCond(); 191d99bd55aSTed Kremenek 192d99bd55aSTed Kremenek if (!condition) 193d99bd55aSTed Kremenek return; 194d99bd55aSTed Kremenek 195d99bd55aSTed Kremenek // Does the loop have an increment? 196d99bd55aSTed Kremenek const Expr *increment = FS->getInc(); 197d99bd55aSTed Kremenek 198d99bd55aSTed Kremenek if (!increment) 199d99bd55aSTed Kremenek return; 200d99bd55aSTed Kremenek 201d99bd55aSTed Kremenek // Strip away '()' and casts. 202d99bd55aSTed Kremenek condition = condition->IgnoreParenCasts(); 203d99bd55aSTed Kremenek increment = increment->IgnoreParenCasts(); 204d99bd55aSTed Kremenek 205d99bd55aSTed Kremenek // Is the loop condition a comparison? 206d99bd55aSTed Kremenek const BinaryOperator *B = dyn_cast<BinaryOperator>(condition); 207d99bd55aSTed Kremenek 208d99bd55aSTed Kremenek if (!B) 209d99bd55aSTed Kremenek return; 210d99bd55aSTed Kremenek 211d99bd55aSTed Kremenek // Is this a comparison? 212d99bd55aSTed Kremenek if (!(B->isRelationalOp() || B->isEqualityOp())) 213d99bd55aSTed Kremenek return; 214d99bd55aSTed Kremenek 215d99bd55aSTed Kremenek // Are we comparing variables? 216d99bd55aSTed Kremenek const DeclRefExpr *drLHS = 217d99bd55aSTed Kremenek dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts()); 218d99bd55aSTed Kremenek const DeclRefExpr *drRHS = 219d99bd55aSTed Kremenek dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts()); 220d99bd55aSTed Kremenek 221d99bd55aSTed Kremenek // Does at least one of the variables have a floating point type? 222d99bd55aSTed Kremenek drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL; 223d99bd55aSTed Kremenek drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL; 224d99bd55aSTed Kremenek 225d99bd55aSTed Kremenek if (!drLHS && !drRHS) 226d99bd55aSTed Kremenek return; 227d99bd55aSTed Kremenek 228d99bd55aSTed Kremenek const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL; 229d99bd55aSTed Kremenek const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL; 230d99bd55aSTed Kremenek 231d99bd55aSTed Kremenek if (!vdLHS && !vdRHS) 232d99bd55aSTed Kremenek return; 233d99bd55aSTed Kremenek 234d99bd55aSTed Kremenek // Does either variable appear in increment? 235de909e49SLenny Maiorani const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS); 236d99bd55aSTed Kremenek 237d99bd55aSTed Kremenek if (!drInc) 238d99bd55aSTed Kremenek return; 239d99bd55aSTed Kremenek 240d99bd55aSTed Kremenek // Emit the error. First figure out which DeclRefExpr in the condition 241d99bd55aSTed Kremenek // referenced the compared variable. 242d99bd55aSTed Kremenek const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS; 243d99bd55aSTed Kremenek 2440e62c1ccSChris Lattner SmallVector<SourceRange, 2> ranges; 245d99bd55aSTed Kremenek llvm::SmallString<256> sbuf; 246d99bd55aSTed Kremenek llvm::raw_svector_ostream os(sbuf); 247d99bd55aSTed Kremenek 248d99bd55aSTed Kremenek os << "Variable '" << drCond->getDecl()->getName() 249d99bd55aSTed Kremenek << "' with floating point type '" << drCond->getType().getAsString() 250d99bd55aSTed Kremenek << "' should not be used as a loop counter"; 251d99bd55aSTed Kremenek 252d99bd55aSTed Kremenek ranges.push_back(drCond->getSourceRange()); 253d99bd55aSTed Kremenek ranges.push_back(drInc->getSourceRange()); 254d99bd55aSTed Kremenek 255d99bd55aSTed Kremenek const char *bugType = "Floating point variable used as loop counter"; 256c29bed39SAnna Zaks 257c29bed39SAnna Zaks PathDiagnosticLocation FSLoc = 258c29bed39SAnna Zaks PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC); 259d99bd55aSTed Kremenek BR.EmitBasicReport(bugType, "Security", os.str(), 260c29bed39SAnna Zaks FSLoc, ranges.data(), ranges.size()); 261d99bd55aSTed Kremenek } 262d99bd55aSTed Kremenek 263d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 264d99bd55aSTed Kremenek // Check: Any use of 'gets' is insecure. 265d99bd55aSTed Kremenek // Originally: <rdar://problem/6335715> 266d99bd55aSTed Kremenek // Implements (part of): 300-BSI (buildsecurityin.us-cert.gov) 267d99bd55aSTed Kremenek // CWE-242: Use of Inherently Dangerous Function 268d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 269d99bd55aSTed Kremenek 270de909e49SLenny Maiorani void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) { 271d99bd55aSTed Kremenek const FunctionProtoType *FPT 272d99bd55aSTed Kremenek = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 273d99bd55aSTed Kremenek if (!FPT) 274d99bd55aSTed Kremenek return; 275d99bd55aSTed Kremenek 276d99bd55aSTed Kremenek // Verify that the function takes a single argument. 277d99bd55aSTed Kremenek if (FPT->getNumArgs() != 1) 278d99bd55aSTed Kremenek return; 279d99bd55aSTed Kremenek 280d99bd55aSTed Kremenek // Is the argument a 'char*'? 281d99bd55aSTed Kremenek const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0)); 282d99bd55aSTed Kremenek if (!PT) 283d99bd55aSTed Kremenek return; 284d99bd55aSTed Kremenek 285d99bd55aSTed Kremenek if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) 286d99bd55aSTed Kremenek return; 287d99bd55aSTed Kremenek 288d99bd55aSTed Kremenek // Issue a warning. 289d99bd55aSTed Kremenek SourceRange R = CE->getCallee()->getSourceRange(); 290c29bed39SAnna Zaks PathDiagnosticLocation CELoc = 291c29bed39SAnna Zaks PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 292d99bd55aSTed Kremenek BR.EmitBasicReport("Potential buffer overflow in call to 'gets'", 293d99bd55aSTed Kremenek "Security", 294d99bd55aSTed Kremenek "Call to function 'gets' is extremely insecure as it can " 295d99bd55aSTed Kremenek "always result in a buffer overflow", 296c29bed39SAnna Zaks CELoc, &R, 1); 297d99bd55aSTed Kremenek } 298d99bd55aSTed Kremenek 299d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 300d99bd55aSTed Kremenek // Check: Any use of 'getpwd' is insecure. 301d99bd55aSTed Kremenek // CWE-477: Use of Obsolete Functions 302d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 303d99bd55aSTed Kremenek 304de909e49SLenny Maiorani void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) { 305d99bd55aSTed Kremenek const FunctionProtoType *FPT 306d99bd55aSTed Kremenek = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 307d99bd55aSTed Kremenek if (!FPT) 308d99bd55aSTed Kremenek return; 309d99bd55aSTed Kremenek 310d99bd55aSTed Kremenek // Verify that the function takes two arguments. 311d99bd55aSTed Kremenek if (FPT->getNumArgs() != 2) 312d99bd55aSTed Kremenek return; 313d99bd55aSTed Kremenek 314d99bd55aSTed Kremenek // Verify the first argument type is integer. 315d99bd55aSTed Kremenek if (!FPT->getArgType(0)->isIntegerType()) 316d99bd55aSTed Kremenek return; 317d99bd55aSTed Kremenek 318d99bd55aSTed Kremenek // Verify the second argument type is char*. 319d99bd55aSTed Kremenek const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1)); 320d99bd55aSTed Kremenek if (!PT) 321d99bd55aSTed Kremenek return; 322d99bd55aSTed Kremenek 323d99bd55aSTed Kremenek if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) 324d99bd55aSTed Kremenek return; 325d99bd55aSTed Kremenek 326d99bd55aSTed Kremenek // Issue a warning. 327d99bd55aSTed Kremenek SourceRange R = CE->getCallee()->getSourceRange(); 328c29bed39SAnna Zaks PathDiagnosticLocation CELoc = 329c29bed39SAnna Zaks PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 330d99bd55aSTed Kremenek BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'", 331d99bd55aSTed Kremenek "Security", 332d99bd55aSTed Kremenek "The getpw() function is dangerous as it may overflow the " 333d99bd55aSTed Kremenek "provided buffer. It is obsoleted by getpwuid().", 334c29bed39SAnna Zaks CELoc, &R, 1); 335d99bd55aSTed Kremenek } 336d99bd55aSTed Kremenek 337d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 338d99bd55aSTed Kremenek // Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp(). 339d99bd55aSTed Kremenek // CWE-377: Insecure Temporary File 340d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 341d99bd55aSTed Kremenek 342de909e49SLenny Maiorani void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) { 343d99bd55aSTed Kremenek const FunctionProtoType *FPT 344d99bd55aSTed Kremenek = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 345d99bd55aSTed Kremenek if(!FPT) 346d99bd55aSTed Kremenek return; 347d99bd55aSTed Kremenek 34870568c2bSLenny Maiorani // Verify that the function takes a single argument. 349d99bd55aSTed Kremenek if (FPT->getNumArgs() != 1) 350d99bd55aSTed Kremenek return; 351d99bd55aSTed Kremenek 352d99bd55aSTed Kremenek // Verify that the argument is Pointer Type. 353d99bd55aSTed Kremenek const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0)); 354d99bd55aSTed Kremenek if (!PT) 355d99bd55aSTed Kremenek return; 356d99bd55aSTed Kremenek 357d99bd55aSTed Kremenek // Verify that the argument is a 'char*'. 358d99bd55aSTed Kremenek if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) 359d99bd55aSTed Kremenek return; 360d99bd55aSTed Kremenek 361d99bd55aSTed Kremenek // Issue a waring. 362d99bd55aSTed Kremenek SourceRange R = CE->getCallee()->getSourceRange(); 363c29bed39SAnna Zaks PathDiagnosticLocation CELoc = 364c29bed39SAnna Zaks PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 365d99bd55aSTed Kremenek BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'", 366d99bd55aSTed Kremenek "Security", 367d99bd55aSTed Kremenek "Call to function 'mktemp' is insecure as it always " 368d99bd55aSTed Kremenek "creates or uses insecure temporary file. Use 'mkstemp' instead", 369c29bed39SAnna Zaks CELoc, &R, 1); 370d99bd55aSTed Kremenek } 371d99bd55aSTed Kremenek 372d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 3736ffe738fSLenny Maiorani // Check: Any use of 'strcpy' is insecure. 3746ffe738fSLenny Maiorani // 3756ffe738fSLenny Maiorani // CWE-119: Improper Restriction of Operations within 3766ffe738fSLenny Maiorani // the Bounds of a Memory Buffer 3776ffe738fSLenny Maiorani //===----------------------------------------------------------------------===// 378de909e49SLenny Maiorani void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) { 379de909e49SLenny Maiorani if (!checkCall_strCommon(CE, FD)) 3806ffe738fSLenny Maiorani return; 3816ffe738fSLenny Maiorani 382de909e49SLenny Maiorani // Issue a warning. 3836ffe738fSLenny Maiorani SourceRange R = CE->getCallee()->getSourceRange(); 384c29bed39SAnna Zaks PathDiagnosticLocation CELoc = 385c29bed39SAnna Zaks PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 3866ffe738fSLenny Maiorani BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in " 3876ffe738fSLenny Maiorani "call 'strcpy'", 3886ffe738fSLenny Maiorani "Security", 3896ffe738fSLenny Maiorani "Call to function 'strcpy' is insecure as it does not " 3906ffe738fSLenny Maiorani "provide bounding of the memory buffer. Replace " 3916ffe738fSLenny Maiorani "unbounded copy functions with analogous functions that " 3926ffe738fSLenny Maiorani "support length arguments such as 'strncpy'. CWE-119.", 393c29bed39SAnna Zaks CELoc, &R, 1); 3946ffe738fSLenny Maiorani } 3956ffe738fSLenny Maiorani 3966ffe738fSLenny Maiorani //===----------------------------------------------------------------------===// 397de909e49SLenny Maiorani // Check: Any use of 'strcat' is insecure. 398de909e49SLenny Maiorani // 399de909e49SLenny Maiorani // CWE-119: Improper Restriction of Operations within 400de909e49SLenny Maiorani // the Bounds of a Memory Buffer 401de909e49SLenny Maiorani //===----------------------------------------------------------------------===// 402de909e49SLenny Maiorani void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) { 403de909e49SLenny Maiorani if (!checkCall_strCommon(CE, FD)) 404de909e49SLenny Maiorani return; 405de909e49SLenny Maiorani 406de909e49SLenny Maiorani // Issue a warning. 407de909e49SLenny Maiorani SourceRange R = CE->getCallee()->getSourceRange(); 408c29bed39SAnna Zaks PathDiagnosticLocation CELoc = 409c29bed39SAnna Zaks PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 410de909e49SLenny Maiorani BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in " 411de909e49SLenny Maiorani "call 'strcat'", 412de909e49SLenny Maiorani "Security", 413de909e49SLenny Maiorani "Call to function 'strcat' is insecure as it does not " 414de909e49SLenny Maiorani "provide bounding of the memory buffer. Replace " 415de909e49SLenny Maiorani "unbounded copy functions with analogous functions that " 416de909e49SLenny Maiorani "support length arguments such as 'strncat'. CWE-119.", 417c29bed39SAnna Zaks CELoc, &R, 1); 418de909e49SLenny Maiorani } 419de909e49SLenny Maiorani 420de909e49SLenny Maiorani //===----------------------------------------------------------------------===// 421de909e49SLenny Maiorani // Common check for str* functions with no bounds parameters. 422de909e49SLenny Maiorani //===----------------------------------------------------------------------===// 423de909e49SLenny Maiorani bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) { 424de909e49SLenny Maiorani const FunctionProtoType *FPT 425de909e49SLenny Maiorani = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 426de909e49SLenny Maiorani if (!FPT) 427de909e49SLenny Maiorani return false; 428de909e49SLenny Maiorani 429de909e49SLenny Maiorani // Verify the function takes two arguments, three in the _chk version. 430de909e49SLenny Maiorani int numArgs = FPT->getNumArgs(); 431de909e49SLenny Maiorani if (numArgs != 2 && numArgs != 3) 432de909e49SLenny Maiorani return false; 433de909e49SLenny Maiorani 434de909e49SLenny Maiorani // Verify the type for both arguments. 435de909e49SLenny Maiorani for (int i = 0; i < 2; i++) { 436de909e49SLenny Maiorani // Verify that the arguments are pointers. 437de909e49SLenny Maiorani const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i)); 438de909e49SLenny Maiorani if (!PT) 439de909e49SLenny Maiorani return false; 440de909e49SLenny Maiorani 441de909e49SLenny Maiorani // Verify that the argument is a 'char*'. 442de909e49SLenny Maiorani if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) 443de909e49SLenny Maiorani return false; 444de909e49SLenny Maiorani } 445de909e49SLenny Maiorani 446de909e49SLenny Maiorani return true; 447de909e49SLenny Maiorani } 448de909e49SLenny Maiorani 449de909e49SLenny Maiorani //===----------------------------------------------------------------------===// 450d99bd55aSTed Kremenek // Check: Linear congruent random number generators should not be used 451d99bd55aSTed Kremenek // Originally: <rdar://problem/63371000> 452d99bd55aSTed Kremenek // CWE-338: Use of cryptographically weak prng 453d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 454d99bd55aSTed Kremenek 455de909e49SLenny Maiorani void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) { 456fca2e961SLenny Maiorani if (!CheckRand) 457d99bd55aSTed Kremenek return; 458d99bd55aSTed Kremenek 459d99bd55aSTed Kremenek const FunctionProtoType *FTP 460d99bd55aSTed Kremenek = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 461d99bd55aSTed Kremenek if (!FTP) 462d99bd55aSTed Kremenek return; 463d99bd55aSTed Kremenek 464d99bd55aSTed Kremenek if (FTP->getNumArgs() == 1) { 465d99bd55aSTed Kremenek // Is the argument an 'unsigned short *'? 466d99bd55aSTed Kremenek // (Actually any integer type is allowed.) 467d99bd55aSTed Kremenek const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0)); 468d99bd55aSTed Kremenek if (!PT) 469d99bd55aSTed Kremenek return; 470d99bd55aSTed Kremenek 471d99bd55aSTed Kremenek if (! PT->getPointeeType()->isIntegerType()) 472d99bd55aSTed Kremenek return; 473d99bd55aSTed Kremenek } 474d99bd55aSTed Kremenek else if (FTP->getNumArgs() != 0) 475d99bd55aSTed Kremenek return; 476d99bd55aSTed Kremenek 477d99bd55aSTed Kremenek // Issue a warning. 478d99bd55aSTed Kremenek llvm::SmallString<256> buf1; 479d99bd55aSTed Kremenek llvm::raw_svector_ostream os1(buf1); 480d99bd55aSTed Kremenek os1 << '\'' << FD << "' is a poor random number generator"; 481d99bd55aSTed Kremenek 482d99bd55aSTed Kremenek llvm::SmallString<256> buf2; 483d99bd55aSTed Kremenek llvm::raw_svector_ostream os2(buf2); 484d99bd55aSTed Kremenek os2 << "Function '" << FD 485d99bd55aSTed Kremenek << "' is obsolete because it implements a poor random number generator." 486d99bd55aSTed Kremenek << " Use 'arc4random' instead"; 487d99bd55aSTed Kremenek 488d99bd55aSTed Kremenek SourceRange R = CE->getCallee()->getSourceRange(); 489c29bed39SAnna Zaks PathDiagnosticLocation CELoc = 490c29bed39SAnna Zaks PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 491c29bed39SAnna Zaks BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1); 492d99bd55aSTed Kremenek } 493d99bd55aSTed Kremenek 494d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 495d99bd55aSTed Kremenek // Check: 'random' should not be used 496d99bd55aSTed Kremenek // Originally: <rdar://problem/63371000> 497d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 498d99bd55aSTed Kremenek 499de909e49SLenny Maiorani void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) { 500fca2e961SLenny Maiorani if (!CheckRand) 501d99bd55aSTed Kremenek return; 502d99bd55aSTed Kremenek 503d99bd55aSTed Kremenek const FunctionProtoType *FTP 504d99bd55aSTed Kremenek = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 505d99bd55aSTed Kremenek if (!FTP) 506d99bd55aSTed Kremenek return; 507d99bd55aSTed Kremenek 508d99bd55aSTed Kremenek // Verify that the function takes no argument. 509d99bd55aSTed Kremenek if (FTP->getNumArgs() != 0) 510d99bd55aSTed Kremenek return; 511d99bd55aSTed Kremenek 512d99bd55aSTed Kremenek // Issue a warning. 513d99bd55aSTed Kremenek SourceRange R = CE->getCallee()->getSourceRange(); 514c29bed39SAnna Zaks PathDiagnosticLocation CELoc = 515c29bed39SAnna Zaks PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 516d99bd55aSTed Kremenek BR.EmitBasicReport("'random' is not a secure random number generator", 517d99bd55aSTed Kremenek "Security", 518d99bd55aSTed Kremenek "The 'random' function produces a sequence of values that " 519d99bd55aSTed Kremenek "an adversary may be able to predict. Use 'arc4random' " 520c29bed39SAnna Zaks "instead", CELoc, &R, 1); 521d99bd55aSTed Kremenek } 522d99bd55aSTed Kremenek 523d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 524*fedf5dfcSAnna Zaks // Check: 'vfork' should not be used. 525*fedf5dfcSAnna Zaks // POS33-C: Do not use vfork(). 526*fedf5dfcSAnna Zaks //===----------------------------------------------------------------------===// 527*fedf5dfcSAnna Zaks 528*fedf5dfcSAnna Zaks void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) { 529*fedf5dfcSAnna Zaks // All calls to vfork() are insecure, issue a warning. 530*fedf5dfcSAnna Zaks SourceRange R = CE->getCallee()->getSourceRange(); 531*fedf5dfcSAnna Zaks PathDiagnosticLocation CELoc = 532*fedf5dfcSAnna Zaks PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 533*fedf5dfcSAnna Zaks BR.EmitBasicReport("Potential insecure implementation-specific behavior in " 534*fedf5dfcSAnna Zaks "call 'vfork'", 535*fedf5dfcSAnna Zaks "Security", 536*fedf5dfcSAnna Zaks "Call to function 'vfork' is insecure as it can lead to " 537*fedf5dfcSAnna Zaks "denial of service situations in the parent process. " 538*fedf5dfcSAnna Zaks "Replace calls to vfork with calls to the safer " 539*fedf5dfcSAnna Zaks "'posix_spawn' function", 540*fedf5dfcSAnna Zaks CELoc, &R, 1); 541*fedf5dfcSAnna Zaks } 542*fedf5dfcSAnna Zaks 543*fedf5dfcSAnna Zaks //===----------------------------------------------------------------------===// 544d99bd55aSTed Kremenek // Check: Should check whether privileges are dropped successfully. 545d99bd55aSTed Kremenek // Originally: <rdar://problem/6337132> 546d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 547d99bd55aSTed Kremenek 548de909e49SLenny Maiorani void WalkAST::checkUncheckedReturnValue(CallExpr *CE) { 549d99bd55aSTed Kremenek const FunctionDecl *FD = CE->getDirectCallee(); 550d99bd55aSTed Kremenek if (!FD) 551d99bd55aSTed Kremenek return; 552d99bd55aSTed Kremenek 553d99bd55aSTed Kremenek if (II_setid[0] == NULL) { 554d99bd55aSTed Kremenek static const char * const identifiers[num_setids] = { 555d99bd55aSTed Kremenek "setuid", "setgid", "seteuid", "setegid", 556d99bd55aSTed Kremenek "setreuid", "setregid" 557d99bd55aSTed Kremenek }; 558d99bd55aSTed Kremenek 559d99bd55aSTed Kremenek for (size_t i = 0; i < num_setids; i++) 560d99bd55aSTed Kremenek II_setid[i] = &BR.getContext().Idents.get(identifiers[i]); 561d99bd55aSTed Kremenek } 562d99bd55aSTed Kremenek 563d99bd55aSTed Kremenek const IdentifierInfo *id = FD->getIdentifier(); 564d99bd55aSTed Kremenek size_t identifierid; 565d99bd55aSTed Kremenek 566d99bd55aSTed Kremenek for (identifierid = 0; identifierid < num_setids; identifierid++) 567d99bd55aSTed Kremenek if (id == II_setid[identifierid]) 568d99bd55aSTed Kremenek break; 569d99bd55aSTed Kremenek 570d99bd55aSTed Kremenek if (identifierid >= num_setids) 571d99bd55aSTed Kremenek return; 572d99bd55aSTed Kremenek 573d99bd55aSTed Kremenek const FunctionProtoType *FTP 574d99bd55aSTed Kremenek = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 575d99bd55aSTed Kremenek if (!FTP) 576d99bd55aSTed Kremenek return; 577d99bd55aSTed Kremenek 578d99bd55aSTed Kremenek // Verify that the function takes one or two arguments (depending on 579d99bd55aSTed Kremenek // the function). 580d99bd55aSTed Kremenek if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2)) 581d99bd55aSTed Kremenek return; 582d99bd55aSTed Kremenek 583d99bd55aSTed Kremenek // The arguments must be integers. 584d99bd55aSTed Kremenek for (unsigned i = 0; i < FTP->getNumArgs(); i++) 585d99bd55aSTed Kremenek if (! FTP->getArgType(i)->isIntegerType()) 586d99bd55aSTed Kremenek return; 587d99bd55aSTed Kremenek 588d99bd55aSTed Kremenek // Issue a warning. 589d99bd55aSTed Kremenek llvm::SmallString<256> buf1; 590d99bd55aSTed Kremenek llvm::raw_svector_ostream os1(buf1); 591d99bd55aSTed Kremenek os1 << "Return value is not checked in call to '" << FD << '\''; 592d99bd55aSTed Kremenek 593d99bd55aSTed Kremenek llvm::SmallString<256> buf2; 594d99bd55aSTed Kremenek llvm::raw_svector_ostream os2(buf2); 595d99bd55aSTed Kremenek os2 << "The return value from the call to '" << FD 596d99bd55aSTed Kremenek << "' is not checked. If an error occurs in '" << FD 597d99bd55aSTed Kremenek << "', the following code may execute with unexpected privileges"; 598d99bd55aSTed Kremenek 599d99bd55aSTed Kremenek SourceRange R = CE->getCallee()->getSourceRange(); 600c29bed39SAnna Zaks PathDiagnosticLocation CELoc = 601c29bed39SAnna Zaks PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 602c29bed39SAnna Zaks BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1); 603d99bd55aSTed Kremenek } 604d99bd55aSTed Kremenek 605d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 606af45aca6SArgyrios Kyrtzidis // SecuritySyntaxChecker 607d99bd55aSTed Kremenek //===----------------------------------------------------------------------===// 608d99bd55aSTed Kremenek 609af45aca6SArgyrios Kyrtzidis namespace { 6106a5674ffSArgyrios Kyrtzidis class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> { 611af45aca6SArgyrios Kyrtzidis public: 612af45aca6SArgyrios Kyrtzidis void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, 613af45aca6SArgyrios Kyrtzidis BugReporter &BR) const { 614c29bed39SAnna Zaks WalkAST walker(BR, mgr.getAnalysisContext(D)); 615d99bd55aSTed Kremenek walker.Visit(D->getBody()); 616d99bd55aSTed Kremenek } 617af45aca6SArgyrios Kyrtzidis }; 618af45aca6SArgyrios Kyrtzidis } 619af45aca6SArgyrios Kyrtzidis 620af45aca6SArgyrios Kyrtzidis void ento::registerSecuritySyntaxChecker(CheckerManager &mgr) { 621af45aca6SArgyrios Kyrtzidis mgr.registerChecker<SecuritySyntaxChecker>(); 622af45aca6SArgyrios Kyrtzidis } 623