blob: a9bd4dab2bad0e891e17c3ca37696667753ae59e [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/AST/StmtVisitor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/Analysis/AnalysisContext.h"
Anna Zaks590dd8e2011-09-20 21:38:35 +000017#include "clang/Basic/TargetInfo.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/StaticAnalyzer/Core/Checker.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 ChecksFilter {
40 DefaultBool check_gets;
41 DefaultBool check_getpw;
42 DefaultBool check_mktemp;
Ted Kremenekb63d8d82012-01-20 05:35:06 +000043 DefaultBool check_mkstemp;
Ted Kremenek76a54242012-01-20 01:44:29 +000044 DefaultBool check_strcpy;
45 DefaultBool check_rand;
46 DefaultBool check_vfork;
47 DefaultBool check_FloatLoopCounter;
Ted Kremenekb63d8d82012-01-20 05:35:06 +000048 DefaultBool check_UncheckedReturn;
Ted Kremenek76a54242012-01-20 01:44:29 +000049};
50
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000051class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump1eb44332009-09-09 15:08:12 +000052 BugReporter &BR;
Ted Kremenek1d26f482011-10-24 01:32:45 +000053 AnalysisDeclContext* AC;
Ted Kremenek24650472009-09-02 02:47:41 +000054 enum { num_setids = 6 };
55 IdentifierInfo *II_setid[num_setids];
Ted Kremenek2c016762010-03-24 22:39:47 +000056
Ted Kremenek88c8bc82010-01-15 08:20:31 +000057 const bool CheckRand;
Ted Kremenek76a54242012-01-20 01:44:29 +000058 const ChecksFilter &filter;
Mike Stump1eb44332009-09-09 15:08:12 +000059
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000060public:
Ted Kremenek76a54242012-01-20 01:44:29 +000061 WalkAST(BugReporter &br, AnalysisDeclContext* ac,
62 const ChecksFilter &f)
Anna Zaks590dd8e2011-09-20 21:38:35 +000063 : BR(br), AC(ac), II_setid(),
Ted Kremenek76a54242012-01-20 01:44:29 +000064 CheckRand(isArc4RandomAvailable(BR.getContext())),
65 filter(f) {}
Mike Stump1eb44332009-09-09 15:08:12 +000066
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000067 // Statement visitor methods.
Ted Kremenekefcbb152009-07-23 22:29:41 +000068 void VisitCallExpr(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000069 void VisitForStmt(ForStmt *S);
Ted Kremenek65a81a92009-08-28 00:08:09 +000070 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek8baf86d2009-07-23 21:34:35 +000071 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000072
73 void VisitChildren(Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +000074
Ted Kremenekefcbb152009-07-23 22:29:41 +000075 // Helpers.
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000076 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +000077
Lenny Maioranic2dace12011-04-03 05:07:11 +000078 typedef void (WalkAST::*FnCheck)(const CallExpr *,
79 const FunctionDecl *);
80
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000081 // Checker-specific methods.
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000082 void checkLoopConditionForFloat(const ForStmt *FS);
83 void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
84 void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
85 void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenekb63d8d82012-01-20 05:35:06 +000086 void checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000087 void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
88 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
89 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
90 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
Anna Zaksa7957ff2011-10-11 04:34:54 +000091 void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000092 void checkUncheckedReturnValue(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000093};
94} // end anonymous namespace
95
96//===----------------------------------------------------------------------===//
97// AST walking.
98//===----------------------------------------------------------------------===//
99
100void WalkAST::VisitChildren(Stmt *S) {
101 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
102 if (Stmt *child = *I)
103 Visit(child);
104}
105
Ted Kremenekefcbb152009-07-23 22:29:41 +0000106void WalkAST::VisitCallExpr(CallExpr *CE) {
Lenny Maioranic2dace12011-04-03 05:07:11 +0000107 // Get the callee.
108 const FunctionDecl *FD = CE->getDirectCallee();
109
110 if (!FD)
111 return;
112
113 // Get the name of the callee. If it's a builtin, strip off the prefix.
114 IdentifierInfo *II = FD->getIdentifier();
115 if (!II) // if no identifier, not a simple C function
116 return;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000117 StringRef Name = II->getName();
Lenny Maioranic2dace12011-04-03 05:07:11 +0000118 if (Name.startswith("__builtin_"))
119 Name = Name.substr(10);
120
121 // Set the evaluation function by switching on the callee name.
122 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000123 .Case("gets", &WalkAST::checkCall_gets)
124 .Case("getpw", &WalkAST::checkCall_getpw)
125 .Case("mktemp", &WalkAST::checkCall_mktemp)
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000126 .Case("mkstemp", &WalkAST::checkCall_mkstemp)
127 .Case("mkdtemp", &WalkAST::checkCall_mkstemp)
128 .Case("mkstemps", &WalkAST::checkCall_mkstemp)
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000129 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
130 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
131 .Case("drand48", &WalkAST::checkCall_rand)
132 .Case("erand48", &WalkAST::checkCall_rand)
133 .Case("jrand48", &WalkAST::checkCall_rand)
134 .Case("lrand48", &WalkAST::checkCall_rand)
135 .Case("mrand48", &WalkAST::checkCall_rand)
136 .Case("nrand48", &WalkAST::checkCall_rand)
137 .Case("lcong48", &WalkAST::checkCall_rand)
138 .Case("rand", &WalkAST::checkCall_rand)
139 .Case("rand_r", &WalkAST::checkCall_rand)
140 .Case("random", &WalkAST::checkCall_random)
Anna Zaksa7957ff2011-10-11 04:34:54 +0000141 .Case("vfork", &WalkAST::checkCall_vfork)
Lenny Maioranic2dace12011-04-03 05:07:11 +0000142 .Default(NULL);
143
144 // If the callee isn't defined, it is not of security concern.
145 // Check and evaluate the call.
146 if (evalFunction)
147 (this->*evalFunction)(CE, FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Ted Kremenekefcbb152009-07-23 22:29:41 +0000149 // Recurse and check children.
150 VisitChildren(CE);
151}
152
Ted Kremenek65a81a92009-08-28 00:08:09 +0000153void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
154 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000155 if (Stmt *child = *I) {
156 if (CallExpr *CE = dyn_cast<CallExpr>(child))
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000157 checkUncheckedReturnValue(CE);
Mike Stump1eb44332009-09-09 15:08:12 +0000158 Visit(child);
159 }
Ted Kremenek65a81a92009-08-28 00:08:09 +0000160}
161
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000162void WalkAST::VisitForStmt(ForStmt *FS) {
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000163 checkLoopConditionForFloat(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000164
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000165 // Recurse and check children.
166 VisitChildren(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000167}
168
169//===----------------------------------------------------------------------===//
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000170// Check: floating poing variable used as loop counter.
Ted Kremenek5abeb522009-07-23 21:44:18 +0000171// Originally: <rdar://problem/6336718>
172// Implements: CERT security coding advisory FLP-30.
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000173//===----------------------------------------------------------------------===//
174
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000175static const DeclRefExpr*
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000176getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000177 expr = expr->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000178
179 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000180 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCall2de56d12010-08-25 11:45:40 +0000181 B->getOpcode() == BO_Comma))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000182 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000184 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000185 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000187 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000188 return rhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000190 return NULL;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000191 }
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000193 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
194 const NamedDecl *ND = DR->getDecl();
195 return ND == x || ND == y ? DR : NULL;
196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000198 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
199 return U->isIncrementDecrementOp()
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000200 ? getIncrementedVar(U->getSubExpr(), x, y) : NULL;
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000201
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000202 return NULL;
203}
204
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000205/// CheckLoopConditionForFloat - This check looks for 'for' statements that
206/// use a floating point variable as a loop counter.
207/// CERT: FLP30-C, FLP30-CPP.
208///
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000209void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000210 if (!filter.check_FloatLoopCounter)
211 return;
212
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000213 // Does the loop have a condition?
214 const Expr *condition = FS->getCond();
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000216 if (!condition)
217 return;
218
219 // Does the loop have an increment?
220 const Expr *increment = FS->getInc();
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000222 if (!increment)
223 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000225 // Strip away '()' and casts.
226 condition = condition->IgnoreParenCasts();
227 increment = increment->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000229 // Is the loop condition a comparison?
230 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
231
232 if (!B)
233 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Ted Kremenekcad9f412009-07-24 20:26:31 +0000235 // Is this a comparison?
236 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000237 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000239 // Are we comparing variables?
John McCallf6a16482010-12-04 03:47:34 +0000240 const DeclRefExpr *drLHS =
241 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
242 const DeclRefExpr *drRHS =
243 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremenekcad9f412009-07-24 20:26:31 +0000245 // Does at least one of the variables have a floating point type?
Douglas Gregor0c293ea2010-06-22 23:07:26 +0000246 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
247 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000249 if (!drLHS && !drRHS)
250 return;
251
252 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
253 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000255 if (!vdLHS && !vdRHS)
Mike Stump1eb44332009-09-09 15:08:12 +0000256 return;
257
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000258 // Does either variable appear in increment?
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000259 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000261 if (!drInc)
262 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000264 // Emit the error. First figure out which DeclRefExpr in the condition
265 // referenced the compared variable.
Ted Kremenekd0f3d712012-10-12 22:56:42 +0000266 assert(drInc->getDecl());
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000267 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
268
Chris Lattner5f9e2722011-07-23 10:55:15 +0000269 SmallVector<SourceRange, 2> ranges;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000270 SmallString<256> sbuf;
Ted Kremenek2c016762010-03-24 22:39:47 +0000271 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Daniel Dunbar4087f272010-08-17 22:39:59 +0000273 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000274 << "' with floating point type '" << drCond->getType().getAsString()
275 << "' should not be used as a loop counter";
276
277 ranges.push_back(drCond->getSourceRange());
278 ranges.push_back(drInc->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000280 const char *bugType = "Floating point variable used as loop counter";
Anna Zaks590dd8e2011-09-20 21:38:35 +0000281
282 PathDiagnosticLocation FSLoc =
283 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000284 BR.EmitBasicReport(AC->getDecl(),
285 bugType, "Security", os.str(),
Anna Zaks590dd8e2011-09-20 21:38:35 +0000286 FSLoc, ranges.data(), ranges.size());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000287}
288
289//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +0000290// Check: Any use of 'gets' is insecure.
291// Originally: <rdar://problem/6335715>
292// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuaa30b3b2009-11-09 08:13:04 +0000293// CWE-242: Use of Inherently Dangerous Function
Ted Kremenekefcbb152009-07-23 22:29:41 +0000294//===----------------------------------------------------------------------===//
295
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000296void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000297 if (!filter.check_gets)
298 return;
299
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000300 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Zhongxing Xubd842e32009-11-09 12:19:26 +0000301 if (!FPT)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000302 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Ted Kremenekefcbb152009-07-23 22:29:41 +0000304 // Verify that the function takes a single argument.
Zhongxing Xubd842e32009-11-09 12:19:26 +0000305 if (FPT->getNumArgs() != 1)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000306 return;
307
308 // Is the argument a 'char*'?
Reid Klecknerdbcc7562013-06-24 16:56:16 +0000309 const PointerType *PT = FPT->getArgType(0)->getAs<PointerType>();
Ted Kremenekefcbb152009-07-23 22:29:41 +0000310 if (!PT)
311 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Ted Kremenekefcbb152009-07-23 22:29:41 +0000313 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
314 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Ted Kremenekefcbb152009-07-23 22:29:41 +0000316 // Issue a warning.
317 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000318 PathDiagnosticLocation CELoc =
319 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000320 BR.EmitBasicReport(AC->getDecl(),
321 "Potential buffer overflow in call to 'gets'",
Ted Kremenekefcbb152009-07-23 22:29:41 +0000322 "Security",
323 "Call to function 'gets' is extremely insecure as it can "
324 "always result in a buffer overflow",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000325 CELoc, &R, 1);
Ted Kremenekefcbb152009-07-23 22:29:41 +0000326}
327
328//===----------------------------------------------------------------------===//
Zhongxing Xubd842e32009-11-09 12:19:26 +0000329// Check: Any use of 'getpwd' is insecure.
330// CWE-477: Use of Obsolete Functions
331//===----------------------------------------------------------------------===//
332
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000333void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000334 if (!filter.check_getpw)
335 return;
336
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000337 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Zhongxing Xubd842e32009-11-09 12:19:26 +0000338 if (!FPT)
339 return;
340
341 // Verify that the function takes two arguments.
342 if (FPT->getNumArgs() != 2)
343 return;
344
345 // Verify the first argument type is integer.
Jordan Rosea5796f82013-04-09 02:30:33 +0000346 if (!FPT->getArgType(0)->isIntegralOrUnscopedEnumerationType())
Zhongxing Xubd842e32009-11-09 12:19:26 +0000347 return;
348
349 // Verify the second argument type is char*.
Reid Klecknerdbcc7562013-06-24 16:56:16 +0000350 const PointerType *PT = FPT->getArgType(1)->getAs<PointerType>();
Zhongxing Xubd842e32009-11-09 12:19:26 +0000351 if (!PT)
352 return;
353
354 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
355 return;
356
357 // Issue a warning.
358 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000359 PathDiagnosticLocation CELoc =
360 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000361 BR.EmitBasicReport(AC->getDecl(),
362 "Potential buffer overflow in call to 'getpw'",
Zhongxing Xubd842e32009-11-09 12:19:26 +0000363 "Security",
364 "The getpw() function is dangerous as it may overflow the "
365 "provided buffer. It is obsoleted by getpwuid().",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000366 CELoc, &R, 1);
Zhongxing Xubd842e32009-11-09 12:19:26 +0000367}
368
369//===----------------------------------------------------------------------===//
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000370// Check: Any use of 'mktemp' is insecure. It is obsoleted by mkstemp().
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000371// CWE-377: Insecure Temporary File
372//===----------------------------------------------------------------------===//
373
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000374void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekb0754172012-06-29 21:01:35 +0000375 if (!filter.check_mktemp) {
376 // Fall back to the security check of looking for enough 'X's in the
377 // format string, since that is a less severe warning.
378 checkCall_mkstemp(CE, FD);
379 return;
380 }
381
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000382 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000383 if(!FPT)
384 return;
Ted Kremenek2c016762010-03-24 22:39:47 +0000385
Lenny Maioraniea4411e2011-03-31 21:26:55 +0000386 // Verify that the function takes a single argument.
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000387 if (FPT->getNumArgs() != 1)
388 return;
389
390 // Verify that the argument is Pointer Type.
Reid Klecknerdbcc7562013-06-24 16:56:16 +0000391 const PointerType *PT = FPT->getArgType(0)->getAs<PointerType>();
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000392 if (!PT)
393 return;
394
395 // Verify that the argument is a 'char*'.
396 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
397 return;
Ted Kremenek431a2cb2010-03-24 22:39:45 +0000398
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000399 // Issue a waring.
400 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000401 PathDiagnosticLocation CELoc =
402 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000403 BR.EmitBasicReport(AC->getDecl(),
404 "Potential insecure temporary file in call 'mktemp'",
Eli Friedmana7e68452010-08-22 01:00:03 +0000405 "Security",
406 "Call to function 'mktemp' is insecure as it always "
Ted Kremenek07189522012-04-04 18:11:35 +0000407 "creates or uses insecure temporary file. Use 'mkstemp' "
408 "instead",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000409 CELoc, &R, 1);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000410}
411
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000412
413//===----------------------------------------------------------------------===//
414// Check: Use of 'mkstemp', 'mktemp', 'mkdtemp' should contain at least 6 X's.
415//===----------------------------------------------------------------------===//
416
417void WalkAST::checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD) {
418 if (!filter.check_mkstemp)
419 return;
420
421 StringRef Name = FD->getIdentifier()->getName();
422 std::pair<signed, signed> ArgSuffix =
423 llvm::StringSwitch<std::pair<signed, signed> >(Name)
424 .Case("mktemp", std::make_pair(0,-1))
425 .Case("mkstemp", std::make_pair(0,-1))
426 .Case("mkdtemp", std::make_pair(0,-1))
427 .Case("mkstemps", std::make_pair(0,1))
428 .Default(std::make_pair(-1, -1));
429
430 assert(ArgSuffix.first >= 0 && "Unsupported function");
431
432 // Check if the number of arguments is consistent with out expectations.
433 unsigned numArgs = CE->getNumArgs();
434 if ((signed) numArgs <= ArgSuffix.first)
435 return;
436
437 const StringLiteral *strArg =
438 dyn_cast<StringLiteral>(CE->getArg((unsigned)ArgSuffix.first)
439 ->IgnoreParenImpCasts());
440
441 // Currently we only handle string literals. It is possible to do better,
442 // either by looking at references to const variables, or by doing real
443 // flow analysis.
444 if (!strArg || strArg->getCharByteWidth() != 1)
445 return;
446
447 // Count the number of X's, taking into account a possible cutoff suffix.
448 StringRef str = strArg->getString();
449 unsigned numX = 0;
450 unsigned n = str.size();
451
452 // Take into account the suffix.
453 unsigned suffix = 0;
454 if (ArgSuffix.second >= 0) {
455 const Expr *suffixEx = CE->getArg((unsigned)ArgSuffix.second);
456 llvm::APSInt Result;
457 if (!suffixEx->EvaluateAsInt(Result, BR.getContext()))
458 return;
459 // FIXME: Issue a warning.
460 if (Result.isNegative())
461 return;
462 suffix = (unsigned) Result.getZExtValue();
463 n = (n > suffix) ? n - suffix : 0;
464 }
465
466 for (unsigned i = 0; i < n; ++i)
467 if (str[i] == 'X') ++numX;
468
469 if (numX >= 6)
470 return;
471
472 // Issue a warning.
473 SourceRange R = strArg->getSourceRange();
474 PathDiagnosticLocation CELoc =
475 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000476 SmallString<512> buf;
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000477 llvm::raw_svector_ostream out(buf);
478 out << "Call to '" << Name << "' should have at least 6 'X's in the"
479 " format string to be secure (" << numX << " 'X'";
480 if (numX != 1)
481 out << 's';
482 out << " seen";
483 if (suffix) {
484 out << ", " << suffix << " character";
485 if (suffix > 1)
486 out << 's';
487 out << " used as a suffix";
488 }
489 out << ')';
Ted Kremenek07189522012-04-04 18:11:35 +0000490 BR.EmitBasicReport(AC->getDecl(),
491 "Insecure temporary file creation", "Security",
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000492 out.str(), CELoc, &R, 1);
493}
494
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000495//===----------------------------------------------------------------------===//
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000496// Check: Any use of 'strcpy' is insecure.
497//
498// CWE-119: Improper Restriction of Operations within
499// the Bounds of a Memory Buffer
500//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000501void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000502 if (!filter.check_strcpy)
503 return;
504
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000505 if (!checkCall_strCommon(CE, FD))
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000506 return;
507
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000508 // Issue a warning.
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000509 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000510 PathDiagnosticLocation CELoc =
511 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000512 BR.EmitBasicReport(AC->getDecl(),
513 "Potential insecure memory buffer bounds restriction in "
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000514 "call 'strcpy'",
515 "Security",
516 "Call to function 'strcpy' is insecure as it does not "
Ted Kremenek07189522012-04-04 18:11:35 +0000517 "provide bounding of the memory buffer. Replace "
518 "unbounded copy functions with analogous functions that "
519 "support length arguments such as 'strlcpy'. CWE-119.",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000520 CELoc, &R, 1);
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000521}
522
523//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000524// Check: Any use of 'strcat' is insecure.
525//
526// CWE-119: Improper Restriction of Operations within
527// the Bounds of a Memory Buffer
528//===----------------------------------------------------------------------===//
529void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000530 if (!filter.check_strcpy)
531 return;
532
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000533 if (!checkCall_strCommon(CE, FD))
534 return;
535
536 // Issue a warning.
537 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000538 PathDiagnosticLocation CELoc =
539 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000540 BR.EmitBasicReport(AC->getDecl(),
541 "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 "
547 "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) {
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000555 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000556 if (!FPT)
557 return false;
558
559 // Verify the function takes two arguments, three in the _chk version.
560 int numArgs = FPT->getNumArgs();
561 if (numArgs != 2 && numArgs != 3)
562 return false;
563
564 // Verify the type for both arguments.
565 for (int i = 0; i < 2; i++) {
566 // Verify that the arguments are pointers.
Reid Klecknerdbcc7562013-06-24 16:56:16 +0000567 const PointerType *PT = FPT->getArgType(i)->getAs<PointerType>();
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000568 if (!PT)
569 return false;
570
571 // Verify that the argument is a 'char*'.
572 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
573 return false;
574 }
575
576 return true;
577}
578
579//===----------------------------------------------------------------------===//
Ted Kremenek24650472009-09-02 02:47:41 +0000580// Check: Linear congruent random number generators should not be used
581// Originally: <rdar://problem/63371000>
582// CWE-338: Use of cryptographically weak prng
583//===----------------------------------------------------------------------===//
584
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000585void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000586 if (!filter.check_rand || !CheckRand)
Ted Kremenek24650472009-09-02 02:47:41 +0000587 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000589 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenek24650472009-09-02 02:47:41 +0000590 if (!FTP)
591 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Ted Kremenek24650472009-09-02 02:47:41 +0000593 if (FTP->getNumArgs() == 1) {
594 // Is the argument an 'unsigned short *'?
595 // (Actually any integer type is allowed.)
Reid Klecknerdbcc7562013-06-24 16:56:16 +0000596 const PointerType *PT = FTP->getArgType(0)->getAs<PointerType>();
Ted Kremenek24650472009-09-02 02:47:41 +0000597 if (!PT)
598 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Jordan Rosea5796f82013-04-09 02:30:33 +0000600 if (! PT->getPointeeType()->isIntegralOrUnscopedEnumerationType())
Ted Kremenek24650472009-09-02 02:47:41 +0000601 return;
602 }
Mike Stump1eb44332009-09-09 15:08:12 +0000603 else if (FTP->getNumArgs() != 0)
Ted Kremenek24650472009-09-02 02:47:41 +0000604 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Ted Kremenek24650472009-09-02 02:47:41 +0000606 // Issue a warning.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000607 SmallString<256> buf1;
Ted Kremenek2c016762010-03-24 22:39:47 +0000608 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000609 os1 << '\'' << *FD << "' is a poor random number generator";
Ted Kremenek24650472009-09-02 02:47:41 +0000610
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000611 SmallString<256> buf2;
Ted Kremenek2c016762010-03-24 22:39:47 +0000612 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000613 os2 << "Function '" << *FD
Ted Kremenek24650472009-09-02 02:47:41 +0000614 << "' is obsolete because it implements a poor random number generator."
615 << " Use 'arc4random' instead";
616
617 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000618 PathDiagnosticLocation CELoc =
619 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000620 BR.EmitBasicReport(AC->getDecl(), os1.str(), "Security", os2.str(),
621 CELoc, &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000622}
623
624//===----------------------------------------------------------------------===//
625// Check: 'random' should not be used
626// Originally: <rdar://problem/63371000>
627//===----------------------------------------------------------------------===//
628
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000629void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000630 if (!CheckRand || !filter.check_rand)
Ted Kremenek24650472009-09-02 02:47:41 +0000631 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000633 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenek24650472009-09-02 02:47:41 +0000634 if (!FTP)
635 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Ted Kremenek24650472009-09-02 02:47:41 +0000637 // Verify that the function takes no argument.
638 if (FTP->getNumArgs() != 0)
639 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Ted Kremenek24650472009-09-02 02:47:41 +0000641 // Issue a warning.
642 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000643 PathDiagnosticLocation CELoc =
644 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000645 BR.EmitBasicReport(AC->getDecl(),
646 "'random' is not a secure random number generator",
Ted Kremenek24650472009-09-02 02:47:41 +0000647 "Security",
648 "The 'random' function produces a sequence of values that "
649 "an adversary may be able to predict. Use 'arc4random' "
Anna Zaks590dd8e2011-09-20 21:38:35 +0000650 "instead", CELoc, &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000651}
652
653//===----------------------------------------------------------------------===//
Anna Zaksa7957ff2011-10-11 04:34:54 +0000654// Check: 'vfork' should not be used.
655// POS33-C: Do not use vfork().
656//===----------------------------------------------------------------------===//
657
658void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000659 if (!filter.check_vfork)
660 return;
661
Anna Zaksa7957ff2011-10-11 04:34:54 +0000662 // All calls to vfork() are insecure, issue a warning.
663 SourceRange R = CE->getCallee()->getSourceRange();
664 PathDiagnosticLocation CELoc =
665 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000666 BR.EmitBasicReport(AC->getDecl(),
667 "Potential insecure implementation-specific behavior in "
Anna Zaksa7957ff2011-10-11 04:34:54 +0000668 "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
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000710 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenek65a81a92009-08-28 00:08:09 +0000711 if (!FTP)
712 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Ted Kremeneka8187832009-08-28 00:24:55 +0000714 // Verify that the function takes one or two arguments (depending on
715 // the function).
Ted Kremenek65a81a92009-08-28 00:08:09 +0000716 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
717 return;
718
719 // The arguments must be integers.
720 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
Jordan Rosea5796f82013-04-09 02:30:33 +0000721 if (! FTP->getArgType(i)->isIntegralOrUnscopedEnumerationType())
Ted Kremenek65a81a92009-08-28 00:08:09 +0000722 return;
723
724 // Issue a warning.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000725 SmallString<256> buf1;
Ted Kremenek2c016762010-03-24 22:39:47 +0000726 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000727 os1 << "Return value is not checked in call to '" << *FD << '\'';
Ted Kremenek65a81a92009-08-28 00:08:09 +0000728
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000729 SmallString<256> buf2;
Ted Kremenek2c016762010-03-24 22:39:47 +0000730 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000731 os2 << "The return value from the call to '" << *FD
732 << "' is not checked. If an error occurs in '" << *FD
Ted Kremenek65a81a92009-08-28 00:08:09 +0000733 << "', the following code may execute with unexpected privileges";
734
735 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000736 PathDiagnosticLocation CELoc =
737 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek07189522012-04-04 18:11:35 +0000738 BR.EmitBasicReport(AC->getDecl(), os1.str(), "Security", os2.str(),
739 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) {\
Ted Kremenekb0754172012-06-29 21:01:35 +0000761 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