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