blob: c4c37779263c5298f29400fc7f1e881898ef7277 [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"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000015#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000016#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000017#include "clang/Basic/TargetInfo.h"
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek9c497622009-07-23 21:34:35 +000019#include "llvm/Support/raw_ostream.h"
Lenny Maioranifca2e962011-04-03 05:07:11 +000020#include "llvm/ADT/StringSwitch.h"
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000021
22using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000023using namespace ento;
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000024
Ted Kremenekabf6ba12010-01-15 08:20:31 +000025static bool isArc4RandomAvailable(const ASTContext &Ctx) {
26 const llvm::Triple &T = Ctx.Target.getTriple();
27 return T.getVendor() == llvm::Triple::Apple ||
Douglas Gregor45e84b02011-01-17 19:16:24 +000028 T.getOS() == llvm::Triple::FreeBSD ||
29 T.getOS() == llvm::Triple::NetBSD ||
30 T.getOS() == llvm::Triple::OpenBSD ||
31 T.getOS() == llvm::Triple::DragonFly;
Ted Kremenekabf6ba12010-01-15 08:20:31 +000032}
33
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000034namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000035class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump11289f42009-09-09 15:08:12 +000036 BugReporter &BR;
Ted Kremenekad5a6002009-09-02 02:47:41 +000037 enum { num_setids = 6 };
38 IdentifierInfo *II_setid[num_setids];
Ted Kremenek8edc6df2010-03-24 22:39:47 +000039
Ted Kremenekabf6ba12010-01-15 08:20:31 +000040 const bool CheckRand;
Mike Stump11289f42009-09-09 15:08:12 +000041
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000042public:
Lenny Maioranifca2e962011-04-03 05:07:11 +000043 WalkAST(BugReporter &br) : BR(br), II_setid(),
Ted Kremenekabf6ba12010-01-15 08:20:31 +000044 CheckRand(isArc4RandomAvailable(BR.getContext())) {}
Mike Stump11289f42009-09-09 15:08:12 +000045
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000046 // Statement visitor methods.
Ted Kremenek6610c032009-07-23 22:29:41 +000047 void VisitCallExpr(CallExpr *CE);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000048 void VisitForStmt(ForStmt *S);
Ted Kremenekd032fcc2009-08-28 00:08:09 +000049 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek9c497622009-07-23 21:34:35 +000050 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000051
52 void VisitChildren(Stmt *S);
Mike Stump11289f42009-09-09 15:08:12 +000053
Ted Kremenek6610c032009-07-23 22:29:41 +000054 // Helpers.
Lenny Maioranide909e42011-04-05 20:18:46 +000055 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
Mike Stump11289f42009-09-09 15:08:12 +000056
Lenny Maioranifca2e962011-04-03 05:07:11 +000057 typedef void (WalkAST::*FnCheck)(const CallExpr *,
58 const FunctionDecl *);
59
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000060 // Checker-specific methods.
Lenny Maioranide909e42011-04-05 20:18:46 +000061 void checkLoopConditionForFloat(const ForStmt *FS);
62 void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
63 void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
64 void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
65 void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
66 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
67 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
68 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
69 void checkUncheckedReturnValue(CallExpr *CE);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000070};
71} // end anonymous namespace
72
73//===----------------------------------------------------------------------===//
74// AST walking.
75//===----------------------------------------------------------------------===//
76
77void WalkAST::VisitChildren(Stmt *S) {
78 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
79 if (Stmt *child = *I)
80 Visit(child);
81}
82
Ted Kremenek6610c032009-07-23 22:29:41 +000083void WalkAST::VisitCallExpr(CallExpr *CE) {
Lenny Maioranifca2e962011-04-03 05:07:11 +000084 // Get the callee.
85 const FunctionDecl *FD = CE->getDirectCallee();
86
87 if (!FD)
88 return;
89
90 // Get the name of the callee. If it's a builtin, strip off the prefix.
91 IdentifierInfo *II = FD->getIdentifier();
92 if (!II) // if no identifier, not a simple C function
93 return;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000094 StringRef Name = II->getName();
Lenny Maioranifca2e962011-04-03 05:07:11 +000095 if (Name.startswith("__builtin_"))
96 Name = Name.substr(10);
97
98 // Set the evaluation function by switching on the callee name.
99 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
Lenny Maioranide909e42011-04-05 20:18:46 +0000100 .Case("gets", &WalkAST::checkCall_gets)
101 .Case("getpw", &WalkAST::checkCall_getpw)
102 .Case("mktemp", &WalkAST::checkCall_mktemp)
103 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
104 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
105 .Case("drand48", &WalkAST::checkCall_rand)
106 .Case("erand48", &WalkAST::checkCall_rand)
107 .Case("jrand48", &WalkAST::checkCall_rand)
108 .Case("lrand48", &WalkAST::checkCall_rand)
109 .Case("mrand48", &WalkAST::checkCall_rand)
110 .Case("nrand48", &WalkAST::checkCall_rand)
111 .Case("lcong48", &WalkAST::checkCall_rand)
112 .Case("rand", &WalkAST::checkCall_rand)
113 .Case("rand_r", &WalkAST::checkCall_rand)
114 .Case("random", &WalkAST::checkCall_random)
Lenny Maioranifca2e962011-04-03 05:07:11 +0000115 .Default(NULL);
116
117 // If the callee isn't defined, it is not of security concern.
118 // Check and evaluate the call.
119 if (evalFunction)
120 (this->*evalFunction)(CE, FD);
Mike Stump11289f42009-09-09 15:08:12 +0000121
Ted Kremenek6610c032009-07-23 22:29:41 +0000122 // Recurse and check children.
123 VisitChildren(CE);
124}
125
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000126void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
127 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000128 if (Stmt *child = *I) {
129 if (CallExpr *CE = dyn_cast<CallExpr>(child))
Lenny Maioranide909e42011-04-05 20:18:46 +0000130 checkUncheckedReturnValue(CE);
Mike Stump11289f42009-09-09 15:08:12 +0000131 Visit(child);
132 }
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000133}
134
Ted Kremenek9c497622009-07-23 21:34:35 +0000135void WalkAST::VisitForStmt(ForStmt *FS) {
Lenny Maioranide909e42011-04-05 20:18:46 +0000136 checkLoopConditionForFloat(FS);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000137
Ted Kremenek9c497622009-07-23 21:34:35 +0000138 // Recurse and check children.
139 VisitChildren(FS);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000140}
141
142//===----------------------------------------------------------------------===//
Ted Kremenek9c497622009-07-23 21:34:35 +0000143// Check: floating poing variable used as loop counter.
Ted Kremenek70e55262009-07-23 21:44:18 +0000144// Originally: <rdar://problem/6336718>
145// Implements: CERT security coding advisory FLP-30.
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000146//===----------------------------------------------------------------------===//
147
Ted Kremenek9c497622009-07-23 21:34:35 +0000148static const DeclRefExpr*
Lenny Maioranide909e42011-04-05 20:18:46 +0000149getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
Ted Kremenek9c497622009-07-23 21:34:35 +0000150 expr = expr->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000151
152 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek9c497622009-07-23 21:34:35 +0000153 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCalle3027922010-08-25 11:45:40 +0000154 B->getOpcode() == BO_Comma))
Ted Kremenek9c497622009-07-23 21:34:35 +0000155 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000156
Lenny Maioranide909e42011-04-05 20:18:46 +0000157 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
Ted Kremenek9c497622009-07-23 21:34:35 +0000158 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +0000159
Lenny Maioranide909e42011-04-05 20:18:46 +0000160 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
Ted Kremenek9c497622009-07-23 21:34:35 +0000161 return rhs;
Mike Stump11289f42009-09-09 15:08:12 +0000162
Ted Kremenek9c497622009-07-23 21:34:35 +0000163 return NULL;
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000164 }
Mike Stump11289f42009-09-09 15:08:12 +0000165
Ted Kremenek9c497622009-07-23 21:34:35 +0000166 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
167 const NamedDecl *ND = DR->getDecl();
168 return ND == x || ND == y ? DR : NULL;
169 }
Mike Stump11289f42009-09-09 15:08:12 +0000170
Ted Kremenek9c497622009-07-23 21:34:35 +0000171 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
172 return U->isIncrementDecrementOp()
Lenny Maioranide909e42011-04-05 20:18:46 +0000173 ? getIncrementedVar(U->getSubExpr(), x, y) : NULL;
Ted Kremenek9c497622009-07-23 21:34:35 +0000174
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000175 return NULL;
176}
177
Ted Kremenek9c497622009-07-23 21:34:35 +0000178/// CheckLoopConditionForFloat - This check looks for 'for' statements that
179/// use a floating point variable as a loop counter.
180/// CERT: FLP30-C, FLP30-CPP.
181///
Lenny Maioranide909e42011-04-05 20:18:46 +0000182void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
Ted Kremenek9c497622009-07-23 21:34:35 +0000183 // Does the loop have a condition?
184 const Expr *condition = FS->getCond();
Mike Stump11289f42009-09-09 15:08:12 +0000185
Ted Kremenek9c497622009-07-23 21:34:35 +0000186 if (!condition)
187 return;
188
189 // Does the loop have an increment?
190 const Expr *increment = FS->getInc();
Mike Stump11289f42009-09-09 15:08:12 +0000191
Ted Kremenek9c497622009-07-23 21:34:35 +0000192 if (!increment)
193 return;
Mike Stump11289f42009-09-09 15:08:12 +0000194
Ted Kremenek9c497622009-07-23 21:34:35 +0000195 // Strip away '()' and casts.
196 condition = condition->IgnoreParenCasts();
197 increment = increment->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000198
Ted Kremenek9c497622009-07-23 21:34:35 +0000199 // Is the loop condition a comparison?
200 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
201
202 if (!B)
203 return;
Mike Stump11289f42009-09-09 15:08:12 +0000204
Ted Kremenekb9cb1132009-07-24 20:26:31 +0000205 // Is this a comparison?
206 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek9c497622009-07-23 21:34:35 +0000207 return;
Mike Stump11289f42009-09-09 15:08:12 +0000208
Ted Kremenek9c497622009-07-23 21:34:35 +0000209 // Are we comparing variables?
John McCall34376a62010-12-04 03:47:34 +0000210 const DeclRefExpr *drLHS =
211 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
212 const DeclRefExpr *drRHS =
213 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump11289f42009-09-09 15:08:12 +0000214
Ted Kremenekb9cb1132009-07-24 20:26:31 +0000215 // Does at least one of the variables have a floating point type?
Douglas Gregor49b4d732010-06-22 23:07:26 +0000216 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
217 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000218
Ted Kremenek9c497622009-07-23 21:34:35 +0000219 if (!drLHS && !drRHS)
220 return;
221
222 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
223 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000224
Ted Kremenek9c497622009-07-23 21:34:35 +0000225 if (!vdLHS && !vdRHS)
Mike Stump11289f42009-09-09 15:08:12 +0000226 return;
227
Ted Kremenek9c497622009-07-23 21:34:35 +0000228 // Does either variable appear in increment?
Lenny Maioranide909e42011-04-05 20:18:46 +0000229 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump11289f42009-09-09 15:08:12 +0000230
Ted Kremenek9c497622009-07-23 21:34:35 +0000231 if (!drInc)
232 return;
Mike Stump11289f42009-09-09 15:08:12 +0000233
Ted Kremenek9c497622009-07-23 21:34:35 +0000234 // Emit the error. First figure out which DeclRefExpr in the condition
235 // referenced the compared variable.
236 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
237
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000238 SmallVector<SourceRange, 2> ranges;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000239 llvm::SmallString<256> sbuf;
240 llvm::raw_svector_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +0000241
Daniel Dunbar56df9772010-08-17 22:39:59 +0000242 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek9c497622009-07-23 21:34:35 +0000243 << "' with floating point type '" << drCond->getType().getAsString()
244 << "' should not be used as a loop counter";
245
246 ranges.push_back(drCond->getSourceRange());
247 ranges.push_back(drInc->getSourceRange());
Mike Stump11289f42009-09-09 15:08:12 +0000248
Ted Kremenek9c497622009-07-23 21:34:35 +0000249 const char *bugType = "Floating point variable used as loop counter";
Benjamin Kramer63415532009-11-29 18:27:55 +0000250 BR.EmitBasicReport(bugType, "Security", os.str(),
Ted Kremenek9c497622009-07-23 21:34:35 +0000251 FS->getLocStart(), ranges.data(), ranges.size());
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000252}
253
254//===----------------------------------------------------------------------===//
Ted Kremenek6610c032009-07-23 22:29:41 +0000255// Check: Any use of 'gets' is insecure.
256// Originally: <rdar://problem/6335715>
257// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuf69973c2009-11-09 08:13:04 +0000258// CWE-242: Use of Inherently Dangerous Function
Ted Kremenek6610c032009-07-23 22:29:41 +0000259//===----------------------------------------------------------------------===//
260
Lenny Maioranide909e42011-04-05 20:18:46 +0000261void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000262 const FunctionProtoType *FPT
263 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000264 if (!FPT)
Ted Kremenek6610c032009-07-23 22:29:41 +0000265 return;
Mike Stump11289f42009-09-09 15:08:12 +0000266
Ted Kremenek6610c032009-07-23 22:29:41 +0000267 // Verify that the function takes a single argument.
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000268 if (FPT->getNumArgs() != 1)
Ted Kremenek6610c032009-07-23 22:29:41 +0000269 return;
270
271 // Is the argument a 'char*'?
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000272 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenek6610c032009-07-23 22:29:41 +0000273 if (!PT)
274 return;
Mike Stump11289f42009-09-09 15:08:12 +0000275
Ted Kremenek6610c032009-07-23 22:29:41 +0000276 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
277 return;
Mike Stump11289f42009-09-09 15:08:12 +0000278
Ted Kremenek6610c032009-07-23 22:29:41 +0000279 // Issue a warning.
280 SourceRange R = CE->getCallee()->getSourceRange();
281 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
282 "Security",
283 "Call to function 'gets' is extremely insecure as it can "
284 "always result in a buffer overflow",
285 CE->getLocStart(), &R, 1);
286}
287
288//===----------------------------------------------------------------------===//
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000289// Check: Any use of 'getpwd' is insecure.
290// CWE-477: Use of Obsolete Functions
291//===----------------------------------------------------------------------===//
292
Lenny Maioranide909e42011-04-05 20:18:46 +0000293void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000294 const FunctionProtoType *FPT
295 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000296 if (!FPT)
297 return;
298
299 // Verify that the function takes two arguments.
300 if (FPT->getNumArgs() != 2)
301 return;
302
303 // Verify the first argument type is integer.
304 if (!FPT->getArgType(0)->isIntegerType())
305 return;
306
307 // Verify the second argument type is char*.
308 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
309 if (!PT)
310 return;
311
312 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
313 return;
314
315 // Issue a warning.
316 SourceRange R = CE->getCallee()->getSourceRange();
317 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
318 "Security",
319 "The getpw() function is dangerous as it may overflow the "
320 "provided buffer. It is obsoleted by getpwuid().",
321 CE->getLocStart(), &R, 1);
322}
323
324//===----------------------------------------------------------------------===//
Zhongxing Xu39bba622009-12-03 09:15:23 +0000325// Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
326// CWE-377: Insecure Temporary File
327//===----------------------------------------------------------------------===//
328
Lenny Maioranide909e42011-04-05 20:18:46 +0000329void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000330 const FunctionProtoType *FPT
331 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xu39bba622009-12-03 09:15:23 +0000332 if(!FPT)
333 return;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000334
Lenny Maiorani70568c22011-03-31 21:26:55 +0000335 // Verify that the function takes a single argument.
Zhongxing Xu39bba622009-12-03 09:15:23 +0000336 if (FPT->getNumArgs() != 1)
337 return;
338
339 // Verify that the argument is Pointer Type.
340 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
341 if (!PT)
342 return;
343
344 // Verify that the argument is a 'char*'.
345 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
346 return;
Ted Kremenekaeaf3d22010-03-24 22:39:45 +0000347
Zhongxing Xu39bba622009-12-03 09:15:23 +0000348 // Issue a waring.
349 SourceRange R = CE->getCallee()->getSourceRange();
350 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
Eli Friedman04831922010-08-22 01:00:03 +0000351 "Security",
352 "Call to function 'mktemp' is insecure as it always "
353 "creates or uses insecure temporary file. Use 'mkstemp' instead",
354 CE->getLocStart(), &R, 1);
Zhongxing Xu39bba622009-12-03 09:15:23 +0000355}
356
Zhongxing Xu39bba622009-12-03 09:15:23 +0000357//===----------------------------------------------------------------------===//
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000358// Check: Any use of 'strcpy' is insecure.
359//
360// CWE-119: Improper Restriction of Operations within
361// the Bounds of a Memory Buffer
362//===----------------------------------------------------------------------===//
Lenny Maioranide909e42011-04-05 20:18:46 +0000363void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
364 if (!checkCall_strCommon(CE, FD))
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000365 return;
366
Lenny Maioranide909e42011-04-05 20:18:46 +0000367 // Issue a warning.
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000368 SourceRange R = CE->getCallee()->getSourceRange();
369 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
Lenny Maioranide909e42011-04-05 20:18:46 +0000370 "call 'strcpy'",
371 "Security",
372 "Call to function 'strcpy' is insecure as it does not "
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000373 "provide bounding of the memory buffer. Replace "
374 "unbounded copy functions with analogous functions that "
375 "support length arguments such as 'strncpy'. CWE-119.",
376 CE->getLocStart(), &R, 1);
377}
378
379//===----------------------------------------------------------------------===//
Lenny Maioranide909e42011-04-05 20:18:46 +0000380// Check: Any use of 'strcat' is insecure.
381//
382// CWE-119: Improper Restriction of Operations within
383// the Bounds of a Memory Buffer
384//===----------------------------------------------------------------------===//
385void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
386 if (!checkCall_strCommon(CE, FD))
387 return;
388
389 // Issue a warning.
390 SourceRange R = CE->getCallee()->getSourceRange();
391 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
392 "call 'strcat'",
393 "Security",
394 "Call to function 'strcat' is insecure as it does not "
395 "provide bounding of the memory buffer. Replace "
396 "unbounded copy functions with analogous functions that "
397 "support length arguments such as 'strncat'. CWE-119.",
398 CE->getLocStart(), &R, 1);
399}
400
401//===----------------------------------------------------------------------===//
402// Common check for str* functions with no bounds parameters.
403//===----------------------------------------------------------------------===//
404bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
405 const FunctionProtoType *FPT
406 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
407 if (!FPT)
408 return false;
409
410 // Verify the function takes two arguments, three in the _chk version.
411 int numArgs = FPT->getNumArgs();
412 if (numArgs != 2 && numArgs != 3)
413 return false;
414
415 // Verify the type for both arguments.
416 for (int i = 0; i < 2; i++) {
417 // Verify that the arguments are pointers.
418 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
419 if (!PT)
420 return false;
421
422 // Verify that the argument is a 'char*'.
423 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
424 return false;
425 }
426
427 return true;
428}
429
430//===----------------------------------------------------------------------===//
Ted Kremenekad5a6002009-09-02 02:47:41 +0000431// Check: Linear congruent random number generators should not be used
432// Originally: <rdar://problem/63371000>
433// CWE-338: Use of cryptographically weak prng
434//===----------------------------------------------------------------------===//
435
Lenny Maioranide909e42011-04-05 20:18:46 +0000436void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Lenny Maioranifca2e962011-04-03 05:07:11 +0000437 if (!CheckRand)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000438 return;
Mike Stump11289f42009-09-09 15:08:12 +0000439
Abramo Bagnara6d810632010-12-14 22:11:44 +0000440 const FunctionProtoType *FTP
441 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000442 if (!FTP)
443 return;
Mike Stump11289f42009-09-09 15:08:12 +0000444
Ted Kremenekad5a6002009-09-02 02:47:41 +0000445 if (FTP->getNumArgs() == 1) {
446 // Is the argument an 'unsigned short *'?
447 // (Actually any integer type is allowed.)
448 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
449 if (!PT)
450 return;
Mike Stump11289f42009-09-09 15:08:12 +0000451
Ted Kremenekad5a6002009-09-02 02:47:41 +0000452 if (! PT->getPointeeType()->isIntegerType())
453 return;
454 }
Mike Stump11289f42009-09-09 15:08:12 +0000455 else if (FTP->getNumArgs() != 0)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000456 return;
Mike Stump11289f42009-09-09 15:08:12 +0000457
Ted Kremenekad5a6002009-09-02 02:47:41 +0000458 // Issue a warning.
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000459 llvm::SmallString<256> buf1;
460 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000461 os1 << '\'' << FD << "' is a poor random number generator";
Ted Kremenekad5a6002009-09-02 02:47:41 +0000462
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000463 llvm::SmallString<256> buf2;
464 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000465 os2 << "Function '" << FD
Ted Kremenekad5a6002009-09-02 02:47:41 +0000466 << "' is obsolete because it implements a poor random number generator."
467 << " Use 'arc4random' instead";
468
469 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000470 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenekad5a6002009-09-02 02:47:41 +0000471}
472
473//===----------------------------------------------------------------------===//
474// Check: 'random' should not be used
475// Originally: <rdar://problem/63371000>
476//===----------------------------------------------------------------------===//
477
Lenny Maioranide909e42011-04-05 20:18:46 +0000478void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Lenny Maioranifca2e962011-04-03 05:07:11 +0000479 if (!CheckRand)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000480 return;
Mike Stump11289f42009-09-09 15:08:12 +0000481
Abramo Bagnara6d810632010-12-14 22:11:44 +0000482 const FunctionProtoType *FTP
483 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000484 if (!FTP)
485 return;
Mike Stump11289f42009-09-09 15:08:12 +0000486
Ted Kremenekad5a6002009-09-02 02:47:41 +0000487 // Verify that the function takes no argument.
488 if (FTP->getNumArgs() != 0)
489 return;
Mike Stump11289f42009-09-09 15:08:12 +0000490
Ted Kremenekad5a6002009-09-02 02:47:41 +0000491 // Issue a warning.
492 SourceRange R = CE->getCallee()->getSourceRange();
493 BR.EmitBasicReport("'random' is not a secure random number generator",
494 "Security",
495 "The 'random' function produces a sequence of values that "
496 "an adversary may be able to predict. Use 'arc4random' "
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000497 "instead", CE->getLocStart(), &R, 1);
Ted Kremenekad5a6002009-09-02 02:47:41 +0000498}
499
500//===----------------------------------------------------------------------===//
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000501// Check: Should check whether privileges are dropped successfully.
502// Originally: <rdar://problem/6337132>
503//===----------------------------------------------------------------------===//
504
Lenny Maioranide909e42011-04-05 20:18:46 +0000505void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000506 const FunctionDecl *FD = CE->getDirectCallee();
507 if (!FD)
508 return;
509
510 if (II_setid[0] == NULL) {
Ted Kremenekad5a6002009-09-02 02:47:41 +0000511 static const char * const identifiers[num_setids] = {
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000512 "setuid", "setgid", "seteuid", "setegid",
513 "setreuid", "setregid"
514 };
Mike Stump11289f42009-09-09 15:08:12 +0000515
Ted Kremenekad5a6002009-09-02 02:47:41 +0000516 for (size_t i = 0; i < num_setids; i++)
Mike Stump11289f42009-09-09 15:08:12 +0000517 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000518 }
Mike Stump11289f42009-09-09 15:08:12 +0000519
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000520 const IdentifierInfo *id = FD->getIdentifier();
521 size_t identifierid;
522
Ted Kremenekad5a6002009-09-02 02:47:41 +0000523 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000524 if (id == II_setid[identifierid])
525 break;
526
Ted Kremenekad5a6002009-09-02 02:47:41 +0000527 if (identifierid >= num_setids)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000528 return;
Mike Stump11289f42009-09-09 15:08:12 +0000529
Abramo Bagnara6d810632010-12-14 22:11:44 +0000530 const FunctionProtoType *FTP
531 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000532 if (!FTP)
533 return;
Mike Stump11289f42009-09-09 15:08:12 +0000534
Ted Kremenekaeca0952009-08-28 00:24:55 +0000535 // Verify that the function takes one or two arguments (depending on
536 // the function).
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000537 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
538 return;
539
540 // The arguments must be integers.
541 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
542 if (! FTP->getArgType(i)->isIntegerType())
543 return;
544
545 // Issue a warning.
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000546 llvm::SmallString<256> buf1;
547 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000548 os1 << "Return value is not checked in call to '" << FD << '\'';
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000549
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000550 llvm::SmallString<256> buf2;
551 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000552 os2 << "The return value from the call to '" << FD
553 << "' is not checked. If an error occurs in '" << FD
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000554 << "', the following code may execute with unexpected privileges";
555
556 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000557 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000558}
559
560//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000561// SecuritySyntaxChecker
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000562//===----------------------------------------------------------------------===//
563
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000564namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000565class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000566public:
567 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
568 BugReporter &BR) const {
569 WalkAST walker(BR);
570 walker.Visit(D->getBody());
571 }
572};
573}
574
575void ento::registerSecuritySyntaxChecker(CheckerManager &mgr) {
576 mgr.registerChecker<SecuritySyntaxChecker>();
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000577}