blob: 10f86a13ffa6d2ae2d598e8709e4aebef7944bca [file] [log] [blame]
Ted Kremenekdbfb5f82009-07-23 01:07:19 +00001//==- 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 Kyrtzidis7dd445e2011-02-17 21:39:33 +000014#include "ClangSACheckers.h"
Anna Zaks590dd8e2011-09-20 21:38:35 +000015#include "clang/Analysis/AnalysisContext.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/Basic/TargetInfo.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Anna Zaks590dd8e2011-09-20 21:38:35 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000021#include "llvm/ADT/SmallString.h"
Lenny Maioranic2dace12011-04-03 05:07:11 +000022#include "llvm/ADT/StringSwitch.h"
Anna Zaks590dd8e2011-09-20 21:38:35 +000023#include "llvm/Support/raw_ostream.h"
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000024
25using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000026using namespace ento;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000027
Ted Kremenek88c8bc82010-01-15 08:20:31 +000028static bool isArc4RandomAvailable(const ASTContext &Ctx) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000029 const llvm::Triple &T = Ctx.getTargetInfo().getTriple();
Ted Kremenek88c8bc82010-01-15 08:20:31 +000030 return T.getVendor() == llvm::Triple::Apple ||
Douglas Gregor0f565592011-01-17 19:16:24 +000031 T.getOS() == llvm::Triple::FreeBSD ||
32 T.getOS() == llvm::Triple::NetBSD ||
33 T.getOS() == llvm::Triple::OpenBSD ||
34 T.getOS() == llvm::Triple::DragonFly;
Ted Kremenek88c8bc82010-01-15 08:20:31 +000035}
36
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000037namespace {
Ted Kremenek76a54242012-01-20 01:44:29 +000038struct DefaultBool {
39 bool val;
40 DefaultBool() : val(false) {}
41 operator bool() const { return val; }
42 DefaultBool &operator=(bool b) { val = b; return *this; }
43};
44
45struct ChecksFilter {
46 DefaultBool check_gets;
47 DefaultBool check_getpw;
48 DefaultBool check_mktemp;
Ted Kremenekb63d8d82012-01-20 05:35:06 +000049 DefaultBool check_mkstemp;
Ted Kremenek76a54242012-01-20 01:44:29 +000050 DefaultBool check_strcpy;
51 DefaultBool check_rand;
52 DefaultBool check_vfork;
53 DefaultBool check_FloatLoopCounter;
Ted Kremenekb63d8d82012-01-20 05:35:06 +000054 DefaultBool check_UncheckedReturn;
Ted Kremenek76a54242012-01-20 01:44:29 +000055};
56
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000057class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump1eb44332009-09-09 15:08:12 +000058 BugReporter &BR;
Ted Kremenek1d26f482011-10-24 01:32:45 +000059 AnalysisDeclContext* AC;
Ted Kremenek24650472009-09-02 02:47:41 +000060 enum { num_setids = 6 };
61 IdentifierInfo *II_setid[num_setids];
Ted Kremenek2c016762010-03-24 22:39:47 +000062
Ted Kremenek88c8bc82010-01-15 08:20:31 +000063 const bool CheckRand;
Ted Kremenek76a54242012-01-20 01:44:29 +000064 const ChecksFilter &filter;
Mike Stump1eb44332009-09-09 15:08:12 +000065
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000066public:
Ted Kremenek76a54242012-01-20 01:44:29 +000067 WalkAST(BugReporter &br, AnalysisDeclContext* ac,
68 const ChecksFilter &f)
Anna Zaks590dd8e2011-09-20 21:38:35 +000069 : BR(br), AC(ac), II_setid(),
Ted Kremenek76a54242012-01-20 01:44:29 +000070 CheckRand(isArc4RandomAvailable(BR.getContext())),
71 filter(f) {}
Mike Stump1eb44332009-09-09 15:08:12 +000072
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000073 // Statement visitor methods.
Ted Kremenekefcbb152009-07-23 22:29:41 +000074 void VisitCallExpr(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000075 void VisitForStmt(ForStmt *S);
Ted Kremenek65a81a92009-08-28 00:08:09 +000076 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek8baf86d2009-07-23 21:34:35 +000077 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000078
79 void VisitChildren(Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +000080
Ted Kremenekefcbb152009-07-23 22:29:41 +000081 // Helpers.
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000082 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +000083
Lenny Maioranic2dace12011-04-03 05:07:11 +000084 typedef void (WalkAST::*FnCheck)(const CallExpr *,
85 const FunctionDecl *);
86
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000087 // Checker-specific methods.
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000088 void checkLoopConditionForFloat(const ForStmt *FS);
89 void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
90 void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
91 void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenekb63d8d82012-01-20 05:35:06 +000092 void checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000093 void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
94 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
95 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
96 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
Anna Zaksa7957ff2011-10-11 04:34:54 +000097 void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000098 void checkUncheckedReturnValue(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000099};
100} // end anonymous namespace
101
102//===----------------------------------------------------------------------===//
103// AST walking.
104//===----------------------------------------------------------------------===//
105
106void WalkAST::VisitChildren(Stmt *S) {
107 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
108 if (Stmt *child = *I)
109 Visit(child);
110}
111
Ted Kremenekefcbb152009-07-23 22:29:41 +0000112void WalkAST::VisitCallExpr(CallExpr *CE) {
Lenny Maioranic2dace12011-04-03 05:07:11 +0000113 // Get the callee.
114 const FunctionDecl *FD = CE->getDirectCallee();
115
116 if (!FD)
117 return;
118
119 // Get the name of the callee. If it's a builtin, strip off the prefix.
120 IdentifierInfo *II = FD->getIdentifier();
121 if (!II) // if no identifier, not a simple C function
122 return;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000123 StringRef Name = II->getName();
Lenny Maioranic2dace12011-04-03 05:07:11 +0000124 if (Name.startswith("__builtin_"))
125 Name = Name.substr(10);
126
127 // Set the evaluation function by switching on the callee name.
128 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000129 .Case("gets", &WalkAST::checkCall_gets)
130 .Case("getpw", &WalkAST::checkCall_getpw)
131 .Case("mktemp", &WalkAST::checkCall_mktemp)
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000132 .Case("mkstemp", &WalkAST::checkCall_mkstemp)
133 .Case("mkdtemp", &WalkAST::checkCall_mkstemp)
134 .Case("mkstemps", &WalkAST::checkCall_mkstemp)
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000135 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
136 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
137 .Case("drand48", &WalkAST::checkCall_rand)
138 .Case("erand48", &WalkAST::checkCall_rand)
139 .Case("jrand48", &WalkAST::checkCall_rand)
140 .Case("lrand48", &WalkAST::checkCall_rand)
141 .Case("mrand48", &WalkAST::checkCall_rand)
142 .Case("nrand48", &WalkAST::checkCall_rand)
143 .Case("lcong48", &WalkAST::checkCall_rand)
144 .Case("rand", &WalkAST::checkCall_rand)
145 .Case("rand_r", &WalkAST::checkCall_rand)
146 .Case("random", &WalkAST::checkCall_random)
Anna Zaksa7957ff2011-10-11 04:34:54 +0000147 .Case("vfork", &WalkAST::checkCall_vfork)
Lenny Maioranic2dace12011-04-03 05:07:11 +0000148 .Default(NULL);
149
150 // If the callee isn't defined, it is not of security concern.
151 // Check and evaluate the call.
152 if (evalFunction)
153 (this->*evalFunction)(CE, FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Ted Kremenekefcbb152009-07-23 22:29:41 +0000155 // Recurse and check children.
156 VisitChildren(CE);
157}
158
Ted Kremenek65a81a92009-08-28 00:08:09 +0000159void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
160 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000161 if (Stmt *child = *I) {
162 if (CallExpr *CE = dyn_cast<CallExpr>(child))
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000163 checkUncheckedReturnValue(CE);
Mike Stump1eb44332009-09-09 15:08:12 +0000164 Visit(child);
165 }
Ted Kremenek65a81a92009-08-28 00:08:09 +0000166}
167
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000168void WalkAST::VisitForStmt(ForStmt *FS) {
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000169 checkLoopConditionForFloat(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000170
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000171 // Recurse and check children.
172 VisitChildren(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000173}
174
175//===----------------------------------------------------------------------===//
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000176// Check: floating poing variable used as loop counter.
Ted Kremenek5abeb522009-07-23 21:44:18 +0000177// Originally: <rdar://problem/6336718>
178// Implements: CERT security coding advisory FLP-30.
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000179//===----------------------------------------------------------------------===//
180
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000181static const DeclRefExpr*
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000182getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000183 expr = expr->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000184
185 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000186 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCall2de56d12010-08-25 11:45:40 +0000187 B->getOpcode() == BO_Comma))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000188 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000190 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000191 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000193 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000194 return rhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000196 return NULL;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000199 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
200 const NamedDecl *ND = DR->getDecl();
201 return ND == x || ND == y ? DR : NULL;
202 }
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000204 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
205 return U->isIncrementDecrementOp()
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000206 ? getIncrementedVar(U->getSubExpr(), x, y) : NULL;
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000207
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000208 return NULL;
209}
210
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000211/// CheckLoopConditionForFloat - This check looks for 'for' statements that
212/// use a floating point variable as a loop counter.
213/// CERT: FLP30-C, FLP30-CPP.
214///
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000215void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000216 if (!filter.check_FloatLoopCounter)
217 return;
218
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000219 // Does the loop have a condition?
220 const Expr *condition = FS->getCond();
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000222 if (!condition)
223 return;
224
225 // Does the loop have an increment?
226 const Expr *increment = FS->getInc();
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000228 if (!increment)
229 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000231 // Strip away '()' and casts.
232 condition = condition->IgnoreParenCasts();
233 increment = increment->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000235 // Is the loop condition a comparison?
236 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
237
238 if (!B)
239 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremenekcad9f412009-07-24 20:26:31 +0000241 // Is this a comparison?
242 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000243 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000245 // Are we comparing variables?
John McCallf6a16482010-12-04 03:47:34 +0000246 const DeclRefExpr *drLHS =
247 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
248 const DeclRefExpr *drRHS =
249 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Ted Kremenekcad9f412009-07-24 20:26:31 +0000251 // Does at least one of the variables have a floating point type?
Douglas Gregor0c293ea2010-06-22 23:07:26 +0000252 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
253 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000255 if (!drLHS && !drRHS)
256 return;
257
258 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
259 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000261 if (!vdLHS && !vdRHS)
Mike Stump1eb44332009-09-09 15:08:12 +0000262 return;
263
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000264 // Does either variable appear in increment?
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000265 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000267 if (!drInc)
268 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000270 // Emit the error. First figure out which DeclRefExpr in the condition
271 // referenced the compared variable.
272 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
273
Chris Lattner5f9e2722011-07-23 10:55:15 +0000274 SmallVector<SourceRange, 2> ranges;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000275 SmallString<256> sbuf;
Ted Kremenek2c016762010-03-24 22:39:47 +0000276 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Daniel Dunbar4087f272010-08-17 22:39:59 +0000278 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000279 << "' with floating point type '" << drCond->getType().getAsString()
280 << "' should not be used as a loop counter";
281
282 ranges.push_back(drCond->getSourceRange());
283 ranges.push_back(drInc->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000285 const char *bugType = "Floating point variable used as loop counter";
Anna Zaks590dd8e2011-09-20 21:38:35 +0000286
287 PathDiagnosticLocation FSLoc =
288 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
Benjamin Kramerf0171732009-11-29 18:27:55 +0000289 BR.EmitBasicReport(bugType, "Security", os.str(),
Anna Zaks590dd8e2011-09-20 21:38:35 +0000290 FSLoc, ranges.data(), ranges.size());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000291}
292
293//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +0000294// Check: Any use of 'gets' is insecure.
295// Originally: <rdar://problem/6335715>
296// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuaa30b3b2009-11-09 08:13:04 +0000297// CWE-242: Use of Inherently Dangerous Function
Ted Kremenekefcbb152009-07-23 22:29:41 +0000298//===----------------------------------------------------------------------===//
299
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000300void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000301 if (!filter.check_gets)
302 return;
303
Abramo Bagnara723df242010-12-14 22:11:44 +0000304 const FunctionProtoType *FPT
305 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000306 if (!FPT)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000307 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Ted Kremenekefcbb152009-07-23 22:29:41 +0000309 // Verify that the function takes a single argument.
Zhongxing Xubd842e32009-11-09 12:19:26 +0000310 if (FPT->getNumArgs() != 1)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000311 return;
312
313 // Is the argument a 'char*'?
Zhongxing Xubd842e32009-11-09 12:19:26 +0000314 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenekefcbb152009-07-23 22:29:41 +0000315 if (!PT)
316 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenekefcbb152009-07-23 22:29:41 +0000318 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
319 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremenekefcbb152009-07-23 22:29:41 +0000321 // Issue a warning.
322 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000323 PathDiagnosticLocation CELoc =
324 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenekefcbb152009-07-23 22:29:41 +0000325 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
326 "Security",
327 "Call to function 'gets' is extremely insecure as it can "
328 "always result in a buffer overflow",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000329 CELoc, &R, 1);
Ted Kremenekefcbb152009-07-23 22:29:41 +0000330}
331
332//===----------------------------------------------------------------------===//
Zhongxing Xubd842e32009-11-09 12:19:26 +0000333// Check: Any use of 'getpwd' is insecure.
334// CWE-477: Use of Obsolete Functions
335//===----------------------------------------------------------------------===//
336
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000337void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000338 if (!filter.check_getpw)
339 return;
340
Abramo Bagnara723df242010-12-14 22:11:44 +0000341 const FunctionProtoType *FPT
342 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000343 if (!FPT)
344 return;
345
346 // Verify that the function takes two arguments.
347 if (FPT->getNumArgs() != 2)
348 return;
349
350 // Verify the first argument type is integer.
351 if (!FPT->getArgType(0)->isIntegerType())
352 return;
353
354 // Verify the second argument type is char*.
355 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
356 if (!PT)
357 return;
358
359 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
360 return;
361
362 // Issue a warning.
363 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000364 PathDiagnosticLocation CELoc =
365 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Zhongxing Xubd842e32009-11-09 12:19:26 +0000366 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
367 "Security",
368 "The getpw() function is dangerous as it may overflow the "
369 "provided buffer. It is obsoleted by getpwuid().",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000370 CELoc, &R, 1);
Zhongxing Xubd842e32009-11-09 12:19:26 +0000371}
372
373//===----------------------------------------------------------------------===//
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000374// Check: Any use of 'mktemp' is insecure. It is obsoleted by mkstemp().
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000375// CWE-377: Insecure Temporary File
376//===----------------------------------------------------------------------===//
377
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000378void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000379 if (!filter.check_mktemp) {
380 // Fall back to the security check of looking for enough 'X's in the
381 // format string, since that is a less severe warning.
382 checkCall_mkstemp(CE, FD);
Ted Kremenek76a54242012-01-20 01:44:29 +0000383 return;
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000384 }
Ted Kremenek76a54242012-01-20 01:44:29 +0000385
Abramo Bagnara723df242010-12-14 22:11:44 +0000386 const FunctionProtoType *FPT
387 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000388 if(!FPT)
389 return;
Ted Kremenek2c016762010-03-24 22:39:47 +0000390
Lenny Maioraniea4411e2011-03-31 21:26:55 +0000391 // Verify that the function takes a single argument.
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000392 if (FPT->getNumArgs() != 1)
393 return;
394
395 // Verify that the argument is Pointer Type.
396 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
397 if (!PT)
398 return;
399
400 // Verify that the argument is a 'char*'.
401 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
402 return;
Ted Kremenek431a2cb2010-03-24 22:39:45 +0000403
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000404 // Issue a waring.
405 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000406 PathDiagnosticLocation CELoc =
407 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000408 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
Eli Friedmana7e68452010-08-22 01:00:03 +0000409 "Security",
410 "Call to function 'mktemp' is insecure as it always "
411 "creates or uses insecure temporary file. Use 'mkstemp' instead",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000412 CELoc, &R, 1);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000413}
414
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000415
416//===----------------------------------------------------------------------===//
417// Check: Use of 'mkstemp', 'mktemp', 'mkdtemp' should contain at least 6 X's.
418//===----------------------------------------------------------------------===//
419
420void WalkAST::checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD) {
421 if (!filter.check_mkstemp)
422 return;
423
424 StringRef Name = FD->getIdentifier()->getName();
425 std::pair<signed, signed> ArgSuffix =
426 llvm::StringSwitch<std::pair<signed, signed> >(Name)
427 .Case("mktemp", std::make_pair(0,-1))
428 .Case("mkstemp", std::make_pair(0,-1))
429 .Case("mkdtemp", std::make_pair(0,-1))
430 .Case("mkstemps", std::make_pair(0,1))
431 .Default(std::make_pair(-1, -1));
432
433 assert(ArgSuffix.first >= 0 && "Unsupported function");
434
435 // Check if the number of arguments is consistent with out expectations.
436 unsigned numArgs = CE->getNumArgs();
437 if ((signed) numArgs <= ArgSuffix.first)
438 return;
439
440 const StringLiteral *strArg =
441 dyn_cast<StringLiteral>(CE->getArg((unsigned)ArgSuffix.first)
442 ->IgnoreParenImpCasts());
443
444 // Currently we only handle string literals. It is possible to do better,
445 // either by looking at references to const variables, or by doing real
446 // flow analysis.
447 if (!strArg || strArg->getCharByteWidth() != 1)
448 return;
449
450 // Count the number of X's, taking into account a possible cutoff suffix.
451 StringRef str = strArg->getString();
452 unsigned numX = 0;
453 unsigned n = str.size();
454
455 // Take into account the suffix.
456 unsigned suffix = 0;
457 if (ArgSuffix.second >= 0) {
458 const Expr *suffixEx = CE->getArg((unsigned)ArgSuffix.second);
459 llvm::APSInt Result;
460 if (!suffixEx->EvaluateAsInt(Result, BR.getContext()))
461 return;
462 // FIXME: Issue a warning.
463 if (Result.isNegative())
464 return;
465 suffix = (unsigned) Result.getZExtValue();
466 n = (n > suffix) ? n - suffix : 0;
467 }
468
469 for (unsigned i = 0; i < n; ++i)
470 if (str[i] == 'X') ++numX;
471
472 if (numX >= 6)
473 return;
474
475 // Issue a warning.
476 SourceRange R = strArg->getSourceRange();
477 PathDiagnosticLocation CELoc =
478 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000479 SmallString<512> buf;
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000480 llvm::raw_svector_ostream out(buf);
481 out << "Call to '" << Name << "' should have at least 6 'X's in the"
482 " format string to be secure (" << numX << " 'X'";
483 if (numX != 1)
484 out << 's';
485 out << " seen";
486 if (suffix) {
487 out << ", " << suffix << " character";
488 if (suffix > 1)
489 out << 's';
490 out << " used as a suffix";
491 }
492 out << ')';
493 BR.EmitBasicReport("Insecure temporary file creation", "Security",
494 out.str(), CELoc, &R, 1);
495}
496
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000497//===----------------------------------------------------------------------===//
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000498// Check: Any use of 'strcpy' is insecure.
499//
500// CWE-119: Improper Restriction of Operations within
501// the Bounds of a Memory Buffer
502//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000503void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000504 if (!filter.check_strcpy)
505 return;
506
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000507 if (!checkCall_strCommon(CE, FD))
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000508 return;
509
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000510 // Issue a warning.
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000511 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000512 PathDiagnosticLocation CELoc =
513 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000514 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000515 "call 'strcpy'",
516 "Security",
517 "Call to function 'strcpy' is insecure as it does not "
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000518 "provide bounding of the memory buffer. Replace "
519 "unbounded copy functions with analogous functions that "
Anna Zaks393b9792012-01-31 19:33:31 +0000520 "support length arguments such as 'strlcpy'. CWE-119.",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000521 CELoc, &R, 1);
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000522}
523
524//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000525// Check: Any use of 'strcat' is insecure.
526//
527// CWE-119: Improper Restriction of Operations within
528// the Bounds of a Memory Buffer
529//===----------------------------------------------------------------------===//
530void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000531 if (!filter.check_strcpy)
532 return;
533
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000534 if (!checkCall_strCommon(CE, FD))
535 return;
536
537 // Issue a warning.
538 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000539 PathDiagnosticLocation CELoc =
540 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000541 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
542 "call 'strcat'",
543 "Security",
544 "Call to function 'strcat' is insecure as it does not "
545 "provide bounding of the memory buffer. Replace "
546 "unbounded copy functions with analogous functions that "
Anna Zaks393b9792012-01-31 19:33:31 +0000547 "support length arguments such as 'strlcat'. CWE-119.",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000548 CELoc, &R, 1);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000549}
550
551//===----------------------------------------------------------------------===//
552// Common check for str* functions with no bounds parameters.
553//===----------------------------------------------------------------------===//
554bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
555 const FunctionProtoType *FPT
556 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
557 if (!FPT)
558 return false;
559
560 // Verify the function takes two arguments, three in the _chk version.
561 int numArgs = FPT->getNumArgs();
562 if (numArgs != 2 && numArgs != 3)
563 return false;
564
565 // Verify the type for both arguments.
566 for (int i = 0; i < 2; i++) {
567 // Verify that the arguments are pointers.
568 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
569 if (!PT)
570 return false;
571
572 // Verify that the argument is a 'char*'.
573 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
574 return false;
575 }
576
577 return true;
578}
579
580//===----------------------------------------------------------------------===//
Ted Kremenek24650472009-09-02 02:47:41 +0000581// Check: Linear congruent random number generators should not be used
582// Originally: <rdar://problem/63371000>
583// CWE-338: Use of cryptographically weak prng
584//===----------------------------------------------------------------------===//
585
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000586void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000587 if (!filter.check_rand || !CheckRand)
Ted Kremenek24650472009-09-02 02:47:41 +0000588 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Abramo Bagnara723df242010-12-14 22:11:44 +0000590 const FunctionProtoType *FTP
591 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000592 if (!FTP)
593 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Ted Kremenek24650472009-09-02 02:47:41 +0000595 if (FTP->getNumArgs() == 1) {
596 // Is the argument an 'unsigned short *'?
597 // (Actually any integer type is allowed.)
598 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
599 if (!PT)
600 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Ted Kremenek24650472009-09-02 02:47:41 +0000602 if (! PT->getPointeeType()->isIntegerType())
603 return;
604 }
Mike Stump1eb44332009-09-09 15:08:12 +0000605 else if (FTP->getNumArgs() != 0)
Ted Kremenek24650472009-09-02 02:47:41 +0000606 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Ted Kremenek24650472009-09-02 02:47:41 +0000608 // Issue a warning.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000609 SmallString<256> buf1;
Ted Kremenek2c016762010-03-24 22:39:47 +0000610 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000611 os1 << '\'' << *FD << "' is a poor random number generator";
Ted Kremenek24650472009-09-02 02:47:41 +0000612
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000613 SmallString<256> buf2;
Ted Kremenek2c016762010-03-24 22:39:47 +0000614 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000615 os2 << "Function '" << *FD
Ted Kremenek24650472009-09-02 02:47:41 +0000616 << "' is obsolete because it implements a poor random number generator."
617 << " Use 'arc4random' instead";
618
619 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000620 PathDiagnosticLocation CELoc =
621 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
622 BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000623}
624
625//===----------------------------------------------------------------------===//
626// Check: 'random' should not be used
627// Originally: <rdar://problem/63371000>
628//===----------------------------------------------------------------------===//
629
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000630void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000631 if (!CheckRand || !filter.check_rand)
Ted Kremenek24650472009-09-02 02:47:41 +0000632 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Abramo Bagnara723df242010-12-14 22:11:44 +0000634 const FunctionProtoType *FTP
635 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000636 if (!FTP)
637 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek24650472009-09-02 02:47:41 +0000639 // Verify that the function takes no argument.
640 if (FTP->getNumArgs() != 0)
641 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Ted Kremenek24650472009-09-02 02:47:41 +0000643 // Issue a warning.
644 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000645 PathDiagnosticLocation CELoc =
646 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek24650472009-09-02 02:47:41 +0000647 BR.EmitBasicReport("'random' is not a secure random number generator",
648 "Security",
649 "The 'random' function produces a sequence of values that "
650 "an adversary may be able to predict. Use 'arc4random' "
Anna Zaks590dd8e2011-09-20 21:38:35 +0000651 "instead", CELoc, &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000652}
653
654//===----------------------------------------------------------------------===//
Anna Zaksa7957ff2011-10-11 04:34:54 +0000655// Check: 'vfork' should not be used.
656// POS33-C: Do not use vfork().
657//===----------------------------------------------------------------------===//
658
659void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000660 if (!filter.check_vfork)
661 return;
662
Anna Zaksa7957ff2011-10-11 04:34:54 +0000663 // All calls to vfork() are insecure, issue a warning.
664 SourceRange R = CE->getCallee()->getSourceRange();
665 PathDiagnosticLocation CELoc =
666 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
667 BR.EmitBasicReport("Potential insecure implementation-specific behavior in "
668 "call 'vfork'",
669 "Security",
670 "Call to function 'vfork' is insecure as it can lead to "
671 "denial of service situations in the parent process. "
672 "Replace calls to vfork with calls to the safer "
673 "'posix_spawn' function",
674 CELoc, &R, 1);
675}
676
677//===----------------------------------------------------------------------===//
Ted Kremenek65a81a92009-08-28 00:08:09 +0000678// Check: Should check whether privileges are dropped successfully.
679// Originally: <rdar://problem/6337132>
680//===----------------------------------------------------------------------===//
681
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000682void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000683 if (!filter.check_UncheckedReturn)
684 return;
685
Ted Kremenek65a81a92009-08-28 00:08:09 +0000686 const FunctionDecl *FD = CE->getDirectCallee();
687 if (!FD)
688 return;
689
690 if (II_setid[0] == NULL) {
Ted Kremenek24650472009-09-02 02:47:41 +0000691 static const char * const identifiers[num_setids] = {
Ted Kremenek65a81a92009-08-28 00:08:09 +0000692 "setuid", "setgid", "seteuid", "setegid",
693 "setreuid", "setregid"
694 };
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Ted Kremenek24650472009-09-02 02:47:41 +0000696 for (size_t i = 0; i < num_setids; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000697 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000698 }
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Ted Kremenek65a81a92009-08-28 00:08:09 +0000700 const IdentifierInfo *id = FD->getIdentifier();
701 size_t identifierid;
702
Ted Kremenek24650472009-09-02 02:47:41 +0000703 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000704 if (id == II_setid[identifierid])
705 break;
706
Ted Kremenek24650472009-09-02 02:47:41 +0000707 if (identifierid >= num_setids)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000708 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Abramo Bagnara723df242010-12-14 22:11:44 +0000710 const FunctionProtoType *FTP
711 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek65a81a92009-08-28 00:08:09 +0000712 if (!FTP)
713 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Ted Kremeneka8187832009-08-28 00:24:55 +0000715 // Verify that the function takes one or two arguments (depending on
716 // the function).
Ted Kremenek65a81a92009-08-28 00:08:09 +0000717 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
718 return;
719
720 // The arguments must be integers.
721 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
722 if (! FTP->getArgType(i)->isIntegerType())
723 return;
724
725 // Issue a warning.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000726 SmallString<256> buf1;
Ted Kremenek2c016762010-03-24 22:39:47 +0000727 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000728 os1 << "Return value is not checked in call to '" << *FD << '\'';
Ted Kremenek65a81a92009-08-28 00:08:09 +0000729
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000730 SmallString<256> buf2;
Ted Kremenek2c016762010-03-24 22:39:47 +0000731 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000732 os2 << "The return value from the call to '" << *FD
733 << "' is not checked. If an error occurs in '" << *FD
Ted Kremenek65a81a92009-08-28 00:08:09 +0000734 << "', the following code may execute with unexpected privileges";
735
736 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000737 PathDiagnosticLocation CELoc =
738 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
739 BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000740}
741
742//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000743// SecuritySyntaxChecker
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000744//===----------------------------------------------------------------------===//
745
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000746namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000747class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000748public:
Ted Kremenek76a54242012-01-20 01:44:29 +0000749 ChecksFilter filter;
750
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000751 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
752 BugReporter &BR) const {
Ted Kremenek76a54242012-01-20 01:44:29 +0000753 WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter);
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000754 walker.Visit(D->getBody());
755 }
756};
757}
758
Ted Kremenek76a54242012-01-20 01:44:29 +0000759#define REGISTER_CHECKER(name) \
760void ento::register##name(CheckerManager &mgr) {\
761 mgr.registerChecker<SecuritySyntaxChecker>()->filter.check_##name = true;\
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000762}
Ted Kremenek76a54242012-01-20 01:44:29 +0000763
764REGISTER_CHECKER(gets)
765REGISTER_CHECKER(getpw)
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000766REGISTER_CHECKER(mkstemp)
Ted Kremenek76a54242012-01-20 01:44:29 +0000767REGISTER_CHECKER(mktemp)
768REGISTER_CHECKER(strcpy)
769REGISTER_CHECKER(rand)
770REGISTER_CHECKER(vfork)
771REGISTER_CHECKER(FloatLoopCounter)
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000772REGISTER_CHECKER(UncheckedReturn)
773
Ted Kremenek76a54242012-01-20 01:44:29 +0000774