blob: 6f55d164a7769b39c69c1c99e364ac6d5ea7c6e3 [file] [log] [blame]
Ted Kremenekdbfb5f82009-07-23 01:07:19 +00001//==- CheckSecuritySyntaxOnly.cpp - Basic security checks --------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a set of flow-insensitive security checks.
11//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +000014#include "ClangSACheckers.h"
Anna Zaks590dd8e2011-09-20 21:38:35 +000015#include "clang/Analysis/AnalysisContext.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/Basic/TargetInfo.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Anna Zaks590dd8e2011-09-20 21:38:35 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Lenny Maioranic2dace12011-04-03 05:07:11 +000021#include "llvm/ADT/StringSwitch.h"
Anna Zaks590dd8e2011-09-20 21:38:35 +000022#include "llvm/Support/raw_ostream.h"
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000023
24using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000025using namespace ento;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000026
Ted Kremenek88c8bc82010-01-15 08:20:31 +000027static bool isArc4RandomAvailable(const ASTContext &Ctx) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000028 const llvm::Triple &T = Ctx.getTargetInfo().getTriple();
Ted Kremenek88c8bc82010-01-15 08:20:31 +000029 return T.getVendor() == llvm::Triple::Apple ||
Douglas Gregor0f565592011-01-17 19:16:24 +000030 T.getOS() == llvm::Triple::FreeBSD ||
31 T.getOS() == llvm::Triple::NetBSD ||
32 T.getOS() == llvm::Triple::OpenBSD ||
33 T.getOS() == llvm::Triple::DragonFly;
Ted Kremenek88c8bc82010-01-15 08:20:31 +000034}
35
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000036namespace {
Ted Kremenek76a54242012-01-20 01:44:29 +000037struct DefaultBool {
38 bool val;
39 DefaultBool() : val(false) {}
40 operator bool() const { return val; }
41 DefaultBool &operator=(bool b) { val = b; return *this; }
42};
43
44struct ChecksFilter {
45 DefaultBool check_gets;
46 DefaultBool check_getpw;
47 DefaultBool check_mktemp;
48 DefaultBool check_strcpy;
49 DefaultBool check_rand;
50 DefaultBool check_vfork;
51 DefaultBool check_FloatLoopCounter;
52};
53
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000054class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump1eb44332009-09-09 15:08:12 +000055 BugReporter &BR;
Ted Kremenek1d26f482011-10-24 01:32:45 +000056 AnalysisDeclContext* AC;
Ted Kremenek24650472009-09-02 02:47:41 +000057 enum { num_setids = 6 };
58 IdentifierInfo *II_setid[num_setids];
Ted Kremenek2c016762010-03-24 22:39:47 +000059
Ted Kremenek88c8bc82010-01-15 08:20:31 +000060 const bool CheckRand;
Ted Kremenek76a54242012-01-20 01:44:29 +000061 const ChecksFilter &filter;
Mike Stump1eb44332009-09-09 15:08:12 +000062
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000063public:
Ted Kremenek76a54242012-01-20 01:44:29 +000064 WalkAST(BugReporter &br, AnalysisDeclContext* ac,
65 const ChecksFilter &f)
Anna Zaks590dd8e2011-09-20 21:38:35 +000066 : BR(br), AC(ac), II_setid(),
Ted Kremenek76a54242012-01-20 01:44:29 +000067 CheckRand(isArc4RandomAvailable(BR.getContext())),
68 filter(f) {}
Mike Stump1eb44332009-09-09 15:08:12 +000069
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000070 // Statement visitor methods.
Ted Kremenekefcbb152009-07-23 22:29:41 +000071 void VisitCallExpr(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000072 void VisitForStmt(ForStmt *S);
Ted Kremenek65a81a92009-08-28 00:08:09 +000073 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek8baf86d2009-07-23 21:34:35 +000074 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000075
76 void VisitChildren(Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +000077
Ted Kremenekefcbb152009-07-23 22:29:41 +000078 // Helpers.
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000079 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +000080
Lenny Maioranic2dace12011-04-03 05:07:11 +000081 typedef void (WalkAST::*FnCheck)(const CallExpr *,
82 const FunctionDecl *);
83
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000084 // Checker-specific methods.
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000085 void checkLoopConditionForFloat(const ForStmt *FS);
86 void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
87 void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
88 void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
89 void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
90 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
91 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
92 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
Anna Zaksa7957ff2011-10-11 04:34:54 +000093 void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000094 void checkUncheckedReturnValue(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000095};
96} // end anonymous namespace
97
98//===----------------------------------------------------------------------===//
99// AST walking.
100//===----------------------------------------------------------------------===//
101
102void WalkAST::VisitChildren(Stmt *S) {
103 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
104 if (Stmt *child = *I)
105 Visit(child);
106}
107
Ted Kremenekefcbb152009-07-23 22:29:41 +0000108void WalkAST::VisitCallExpr(CallExpr *CE) {
Lenny Maioranic2dace12011-04-03 05:07:11 +0000109 // Get the callee.
110 const FunctionDecl *FD = CE->getDirectCallee();
111
112 if (!FD)
113 return;
114
115 // Get the name of the callee. If it's a builtin, strip off the prefix.
116 IdentifierInfo *II = FD->getIdentifier();
117 if (!II) // if no identifier, not a simple C function
118 return;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000119 StringRef Name = II->getName();
Lenny Maioranic2dace12011-04-03 05:07:11 +0000120 if (Name.startswith("__builtin_"))
121 Name = Name.substr(10);
122
123 // Set the evaluation function by switching on the callee name.
124 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000125 .Case("gets", &WalkAST::checkCall_gets)
126 .Case("getpw", &WalkAST::checkCall_getpw)
127 .Case("mktemp", &WalkAST::checkCall_mktemp)
128 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
129 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
130 .Case("drand48", &WalkAST::checkCall_rand)
131 .Case("erand48", &WalkAST::checkCall_rand)
132 .Case("jrand48", &WalkAST::checkCall_rand)
133 .Case("lrand48", &WalkAST::checkCall_rand)
134 .Case("mrand48", &WalkAST::checkCall_rand)
135 .Case("nrand48", &WalkAST::checkCall_rand)
136 .Case("lcong48", &WalkAST::checkCall_rand)
137 .Case("rand", &WalkAST::checkCall_rand)
138 .Case("rand_r", &WalkAST::checkCall_rand)
139 .Case("random", &WalkAST::checkCall_random)
Anna Zaksa7957ff2011-10-11 04:34:54 +0000140 .Case("vfork", &WalkAST::checkCall_vfork)
Lenny Maioranic2dace12011-04-03 05:07:11 +0000141 .Default(NULL);
142
143 // If the callee isn't defined, it is not of security concern.
144 // Check and evaluate the call.
145 if (evalFunction)
146 (this->*evalFunction)(CE, FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Ted Kremenekefcbb152009-07-23 22:29:41 +0000148 // Recurse and check children.
149 VisitChildren(CE);
150}
151
Ted Kremenek65a81a92009-08-28 00:08:09 +0000152void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
153 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000154 if (Stmt *child = *I) {
155 if (CallExpr *CE = dyn_cast<CallExpr>(child))
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000156 checkUncheckedReturnValue(CE);
Mike Stump1eb44332009-09-09 15:08:12 +0000157 Visit(child);
158 }
Ted Kremenek65a81a92009-08-28 00:08:09 +0000159}
160
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000161void WalkAST::VisitForStmt(ForStmt *FS) {
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000162 checkLoopConditionForFloat(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000163
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000164 // Recurse and check children.
165 VisitChildren(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000166}
167
168//===----------------------------------------------------------------------===//
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000169// Check: floating poing variable used as loop counter.
Ted Kremenek5abeb522009-07-23 21:44:18 +0000170// Originally: <rdar://problem/6336718>
171// Implements: CERT security coding advisory FLP-30.
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000172//===----------------------------------------------------------------------===//
173
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000174static const DeclRefExpr*
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000175getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000176 expr = expr->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000177
178 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000179 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCall2de56d12010-08-25 11:45:40 +0000180 B->getOpcode() == BO_Comma))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000181 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000183 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000184 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000186 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000187 return rhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000189 return NULL;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000192 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
193 const NamedDecl *ND = DR->getDecl();
194 return ND == x || ND == y ? DR : NULL;
195 }
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000197 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
198 return U->isIncrementDecrementOp()
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000199 ? getIncrementedVar(U->getSubExpr(), x, y) : NULL;
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000200
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000201 return NULL;
202}
203
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000204/// CheckLoopConditionForFloat - This check looks for 'for' statements that
205/// use a floating point variable as a loop counter.
206/// CERT: FLP30-C, FLP30-CPP.
207///
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000208void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000209 if (!filter.check_FloatLoopCounter)
210 return;
211
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000212 // Does the loop have a condition?
213 const Expr *condition = FS->getCond();
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000215 if (!condition)
216 return;
217
218 // Does the loop have an increment?
219 const Expr *increment = FS->getInc();
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000221 if (!increment)
222 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000224 // Strip away '()' and casts.
225 condition = condition->IgnoreParenCasts();
226 increment = increment->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000228 // Is the loop condition a comparison?
229 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
230
231 if (!B)
232 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Ted Kremenekcad9f412009-07-24 20:26:31 +0000234 // Is this a comparison?
235 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000236 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000238 // Are we comparing variables?
John McCallf6a16482010-12-04 03:47:34 +0000239 const DeclRefExpr *drLHS =
240 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
241 const DeclRefExpr *drRHS =
242 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Ted Kremenekcad9f412009-07-24 20:26:31 +0000244 // Does at least one of the variables have a floating point type?
Douglas Gregor0c293ea2010-06-22 23:07:26 +0000245 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
246 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000248 if (!drLHS && !drRHS)
249 return;
250
251 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
252 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000254 if (!vdLHS && !vdRHS)
Mike Stump1eb44332009-09-09 15:08:12 +0000255 return;
256
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000257 // Does either variable appear in increment?
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000258 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000260 if (!drInc)
261 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000263 // Emit the error. First figure out which DeclRefExpr in the condition
264 // referenced the compared variable.
265 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
266
Chris Lattner5f9e2722011-07-23 10:55:15 +0000267 SmallVector<SourceRange, 2> ranges;
Ted Kremenek2c016762010-03-24 22:39:47 +0000268 llvm::SmallString<256> sbuf;
269 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Daniel Dunbar4087f272010-08-17 22:39:59 +0000271 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000272 << "' with floating point type '" << drCond->getType().getAsString()
273 << "' should not be used as a loop counter";
274
275 ranges.push_back(drCond->getSourceRange());
276 ranges.push_back(drInc->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000278 const char *bugType = "Floating point variable used as loop counter";
Anna Zaks590dd8e2011-09-20 21:38:35 +0000279
280 PathDiagnosticLocation FSLoc =
281 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
Benjamin Kramerf0171732009-11-29 18:27:55 +0000282 BR.EmitBasicReport(bugType, "Security", os.str(),
Anna Zaks590dd8e2011-09-20 21:38:35 +0000283 FSLoc, ranges.data(), ranges.size());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000284}
285
286//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +0000287// Check: Any use of 'gets' is insecure.
288// Originally: <rdar://problem/6335715>
289// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuaa30b3b2009-11-09 08:13:04 +0000290// CWE-242: Use of Inherently Dangerous Function
Ted Kremenekefcbb152009-07-23 22:29:41 +0000291//===----------------------------------------------------------------------===//
292
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000293void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000294 if (!filter.check_gets)
295 return;
296
Abramo Bagnara723df242010-12-14 22:11:44 +0000297 const FunctionProtoType *FPT
298 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000299 if (!FPT)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000300 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Ted Kremenekefcbb152009-07-23 22:29:41 +0000302 // Verify that the function takes a single argument.
Zhongxing Xubd842e32009-11-09 12:19:26 +0000303 if (FPT->getNumArgs() != 1)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000304 return;
305
306 // Is the argument a 'char*'?
Zhongxing Xubd842e32009-11-09 12:19:26 +0000307 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenekefcbb152009-07-23 22:29:41 +0000308 if (!PT)
309 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Ted Kremenekefcbb152009-07-23 22:29:41 +0000311 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
312 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenekefcbb152009-07-23 22:29:41 +0000314 // Issue a warning.
315 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000316 PathDiagnosticLocation CELoc =
317 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenekefcbb152009-07-23 22:29:41 +0000318 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
319 "Security",
320 "Call to function 'gets' is extremely insecure as it can "
321 "always result in a buffer overflow",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000322 CELoc, &R, 1);
Ted Kremenekefcbb152009-07-23 22:29:41 +0000323}
324
325//===----------------------------------------------------------------------===//
Zhongxing Xubd842e32009-11-09 12:19:26 +0000326// Check: Any use of 'getpwd' is insecure.
327// CWE-477: Use of Obsolete Functions
328//===----------------------------------------------------------------------===//
329
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000330void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000331 if (!filter.check_getpw)
332 return;
333
Abramo Bagnara723df242010-12-14 22:11:44 +0000334 const FunctionProtoType *FPT
335 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000336 if (!FPT)
337 return;
338
339 // Verify that the function takes two arguments.
340 if (FPT->getNumArgs() != 2)
341 return;
342
343 // Verify the first argument type is integer.
344 if (!FPT->getArgType(0)->isIntegerType())
345 return;
346
347 // Verify the second argument type is char*.
348 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
349 if (!PT)
350 return;
351
352 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
353 return;
354
355 // Issue a warning.
356 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000357 PathDiagnosticLocation CELoc =
358 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Zhongxing Xubd842e32009-11-09 12:19:26 +0000359 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
360 "Security",
361 "The getpw() function is dangerous as it may overflow the "
362 "provided buffer. It is obsoleted by getpwuid().",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000363 CELoc, &R, 1);
Zhongxing Xubd842e32009-11-09 12:19:26 +0000364}
365
366//===----------------------------------------------------------------------===//
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000367// Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
368// CWE-377: Insecure Temporary File
369//===----------------------------------------------------------------------===//
370
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000371void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000372 if (!filter.check_mktemp)
373 return;
374
Abramo Bagnara723df242010-12-14 22:11:44 +0000375 const FunctionProtoType *FPT
376 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000377 if(!FPT)
378 return;
Ted Kremenek2c016762010-03-24 22:39:47 +0000379
Lenny Maioraniea4411e2011-03-31 21:26:55 +0000380 // Verify that the function takes a single argument.
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000381 if (FPT->getNumArgs() != 1)
382 return;
383
384 // Verify that the argument is Pointer Type.
385 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
386 if (!PT)
387 return;
388
389 // Verify that the argument is a 'char*'.
390 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
391 return;
Ted Kremenek431a2cb2010-03-24 22:39:45 +0000392
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000393 // Issue a waring.
394 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000395 PathDiagnosticLocation CELoc =
396 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000397 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
Eli Friedmana7e68452010-08-22 01:00:03 +0000398 "Security",
399 "Call to function 'mktemp' is insecure as it always "
400 "creates or uses insecure temporary file. Use 'mkstemp' instead",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000401 CELoc, &R, 1);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000402}
403
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000404//===----------------------------------------------------------------------===//
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000405// Check: Any use of 'strcpy' is insecure.
406//
407// CWE-119: Improper Restriction of Operations within
408// the Bounds of a Memory Buffer
409//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000410void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000411 if (!filter.check_strcpy)
412 return;
413
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000414 if (!checkCall_strCommon(CE, FD))
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000415 return;
416
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000417 // Issue a warning.
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000418 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000419 PathDiagnosticLocation CELoc =
420 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000421 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000422 "call 'strcpy'",
423 "Security",
424 "Call to function 'strcpy' is insecure as it does not "
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000425 "provide bounding of the memory buffer. Replace "
426 "unbounded copy functions with analogous functions that "
427 "support length arguments such as 'strncpy'. CWE-119.",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000428 CELoc, &R, 1);
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000429}
430
431//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000432// Check: Any use of 'strcat' is insecure.
433//
434// CWE-119: Improper Restriction of Operations within
435// the Bounds of a Memory Buffer
436//===----------------------------------------------------------------------===//
437void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000438 if (!filter.check_strcpy)
439 return;
440
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000441 if (!checkCall_strCommon(CE, FD))
442 return;
443
444 // Issue a warning.
445 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000446 PathDiagnosticLocation CELoc =
447 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000448 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
449 "call 'strcat'",
450 "Security",
451 "Call to function 'strcat' is insecure as it does not "
452 "provide bounding of the memory buffer. Replace "
453 "unbounded copy functions with analogous functions that "
454 "support length arguments such as 'strncat'. CWE-119.",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000455 CELoc, &R, 1);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000456}
457
458//===----------------------------------------------------------------------===//
459// Common check for str* functions with no bounds parameters.
460//===----------------------------------------------------------------------===//
461bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
462 const FunctionProtoType *FPT
463 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
464 if (!FPT)
465 return false;
466
467 // Verify the function takes two arguments, three in the _chk version.
468 int numArgs = FPT->getNumArgs();
469 if (numArgs != 2 && numArgs != 3)
470 return false;
471
472 // Verify the type for both arguments.
473 for (int i = 0; i < 2; i++) {
474 // Verify that the arguments are pointers.
475 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
476 if (!PT)
477 return false;
478
479 // Verify that the argument is a 'char*'.
480 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
481 return false;
482 }
483
484 return true;
485}
486
487//===----------------------------------------------------------------------===//
Ted Kremenek24650472009-09-02 02:47:41 +0000488// Check: Linear congruent random number generators should not be used
489// Originally: <rdar://problem/63371000>
490// CWE-338: Use of cryptographically weak prng
491//===----------------------------------------------------------------------===//
492
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000493void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000494 if (!filter.check_rand || !CheckRand)
Ted Kremenek24650472009-09-02 02:47:41 +0000495 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Abramo Bagnara723df242010-12-14 22:11:44 +0000497 const FunctionProtoType *FTP
498 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000499 if (!FTP)
500 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Ted Kremenek24650472009-09-02 02:47:41 +0000502 if (FTP->getNumArgs() == 1) {
503 // Is the argument an 'unsigned short *'?
504 // (Actually any integer type is allowed.)
505 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
506 if (!PT)
507 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Ted Kremenek24650472009-09-02 02:47:41 +0000509 if (! PT->getPointeeType()->isIntegerType())
510 return;
511 }
Mike Stump1eb44332009-09-09 15:08:12 +0000512 else if (FTP->getNumArgs() != 0)
Ted Kremenek24650472009-09-02 02:47:41 +0000513 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Ted Kremenek24650472009-09-02 02:47:41 +0000515 // Issue a warning.
Ted Kremenek2c016762010-03-24 22:39:47 +0000516 llvm::SmallString<256> buf1;
517 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000518 os1 << '\'' << *FD << "' is a poor random number generator";
Ted Kremenek24650472009-09-02 02:47:41 +0000519
Ted Kremenek2c016762010-03-24 22:39:47 +0000520 llvm::SmallString<256> buf2;
521 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000522 os2 << "Function '" << *FD
Ted Kremenek24650472009-09-02 02:47:41 +0000523 << "' is obsolete because it implements a poor random number generator."
524 << " Use 'arc4random' instead";
525
526 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000527 PathDiagnosticLocation CELoc =
528 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
529 BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000530}
531
532//===----------------------------------------------------------------------===//
533// Check: 'random' should not be used
534// Originally: <rdar://problem/63371000>
535//===----------------------------------------------------------------------===//
536
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000537void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Lenny Maioranic2dace12011-04-03 05:07:11 +0000538 if (!CheckRand)
Ted Kremenek24650472009-09-02 02:47:41 +0000539 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Abramo Bagnara723df242010-12-14 22:11:44 +0000541 const FunctionProtoType *FTP
542 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000543 if (!FTP)
544 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Ted Kremenek24650472009-09-02 02:47:41 +0000546 // Verify that the function takes no argument.
547 if (FTP->getNumArgs() != 0)
548 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Ted Kremenek24650472009-09-02 02:47:41 +0000550 // Issue a warning.
551 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000552 PathDiagnosticLocation CELoc =
553 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek24650472009-09-02 02:47:41 +0000554 BR.EmitBasicReport("'random' is not a secure random number generator",
555 "Security",
556 "The 'random' function produces a sequence of values that "
557 "an adversary may be able to predict. Use 'arc4random' "
Anna Zaks590dd8e2011-09-20 21:38:35 +0000558 "instead", CELoc, &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000559}
560
561//===----------------------------------------------------------------------===//
Anna Zaksa7957ff2011-10-11 04:34:54 +0000562// Check: 'vfork' should not be used.
563// POS33-C: Do not use vfork().
564//===----------------------------------------------------------------------===//
565
566void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek76a54242012-01-20 01:44:29 +0000567 if (!filter.check_vfork)
568 return;
569
Anna Zaksa7957ff2011-10-11 04:34:54 +0000570 // All calls to vfork() are insecure, issue a warning.
571 SourceRange R = CE->getCallee()->getSourceRange();
572 PathDiagnosticLocation CELoc =
573 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
574 BR.EmitBasicReport("Potential insecure implementation-specific behavior in "
575 "call 'vfork'",
576 "Security",
577 "Call to function 'vfork' is insecure as it can lead to "
578 "denial of service situations in the parent process. "
579 "Replace calls to vfork with calls to the safer "
580 "'posix_spawn' function",
581 CELoc, &R, 1);
582}
583
584//===----------------------------------------------------------------------===//
Ted Kremenek65a81a92009-08-28 00:08:09 +0000585// Check: Should check whether privileges are dropped successfully.
586// Originally: <rdar://problem/6337132>
587//===----------------------------------------------------------------------===//
588
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000589void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
Ted Kremenek65a81a92009-08-28 00:08:09 +0000590 const FunctionDecl *FD = CE->getDirectCallee();
591 if (!FD)
592 return;
593
594 if (II_setid[0] == NULL) {
Ted Kremenek24650472009-09-02 02:47:41 +0000595 static const char * const identifiers[num_setids] = {
Ted Kremenek65a81a92009-08-28 00:08:09 +0000596 "setuid", "setgid", "seteuid", "setegid",
597 "setreuid", "setregid"
598 };
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Ted Kremenek24650472009-09-02 02:47:41 +0000600 for (size_t i = 0; i < num_setids; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000601 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000602 }
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Ted Kremenek65a81a92009-08-28 00:08:09 +0000604 const IdentifierInfo *id = FD->getIdentifier();
605 size_t identifierid;
606
Ted Kremenek24650472009-09-02 02:47:41 +0000607 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000608 if (id == II_setid[identifierid])
609 break;
610
Ted Kremenek24650472009-09-02 02:47:41 +0000611 if (identifierid >= num_setids)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000612 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Abramo Bagnara723df242010-12-14 22:11:44 +0000614 const FunctionProtoType *FTP
615 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek65a81a92009-08-28 00:08:09 +0000616 if (!FTP)
617 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Ted Kremeneka8187832009-08-28 00:24:55 +0000619 // Verify that the function takes one or two arguments (depending on
620 // the function).
Ted Kremenek65a81a92009-08-28 00:08:09 +0000621 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
622 return;
623
624 // The arguments must be integers.
625 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
626 if (! FTP->getArgType(i)->isIntegerType())
627 return;
628
629 // Issue a warning.
Ted Kremenek2c016762010-03-24 22:39:47 +0000630 llvm::SmallString<256> buf1;
631 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000632 os1 << "Return value is not checked in call to '" << *FD << '\'';
Ted Kremenek65a81a92009-08-28 00:08:09 +0000633
Ted Kremenek2c016762010-03-24 22:39:47 +0000634 llvm::SmallString<256> buf2;
635 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000636 os2 << "The return value from the call to '" << *FD
637 << "' is not checked. If an error occurs in '" << *FD
Ted Kremenek65a81a92009-08-28 00:08:09 +0000638 << "', the following code may execute with unexpected privileges";
639
640 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000641 PathDiagnosticLocation CELoc =
642 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
643 BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000644}
645
646//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000647// SecuritySyntaxChecker
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000648//===----------------------------------------------------------------------===//
649
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000650namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000651class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000652public:
Ted Kremenek76a54242012-01-20 01:44:29 +0000653 ChecksFilter filter;
654
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000655 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
656 BugReporter &BR) const {
Ted Kremenek76a54242012-01-20 01:44:29 +0000657 WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter);
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000658 walker.Visit(D->getBody());
659 }
660};
661}
662
Ted Kremenek76a54242012-01-20 01:44:29 +0000663#define REGISTER_CHECKER(name) \
664void ento::register##name(CheckerManager &mgr) {\
665 mgr.registerChecker<SecuritySyntaxChecker>()->filter.check_##name = true;\
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000666}
Ted Kremenek76a54242012-01-20 01:44:29 +0000667
668REGISTER_CHECKER(gets)
669REGISTER_CHECKER(getpw)
670REGISTER_CHECKER(mktemp)
671REGISTER_CHECKER(strcpy)
672REGISTER_CHECKER(rand)
673REGISTER_CHECKER(vfork)
674REGISTER_CHECKER(FloatLoopCounter)
675