blob: b3a234f11a737d2a68e8dba5191c083cf2d6fcb6 [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
Ted Kremenekabf6ba12010-01-15 08:20:31 +000014#include "clang/Basic/TargetInfo.h"
Ted Kremenekd99bd552010-12-23 19:38:26 +000015#include "clang/StaticAnalyzer/BugReporter/BugReporter.h"
16#include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenek9c497622009-07-23 21:34:35 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000019
20using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000021using namespace ento;
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000022
Ted Kremenekabf6ba12010-01-15 08:20:31 +000023static bool isArc4RandomAvailable(const ASTContext &Ctx) {
24 const llvm::Triple &T = Ctx.Target.getTriple();
25 return T.getVendor() == llvm::Triple::Apple ||
Douglas Gregor45e84b02011-01-17 19:16:24 +000026 T.getOS() == llvm::Triple::FreeBSD ||
27 T.getOS() == llvm::Triple::NetBSD ||
28 T.getOS() == llvm::Triple::OpenBSD ||
29 T.getOS() == llvm::Triple::DragonFly;
Ted Kremenekabf6ba12010-01-15 08:20:31 +000030}
31
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000032namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000033class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump11289f42009-09-09 15:08:12 +000034 BugReporter &BR;
Ted Kremenekd032fcc2009-08-28 00:08:09 +000035 IdentifierInfo *II_gets;
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +000036 IdentifierInfo *II_getpw;
Zhongxing Xu39bba622009-12-03 09:15:23 +000037 IdentifierInfo *II_mktemp;
Ted Kremenekad5a6002009-09-02 02:47:41 +000038 enum { num_rands = 9 };
39 IdentifierInfo *II_rand[num_rands];
40 IdentifierInfo *II_random;
41 enum { num_setids = 6 };
42 IdentifierInfo *II_setid[num_setids];
Ted Kremenek8edc6df2010-03-24 22:39:47 +000043
Ted Kremenekabf6ba12010-01-15 08:20:31 +000044 const bool CheckRand;
Mike Stump11289f42009-09-09 15:08:12 +000045
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000046public:
Ted Kremenek6610c032009-07-23 22:29:41 +000047 WalkAST(BugReporter &br) : BR(br),
Eli Friedman04831922010-08-22 01:00:03 +000048 II_gets(0), II_getpw(0), II_mktemp(0),
49 II_rand(), II_random(0), II_setid(),
Ted Kremenekabf6ba12010-01-15 08:20:31 +000050 CheckRand(isArc4RandomAvailable(BR.getContext())) {}
Mike Stump11289f42009-09-09 15:08:12 +000051
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000052 // Statement visitor methods.
Ted Kremenek6610c032009-07-23 22:29:41 +000053 void VisitCallExpr(CallExpr *CE);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000054 void VisitForStmt(ForStmt *S);
Ted Kremenekd032fcc2009-08-28 00:08:09 +000055 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek9c497622009-07-23 21:34:35 +000056 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000057
58 void VisitChildren(Stmt *S);
Mike Stump11289f42009-09-09 15:08:12 +000059
Ted Kremenek6610c032009-07-23 22:29:41 +000060 // Helpers.
61 IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str);
Mike Stump11289f42009-09-09 15:08:12 +000062
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000063 // Checker-specific methods.
Ted Kremenek9c497622009-07-23 21:34:35 +000064 void CheckLoopConditionForFloat(const ForStmt *FS);
Ted Kremenek6610c032009-07-23 22:29:41 +000065 void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD);
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +000066 void CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
Zhongxing Xu39bba622009-12-03 09:15:23 +000067 void CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenekad5a6002009-09-02 02:47:41 +000068 void CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD);
69 void CheckCall_random(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenekd032fcc2009-08-28 00:08:09 +000070 void CheckUncheckedReturnValue(CallExpr *CE);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000071};
72} // end anonymous namespace
73
74//===----------------------------------------------------------------------===//
Ted Kremenek6610c032009-07-23 22:29:41 +000075// Helper methods.
76//===----------------------------------------------------------------------===//
77
78IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) {
79 if (!II)
80 II = &BR.getContext().Idents.get(str);
Mike Stump11289f42009-09-09 15:08:12 +000081
82 return II;
Ted Kremenek6610c032009-07-23 22:29:41 +000083}
84
85//===----------------------------------------------------------------------===//
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000086// AST walking.
87//===----------------------------------------------------------------------===//
88
89void WalkAST::VisitChildren(Stmt *S) {
90 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
91 if (Stmt *child = *I)
92 Visit(child);
93}
94
Ted Kremenek6610c032009-07-23 22:29:41 +000095void WalkAST::VisitCallExpr(CallExpr *CE) {
96 if (const FunctionDecl *FD = CE->getDirectCallee()) {
Ted Kremenekad5a6002009-09-02 02:47:41 +000097 CheckCall_gets(CE, FD);
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +000098 CheckCall_getpw(CE, FD);
Zhongxing Xu39bba622009-12-03 09:15:23 +000099 CheckCall_mktemp(CE, FD);
Ted Kremenekabf6ba12010-01-15 08:20:31 +0000100 if (CheckRand) {
101 CheckCall_rand(CE, FD);
102 CheckCall_random(CE, FD);
103 }
Ted Kremenek6610c032009-07-23 22:29:41 +0000104 }
Mike Stump11289f42009-09-09 15:08:12 +0000105
Ted Kremenek6610c032009-07-23 22:29:41 +0000106 // Recurse and check children.
107 VisitChildren(CE);
108}
109
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000110void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
111 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000112 if (Stmt *child = *I) {
113 if (CallExpr *CE = dyn_cast<CallExpr>(child))
114 CheckUncheckedReturnValue(CE);
115 Visit(child);
116 }
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000117}
118
Ted Kremenek9c497622009-07-23 21:34:35 +0000119void WalkAST::VisitForStmt(ForStmt *FS) {
Mike Stump11289f42009-09-09 15:08:12 +0000120 CheckLoopConditionForFloat(FS);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000121
Ted Kremenek9c497622009-07-23 21:34:35 +0000122 // Recurse and check children.
123 VisitChildren(FS);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000124}
125
126//===----------------------------------------------------------------------===//
Ted Kremenek9c497622009-07-23 21:34:35 +0000127// Check: floating poing variable used as loop counter.
Ted Kremenek70e55262009-07-23 21:44:18 +0000128// Originally: <rdar://problem/6336718>
129// Implements: CERT security coding advisory FLP-30.
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000130//===----------------------------------------------------------------------===//
131
Ted Kremenek9c497622009-07-23 21:34:35 +0000132static const DeclRefExpr*
133GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
134 expr = expr->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000135
136 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek9c497622009-07-23 21:34:35 +0000137 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCalle3027922010-08-25 11:45:40 +0000138 B->getOpcode() == BO_Comma))
Ted Kremenek9c497622009-07-23 21:34:35 +0000139 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000140
Ted Kremenek9c497622009-07-23 21:34:35 +0000141 if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
142 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +0000143
Ted Kremenek9c497622009-07-23 21:34:35 +0000144 if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
145 return rhs;
Mike Stump11289f42009-09-09 15:08:12 +0000146
Ted Kremenek9c497622009-07-23 21:34:35 +0000147 return NULL;
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000148 }
Mike Stump11289f42009-09-09 15:08:12 +0000149
Ted Kremenek9c497622009-07-23 21:34:35 +0000150 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
151 const NamedDecl *ND = DR->getDecl();
152 return ND == x || ND == y ? DR : NULL;
153 }
Mike Stump11289f42009-09-09 15:08:12 +0000154
Ted Kremenek9c497622009-07-23 21:34:35 +0000155 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
156 return U->isIncrementDecrementOp()
157 ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
158
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000159 return NULL;
160}
161
Ted Kremenek9c497622009-07-23 21:34:35 +0000162/// CheckLoopConditionForFloat - This check looks for 'for' statements that
163/// use a floating point variable as a loop counter.
164/// CERT: FLP30-C, FLP30-CPP.
165///
166void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
167 // Does the loop have a condition?
168 const Expr *condition = FS->getCond();
Mike Stump11289f42009-09-09 15:08:12 +0000169
Ted Kremenek9c497622009-07-23 21:34:35 +0000170 if (!condition)
171 return;
172
173 // Does the loop have an increment?
174 const Expr *increment = FS->getInc();
Mike Stump11289f42009-09-09 15:08:12 +0000175
Ted Kremenek9c497622009-07-23 21:34:35 +0000176 if (!increment)
177 return;
Mike Stump11289f42009-09-09 15:08:12 +0000178
Ted Kremenek9c497622009-07-23 21:34:35 +0000179 // Strip away '()' and casts.
180 condition = condition->IgnoreParenCasts();
181 increment = increment->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000182
Ted Kremenek9c497622009-07-23 21:34:35 +0000183 // Is the loop condition a comparison?
184 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
185
186 if (!B)
187 return;
Mike Stump11289f42009-09-09 15:08:12 +0000188
Ted Kremenekb9cb1132009-07-24 20:26:31 +0000189 // Is this a comparison?
190 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek9c497622009-07-23 21:34:35 +0000191 return;
Mike Stump11289f42009-09-09 15:08:12 +0000192
Ted Kremenek9c497622009-07-23 21:34:35 +0000193 // Are we comparing variables?
John McCall34376a62010-12-04 03:47:34 +0000194 const DeclRefExpr *drLHS =
195 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
196 const DeclRefExpr *drRHS =
197 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump11289f42009-09-09 15:08:12 +0000198
Ted Kremenekb9cb1132009-07-24 20:26:31 +0000199 // Does at least one of the variables have a floating point type?
Douglas Gregor49b4d732010-06-22 23:07:26 +0000200 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
201 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000202
Ted Kremenek9c497622009-07-23 21:34:35 +0000203 if (!drLHS && !drRHS)
204 return;
205
206 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
207 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000208
Ted Kremenek9c497622009-07-23 21:34:35 +0000209 if (!vdLHS && !vdRHS)
Mike Stump11289f42009-09-09 15:08:12 +0000210 return;
211
Ted Kremenek9c497622009-07-23 21:34:35 +0000212 // Does either variable appear in increment?
213 const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump11289f42009-09-09 15:08:12 +0000214
Ted Kremenek9c497622009-07-23 21:34:35 +0000215 if (!drInc)
216 return;
Mike Stump11289f42009-09-09 15:08:12 +0000217
Ted Kremenek9c497622009-07-23 21:34:35 +0000218 // Emit the error. First figure out which DeclRefExpr in the condition
219 // referenced the compared variable.
220 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
221
Mike Stump11289f42009-09-09 15:08:12 +0000222 llvm::SmallVector<SourceRange, 2> ranges;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000223 llvm::SmallString<256> sbuf;
224 llvm::raw_svector_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +0000225
Daniel Dunbar56df9772010-08-17 22:39:59 +0000226 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek9c497622009-07-23 21:34:35 +0000227 << "' with floating point type '" << drCond->getType().getAsString()
228 << "' should not be used as a loop counter";
229
230 ranges.push_back(drCond->getSourceRange());
231 ranges.push_back(drInc->getSourceRange());
Mike Stump11289f42009-09-09 15:08:12 +0000232
Ted Kremenek9c497622009-07-23 21:34:35 +0000233 const char *bugType = "Floating point variable used as loop counter";
Benjamin Kramer63415532009-11-29 18:27:55 +0000234 BR.EmitBasicReport(bugType, "Security", os.str(),
Ted Kremenek9c497622009-07-23 21:34:35 +0000235 FS->getLocStart(), ranges.data(), ranges.size());
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000236}
237
238//===----------------------------------------------------------------------===//
Ted Kremenek6610c032009-07-23 22:29:41 +0000239// Check: Any use of 'gets' is insecure.
240// Originally: <rdar://problem/6335715>
241// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuf69973c2009-11-09 08:13:04 +0000242// CWE-242: Use of Inherently Dangerous Function
Ted Kremenek6610c032009-07-23 22:29:41 +0000243//===----------------------------------------------------------------------===//
244
245void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
246 if (FD->getIdentifier() != GetIdentifier(II_gets, "gets"))
247 return;
Mike Stump11289f42009-09-09 15:08:12 +0000248
Abramo Bagnara6d810632010-12-14 22:11:44 +0000249 const FunctionProtoType *FPT
250 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000251 if (!FPT)
Ted Kremenek6610c032009-07-23 22:29:41 +0000252 return;
Mike Stump11289f42009-09-09 15:08:12 +0000253
Ted Kremenek6610c032009-07-23 22:29:41 +0000254 // Verify that the function takes a single argument.
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000255 if (FPT->getNumArgs() != 1)
Ted Kremenek6610c032009-07-23 22:29:41 +0000256 return;
257
258 // Is the argument a 'char*'?
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000259 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenek6610c032009-07-23 22:29:41 +0000260 if (!PT)
261 return;
Mike Stump11289f42009-09-09 15:08:12 +0000262
Ted Kremenek6610c032009-07-23 22:29:41 +0000263 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
264 return;
Mike Stump11289f42009-09-09 15:08:12 +0000265
Ted Kremenek6610c032009-07-23 22:29:41 +0000266 // Issue a warning.
267 SourceRange R = CE->getCallee()->getSourceRange();
268 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
269 "Security",
270 "Call to function 'gets' is extremely insecure as it can "
271 "always result in a buffer overflow",
272 CE->getLocStart(), &R, 1);
273}
274
275//===----------------------------------------------------------------------===//
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000276// Check: Any use of 'getpwd' is insecure.
277// CWE-477: Use of Obsolete Functions
278//===----------------------------------------------------------------------===//
279
280void WalkAST::CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
281 if (FD->getIdentifier() != GetIdentifier(II_getpw, "getpw"))
282 return;
283
Abramo Bagnara6d810632010-12-14 22:11:44 +0000284 const FunctionProtoType *FPT
285 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000286 if (!FPT)
287 return;
288
289 // Verify that the function takes two arguments.
290 if (FPT->getNumArgs() != 2)
291 return;
292
293 // Verify the first argument type is integer.
294 if (!FPT->getArgType(0)->isIntegerType())
295 return;
296
297 // Verify the second argument type is char*.
298 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
299 if (!PT)
300 return;
301
302 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
303 return;
304
305 // Issue a warning.
306 SourceRange R = CE->getCallee()->getSourceRange();
307 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
308 "Security",
309 "The getpw() function is dangerous as it may overflow the "
310 "provided buffer. It is obsoleted by getpwuid().",
311 CE->getLocStart(), &R, 1);
312}
313
314//===----------------------------------------------------------------------===//
Zhongxing Xu39bba622009-12-03 09:15:23 +0000315// Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
316// CWE-377: Insecure Temporary File
317//===----------------------------------------------------------------------===//
318
319void WalkAST::CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
320 if (FD->getIdentifier() != GetIdentifier(II_mktemp, "mktemp"))
321 return;
322
Abramo Bagnara6d810632010-12-14 22:11:44 +0000323 const FunctionProtoType *FPT
324 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xu39bba622009-12-03 09:15:23 +0000325 if(!FPT)
326 return;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000327
Zhongxing Xu39bba622009-12-03 09:15:23 +0000328 // Verify that the funcion takes a single argument.
329 if (FPT->getNumArgs() != 1)
330 return;
331
332 // Verify that the argument is Pointer Type.
333 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
334 if (!PT)
335 return;
336
337 // Verify that the argument is a 'char*'.
338 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
339 return;
Ted Kremenekaeaf3d22010-03-24 22:39:45 +0000340
Zhongxing Xu39bba622009-12-03 09:15:23 +0000341 // Issue a waring.
342 SourceRange R = CE->getCallee()->getSourceRange();
343 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
Eli Friedman04831922010-08-22 01:00:03 +0000344 "Security",
345 "Call to function 'mktemp' is insecure as it always "
346 "creates or uses insecure temporary file. Use 'mkstemp' instead",
347 CE->getLocStart(), &R, 1);
Zhongxing Xu39bba622009-12-03 09:15:23 +0000348}
349
Zhongxing Xu39bba622009-12-03 09:15:23 +0000350//===----------------------------------------------------------------------===//
Ted Kremenekad5a6002009-09-02 02:47:41 +0000351// Check: Linear congruent random number generators should not be used
352// Originally: <rdar://problem/63371000>
353// CWE-338: Use of cryptographically weak prng
354//===----------------------------------------------------------------------===//
355
356void WalkAST::CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
357 if (II_rand[0] == NULL) {
358 // This check applies to these functions
359 static const char * const identifiers[num_rands] = {
360 "drand48", "erand48", "jrand48", "lrand48", "mrand48", "nrand48",
361 "lcong48",
362 "rand", "rand_r"
363 };
Mike Stump11289f42009-09-09 15:08:12 +0000364
Ted Kremenekad5a6002009-09-02 02:47:41 +0000365 for (size_t i = 0; i < num_rands; i++)
Mike Stump11289f42009-09-09 15:08:12 +0000366 II_rand[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenekad5a6002009-09-02 02:47:41 +0000367 }
Mike Stump11289f42009-09-09 15:08:12 +0000368
Ted Kremenekad5a6002009-09-02 02:47:41 +0000369 const IdentifierInfo *id = FD->getIdentifier();
370 size_t identifierid;
371
372 for (identifierid = 0; identifierid < num_rands; identifierid++)
373 if (id == II_rand[identifierid])
374 break;
375
376 if (identifierid >= num_rands)
377 return;
Mike Stump11289f42009-09-09 15:08:12 +0000378
Abramo Bagnara6d810632010-12-14 22:11:44 +0000379 const FunctionProtoType *FTP
380 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000381 if (!FTP)
382 return;
Mike Stump11289f42009-09-09 15:08:12 +0000383
Ted Kremenekad5a6002009-09-02 02:47:41 +0000384 if (FTP->getNumArgs() == 1) {
385 // Is the argument an 'unsigned short *'?
386 // (Actually any integer type is allowed.)
387 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
388 if (!PT)
389 return;
Mike Stump11289f42009-09-09 15:08:12 +0000390
Ted Kremenekad5a6002009-09-02 02:47:41 +0000391 if (! PT->getPointeeType()->isIntegerType())
392 return;
393 }
Mike Stump11289f42009-09-09 15:08:12 +0000394 else if (FTP->getNumArgs() != 0)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000395 return;
Mike Stump11289f42009-09-09 15:08:12 +0000396
Ted Kremenekad5a6002009-09-02 02:47:41 +0000397 // Issue a warning.
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000398 llvm::SmallString<256> buf1;
399 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000400 os1 << '\'' << FD << "' is a poor random number generator";
Ted Kremenekad5a6002009-09-02 02:47:41 +0000401
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000402 llvm::SmallString<256> buf2;
403 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000404 os2 << "Function '" << FD
Ted Kremenekad5a6002009-09-02 02:47:41 +0000405 << "' is obsolete because it implements a poor random number generator."
406 << " Use 'arc4random' instead";
407
408 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000409 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenekad5a6002009-09-02 02:47:41 +0000410}
411
412//===----------------------------------------------------------------------===//
413// Check: 'random' should not be used
414// Originally: <rdar://problem/63371000>
415//===----------------------------------------------------------------------===//
416
417void WalkAST::CheckCall_random(const CallExpr *CE, const FunctionDecl *FD) {
418 if (FD->getIdentifier() != GetIdentifier(II_random, "random"))
419 return;
Mike Stump11289f42009-09-09 15:08:12 +0000420
Abramo Bagnara6d810632010-12-14 22:11:44 +0000421 const FunctionProtoType *FTP
422 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000423 if (!FTP)
424 return;
Mike Stump11289f42009-09-09 15:08:12 +0000425
Ted Kremenekad5a6002009-09-02 02:47:41 +0000426 // Verify that the function takes no argument.
427 if (FTP->getNumArgs() != 0)
428 return;
Mike Stump11289f42009-09-09 15:08:12 +0000429
Ted Kremenekad5a6002009-09-02 02:47:41 +0000430 // Issue a warning.
431 SourceRange R = CE->getCallee()->getSourceRange();
432 BR.EmitBasicReport("'random' is not a secure random number generator",
433 "Security",
434 "The 'random' function produces a sequence of values that "
435 "an adversary may be able to predict. Use 'arc4random' "
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000436 "instead", CE->getLocStart(), &R, 1);
Ted Kremenekad5a6002009-09-02 02:47:41 +0000437}
438
439//===----------------------------------------------------------------------===//
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000440// Check: Should check whether privileges are dropped successfully.
441// Originally: <rdar://problem/6337132>
442//===----------------------------------------------------------------------===//
443
444void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) {
445 const FunctionDecl *FD = CE->getDirectCallee();
446 if (!FD)
447 return;
448
449 if (II_setid[0] == NULL) {
Ted Kremenekad5a6002009-09-02 02:47:41 +0000450 static const char * const identifiers[num_setids] = {
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000451 "setuid", "setgid", "seteuid", "setegid",
452 "setreuid", "setregid"
453 };
Mike Stump11289f42009-09-09 15:08:12 +0000454
Ted Kremenekad5a6002009-09-02 02:47:41 +0000455 for (size_t i = 0; i < num_setids; i++)
Mike Stump11289f42009-09-09 15:08:12 +0000456 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000457 }
Mike Stump11289f42009-09-09 15:08:12 +0000458
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000459 const IdentifierInfo *id = FD->getIdentifier();
460 size_t identifierid;
461
Ted Kremenekad5a6002009-09-02 02:47:41 +0000462 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000463 if (id == II_setid[identifierid])
464 break;
465
Ted Kremenekad5a6002009-09-02 02:47:41 +0000466 if (identifierid >= num_setids)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000467 return;
Mike Stump11289f42009-09-09 15:08:12 +0000468
Abramo Bagnara6d810632010-12-14 22:11:44 +0000469 const FunctionProtoType *FTP
470 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000471 if (!FTP)
472 return;
Mike Stump11289f42009-09-09 15:08:12 +0000473
Ted Kremenekaeca0952009-08-28 00:24:55 +0000474 // Verify that the function takes one or two arguments (depending on
475 // the function).
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000476 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
477 return;
478
479 // The arguments must be integers.
480 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
481 if (! FTP->getArgType(i)->isIntegerType())
482 return;
483
484 // Issue a warning.
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000485 llvm::SmallString<256> buf1;
486 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000487 os1 << "Return value is not checked in call to '" << FD << '\'';
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000488
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000489 llvm::SmallString<256> buf2;
490 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000491 os2 << "The return value from the call to '" << FD
492 << "' is not checked. If an error occurs in '" << FD
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000493 << "', the following code may execute with unexpected privileges";
494
495 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000496 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000497}
498
499//===----------------------------------------------------------------------===//
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000500// Entry point for check.
501//===----------------------------------------------------------------------===//
502
Ted Kremenek98857c92010-12-23 07:20:52 +0000503void ento::CheckSecuritySyntaxOnly(const Decl *D, BugReporter &BR) {
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000504 WalkAST walker(BR);
Mike Stump11289f42009-09-09 15:08:12 +0000505 walker.Visit(D->getBody());
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000506}