blob: e0c113c862629d7b96afe8c4e11bc12e844ba448 [file] [log] [blame]
Ted Kremenekc5b4c0e2009-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 Kyrtzidisaf45aca2011-02-17 21:39:33 +000014#include "ClangSACheckers.h"
Anna Zaksc29bed32011-09-20 21:38:35 +000015#include "clang/AST/StmtVisitor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Analysis/AnalysisContext.h"
Anna Zaksc29bed32011-09-20 21:38:35 +000017#include "clang/Basic/TargetInfo.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/StaticAnalyzer/Core/Checker.h"
Anna Zaksc29bed32011-09-20 21:38:35 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000021#include "llvm/ADT/SmallString.h"
Lenny Maioranifca2e962011-04-03 05:07:11 +000022#include "llvm/ADT/StringSwitch.h"
Anna Zaksc29bed32011-09-20 21:38:35 +000023#include "llvm/Support/raw_ostream.h"
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000024
25using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000026using namespace ento;
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000027
Ted Kremenekabf6ba12010-01-15 08:20:31 +000028static bool isArc4RandomAvailable(const ASTContext &Ctx) {
Douglas Gregore8bbc122011-09-02 00:18:52 +000029 const llvm::Triple &T = Ctx.getTargetInfo().getTriple();
Ted Kremenekabf6ba12010-01-15 08:20:31 +000030 return T.getVendor() == llvm::Triple::Apple ||
Ed Schoutene5bdc852015-03-11 08:48:55 +000031 T.getOS() == llvm::Triple::CloudABI ||
Douglas Gregor45e84b02011-01-17 19:16:24 +000032 T.getOS() == llvm::Triple::FreeBSD ||
33 T.getOS() == llvm::Triple::NetBSD ||
34 T.getOS() == llvm::Triple::OpenBSD ||
Eli Friedman9fa28852012-08-08 23:57:20 +000035 T.getOS() == llvm::Triple::Bitrig ||
Douglas Gregor45e84b02011-01-17 19:16:24 +000036 T.getOS() == llvm::Triple::DragonFly;
Ted Kremenekabf6ba12010-01-15 08:20:31 +000037}
38
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000039namespace {
Ted Kremenekc54dc952012-01-20 01:44:29 +000040struct ChecksFilter {
41 DefaultBool check_gets;
42 DefaultBool check_getpw;
43 DefaultBool check_mktemp;
Ted Kremenek89eaf8d2012-01-20 05:35:06 +000044 DefaultBool check_mkstemp;
Ted Kremenekc54dc952012-01-20 01:44:29 +000045 DefaultBool check_strcpy;
46 DefaultBool check_rand;
47 DefaultBool check_vfork;
48 DefaultBool check_FloatLoopCounter;
Ted Kremenek89eaf8d2012-01-20 05:35:06 +000049 DefaultBool check_UncheckedReturn;
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000050
51 CheckName checkName_gets;
52 CheckName checkName_getpw;
53 CheckName checkName_mktemp;
54 CheckName checkName_mkstemp;
55 CheckName checkName_strcpy;
56 CheckName checkName_rand;
57 CheckName checkName_vfork;
58 CheckName checkName_FloatLoopCounter;
59 CheckName checkName_UncheckedReturn;
Ted Kremenekc54dc952012-01-20 01:44:29 +000060};
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000061
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000062class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump11289f42009-09-09 15:08:12 +000063 BugReporter &BR;
Ted Kremenek81ce1c82011-10-24 01:32:45 +000064 AnalysisDeclContext* AC;
Ted Kremenekad5a6002009-09-02 02:47:41 +000065 enum { num_setids = 6 };
66 IdentifierInfo *II_setid[num_setids];
Ted Kremenek8edc6df2010-03-24 22:39:47 +000067
Ted Kremenekabf6ba12010-01-15 08:20:31 +000068 const bool CheckRand;
Ted Kremenekc54dc952012-01-20 01:44:29 +000069 const ChecksFilter &filter;
Mike Stump11289f42009-09-09 15:08:12 +000070
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000071public:
Ted Kremenekc54dc952012-01-20 01:44:29 +000072 WalkAST(BugReporter &br, AnalysisDeclContext* ac,
73 const ChecksFilter &f)
Anna Zaksc29bed32011-09-20 21:38:35 +000074 : BR(br), AC(ac), II_setid(),
Ted Kremenekc54dc952012-01-20 01:44:29 +000075 CheckRand(isArc4RandomAvailable(BR.getContext())),
76 filter(f) {}
Mike Stump11289f42009-09-09 15:08:12 +000077
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000078 // Statement visitor methods.
Ted Kremenek6610c032009-07-23 22:29:41 +000079 void VisitCallExpr(CallExpr *CE);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000080 void VisitForStmt(ForStmt *S);
Ted Kremenekd032fcc2009-08-28 00:08:09 +000081 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek9c497622009-07-23 21:34:35 +000082 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000083
84 void VisitChildren(Stmt *S);
Mike Stump11289f42009-09-09 15:08:12 +000085
Ted Kremenek6610c032009-07-23 22:29:41 +000086 // Helpers.
Lenny Maioranide909e42011-04-05 20:18:46 +000087 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
Mike Stump11289f42009-09-09 15:08:12 +000088
Lenny Maioranifca2e962011-04-03 05:07:11 +000089 typedef void (WalkAST::*FnCheck)(const CallExpr *,
90 const FunctionDecl *);
91
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000092 // Checker-specific methods.
Lenny Maioranide909e42011-04-05 20:18:46 +000093 void checkLoopConditionForFloat(const ForStmt *FS);
94 void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
95 void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
96 void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenek89eaf8d2012-01-20 05:35:06 +000097 void checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maioranide909e42011-04-05 20:18:46 +000098 void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
99 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
100 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
101 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
Anna Zaksfedf5df2011-10-11 04:34:54 +0000102 void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maioranide909e42011-04-05 20:18:46 +0000103 void checkUncheckedReturnValue(CallExpr *CE);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000104};
105} // end anonymous namespace
106
107//===----------------------------------------------------------------------===//
108// AST walking.
109//===----------------------------------------------------------------------===//
110
111void WalkAST::VisitChildren(Stmt *S) {
Benjamin Kramer973431b2015-07-03 15:12:24 +0000112 for (Stmt *Child : S->children())
113 if (Child)
114 Visit(Child);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000115}
116
Ted Kremenek6610c032009-07-23 22:29:41 +0000117void WalkAST::VisitCallExpr(CallExpr *CE) {
Lenny Maioranifca2e962011-04-03 05:07:11 +0000118 // Get the callee.
119 const FunctionDecl *FD = CE->getDirectCallee();
120
121 if (!FD)
122 return;
123
124 // Get the name of the callee. If it's a builtin, strip off the prefix.
125 IdentifierInfo *II = FD->getIdentifier();
126 if (!II) // if no identifier, not a simple C function
127 return;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000128 StringRef Name = II->getName();
Lenny Maioranifca2e962011-04-03 05:07:11 +0000129 if (Name.startswith("__builtin_"))
130 Name = Name.substr(10);
131
132 // Set the evaluation function by switching on the callee name.
133 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
Lenny Maioranide909e42011-04-05 20:18:46 +0000134 .Case("gets", &WalkAST::checkCall_gets)
135 .Case("getpw", &WalkAST::checkCall_getpw)
136 .Case("mktemp", &WalkAST::checkCall_mktemp)
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000137 .Case("mkstemp", &WalkAST::checkCall_mkstemp)
138 .Case("mkdtemp", &WalkAST::checkCall_mkstemp)
139 .Case("mkstemps", &WalkAST::checkCall_mkstemp)
Lenny Maioranide909e42011-04-05 20:18:46 +0000140 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
141 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
142 .Case("drand48", &WalkAST::checkCall_rand)
143 .Case("erand48", &WalkAST::checkCall_rand)
144 .Case("jrand48", &WalkAST::checkCall_rand)
145 .Case("lrand48", &WalkAST::checkCall_rand)
146 .Case("mrand48", &WalkAST::checkCall_rand)
147 .Case("nrand48", &WalkAST::checkCall_rand)
148 .Case("lcong48", &WalkAST::checkCall_rand)
149 .Case("rand", &WalkAST::checkCall_rand)
150 .Case("rand_r", &WalkAST::checkCall_rand)
151 .Case("random", &WalkAST::checkCall_random)
Anna Zaksfedf5df2011-10-11 04:34:54 +0000152 .Case("vfork", &WalkAST::checkCall_vfork)
Craig Topper0dbb7832014-05-27 02:45:47 +0000153 .Default(nullptr);
Lenny Maioranifca2e962011-04-03 05:07:11 +0000154
155 // If the callee isn't defined, it is not of security concern.
156 // Check and evaluate the call.
157 if (evalFunction)
158 (this->*evalFunction)(CE, FD);
Mike Stump11289f42009-09-09 15:08:12 +0000159
Ted Kremenek6610c032009-07-23 22:29:41 +0000160 // Recurse and check children.
161 VisitChildren(CE);
162}
163
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000164void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
Benjamin Kramer973431b2015-07-03 15:12:24 +0000165 for (Stmt *Child : S->children())
166 if (Child) {
167 if (CallExpr *CE = dyn_cast<CallExpr>(Child))
Lenny Maioranide909e42011-04-05 20:18:46 +0000168 checkUncheckedReturnValue(CE);
Benjamin Kramer973431b2015-07-03 15:12:24 +0000169 Visit(Child);
Mike Stump11289f42009-09-09 15:08:12 +0000170 }
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000171}
172
Ted Kremenek9c497622009-07-23 21:34:35 +0000173void WalkAST::VisitForStmt(ForStmt *FS) {
Lenny Maioranide909e42011-04-05 20:18:46 +0000174 checkLoopConditionForFloat(FS);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000175
Ted Kremenek9c497622009-07-23 21:34:35 +0000176 // Recurse and check children.
177 VisitChildren(FS);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000178}
179
180//===----------------------------------------------------------------------===//
Ted Kremenek9c497622009-07-23 21:34:35 +0000181// Check: floating poing variable used as loop counter.
Ted Kremenek70e55262009-07-23 21:44:18 +0000182// Originally: <rdar://problem/6336718>
183// Implements: CERT security coding advisory FLP-30.
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000184//===----------------------------------------------------------------------===//
185
Ted Kremenek9c497622009-07-23 21:34:35 +0000186static const DeclRefExpr*
Lenny Maioranide909e42011-04-05 20:18:46 +0000187getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
Ted Kremenek9c497622009-07-23 21:34:35 +0000188 expr = expr->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000189
190 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek9c497622009-07-23 21:34:35 +0000191 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCalle3027922010-08-25 11:45:40 +0000192 B->getOpcode() == BO_Comma))
Craig Topper0dbb7832014-05-27 02:45:47 +0000193 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000194
Lenny Maioranide909e42011-04-05 20:18:46 +0000195 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
Ted Kremenek9c497622009-07-23 21:34:35 +0000196 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +0000197
Lenny Maioranide909e42011-04-05 20:18:46 +0000198 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
Ted Kremenek9c497622009-07-23 21:34:35 +0000199 return rhs;
Mike Stump11289f42009-09-09 15:08:12 +0000200
Craig Topper0dbb7832014-05-27 02:45:47 +0000201 return nullptr;
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000202 }
Mike Stump11289f42009-09-09 15:08:12 +0000203
Ted Kremenek9c497622009-07-23 21:34:35 +0000204 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
205 const NamedDecl *ND = DR->getDecl();
Craig Topper0dbb7832014-05-27 02:45:47 +0000206 return ND == x || ND == y ? DR : nullptr;
Ted Kremenek9c497622009-07-23 21:34:35 +0000207 }
Mike Stump11289f42009-09-09 15:08:12 +0000208
Ted Kremenek9c497622009-07-23 21:34:35 +0000209 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
210 return U->isIncrementDecrementOp()
Craig Topper0dbb7832014-05-27 02:45:47 +0000211 ? getIncrementedVar(U->getSubExpr(), x, y) : nullptr;
Ted Kremenek9c497622009-07-23 21:34:35 +0000212
Craig Topper0dbb7832014-05-27 02:45:47 +0000213 return nullptr;
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000214}
215
Ted Kremenek9c497622009-07-23 21:34:35 +0000216/// CheckLoopConditionForFloat - This check looks for 'for' statements that
217/// use a floating point variable as a loop counter.
218/// CERT: FLP30-C, FLP30-CPP.
219///
Lenny Maioranide909e42011-04-05 20:18:46 +0000220void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000221 if (!filter.check_FloatLoopCounter)
222 return;
223
Ted Kremenek9c497622009-07-23 21:34:35 +0000224 // Does the loop have a condition?
225 const Expr *condition = FS->getCond();
Mike Stump11289f42009-09-09 15:08:12 +0000226
Ted Kremenek9c497622009-07-23 21:34:35 +0000227 if (!condition)
228 return;
229
230 // Does the loop have an increment?
231 const Expr *increment = FS->getInc();
Mike Stump11289f42009-09-09 15:08:12 +0000232
Ted Kremenek9c497622009-07-23 21:34:35 +0000233 if (!increment)
234 return;
Mike Stump11289f42009-09-09 15:08:12 +0000235
Ted Kremenek9c497622009-07-23 21:34:35 +0000236 // Strip away '()' and casts.
237 condition = condition->IgnoreParenCasts();
238 increment = increment->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000239
Ted Kremenek9c497622009-07-23 21:34:35 +0000240 // Is the loop condition a comparison?
241 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
242
243 if (!B)
244 return;
Mike Stump11289f42009-09-09 15:08:12 +0000245
Ted Kremenekb9cb1132009-07-24 20:26:31 +0000246 // Is this a comparison?
247 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek9c497622009-07-23 21:34:35 +0000248 return;
Mike Stump11289f42009-09-09 15:08:12 +0000249
Ted Kremenek9c497622009-07-23 21:34:35 +0000250 // Are we comparing variables?
John McCall34376a62010-12-04 03:47:34 +0000251 const DeclRefExpr *drLHS =
252 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
253 const DeclRefExpr *drRHS =
254 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump11289f42009-09-09 15:08:12 +0000255
Ted Kremenekb9cb1132009-07-24 20:26:31 +0000256 // Does at least one of the variables have a floating point type?
Craig Topper0dbb7832014-05-27 02:45:47 +0000257 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : nullptr;
258 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000259
Ted Kremenek9c497622009-07-23 21:34:35 +0000260 if (!drLHS && !drRHS)
261 return;
262
Craig Topper0dbb7832014-05-27 02:45:47 +0000263 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : nullptr;
264 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000265
Ted Kremenek9c497622009-07-23 21:34:35 +0000266 if (!vdLHS && !vdRHS)
Mike Stump11289f42009-09-09 15:08:12 +0000267 return;
268
Ted Kremenek9c497622009-07-23 21:34:35 +0000269 // Does either variable appear in increment?
Lenny Maioranide909e42011-04-05 20:18:46 +0000270 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump11289f42009-09-09 15:08:12 +0000271
Ted Kremenek9c497622009-07-23 21:34:35 +0000272 if (!drInc)
273 return;
Mike Stump11289f42009-09-09 15:08:12 +0000274
Ted Kremenek9c497622009-07-23 21:34:35 +0000275 // Emit the error. First figure out which DeclRefExpr in the condition
276 // referenced the compared variable.
Ted Kremenek26a36612012-10-12 22:56:42 +0000277 assert(drInc->getDecl());
Ted Kremenek9c497622009-07-23 21:34:35 +0000278 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
279
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000280 SmallVector<SourceRange, 2> ranges;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000281 SmallString<256> sbuf;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000282 llvm::raw_svector_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +0000283
Daniel Dunbar56df9772010-08-17 22:39:59 +0000284 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek9c497622009-07-23 21:34:35 +0000285 << "' with floating point type '" << drCond->getType().getAsString()
286 << "' should not be used as a loop counter";
287
288 ranges.push_back(drCond->getSourceRange());
289 ranges.push_back(drInc->getSourceRange());
Mike Stump11289f42009-09-09 15:08:12 +0000290
Ted Kremenek9c497622009-07-23 21:34:35 +0000291 const char *bugType = "Floating point variable used as loop counter";
Anna Zaksc29bed32011-09-20 21:38:35 +0000292
293 PathDiagnosticLocation FSLoc =
294 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000295 BR.EmitBasicReport(AC->getDecl(), filter.checkName_FloatLoopCounter,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000296 bugType, "Security", os.str(),
Jordan Rose42b42482013-10-07 17:16:59 +0000297 FSLoc, ranges);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000298}
299
300//===----------------------------------------------------------------------===//
Ted Kremenek6610c032009-07-23 22:29:41 +0000301// Check: Any use of 'gets' is insecure.
302// Originally: <rdar://problem/6335715>
303// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuf69973c2009-11-09 08:13:04 +0000304// CWE-242: Use of Inherently Dangerous Function
Ted Kremenek6610c032009-07-23 22:29:41 +0000305//===----------------------------------------------------------------------===//
306
Lenny Maioranide909e42011-04-05 20:18:46 +0000307void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000308 if (!filter.check_gets)
309 return;
310
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000311 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000312 if (!FPT)
Ted Kremenek6610c032009-07-23 22:29:41 +0000313 return;
Mike Stump11289f42009-09-09 15:08:12 +0000314
Ted Kremenek6610c032009-07-23 22:29:41 +0000315 // Verify that the function takes a single argument.
Alp Toker9cacbab2014-01-20 20:26:09 +0000316 if (FPT->getNumParams() != 1)
Ted Kremenek6610c032009-07-23 22:29:41 +0000317 return;
318
319 // Is the argument a 'char*'?
Alp Toker9cacbab2014-01-20 20:26:09 +0000320 const PointerType *PT = FPT->getParamType(0)->getAs<PointerType>();
Ted Kremenek6610c032009-07-23 22:29:41 +0000321 if (!PT)
322 return;
Mike Stump11289f42009-09-09 15:08:12 +0000323
Ted Kremenek6610c032009-07-23 22:29:41 +0000324 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
325 return;
Mike Stump11289f42009-09-09 15:08:12 +0000326
Ted Kremenek6610c032009-07-23 22:29:41 +0000327 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000328 PathDiagnosticLocation CELoc =
329 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000330 BR.EmitBasicReport(AC->getDecl(), filter.checkName_gets,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000331 "Potential buffer overflow in call to 'gets'",
Ted Kremenek6610c032009-07-23 22:29:41 +0000332 "Security",
333 "Call to function 'gets' is extremely insecure as it can "
334 "always result in a buffer overflow",
Jordan Rose42b42482013-10-07 17:16:59 +0000335 CELoc, CE->getCallee()->getSourceRange());
Ted Kremenek6610c032009-07-23 22:29:41 +0000336}
337
338//===----------------------------------------------------------------------===//
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000339// Check: Any use of 'getpwd' is insecure.
340// CWE-477: Use of Obsolete Functions
341//===----------------------------------------------------------------------===//
342
Lenny Maioranide909e42011-04-05 20:18:46 +0000343void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000344 if (!filter.check_getpw)
345 return;
346
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000347 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000348 if (!FPT)
349 return;
350
351 // Verify that the function takes two arguments.
Alp Toker9cacbab2014-01-20 20:26:09 +0000352 if (FPT->getNumParams() != 2)
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000353 return;
354
355 // Verify the first argument type is integer.
Alp Toker9cacbab2014-01-20 20:26:09 +0000356 if (!FPT->getParamType(0)->isIntegralOrUnscopedEnumerationType())
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000357 return;
358
359 // Verify the second argument type is char*.
Alp Toker9cacbab2014-01-20 20:26:09 +0000360 const PointerType *PT = FPT->getParamType(1)->getAs<PointerType>();
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000361 if (!PT)
362 return;
363
364 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
365 return;
366
367 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000368 PathDiagnosticLocation CELoc =
369 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000370 BR.EmitBasicReport(AC->getDecl(), filter.checkName_getpw,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000371 "Potential buffer overflow in call to 'getpw'",
Zhongxing Xud6e7f9d2009-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().",
Jordan Rose42b42482013-10-07 17:16:59 +0000375 CELoc, CE->getCallee()->getSourceRange());
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000376}
377
378//===----------------------------------------------------------------------===//
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000379// Check: Any use of 'mktemp' is insecure. It is obsoleted by mkstemp().
Zhongxing Xu39bba622009-12-03 09:15:23 +0000380// CWE-377: Insecure Temporary File
381//===----------------------------------------------------------------------===//
382
Lenny Maioranide909e42011-04-05 20:18:46 +0000383void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekafddb9c2012-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
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000391 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Zhongxing Xu39bba622009-12-03 09:15:23 +0000392 if(!FPT)
393 return;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000394
Lenny Maiorani70568c22011-03-31 21:26:55 +0000395 // Verify that the function takes a single argument.
Alp Toker9cacbab2014-01-20 20:26:09 +0000396 if (FPT->getNumParams() != 1)
Zhongxing Xu39bba622009-12-03 09:15:23 +0000397 return;
398
399 // Verify that the argument is Pointer Type.
Alp Toker9cacbab2014-01-20 20:26:09 +0000400 const PointerType *PT = FPT->getParamType(0)->getAs<PointerType>();
Zhongxing Xu39bba622009-12-03 09:15:23 +0000401 if (!PT)
402 return;
403
404 // Verify that the argument is a 'char*'.
405 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
406 return;
Ted Kremenekaeaf3d22010-03-24 22:39:45 +0000407
Alp Tokerc3f36af2014-05-15 01:35:53 +0000408 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000409 PathDiagnosticLocation CELoc =
410 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000411 BR.EmitBasicReport(AC->getDecl(), filter.checkName_mktemp,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000412 "Potential insecure temporary file in call 'mktemp'",
Eli Friedman04831922010-08-22 01:00:03 +0000413 "Security",
414 "Call to function 'mktemp' is insecure as it always "
Ted Kremenek5a10f082012-04-04 18:11:35 +0000415 "creates or uses insecure temporary file. Use 'mkstemp' "
416 "instead",
Jordan Rose42b42482013-10-07 17:16:59 +0000417 CELoc, CE->getCallee()->getSourceRange());
Zhongxing Xu39bba622009-12-03 09:15:23 +0000418}
419
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000420
421//===----------------------------------------------------------------------===//
422// Check: Use of 'mkstemp', 'mktemp', 'mkdtemp' should contain at least 6 X's.
423//===----------------------------------------------------------------------===//
424
425void WalkAST::checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD) {
426 if (!filter.check_mkstemp)
427 return;
428
429 StringRef Name = FD->getIdentifier()->getName();
430 std::pair<signed, signed> ArgSuffix =
431 llvm::StringSwitch<std::pair<signed, signed> >(Name)
432 .Case("mktemp", std::make_pair(0,-1))
433 .Case("mkstemp", std::make_pair(0,-1))
434 .Case("mkdtemp", std::make_pair(0,-1))
435 .Case("mkstemps", std::make_pair(0,1))
436 .Default(std::make_pair(-1, -1));
437
438 assert(ArgSuffix.first >= 0 && "Unsupported function");
439
440 // Check if the number of arguments is consistent with out expectations.
441 unsigned numArgs = CE->getNumArgs();
442 if ((signed) numArgs <= ArgSuffix.first)
443 return;
444
445 const StringLiteral *strArg =
446 dyn_cast<StringLiteral>(CE->getArg((unsigned)ArgSuffix.first)
447 ->IgnoreParenImpCasts());
448
449 // Currently we only handle string literals. It is possible to do better,
450 // either by looking at references to const variables, or by doing real
451 // flow analysis.
452 if (!strArg || strArg->getCharByteWidth() != 1)
453 return;
454
455 // Count the number of X's, taking into account a possible cutoff suffix.
456 StringRef str = strArg->getString();
457 unsigned numX = 0;
458 unsigned n = str.size();
459
460 // Take into account the suffix.
461 unsigned suffix = 0;
462 if (ArgSuffix.second >= 0) {
463 const Expr *suffixEx = CE->getArg((unsigned)ArgSuffix.second);
464 llvm::APSInt Result;
465 if (!suffixEx->EvaluateAsInt(Result, BR.getContext()))
466 return;
467 // FIXME: Issue a warning.
468 if (Result.isNegative())
469 return;
470 suffix = (unsigned) Result.getZExtValue();
471 n = (n > suffix) ? n - suffix : 0;
472 }
473
474 for (unsigned i = 0; i < n; ++i)
475 if (str[i] == 'X') ++numX;
476
477 if (numX >= 6)
478 return;
479
480 // Issue a warning.
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000481 PathDiagnosticLocation CELoc =
482 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000483 SmallString<512> buf;
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000484 llvm::raw_svector_ostream out(buf);
485 out << "Call to '" << Name << "' should have at least 6 'X's in the"
486 " format string to be secure (" << numX << " 'X'";
487 if (numX != 1)
488 out << 's';
489 out << " seen";
490 if (suffix) {
491 out << ", " << suffix << " character";
492 if (suffix > 1)
493 out << 's';
494 out << " used as a suffix";
495 }
496 out << ')';
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000497 BR.EmitBasicReport(AC->getDecl(), filter.checkName_mkstemp,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000498 "Insecure temporary file creation", "Security",
Jordan Rose42b42482013-10-07 17:16:59 +0000499 out.str(), CELoc, strArg->getSourceRange());
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000500}
501
Zhongxing Xu39bba622009-12-03 09:15:23 +0000502//===----------------------------------------------------------------------===//
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000503// Check: Any use of 'strcpy' is insecure.
504//
505// CWE-119: Improper Restriction of Operations within
506// the Bounds of a Memory Buffer
507//===----------------------------------------------------------------------===//
Lenny Maioranide909e42011-04-05 20:18:46 +0000508void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000509 if (!filter.check_strcpy)
510 return;
511
Lenny Maioranide909e42011-04-05 20:18:46 +0000512 if (!checkCall_strCommon(CE, FD))
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000513 return;
514
Lenny Maioranide909e42011-04-05 20:18:46 +0000515 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000516 PathDiagnosticLocation CELoc =
517 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000518 BR.EmitBasicReport(AC->getDecl(), filter.checkName_strcpy,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000519 "Potential insecure memory buffer bounds restriction in "
Lenny Maioranide909e42011-04-05 20:18:46 +0000520 "call 'strcpy'",
521 "Security",
522 "Call to function 'strcpy' is insecure as it does not "
Ted Kremenek5a10f082012-04-04 18:11:35 +0000523 "provide bounding of the memory buffer. Replace "
524 "unbounded copy functions with analogous functions that "
525 "support length arguments such as 'strlcpy'. CWE-119.",
Jordan Rose42b42482013-10-07 17:16:59 +0000526 CELoc, CE->getCallee()->getSourceRange());
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000527}
528
529//===----------------------------------------------------------------------===//
Lenny Maioranide909e42011-04-05 20:18:46 +0000530// Check: Any use of 'strcat' is insecure.
531//
532// CWE-119: Improper Restriction of Operations within
533// the Bounds of a Memory Buffer
534//===----------------------------------------------------------------------===//
535void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000536 if (!filter.check_strcpy)
537 return;
538
Lenny Maioranide909e42011-04-05 20:18:46 +0000539 if (!checkCall_strCommon(CE, FD))
540 return;
541
542 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000543 PathDiagnosticLocation CELoc =
544 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000545 BR.EmitBasicReport(AC->getDecl(), filter.checkName_strcpy,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000546 "Potential insecure memory buffer bounds restriction in "
547 "call 'strcat'",
548 "Security",
549 "Call to function 'strcat' is insecure as it does not "
550 "provide bounding of the memory buffer. Replace "
551 "unbounded copy functions with analogous functions that "
552 "support length arguments such as 'strlcat'. CWE-119.",
Jordan Rose42b42482013-10-07 17:16:59 +0000553 CELoc, CE->getCallee()->getSourceRange());
Lenny Maioranide909e42011-04-05 20:18:46 +0000554}
555
556//===----------------------------------------------------------------------===//
557// Common check for str* functions with no bounds parameters.
558//===----------------------------------------------------------------------===//
559bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000560 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Lenny Maioranide909e42011-04-05 20:18:46 +0000561 if (!FPT)
562 return false;
563
564 // Verify the function takes two arguments, three in the _chk version.
Alp Toker9cacbab2014-01-20 20:26:09 +0000565 int numArgs = FPT->getNumParams();
Lenny Maioranide909e42011-04-05 20:18:46 +0000566 if (numArgs != 2 && numArgs != 3)
567 return false;
568
569 // Verify the type for both arguments.
570 for (int i = 0; i < 2; i++) {
571 // Verify that the arguments are pointers.
Alp Toker9cacbab2014-01-20 20:26:09 +0000572 const PointerType *PT = FPT->getParamType(i)->getAs<PointerType>();
Lenny Maioranide909e42011-04-05 20:18:46 +0000573 if (!PT)
574 return false;
575
576 // Verify that the argument is a 'char*'.
577 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
578 return false;
579 }
580
581 return true;
582}
583
584//===----------------------------------------------------------------------===//
Ted Kremenekad5a6002009-09-02 02:47:41 +0000585// Check: Linear congruent random number generators should not be used
586// Originally: <rdar://problem/63371000>
587// CWE-338: Use of cryptographically weak prng
588//===----------------------------------------------------------------------===//
589
Lenny Maioranide909e42011-04-05 20:18:46 +0000590void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000591 if (!filter.check_rand || !CheckRand)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000592 return;
Mike Stump11289f42009-09-09 15:08:12 +0000593
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000594 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenekad5a6002009-09-02 02:47:41 +0000595 if (!FTP)
596 return;
Mike Stump11289f42009-09-09 15:08:12 +0000597
Alp Toker9cacbab2014-01-20 20:26:09 +0000598 if (FTP->getNumParams() == 1) {
Ted Kremenekad5a6002009-09-02 02:47:41 +0000599 // Is the argument an 'unsigned short *'?
600 // (Actually any integer type is allowed.)
Alp Toker9cacbab2014-01-20 20:26:09 +0000601 const PointerType *PT = FTP->getParamType(0)->getAs<PointerType>();
Ted Kremenekad5a6002009-09-02 02:47:41 +0000602 if (!PT)
603 return;
Mike Stump11289f42009-09-09 15:08:12 +0000604
Jordan Rose61e221f2013-04-09 02:30:33 +0000605 if (! PT->getPointeeType()->isIntegralOrUnscopedEnumerationType())
Ted Kremenekad5a6002009-09-02 02:47:41 +0000606 return;
Alp Toker9cacbab2014-01-20 20:26:09 +0000607 } else if (FTP->getNumParams() != 0)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000608 return;
Mike Stump11289f42009-09-09 15:08:12 +0000609
Ted Kremenekad5a6002009-09-02 02:47:41 +0000610 // Issue a warning.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000611 SmallString<256> buf1;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000612 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000613 os1 << '\'' << *FD << "' is a poor random number generator";
Ted Kremenekad5a6002009-09-02 02:47:41 +0000614
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000615 SmallString<256> buf2;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000616 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000617 os2 << "Function '" << *FD
Ted Kremenekad5a6002009-09-02 02:47:41 +0000618 << "' is obsolete because it implements a poor random number generator."
619 << " Use 'arc4random' instead";
620
Anna Zaksc29bed32011-09-20 21:38:35 +0000621 PathDiagnosticLocation CELoc =
622 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000623 BR.EmitBasicReport(AC->getDecl(), filter.checkName_rand, os1.str(),
624 "Security", os2.str(), CELoc,
625 CE->getCallee()->getSourceRange());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000626}
627
628//===----------------------------------------------------------------------===//
629// Check: 'random' should not be used
630// Originally: <rdar://problem/63371000>
631//===----------------------------------------------------------------------===//
632
Lenny Maioranide909e42011-04-05 20:18:46 +0000633void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000634 if (!CheckRand || !filter.check_rand)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000635 return;
Mike Stump11289f42009-09-09 15:08:12 +0000636
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000637 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenekad5a6002009-09-02 02:47:41 +0000638 if (!FTP)
639 return;
Mike Stump11289f42009-09-09 15:08:12 +0000640
Ted Kremenekad5a6002009-09-02 02:47:41 +0000641 // Verify that the function takes no argument.
Alp Toker9cacbab2014-01-20 20:26:09 +0000642 if (FTP->getNumParams() != 0)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000643 return;
Mike Stump11289f42009-09-09 15:08:12 +0000644
Ted Kremenekad5a6002009-09-02 02:47:41 +0000645 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000646 PathDiagnosticLocation CELoc =
647 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000648 BR.EmitBasicReport(AC->getDecl(), filter.checkName_rand,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000649 "'random' is not a secure random number generator",
Ted Kremenekad5a6002009-09-02 02:47:41 +0000650 "Security",
651 "The 'random' function produces a sequence of values that "
652 "an adversary may be able to predict. Use 'arc4random' "
Jordan Rose42b42482013-10-07 17:16:59 +0000653 "instead", CELoc, CE->getCallee()->getSourceRange());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000654}
655
656//===----------------------------------------------------------------------===//
Anna Zaksfedf5df2011-10-11 04:34:54 +0000657// Check: 'vfork' should not be used.
658// POS33-C: Do not use vfork().
659//===----------------------------------------------------------------------===//
660
661void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000662 if (!filter.check_vfork)
663 return;
664
Anna Zaksfedf5df2011-10-11 04:34:54 +0000665 // All calls to vfork() are insecure, issue a warning.
Anna Zaksfedf5df2011-10-11 04:34:54 +0000666 PathDiagnosticLocation CELoc =
667 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000668 BR.EmitBasicReport(AC->getDecl(), filter.checkName_vfork,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000669 "Potential insecure implementation-specific behavior in "
Anna Zaksfedf5df2011-10-11 04:34:54 +0000670 "call 'vfork'",
671 "Security",
672 "Call to function 'vfork' is insecure as it can lead to "
673 "denial of service situations in the parent process. "
674 "Replace calls to vfork with calls to the safer "
675 "'posix_spawn' function",
Jordan Rose42b42482013-10-07 17:16:59 +0000676 CELoc, CE->getCallee()->getSourceRange());
Anna Zaksfedf5df2011-10-11 04:34:54 +0000677}
678
679//===----------------------------------------------------------------------===//
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000680// Check: Should check whether privileges are dropped successfully.
681// Originally: <rdar://problem/6337132>
682//===----------------------------------------------------------------------===//
683
Lenny Maioranide909e42011-04-05 20:18:46 +0000684void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000685 if (!filter.check_UncheckedReturn)
686 return;
687
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000688 const FunctionDecl *FD = CE->getDirectCallee();
689 if (!FD)
690 return;
691
Craig Topper0dbb7832014-05-27 02:45:47 +0000692 if (II_setid[0] == nullptr) {
Ted Kremenekad5a6002009-09-02 02:47:41 +0000693 static const char * const identifiers[num_setids] = {
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000694 "setuid", "setgid", "seteuid", "setegid",
695 "setreuid", "setregid"
696 };
Mike Stump11289f42009-09-09 15:08:12 +0000697
Ted Kremenekad5a6002009-09-02 02:47:41 +0000698 for (size_t i = 0; i < num_setids; i++)
Mike Stump11289f42009-09-09 15:08:12 +0000699 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000700 }
Mike Stump11289f42009-09-09 15:08:12 +0000701
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000702 const IdentifierInfo *id = FD->getIdentifier();
703 size_t identifierid;
704
Ted Kremenekad5a6002009-09-02 02:47:41 +0000705 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000706 if (id == II_setid[identifierid])
707 break;
708
Ted Kremenekad5a6002009-09-02 02:47:41 +0000709 if (identifierid >= num_setids)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000710 return;
Mike Stump11289f42009-09-09 15:08:12 +0000711
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000712 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000713 if (!FTP)
714 return;
Mike Stump11289f42009-09-09 15:08:12 +0000715
Ted Kremenekaeca0952009-08-28 00:24:55 +0000716 // Verify that the function takes one or two arguments (depending on
717 // the function).
Alp Toker9cacbab2014-01-20 20:26:09 +0000718 if (FTP->getNumParams() != (identifierid < 4 ? 1 : 2))
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000719 return;
720
721 // The arguments must be integers.
Alp Toker9cacbab2014-01-20 20:26:09 +0000722 for (unsigned i = 0; i < FTP->getNumParams(); i++)
723 if (!FTP->getParamType(i)->isIntegralOrUnscopedEnumerationType())
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000724 return;
725
726 // Issue a warning.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000727 SmallString<256> buf1;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000728 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000729 os1 << "Return value is not checked in call to '" << *FD << '\'';
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000730
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000731 SmallString<256> buf2;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000732 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000733 os2 << "The return value from the call to '" << *FD
734 << "' is not checked. If an error occurs in '" << *FD
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000735 << "', the following code may execute with unexpected privileges";
736
Anna Zaksc29bed32011-09-20 21:38:35 +0000737 PathDiagnosticLocation CELoc =
738 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000739 BR.EmitBasicReport(AC->getDecl(), filter.checkName_UncheckedReturn, os1.str(),
740 "Security", os2.str(), CELoc,
741 CE->getCallee()->getSourceRange());
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000742}
743
744//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000745// SecuritySyntaxChecker
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000746//===----------------------------------------------------------------------===//
747
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000748namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000749class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000750public:
Ted Kremenekc54dc952012-01-20 01:44:29 +0000751 ChecksFilter filter;
752
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000753 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
754 BugReporter &BR) const {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000755 WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000756 walker.Visit(D->getBody());
757 }
758};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000759}
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000760
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000761#define REGISTER_CHECKER(name) \
762 void ento::register##name(CheckerManager &mgr) { \
763 SecuritySyntaxChecker *checker = \
764 mgr.registerChecker<SecuritySyntaxChecker>(); \
765 checker->filter.check_##name = true; \
766 checker->filter.checkName_##name = mgr.getCurrentCheckName(); \
767 }
Ted Kremenekc54dc952012-01-20 01:44:29 +0000768
769REGISTER_CHECKER(gets)
770REGISTER_CHECKER(getpw)
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000771REGISTER_CHECKER(mkstemp)
Ted Kremenekc54dc952012-01-20 01:44:29 +0000772REGISTER_CHECKER(mktemp)
773REGISTER_CHECKER(strcpy)
774REGISTER_CHECKER(rand)
775REGISTER_CHECKER(vfork)
776REGISTER_CHECKER(FloatLoopCounter)
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000777REGISTER_CHECKER(UncheckedReturn)
778
Ted Kremenekc54dc952012-01-20 01:44:29 +0000779