blob: 1d69e6b3ff38258ce838e08d617f79784667034a [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/Analysis/AnalysisContext.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/Basic/TargetInfo.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Anna Zaksc29bed32011-09-20 21:38:35 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Lenny Maioranifca2e962011-04-03 05:07:11 +000021#include "llvm/ADT/StringSwitch.h"
Anna Zaksc29bed32011-09-20 21:38:35 +000022#include "llvm/Support/raw_ostream.h"
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000023
24using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000025using namespace ento;
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000026
Ted Kremenekabf6ba12010-01-15 08:20:31 +000027static bool isArc4RandomAvailable(const ASTContext &Ctx) {
Douglas Gregore8bbc122011-09-02 00:18:52 +000028 const llvm::Triple &T = Ctx.getTargetInfo().getTriple();
Ted Kremenekabf6ba12010-01-15 08:20:31 +000029 return T.getVendor() == llvm::Triple::Apple ||
Douglas Gregor45e84b02011-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 Kremenekabf6ba12010-01-15 08:20:31 +000034}
35
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000036namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000037class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump11289f42009-09-09 15:08:12 +000038 BugReporter &BR;
Anna Zaksc29bed32011-09-20 21:38:35 +000039 AnalysisContext* AC;
Ted Kremenekad5a6002009-09-02 02:47:41 +000040 enum { num_setids = 6 };
41 IdentifierInfo *II_setid[num_setids];
Ted Kremenek8edc6df2010-03-24 22:39:47 +000042
Ted Kremenekabf6ba12010-01-15 08:20:31 +000043 const bool CheckRand;
Mike Stump11289f42009-09-09 15:08:12 +000044
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000045public:
Anna Zaksc29bed32011-09-20 21:38:35 +000046 WalkAST(BugReporter &br, AnalysisContext* ac)
47 : BR(br), AC(ac), II_setid(),
48 CheckRand(isArc4RandomAvailable(BR.getContext())) {}
Mike Stump11289f42009-09-09 15:08:12 +000049
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000050 // Statement visitor methods.
Ted Kremenek6610c032009-07-23 22:29:41 +000051 void VisitCallExpr(CallExpr *CE);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000052 void VisitForStmt(ForStmt *S);
Ted Kremenekd032fcc2009-08-28 00:08:09 +000053 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek9c497622009-07-23 21:34:35 +000054 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000055
56 void VisitChildren(Stmt *S);
Mike Stump11289f42009-09-09 15:08:12 +000057
Ted Kremenek6610c032009-07-23 22:29:41 +000058 // Helpers.
Lenny Maioranide909e42011-04-05 20:18:46 +000059 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
Mike Stump11289f42009-09-09 15:08:12 +000060
Lenny Maioranifca2e962011-04-03 05:07:11 +000061 typedef void (WalkAST::*FnCheck)(const CallExpr *,
62 const FunctionDecl *);
63
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000064 // Checker-specific methods.
Lenny Maioranide909e42011-04-05 20:18:46 +000065 void checkLoopConditionForFloat(const ForStmt *FS);
66 void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
67 void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
68 void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
69 void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
70 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
71 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
72 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
73 void checkUncheckedReturnValue(CallExpr *CE);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000074};
75} // end anonymous namespace
76
77//===----------------------------------------------------------------------===//
78// AST walking.
79//===----------------------------------------------------------------------===//
80
81void WalkAST::VisitChildren(Stmt *S) {
82 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
83 if (Stmt *child = *I)
84 Visit(child);
85}
86
Ted Kremenek6610c032009-07-23 22:29:41 +000087void WalkAST::VisitCallExpr(CallExpr *CE) {
Lenny Maioranifca2e962011-04-03 05:07:11 +000088 // Get the callee.
89 const FunctionDecl *FD = CE->getDirectCallee();
90
91 if (!FD)
92 return;
93
94 // Get the name of the callee. If it's a builtin, strip off the prefix.
95 IdentifierInfo *II = FD->getIdentifier();
96 if (!II) // if no identifier, not a simple C function
97 return;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000098 StringRef Name = II->getName();
Lenny Maioranifca2e962011-04-03 05:07:11 +000099 if (Name.startswith("__builtin_"))
100 Name = Name.substr(10);
101
102 // Set the evaluation function by switching on the callee name.
103 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
Lenny Maioranide909e42011-04-05 20:18:46 +0000104 .Case("gets", &WalkAST::checkCall_gets)
105 .Case("getpw", &WalkAST::checkCall_getpw)
106 .Case("mktemp", &WalkAST::checkCall_mktemp)
107 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
108 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
109 .Case("drand48", &WalkAST::checkCall_rand)
110 .Case("erand48", &WalkAST::checkCall_rand)
111 .Case("jrand48", &WalkAST::checkCall_rand)
112 .Case("lrand48", &WalkAST::checkCall_rand)
113 .Case("mrand48", &WalkAST::checkCall_rand)
114 .Case("nrand48", &WalkAST::checkCall_rand)
115 .Case("lcong48", &WalkAST::checkCall_rand)
116 .Case("rand", &WalkAST::checkCall_rand)
117 .Case("rand_r", &WalkAST::checkCall_rand)
118 .Case("random", &WalkAST::checkCall_random)
Lenny Maioranifca2e962011-04-03 05:07:11 +0000119 .Default(NULL);
120
121 // If the callee isn't defined, it is not of security concern.
122 // Check and evaluate the call.
123 if (evalFunction)
124 (this->*evalFunction)(CE, FD);
Mike Stump11289f42009-09-09 15:08:12 +0000125
Ted Kremenek6610c032009-07-23 22:29:41 +0000126 // Recurse and check children.
127 VisitChildren(CE);
128}
129
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000130void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
131 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000132 if (Stmt *child = *I) {
133 if (CallExpr *CE = dyn_cast<CallExpr>(child))
Lenny Maioranide909e42011-04-05 20:18:46 +0000134 checkUncheckedReturnValue(CE);
Mike Stump11289f42009-09-09 15:08:12 +0000135 Visit(child);
136 }
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000137}
138
Ted Kremenek9c497622009-07-23 21:34:35 +0000139void WalkAST::VisitForStmt(ForStmt *FS) {
Lenny Maioranide909e42011-04-05 20:18:46 +0000140 checkLoopConditionForFloat(FS);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000141
Ted Kremenek9c497622009-07-23 21:34:35 +0000142 // Recurse and check children.
143 VisitChildren(FS);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000144}
145
146//===----------------------------------------------------------------------===//
Ted Kremenek9c497622009-07-23 21:34:35 +0000147// Check: floating poing variable used as loop counter.
Ted Kremenek70e55262009-07-23 21:44:18 +0000148// Originally: <rdar://problem/6336718>
149// Implements: CERT security coding advisory FLP-30.
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000150//===----------------------------------------------------------------------===//
151
Ted Kremenek9c497622009-07-23 21:34:35 +0000152static const DeclRefExpr*
Lenny Maioranide909e42011-04-05 20:18:46 +0000153getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
Ted Kremenek9c497622009-07-23 21:34:35 +0000154 expr = expr->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000155
156 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek9c497622009-07-23 21:34:35 +0000157 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCalle3027922010-08-25 11:45:40 +0000158 B->getOpcode() == BO_Comma))
Ted Kremenek9c497622009-07-23 21:34:35 +0000159 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000160
Lenny Maioranide909e42011-04-05 20:18:46 +0000161 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
Ted Kremenek9c497622009-07-23 21:34:35 +0000162 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +0000163
Lenny Maioranide909e42011-04-05 20:18:46 +0000164 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
Ted Kremenek9c497622009-07-23 21:34:35 +0000165 return rhs;
Mike Stump11289f42009-09-09 15:08:12 +0000166
Ted Kremenek9c497622009-07-23 21:34:35 +0000167 return NULL;
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000168 }
Mike Stump11289f42009-09-09 15:08:12 +0000169
Ted Kremenek9c497622009-07-23 21:34:35 +0000170 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
171 const NamedDecl *ND = DR->getDecl();
172 return ND == x || ND == y ? DR : NULL;
173 }
Mike Stump11289f42009-09-09 15:08:12 +0000174
Ted Kremenek9c497622009-07-23 21:34:35 +0000175 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
176 return U->isIncrementDecrementOp()
Lenny Maioranide909e42011-04-05 20:18:46 +0000177 ? getIncrementedVar(U->getSubExpr(), x, y) : NULL;
Ted Kremenek9c497622009-07-23 21:34:35 +0000178
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000179 return NULL;
180}
181
Ted Kremenek9c497622009-07-23 21:34:35 +0000182/// CheckLoopConditionForFloat - This check looks for 'for' statements that
183/// use a floating point variable as a loop counter.
184/// CERT: FLP30-C, FLP30-CPP.
185///
Lenny Maioranide909e42011-04-05 20:18:46 +0000186void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
Ted Kremenek9c497622009-07-23 21:34:35 +0000187 // Does the loop have a condition?
188 const Expr *condition = FS->getCond();
Mike Stump11289f42009-09-09 15:08:12 +0000189
Ted Kremenek9c497622009-07-23 21:34:35 +0000190 if (!condition)
191 return;
192
193 // Does the loop have an increment?
194 const Expr *increment = FS->getInc();
Mike Stump11289f42009-09-09 15:08:12 +0000195
Ted Kremenek9c497622009-07-23 21:34:35 +0000196 if (!increment)
197 return;
Mike Stump11289f42009-09-09 15:08:12 +0000198
Ted Kremenek9c497622009-07-23 21:34:35 +0000199 // Strip away '()' and casts.
200 condition = condition->IgnoreParenCasts();
201 increment = increment->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000202
Ted Kremenek9c497622009-07-23 21:34:35 +0000203 // Is the loop condition a comparison?
204 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
205
206 if (!B)
207 return;
Mike Stump11289f42009-09-09 15:08:12 +0000208
Ted Kremenekb9cb1132009-07-24 20:26:31 +0000209 // Is this a comparison?
210 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek9c497622009-07-23 21:34:35 +0000211 return;
Mike Stump11289f42009-09-09 15:08:12 +0000212
Ted Kremenek9c497622009-07-23 21:34:35 +0000213 // Are we comparing variables?
John McCall34376a62010-12-04 03:47:34 +0000214 const DeclRefExpr *drLHS =
215 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
216 const DeclRefExpr *drRHS =
217 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump11289f42009-09-09 15:08:12 +0000218
Ted Kremenekb9cb1132009-07-24 20:26:31 +0000219 // Does at least one of the variables have a floating point type?
Douglas Gregor49b4d732010-06-22 23:07:26 +0000220 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
221 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000222
Ted Kremenek9c497622009-07-23 21:34:35 +0000223 if (!drLHS && !drRHS)
224 return;
225
226 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
227 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000228
Ted Kremenek9c497622009-07-23 21:34:35 +0000229 if (!vdLHS && !vdRHS)
Mike Stump11289f42009-09-09 15:08:12 +0000230 return;
231
Ted Kremenek9c497622009-07-23 21:34:35 +0000232 // Does either variable appear in increment?
Lenny Maioranide909e42011-04-05 20:18:46 +0000233 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump11289f42009-09-09 15:08:12 +0000234
Ted Kremenek9c497622009-07-23 21:34:35 +0000235 if (!drInc)
236 return;
Mike Stump11289f42009-09-09 15:08:12 +0000237
Ted Kremenek9c497622009-07-23 21:34:35 +0000238 // Emit the error. First figure out which DeclRefExpr in the condition
239 // referenced the compared variable.
240 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
241
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000242 SmallVector<SourceRange, 2> ranges;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000243 llvm::SmallString<256> sbuf;
244 llvm::raw_svector_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +0000245
Daniel Dunbar56df9772010-08-17 22:39:59 +0000246 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek9c497622009-07-23 21:34:35 +0000247 << "' with floating point type '" << drCond->getType().getAsString()
248 << "' should not be used as a loop counter";
249
250 ranges.push_back(drCond->getSourceRange());
251 ranges.push_back(drInc->getSourceRange());
Mike Stump11289f42009-09-09 15:08:12 +0000252
Ted Kremenek9c497622009-07-23 21:34:35 +0000253 const char *bugType = "Floating point variable used as loop counter";
Anna Zaksc29bed32011-09-20 21:38:35 +0000254
255 PathDiagnosticLocation FSLoc =
256 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
Benjamin Kramer63415532009-11-29 18:27:55 +0000257 BR.EmitBasicReport(bugType, "Security", os.str(),
Anna Zaksc29bed32011-09-20 21:38:35 +0000258 FSLoc, ranges.data(), ranges.size());
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000259}
260
261//===----------------------------------------------------------------------===//
Ted Kremenek6610c032009-07-23 22:29:41 +0000262// Check: Any use of 'gets' is insecure.
263// Originally: <rdar://problem/6335715>
264// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuf69973c2009-11-09 08:13:04 +0000265// CWE-242: Use of Inherently Dangerous Function
Ted Kremenek6610c032009-07-23 22:29:41 +0000266//===----------------------------------------------------------------------===//
267
Lenny Maioranide909e42011-04-05 20:18:46 +0000268void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000269 const FunctionProtoType *FPT
270 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000271 if (!FPT)
Ted Kremenek6610c032009-07-23 22:29:41 +0000272 return;
Mike Stump11289f42009-09-09 15:08:12 +0000273
Ted Kremenek6610c032009-07-23 22:29:41 +0000274 // Verify that the function takes a single argument.
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000275 if (FPT->getNumArgs() != 1)
Ted Kremenek6610c032009-07-23 22:29:41 +0000276 return;
277
278 // Is the argument a 'char*'?
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000279 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenek6610c032009-07-23 22:29:41 +0000280 if (!PT)
281 return;
Mike Stump11289f42009-09-09 15:08:12 +0000282
Ted Kremenek6610c032009-07-23 22:29:41 +0000283 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
284 return;
Mike Stump11289f42009-09-09 15:08:12 +0000285
Ted Kremenek6610c032009-07-23 22:29:41 +0000286 // Issue a warning.
287 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaksc29bed32011-09-20 21:38:35 +0000288 PathDiagnosticLocation CELoc =
289 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek6610c032009-07-23 22:29:41 +0000290 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
291 "Security",
292 "Call to function 'gets' is extremely insecure as it can "
293 "always result in a buffer overflow",
Anna Zaksc29bed32011-09-20 21:38:35 +0000294 CELoc, &R, 1);
Ted Kremenek6610c032009-07-23 22:29:41 +0000295}
296
297//===----------------------------------------------------------------------===//
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000298// Check: Any use of 'getpwd' is insecure.
299// CWE-477: Use of Obsolete Functions
300//===----------------------------------------------------------------------===//
301
Lenny Maioranide909e42011-04-05 20:18:46 +0000302void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000303 const FunctionProtoType *FPT
304 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000305 if (!FPT)
306 return;
307
308 // Verify that the function takes two arguments.
309 if (FPT->getNumArgs() != 2)
310 return;
311
312 // Verify the first argument type is integer.
313 if (!FPT->getArgType(0)->isIntegerType())
314 return;
315
316 // Verify the second argument type is char*.
317 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
318 if (!PT)
319 return;
320
321 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
322 return;
323
324 // Issue a warning.
325 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaksc29bed32011-09-20 21:38:35 +0000326 PathDiagnosticLocation CELoc =
327 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000328 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
329 "Security",
330 "The getpw() function is dangerous as it may overflow the "
331 "provided buffer. It is obsoleted by getpwuid().",
Anna Zaksc29bed32011-09-20 21:38:35 +0000332 CELoc, &R, 1);
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000333}
334
335//===----------------------------------------------------------------------===//
Zhongxing Xu39bba622009-12-03 09:15:23 +0000336// Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
337// CWE-377: Insecure Temporary File
338//===----------------------------------------------------------------------===//
339
Lenny Maioranide909e42011-04-05 20:18:46 +0000340void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000341 const FunctionProtoType *FPT
342 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xu39bba622009-12-03 09:15:23 +0000343 if(!FPT)
344 return;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000345
Lenny Maiorani70568c22011-03-31 21:26:55 +0000346 // Verify that the function takes a single argument.
Zhongxing Xu39bba622009-12-03 09:15:23 +0000347 if (FPT->getNumArgs() != 1)
348 return;
349
350 // Verify that the argument is Pointer Type.
351 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
352 if (!PT)
353 return;
354
355 // Verify that the argument is a 'char*'.
356 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
357 return;
Ted Kremenekaeaf3d22010-03-24 22:39:45 +0000358
Zhongxing Xu39bba622009-12-03 09:15:23 +0000359 // Issue a waring.
360 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaksc29bed32011-09-20 21:38:35 +0000361 PathDiagnosticLocation CELoc =
362 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Zhongxing Xu39bba622009-12-03 09:15:23 +0000363 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
Eli Friedman04831922010-08-22 01:00:03 +0000364 "Security",
365 "Call to function 'mktemp' is insecure as it always "
366 "creates or uses insecure temporary file. Use 'mkstemp' instead",
Anna Zaksc29bed32011-09-20 21:38:35 +0000367 CELoc, &R, 1);
Zhongxing Xu39bba622009-12-03 09:15:23 +0000368}
369
Zhongxing Xu39bba622009-12-03 09:15:23 +0000370//===----------------------------------------------------------------------===//
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000371// Check: Any use of 'strcpy' is insecure.
372//
373// CWE-119: Improper Restriction of Operations within
374// the Bounds of a Memory Buffer
375//===----------------------------------------------------------------------===//
Lenny Maioranide909e42011-04-05 20:18:46 +0000376void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
377 if (!checkCall_strCommon(CE, FD))
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000378 return;
379
Lenny Maioranide909e42011-04-05 20:18:46 +0000380 // Issue a warning.
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000381 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaksc29bed32011-09-20 21:38:35 +0000382 PathDiagnosticLocation CELoc =
383 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000384 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
Lenny Maioranide909e42011-04-05 20:18:46 +0000385 "call 'strcpy'",
386 "Security",
387 "Call to function 'strcpy' is insecure as it does not "
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000388 "provide bounding of the memory buffer. Replace "
389 "unbounded copy functions with analogous functions that "
390 "support length arguments such as 'strncpy'. CWE-119.",
Anna Zaksc29bed32011-09-20 21:38:35 +0000391 CELoc, &R, 1);
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000392}
393
394//===----------------------------------------------------------------------===//
Lenny Maioranide909e42011-04-05 20:18:46 +0000395// Check: Any use of 'strcat' is insecure.
396//
397// CWE-119: Improper Restriction of Operations within
398// the Bounds of a Memory Buffer
399//===----------------------------------------------------------------------===//
400void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
401 if (!checkCall_strCommon(CE, FD))
402 return;
403
404 // Issue a warning.
405 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaksc29bed32011-09-20 21:38:35 +0000406 PathDiagnosticLocation CELoc =
407 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Lenny Maioranide909e42011-04-05 20:18:46 +0000408 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
409 "call 'strcat'",
410 "Security",
411 "Call to function 'strcat' is insecure as it does not "
412 "provide bounding of the memory buffer. Replace "
413 "unbounded copy functions with analogous functions that "
414 "support length arguments such as 'strncat'. CWE-119.",
Anna Zaksc29bed32011-09-20 21:38:35 +0000415 CELoc, &R, 1);
Lenny Maioranide909e42011-04-05 20:18:46 +0000416}
417
418//===----------------------------------------------------------------------===//
419// Common check for str* functions with no bounds parameters.
420//===----------------------------------------------------------------------===//
421bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
422 const FunctionProtoType *FPT
423 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
424 if (!FPT)
425 return false;
426
427 // Verify the function takes two arguments, three in the _chk version.
428 int numArgs = FPT->getNumArgs();
429 if (numArgs != 2 && numArgs != 3)
430 return false;
431
432 // Verify the type for both arguments.
433 for (int i = 0; i < 2; i++) {
434 // Verify that the arguments are pointers.
435 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
436 if (!PT)
437 return false;
438
439 // Verify that the argument is a 'char*'.
440 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
441 return false;
442 }
443
444 return true;
445}
446
447//===----------------------------------------------------------------------===//
Ted Kremenekad5a6002009-09-02 02:47:41 +0000448// Check: Linear congruent random number generators should not be used
449// Originally: <rdar://problem/63371000>
450// CWE-338: Use of cryptographically weak prng
451//===----------------------------------------------------------------------===//
452
Lenny Maioranide909e42011-04-05 20:18:46 +0000453void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Lenny Maioranifca2e962011-04-03 05:07:11 +0000454 if (!CheckRand)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000455 return;
Mike Stump11289f42009-09-09 15:08:12 +0000456
Abramo Bagnara6d810632010-12-14 22:11:44 +0000457 const FunctionProtoType *FTP
458 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000459 if (!FTP)
460 return;
Mike Stump11289f42009-09-09 15:08:12 +0000461
Ted Kremenekad5a6002009-09-02 02:47:41 +0000462 if (FTP->getNumArgs() == 1) {
463 // Is the argument an 'unsigned short *'?
464 // (Actually any integer type is allowed.)
465 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
466 if (!PT)
467 return;
Mike Stump11289f42009-09-09 15:08:12 +0000468
Ted Kremenekad5a6002009-09-02 02:47:41 +0000469 if (! PT->getPointeeType()->isIntegerType())
470 return;
471 }
Mike Stump11289f42009-09-09 15:08:12 +0000472 else if (FTP->getNumArgs() != 0)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000473 return;
Mike Stump11289f42009-09-09 15:08:12 +0000474
Ted Kremenekad5a6002009-09-02 02:47:41 +0000475 // Issue a warning.
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000476 llvm::SmallString<256> buf1;
477 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000478 os1 << '\'' << FD << "' is a poor random number generator";
Ted Kremenekad5a6002009-09-02 02:47:41 +0000479
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000480 llvm::SmallString<256> buf2;
481 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000482 os2 << "Function '" << FD
Ted Kremenekad5a6002009-09-02 02:47:41 +0000483 << "' is obsolete because it implements a poor random number generator."
484 << " Use 'arc4random' instead";
485
486 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaksc29bed32011-09-20 21:38:35 +0000487 PathDiagnosticLocation CELoc =
488 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
489 BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1);
Ted Kremenekad5a6002009-09-02 02:47:41 +0000490}
491
492//===----------------------------------------------------------------------===//
493// Check: 'random' should not be used
494// Originally: <rdar://problem/63371000>
495//===----------------------------------------------------------------------===//
496
Lenny Maioranide909e42011-04-05 20:18:46 +0000497void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Lenny Maioranifca2e962011-04-03 05:07:11 +0000498 if (!CheckRand)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000499 return;
Mike Stump11289f42009-09-09 15:08:12 +0000500
Abramo Bagnara6d810632010-12-14 22:11:44 +0000501 const FunctionProtoType *FTP
502 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000503 if (!FTP)
504 return;
Mike Stump11289f42009-09-09 15:08:12 +0000505
Ted Kremenekad5a6002009-09-02 02:47:41 +0000506 // Verify that the function takes no argument.
507 if (FTP->getNumArgs() != 0)
508 return;
Mike Stump11289f42009-09-09 15:08:12 +0000509
Ted Kremenekad5a6002009-09-02 02:47:41 +0000510 // Issue a warning.
511 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaksc29bed32011-09-20 21:38:35 +0000512 PathDiagnosticLocation CELoc =
513 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenekad5a6002009-09-02 02:47:41 +0000514 BR.EmitBasicReport("'random' is not a secure random number generator",
515 "Security",
516 "The 'random' function produces a sequence of values that "
517 "an adversary may be able to predict. Use 'arc4random' "
Anna Zaksc29bed32011-09-20 21:38:35 +0000518 "instead", CELoc, &R, 1);
Ted Kremenekad5a6002009-09-02 02:47:41 +0000519}
520
521//===----------------------------------------------------------------------===//
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000522// Check: Should check whether privileges are dropped successfully.
523// Originally: <rdar://problem/6337132>
524//===----------------------------------------------------------------------===//
525
Lenny Maioranide909e42011-04-05 20:18:46 +0000526void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000527 const FunctionDecl *FD = CE->getDirectCallee();
528 if (!FD)
529 return;
530
531 if (II_setid[0] == NULL) {
Ted Kremenekad5a6002009-09-02 02:47:41 +0000532 static const char * const identifiers[num_setids] = {
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000533 "setuid", "setgid", "seteuid", "setegid",
534 "setreuid", "setregid"
535 };
Mike Stump11289f42009-09-09 15:08:12 +0000536
Ted Kremenekad5a6002009-09-02 02:47:41 +0000537 for (size_t i = 0; i < num_setids; i++)
Mike Stump11289f42009-09-09 15:08:12 +0000538 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000539 }
Mike Stump11289f42009-09-09 15:08:12 +0000540
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000541 const IdentifierInfo *id = FD->getIdentifier();
542 size_t identifierid;
543
Ted Kremenekad5a6002009-09-02 02:47:41 +0000544 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000545 if (id == II_setid[identifierid])
546 break;
547
Ted Kremenekad5a6002009-09-02 02:47:41 +0000548 if (identifierid >= num_setids)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000549 return;
Mike Stump11289f42009-09-09 15:08:12 +0000550
Abramo Bagnara6d810632010-12-14 22:11:44 +0000551 const FunctionProtoType *FTP
552 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000553 if (!FTP)
554 return;
Mike Stump11289f42009-09-09 15:08:12 +0000555
Ted Kremenekaeca0952009-08-28 00:24:55 +0000556 // Verify that the function takes one or two arguments (depending on
557 // the function).
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000558 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
559 return;
560
561 // The arguments must be integers.
562 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
563 if (! FTP->getArgType(i)->isIntegerType())
564 return;
565
566 // Issue a warning.
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000567 llvm::SmallString<256> buf1;
568 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000569 os1 << "Return value is not checked in call to '" << FD << '\'';
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000570
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000571 llvm::SmallString<256> buf2;
572 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000573 os2 << "The return value from the call to '" << FD
574 << "' is not checked. If an error occurs in '" << FD
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000575 << "', the following code may execute with unexpected privileges";
576
577 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaksc29bed32011-09-20 21:38:35 +0000578 PathDiagnosticLocation CELoc =
579 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
580 BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1);
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000581}
582
583//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000584// SecuritySyntaxChecker
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000585//===----------------------------------------------------------------------===//
586
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000587namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000588class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000589public:
590 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
591 BugReporter &BR) const {
Anna Zaksc29bed32011-09-20 21:38:35 +0000592 WalkAST walker(BR, mgr.getAnalysisContext(D));
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000593 walker.Visit(D->getBody());
594 }
595};
596}
597
598void ento::registerSecuritySyntaxChecker(CheckerManager &mgr) {
599 mgr.registerChecker<SecuritySyntaxChecker>();
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000600}