blob: 62831be26eb2e6f8fe00def292e18b2d3ba8d9a4 [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
Artem Dergachev1ebd1c42018-01-12 22:12:11 +0000513 const auto *Target = CE->getArg(0)->IgnoreImpCasts(),
514 *Source = CE->getArg(1)->IgnoreImpCasts();
515 if (const auto *DeclRef = dyn_cast<DeclRefExpr>(Target))
516 if (const auto *Array = dyn_cast<ConstantArrayType>(DeclRef->getType())) {
517 uint64_t ArraySize = BR.getContext().getTypeSize(Array) / 8;
518 if (const auto *String = dyn_cast<StringLiteral>(Source)) {
519 if (ArraySize >= String->getLength() + 1)
520 return;
521 }
522 }
523
Lenny Maioranide909e42011-04-05 20:18:46 +0000524 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000525 PathDiagnosticLocation CELoc =
526 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000527 BR.EmitBasicReport(AC->getDecl(), filter.checkName_strcpy,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000528 "Potential insecure memory buffer bounds restriction in "
Lenny Maioranide909e42011-04-05 20:18:46 +0000529 "call 'strcpy'",
530 "Security",
531 "Call to function 'strcpy' is insecure as it does not "
Ted Kremenek5a10f082012-04-04 18:11:35 +0000532 "provide bounding of the memory buffer. Replace "
533 "unbounded copy functions with analogous functions that "
534 "support length arguments such as 'strlcpy'. CWE-119.",
Jordan Rose42b42482013-10-07 17:16:59 +0000535 CELoc, CE->getCallee()->getSourceRange());
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000536}
537
538//===----------------------------------------------------------------------===//
Lenny Maioranide909e42011-04-05 20:18:46 +0000539// Check: Any use of 'strcat' is insecure.
540//
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000541// CWE-119: Improper Restriction of Operations within
542// the Bounds of a Memory Buffer
Lenny Maioranide909e42011-04-05 20:18:46 +0000543//===----------------------------------------------------------------------===//
544void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000545 if (!filter.check_strcpy)
546 return;
547
Lenny Maioranide909e42011-04-05 20:18:46 +0000548 if (!checkCall_strCommon(CE, FD))
549 return;
550
551 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000552 PathDiagnosticLocation CELoc =
553 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000554 BR.EmitBasicReport(AC->getDecl(), filter.checkName_strcpy,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000555 "Potential insecure memory buffer bounds restriction in "
556 "call 'strcat'",
557 "Security",
558 "Call to function 'strcat' is insecure as it does not "
559 "provide bounding of the memory buffer. Replace "
560 "unbounded copy functions with analogous functions that "
561 "support length arguments such as 'strlcat'. CWE-119.",
Jordan Rose42b42482013-10-07 17:16:59 +0000562 CELoc, CE->getCallee()->getSourceRange());
Lenny Maioranide909e42011-04-05 20:18:46 +0000563}
564
565//===----------------------------------------------------------------------===//
566// Common check for str* functions with no bounds parameters.
567//===----------------------------------------------------------------------===//
568bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000569 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
Lenny Maioranide909e42011-04-05 20:18:46 +0000570 if (!FPT)
571 return false;
572
573 // Verify the function takes two arguments, three in the _chk version.
Alp Toker9cacbab2014-01-20 20:26:09 +0000574 int numArgs = FPT->getNumParams();
Lenny Maioranide909e42011-04-05 20:18:46 +0000575 if (numArgs != 2 && numArgs != 3)
576 return false;
577
578 // Verify the type for both arguments.
579 for (int i = 0; i < 2; i++) {
580 // Verify that the arguments are pointers.
Alp Toker9cacbab2014-01-20 20:26:09 +0000581 const PointerType *PT = FPT->getParamType(i)->getAs<PointerType>();
Lenny Maioranide909e42011-04-05 20:18:46 +0000582 if (!PT)
583 return false;
584
585 // Verify that the argument is a 'char*'.
586 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
587 return false;
588 }
589
590 return true;
591}
592
593//===----------------------------------------------------------------------===//
Ted Kremenekad5a6002009-09-02 02:47:41 +0000594// Check: Linear congruent random number generators should not be used
595// Originally: <rdar://problem/63371000>
596// CWE-338: Use of cryptographically weak prng
597//===----------------------------------------------------------------------===//
598
Lenny Maioranide909e42011-04-05 20:18:46 +0000599void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000600 if (!filter.check_rand || !CheckRand)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000601 return;
Mike Stump11289f42009-09-09 15:08:12 +0000602
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000603 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenekad5a6002009-09-02 02:47:41 +0000604 if (!FTP)
605 return;
Mike Stump11289f42009-09-09 15:08:12 +0000606
Alp Toker9cacbab2014-01-20 20:26:09 +0000607 if (FTP->getNumParams() == 1) {
Ted Kremenekad5a6002009-09-02 02:47:41 +0000608 // Is the argument an 'unsigned short *'?
609 // (Actually any integer type is allowed.)
Alp Toker9cacbab2014-01-20 20:26:09 +0000610 const PointerType *PT = FTP->getParamType(0)->getAs<PointerType>();
Ted Kremenekad5a6002009-09-02 02:47:41 +0000611 if (!PT)
612 return;
Mike Stump11289f42009-09-09 15:08:12 +0000613
Jordan Rose61e221f2013-04-09 02:30:33 +0000614 if (! PT->getPointeeType()->isIntegralOrUnscopedEnumerationType())
Ted Kremenekad5a6002009-09-02 02:47:41 +0000615 return;
Alp Toker9cacbab2014-01-20 20:26:09 +0000616 } else if (FTP->getNumParams() != 0)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000617 return;
Mike Stump11289f42009-09-09 15:08:12 +0000618
Ted Kremenekad5a6002009-09-02 02:47:41 +0000619 // Issue a warning.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000620 SmallString<256> buf1;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000621 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000622 os1 << '\'' << *FD << "' is a poor random number generator";
Ted Kremenekad5a6002009-09-02 02:47:41 +0000623
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000624 SmallString<256> buf2;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000625 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000626 os2 << "Function '" << *FD
Ted Kremenekad5a6002009-09-02 02:47:41 +0000627 << "' is obsolete because it implements a poor random number generator."
628 << " Use 'arc4random' instead";
629
Anna Zaksc29bed32011-09-20 21:38:35 +0000630 PathDiagnosticLocation CELoc =
631 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000632 BR.EmitBasicReport(AC->getDecl(), filter.checkName_rand, os1.str(),
633 "Security", os2.str(), CELoc,
634 CE->getCallee()->getSourceRange());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000635}
636
637//===----------------------------------------------------------------------===//
638// Check: 'random' should not be used
639// Originally: <rdar://problem/63371000>
640//===----------------------------------------------------------------------===//
641
Lenny Maioranide909e42011-04-05 20:18:46 +0000642void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000643 if (!CheckRand || !filter.check_rand)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000644 return;
Mike Stump11289f42009-09-09 15:08:12 +0000645
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000646 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenekad5a6002009-09-02 02:47:41 +0000647 if (!FTP)
648 return;
Mike Stump11289f42009-09-09 15:08:12 +0000649
Ted Kremenekad5a6002009-09-02 02:47:41 +0000650 // Verify that the function takes no argument.
Alp Toker9cacbab2014-01-20 20:26:09 +0000651 if (FTP->getNumParams() != 0)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000652 return;
Mike Stump11289f42009-09-09 15:08:12 +0000653
Ted Kremenekad5a6002009-09-02 02:47:41 +0000654 // Issue a warning.
Anna Zaksc29bed32011-09-20 21:38:35 +0000655 PathDiagnosticLocation CELoc =
656 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000657 BR.EmitBasicReport(AC->getDecl(), filter.checkName_rand,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000658 "'random' is not a secure random number generator",
Ted Kremenekad5a6002009-09-02 02:47:41 +0000659 "Security",
660 "The 'random' function produces a sequence of values that "
661 "an adversary may be able to predict. Use 'arc4random' "
Jordan Rose42b42482013-10-07 17:16:59 +0000662 "instead", CELoc, CE->getCallee()->getSourceRange());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000663}
664
665//===----------------------------------------------------------------------===//
Anna Zaksfedf5df2011-10-11 04:34:54 +0000666// Check: 'vfork' should not be used.
667// POS33-C: Do not use vfork().
668//===----------------------------------------------------------------------===//
669
670void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000671 if (!filter.check_vfork)
672 return;
673
Anna Zaksfedf5df2011-10-11 04:34:54 +0000674 // All calls to vfork() are insecure, issue a warning.
Anna Zaksfedf5df2011-10-11 04:34:54 +0000675 PathDiagnosticLocation CELoc =
676 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000677 BR.EmitBasicReport(AC->getDecl(), filter.checkName_vfork,
Ted Kremenek5a10f082012-04-04 18:11:35 +0000678 "Potential insecure implementation-specific behavior in "
Anna Zaksfedf5df2011-10-11 04:34:54 +0000679 "call 'vfork'",
680 "Security",
681 "Call to function 'vfork' is insecure as it can lead to "
682 "denial of service situations in the parent process. "
683 "Replace calls to vfork with calls to the safer "
684 "'posix_spawn' function",
Jordan Rose42b42482013-10-07 17:16:59 +0000685 CELoc, CE->getCallee()->getSourceRange());
Anna Zaksfedf5df2011-10-11 04:34:54 +0000686}
687
688//===----------------------------------------------------------------------===//
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000689// Check: Should check whether privileges are dropped successfully.
690// Originally: <rdar://problem/6337132>
691//===----------------------------------------------------------------------===//
692
Lenny Maioranide909e42011-04-05 20:18:46 +0000693void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000694 if (!filter.check_UncheckedReturn)
695 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000696
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000697 const FunctionDecl *FD = CE->getDirectCallee();
698 if (!FD)
699 return;
700
Craig Topper0dbb7832014-05-27 02:45:47 +0000701 if (II_setid[0] == nullptr) {
Ted Kremenekad5a6002009-09-02 02:47:41 +0000702 static const char * const identifiers[num_setids] = {
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000703 "setuid", "setgid", "seteuid", "setegid",
704 "setreuid", "setregid"
705 };
Mike Stump11289f42009-09-09 15:08:12 +0000706
Ted Kremenekad5a6002009-09-02 02:47:41 +0000707 for (size_t i = 0; i < num_setids; i++)
Mike Stump11289f42009-09-09 15:08:12 +0000708 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000709 }
Mike Stump11289f42009-09-09 15:08:12 +0000710
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000711 const IdentifierInfo *id = FD->getIdentifier();
712 size_t identifierid;
713
Ted Kremenekad5a6002009-09-02 02:47:41 +0000714 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000715 if (id == II_setid[identifierid])
716 break;
717
Ted Kremenekad5a6002009-09-02 02:47:41 +0000718 if (identifierid >= num_setids)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000719 return;
Mike Stump11289f42009-09-09 15:08:12 +0000720
Eli Friedman57d1f1c2013-06-24 18:47:11 +0000721 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000722 if (!FTP)
723 return;
Mike Stump11289f42009-09-09 15:08:12 +0000724
Ted Kremenekaeca0952009-08-28 00:24:55 +0000725 // Verify that the function takes one or two arguments (depending on
726 // the function).
Alp Toker9cacbab2014-01-20 20:26:09 +0000727 if (FTP->getNumParams() != (identifierid < 4 ? 1 : 2))
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000728 return;
729
730 // The arguments must be integers.
Alp Toker9cacbab2014-01-20 20:26:09 +0000731 for (unsigned i = 0; i < FTP->getNumParams(); i++)
732 if (!FTP->getParamType(i)->isIntegralOrUnscopedEnumerationType())
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000733 return;
734
735 // Issue a warning.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000736 SmallString<256> buf1;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000737 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000738 os1 << "Return value is not checked in call to '" << *FD << '\'';
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000739
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000740 SmallString<256> buf2;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000741 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000742 os2 << "The return value from the call to '" << *FD
743 << "' is not checked. If an error occurs in '" << *FD
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000744 << "', the following code may execute with unexpected privileges";
745
Anna Zaksc29bed32011-09-20 21:38:35 +0000746 PathDiagnosticLocation CELoc =
747 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000748 BR.EmitBasicReport(AC->getDecl(), filter.checkName_UncheckedReturn, os1.str(),
749 "Security", os2.str(), CELoc,
750 CE->getCallee()->getSourceRange());
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000751}
752
753//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000754// SecuritySyntaxChecker
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000755//===----------------------------------------------------------------------===//
756
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000757namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000758class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000759public:
Ted Kremenekc54dc952012-01-20 01:44:29 +0000760 ChecksFilter filter;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000761
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000762 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
763 BugReporter &BR) const {
Ted Kremenekc54dc952012-01-20 01:44:29 +0000764 WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000765 walker.Visit(D->getBody());
766 }
767};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000768}
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000769
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000770#define REGISTER_CHECKER(name) \
771 void ento::register##name(CheckerManager &mgr) { \
772 SecuritySyntaxChecker *checker = \
773 mgr.registerChecker<SecuritySyntaxChecker>(); \
774 checker->filter.check_##name = true; \
775 checker->filter.checkName_##name = mgr.getCurrentCheckName(); \
776 }
Ted Kremenekc54dc952012-01-20 01:44:29 +0000777
778REGISTER_CHECKER(gets)
779REGISTER_CHECKER(getpw)
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000780REGISTER_CHECKER(mkstemp)
Ted Kremenekc54dc952012-01-20 01:44:29 +0000781REGISTER_CHECKER(mktemp)
782REGISTER_CHECKER(strcpy)
783REGISTER_CHECKER(rand)
784REGISTER_CHECKER(vfork)
785REGISTER_CHECKER(FloatLoopCounter)
Ted Kremenek89eaf8d2012-01-20 05:35:06 +0000786REGISTER_CHECKER(UncheckedReturn)
787
Ted Kremenekc54dc952012-01-20 01:44:29 +0000788