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