blob: 6dbacad7f2eab8476326e37168a25bb35b1d6961 [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"
George Karpenkov50657f62017-09-06 21:45:03 +000016#include "clang/Analysis/AnalysisDeclContext.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 ||
35 T.getOS() == llvm::Triple::DragonFly;
Ted Kremenekabf6ba12010-01-15 08:20:31 +000036}
37
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000038namespace {
Ted Kremenekc54dc952012-01-20 01:44:29 +000039struct ChecksFilter {
40 DefaultBool check_gets;
41 DefaultBool check_getpw;
42 DefaultBool check_mktemp;
Ted Kremenek89eaf8d2012-01-20 05:35:06 +000043 DefaultBool check_mkstemp;
Ted Kremenekc54dc952012-01-20 01:44:29 +000044 DefaultBool check_strcpy;
45 DefaultBool check_rand;
46 DefaultBool check_vfork;
47 DefaultBool check_FloatLoopCounter;
Ted Kremenek89eaf8d2012-01-20 05:35:06 +000048 DefaultBool check_UncheckedReturn;
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000049
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 Kremenekc54dc952012-01-20 01:44:29 +000059};
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000060
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000061class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump11289f42009-09-09 15:08:12 +000062 BugReporter &BR;
Ted Kremenek81ce1c82011-10-24 01:32:45 +000063 AnalysisDeclContext* AC;
Ted Kremenekad5a6002009-09-02 02:47:41 +000064 enum { num_setids = 6 };
65 IdentifierInfo *II_setid[num_setids];
Ted Kremenek8edc6df2010-03-24 22:39:47 +000066
Ted Kremenekabf6ba12010-01-15 08:20:31 +000067 const bool CheckRand;
Ted Kremenekc54dc952012-01-20 01:44:29 +000068 const ChecksFilter &filter;
Mike Stump11289f42009-09-09 15:08:12 +000069
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000070public:
Ted Kremenekc54dc952012-01-20 01:44:29 +000071 WalkAST(BugReporter &br, AnalysisDeclContext* ac,
72 const ChecksFilter &f)
Anna Zaksc29bed32011-09-20 21:38:35 +000073 : BR(br), AC(ac), II_setid(),
Ted Kremenekc54dc952012-01-20 01:44:29 +000074 CheckRand(isArc4RandomAvailable(BR.getContext())),
75 filter(f) {}
Mike Stump11289f42009-09-09 15:08:12 +000076
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000077 // Statement visitor methods.
Ted Kremenek6610c032009-07-23 22:29:41 +000078 void VisitCallExpr(CallExpr *CE);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000079 void VisitForStmt(ForStmt *S);
Ted Kremenekd032fcc2009-08-28 00:08:09 +000080 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek9c497622009-07-23 21:34:35 +000081 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000082
83 void VisitChildren(Stmt *S);
Mike Stump11289f42009-09-09 15:08:12 +000084
Ted Kremenek6610c032009-07-23 22:29:41 +000085 // Helpers.
Lenny Maioranide909e42011-04-05 20:18:46 +000086 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
Mike Stump11289f42009-09-09 15:08:12 +000087
Pierre Gousseau2a3ca842015-11-26 22:08:58 +000088 typedef void (WalkAST::*FnCheck)(const CallExpr *, const FunctionDecl *);
Lenny Maioranifca2e962011-04-03 05:07:11 +000089
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000090 // Checker-specific methods.
Lenny Maioranide909e42011-04-05 20:18:46 +000091 void checkLoopConditionForFloat(const ForStmt *FS);
92 void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
93 void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
94 void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenek89eaf8d2012-01-20 05:35:06 +000095 void checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maioranide909e42011-04-05 20:18:46 +000096 void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
97 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
98 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
99 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
Anna Zaksfedf5df2011-10-11 04:34:54 +0000100 void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maioranide909e42011-04-05 20:18:46 +0000101 void checkUncheckedReturnValue(CallExpr *CE);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000102};
103} // end anonymous namespace
104
105//===----------------------------------------------------------------------===//
106// AST walking.
107//===----------------------------------------------------------------------===//
108
109void WalkAST::VisitChildren(Stmt *S) {
Benjamin Kramer973431b2015-07-03 15:12:24 +0000110 for (Stmt *Child : S->children())
111 if (Child)
112 Visit(Child);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000113}
114
Ted Kremenek6610c032009-07-23 22:29:41 +0000115void WalkAST::VisitCallExpr(CallExpr *CE) {
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000116 // Get the callee.
Lenny Maioranifca2e962011-04-03 05:07:11 +0000117 const FunctionDecl *FD = CE->getDirectCallee();
118
119 if (!FD)
120 return;
121
122 // Get the name of the callee. If it's a builtin, strip off the prefix.
123 IdentifierInfo *II = FD->getIdentifier();
124 if (!II) // if no identifier, not a simple C function
125 return;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000126 StringRef Name = II->getName();
Lenny Maioranifca2e962011-04-03 05:07:11 +0000127 if (Name.startswith("__builtin_"))
128 Name = Name.substr(10);
129
130 // Set the evaluation function by switching on the callee name.
131 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
Lenny Maioranide909e42011-04-05 20:18:46 +0000132 .Case("gets", &WalkAST::checkCall_gets)
133 .Case("getpw", &WalkAST::checkCall_getpw)
134 .Case("mktemp", &WalkAST::checkCall_mktemp)
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000135 .Case("mkstemp", &WalkAST::checkCall_mkstemp)
136 .Case("mkdtemp", &WalkAST::checkCall_mkstemp)
137 .Case("mkstemps", &WalkAST::checkCall_mkstemp)
Lenny Maioranide909e42011-04-05 20:18:46 +0000138 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
139 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
140 .Case("drand48", &WalkAST::checkCall_rand)
141 .Case("erand48", &WalkAST::checkCall_rand)
142 .Case("jrand48", &WalkAST::checkCall_rand)
143 .Case("lrand48", &WalkAST::checkCall_rand)
144 .Case("mrand48", &WalkAST::checkCall_rand)
145 .Case("nrand48", &WalkAST::checkCall_rand)
146 .Case("lcong48", &WalkAST::checkCall_rand)
147 .Case("rand", &WalkAST::checkCall_rand)
148 .Case("rand_r", &WalkAST::checkCall_rand)
149 .Case("random", &WalkAST::checkCall_random)
Anna Zaksfedf5df2011-10-11 04:34:54 +0000150 .Case("vfork", &WalkAST::checkCall_vfork)
Craig Topper0dbb7832014-05-27 02:45:47 +0000151 .Default(nullptr);
Lenny Maioranifca2e962011-04-03 05:07:11 +0000152
153 // If the callee isn't defined, it is not of security concern.
154 // Check and evaluate the call.
155 if (evalFunction)
156 (this->*evalFunction)(CE, FD);
Mike Stump11289f42009-09-09 15:08:12 +0000157
Ted Kremenek6610c032009-07-23 22:29:41 +0000158 // Recurse and check children.
159 VisitChildren(CE);
160}
161
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000162void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
Benjamin Kramer973431b2015-07-03 15:12:24 +0000163 for (Stmt *Child : S->children())
164 if (Child) {
165 if (CallExpr *CE = dyn_cast<CallExpr>(Child))
Lenny Maioranide909e42011-04-05 20:18:46 +0000166 checkUncheckedReturnValue(CE);
Benjamin Kramer973431b2015-07-03 15:12:24 +0000167 Visit(Child);
Mike Stump11289f42009-09-09 15:08:12 +0000168 }
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000169}
170
Ted Kremenek9c497622009-07-23 21:34:35 +0000171void WalkAST::VisitForStmt(ForStmt *FS) {
Lenny Maioranide909e42011-04-05 20:18:46 +0000172 checkLoopConditionForFloat(FS);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000173
Ted Kremenek9c497622009-07-23 21:34:35 +0000174 // Recurse and check children.
175 VisitChildren(FS);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000176}
177
178//===----------------------------------------------------------------------===//
Ted Kremenek9c497622009-07-23 21:34:35 +0000179// Check: floating poing variable used as loop counter.
Ted Kremenek70e55262009-07-23 21:44:18 +0000180// Originally: <rdar://problem/6336718>
181// Implements: CERT security coding advisory FLP-30.
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000182//===----------------------------------------------------------------------===//
183
Ted Kremenek9c497622009-07-23 21:34:35 +0000184static const DeclRefExpr*
Lenny Maioranide909e42011-04-05 20:18:46 +0000185getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
Ted Kremenek9c497622009-07-23 21:34:35 +0000186 expr = expr->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000187
188 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek9c497622009-07-23 21:34:35 +0000189 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCalle3027922010-08-25 11:45:40 +0000190 B->getOpcode() == BO_Comma))
Craig Topper0dbb7832014-05-27 02:45:47 +0000191 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000192
Lenny Maioranide909e42011-04-05 20:18:46 +0000193 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
Ted Kremenek9c497622009-07-23 21:34:35 +0000194 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +0000195
Lenny Maioranide909e42011-04-05 20:18:46 +0000196 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
Ted Kremenek9c497622009-07-23 21:34:35 +0000197 return rhs;
Mike Stump11289f42009-09-09 15:08:12 +0000198
Craig Topper0dbb7832014-05-27 02:45:47 +0000199 return nullptr;
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000200 }
Mike Stump11289f42009-09-09 15:08:12 +0000201
Ted Kremenek9c497622009-07-23 21:34:35 +0000202 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
203 const NamedDecl *ND = DR->getDecl();
Craig Topper0dbb7832014-05-27 02:45:47 +0000204 return ND == x || ND == y ? DR : nullptr;
Ted Kremenek9c497622009-07-23 21:34:35 +0000205 }
Mike Stump11289f42009-09-09 15:08:12 +0000206
Ted Kremenek9c497622009-07-23 21:34:35 +0000207 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
208 return U->isIncrementDecrementOp()
Craig Topper0dbb7832014-05-27 02:45:47 +0000209 ? getIncrementedVar(U->getSubExpr(), x, y) : nullptr;
Ted Kremenek9c497622009-07-23 21:34:35 +0000210
Craig Topper0dbb7832014-05-27 02:45:47 +0000211 return nullptr;
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000212}
213
Ted Kremenek9c497622009-07-23 21:34:35 +0000214/// CheckLoopConditionForFloat - This check looks for 'for' statements that
215/// use a floating point variable as a loop counter.
216/// CERT: FLP30-C, FLP30-CPP.
217///
Lenny Maioranide909e42011-04-05 20:18:46 +0000218void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000219 if (!filter.check_FloatLoopCounter)
220 return;
221
Ted Kremenek9c497622009-07-23 21:34:35 +0000222 // Does the loop have a condition?
223 const Expr *condition = FS->getCond();
Mike Stump11289f42009-09-09 15:08:12 +0000224
Ted Kremenek9c497622009-07-23 21:34:35 +0000225 if (!condition)
226 return;
227
228 // Does the loop have an increment?
229 const Expr *increment = FS->getInc();
Mike Stump11289f42009-09-09 15:08:12 +0000230
Ted Kremenek9c497622009-07-23 21:34:35 +0000231 if (!increment)
232 return;
Mike Stump11289f42009-09-09 15:08:12 +0000233
Ted Kremenek9c497622009-07-23 21:34:35 +0000234 // Strip away '()' and casts.
235 condition = condition->IgnoreParenCasts();
236 increment = increment->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000237
Ted Kremenek9c497622009-07-23 21:34:35 +0000238 // Is the loop condition a comparison?
239 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
240
241 if (!B)
242 return;
Mike Stump11289f42009-09-09 15:08:12 +0000243
Ted Kremenekb9cb1132009-07-24 20:26:31 +0000244 // Is this a comparison?
245 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek9c497622009-07-23 21:34:35 +0000246 return;
Mike Stump11289f42009-09-09 15:08:12 +0000247
Ted Kremenek9c497622009-07-23 21:34:35 +0000248 // Are we comparing variables?
John McCall34376a62010-12-04 03:47:34 +0000249 const DeclRefExpr *drLHS =
250 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
251 const DeclRefExpr *drRHS =
252 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump11289f42009-09-09 15:08:12 +0000253
Ted Kremenekb9cb1132009-07-24 20:26:31 +0000254 // Does at least one of the variables have a floating point type?
Craig Topper0dbb7832014-05-27 02:45:47 +0000255 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : nullptr;
256 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000257
Ted Kremenek9c497622009-07-23 21:34:35 +0000258 if (!drLHS && !drRHS)
259 return;
260
Craig Topper0dbb7832014-05-27 02:45:47 +0000261 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : nullptr;
262 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000263
Ted Kremenek9c497622009-07-23 21:34:35 +0000264 if (!vdLHS && !vdRHS)
Mike Stump11289f42009-09-09 15:08:12 +0000265 return;
266
Ted Kremenek9c497622009-07-23 21:34:35 +0000267 // Does either variable appear in increment?
Lenny Maioranide909e42011-04-05 20:18:46 +0000268 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump11289f42009-09-09 15:08:12 +0000269
Ted Kremenek9c497622009-07-23 21:34:35 +0000270 if (!drInc)
271 return;
Mike Stump11289f42009-09-09 15:08:12 +0000272
Ted Kremenek9c497622009-07-23 21:34:35 +0000273 // Emit the error. First figure out which DeclRefExpr in the condition
274 // referenced the compared variable.
Ted Kremenek26a36612012-10-12 22:56:42 +0000275 assert(drInc->getDecl());
Ted Kremenek9c497622009-07-23 21:34:35 +0000276 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
277
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000278 SmallVector<SourceRange, 2> ranges;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000279 SmallString<256> sbuf;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000280 llvm::raw_svector_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +0000281
Daniel Dunbar56df9772010-08-17 22:39:59 +0000282 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek9c497622009-07-23 21:34:35 +0000283 << "' with floating point type '" << drCond->getType().getAsString()
284 << "' should not be used as a loop counter";
285
286 ranges.push_back(drCond->getSourceRange());
287 ranges.push_back(drInc->getSourceRange());
Mike Stump11289f42009-09-09 15:08:12 +0000288
Ted Kremenek9c497622009-07-23 21:34:35 +0000289 const char *bugType = "Floating point variable used as loop counter";
Anna Zaksc29bed32011-09-20 21:38:35 +0000290
291 PathDiagnosticLocation FSLoc =
292 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000293 BR.EmitBasicReport(AC->getDecl(), filter.checkName_FloatLoopCounter,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000294 bugType, "Security", os.str(),
Jordan Rose42b42482013-10-07 17:16:59 +0000295 FSLoc, ranges);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000296}
297
298//===----------------------------------------------------------------------===//
Ted Kremenek6610c032009-07-23 22:29:41 +0000299// Check: Any use of 'gets' is insecure.
300// Originally: <rdar://problem/6335715>
301// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuf69973c2009-11-09 08:13:04 +0000302// CWE-242: Use of Inherently Dangerous Function
Ted Kremenek6610c032009-07-23 22:29:41 +0000303//===----------------------------------------------------------------------===//
304
Lenny Maioranide909e42011-04-05 20:18:46 +0000305void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000306 if (!filter.check_gets)
307 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000308
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000309 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000310 if (!FPT)
Ted Kremenek6610c032009-07-23 22:29:41 +0000311 return;
Mike Stump11289f42009-09-09 15:08:12 +0000312
Ted Kremenek6610c032009-07-23 22:29:41 +0000313 // Verify that the function takes a single argument.
Alp Toker9cacbab2014-01-20 20:26:09 +0000314 if (FPT->getNumParams() != 1)
Ted Kremenek6610c032009-07-23 22:29:41 +0000315 return;
316
317 // Is the argument a 'char*'?
Alp Toker9cacbab2014-01-20 20:26:09 +0000318 const PointerType *PT = FPT->getParamType(0)->getAs<PointerType>();
Ted Kremenek6610c032009-07-23 22:29:41 +0000319 if (!PT)
320 return;
Mike Stump11289f42009-09-09 15:08:12 +0000321
Ted Kremenek6610c032009-07-23 22:29:41 +0000322 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
323 return;
Mike Stump11289f42009-09-09 15:08:12 +0000324
Ted Kremenek6610c032009-07-23 22:29:41 +0000325 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000326 PathDiagnosticLocation CELoc =
327 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000328 BR.EmitBasicReport(AC->getDecl(), filter.checkName_gets,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000329 "Potential buffer overflow in call to 'gets'",
Ted Kremenek6610c032009-07-23 22:29:41 +0000330 "Security",
331 "Call to function 'gets' is extremely insecure as it can "
332 "always result in a buffer overflow",
Jordan Rose42b42482013-10-07 17:16:59 +0000333 CELoc, CE->getCallee()->getSourceRange());
Ted Kremenek6610c032009-07-23 22:29:41 +0000334}
335
336//===----------------------------------------------------------------------===//
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000337// Check: Any use of 'getpwd' is insecure.
338// CWE-477: Use of Obsolete Functions
339//===----------------------------------------------------------------------===//
340
Lenny Maioranide909e42011-04-05 20:18:46 +0000341void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000342 if (!filter.check_getpw)
343 return;
344
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000345 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000346 if (!FPT)
347 return;
348
349 // Verify that the function takes two arguments.
Alp Toker9cacbab2014-01-20 20:26:09 +0000350 if (FPT->getNumParams() != 2)
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000351 return;
352
353 // Verify the first argument type is integer.
Alp Toker9cacbab2014-01-20 20:26:09 +0000354 if (!FPT->getParamType(0)->isIntegralOrUnscopedEnumerationType())
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000355 return;
356
357 // Verify the second argument type is char*.
Alp Toker9cacbab2014-01-20 20:26:09 +0000358 const PointerType *PT = FPT->getParamType(1)->getAs<PointerType>();
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000359 if (!PT)
360 return;
361
362 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
363 return;
364
365 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000366 PathDiagnosticLocation CELoc =
367 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000368 BR.EmitBasicReport(AC->getDecl(), filter.checkName_getpw,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000369 "Potential buffer overflow in call to 'getpw'",
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000370 "Security",
371 "The getpw() function is dangerous as it may overflow the "
372 "provided buffer. It is obsoleted by getpwuid().",
Jordan Rose42b42482013-10-07 17:16:59 +0000373 CELoc, CE->getCallee()->getSourceRange());
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000374}
375
376//===----------------------------------------------------------------------===//
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000377// Check: Any use of 'mktemp' is insecure. It is obsoleted by mkstemp().
Zhongxing Xu39bba622009-12-03 09:15:23 +0000378// CWE-377: Insecure Temporary File
379//===----------------------------------------------------------------------===//
380
Lenny Maioranide909e42011-04-05 20:18:46 +0000381void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekafddb9c2012-06-29 21:01:35 +0000382 if (!filter.check_mktemp) {
383 // Fall back to the security check of looking for enough 'X's in the
384 // format string, since that is a less severe warning.
385 checkCall_mkstemp(CE, FD);
386 return;
387 }
388
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000389 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Zhongxing Xu39bba622009-12-03 09:15:23 +0000390 if(!FPT)
391 return;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000392
Lenny Maiorani70568c22011-03-31 21:26:55 +0000393 // Verify that the function takes a single argument.
Alp Toker9cacbab2014-01-20 20:26:09 +0000394 if (FPT->getNumParams() != 1)
Zhongxing Xu39bba622009-12-03 09:15:23 +0000395 return;
396
397 // Verify that the argument is Pointer Type.
Alp Toker9cacbab2014-01-20 20:26:09 +0000398 const PointerType *PT = FPT->getParamType(0)->getAs<PointerType>();
Zhongxing Xu39bba622009-12-03 09:15:23 +0000399 if (!PT)
400 return;
401
402 // Verify that the argument is a 'char*'.
403 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
404 return;
Ted Kremenekaeaf3d22010-03-24 22:39:45 +0000405
Alp Tokerc3f36af2014-05-15 01:35:53 +0000406 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000407 PathDiagnosticLocation CELoc =
408 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000409 BR.EmitBasicReport(AC->getDecl(), filter.checkName_mktemp,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000410 "Potential insecure temporary file in call 'mktemp'",
Eli Friedman04831922010-08-22 01:00:03 +0000411 "Security",
412 "Call to function 'mktemp' is insecure as it always "
Ted Kremenek5a10f082012-04-04 18:11:35 +0000413 "creates or uses insecure temporary file. Use 'mkstemp' "
414 "instead",
Jordan Rose42b42482013-10-07 17:16:59 +0000415 CELoc, CE->getCallee()->getSourceRange());
Zhongxing Xu39bba622009-12-03 09:15:23 +0000416}
417
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000418
419//===----------------------------------------------------------------------===//
420// Check: Use of 'mkstemp', 'mktemp', 'mkdtemp' should contain at least 6 X's.
421//===----------------------------------------------------------------------===//
422
423void WalkAST::checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD) {
424 if (!filter.check_mkstemp)
425 return;
426
427 StringRef Name = FD->getIdentifier()->getName();
428 std::pair<signed, signed> ArgSuffix =
429 llvm::StringSwitch<std::pair<signed, signed> >(Name)
430 .Case("mktemp", std::make_pair(0,-1))
431 .Case("mkstemp", std::make_pair(0,-1))
432 .Case("mkdtemp", std::make_pair(0,-1))
433 .Case("mkstemps", std::make_pair(0,1))
434 .Default(std::make_pair(-1, -1));
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000435
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000436 assert(ArgSuffix.first >= 0 && "Unsupported function");
437
438 // Check if the number of arguments is consistent with out expectations.
439 unsigned numArgs = CE->getNumArgs();
440 if ((signed) numArgs <= ArgSuffix.first)
441 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000442
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000443 const StringLiteral *strArg =
444 dyn_cast<StringLiteral>(CE->getArg((unsigned)ArgSuffix.first)
445 ->IgnoreParenImpCasts());
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000446
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000447 // Currently we only handle string literals. It is possible to do better,
448 // either by looking at references to const variables, or by doing real
449 // flow analysis.
450 if (!strArg || strArg->getCharByteWidth() != 1)
451 return;
452
453 // Count the number of X's, taking into account a possible cutoff suffix.
454 StringRef str = strArg->getString();
455 unsigned numX = 0;
456 unsigned n = str.size();
457
458 // Take into account the suffix.
459 unsigned suffix = 0;
460 if (ArgSuffix.second >= 0) {
461 const Expr *suffixEx = CE->getArg((unsigned)ArgSuffix.second);
462 llvm::APSInt Result;
463 if (!suffixEx->EvaluateAsInt(Result, BR.getContext()))
464 return;
465 // FIXME: Issue a warning.
466 if (Result.isNegative())
467 return;
468 suffix = (unsigned) Result.getZExtValue();
469 n = (n > suffix) ? n - suffix : 0;
470 }
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000471
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000472 for (unsigned i = 0; i < n; ++i)
473 if (str[i] == 'X') ++numX;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000474
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000475 if (numX >= 6)
476 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000477
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000478 // Issue a warning.
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000479 PathDiagnosticLocation CELoc =
480 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000481 SmallString<512> buf;
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000482 llvm::raw_svector_ostream out(buf);
483 out << "Call to '" << Name << "' should have at least 6 'X's in the"
484 " format string to be secure (" << numX << " 'X'";
485 if (numX != 1)
486 out << 's';
487 out << " seen";
488 if (suffix) {
489 out << ", " << suffix << " character";
490 if (suffix > 1)
491 out << 's';
492 out << " used as a suffix";
493 }
494 out << ')';
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000495 BR.EmitBasicReport(AC->getDecl(), filter.checkName_mkstemp,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000496 "Insecure temporary file creation", "Security",
Jordan Rose42b42482013-10-07 17:16:59 +0000497 out.str(), CELoc, strArg->getSourceRange());
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000498}
499
Zhongxing Xu39bba622009-12-03 09:15:23 +0000500//===----------------------------------------------------------------------===//
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000501// Check: Any use of 'strcpy' is insecure.
502//
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000503// CWE-119: Improper Restriction of Operations within
504// the Bounds of a Memory Buffer
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000505//===----------------------------------------------------------------------===//
Lenny Maioranide909e42011-04-05 20:18:46 +0000506void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000507 if (!filter.check_strcpy)
508 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000509
Lenny Maioranide909e42011-04-05 20:18:46 +0000510 if (!checkCall_strCommon(CE, FD))
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000511 return;
512
Lenny Maioranide909e42011-04-05 20:18:46 +0000513 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000514 PathDiagnosticLocation CELoc =
515 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000516 BR.EmitBasicReport(AC->getDecl(), filter.checkName_strcpy,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000517 "Potential insecure memory buffer bounds restriction in "
Lenny Maioranide909e42011-04-05 20:18:46 +0000518 "call 'strcpy'",
519 "Security",
520 "Call to function 'strcpy' is insecure as it does not "
Ted Kremenek5a10f082012-04-04 18:11:35 +0000521 "provide bounding of the memory buffer. Replace "
522 "unbounded copy functions with analogous functions that "
523 "support length arguments such as 'strlcpy'. CWE-119.",
Jordan Rose42b42482013-10-07 17:16:59 +0000524 CELoc, CE->getCallee()->getSourceRange());
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000525}
526
527//===----------------------------------------------------------------------===//
Lenny Maioranide909e42011-04-05 20:18:46 +0000528// Check: Any use of 'strcat' is insecure.
529//
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000530// CWE-119: Improper Restriction of Operations within
531// the Bounds of a Memory Buffer
Lenny Maioranide909e42011-04-05 20:18:46 +0000532//===----------------------------------------------------------------------===//
533void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000534 if (!filter.check_strcpy)
535 return;
536
Lenny Maioranide909e42011-04-05 20:18:46 +0000537 if (!checkCall_strCommon(CE, FD))
538 return;
539
540 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000541 PathDiagnosticLocation CELoc =
542 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000543 BR.EmitBasicReport(AC->getDecl(), filter.checkName_strcpy,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000544 "Potential insecure memory buffer bounds restriction in "
545 "call 'strcat'",
546 "Security",
547 "Call to function 'strcat' is insecure as it does not "
548 "provide bounding of the memory buffer. Replace "
549 "unbounded copy functions with analogous functions that "
550 "support length arguments such as 'strlcat'. CWE-119.",
Jordan Rose42b42482013-10-07 17:16:59 +0000551 CELoc, CE->getCallee()->getSourceRange());
Lenny Maioranide909e42011-04-05 20:18:46 +0000552}
553
554//===----------------------------------------------------------------------===//
555// Common check for str* functions with no bounds parameters.
556//===----------------------------------------------------------------------===//
557bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000558 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Lenny Maioranide909e42011-04-05 20:18:46 +0000559 if (!FPT)
560 return false;
561
562 // Verify the function takes two arguments, three in the _chk version.
Alp Toker9cacbab2014-01-20 20:26:09 +0000563 int numArgs = FPT->getNumParams();
Lenny Maioranide909e42011-04-05 20:18:46 +0000564 if (numArgs != 2 && numArgs != 3)
565 return false;
566
567 // Verify the type for both arguments.
568 for (int i = 0; i < 2; i++) {
569 // Verify that the arguments are pointers.
Alp Toker9cacbab2014-01-20 20:26:09 +0000570 const PointerType *PT = FPT->getParamType(i)->getAs<PointerType>();
Lenny Maioranide909e42011-04-05 20:18:46 +0000571 if (!PT)
572 return false;
573
574 // Verify that the argument is a 'char*'.
575 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
576 return false;
577 }
578
579 return true;
580}
581
582//===----------------------------------------------------------------------===//
Ted Kremenekad5a6002009-09-02 02:47:41 +0000583// Check: Linear congruent random number generators should not be used
584// Originally: <rdar://problem/63371000>
585// CWE-338: Use of cryptographically weak prng
586//===----------------------------------------------------------------------===//
587
Lenny Maioranide909e42011-04-05 20:18:46 +0000588void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000589 if (!filter.check_rand || !CheckRand)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000590 return;
Mike Stump11289f42009-09-09 15:08:12 +0000591
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000592 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenekad5a6002009-09-02 02:47:41 +0000593 if (!FTP)
594 return;
Mike Stump11289f42009-09-09 15:08:12 +0000595
Alp Toker9cacbab2014-01-20 20:26:09 +0000596 if (FTP->getNumParams() == 1) {
Ted Kremenekad5a6002009-09-02 02:47:41 +0000597 // Is the argument an 'unsigned short *'?
598 // (Actually any integer type is allowed.)
Alp Toker9cacbab2014-01-20 20:26:09 +0000599 const PointerType *PT = FTP->getParamType(0)->getAs<PointerType>();
Ted Kremenekad5a6002009-09-02 02:47:41 +0000600 if (!PT)
601 return;
Mike Stump11289f42009-09-09 15:08:12 +0000602
Jordan Rose61e221f2013-04-09 02:30:33 +0000603 if (! PT->getPointeeType()->isIntegralOrUnscopedEnumerationType())
Ted Kremenekad5a6002009-09-02 02:47:41 +0000604 return;
Alp Toker9cacbab2014-01-20 20:26:09 +0000605 } else if (FTP->getNumParams() != 0)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000606 return;
Mike Stump11289f42009-09-09 15:08:12 +0000607
Ted Kremenekad5a6002009-09-02 02:47:41 +0000608 // Issue a warning.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000609 SmallString<256> buf1;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000610 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000611 os1 << '\'' << *FD << "' is a poor random number generator";
Ted Kremenekad5a6002009-09-02 02:47:41 +0000612
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000613 SmallString<256> buf2;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000614 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000615 os2 << "Function '" << *FD
Ted Kremenekad5a6002009-09-02 02:47:41 +0000616 << "' is obsolete because it implements a poor random number generator."
617 << " Use 'arc4random' instead";
618
Anna Zaksc29bed32011-09-20 21:38:35 +0000619 PathDiagnosticLocation CELoc =
620 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000621 BR.EmitBasicReport(AC->getDecl(), filter.checkName_rand, os1.str(),
622 "Security", os2.str(), CELoc,
623 CE->getCallee()->getSourceRange());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000624}
625
626//===----------------------------------------------------------------------===//
627// Check: 'random' should not be used
628// Originally: <rdar://problem/63371000>
629//===----------------------------------------------------------------------===//
630
Lenny Maioranide909e42011-04-05 20:18:46 +0000631void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000632 if (!CheckRand || !filter.check_rand)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000633 return;
Mike Stump11289f42009-09-09 15:08:12 +0000634
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000635 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenekad5a6002009-09-02 02:47:41 +0000636 if (!FTP)
637 return;
Mike Stump11289f42009-09-09 15:08:12 +0000638
Ted Kremenekad5a6002009-09-02 02:47:41 +0000639 // Verify that the function takes no argument.
Alp Toker9cacbab2014-01-20 20:26:09 +0000640 if (FTP->getNumParams() != 0)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000641 return;
Mike Stump11289f42009-09-09 15:08:12 +0000642
Ted Kremenekad5a6002009-09-02 02:47:41 +0000643 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000644 PathDiagnosticLocation CELoc =
645 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000646 BR.EmitBasicReport(AC->getDecl(), filter.checkName_rand,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000647 "'random' is not a secure random number generator",
Ted Kremenekad5a6002009-09-02 02:47:41 +0000648 "Security",
649 "The 'random' function produces a sequence of values that "
650 "an adversary may be able to predict. Use 'arc4random' "
Jordan Rose42b42482013-10-07 17:16:59 +0000651 "instead", CELoc, CE->getCallee()->getSourceRange());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000652}
653
654//===----------------------------------------------------------------------===//
Anna Zaksfedf5df2011-10-11 04:34:54 +0000655// Check: 'vfork' should not be used.
656// POS33-C: Do not use vfork().
657//===----------------------------------------------------------------------===//
658
659void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000660 if (!filter.check_vfork)
661 return;
662
Anna Zaksfedf5df2011-10-11 04:34:54 +0000663 // All calls to vfork() are insecure, issue a warning.
Anna Zaksfedf5df2011-10-11 04:34:54 +0000664 PathDiagnosticLocation CELoc =
665 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000666 BR.EmitBasicReport(AC->getDecl(), filter.checkName_vfork,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000667 "Potential insecure implementation-specific behavior in "
Anna Zaksfedf5df2011-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",
Jordan Rose42b42482013-10-07 17:16:59 +0000674 CELoc, CE->getCallee()->getSourceRange());
Anna Zaksfedf5df2011-10-11 04:34:54 +0000675}
676
677//===----------------------------------------------------------------------===//
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000678// Check: Should check whether privileges are dropped successfully.
679// Originally: <rdar://problem/6337132>
680//===----------------------------------------------------------------------===//
681
Lenny Maioranide909e42011-04-05 20:18:46 +0000682void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000683 if (!filter.check_UncheckedReturn)
684 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000685
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000686 const FunctionDecl *FD = CE->getDirectCallee();
687 if (!FD)
688 return;
689
Craig Topper0dbb7832014-05-27 02:45:47 +0000690 if (II_setid[0] == nullptr) {
Ted Kremenekad5a6002009-09-02 02:47:41 +0000691 static const char * const identifiers[num_setids] = {
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000692 "setuid", "setgid", "seteuid", "setegid",
693 "setreuid", "setregid"
694 };
Mike Stump11289f42009-09-09 15:08:12 +0000695
Ted Kremenekad5a6002009-09-02 02:47:41 +0000696 for (size_t i = 0; i < num_setids; i++)
Mike Stump11289f42009-09-09 15:08:12 +0000697 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000698 }
Mike Stump11289f42009-09-09 15:08:12 +0000699
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000700 const IdentifierInfo *id = FD->getIdentifier();
701 size_t identifierid;
702
Ted Kremenekad5a6002009-09-02 02:47:41 +0000703 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000704 if (id == II_setid[identifierid])
705 break;
706
Ted Kremenekad5a6002009-09-02 02:47:41 +0000707 if (identifierid >= num_setids)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000708 return;
Mike Stump11289f42009-09-09 15:08:12 +0000709
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000710 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000711 if (!FTP)
712 return;
Mike Stump11289f42009-09-09 15:08:12 +0000713
Ted Kremenekaeca0952009-08-28 00:24:55 +0000714 // Verify that the function takes one or two arguments (depending on
715 // the function).
Alp Toker9cacbab2014-01-20 20:26:09 +0000716 if (FTP->getNumParams() != (identifierid < 4 ? 1 : 2))
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000717 return;
718
719 // The arguments must be integers.
Alp Toker9cacbab2014-01-20 20:26:09 +0000720 for (unsigned i = 0; i < FTP->getNumParams(); i++)
721 if (!FTP->getParamType(i)->isIntegralOrUnscopedEnumerationType())
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000722 return;
723
724 // Issue a warning.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000725 SmallString<256> buf1;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000726 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000727 os1 << "Return value is not checked in call to '" << *FD << '\'';
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000728
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000729 SmallString<256> buf2;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000730 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb89514a2011-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 Kremenekd032fcc2009-08-28 00:08:09 +0000733 << "', the following code may execute with unexpected privileges";
734
Anna Zaksc29bed32011-09-20 21:38:35 +0000735 PathDiagnosticLocation CELoc =
736 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000737 BR.EmitBasicReport(AC->getDecl(), filter.checkName_UncheckedReturn, os1.str(),
738 "Security", os2.str(), CELoc,
739 CE->getCallee()->getSourceRange());
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000740}
741
742//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000743// SecuritySyntaxChecker
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000744//===----------------------------------------------------------------------===//
745
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000746namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000747class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000748public:
Ted Kremenekc54dc952012-01-20 01:44:29 +0000749 ChecksFilter filter;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000750
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000751 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
752 BugReporter &BR) const {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000753 WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000754 walker.Visit(D->getBody());
755 }
756};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000757}
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000758
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000759#define REGISTER_CHECKER(name) \
760 void ento::register##name(CheckerManager &mgr) { \
761 SecuritySyntaxChecker *checker = \
762 mgr.registerChecker<SecuritySyntaxChecker>(); \
763 checker->filter.check_##name = true; \
764 checker->filter.checkName_##name = mgr.getCurrentCheckName(); \
765 }
Ted Kremenekc54dc952012-01-20 01:44:29 +0000766
767REGISTER_CHECKER(gets)
768REGISTER_CHECKER(getpw)
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000769REGISTER_CHECKER(mkstemp)
Ted Kremenekc54dc952012-01-20 01:44:29 +0000770REGISTER_CHECKER(mktemp)
771REGISTER_CHECKER(strcpy)
772REGISTER_CHECKER(rand)
773REGISTER_CHECKER(vfork)
774REGISTER_CHECKER(FloatLoopCounter)
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000775REGISTER_CHECKER(UncheckedReturn)
776
Ted Kremenekc54dc952012-01-20 01:44:29 +0000777