blob: 57063643fc3a24b74443ad8c09a8a601ffc3cc99 [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;
Stephen Hines651f13c2014-04-23 16:59:28 -070049
50 CheckName checkName_gets;
51 CheckName checkName_getpw;
52 CheckName checkName_mktemp;
53 CheckName checkName_mkstemp;
54 CheckName checkName_strcpy;
55 CheckName checkName_rand;
56 CheckName checkName_vfork;
57 CheckName checkName_FloatLoopCounter;
58 CheckName checkName_UncheckedReturn;
Ted Kremenek76a54242012-01-20 01:44:29 +000059};
Stephen Hines651f13c2014-04-23 16:59:28 -070060
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000061class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump1eb44332009-09-09 15:08:12 +000062 BugReporter &BR;
Ted Kremenek1d26f482011-10-24 01:32:45 +000063 AnalysisDeclContext* AC;
Ted Kremenek24650472009-09-02 02:47:41 +000064 enum { num_setids = 6 };
65 IdentifierInfo *II_setid[num_setids];
Ted Kremenek2c016762010-03-24 22:39:47 +000066
Ted Kremenek88c8bc82010-01-15 08:20:31 +000067 const bool CheckRand;
Ted Kremenek76a54242012-01-20 01:44:29 +000068 const ChecksFilter &filter;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000070public:
Ted Kremenek76a54242012-01-20 01:44:29 +000071 WalkAST(BugReporter &br, AnalysisDeclContext* ac,
72 const ChecksFilter &f)
Anna Zaks590dd8e2011-09-20 21:38:35 +000073 : BR(br), AC(ac), II_setid(),
Ted Kremenek76a54242012-01-20 01:44:29 +000074 CheckRand(isArc4RandomAvailable(BR.getContext())),
75 filter(f) {}
Mike Stump1eb44332009-09-09 15:08:12 +000076
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000077 // Statement visitor methods.
Ted Kremenekefcbb152009-07-23 22:29:41 +000078 void VisitCallExpr(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000079 void VisitForStmt(ForStmt *S);
Ted Kremenek65a81a92009-08-28 00:08:09 +000080 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek8baf86d2009-07-23 21:34:35 +000081 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000082
83 void VisitChildren(Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +000084
Ted Kremenekefcbb152009-07-23 22:29:41 +000085 // Helpers.
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000086 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +000087
Lenny Maioranic2dace12011-04-03 05:07:11 +000088 typedef void (WalkAST::*FnCheck)(const CallExpr *,
89 const FunctionDecl *);
90
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000091 // Checker-specific methods.
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000092 void checkLoopConditionForFloat(const ForStmt *FS);
93 void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
94 void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
95 void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenekb63d8d82012-01-20 05:35:06 +000096 void checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000097 void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
98 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
99 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
100 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
Anna Zaksa7957ff2011-10-11 04:34:54 +0000101 void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000102 void checkUncheckedReturnValue(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000103};
104} // end anonymous namespace
105
106//===----------------------------------------------------------------------===//
107// AST walking.
108//===----------------------------------------------------------------------===//
109
110void WalkAST::VisitChildren(Stmt *S) {
111 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
112 if (Stmt *child = *I)
113 Visit(child);
114}
115
Ted Kremenekefcbb152009-07-23 22:29:41 +0000116void WalkAST::VisitCallExpr(CallExpr *CE) {
Lenny Maioranic2dace12011-04-03 05:07:11 +0000117 // Get the callee.
118 const FunctionDecl *FD = CE->getDirectCallee();
119
120 if (!FD)
121 return;
122
123 // Get the name of the callee. If it's a builtin, strip off the prefix.
124 IdentifierInfo *II = FD->getIdentifier();
125 if (!II) // if no identifier, not a simple C function
126 return;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000127 StringRef Name = II->getName();
Lenny Maioranic2dace12011-04-03 05:07:11 +0000128 if (Name.startswith("__builtin_"))
129 Name = Name.substr(10);
130
131 // Set the evaluation function by switching on the callee name.
132 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000133 .Case("gets", &WalkAST::checkCall_gets)
134 .Case("getpw", &WalkAST::checkCall_getpw)
135 .Case("mktemp", &WalkAST::checkCall_mktemp)
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000136 .Case("mkstemp", &WalkAST::checkCall_mkstemp)
137 .Case("mkdtemp", &WalkAST::checkCall_mkstemp)
138 .Case("mkstemps", &WalkAST::checkCall_mkstemp)
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000139 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
140 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
141 .Case("drand48", &WalkAST::checkCall_rand)
142 .Case("erand48", &WalkAST::checkCall_rand)
143 .Case("jrand48", &WalkAST::checkCall_rand)
144 .Case("lrand48", &WalkAST::checkCall_rand)
145 .Case("mrand48", &WalkAST::checkCall_rand)
146 .Case("nrand48", &WalkAST::checkCall_rand)
147 .Case("lcong48", &WalkAST::checkCall_rand)
148 .Case("rand", &WalkAST::checkCall_rand)
149 .Case("rand_r", &WalkAST::checkCall_rand)
150 .Case("random", &WalkAST::checkCall_random)
Anna Zaksa7957ff2011-10-11 04:34:54 +0000151 .Case("vfork", &WalkAST::checkCall_vfork)
Lenny Maioranic2dace12011-04-03 05:07:11 +0000152 .Default(NULL);
153
154 // If the callee isn't defined, it is not of security concern.
155 // Check and evaluate the call.
156 if (evalFunction)
157 (this->*evalFunction)(CE, FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Ted Kremenekefcbb152009-07-23 22:29:41 +0000159 // Recurse and check children.
160 VisitChildren(CE);
161}
162
Ted Kremenek65a81a92009-08-28 00:08:09 +0000163void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
164 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000165 if (Stmt *child = *I) {
166 if (CallExpr *CE = dyn_cast<CallExpr>(child))
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000167 checkUncheckedReturnValue(CE);
Mike Stump1eb44332009-09-09 15:08:12 +0000168 Visit(child);
169 }
Ted Kremenek65a81a92009-08-28 00:08:09 +0000170}
171
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000172void WalkAST::VisitForStmt(ForStmt *FS) {
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000173 checkLoopConditionForFloat(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000174
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000175 // Recurse and check children.
176 VisitChildren(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000177}
178
179//===----------------------------------------------------------------------===//
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000180// Check: floating poing variable used as loop counter.
Ted Kremenek5abeb522009-07-23 21:44:18 +0000181// Originally: <rdar://problem/6336718>
182// Implements: CERT security coding advisory FLP-30.
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000183//===----------------------------------------------------------------------===//
184
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000185static const DeclRefExpr*
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000186getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000187 expr = expr->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000188
189 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000190 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCall2de56d12010-08-25 11:45:40 +0000191 B->getOpcode() == BO_Comma))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000192 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000194 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000195 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000197 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000198 return rhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000200 return NULL;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000201 }
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000203 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
204 const NamedDecl *ND = DR->getDecl();
205 return ND == x || ND == y ? DR : NULL;
206 }
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000208 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
209 return U->isIncrementDecrementOp()
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000210 ? getIncrementedVar(U->getSubExpr(), x, y) : NULL;
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000211
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000212 return NULL;
213}
214
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000215/// CheckLoopConditionForFloat - This check looks for 'for' statements that
216/// use a floating point variable as a loop counter.
217/// CERT: FLP30-C, FLP30-CPP.
218///
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000219void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000220 if (!filter.check_FloatLoopCounter)
221 return;
222
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000223 // Does the loop have a condition?
224 const Expr *condition = FS->getCond();
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000226 if (!condition)
227 return;
228
229 // Does the loop have an increment?
230 const Expr *increment = FS->getInc();
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000232 if (!increment)
233 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000235 // Strip away '()' and casts.
236 condition = condition->IgnoreParenCasts();
237 increment = increment->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000239 // Is the loop condition a comparison?
240 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
241
242 if (!B)
243 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremenekcad9f412009-07-24 20:26:31 +0000245 // Is this a comparison?
246 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000247 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000249 // Are we comparing variables?
John McCallf6a16482010-12-04 03:47:34 +0000250 const DeclRefExpr *drLHS =
251 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
252 const DeclRefExpr *drRHS =
253 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Ted Kremenekcad9f412009-07-24 20:26:31 +0000255 // Does at least one of the variables have a floating point type?
Douglas Gregor0c293ea2010-06-22 23:07:26 +0000256 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
257 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000259 if (!drLHS && !drRHS)
260 return;
261
262 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
263 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000265 if (!vdLHS && !vdRHS)
Mike Stump1eb44332009-09-09 15:08:12 +0000266 return;
267
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000268 // Does either variable appear in increment?
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000269 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000271 if (!drInc)
272 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000274 // Emit the error. First figure out which DeclRefExpr in the condition
275 // referenced the compared variable.
Ted Kremenekd0f3d712012-10-12 22:56:42 +0000276 assert(drInc->getDecl());
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000277 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
278
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 SmallVector<SourceRange, 2> ranges;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000280 SmallString<256> sbuf;
Ted Kremenek2c016762010-03-24 22:39:47 +0000281 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Daniel Dunbar4087f272010-08-17 22:39:59 +0000283 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000284 << "' with floating point type '" << drCond->getType().getAsString()
285 << "' should not be used as a loop counter";
286
287 ranges.push_back(drCond->getSourceRange());
288 ranges.push_back(drInc->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000290 const char *bugType = "Floating point variable used as loop counter";
Anna Zaks590dd8e2011-09-20 21:38:35 +0000291
292 PathDiagnosticLocation FSLoc =
293 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
Stephen Hines651f13c2014-04-23 16:59:28 -0700294 BR.EmitBasicReport(AC->getDecl(), filter.checkName_FloatLoopCounter,
Ted Kremenek07189522012-04-04 18:11:35 +0000295 bugType, "Security", os.str(),
Jordan Rose31b71f32013-10-07 17:16:59 +0000296 FSLoc, ranges);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000297}
298
299//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +0000300// Check: Any use of 'gets' is insecure.
301// Originally: <rdar://problem/6335715>
302// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuaa30b3b2009-11-09 08:13:04 +0000303// CWE-242: Use of Inherently Dangerous Function
Ted Kremenekefcbb152009-07-23 22:29:41 +0000304//===----------------------------------------------------------------------===//
305
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000306void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000307 if (!filter.check_gets)
308 return;
309
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000310 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Zhongxing Xubd842e32009-11-09 12:19:26 +0000311 if (!FPT)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000312 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenekefcbb152009-07-23 22:29:41 +0000314 // Verify that the function takes a single argument.
Stephen Hines651f13c2014-04-23 16:59:28 -0700315 if (FPT->getNumParams() != 1)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000316 return;
317
318 // Is the argument a 'char*'?
Stephen Hines651f13c2014-04-23 16:59:28 -0700319 const PointerType *PT = FPT->getParamType(0)->getAs<PointerType>();
Ted Kremenekefcbb152009-07-23 22:29:41 +0000320 if (!PT)
321 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenekefcbb152009-07-23 22:29:41 +0000323 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
324 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Ted Kremenekefcbb152009-07-23 22:29:41 +0000326 // Issue a warning.
Anna Zaks590dd8e2011-09-20 21:38:35 +0000327 PathDiagnosticLocation CELoc =
328 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Stephen Hines651f13c2014-04-23 16:59:28 -0700329 BR.EmitBasicReport(AC->getDecl(), filter.checkName_gets,
Ted Kremenek07189522012-04-04 18:11:35 +0000330 "Potential buffer overflow in call to 'gets'",
Ted Kremenekefcbb152009-07-23 22:29:41 +0000331 "Security",
332 "Call to function 'gets' is extremely insecure as it can "
333 "always result in a buffer overflow",
Jordan Rose31b71f32013-10-07 17:16:59 +0000334 CELoc, CE->getCallee()->getSourceRange());
Ted Kremenekefcbb152009-07-23 22:29:41 +0000335}
336
337//===----------------------------------------------------------------------===//
Zhongxing Xubd842e32009-11-09 12:19:26 +0000338// Check: Any use of 'getpwd' is insecure.
339// CWE-477: Use of Obsolete Functions
340//===----------------------------------------------------------------------===//
341
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000342void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000343 if (!filter.check_getpw)
344 return;
345
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000346 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Zhongxing Xubd842e32009-11-09 12:19:26 +0000347 if (!FPT)
348 return;
349
350 // Verify that the function takes two arguments.
Stephen Hines651f13c2014-04-23 16:59:28 -0700351 if (FPT->getNumParams() != 2)
Zhongxing Xubd842e32009-11-09 12:19:26 +0000352 return;
353
354 // Verify the first argument type is integer.
Stephen Hines651f13c2014-04-23 16:59:28 -0700355 if (!FPT->getParamType(0)->isIntegralOrUnscopedEnumerationType())
Zhongxing Xubd842e32009-11-09 12:19:26 +0000356 return;
357
358 // Verify the second argument type is char*.
Stephen Hines651f13c2014-04-23 16:59:28 -0700359 const PointerType *PT = FPT->getParamType(1)->getAs<PointerType>();
Zhongxing Xubd842e32009-11-09 12:19:26 +0000360 if (!PT)
361 return;
362
363 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
364 return;
365
366 // Issue a warning.
Anna Zaks590dd8e2011-09-20 21:38:35 +0000367 PathDiagnosticLocation CELoc =
368 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Stephen Hines651f13c2014-04-23 16:59:28 -0700369 BR.EmitBasicReport(AC->getDecl(), filter.checkName_getpw,
Ted Kremenek07189522012-04-04 18:11:35 +0000370 "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().",
Jordan Rose31b71f32013-10-07 17:16:59 +0000374 CELoc, CE->getCallee()->getSourceRange());
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
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000390 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000391 if(!FPT)
392 return;
Ted Kremenek2c016762010-03-24 22:39:47 +0000393
Lenny Maioraniea4411e2011-03-31 21:26:55 +0000394 // Verify that the function takes a single argument.
Stephen Hines651f13c2014-04-23 16:59:28 -0700395 if (FPT->getNumParams() != 1)
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000396 return;
397
398 // Verify that the argument is Pointer Type.
Stephen Hines651f13c2014-04-23 16:59:28 -0700399 const PointerType *PT = FPT->getParamType(0)->getAs<PointerType>();
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000400 if (!PT)
401 return;
402
403 // Verify that the argument is a 'char*'.
404 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
405 return;
Ted Kremenek431a2cb2010-03-24 22:39:45 +0000406
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000407 // Issue a waring.
Anna Zaks590dd8e2011-09-20 21:38:35 +0000408 PathDiagnosticLocation CELoc =
409 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Stephen Hines651f13c2014-04-23 16:59:28 -0700410 BR.EmitBasicReport(AC->getDecl(), filter.checkName_mktemp,
Ted Kremenek07189522012-04-04 18:11:35 +0000411 "Potential insecure temporary file in call 'mktemp'",
Eli Friedmana7e68452010-08-22 01:00:03 +0000412 "Security",
413 "Call to function 'mktemp' is insecure as it always "
Ted Kremenek07189522012-04-04 18:11:35 +0000414 "creates or uses insecure temporary file. Use 'mkstemp' "
415 "instead",
Jordan Rose31b71f32013-10-07 17:16:59 +0000416 CELoc, CE->getCallee()->getSourceRange());
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000417}
418
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000419
420//===----------------------------------------------------------------------===//
421// Check: Use of 'mkstemp', 'mktemp', 'mkdtemp' should contain at least 6 X's.
422//===----------------------------------------------------------------------===//
423
424void WalkAST::checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD) {
425 if (!filter.check_mkstemp)
426 return;
427
428 StringRef Name = FD->getIdentifier()->getName();
429 std::pair<signed, signed> ArgSuffix =
430 llvm::StringSwitch<std::pair<signed, signed> >(Name)
431 .Case("mktemp", std::make_pair(0,-1))
432 .Case("mkstemp", std::make_pair(0,-1))
433 .Case("mkdtemp", std::make_pair(0,-1))
434 .Case("mkstemps", std::make_pair(0,1))
435 .Default(std::make_pair(-1, -1));
436
437 assert(ArgSuffix.first >= 0 && "Unsupported function");
438
439 // Check if the number of arguments is consistent with out expectations.
440 unsigned numArgs = CE->getNumArgs();
441 if ((signed) numArgs <= ArgSuffix.first)
442 return;
443
444 const StringLiteral *strArg =
445 dyn_cast<StringLiteral>(CE->getArg((unsigned)ArgSuffix.first)
446 ->IgnoreParenImpCasts());
447
448 // Currently we only handle string literals. It is possible to do better,
449 // either by looking at references to const variables, or by doing real
450 // flow analysis.
451 if (!strArg || strArg->getCharByteWidth() != 1)
452 return;
453
454 // Count the number of X's, taking into account a possible cutoff suffix.
455 StringRef str = strArg->getString();
456 unsigned numX = 0;
457 unsigned n = str.size();
458
459 // Take into account the suffix.
460 unsigned suffix = 0;
461 if (ArgSuffix.second >= 0) {
462 const Expr *suffixEx = CE->getArg((unsigned)ArgSuffix.second);
463 llvm::APSInt Result;
464 if (!suffixEx->EvaluateAsInt(Result, BR.getContext()))
465 return;
466 // FIXME: Issue a warning.
467 if (Result.isNegative())
468 return;
469 suffix = (unsigned) Result.getZExtValue();
470 n = (n > suffix) ? n - suffix : 0;
471 }
472
473 for (unsigned i = 0; i < n; ++i)
474 if (str[i] == 'X') ++numX;
475
476 if (numX >= 6)
477 return;
478
479 // Issue a warning.
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000480 PathDiagnosticLocation CELoc =
481 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000482 SmallString<512> buf;
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000483 llvm::raw_svector_ostream out(buf);
484 out << "Call to '" << Name << "' should have at least 6 'X's in the"
485 " format string to be secure (" << numX << " 'X'";
486 if (numX != 1)
487 out << 's';
488 out << " seen";
489 if (suffix) {
490 out << ", " << suffix << " character";
491 if (suffix > 1)
492 out << 's';
493 out << " used as a suffix";
494 }
495 out << ')';
Stephen Hines651f13c2014-04-23 16:59:28 -0700496 BR.EmitBasicReport(AC->getDecl(), filter.checkName_mkstemp,
Ted Kremenek07189522012-04-04 18:11:35 +0000497 "Insecure temporary file creation", "Security",
Jordan Rose31b71f32013-10-07 17:16:59 +0000498 out.str(), CELoc, strArg->getSourceRange());
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000499}
500
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000501//===----------------------------------------------------------------------===//
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000502// Check: Any use of 'strcpy' is insecure.
503//
504// CWE-119: Improper Restriction of Operations within
505// the Bounds of a Memory Buffer
506//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000507void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000508 if (!filter.check_strcpy)
509 return;
510
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000511 if (!checkCall_strCommon(CE, FD))
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000512 return;
513
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000514 // Issue a warning.
Anna Zaks590dd8e2011-09-20 21:38:35 +0000515 PathDiagnosticLocation CELoc =
516 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Stephen Hines651f13c2014-04-23 16:59:28 -0700517 BR.EmitBasicReport(AC->getDecl(), filter.checkName_strcpy,
Ted Kremenek07189522012-04-04 18:11:35 +0000518 "Potential insecure memory buffer bounds restriction in "
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000519 "call 'strcpy'",
520 "Security",
521 "Call to function 'strcpy' is insecure as it does not "
Ted Kremenek07189522012-04-04 18:11:35 +0000522 "provide bounding of the memory buffer. Replace "
523 "unbounded copy functions with analogous functions that "
524 "support length arguments such as 'strlcpy'. CWE-119.",
Jordan Rose31b71f32013-10-07 17:16:59 +0000525 CELoc, CE->getCallee()->getSourceRange());
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000526}
527
528//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000529// Check: Any use of 'strcat' is insecure.
530//
531// CWE-119: Improper Restriction of Operations within
532// the Bounds of a Memory Buffer
533//===----------------------------------------------------------------------===//
534void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000535 if (!filter.check_strcpy)
536 return;
537
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000538 if (!checkCall_strCommon(CE, FD))
539 return;
540
541 // Issue a warning.
Anna Zaks590dd8e2011-09-20 21:38:35 +0000542 PathDiagnosticLocation CELoc =
543 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Stephen Hines651f13c2014-04-23 16:59:28 -0700544 BR.EmitBasicReport(AC->getDecl(), filter.checkName_strcpy,
Ted Kremenek07189522012-04-04 18:11:35 +0000545 "Potential insecure memory buffer bounds restriction in "
546 "call 'strcat'",
547 "Security",
548 "Call to function 'strcat' is insecure as it does not "
549 "provide bounding of the memory buffer. Replace "
550 "unbounded copy functions with analogous functions that "
551 "support length arguments such as 'strlcat'. CWE-119.",
Jordan Rose31b71f32013-10-07 17:16:59 +0000552 CELoc, CE->getCallee()->getSourceRange());
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000553}
554
555//===----------------------------------------------------------------------===//
556// Common check for str* functions with no bounds parameters.
557//===----------------------------------------------------------------------===//
558bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000559 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000560 if (!FPT)
561 return false;
562
563 // Verify the function takes two arguments, three in the _chk version.
Stephen Hines651f13c2014-04-23 16:59:28 -0700564 int numArgs = FPT->getNumParams();
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000565 if (numArgs != 2 && numArgs != 3)
566 return false;
567
568 // Verify the type for both arguments.
569 for (int i = 0; i < 2; i++) {
570 // Verify that the arguments are pointers.
Stephen Hines651f13c2014-04-23 16:59:28 -0700571 const PointerType *PT = FPT->getParamType(i)->getAs<PointerType>();
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000572 if (!PT)
573 return false;
574
575 // Verify that the argument is a 'char*'.
576 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
577 return false;
578 }
579
580 return true;
581}
582
583//===----------------------------------------------------------------------===//
Ted Kremenek24650472009-09-02 02:47:41 +0000584// Check: Linear congruent random number generators should not be used
585// Originally: <rdar://problem/63371000>
586// CWE-338: Use of cryptographically weak prng
587//===----------------------------------------------------------------------===//
588
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000589void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000590 if (!filter.check_rand || !CheckRand)
Ted Kremenek24650472009-09-02 02:47:41 +0000591 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000593 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenek24650472009-09-02 02:47:41 +0000594 if (!FTP)
595 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Stephen Hines651f13c2014-04-23 16:59:28 -0700597 if (FTP->getNumParams() == 1) {
Ted Kremenek24650472009-09-02 02:47:41 +0000598 // Is the argument an 'unsigned short *'?
599 // (Actually any integer type is allowed.)
Stephen Hines651f13c2014-04-23 16:59:28 -0700600 const PointerType *PT = FTP->getParamType(0)->getAs<PointerType>();
Ted Kremenek24650472009-09-02 02:47:41 +0000601 if (!PT)
602 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Jordan Rosea5796f82013-04-09 02:30:33 +0000604 if (! PT->getPointeeType()->isIntegralOrUnscopedEnumerationType())
Ted Kremenek24650472009-09-02 02:47:41 +0000605 return;
Stephen Hines651f13c2014-04-23 16:59:28 -0700606 } else if (FTP->getNumParams() != 0)
Ted Kremenek24650472009-09-02 02:47:41 +0000607 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Ted Kremenek24650472009-09-02 02:47:41 +0000609 // Issue a warning.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000610 SmallString<256> buf1;
Ted Kremenek2c016762010-03-24 22:39:47 +0000611 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000612 os1 << '\'' << *FD << "' is a poor random number generator";
Ted Kremenek24650472009-09-02 02:47:41 +0000613
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000614 SmallString<256> buf2;
Ted Kremenek2c016762010-03-24 22:39:47 +0000615 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000616 os2 << "Function '" << *FD
Ted Kremenek24650472009-09-02 02:47:41 +0000617 << "' is obsolete because it implements a poor random number generator."
618 << " Use 'arc4random' instead";
619
Anna Zaks590dd8e2011-09-20 21:38:35 +0000620 PathDiagnosticLocation CELoc =
621 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Stephen Hines651f13c2014-04-23 16:59:28 -0700622 BR.EmitBasicReport(AC->getDecl(), filter.checkName_rand, os1.str(),
623 "Security", os2.str(), CELoc,
624 CE->getCallee()->getSourceRange());
Ted Kremenek24650472009-09-02 02:47:41 +0000625}
626
627//===----------------------------------------------------------------------===//
628// Check: 'random' should not be used
629// Originally: <rdar://problem/63371000>
630//===----------------------------------------------------------------------===//
631
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000632void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000633 if (!CheckRand || !filter.check_rand)
Ted Kremenek24650472009-09-02 02:47:41 +0000634 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000636 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenek24650472009-09-02 02:47:41 +0000637 if (!FTP)
638 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Ted Kremenek24650472009-09-02 02:47:41 +0000640 // Verify that the function takes no argument.
Stephen Hines651f13c2014-04-23 16:59:28 -0700641 if (FTP->getNumParams() != 0)
Ted Kremenek24650472009-09-02 02:47:41 +0000642 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Ted Kremenek24650472009-09-02 02:47:41 +0000644 // Issue a warning.
Anna Zaks590dd8e2011-09-20 21:38:35 +0000645 PathDiagnosticLocation CELoc =
646 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Stephen Hines651f13c2014-04-23 16:59:28 -0700647 BR.EmitBasicReport(AC->getDecl(), filter.checkName_rand,
Ted Kremenek07189522012-04-04 18:11:35 +0000648 "'random' is not a secure random number generator",
Ted Kremenek24650472009-09-02 02:47:41 +0000649 "Security",
650 "The 'random' function produces a sequence of values that "
651 "an adversary may be able to predict. Use 'arc4random' "
Jordan Rose31b71f32013-10-07 17:16:59 +0000652 "instead", CELoc, CE->getCallee()->getSourceRange());
Ted Kremenek24650472009-09-02 02:47:41 +0000653}
654
655//===----------------------------------------------------------------------===//
Anna Zaksa7957ff2011-10-11 04:34:54 +0000656// Check: 'vfork' should not be used.
657// POS33-C: Do not use vfork().
658//===----------------------------------------------------------------------===//
659
660void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000661 if (!filter.check_vfork)
662 return;
663
Anna Zaksa7957ff2011-10-11 04:34:54 +0000664 // All calls to vfork() are insecure, issue a warning.
Anna Zaksa7957ff2011-10-11 04:34:54 +0000665 PathDiagnosticLocation CELoc =
666 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Stephen Hines651f13c2014-04-23 16:59:28 -0700667 BR.EmitBasicReport(AC->getDecl(), filter.checkName_vfork,
Ted Kremenek07189522012-04-04 18:11:35 +0000668 "Potential insecure implementation-specific behavior in "
Anna Zaksa7957ff2011-10-11 04:34:54 +0000669 "call 'vfork'",
670 "Security",
671 "Call to function 'vfork' is insecure as it can lead to "
672 "denial of service situations in the parent process. "
673 "Replace calls to vfork with calls to the safer "
674 "'posix_spawn' function",
Jordan Rose31b71f32013-10-07 17:16:59 +0000675 CELoc, CE->getCallee()->getSourceRange());
Anna Zaksa7957ff2011-10-11 04:34:54 +0000676}
677
678//===----------------------------------------------------------------------===//
Ted Kremenek65a81a92009-08-28 00:08:09 +0000679// Check: Should check whether privileges are dropped successfully.
680// Originally: <rdar://problem/6337132>
681//===----------------------------------------------------------------------===//
682
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000683void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000684 if (!filter.check_UncheckedReturn)
685 return;
686
Ted Kremenek65a81a92009-08-28 00:08:09 +0000687 const FunctionDecl *FD = CE->getDirectCallee();
688 if (!FD)
689 return;
690
691 if (II_setid[0] == NULL) {
Ted Kremenek24650472009-09-02 02:47:41 +0000692 static const char * const identifiers[num_setids] = {
Ted Kremenek65a81a92009-08-28 00:08:09 +0000693 "setuid", "setgid", "seteuid", "setegid",
694 "setreuid", "setregid"
695 };
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Ted Kremenek24650472009-09-02 02:47:41 +0000697 for (size_t i = 0; i < num_setids; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000698 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000699 }
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Ted Kremenek65a81a92009-08-28 00:08:09 +0000701 const IdentifierInfo *id = FD->getIdentifier();
702 size_t identifierid;
703
Ted Kremenek24650472009-09-02 02:47:41 +0000704 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000705 if (id == II_setid[identifierid])
706 break;
707
Ted Kremenek24650472009-09-02 02:47:41 +0000708 if (identifierid >= num_setids)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000709 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Eli Friedmanfa8277c2013-06-24 18:47:11 +0000711 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenek65a81a92009-08-28 00:08:09 +0000712 if (!FTP)
713 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Ted Kremeneka8187832009-08-28 00:24:55 +0000715 // Verify that the function takes one or two arguments (depending on
716 // the function).
Stephen Hines651f13c2014-04-23 16:59:28 -0700717 if (FTP->getNumParams() != (identifierid < 4 ? 1 : 2))
Ted Kremenek65a81a92009-08-28 00:08:09 +0000718 return;
719
720 // The arguments must be integers.
Stephen Hines651f13c2014-04-23 16:59:28 -0700721 for (unsigned i = 0; i < FTP->getNumParams(); i++)
722 if (!FTP->getParamType(i)->isIntegralOrUnscopedEnumerationType())
Ted Kremenek65a81a92009-08-28 00:08:09 +0000723 return;
724
725 // Issue a warning.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000726 SmallString<256> buf1;
Ted Kremenek2c016762010-03-24 22:39:47 +0000727 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000728 os1 << "Return value is not checked in call to '" << *FD << '\'';
Ted Kremenek65a81a92009-08-28 00:08:09 +0000729
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000730 SmallString<256> buf2;
Ted Kremenek2c016762010-03-24 22:39:47 +0000731 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000732 os2 << "The return value from the call to '" << *FD
733 << "' is not checked. If an error occurs in '" << *FD
Ted Kremenek65a81a92009-08-28 00:08:09 +0000734 << "', the following code may execute with unexpected privileges";
735
Anna Zaks590dd8e2011-09-20 21:38:35 +0000736 PathDiagnosticLocation CELoc =
737 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Stephen Hines651f13c2014-04-23 16:59:28 -0700738 BR.EmitBasicReport(AC->getDecl(), filter.checkName_UncheckedReturn, os1.str(),
739 "Security", os2.str(), CELoc,
740 CE->getCallee()->getSourceRange());
Ted Kremenek65a81a92009-08-28 00:08:09 +0000741}
742
743//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000744// SecuritySyntaxChecker
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000745//===----------------------------------------------------------------------===//
746
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000747namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000748class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000749public:
Ted Kremenek76a54242012-01-20 01:44:29 +0000750 ChecksFilter filter;
751
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000752 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
753 BugReporter &BR) const {
Ted Kremenek76a54242012-01-20 01:44:29 +0000754 WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter);
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000755 walker.Visit(D->getBody());
756 }
757};
758}
759
Stephen Hines651f13c2014-04-23 16:59:28 -0700760#define REGISTER_CHECKER(name) \
761 void ento::register##name(CheckerManager &mgr) { \
762 SecuritySyntaxChecker *checker = \
763 mgr.registerChecker<SecuritySyntaxChecker>(); \
764 checker->filter.check_##name = true; \
765 checker->filter.checkName_##name = mgr.getCurrentCheckName(); \
766 }
Ted Kremenek76a54242012-01-20 01:44:29 +0000767
768REGISTER_CHECKER(gets)
769REGISTER_CHECKER(getpw)
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000770REGISTER_CHECKER(mkstemp)
Ted Kremenek76a54242012-01-20 01:44:29 +0000771REGISTER_CHECKER(mktemp)
772REGISTER_CHECKER(strcpy)
773REGISTER_CHECKER(rand)
774REGISTER_CHECKER(vfork)
775REGISTER_CHECKER(FloatLoopCounter)
Ted Kremenekb63d8d82012-01-20 05:35:06 +0000776REGISTER_CHECKER(UncheckedReturn)
777
Ted Kremenek76a54242012-01-20 01:44:29 +0000778