blob: b8b7c367969c5c423457c12c918187b6fb04838c [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 ||
Eli Friedman42f74f22012-08-08 23:57:20 +000034 T.getOS() == llvm::Triple::Bitrig ||
Douglas Gregor0f565592011-01-17 19:16:24 +000035 T.getOS() == llvm::Triple::DragonFly;
Ted Kremenek88c8bc82010-01-15 08:20:31 +000036}
37
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000038namespace {
Ted Kremenek76a54242012-01-20 01:44:29 +000039struct DefaultBool {
40 bool val;
41 DefaultBool() : val(false) {}
42 operator bool() const { return val; }
43 DefaultBool &operator=(bool b) { val = b; return *this; }
44};
45
46struct ChecksFilter {
47 DefaultBool check_gets;
48 DefaultBool check_getpw;
49 DefaultBool check_mktemp;
Ted Kremenekb63d8d82012-01-20 05:35:06 +000050 DefaultBool check_mkstemp;
Ted Kremenek76a54242012-01-20 01:44:29 +000051 DefaultBool check_strcpy;
52 DefaultBool check_rand;
53 DefaultBool check_vfork;
54 DefaultBool check_FloatLoopCounter;
Ted Kremenekb63d8d82012-01-20 05:35:06 +000055 DefaultBool check_UncheckedReturn;
Ted Kremenek76a54242012-01-20 01:44:29 +000056};
57
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000058class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump1eb44332009-09-09 15:08:12 +000059 BugReporter &BR;
Ted Kremenek1d26f482011-10-24 01:32:45 +000060 AnalysisDeclContext* AC;
Ted Kremenek24650472009-09-02 02:47:41 +000061 enum { num_setids = 6 };
62 IdentifierInfo *II_setid[num_setids];
Ted Kremenek2c016762010-03-24 22:39:47 +000063
Ted Kremenek88c8bc82010-01-15 08:20:31 +000064 const bool CheckRand;
Ted Kremenek76a54242012-01-20 01:44:29 +000065 const ChecksFilter &filter;
Mike Stump1eb44332009-09-09 15:08:12 +000066
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000067public:
Ted Kremenek76a54242012-01-20 01:44:29 +000068 WalkAST(BugReporter &br, AnalysisDeclContext* ac,
69 const ChecksFilter &f)
Anna Zaks590dd8e2011-09-20 21:38:35 +000070 : BR(br), AC(ac), II_setid(),
Ted Kremenek76a54242012-01-20 01:44:29 +000071 CheckRand(isArc4RandomAvailable(BR.getContext())),
72 filter(f) {}
Mike Stump1eb44332009-09-09 15:08:12 +000073
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000074 // Statement visitor methods.
Ted Kremenekefcbb152009-07-23 22:29:41 +000075 void VisitCallExpr(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000076 void VisitForStmt(ForStmt *S);
Ted Kremenek65a81a92009-08-28 00:08:09 +000077 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek8baf86d2009-07-23 21:34:35 +000078 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000079
80 void VisitChildren(Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +000081
Ted Kremenekefcbb152009-07-23 22:29:41 +000082 // Helpers.
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000083 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +000084
Lenny Maioranic2dace12011-04-03 05:07:11 +000085 typedef void (WalkAST::*FnCheck)(const CallExpr *,
86 const FunctionDecl *);
87
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000088 // Checker-specific methods.
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000089 void checkLoopConditionForFloat(const ForStmt *FS);
90 void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
91 void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
92 void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenekb63d8d82012-01-20 05:35:06 +000093 void checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000094 void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
95 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
96 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
97 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
Anna Zaksa7957ff2011-10-11 04:34:54 +000098 void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000099 void checkUncheckedReturnValue(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000100};
101} // end anonymous namespace
102
103//===----------------------------------------------------------------------===//
104// AST walking.
105//===----------------------------------------------------------------------===//
106
107void WalkAST::VisitChildren(Stmt *S) {
108 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
109 if (Stmt *child = *I)
110 Visit(child);
111}
112
Ted Kremenekefcbb152009-07-23 22:29:41 +0000113void WalkAST::VisitCallExpr(CallExpr *CE) {
Lenny Maioranic2dace12011-04-03 05:07:11 +0000114 // Get the callee.
115 const FunctionDecl *FD = CE->getDirectCallee();
116
117 if (!FD)
118 return;
119
120 // Get the name of the callee. If it's a builtin, strip off the prefix.
121 IdentifierInfo *II = FD->getIdentifier();
122 if (!II) // if no identifier, not a simple C function
123 return;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000124 StringRef Name = II->getName();
Lenny Maioranic2dace12011-04-03 05:07:11 +0000125 if (Name.startswith("__builtin_"))
126 Name = Name.substr(10);
127
128 // Set the evaluation function by switching on the callee name.
129 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000130 .Case("gets", &WalkAST::checkCall_gets)
131 .Case("getpw", &WalkAST::checkCall_getpw)
132 .Case("mktemp", &WalkAST::checkCall_mktemp)
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000133 .Case("mkstemp", &WalkAST::checkCall_mkstemp)
134 .Case("mkdtemp", &WalkAST::checkCall_mkstemp)
135 .Case("mkstemps", &WalkAST::checkCall_mkstemp)
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000136 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
137 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
138 .Case("drand48", &WalkAST::checkCall_rand)
139 .Case("erand48", &WalkAST::checkCall_rand)
140 .Case("jrand48", &WalkAST::checkCall_rand)
141 .Case("lrand48", &WalkAST::checkCall_rand)
142 .Case("mrand48", &WalkAST::checkCall_rand)
143 .Case("nrand48", &WalkAST::checkCall_rand)
144 .Case("lcong48", &WalkAST::checkCall_rand)
145 .Case("rand", &WalkAST::checkCall_rand)
146 .Case("rand_r", &WalkAST::checkCall_rand)
147 .Case("random", &WalkAST::checkCall_random)
Anna Zaksa7957ff2011-10-11 04:34:54 +0000148 .Case("vfork", &WalkAST::checkCall_vfork)
Lenny Maioranic2dace12011-04-03 05:07:11 +0000149 .Default(NULL);
150
151 // If the callee isn't defined, it is not of security concern.
152 // Check and evaluate the call.
153 if (evalFunction)
154 (this->*evalFunction)(CE, FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Ted Kremenekefcbb152009-07-23 22:29:41 +0000156 // Recurse and check children.
157 VisitChildren(CE);
158}
159
Ted Kremenek65a81a92009-08-28 00:08:09 +0000160void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
161 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000162 if (Stmt *child = *I) {
163 if (CallExpr *CE = dyn_cast<CallExpr>(child))
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000164 checkUncheckedReturnValue(CE);
Mike Stump1eb44332009-09-09 15:08:12 +0000165 Visit(child);
166 }
Ted Kremenek65a81a92009-08-28 00:08:09 +0000167}
168
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000169void WalkAST::VisitForStmt(ForStmt *FS) {
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000170 checkLoopConditionForFloat(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000171
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000172 // Recurse and check children.
173 VisitChildren(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000174}
175
176//===----------------------------------------------------------------------===//
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000177// Check: floating poing variable used as loop counter.
Ted Kremenek5abeb522009-07-23 21:44:18 +0000178// Originally: <rdar://problem/6336718>
179// Implements: CERT security coding advisory FLP-30.
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000180//===----------------------------------------------------------------------===//
181
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000182static const DeclRefExpr*
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000183getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000184 expr = expr->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000185
186 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000187 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCall2de56d12010-08-25 11:45:40 +0000188 B->getOpcode() == BO_Comma))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000189 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000191 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000192 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000194 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000195 return rhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000197 return NULL;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000200 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
201 const NamedDecl *ND = DR->getDecl();
202 return ND == x || ND == y ? DR : NULL;
203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000205 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
206 return U->isIncrementDecrementOp()
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000207 ? getIncrementedVar(U->getSubExpr(), x, y) : NULL;
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000208
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000209 return NULL;
210}
211
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000212/// CheckLoopConditionForFloat - This check looks for 'for' statements that
213/// use a floating point variable as a loop counter.
214/// CERT: FLP30-C, FLP30-CPP.
215///
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000216void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000217 if (!filter.check_FloatLoopCounter)
218 return;
219
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000220 // Does the loop have a condition?
221 const Expr *condition = FS->getCond();
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000223 if (!condition)
224 return;
225
226 // Does the loop have an increment?
227 const Expr *increment = FS->getInc();
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000229 if (!increment)
230 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000232 // Strip away '()' and casts.
233 condition = condition->IgnoreParenCasts();
234 increment = increment->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000236 // Is the loop condition a comparison?
237 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
238
239 if (!B)
240 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Ted Kremenekcad9f412009-07-24 20:26:31 +0000242 // Is this a comparison?
243 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000244 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000246 // Are we comparing variables?
John McCallf6a16482010-12-04 03:47:34 +0000247 const DeclRefExpr *drLHS =
248 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
249 const DeclRefExpr *drRHS =
250 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Ted Kremenekcad9f412009-07-24 20:26:31 +0000252 // Does at least one of the variables have a floating point type?
Douglas Gregor0c293ea2010-06-22 23:07:26 +0000253 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
254 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000256 if (!drLHS && !drRHS)
257 return;
258
259 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
260 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000262 if (!vdLHS && !vdRHS)
Mike Stump1eb44332009-09-09 15:08:12 +0000263 return;
264
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000265 // Does either variable appear in increment?
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000266 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000268 if (!drInc)
269 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000271 // Emit the error. First figure out which DeclRefExpr in the condition
272 // referenced the compared variable.
273 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
274
Chris Lattner5f9e2722011-07-23 10:55:15 +0000275 SmallVector<SourceRange, 2> ranges;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000276 SmallString<256> sbuf;
Ted Kremenek2c016762010-03-24 22:39:47 +0000277 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Daniel Dunbar4087f272010-08-17 22:39:59 +0000279 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000280 << "' with floating point type '" << drCond->getType().getAsString()
281 << "' should not be used as a loop counter";
282
283 ranges.push_back(drCond->getSourceRange());
284 ranges.push_back(drInc->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000286 const char *bugType = "Floating point variable used as loop counter";
Anna Zaks590dd8e2011-09-20 21:38:35 +0000287
288 PathDiagnosticLocation FSLoc =
289 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000290 BR.EmitBasicReport(AC->getDecl(),
291 bugType, "Security", os.str(),
Anna Zaks590dd8e2011-09-20 21:38:35 +0000292 FSLoc, ranges.data(), ranges.size());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000293}
294
295//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +0000296// Check: Any use of 'gets' is insecure.
297// Originally: <rdar://problem/6335715>
298// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuaa30b3b2009-11-09 08:13:04 +0000299// CWE-242: Use of Inherently Dangerous Function
Ted Kremenekefcbb152009-07-23 22:29:41 +0000300//===----------------------------------------------------------------------===//
301
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000302void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000303 if (!filter.check_gets)
304 return;
305
Abramo Bagnara723df242010-12-14 22:11:44 +0000306 const FunctionProtoType *FPT
307 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000308 if (!FPT)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000309 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Ted Kremenekefcbb152009-07-23 22:29:41 +0000311 // Verify that the function takes a single argument.
Zhongxing Xubd842e32009-11-09 12:19:26 +0000312 if (FPT->getNumArgs() != 1)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000313 return;
314
315 // Is the argument a 'char*'?
Zhongxing Xubd842e32009-11-09 12:19:26 +0000316 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenekefcbb152009-07-23 22:29:41 +0000317 if (!PT)
318 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Ted Kremenekefcbb152009-07-23 22:29:41 +0000320 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
321 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenekefcbb152009-07-23 22:29:41 +0000323 // Issue a warning.
324 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000325 PathDiagnosticLocation CELoc =
326 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000327 BR.EmitBasicReport(AC->getDecl(),
328 "Potential buffer overflow in call to 'gets'",
Ted Kremenekefcbb152009-07-23 22:29:41 +0000329 "Security",
330 "Call to function 'gets' is extremely insecure as it can "
331 "always result in a buffer overflow",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000332 CELoc, &R, 1);
Ted Kremenekefcbb152009-07-23 22:29:41 +0000333}
334
335//===----------------------------------------------------------------------===//
Zhongxing Xubd842e32009-11-09 12:19:26 +0000336// Check: Any use of 'getpwd' is insecure.
337// CWE-477: Use of Obsolete Functions
338//===----------------------------------------------------------------------===//
339
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000340void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000341 if (!filter.check_getpw)
342 return;
343
Abramo Bagnara723df242010-12-14 22:11:44 +0000344 const FunctionProtoType *FPT
345 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000346 if (!FPT)
347 return;
348
349 // Verify that the function takes two arguments.
350 if (FPT->getNumArgs() != 2)
351 return;
352
353 // Verify the first argument type is integer.
354 if (!FPT->getArgType(0)->isIntegerType())
355 return;
356
357 // Verify the second argument type is char*.
358 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
359 if (!PT)
360 return;
361
362 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
363 return;
364
365 // Issue a warning.
366 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000367 PathDiagnosticLocation CELoc =
368 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000369 BR.EmitBasicReport(AC->getDecl(),
370 "Potential buffer overflow in call to 'getpw'",
Zhongxing Xubd842e32009-11-09 12:19:26 +0000371 "Security",
372 "The getpw() function is dangerous as it may overflow the "
373 "provided buffer. It is obsoleted by getpwuid().",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000374 CELoc, &R, 1);
Zhongxing Xubd842e32009-11-09 12:19:26 +0000375}
376
377//===----------------------------------------------------------------------===//
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000378// Check: Any use of 'mktemp' is insecure. It is obsoleted by mkstemp().
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000379// CWE-377: Insecure Temporary File
380//===----------------------------------------------------------------------===//
381
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000382void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekb0754172012-06-29 21:01:35 +0000383 if (!filter.check_mktemp) {
384 // Fall back to the security check of looking for enough 'X's in the
385 // format string, since that is a less severe warning.
386 checkCall_mkstemp(CE, FD);
387 return;
388 }
389
Abramo Bagnara723df242010-12-14 22:11:44 +0000390 const FunctionProtoType *FPT
391 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000392 if(!FPT)
393 return;
Ted Kremenek2c016762010-03-24 22:39:47 +0000394
Lenny Maioraniea4411e2011-03-31 21:26:55 +0000395 // Verify that the function takes a single argument.
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000396 if (FPT->getNumArgs() != 1)
397 return;
398
399 // Verify that the argument is Pointer Type.
400 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
401 if (!PT)
402 return;
403
404 // Verify that the argument is a 'char*'.
405 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
406 return;
Ted Kremenek431a2cb2010-03-24 22:39:45 +0000407
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000408 // Issue a waring.
409 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000410 PathDiagnosticLocation CELoc =
411 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000412 BR.EmitBasicReport(AC->getDecl(),
413 "Potential insecure temporary file in call 'mktemp'",
Eli Friedmana7e68452010-08-22 01:00:03 +0000414 "Security",
415 "Call to function 'mktemp' is insecure as it always "
Ted Kremenek07189522012-04-04 18:11:35 +0000416 "creates or uses insecure temporary file. Use 'mkstemp' "
417 "instead",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000418 CELoc, &R, 1);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000419}
420
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000421
422//===----------------------------------------------------------------------===//
423// Check: Use of 'mkstemp', 'mktemp', 'mkdtemp' should contain at least 6 X's.
424//===----------------------------------------------------------------------===//
425
426void WalkAST::checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD) {
427 if (!filter.check_mkstemp)
428 return;
429
430 StringRef Name = FD->getIdentifier()->getName();
431 std::pair<signed, signed> ArgSuffix =
432 llvm::StringSwitch<std::pair<signed, signed> >(Name)
433 .Case("mktemp", std::make_pair(0,-1))
434 .Case("mkstemp", std::make_pair(0,-1))
435 .Case("mkdtemp", std::make_pair(0,-1))
436 .Case("mkstemps", std::make_pair(0,1))
437 .Default(std::make_pair(-1, -1));
438
439 assert(ArgSuffix.first >= 0 && "Unsupported function");
440
441 // Check if the number of arguments is consistent with out expectations.
442 unsigned numArgs = CE->getNumArgs();
443 if ((signed) numArgs <= ArgSuffix.first)
444 return;
445
446 const StringLiteral *strArg =
447 dyn_cast<StringLiteral>(CE->getArg((unsigned)ArgSuffix.first)
448 ->IgnoreParenImpCasts());
449
450 // Currently we only handle string literals. It is possible to do better,
451 // either by looking at references to const variables, or by doing real
452 // flow analysis.
453 if (!strArg || strArg->getCharByteWidth() != 1)
454 return;
455
456 // Count the number of X's, taking into account a possible cutoff suffix.
457 StringRef str = strArg->getString();
458 unsigned numX = 0;
459 unsigned n = str.size();
460
461 // Take into account the suffix.
462 unsigned suffix = 0;
463 if (ArgSuffix.second >= 0) {
464 const Expr *suffixEx = CE->getArg((unsigned)ArgSuffix.second);
465 llvm::APSInt Result;
466 if (!suffixEx->EvaluateAsInt(Result, BR.getContext()))
467 return;
468 // FIXME: Issue a warning.
469 if (Result.isNegative())
470 return;
471 suffix = (unsigned) Result.getZExtValue();
472 n = (n > suffix) ? n - suffix : 0;
473 }
474
475 for (unsigned i = 0; i < n; ++i)
476 if (str[i] == 'X') ++numX;
477
478 if (numX >= 6)
479 return;
480
481 // Issue a warning.
482 SourceRange R = strArg->getSourceRange();
483 PathDiagnosticLocation CELoc =
484 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000485 SmallString<512> buf;
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000486 llvm::raw_svector_ostream out(buf);
487 out << "Call to '" << Name << "' should have at least 6 'X's in the"
488 " format string to be secure (" << numX << " 'X'";
489 if (numX != 1)
490 out << 's';
491 out << " seen";
492 if (suffix) {
493 out << ", " << suffix << " character";
494 if (suffix > 1)
495 out << 's';
496 out << " used as a suffix";
497 }
498 out << ')';
Ted Kremenek07189522012-04-04 18:11:35 +0000499 BR.EmitBasicReport(AC->getDecl(),
500 "Insecure temporary file creation", "Security",
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000501 out.str(), CELoc, &R, 1);
502}
503
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000504//===----------------------------------------------------------------------===//
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000505// Check: Any use of 'strcpy' is insecure.
506//
507// CWE-119: Improper Restriction of Operations within
508// the Bounds of a Memory Buffer
509//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000510void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000511 if (!filter.check_strcpy)
512 return;
513
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000514 if (!checkCall_strCommon(CE, FD))
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000515 return;
516
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000517 // Issue a warning.
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000518 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000519 PathDiagnosticLocation CELoc =
520 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000521 BR.EmitBasicReport(AC->getDecl(),
522 "Potential insecure memory buffer bounds restriction in "
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000523 "call 'strcpy'",
524 "Security",
525 "Call to function 'strcpy' is insecure as it does not "
Ted Kremenek07189522012-04-04 18:11:35 +0000526 "provide bounding of the memory buffer. Replace "
527 "unbounded copy functions with analogous functions that "
528 "support length arguments such as 'strlcpy'. CWE-119.",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000529 CELoc, &R, 1);
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000530}
531
532//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000533// Check: Any use of 'strcat' is insecure.
534//
535// CWE-119: Improper Restriction of Operations within
536// the Bounds of a Memory Buffer
537//===----------------------------------------------------------------------===//
538void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000539 if (!filter.check_strcpy)
540 return;
541
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000542 if (!checkCall_strCommon(CE, FD))
543 return;
544
545 // Issue a warning.
546 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000547 PathDiagnosticLocation CELoc =
548 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000549 BR.EmitBasicReport(AC->getDecl(),
550 "Potential insecure memory buffer bounds restriction in "
551 "call 'strcat'",
552 "Security",
553 "Call to function 'strcat' is insecure as it does not "
554 "provide bounding of the memory buffer. Replace "
555 "unbounded copy functions with analogous functions that "
556 "support length arguments such as 'strlcat'. CWE-119.",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000557 CELoc, &R, 1);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000558}
559
560//===----------------------------------------------------------------------===//
561// Common check for str* functions with no bounds parameters.
562//===----------------------------------------------------------------------===//
563bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
564 const FunctionProtoType *FPT
565 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
566 if (!FPT)
567 return false;
568
569 // Verify the function takes two arguments, three in the _chk version.
570 int numArgs = FPT->getNumArgs();
571 if (numArgs != 2 && numArgs != 3)
572 return false;
573
574 // Verify the type for both arguments.
575 for (int i = 0; i < 2; i++) {
576 // Verify that the arguments are pointers.
577 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
578 if (!PT)
579 return false;
580
581 // Verify that the argument is a 'char*'.
582 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
583 return false;
584 }
585
586 return true;
587}
588
589//===----------------------------------------------------------------------===//
Ted Kremenek24650472009-09-02 02:47:41 +0000590// Check: Linear congruent random number generators should not be used
591// Originally: <rdar://problem/63371000>
592// CWE-338: Use of cryptographically weak prng
593//===----------------------------------------------------------------------===//
594
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000595void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000596 if (!filter.check_rand || !CheckRand)
Ted Kremenek24650472009-09-02 02:47:41 +0000597 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Abramo Bagnara723df242010-12-14 22:11:44 +0000599 const FunctionProtoType *FTP
600 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000601 if (!FTP)
602 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Ted Kremenek24650472009-09-02 02:47:41 +0000604 if (FTP->getNumArgs() == 1) {
605 // Is the argument an 'unsigned short *'?
606 // (Actually any integer type is allowed.)
607 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
608 if (!PT)
609 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Ted Kremenek24650472009-09-02 02:47:41 +0000611 if (! PT->getPointeeType()->isIntegerType())
612 return;
613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614 else if (FTP->getNumArgs() != 0)
Ted Kremenek24650472009-09-02 02:47:41 +0000615 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Ted Kremenek24650472009-09-02 02:47:41 +0000617 // Issue a warning.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000618 SmallString<256> buf1;
Ted Kremenek2c016762010-03-24 22:39:47 +0000619 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000620 os1 << '\'' << *FD << "' is a poor random number generator";
Ted Kremenek24650472009-09-02 02:47:41 +0000621
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000622 SmallString<256> buf2;
Ted Kremenek2c016762010-03-24 22:39:47 +0000623 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000624 os2 << "Function '" << *FD
Ted Kremenek24650472009-09-02 02:47:41 +0000625 << "' is obsolete because it implements a poor random number generator."
626 << " Use 'arc4random' instead";
627
628 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000629 PathDiagnosticLocation CELoc =
630 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000631 BR.EmitBasicReport(AC->getDecl(), os1.str(), "Security", os2.str(),
632 CELoc, &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000633}
634
635//===----------------------------------------------------------------------===//
636// Check: 'random' should not be used
637// Originally: <rdar://problem/63371000>
638//===----------------------------------------------------------------------===//
639
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000640void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000641 if (!CheckRand || !filter.check_rand)
Ted Kremenek24650472009-09-02 02:47:41 +0000642 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Abramo Bagnara723df242010-12-14 22:11:44 +0000644 const FunctionProtoType *FTP
645 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000646 if (!FTP)
647 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremenek24650472009-09-02 02:47:41 +0000649 // Verify that the function takes no argument.
650 if (FTP->getNumArgs() != 0)
651 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenek24650472009-09-02 02:47:41 +0000653 // Issue a warning.
654 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000655 PathDiagnosticLocation CELoc =
656 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000657 BR.EmitBasicReport(AC->getDecl(),
658 "'random' is not a secure random number generator",
Ted Kremenek24650472009-09-02 02:47:41 +0000659 "Security",
660 "The 'random' function produces a sequence of values that "
661 "an adversary may be able to predict. Use 'arc4random' "
Anna Zaks590dd8e2011-09-20 21:38:35 +0000662 "instead", CELoc, &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000663}
664
665//===----------------------------------------------------------------------===//
Anna Zaksa7957ff2011-10-11 04:34:54 +0000666// Check: 'vfork' should not be used.
667// POS33-C: Do not use vfork().
668//===----------------------------------------------------------------------===//
669
670void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000671 if (!filter.check_vfork)
672 return;
673
Anna Zaksa7957ff2011-10-11 04:34:54 +0000674 // All calls to vfork() are insecure, issue a warning.
675 SourceRange R = CE->getCallee()->getSourceRange();
676 PathDiagnosticLocation CELoc =
677 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000678 BR.EmitBasicReport(AC->getDecl(),
679 "Potential insecure implementation-specific behavior in "
Anna Zaksa7957ff2011-10-11 04:34:54 +0000680 "call 'vfork'",
681 "Security",
682 "Call to function 'vfork' is insecure as it can lead to "
683 "denial of service situations in the parent process. "
684 "Replace calls to vfork with calls to the safer "
685 "'posix_spawn' function",
686 CELoc, &R, 1);
687}
688
689//===----------------------------------------------------------------------===//
Ted Kremenek65a81a92009-08-28 00:08:09 +0000690// Check: Should check whether privileges are dropped successfully.
691// Originally: <rdar://problem/6337132>
692//===----------------------------------------------------------------------===//
693
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000694void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000695 if (!filter.check_UncheckedReturn)
696 return;
697
Ted Kremenek65a81a92009-08-28 00:08:09 +0000698 const FunctionDecl *FD = CE->getDirectCallee();
699 if (!FD)
700 return;
701
702 if (II_setid[0] == NULL) {
Ted Kremenek24650472009-09-02 02:47:41 +0000703 static const char * const identifiers[num_setids] = {
Ted Kremenek65a81a92009-08-28 00:08:09 +0000704 "setuid", "setgid", "seteuid", "setegid",
705 "setreuid", "setregid"
706 };
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Ted Kremenek24650472009-09-02 02:47:41 +0000708 for (size_t i = 0; i < num_setids; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000709 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000710 }
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremenek65a81a92009-08-28 00:08:09 +0000712 const IdentifierInfo *id = FD->getIdentifier();
713 size_t identifierid;
714
Ted Kremenek24650472009-09-02 02:47:41 +0000715 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000716 if (id == II_setid[identifierid])
717 break;
718
Ted Kremenek24650472009-09-02 02:47:41 +0000719 if (identifierid >= num_setids)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000720 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Abramo Bagnara723df242010-12-14 22:11:44 +0000722 const FunctionProtoType *FTP
723 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek65a81a92009-08-28 00:08:09 +0000724 if (!FTP)
725 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Ted Kremeneka8187832009-08-28 00:24:55 +0000727 // Verify that the function takes one or two arguments (depending on
728 // the function).
Ted Kremenek65a81a92009-08-28 00:08:09 +0000729 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
730 return;
731
732 // The arguments must be integers.
733 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
734 if (! FTP->getArgType(i)->isIntegerType())
735 return;
736
737 // Issue a warning.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000738 SmallString<256> buf1;
Ted Kremenek2c016762010-03-24 22:39:47 +0000739 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000740 os1 << "Return value is not checked in call to '" << *FD << '\'';
Ted Kremenek65a81a92009-08-28 00:08:09 +0000741
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000742 SmallString<256> buf2;
Ted Kremenek2c016762010-03-24 22:39:47 +0000743 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000744 os2 << "The return value from the call to '" << *FD
745 << "' is not checked. If an error occurs in '" << *FD
Ted Kremenek65a81a92009-08-28 00:08:09 +0000746 << "', the following code may execute with unexpected privileges";
747
748 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000749 PathDiagnosticLocation CELoc =
750 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000751 BR.EmitBasicReport(AC->getDecl(), os1.str(), "Security", os2.str(),
752 CELoc, &R, 1);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000753}
754
755//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000756// SecuritySyntaxChecker
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000757//===----------------------------------------------------------------------===//
758
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000759namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000760class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000761public:
Ted Kremenek76a54242012-01-20 01:44:29 +0000762 ChecksFilter filter;
763
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000764 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
765 BugReporter &BR) const {
Ted Kremenek76a54242012-01-20 01:44:29 +0000766 WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter);
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000767 walker.Visit(D->getBody());
768 }
769};
770}
771
Ted Kremenek76a54242012-01-20 01:44:29 +0000772#define REGISTER_CHECKER(name) \
773void ento::register##name(CheckerManager &mgr) {\
Ted Kremenekb0754172012-06-29 21:01:35 +0000774 mgr.registerChecker<SecuritySyntaxChecker>()->filter.check_##name = true;\
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000775}
Ted Kremenek76a54242012-01-20 01:44:29 +0000776
777REGISTER_CHECKER(gets)
778REGISTER_CHECKER(getpw)
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000779REGISTER_CHECKER(mkstemp)
Ted Kremenek76a54242012-01-20 01:44:29 +0000780REGISTER_CHECKER(mktemp)
781REGISTER_CHECKER(strcpy)
782REGISTER_CHECKER(rand)
783REGISTER_CHECKER(vfork)
784REGISTER_CHECKER(FloatLoopCounter)
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000785REGISTER_CHECKER(UncheckedReturn)
786
Ted Kremenek76a54242012-01-20 01:44:29 +0000787