blob: 5cd61941841d832d8df53cd190a2d9cc981c4583 [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.
Ted Kremenekd0f3d712012-10-12 22:56:42 +0000273 assert(drInc->getDecl());
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000274 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
275
Chris Lattner5f9e2722011-07-23 10:55:15 +0000276 SmallVector<SourceRange, 2> ranges;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000277 SmallString<256> sbuf;
Ted Kremenek2c016762010-03-24 22:39:47 +0000278 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Daniel Dunbar4087f272010-08-17 22:39:59 +0000280 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000281 << "' with floating point type '" << drCond->getType().getAsString()
282 << "' should not be used as a loop counter";
283
284 ranges.push_back(drCond->getSourceRange());
285 ranges.push_back(drInc->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000287 const char *bugType = "Floating point variable used as loop counter";
Anna Zaks590dd8e2011-09-20 21:38:35 +0000288
289 PathDiagnosticLocation FSLoc =
290 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000291 BR.EmitBasicReport(AC->getDecl(),
292 bugType, "Security", os.str(),
Anna Zaks590dd8e2011-09-20 21:38:35 +0000293 FSLoc, ranges.data(), ranges.size());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000294}
295
296//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +0000297// Check: Any use of 'gets' is insecure.
298// Originally: <rdar://problem/6335715>
299// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuaa30b3b2009-11-09 08:13:04 +0000300// CWE-242: Use of Inherently Dangerous Function
Ted Kremenekefcbb152009-07-23 22:29:41 +0000301//===----------------------------------------------------------------------===//
302
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000303void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000304 if (!filter.check_gets)
305 return;
306
Abramo Bagnara723df242010-12-14 22:11:44 +0000307 const FunctionProtoType *FPT
308 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000309 if (!FPT)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000310 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Ted Kremenekefcbb152009-07-23 22:29:41 +0000312 // Verify that the function takes a single argument.
Zhongxing Xubd842e32009-11-09 12:19:26 +0000313 if (FPT->getNumArgs() != 1)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000314 return;
315
316 // Is the argument a 'char*'?
Zhongxing Xubd842e32009-11-09 12:19:26 +0000317 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenekefcbb152009-07-23 22:29:41 +0000318 if (!PT)
319 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremenekefcbb152009-07-23 22:29:41 +0000321 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
322 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Ted Kremenekefcbb152009-07-23 22:29:41 +0000324 // Issue a warning.
325 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000326 PathDiagnosticLocation CELoc =
327 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000328 BR.EmitBasicReport(AC->getDecl(),
329 "Potential buffer overflow in call to 'gets'",
Ted Kremenekefcbb152009-07-23 22:29:41 +0000330 "Security",
331 "Call to function 'gets' is extremely insecure as it can "
332 "always result in a buffer overflow",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000333 CELoc, &R, 1);
Ted Kremenekefcbb152009-07-23 22:29:41 +0000334}
335
336//===----------------------------------------------------------------------===//
Zhongxing Xubd842e32009-11-09 12:19:26 +0000337// Check: Any use of 'getpwd' is insecure.
338// CWE-477: Use of Obsolete Functions
339//===----------------------------------------------------------------------===//
340
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000341void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000342 if (!filter.check_getpw)
343 return;
344
Abramo Bagnara723df242010-12-14 22:11:44 +0000345 const FunctionProtoType *FPT
346 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000347 if (!FPT)
348 return;
349
350 // Verify that the function takes two arguments.
351 if (FPT->getNumArgs() != 2)
352 return;
353
354 // Verify the first argument type is integer.
355 if (!FPT->getArgType(0)->isIntegerType())
356 return;
357
358 // Verify the second argument type is char*.
359 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
360 if (!PT)
361 return;
362
363 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
364 return;
365
366 // Issue a warning.
367 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000368 PathDiagnosticLocation CELoc =
369 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000370 BR.EmitBasicReport(AC->getDecl(),
371 "Potential buffer overflow in call to 'getpw'",
Zhongxing Xubd842e32009-11-09 12:19:26 +0000372 "Security",
373 "The getpw() function is dangerous as it may overflow the "
374 "provided buffer. It is obsoleted by getpwuid().",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000375 CELoc, &R, 1);
Zhongxing Xubd842e32009-11-09 12:19:26 +0000376}
377
378//===----------------------------------------------------------------------===//
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000379// Check: Any use of 'mktemp' is insecure. It is obsoleted by mkstemp().
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000380// CWE-377: Insecure Temporary File
381//===----------------------------------------------------------------------===//
382
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000383void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekb0754172012-06-29 21:01:35 +0000384 if (!filter.check_mktemp) {
385 // Fall back to the security check of looking for enough 'X's in the
386 // format string, since that is a less severe warning.
387 checkCall_mkstemp(CE, FD);
388 return;
389 }
390
Abramo Bagnara723df242010-12-14 22:11:44 +0000391 const FunctionProtoType *FPT
392 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000393 if(!FPT)
394 return;
Ted Kremenek2c016762010-03-24 22:39:47 +0000395
Lenny Maioraniea4411e2011-03-31 21:26:55 +0000396 // Verify that the function takes a single argument.
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000397 if (FPT->getNumArgs() != 1)
398 return;
399
400 // Verify that the argument is Pointer Type.
401 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
402 if (!PT)
403 return;
404
405 // Verify that the argument is a 'char*'.
406 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
407 return;
Ted Kremenek431a2cb2010-03-24 22:39:45 +0000408
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000409 // Issue a waring.
410 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000411 PathDiagnosticLocation CELoc =
412 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000413 BR.EmitBasicReport(AC->getDecl(),
414 "Potential insecure temporary file in call 'mktemp'",
Eli Friedmana7e68452010-08-22 01:00:03 +0000415 "Security",
416 "Call to function 'mktemp' is insecure as it always "
Ted Kremenek07189522012-04-04 18:11:35 +0000417 "creates or uses insecure temporary file. Use 'mkstemp' "
418 "instead",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000419 CELoc, &R, 1);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000420}
421
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000422
423//===----------------------------------------------------------------------===//
424// Check: Use of 'mkstemp', 'mktemp', 'mkdtemp' should contain at least 6 X's.
425//===----------------------------------------------------------------------===//
426
427void WalkAST::checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD) {
428 if (!filter.check_mkstemp)
429 return;
430
431 StringRef Name = FD->getIdentifier()->getName();
432 std::pair<signed, signed> ArgSuffix =
433 llvm::StringSwitch<std::pair<signed, signed> >(Name)
434 .Case("mktemp", std::make_pair(0,-1))
435 .Case("mkstemp", std::make_pair(0,-1))
436 .Case("mkdtemp", std::make_pair(0,-1))
437 .Case("mkstemps", std::make_pair(0,1))
438 .Default(std::make_pair(-1, -1));
439
440 assert(ArgSuffix.first >= 0 && "Unsupported function");
441
442 // Check if the number of arguments is consistent with out expectations.
443 unsigned numArgs = CE->getNumArgs();
444 if ((signed) numArgs <= ArgSuffix.first)
445 return;
446
447 const StringLiteral *strArg =
448 dyn_cast<StringLiteral>(CE->getArg((unsigned)ArgSuffix.first)
449 ->IgnoreParenImpCasts());
450
451 // Currently we only handle string literals. It is possible to do better,
452 // either by looking at references to const variables, or by doing real
453 // flow analysis.
454 if (!strArg || strArg->getCharByteWidth() != 1)
455 return;
456
457 // Count the number of X's, taking into account a possible cutoff suffix.
458 StringRef str = strArg->getString();
459 unsigned numX = 0;
460 unsigned n = str.size();
461
462 // Take into account the suffix.
463 unsigned suffix = 0;
464 if (ArgSuffix.second >= 0) {
465 const Expr *suffixEx = CE->getArg((unsigned)ArgSuffix.second);
466 llvm::APSInt Result;
467 if (!suffixEx->EvaluateAsInt(Result, BR.getContext()))
468 return;
469 // FIXME: Issue a warning.
470 if (Result.isNegative())
471 return;
472 suffix = (unsigned) Result.getZExtValue();
473 n = (n > suffix) ? n - suffix : 0;
474 }
475
476 for (unsigned i = 0; i < n; ++i)
477 if (str[i] == 'X') ++numX;
478
479 if (numX >= 6)
480 return;
481
482 // Issue a warning.
483 SourceRange R = strArg->getSourceRange();
484 PathDiagnosticLocation CELoc =
485 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000486 SmallString<512> buf;
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000487 llvm::raw_svector_ostream out(buf);
488 out << "Call to '" << Name << "' should have at least 6 'X's in the"
489 " format string to be secure (" << numX << " 'X'";
490 if (numX != 1)
491 out << 's';
492 out << " seen";
493 if (suffix) {
494 out << ", " << suffix << " character";
495 if (suffix > 1)
496 out << 's';
497 out << " used as a suffix";
498 }
499 out << ')';
Ted Kremenek07189522012-04-04 18:11:35 +0000500 BR.EmitBasicReport(AC->getDecl(),
501 "Insecure temporary file creation", "Security",
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000502 out.str(), CELoc, &R, 1);
503}
504
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000505//===----------------------------------------------------------------------===//
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000506// Check: Any use of 'strcpy' is insecure.
507//
508// CWE-119: Improper Restriction of Operations within
509// the Bounds of a Memory Buffer
510//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000511void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000512 if (!filter.check_strcpy)
513 return;
514
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000515 if (!checkCall_strCommon(CE, FD))
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000516 return;
517
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000518 // Issue a warning.
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000519 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000520 PathDiagnosticLocation CELoc =
521 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000522 BR.EmitBasicReport(AC->getDecl(),
523 "Potential insecure memory buffer bounds restriction in "
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000524 "call 'strcpy'",
525 "Security",
526 "Call to function 'strcpy' is insecure as it does not "
Ted Kremenek07189522012-04-04 18:11:35 +0000527 "provide bounding of the memory buffer. Replace "
528 "unbounded copy functions with analogous functions that "
529 "support length arguments such as 'strlcpy'. CWE-119.",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000530 CELoc, &R, 1);
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000531}
532
533//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000534// Check: Any use of 'strcat' is insecure.
535//
536// CWE-119: Improper Restriction of Operations within
537// the Bounds of a Memory Buffer
538//===----------------------------------------------------------------------===//
539void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000540 if (!filter.check_strcpy)
541 return;
542
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000543 if (!checkCall_strCommon(CE, FD))
544 return;
545
546 // Issue a warning.
547 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000548 PathDiagnosticLocation CELoc =
549 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000550 BR.EmitBasicReport(AC->getDecl(),
551 "Potential insecure memory buffer bounds restriction in "
552 "call 'strcat'",
553 "Security",
554 "Call to function 'strcat' is insecure as it does not "
555 "provide bounding of the memory buffer. Replace "
556 "unbounded copy functions with analogous functions that "
557 "support length arguments such as 'strlcat'. CWE-119.",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000558 CELoc, &R, 1);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000559}
560
561//===----------------------------------------------------------------------===//
562// Common check for str* functions with no bounds parameters.
563//===----------------------------------------------------------------------===//
564bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
565 const FunctionProtoType *FPT
566 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
567 if (!FPT)
568 return false;
569
570 // Verify the function takes two arguments, three in the _chk version.
571 int numArgs = FPT->getNumArgs();
572 if (numArgs != 2 && numArgs != 3)
573 return false;
574
575 // Verify the type for both arguments.
576 for (int i = 0; i < 2; i++) {
577 // Verify that the arguments are pointers.
578 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
579 if (!PT)
580 return false;
581
582 // Verify that the argument is a 'char*'.
583 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
584 return false;
585 }
586
587 return true;
588}
589
590//===----------------------------------------------------------------------===//
Ted Kremenek24650472009-09-02 02:47:41 +0000591// Check: Linear congruent random number generators should not be used
592// Originally: <rdar://problem/63371000>
593// CWE-338: Use of cryptographically weak prng
594//===----------------------------------------------------------------------===//
595
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000596void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000597 if (!filter.check_rand || !CheckRand)
Ted Kremenek24650472009-09-02 02:47:41 +0000598 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Abramo Bagnara723df242010-12-14 22:11:44 +0000600 const FunctionProtoType *FTP
601 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000602 if (!FTP)
603 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Ted Kremenek24650472009-09-02 02:47:41 +0000605 if (FTP->getNumArgs() == 1) {
606 // Is the argument an 'unsigned short *'?
607 // (Actually any integer type is allowed.)
608 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
609 if (!PT)
610 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Ted Kremenek24650472009-09-02 02:47:41 +0000612 if (! PT->getPointeeType()->isIntegerType())
613 return;
614 }
Mike Stump1eb44332009-09-09 15:08:12 +0000615 else if (FTP->getNumArgs() != 0)
Ted Kremenek24650472009-09-02 02:47:41 +0000616 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Ted Kremenek24650472009-09-02 02:47:41 +0000618 // Issue a warning.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000619 SmallString<256> buf1;
Ted Kremenek2c016762010-03-24 22:39:47 +0000620 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000621 os1 << '\'' << *FD << "' is a poor random number generator";
Ted Kremenek24650472009-09-02 02:47:41 +0000622
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000623 SmallString<256> buf2;
Ted Kremenek2c016762010-03-24 22:39:47 +0000624 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000625 os2 << "Function '" << *FD
Ted Kremenek24650472009-09-02 02:47:41 +0000626 << "' is obsolete because it implements a poor random number generator."
627 << " Use 'arc4random' instead";
628
629 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000630 PathDiagnosticLocation CELoc =
631 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000632 BR.EmitBasicReport(AC->getDecl(), os1.str(), "Security", os2.str(),
633 CELoc, &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000634}
635
636//===----------------------------------------------------------------------===//
637// Check: 'random' should not be used
638// Originally: <rdar://problem/63371000>
639//===----------------------------------------------------------------------===//
640
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000641void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000642 if (!CheckRand || !filter.check_rand)
Ted Kremenek24650472009-09-02 02:47:41 +0000643 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Abramo Bagnara723df242010-12-14 22:11:44 +0000645 const FunctionProtoType *FTP
646 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000647 if (!FTP)
648 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Ted Kremenek24650472009-09-02 02:47:41 +0000650 // Verify that the function takes no argument.
651 if (FTP->getNumArgs() != 0)
652 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Ted Kremenek24650472009-09-02 02:47:41 +0000654 // Issue a warning.
655 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000656 PathDiagnosticLocation CELoc =
657 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000658 BR.EmitBasicReport(AC->getDecl(),
659 "'random' is not a secure random number generator",
Ted Kremenek24650472009-09-02 02:47:41 +0000660 "Security",
661 "The 'random' function produces a sequence of values that "
662 "an adversary may be able to predict. Use 'arc4random' "
Anna Zaks590dd8e2011-09-20 21:38:35 +0000663 "instead", CELoc, &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000664}
665
666//===----------------------------------------------------------------------===//
Anna Zaksa7957ff2011-10-11 04:34:54 +0000667// Check: 'vfork' should not be used.
668// POS33-C: Do not use vfork().
669//===----------------------------------------------------------------------===//
670
671void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000672 if (!filter.check_vfork)
673 return;
674
Anna Zaksa7957ff2011-10-11 04:34:54 +0000675 // All calls to vfork() are insecure, issue a warning.
676 SourceRange R = CE->getCallee()->getSourceRange();
677 PathDiagnosticLocation CELoc =
678 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000679 BR.EmitBasicReport(AC->getDecl(),
680 "Potential insecure implementation-specific behavior in "
Anna Zaksa7957ff2011-10-11 04:34:54 +0000681 "call 'vfork'",
682 "Security",
683 "Call to function 'vfork' is insecure as it can lead to "
684 "denial of service situations in the parent process. "
685 "Replace calls to vfork with calls to the safer "
686 "'posix_spawn' function",
687 CELoc, &R, 1);
688}
689
690//===----------------------------------------------------------------------===//
Ted Kremenek65a81a92009-08-28 00:08:09 +0000691// Check: Should check whether privileges are dropped successfully.
692// Originally: <rdar://problem/6337132>
693//===----------------------------------------------------------------------===//
694
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000695void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000696 if (!filter.check_UncheckedReturn)
697 return;
698
Ted Kremenek65a81a92009-08-28 00:08:09 +0000699 const FunctionDecl *FD = CE->getDirectCallee();
700 if (!FD)
701 return;
702
703 if (II_setid[0] == NULL) {
Ted Kremenek24650472009-09-02 02:47:41 +0000704 static const char * const identifiers[num_setids] = {
Ted Kremenek65a81a92009-08-28 00:08:09 +0000705 "setuid", "setgid", "seteuid", "setegid",
706 "setreuid", "setregid"
707 };
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Ted Kremenek24650472009-09-02 02:47:41 +0000709 for (size_t i = 0; i < num_setids; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000710 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Ted Kremenek65a81a92009-08-28 00:08:09 +0000713 const IdentifierInfo *id = FD->getIdentifier();
714 size_t identifierid;
715
Ted Kremenek24650472009-09-02 02:47:41 +0000716 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000717 if (id == II_setid[identifierid])
718 break;
719
Ted Kremenek24650472009-09-02 02:47:41 +0000720 if (identifierid >= num_setids)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000721 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Abramo Bagnara723df242010-12-14 22:11:44 +0000723 const FunctionProtoType *FTP
724 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek65a81a92009-08-28 00:08:09 +0000725 if (!FTP)
726 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Ted Kremeneka8187832009-08-28 00:24:55 +0000728 // Verify that the function takes one or two arguments (depending on
729 // the function).
Ted Kremenek65a81a92009-08-28 00:08:09 +0000730 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
731 return;
732
733 // The arguments must be integers.
734 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
735 if (! FTP->getArgType(i)->isIntegerType())
736 return;
737
738 // Issue a warning.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000739 SmallString<256> buf1;
Ted Kremenek2c016762010-03-24 22:39:47 +0000740 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000741 os1 << "Return value is not checked in call to '" << *FD << '\'';
Ted Kremenek65a81a92009-08-28 00:08:09 +0000742
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000743 SmallString<256> buf2;
Ted Kremenek2c016762010-03-24 22:39:47 +0000744 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000745 os2 << "The return value from the call to '" << *FD
746 << "' is not checked. If an error occurs in '" << *FD
Ted Kremenek65a81a92009-08-28 00:08:09 +0000747 << "', the following code may execute with unexpected privileges";
748
749 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000750 PathDiagnosticLocation CELoc =
751 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000752 BR.EmitBasicReport(AC->getDecl(), os1.str(), "Security", os2.str(),
753 CELoc, &R, 1);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000754}
755
756//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000757// SecuritySyntaxChecker
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000758//===----------------------------------------------------------------------===//
759
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000760namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000761class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000762public:
Ted Kremenek76a54242012-01-20 01:44:29 +0000763 ChecksFilter filter;
764
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000765 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
766 BugReporter &BR) const {
Ted Kremenek76a54242012-01-20 01:44:29 +0000767 WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter);
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000768 walker.Visit(D->getBody());
769 }
770};
771}
772
Ted Kremenek76a54242012-01-20 01:44:29 +0000773#define REGISTER_CHECKER(name) \
774void ento::register##name(CheckerManager &mgr) {\
Ted Kremenekb0754172012-06-29 21:01:35 +0000775 mgr.registerChecker<SecuritySyntaxChecker>()->filter.check_##name = true;\
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000776}
Ted Kremenek76a54242012-01-20 01:44:29 +0000777
778REGISTER_CHECKER(gets)
779REGISTER_CHECKER(getpw)
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000780REGISTER_CHECKER(mkstemp)
Ted Kremenek76a54242012-01-20 01:44:29 +0000781REGISTER_CHECKER(mktemp)
782REGISTER_CHECKER(strcpy)
783REGISTER_CHECKER(rand)
784REGISTER_CHECKER(vfork)
785REGISTER_CHECKER(FloatLoopCounter)
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000786REGISTER_CHECKER(UncheckedReturn)
787
Ted Kremenek76a54242012-01-20 01:44:29 +0000788