zcov: / lib/Checker/CheckSecuritySyntaxOnly.cpp


Files: 1 Branches Taken: 71.0% 115 / 162
Generated: 2010-02-10 01:31 Branches Executed: 97.5% 158 / 162
Line Coverage: 83.1% 172 / 207


Programs: 1 Runs 2897


       1                 : //==- CheckSecuritySyntaxOnly.cpp - Basic security checks --------*- C++ -*-==//
       2                 : //
       3                 : //                     The LLVM Compiler Infrastructure
       4                 : //
       5                 : // This file is distributed under the University of Illinois Open Source
       6                 : // License. See LICENSE.TXT for details.
       7                 : //
       8                 : //===----------------------------------------------------------------------===//
       9                 : //
      10                 : //  This file defines a set of flow-insensitive security checks.
      11                 : //
      12                 : //===----------------------------------------------------------------------===//
      13                 : 
      14                 : #include "clang/Basic/TargetInfo.h"
      15                 : #include "clang/Checker/BugReporter/BugReporter.h"
      16                 : #include "clang/Checker/Checkers/LocalCheckers.h"
      17                 : #include "clang/AST/StmtVisitor.h"
      18                 : #include "llvm/Support/raw_ostream.h"
      19                 : 
      20                 : using namespace clang;
      21                 : 
      22                7: static bool isArc4RandomAvailable(const ASTContext &Ctx) {
      23                7:   const llvm::Triple &T = Ctx.Target.getTriple();
      24                 :   return T.getVendor() == llvm::Triple::Apple ||
                        1: branch 1 taken
                        6: branch 2 taken
                        0: branch 4 not taken
                        1: branch 5 taken
      25                7:          T.getOS() == llvm::Triple::FreeBSD;
      26                 : }
      27                 : 
      28                 : namespace {
      29                 : class WalkAST : public StmtVisitor<WalkAST> {
      30                 :   BugReporter &BR;
      31                 :   IdentifierInfo *II_gets;
      32                 :   IdentifierInfo *II_getpw;
      33                 :   IdentifierInfo *II_mktemp;
      34                 :   enum { num_rands = 9 };
      35                 :   IdentifierInfo *II_rand[num_rands];
      36                 :   IdentifierInfo *II_random;
      37                 :   enum { num_setids = 6 };
      38                 :   IdentifierInfo *II_setid[num_setids];
      39                 :   
      40                 :   const bool CheckRand;
      41                 : 
      42                 : public:
      43                7:   WalkAST(BugReporter &br) : BR(br),
      44                 : 			     II_gets(0), II_getpw(0), II_mktemp(0),
      45                 : 			     II_rand(), II_random(0), II_setid(),
                       35: branch 1 taken
                        7: branch 2 taken
      46                7:                  CheckRand(isArc4RandomAvailable(BR.getContext())) {}
      47                 : 
      48                 :   // Statement visitor methods.
      49                 :   void VisitCallExpr(CallExpr *CE);
      50                 :   void VisitForStmt(ForStmt *S);
      51                 :   void VisitCompoundStmt (CompoundStmt *S);
      52              201:   void VisitStmt(Stmt *S) { VisitChildren(S); }
      53                 : 
      54                 :   void VisitChildren(Stmt *S);
      55                 : 
      56                 :   // Helpers.
      57                 :   IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str);
      58                 : 
      59                 :   // Checker-specific methods.
      60                 :   void CheckLoopConditionForFloat(const ForStmt *FS);
      61                 :   void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD);
      62                 :   void CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
      63                 :   void CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
      64                 :   void CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD);
      65                 :   void CheckCall_random(const CallExpr *CE, const FunctionDecl *FD);
      66                 :   void CheckUncheckedReturnValue(CallExpr *CE);
      67                 : };
      68                 : } // end anonymous namespace
      69                 : 
      70                 : //===----------------------------------------------------------------------===//
      71                 : // Helper methods.
      72                 : //===----------------------------------------------------------------------===//
      73                 : 
      74              122: IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) {
                       23: branch 0 taken
                       99: branch 1 taken
      75              122:   if (!II)
      76               23:     II = &BR.getContext().Idents.get(str);
      77                 : 
      78              122:   return II;
      79                 : }
      80                 : 
      81                 : //===----------------------------------------------------------------------===//
      82                 : // AST walking.
      83                 : //===----------------------------------------------------------------------===//
      84                 : 
      85              242: void WalkAST::VisitChildren(Stmt *S) {
                      204: branch 4 taken
                      242: branch 5 taken
      86              446:   for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
                      203: branch 1 taken
                        1: branch 2 taken
      87              204:     if (Stmt *child = *I)
      88              203:       Visit(child);
      89              242: }
      90                 : 
      91               33: void WalkAST::VisitCallExpr(CallExpr *CE) {
                       33: branch 1 taken
                        0: branch 2 not taken
      92               33:   if (const FunctionDecl *FD = CE->getDirectCallee()) {
      93               33:     CheckCall_gets(CE, FD);
      94               33:     CheckCall_getpw(CE, FD);
      95               33:     CheckCall_mktemp(CE, FD);
                       23: branch 0 taken
                       10: branch 1 taken
      96               33:     if (CheckRand) {
      97               23:       CheckCall_rand(CE, FD);
      98               23:       CheckCall_random(CE, FD);
      99                 :     }
     100                 :   }
     101                 : 
     102                 :   // Recurse and check children.
     103               33:   VisitChildren(CE);
     104               33: }
     105                 : 
     106               15: void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
                       47: branch 4 taken
                       15: branch 5 taken
     107               62:   for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
                       47: branch 1 taken
                        0: branch 2 not taken
     108               47:     if (Stmt *child = *I) {
                       27: branch 1 taken
                       20: branch 2 taken
     109               47:       if (CallExpr *CE = dyn_cast<CallExpr>(child))
     110               27:         CheckUncheckedReturnValue(CE);
     111               47:       Visit(child);
     112                 :     }
     113               15: }
     114                 : 
     115                8: void WalkAST::VisitForStmt(ForStmt *FS) {
     116                8:   CheckLoopConditionForFloat(FS);
     117                 : 
     118                 :   // Recurse and check children.
     119                8:   VisitChildren(FS);
     120                8: }
     121                 : 
     122                 : //===----------------------------------------------------------------------===//
     123                 : // Check: floating poing variable used as loop counter.
     124                 : // Originally: <rdar://problem/6336718>
     125                 : // Implements: CERT security coding advisory FLP-30.
     126                 : //===----------------------------------------------------------------------===//
     127                 : 
     128                 : static const DeclRefExpr*
     129               17: GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
     130               17:   expr = expr->IgnoreParenCasts();
     131                 : 
                        4: branch 1 taken
                       13: branch 2 taken
     132               17:   if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
                        1: branch 1 taken
                        3: branch 2 taken
                        1: branch 4 taken
                        0: branch 5 not taken
                        0: branch 7 not taken
                        1: branch 8 taken
                        0: branch 9 not taken
                        4: branch 10 taken
     133                4:     if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
     134                 :           B->getOpcode() == BinaryOperator::Comma))
     135                0:       return NULL;
     136                 : 
                        4: branch 2 taken
                        0: branch 3 not taken
     137                4:     if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
     138                4:       return lhs;
     139                 : 
                        0: branch 2 not taken
                        0: branch 3 not taken
     140                0:     if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
     141                0:       return rhs;
     142                 : 
     143                0:     return NULL;
     144                 :   }
     145                 : 
                        8: branch 1 taken
                        5: branch 2 taken
     146               13:   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
     147                8:     const NamedDecl *ND = DR->getDecl();
                        1: branch 0 taken
                        7: branch 1 taken
                        1: branch 2 taken
                        0: branch 3 not taken
     148                8:     return ND == x || ND == y ? DR : NULL;
     149                 :   }
     150                 : 
                        5: branch 1 taken
                        0: branch 2 not taken
     151                5:   if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
     152                 :     return U->isIncrementDecrementOp()
                        5: branch 1 taken
                        0: branch 2 not taken
     153                5:       ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
     154                 : 
     155                0:   return NULL;
     156                 : }
     157                 : 
     158                 : /// CheckLoopConditionForFloat - This check looks for 'for' statements that
     159                 : ///  use a floating point variable as a loop counter.
     160                 : ///  CERT: FLP30-C, FLP30-CPP.
     161                 : ///
     162                8: void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
     163                 :   // Does the loop have a condition?
     164                8:   const Expr *condition = FS->getCond();
     165                 : 
                        0: branch 0 not taken
                        8: branch 1 taken
     166                8:   if (!condition)
     167                0:     return;
     168                 : 
     169                 :   // Does the loop have an increment?
     170                8:   const Expr *increment = FS->getInc();
     171                 : 
                        0: branch 0 not taken
                        8: branch 1 taken
     172                8:   if (!increment)
     173                0:     return;
     174                 : 
     175                 :   // Strip away '()' and casts.
     176                8:   condition = condition->IgnoreParenCasts();
     177                8:   increment = increment->IgnoreParenCasts();
     178                 : 
     179                 :   // Is the loop condition a comparison?
     180                8:   const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
     181                 : 
                        0: branch 0 not taken
                        8: branch 1 taken
     182                8:   if (!B)
     183                0:     return;
     184                 : 
     185                 :   // Is this a comparison?
                        0: branch 1 not taken
                        8: branch 2 taken
                        0: branch 4 not taken
                        0: branch 5 not taken
                        0: branch 6 not taken
                        8: branch 7 taken
     186                8:   if (!(B->isRelationalOp() || B->isEqualityOp()))
     187                0:     return;
     188                 : 
     189                 :   // Are we comparing variables?
     190                8:   const DeclRefExpr *drLHS = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParens());
     191                8:   const DeclRefExpr *drRHS = dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParens());
     192                 : 
     193                 :   // Does at least one of the variables have a floating point type?
                        7: branch 0 taken
                        1: branch 1 taken
                        7: branch 5 taken
                        0: branch 6 not taken
     194                8:   drLHS = drLHS && drLHS->getType()->isFloatingType() ? drLHS : NULL;
                        1: branch 0 taken
                        7: branch 1 taken
                        1: branch 5 taken
                        0: branch 6 not taken
     195                8:   drRHS = drRHS && drRHS->getType()->isFloatingType() ? drRHS : NULL;
     196                 : 
                        1: branch 0 taken
                        7: branch 1 taken
                        0: branch 2 not taken
                        1: branch 3 taken
     197                8:   if (!drLHS && !drRHS)
     198                0:     return;
     199                 : 
                        7: branch 0 taken
                        1: branch 1 taken
     200                8:   const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
                        1: branch 0 taken
                        7: branch 1 taken
     201                8:   const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
     202                 : 
                        1: branch 0 taken
                        7: branch 1 taken
                        0: branch 2 not taken
                        1: branch 3 taken
     203                8:   if (!vdLHS && !vdRHS)
     204                0:     return;
     205                 : 
     206                 :   // Does either variable appear in increment?
     207                8:   const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
     208                 : 
                        0: branch 0 not taken
                        8: branch 1 taken
     209                8:   if (!drInc)
     210                0:     return;
     211                 : 
     212                 :   // Emit the error.  First figure out which DeclRefExpr in the condition
     213                 :   // referenced the compared variable.
                        7: branch 1 taken
                        1: branch 2 taken
     214                8:   const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
     215                 : 
     216                8:   llvm::SmallVector<SourceRange, 2> ranges;
     217                8:   std::string sbuf;
     218                8:   llvm::raw_string_ostream os(sbuf);
     219                 : 
     220                 :   os << "Variable '" << drCond->getDecl()->getNameAsCString()
     221                 :      << "' with floating point type '" << drCond->getType().getAsString()
     222                8:      << "' should not be used as a loop counter";
     223                 : 
     224                8:   ranges.push_back(drCond->getSourceRange());
     225                8:   ranges.push_back(drInc->getSourceRange());
     226                 : 
     227                8:   const char *bugType = "Floating point variable used as loop counter";
     228                 :   BR.EmitBasicReport(bugType, "Security", os.str(),
     229                8:                      FS->getLocStart(), ranges.data(), ranges.size());
     230                 : }
     231                 : 
     232                 : //===----------------------------------------------------------------------===//
     233                 : // Check: Any use of 'gets' is insecure.
     234                 : // Originally: <rdar://problem/6335715>
     235                 : // Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
     236                 : // CWE-242: Use of Inherently Dangerous Function
     237                 : //===----------------------------------------------------------------------===//
     238                 : 
     239               33: void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
                       32: branch 2 taken
                        1: branch 3 taken
     240               33:   if (FD->getIdentifier() != GetIdentifier(II_gets, "gets"))
     241               32:     return;
     242                 : 
     243                1:   const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FD->getType());
                        0: branch 0 not taken
                        1: branch 1 taken
     244                1:   if (!FPT)
     245                0:     return;
     246                 : 
     247                 :   // Verify that the function takes a single argument.
                        0: branch 1 not taken
                        1: branch 2 taken
     248                1:   if (FPT->getNumArgs() != 1)
     249                0:     return;
     250                 : 
     251                 :   // Is the argument a 'char*'?
     252                1:   const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
                        0: branch 0 not taken
                        1: branch 1 taken
     253                1:   if (!PT)
     254                0:     return;
     255                 : 
                        0: branch 5 not taken
                        1: branch 6 taken
     256                1:   if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
     257                0:     return;
     258                 : 
     259                 :   // Issue a warning.
     260                1:   SourceRange R = CE->getCallee()->getSourceRange();
     261                 :   BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
     262                 :                      "Security",
     263                 :                      "Call to function 'gets' is extremely insecure as it can "
     264                 :                      "always result in a buffer overflow",
     265                1:                      CE->getLocStart(), &R, 1);
     266                 : }
     267                 : 
     268                 : //===----------------------------------------------------------------------===//
     269                 : // Check: Any use of 'getpwd' is insecure.
     270                 : // CWE-477: Use of Obsolete Functions
     271                 : //===----------------------------------------------------------------------===//
     272                 : 
     273               33: void WalkAST::CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
                       32: branch 2 taken
                        1: branch 3 taken
     274               33:   if (FD->getIdentifier() != GetIdentifier(II_getpw, "getpw"))
     275               32:     return;
     276                 : 
     277                1:   const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FD->getType());
                        0: branch 0 not taken
                        1: branch 1 taken
     278                1:   if (!FPT)
     279                0:     return;
     280                 : 
     281                 :   // Verify that the function takes two arguments.
                        0: branch 1 not taken
                        1: branch 2 taken
     282                1:   if (FPT->getNumArgs() != 2)
     283                0:     return;
     284                 : 
     285                 :   // Verify the first argument type is integer.
                        0: branch 3 not taken
                        1: branch 4 taken
     286                1:   if (!FPT->getArgType(0)->isIntegerType())
     287                0:     return;
     288                 : 
     289                 :   // Verify the second argument type is char*.
     290                1:   const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
                        0: branch 0 not taken
                        1: branch 1 taken
     291                1:   if (!PT)
     292                0:     return;
     293                 : 
                        0: branch 5 not taken
                        1: branch 6 taken
     294                1:   if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
     295                0:     return;
     296                 : 
     297                 :   // Issue a warning.
     298                1:   SourceRange R = CE->getCallee()->getSourceRange();
     299                 :   BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
     300                 :                      "Security",
     301                 :                      "The getpw() function is dangerous as it may overflow the "
     302                 :                      "provided buffer. It is obsoleted by getpwuid().",
     303                1:                      CE->getLocStart(), &R, 1);
     304                 : }
     305                 : 
     306                 : //===----------------------------------------------------------------------===//
     307                 : // Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
     308                 : // CWE-377: Insecure Temporary File
     309                 : //===----------------------------------------------------------------------===//
     310                 : 
     311               33: void WalkAST::CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
                       32: branch 2 taken
                        1: branch 3 taken
     312               33:   if (FD->getIdentifier() != GetIdentifier(II_mktemp, "mktemp"))
     313               32:     return;
     314                 : 
     315                1:   const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FD->getType());
                        0: branch 0 not taken
                        1: branch 1 taken
     316                1:   if(!FPT)
     317                0:     return;
     318                 :   
     319                 :   // Verify that the funcion takes a single argument.
                        0: branch 1 not taken
                        1: branch 2 taken
     320                1:   if (FPT->getNumArgs() != 1)
     321                0:     return;
     322                 : 
     323                 :   // Verify that the argument is Pointer Type.
     324                1:   const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
                        0: branch 0 not taken
                        1: branch 1 taken
     325                1:   if (!PT)
     326                0:     return;
     327                 : 
     328                 :   // Verify that the argument is a 'char*'.
                        0: branch 5 not taken
                        1: branch 6 taken
     329                1:   if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
     330                0:     return;
     331                 :   
     332                 :   // Issue a waring.
     333                1:   SourceRange R = CE->getCallee()->getSourceRange();
     334                 :   BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
     335                 : 		     "Security",
     336                 : 		     "Call to function 'mktemp' is insecure as it always "
     337                 : 		     "creates or uses insecure temporary file",
     338                1: 		     CE->getLocStart(), &R, 1);
     339                 : }
     340                 : 
     341                 : 
     342                 : //===----------------------------------------------------------------------===//
     343                 : // Check: Linear congruent random number generators should not be used
     344                 : // Originally: <rdar://problem/63371000>
     345                 : // CWE-338: Use of cryptographically weak prng
     346                 : //===----------------------------------------------------------------------===//
     347                 : 
     348               23: void WalkAST::CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
                        5: branch 0 taken
                       18: branch 1 taken
     349               23:   if (II_rand[0] == NULL) {
     350                 :     // This check applies to these functions
     351                 :     static const char * const identifiers[num_rands] = {
     352                 :       "drand48", "erand48", "jrand48", "lrand48", "mrand48", "nrand48",
     353                 :       "lcong48",
     354                 :       "rand", "rand_r"
     355                 :     };
     356                 : 
                       45: branch 0 taken
                        5: branch 1 taken
     357               50:     for (size_t i = 0; i < num_rands; i++)
     358               45:       II_rand[i] = &BR.getContext().Idents.get(identifiers[i]);
     359                 :   }
     360                 : 
     361               23:   const IdentifierInfo *id = FD->getIdentifier();
     362                 :   size_t identifierid;
     363                 : 
                      171: branch 0 taken
                       14: branch 1 taken
     364              185:   for (identifierid = 0; identifierid < num_rands; identifierid++)
                        9: branch 0 taken
                      162: branch 1 taken
     365              171:     if (id == II_rand[identifierid])
     366                9:       break;
     367                 : 
                       14: branch 0 taken
                        9: branch 1 taken
     368               23:   if (identifierid >= num_rands)
     369               14:     return;
     370                 : 
     371                9:   const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType());
                        0: branch 0 not taken
                        9: branch 1 taken
     372                9:   if (!FTP)
     373                0:     return;
     374                 : 
                        5: branch 1 taken
                        4: branch 2 taken
     375                9:   if (FTP->getNumArgs() == 1) {
     376                 :     // Is the argument an 'unsigned short *'?
     377                 :     // (Actually any integer type is allowed.)
     378                5:     const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
                        0: branch 0 not taken
                        5: branch 1 taken
     379                5:     if (!PT)
     380                0:       return;
     381                 : 
                        0: branch 3 not taken
                        5: branch 4 taken
     382                5:     if (! PT->getPointeeType()->isIntegerType())
     383                0:       return;
     384                 :   }
                        0: branch 1 not taken
                        4: branch 2 taken
     385                4:   else if (FTP->getNumArgs() != 0)
     386                0:     return;
     387                 : 
     388                 :   // Issue a warning.
     389                9:   std::string buf1;
     390                9:   llvm::raw_string_ostream os1(buf1);
     391                9:   os1 << "'" << FD->getNameAsString() << "' is a poor random number generator";
     392                 : 
     393                9:   std::string buf2;
     394                9:   llvm::raw_string_ostream os2(buf2);
     395                 :   os2 << "Function '" << FD->getNameAsString()
     396                 :       << "' is obsolete because it implements a poor random number generator."
     397                9:       << "  Use 'arc4random' instead";
     398                 : 
     399                9:   SourceRange R = CE->getCallee()->getSourceRange();
     400                 : 
     401                 :   BR.EmitBasicReport(os1.str(), "Security", os2.str(),
     402                9:                      CE->getLocStart(), &R, 1);
     403                 : }
     404                 : 
     405                 : //===----------------------------------------------------------------------===//
     406                 : // Check: 'random' should not be used
     407                 : // Originally: <rdar://problem/63371000>
     408                 : //===----------------------------------------------------------------------===//
     409                 : 
     410               23: void WalkAST::CheckCall_random(const CallExpr *CE, const FunctionDecl *FD) {
                       22: branch 2 taken
                        1: branch 3 taken
     411               23:   if (FD->getIdentifier() != GetIdentifier(II_random, "random"))
     412               22:     return;
     413                 : 
     414                1:   const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType());
                        0: branch 0 not taken
                        1: branch 1 taken
     415                1:   if (!FTP)
     416                0:     return;
     417                 : 
     418                 :   // Verify that the function takes no argument.
                        0: branch 1 not taken
                        1: branch 2 taken
     419                1:   if (FTP->getNumArgs() != 0)
     420                0:     return;
     421                 : 
     422                 :   // Issue a warning.
     423                1:   SourceRange R = CE->getCallee()->getSourceRange();
     424                 :   BR.EmitBasicReport("'random' is not a secure random number generator",
     425                 :                      "Security",
     426                 :                      "The 'random' function produces a sequence of values that "
     427                 :                      "an adversary may be able to predict.  Use 'arc4random' "
     428                 :                      "instead",
     429                1:                      CE->getLocStart(), &R, 1);
     430                 : }
     431                 : 
     432                 : //===----------------------------------------------------------------------===//
     433                 : // Check: Should check whether privileges are dropped successfully.
     434                 : // Originally: <rdar://problem/6337132>
     435                 : //===----------------------------------------------------------------------===//
     436                 : 
     437               27: void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) {
     438               27:   const FunctionDecl *FD = CE->getDirectCallee();
                        0: branch 0 not taken
                       27: branch 1 taken
     439               27:   if (!FD)
     440                0:     return;
     441                 : 
                        5: branch 0 taken
                       22: branch 1 taken
     442               27:   if (II_setid[0] == NULL) {
     443                 :     static const char * const identifiers[num_setids] = {
     444                 :       "setuid", "setgid", "seteuid", "setegid",
     445                 :       "setreuid", "setregid"
     446                 :     };
     447                 : 
                       30: branch 0 taken
                        5: branch 1 taken
     448               35:     for (size_t i = 0; i < num_setids; i++)
     449               30:       II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
     450                 :   }
     451                 : 
     452               27:   const IdentifierInfo *id = FD->getIdentifier();
     453                 :   size_t identifierid;
     454                 : 
                      151: branch 0 taken
                       23: branch 1 taken
     455              174:   for (identifierid = 0; identifierid < num_setids; identifierid++)
                        4: branch 0 taken
                      147: branch 1 taken
     456              151:     if (id == II_setid[identifierid])
     457                4:       break;
     458                 : 
                       23: branch 0 taken
                        4: branch 1 taken
     459               27:   if (identifierid >= num_setids)
     460               23:     return;
     461                 : 
     462                4:   const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType());
                        0: branch 0 not taken
                        4: branch 1 taken
     463                4:   if (!FTP)
     464                0:     return;
     465                 : 
     466                 :   // Verify that the function takes one or two arguments (depending on
     467                 :   //   the function).
                        2: branch 1 taken
                        2: branch 2 taken
                        0: branch 3 not taken
                        4: branch 4 taken
     468                4:   if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
     469                0:     return;
     470                 : 
     471                 :   // The arguments must be integers.
                        6: branch 1 taken
                        4: branch 2 taken
     472               10:   for (unsigned i = 0; i < FTP->getNumArgs(); i++)
                        0: branch 3 not taken
                        6: branch 4 taken
     473                6:     if (! FTP->getArgType(i)->isIntegerType())
     474                0:       return;
     475                 : 
     476                 :   // Issue a warning.
     477                4:   std::string buf1;
     478                4:   llvm::raw_string_ostream os1(buf1);
     479                 :   os1 << "Return value is not checked in call to '" << FD->getNameAsString()
     480                4:      << "'";
     481                 : 
     482                4:   std::string buf2;
     483                4:   llvm::raw_string_ostream os2(buf2);
     484                 :   os2 << "The return value from the call to '" << FD->getNameAsString()
     485                 :       << "' is not checked.  If an error occurs in '"
     486                 :       << FD->getNameAsString()
     487                4:       << "', the following code may execute with unexpected privileges";
     488                 : 
     489                4:   SourceRange R = CE->getCallee()->getSourceRange();
     490                 : 
     491                 :   BR.EmitBasicReport(os1.str(), "Security", os2.str(),
     492                4:                      CE->getLocStart(), &R, 1);
     493                 : }
     494                 : 
     495                 : //===----------------------------------------------------------------------===//
     496                 : // Entry point for check.
     497                 : //===----------------------------------------------------------------------===//
     498                 : 
     499                7: void clang::CheckSecuritySyntaxOnly(const Decl *D, BugReporter &BR) {
     500                7:   WalkAST walker(BR);
     501                7:   walker.Visit(D->getBody());
     502                7: }

Generated: 2010-02-10 01:31 by zcov