blob: 12a8ec30befed495ab9281ef902db71680cf90ab [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"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000015#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000016#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +000017#include "clang/Basic/TargetInfo.h"
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek8baf86d2009-07-23 21:34:35 +000019#include "llvm/Support/raw_ostream.h"
Lenny Maioranic2dace12011-04-03 05:07:11 +000020#include "llvm/ADT/StringSwitch.h"
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000021
22using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000023using namespace ento;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000024
Ted Kremenek88c8bc82010-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 Gregor0f565592011-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 Kremenek88c8bc82010-01-15 08:20:31 +000032}
33
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000034namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000035class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump1eb44332009-09-09 15:08:12 +000036 BugReporter &BR;
Ted Kremenek24650472009-09-02 02:47:41 +000037 enum { num_setids = 6 };
38 IdentifierInfo *II_setid[num_setids];
Ted Kremenek2c016762010-03-24 22:39:47 +000039
Ted Kremenek88c8bc82010-01-15 08:20:31 +000040 const bool CheckRand;
Mike Stump1eb44332009-09-09 15:08:12 +000041
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000042public:
Lenny Maioranic2dace12011-04-03 05:07:11 +000043 WalkAST(BugReporter &br) : BR(br), II_setid(),
Ted Kremenek88c8bc82010-01-15 08:20:31 +000044 CheckRand(isArc4RandomAvailable(BR.getContext())) {}
Mike Stump1eb44332009-09-09 15:08:12 +000045
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000046 // Statement visitor methods.
Ted Kremenekefcbb152009-07-23 22:29:41 +000047 void VisitCallExpr(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000048 void VisitForStmt(ForStmt *S);
Ted Kremenek65a81a92009-08-28 00:08:09 +000049 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek8baf86d2009-07-23 21:34:35 +000050 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000051
52 void VisitChildren(Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +000053
Ted Kremenekefcbb152009-07-23 22:29:41 +000054 // Helpers.
55 IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str);
Mike Stump1eb44332009-09-09 15:08:12 +000056
Lenny Maioranic2dace12011-04-03 05:07:11 +000057 typedef void (WalkAST::*FnCheck)(const CallExpr *,
58 const FunctionDecl *);
59
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000060 // Checker-specific methods.
Ted Kremenek8baf86d2009-07-23 21:34:35 +000061 void CheckLoopConditionForFloat(const ForStmt *FS);
Ted Kremenekefcbb152009-07-23 22:29:41 +000062 void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD);
Zhongxing Xubd842e32009-11-09 12:19:26 +000063 void CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
Zhongxing Xu1bf40562009-12-03 09:15:23 +000064 void CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani5b67a822011-03-31 22:09:14 +000065 void CheckCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maioranic2dace12011-04-03 05:07:11 +000066 void CheckCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenek24650472009-09-02 02:47:41 +000067 void CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD);
68 void CheckCall_random(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenek65a81a92009-08-28 00:08:09 +000069 void CheckUncheckedReturnValue(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000070};
71} // end anonymous namespace
72
73//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +000074// Helper methods.
75//===----------------------------------------------------------------------===//
76
77IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) {
78 if (!II)
79 II = &BR.getContext().Idents.get(str);
Mike Stump1eb44332009-09-09 15:08:12 +000080
81 return II;
Ted Kremenekefcbb152009-07-23 22:29:41 +000082}
83
84//===----------------------------------------------------------------------===//
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000085// AST walking.
86//===----------------------------------------------------------------------===//
87
88void WalkAST::VisitChildren(Stmt *S) {
89 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
90 if (Stmt *child = *I)
91 Visit(child);
92}
93
Ted Kremenekefcbb152009-07-23 22:29:41 +000094void WalkAST::VisitCallExpr(CallExpr *CE) {
Lenny Maioranic2dace12011-04-03 05:07:11 +000095 // Get the callee.
96 const FunctionDecl *FD = CE->getDirectCallee();
97
98 if (!FD)
99 return;
100
101 // Get the name of the callee. If it's a builtin, strip off the prefix.
102 IdentifierInfo *II = FD->getIdentifier();
103 if (!II) // if no identifier, not a simple C function
104 return;
105 llvm::StringRef Name = II->getName();
106 if (Name.startswith("__builtin_"))
107 Name = Name.substr(10);
108
109 // Set the evaluation function by switching on the callee name.
110 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
111 .Case("gets", &WalkAST::CheckCall_gets)
112 .Case("getpw", &WalkAST::CheckCall_getpw)
113 .Case("mktemp", &WalkAST::CheckCall_mktemp)
114 .Cases("strcpy", "__strcpy_chk", &WalkAST::CheckCall_strcpy)
115 .Case("drand48", &WalkAST::CheckCall_rand)
116 .Case("erand48", &WalkAST::CheckCall_rand)
117 .Case("jrand48", &WalkAST::CheckCall_rand)
118 .Case("lrand48", &WalkAST::CheckCall_rand)
119 .Case("mrand48", &WalkAST::CheckCall_rand)
120 .Case("nrand48", &WalkAST::CheckCall_rand)
121 .Case("lcong48", &WalkAST::CheckCall_rand)
122 .Case("rand", &WalkAST::CheckCall_rand)
123 .Case("rand_r", &WalkAST::CheckCall_rand)
124 .Case("random", &WalkAST::CheckCall_random)
125 .Default(NULL);
126
127 // If the callee isn't defined, it is not of security concern.
128 // Check and evaluate the call.
129 if (evalFunction)
130 (this->*evalFunction)(CE, FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremenekefcbb152009-07-23 22:29:41 +0000132 // Recurse and check children.
133 VisitChildren(CE);
134}
135
Ted Kremenek65a81a92009-08-28 00:08:09 +0000136void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
137 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000138 if (Stmt *child = *I) {
139 if (CallExpr *CE = dyn_cast<CallExpr>(child))
140 CheckUncheckedReturnValue(CE);
141 Visit(child);
142 }
Ted Kremenek65a81a92009-08-28 00:08:09 +0000143}
144
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000145void WalkAST::VisitForStmt(ForStmt *FS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000146 CheckLoopConditionForFloat(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000147
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000148 // Recurse and check children.
149 VisitChildren(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000150}
151
152//===----------------------------------------------------------------------===//
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000153// Check: floating poing variable used as loop counter.
Ted Kremenek5abeb522009-07-23 21:44:18 +0000154// Originally: <rdar://problem/6336718>
155// Implements: CERT security coding advisory FLP-30.
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000156//===----------------------------------------------------------------------===//
157
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000158static const DeclRefExpr*
159GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
160 expr = expr->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000161
162 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000163 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCall2de56d12010-08-25 11:45:40 +0000164 B->getOpcode() == BO_Comma))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000165 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000167 if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
168 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000170 if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
171 return rhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000173 return NULL;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000174 }
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000176 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
177 const NamedDecl *ND = DR->getDecl();
178 return ND == x || ND == y ? DR : NULL;
179 }
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000181 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
182 return U->isIncrementDecrementOp()
183 ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
184
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000185 return NULL;
186}
187
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000188/// CheckLoopConditionForFloat - This check looks for 'for' statements that
189/// use a floating point variable as a loop counter.
190/// CERT: FLP30-C, FLP30-CPP.
191///
192void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
193 // Does the loop have a condition?
194 const Expr *condition = FS->getCond();
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000196 if (!condition)
197 return;
198
199 // Does the loop have an increment?
200 const Expr *increment = FS->getInc();
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000202 if (!increment)
203 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000205 // Strip away '()' and casts.
206 condition = condition->IgnoreParenCasts();
207 increment = increment->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000209 // Is the loop condition a comparison?
210 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
211
212 if (!B)
213 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Ted Kremenekcad9f412009-07-24 20:26:31 +0000215 // Is this a comparison?
216 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000217 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000219 // Are we comparing variables?
John McCallf6a16482010-12-04 03:47:34 +0000220 const DeclRefExpr *drLHS =
221 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
222 const DeclRefExpr *drRHS =
223 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Ted Kremenekcad9f412009-07-24 20:26:31 +0000225 // Does at least one of the variables have a floating point type?
Douglas Gregor0c293ea2010-06-22 23:07:26 +0000226 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
227 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000229 if (!drLHS && !drRHS)
230 return;
231
232 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
233 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000235 if (!vdLHS && !vdRHS)
Mike Stump1eb44332009-09-09 15:08:12 +0000236 return;
237
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000238 // Does either variable appear in increment?
239 const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000241 if (!drInc)
242 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000244 // Emit the error. First figure out which DeclRefExpr in the condition
245 // referenced the compared variable.
246 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
247
Mike Stump1eb44332009-09-09 15:08:12 +0000248 llvm::SmallVector<SourceRange, 2> ranges;
Ted Kremenek2c016762010-03-24 22:39:47 +0000249 llvm::SmallString<256> sbuf;
250 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Daniel Dunbar4087f272010-08-17 22:39:59 +0000252 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000253 << "' with floating point type '" << drCond->getType().getAsString()
254 << "' should not be used as a loop counter";
255
256 ranges.push_back(drCond->getSourceRange());
257 ranges.push_back(drInc->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000259 const char *bugType = "Floating point variable used as loop counter";
Benjamin Kramerf0171732009-11-29 18:27:55 +0000260 BR.EmitBasicReport(bugType, "Security", os.str(),
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000261 FS->getLocStart(), ranges.data(), ranges.size());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000262}
263
264//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +0000265// Check: Any use of 'gets' is insecure.
266// Originally: <rdar://problem/6335715>
267// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuaa30b3b2009-11-09 08:13:04 +0000268// CWE-242: Use of Inherently Dangerous Function
Ted Kremenekefcbb152009-07-23 22:29:41 +0000269//===----------------------------------------------------------------------===//
270
271void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000272 const FunctionProtoType *FPT
273 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000274 if (!FPT)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000275 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Ted Kremenekefcbb152009-07-23 22:29:41 +0000277 // Verify that the function takes a single argument.
Zhongxing Xubd842e32009-11-09 12:19:26 +0000278 if (FPT->getNumArgs() != 1)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000279 return;
280
281 // Is the argument a 'char*'?
Zhongxing Xubd842e32009-11-09 12:19:26 +0000282 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenekefcbb152009-07-23 22:29:41 +0000283 if (!PT)
284 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Ted Kremenekefcbb152009-07-23 22:29:41 +0000286 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
287 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Ted Kremenekefcbb152009-07-23 22:29:41 +0000289 // Issue a warning.
290 SourceRange R = CE->getCallee()->getSourceRange();
291 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
292 "Security",
293 "Call to function 'gets' is extremely insecure as it can "
294 "always result in a buffer overflow",
295 CE->getLocStart(), &R, 1);
296}
297
298//===----------------------------------------------------------------------===//
Zhongxing Xubd842e32009-11-09 12:19:26 +0000299// Check: Any use of 'getpwd' is insecure.
300// CWE-477: Use of Obsolete Functions
301//===----------------------------------------------------------------------===//
302
303void WalkAST::CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000304 const FunctionProtoType *FPT
305 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000306 if (!FPT)
307 return;
308
309 // Verify that the function takes two arguments.
310 if (FPT->getNumArgs() != 2)
311 return;
312
313 // Verify the first argument type is integer.
314 if (!FPT->getArgType(0)->isIntegerType())
315 return;
316
317 // Verify the second argument type is char*.
318 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
319 if (!PT)
320 return;
321
322 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
323 return;
324
325 // Issue a warning.
326 SourceRange R = CE->getCallee()->getSourceRange();
327 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
328 "Security",
329 "The getpw() function is dangerous as it may overflow the "
330 "provided buffer. It is obsoleted by getpwuid().",
331 CE->getLocStart(), &R, 1);
332}
333
334//===----------------------------------------------------------------------===//
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000335// Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
336// CWE-377: Insecure Temporary File
337//===----------------------------------------------------------------------===//
338
339void WalkAST::CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000340 const FunctionProtoType *FPT
341 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000342 if(!FPT)
343 return;
Ted Kremenek2c016762010-03-24 22:39:47 +0000344
Lenny Maioraniea4411e2011-03-31 21:26:55 +0000345 // Verify that the function takes a single argument.
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000346 if (FPT->getNumArgs() != 1)
347 return;
348
349 // Verify that the argument is Pointer Type.
350 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
351 if (!PT)
352 return;
353
354 // Verify that the argument is a 'char*'.
355 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
356 return;
Ted Kremenek431a2cb2010-03-24 22:39:45 +0000357
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000358 // Issue a waring.
359 SourceRange R = CE->getCallee()->getSourceRange();
360 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
Eli Friedmana7e68452010-08-22 01:00:03 +0000361 "Security",
362 "Call to function 'mktemp' is insecure as it always "
363 "creates or uses insecure temporary file. Use 'mkstemp' instead",
364 CE->getLocStart(), &R, 1);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000365}
366
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000367//===----------------------------------------------------------------------===//
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000368// Check: Any use of 'strcpy' is insecure.
369//
370// CWE-119: Improper Restriction of Operations within
371// the Bounds of a Memory Buffer
372//===----------------------------------------------------------------------===//
373void WalkAST::CheckCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000374 const FunctionProtoType *FPT
375 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
376 if (!FPT)
377 return;
378
379 // Verify the function takes two arguments
380 int numArgs = FPT->getNumArgs();
381 if (numArgs != 2 && numArgs != 3)
382 return;
383
384 // Verify the type for both arguments
385 for (int i = 0; i < 2; i++) {
386 // Verify that the arguments are pointers
387 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
388 if (!PT)
389 return;
390
391 // Verify that the argument is a 'char*'.
392 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
393 return;
394 }
395
396 // Issue a warning
397 SourceRange R = CE->getCallee()->getSourceRange();
398 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
399 "call 'strcpy'",
400 "Security",
401 "Call to function 'strcpy' is insecure as it does not "
402 "provide bounding of the memory buffer. Replace "
403 "unbounded copy functions with analogous functions that "
404 "support length arguments such as 'strncpy'. CWE-119.",
405 CE->getLocStart(), &R, 1);
406}
407
408//===----------------------------------------------------------------------===//
Ted Kremenek24650472009-09-02 02:47:41 +0000409// Check: Linear congruent random number generators should not be used
410// Originally: <rdar://problem/63371000>
411// CWE-338: Use of cryptographically weak prng
412//===----------------------------------------------------------------------===//
413
414void WalkAST::CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Lenny Maioranic2dace12011-04-03 05:07:11 +0000415 if (!CheckRand)
Ted Kremenek24650472009-09-02 02:47:41 +0000416 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Abramo Bagnara723df242010-12-14 22:11:44 +0000418 const FunctionProtoType *FTP
419 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000420 if (!FTP)
421 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Ted Kremenek24650472009-09-02 02:47:41 +0000423 if (FTP->getNumArgs() == 1) {
424 // Is the argument an 'unsigned short *'?
425 // (Actually any integer type is allowed.)
426 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
427 if (!PT)
428 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenek24650472009-09-02 02:47:41 +0000430 if (! PT->getPointeeType()->isIntegerType())
431 return;
432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433 else if (FTP->getNumArgs() != 0)
Ted Kremenek24650472009-09-02 02:47:41 +0000434 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremenek24650472009-09-02 02:47:41 +0000436 // Issue a warning.
Ted Kremenek2c016762010-03-24 22:39:47 +0000437 llvm::SmallString<256> buf1;
438 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000439 os1 << '\'' << FD << "' is a poor random number generator";
Ted Kremenek24650472009-09-02 02:47:41 +0000440
Ted Kremenek2c016762010-03-24 22:39:47 +0000441 llvm::SmallString<256> buf2;
442 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000443 os2 << "Function '" << FD
Ted Kremenek24650472009-09-02 02:47:41 +0000444 << "' is obsolete because it implements a poor random number generator."
445 << " Use 'arc4random' instead";
446
447 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek2c016762010-03-24 22:39:47 +0000448 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000449}
450
451//===----------------------------------------------------------------------===//
452// Check: 'random' should not be used
453// Originally: <rdar://problem/63371000>
454//===----------------------------------------------------------------------===//
455
456void WalkAST::CheckCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Lenny Maioranic2dace12011-04-03 05:07:11 +0000457 if (!CheckRand)
Ted Kremenek24650472009-09-02 02:47:41 +0000458 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Abramo Bagnara723df242010-12-14 22:11:44 +0000460 const FunctionProtoType *FTP
461 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000462 if (!FTP)
463 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Ted Kremenek24650472009-09-02 02:47:41 +0000465 // Verify that the function takes no argument.
466 if (FTP->getNumArgs() != 0)
467 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Ted Kremenek24650472009-09-02 02:47:41 +0000469 // Issue a warning.
470 SourceRange R = CE->getCallee()->getSourceRange();
471 BR.EmitBasicReport("'random' is not a secure random number generator",
472 "Security",
473 "The 'random' function produces a sequence of values that "
474 "an adversary may be able to predict. Use 'arc4random' "
Ted Kremenek2c016762010-03-24 22:39:47 +0000475 "instead", CE->getLocStart(), &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000476}
477
478//===----------------------------------------------------------------------===//
Ted Kremenek65a81a92009-08-28 00:08:09 +0000479// Check: Should check whether privileges are dropped successfully.
480// Originally: <rdar://problem/6337132>
481//===----------------------------------------------------------------------===//
482
483void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) {
484 const FunctionDecl *FD = CE->getDirectCallee();
485 if (!FD)
486 return;
487
488 if (II_setid[0] == NULL) {
Ted Kremenek24650472009-09-02 02:47:41 +0000489 static const char * const identifiers[num_setids] = {
Ted Kremenek65a81a92009-08-28 00:08:09 +0000490 "setuid", "setgid", "seteuid", "setegid",
491 "setreuid", "setregid"
492 };
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek24650472009-09-02 02:47:41 +0000494 for (size_t i = 0; i < num_setids; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000495 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000496 }
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Ted Kremenek65a81a92009-08-28 00:08:09 +0000498 const IdentifierInfo *id = FD->getIdentifier();
499 size_t identifierid;
500
Ted Kremenek24650472009-09-02 02:47:41 +0000501 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000502 if (id == II_setid[identifierid])
503 break;
504
Ted Kremenek24650472009-09-02 02:47:41 +0000505 if (identifierid >= num_setids)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000506 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Abramo Bagnara723df242010-12-14 22:11:44 +0000508 const FunctionProtoType *FTP
509 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek65a81a92009-08-28 00:08:09 +0000510 if (!FTP)
511 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Ted Kremeneka8187832009-08-28 00:24:55 +0000513 // Verify that the function takes one or two arguments (depending on
514 // the function).
Ted Kremenek65a81a92009-08-28 00:08:09 +0000515 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
516 return;
517
518 // The arguments must be integers.
519 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
520 if (! FTP->getArgType(i)->isIntegerType())
521 return;
522
523 // Issue a warning.
Ted Kremenek2c016762010-03-24 22:39:47 +0000524 llvm::SmallString<256> buf1;
525 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000526 os1 << "Return value is not checked in call to '" << FD << '\'';
Ted Kremenek65a81a92009-08-28 00:08:09 +0000527
Ted Kremenek2c016762010-03-24 22:39:47 +0000528 llvm::SmallString<256> buf2;
529 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000530 os2 << "The return value from the call to '" << FD
531 << "' is not checked. If an error occurs in '" << FD
Ted Kremenek65a81a92009-08-28 00:08:09 +0000532 << "', the following code may execute with unexpected privileges";
533
534 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek2c016762010-03-24 22:39:47 +0000535 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000536}
537
538//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000539// SecuritySyntaxChecker
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000540//===----------------------------------------------------------------------===//
541
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000542namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000543class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000544public:
545 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
546 BugReporter &BR) const {
547 WalkAST walker(BR);
548 walker.Visit(D->getBody());
549 }
550};
551}
552
553void ento::registerSecuritySyntaxChecker(CheckerManager &mgr) {
554 mgr.registerChecker<SecuritySyntaxChecker>();
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000555}