blob: b30e985f38a55a1983baccb55350ae8b0482c692 [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
Ted Kremenek88c8bc82010-01-15 08:20:31 +000014#include "clang/Basic/TargetInfo.h"
Ted Kremenek21142582010-12-23 19:38:26 +000015#include "clang/StaticAnalyzer/BugReporter/BugReporter.h"
16#include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenek8baf86d2009-07-23 21:34:35 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000019
20using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000021using namespace ento;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000022
Ted Kremenek88c8bc82010-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 ||
26 T.getOS() == llvm::Triple::FreeBSD;
27}
28
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000029namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000030class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump1eb44332009-09-09 15:08:12 +000031 BugReporter &BR;
Ted Kremenek65a81a92009-08-28 00:08:09 +000032 IdentifierInfo *II_gets;
Zhongxing Xubd842e32009-11-09 12:19:26 +000033 IdentifierInfo *II_getpw;
Zhongxing Xu1bf40562009-12-03 09:15:23 +000034 IdentifierInfo *II_mktemp;
Ted Kremenek24650472009-09-02 02:47:41 +000035 enum { num_rands = 9 };
36 IdentifierInfo *II_rand[num_rands];
37 IdentifierInfo *II_random;
38 enum { num_setids = 6 };
39 IdentifierInfo *II_setid[num_setids];
Ted Kremenek2c016762010-03-24 22:39:47 +000040
Ted Kremenek88c8bc82010-01-15 08:20:31 +000041 const bool CheckRand;
Mike Stump1eb44332009-09-09 15:08:12 +000042
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000043public:
Ted Kremenekefcbb152009-07-23 22:29:41 +000044 WalkAST(BugReporter &br) : BR(br),
Eli Friedmana7e68452010-08-22 01:00:03 +000045 II_gets(0), II_getpw(0), II_mktemp(0),
46 II_rand(), II_random(0), II_setid(),
Ted Kremenek88c8bc82010-01-15 08:20:31 +000047 CheckRand(isArc4RandomAvailable(BR.getContext())) {}
Mike Stump1eb44332009-09-09 15:08:12 +000048
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000049 // Statement visitor methods.
Ted Kremenekefcbb152009-07-23 22:29:41 +000050 void VisitCallExpr(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000051 void VisitForStmt(ForStmt *S);
Ted Kremenek65a81a92009-08-28 00:08:09 +000052 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek8baf86d2009-07-23 21:34:35 +000053 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000054
55 void VisitChildren(Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +000056
Ted Kremenekefcbb152009-07-23 22:29:41 +000057 // Helpers.
58 IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str);
Mike Stump1eb44332009-09-09 15:08:12 +000059
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);
Ted Kremenek24650472009-09-02 02:47:41 +000065 void CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD);
66 void CheckCall_random(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenek65a81a92009-08-28 00:08:09 +000067 void CheckUncheckedReturnValue(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000068};
69} // end anonymous namespace
70
71//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +000072// Helper methods.
73//===----------------------------------------------------------------------===//
74
75IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) {
76 if (!II)
77 II = &BR.getContext().Idents.get(str);
Mike Stump1eb44332009-09-09 15:08:12 +000078
79 return II;
Ted Kremenekefcbb152009-07-23 22:29:41 +000080}
81
82//===----------------------------------------------------------------------===//
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000083// AST walking.
84//===----------------------------------------------------------------------===//
85
86void WalkAST::VisitChildren(Stmt *S) {
87 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
88 if (Stmt *child = *I)
89 Visit(child);
90}
91
Ted Kremenekefcbb152009-07-23 22:29:41 +000092void WalkAST::VisitCallExpr(CallExpr *CE) {
93 if (const FunctionDecl *FD = CE->getDirectCallee()) {
Ted Kremenek24650472009-09-02 02:47:41 +000094 CheckCall_gets(CE, FD);
Zhongxing Xubd842e32009-11-09 12:19:26 +000095 CheckCall_getpw(CE, FD);
Zhongxing Xu1bf40562009-12-03 09:15:23 +000096 CheckCall_mktemp(CE, FD);
Ted Kremenek88c8bc82010-01-15 08:20:31 +000097 if (CheckRand) {
98 CheckCall_rand(CE, FD);
99 CheckCall_random(CE, FD);
100 }
Ted Kremenekefcbb152009-07-23 22:29:41 +0000101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenekefcbb152009-07-23 22:29:41 +0000103 // Recurse and check children.
104 VisitChildren(CE);
105}
106
Ted Kremenek65a81a92009-08-28 00:08:09 +0000107void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
108 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000109 if (Stmt *child = *I) {
110 if (CallExpr *CE = dyn_cast<CallExpr>(child))
111 CheckUncheckedReturnValue(CE);
112 Visit(child);
113 }
Ted Kremenek65a81a92009-08-28 00:08:09 +0000114}
115
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000116void WalkAST::VisitForStmt(ForStmt *FS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000117 CheckLoopConditionForFloat(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000118
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000119 // Recurse and check children.
120 VisitChildren(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000121}
122
123//===----------------------------------------------------------------------===//
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000124// Check: floating poing variable used as loop counter.
Ted Kremenek5abeb522009-07-23 21:44:18 +0000125// Originally: <rdar://problem/6336718>
126// Implements: CERT security coding advisory FLP-30.
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000127//===----------------------------------------------------------------------===//
128
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000129static const DeclRefExpr*
130GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
131 expr = expr->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000132
133 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000134 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCall2de56d12010-08-25 11:45:40 +0000135 B->getOpcode() == BO_Comma))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000136 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000138 if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
139 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000141 if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
142 return rhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000144 return NULL;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000145 }
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000147 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
148 const NamedDecl *ND = DR->getDecl();
149 return ND == x || ND == y ? DR : NULL;
150 }
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000152 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
153 return U->isIncrementDecrementOp()
154 ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
155
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000156 return NULL;
157}
158
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000159/// CheckLoopConditionForFloat - This check looks for 'for' statements that
160/// use a floating point variable as a loop counter.
161/// CERT: FLP30-C, FLP30-CPP.
162///
163void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
164 // Does the loop have a condition?
165 const Expr *condition = FS->getCond();
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000167 if (!condition)
168 return;
169
170 // Does the loop have an increment?
171 const Expr *increment = FS->getInc();
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000173 if (!increment)
174 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000176 // Strip away '()' and casts.
177 condition = condition->IgnoreParenCasts();
178 increment = increment->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000180 // Is the loop condition a comparison?
181 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
182
183 if (!B)
184 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremenekcad9f412009-07-24 20:26:31 +0000186 // Is this a comparison?
187 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000188 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000190 // Are we comparing variables?
John McCallf6a16482010-12-04 03:47:34 +0000191 const DeclRefExpr *drLHS =
192 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
193 const DeclRefExpr *drRHS =
194 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Ted Kremenekcad9f412009-07-24 20:26:31 +0000196 // Does at least one of the variables have a floating point type?
Douglas Gregor0c293ea2010-06-22 23:07:26 +0000197 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
198 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000200 if (!drLHS && !drRHS)
201 return;
202
203 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
204 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000206 if (!vdLHS && !vdRHS)
Mike Stump1eb44332009-09-09 15:08:12 +0000207 return;
208
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000209 // Does either variable appear in increment?
210 const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000212 if (!drInc)
213 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000215 // Emit the error. First figure out which DeclRefExpr in the condition
216 // referenced the compared variable.
217 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
218
Mike Stump1eb44332009-09-09 15:08:12 +0000219 llvm::SmallVector<SourceRange, 2> ranges;
Ted Kremenek2c016762010-03-24 22:39:47 +0000220 llvm::SmallString<256> sbuf;
221 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Daniel Dunbar4087f272010-08-17 22:39:59 +0000223 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000224 << "' with floating point type '" << drCond->getType().getAsString()
225 << "' should not be used as a loop counter";
226
227 ranges.push_back(drCond->getSourceRange());
228 ranges.push_back(drInc->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000230 const char *bugType = "Floating point variable used as loop counter";
Benjamin Kramerf0171732009-11-29 18:27:55 +0000231 BR.EmitBasicReport(bugType, "Security", os.str(),
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000232 FS->getLocStart(), ranges.data(), ranges.size());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000233}
234
235//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +0000236// Check: Any use of 'gets' is insecure.
237// Originally: <rdar://problem/6335715>
238// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuaa30b3b2009-11-09 08:13:04 +0000239// CWE-242: Use of Inherently Dangerous Function
Ted Kremenekefcbb152009-07-23 22:29:41 +0000240//===----------------------------------------------------------------------===//
241
242void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
243 if (FD->getIdentifier() != GetIdentifier(II_gets, "gets"))
244 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Abramo Bagnara723df242010-12-14 22:11:44 +0000246 const FunctionProtoType *FPT
247 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000248 if (!FPT)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000249 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Ted Kremenekefcbb152009-07-23 22:29:41 +0000251 // Verify that the function takes a single argument.
Zhongxing Xubd842e32009-11-09 12:19:26 +0000252 if (FPT->getNumArgs() != 1)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000253 return;
254
255 // Is the argument a 'char*'?
Zhongxing Xubd842e32009-11-09 12:19:26 +0000256 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenekefcbb152009-07-23 22:29:41 +0000257 if (!PT)
258 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Ted Kremenekefcbb152009-07-23 22:29:41 +0000260 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
261 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Ted Kremenekefcbb152009-07-23 22:29:41 +0000263 // Issue a warning.
264 SourceRange R = CE->getCallee()->getSourceRange();
265 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
266 "Security",
267 "Call to function 'gets' is extremely insecure as it can "
268 "always result in a buffer overflow",
269 CE->getLocStart(), &R, 1);
270}
271
272//===----------------------------------------------------------------------===//
Zhongxing Xubd842e32009-11-09 12:19:26 +0000273// Check: Any use of 'getpwd' is insecure.
274// CWE-477: Use of Obsolete Functions
275//===----------------------------------------------------------------------===//
276
277void WalkAST::CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
278 if (FD->getIdentifier() != GetIdentifier(II_getpw, "getpw"))
279 return;
280
Abramo Bagnara723df242010-12-14 22:11:44 +0000281 const FunctionProtoType *FPT
282 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000283 if (!FPT)
284 return;
285
286 // Verify that the function takes two arguments.
287 if (FPT->getNumArgs() != 2)
288 return;
289
290 // Verify the first argument type is integer.
291 if (!FPT->getArgType(0)->isIntegerType())
292 return;
293
294 // Verify the second argument type is char*.
295 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
296 if (!PT)
297 return;
298
299 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
300 return;
301
302 // Issue a warning.
303 SourceRange R = CE->getCallee()->getSourceRange();
304 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
305 "Security",
306 "The getpw() function is dangerous as it may overflow the "
307 "provided buffer. It is obsoleted by getpwuid().",
308 CE->getLocStart(), &R, 1);
309}
310
311//===----------------------------------------------------------------------===//
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000312// Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
313// CWE-377: Insecure Temporary File
314//===----------------------------------------------------------------------===//
315
316void WalkAST::CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
317 if (FD->getIdentifier() != GetIdentifier(II_mktemp, "mktemp"))
318 return;
319
Abramo Bagnara723df242010-12-14 22:11:44 +0000320 const FunctionProtoType *FPT
321 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000322 if(!FPT)
323 return;
Ted Kremenek2c016762010-03-24 22:39:47 +0000324
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000325 // Verify that the funcion takes a single argument.
326 if (FPT->getNumArgs() != 1)
327 return;
328
329 // Verify that the argument is Pointer Type.
330 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
331 if (!PT)
332 return;
333
334 // Verify that the argument is a 'char*'.
335 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
336 return;
Ted Kremenek431a2cb2010-03-24 22:39:45 +0000337
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000338 // Issue a waring.
339 SourceRange R = CE->getCallee()->getSourceRange();
340 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
Eli Friedmana7e68452010-08-22 01:00:03 +0000341 "Security",
342 "Call to function 'mktemp' is insecure as it always "
343 "creates or uses insecure temporary file. Use 'mkstemp' instead",
344 CE->getLocStart(), &R, 1);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000345}
346
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000347//===----------------------------------------------------------------------===//
Ted Kremenek24650472009-09-02 02:47:41 +0000348// Check: Linear congruent random number generators should not be used
349// Originally: <rdar://problem/63371000>
350// CWE-338: Use of cryptographically weak prng
351//===----------------------------------------------------------------------===//
352
353void WalkAST::CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
354 if (II_rand[0] == NULL) {
355 // This check applies to these functions
356 static const char * const identifiers[num_rands] = {
357 "drand48", "erand48", "jrand48", "lrand48", "mrand48", "nrand48",
358 "lcong48",
359 "rand", "rand_r"
360 };
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek24650472009-09-02 02:47:41 +0000362 for (size_t i = 0; i < num_rands; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000363 II_rand[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek24650472009-09-02 02:47:41 +0000364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Ted Kremenek24650472009-09-02 02:47:41 +0000366 const IdentifierInfo *id = FD->getIdentifier();
367 size_t identifierid;
368
369 for (identifierid = 0; identifierid < num_rands; identifierid++)
370 if (id == II_rand[identifierid])
371 break;
372
373 if (identifierid >= num_rands)
374 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Abramo Bagnara723df242010-12-14 22:11:44 +0000376 const FunctionProtoType *FTP
377 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000378 if (!FTP)
379 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek24650472009-09-02 02:47:41 +0000381 if (FTP->getNumArgs() == 1) {
382 // Is the argument an 'unsigned short *'?
383 // (Actually any integer type is allowed.)
384 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
385 if (!PT)
386 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Ted Kremenek24650472009-09-02 02:47:41 +0000388 if (! PT->getPointeeType()->isIntegerType())
389 return;
390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391 else if (FTP->getNumArgs() != 0)
Ted Kremenek24650472009-09-02 02:47:41 +0000392 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek24650472009-09-02 02:47:41 +0000394 // Issue a warning.
Ted Kremenek2c016762010-03-24 22:39:47 +0000395 llvm::SmallString<256> buf1;
396 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000397 os1 << '\'' << FD << "' is a poor random number generator";
Ted Kremenek24650472009-09-02 02:47:41 +0000398
Ted Kremenek2c016762010-03-24 22:39:47 +0000399 llvm::SmallString<256> buf2;
400 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000401 os2 << "Function '" << FD
Ted Kremenek24650472009-09-02 02:47:41 +0000402 << "' is obsolete because it implements a poor random number generator."
403 << " Use 'arc4random' instead";
404
405 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek2c016762010-03-24 22:39:47 +0000406 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000407}
408
409//===----------------------------------------------------------------------===//
410// Check: 'random' should not be used
411// Originally: <rdar://problem/63371000>
412//===----------------------------------------------------------------------===//
413
414void WalkAST::CheckCall_random(const CallExpr *CE, const FunctionDecl *FD) {
415 if (FD->getIdentifier() != GetIdentifier(II_random, "random"))
416 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 // Verify that the function takes no argument.
424 if (FTP->getNumArgs() != 0)
425 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek24650472009-09-02 02:47:41 +0000427 // Issue a warning.
428 SourceRange R = CE->getCallee()->getSourceRange();
429 BR.EmitBasicReport("'random' is not a secure random number generator",
430 "Security",
431 "The 'random' function produces a sequence of values that "
432 "an adversary may be able to predict. Use 'arc4random' "
Ted Kremenek2c016762010-03-24 22:39:47 +0000433 "instead", CE->getLocStart(), &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000434}
435
436//===----------------------------------------------------------------------===//
Ted Kremenek65a81a92009-08-28 00:08:09 +0000437// Check: Should check whether privileges are dropped successfully.
438// Originally: <rdar://problem/6337132>
439//===----------------------------------------------------------------------===//
440
441void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) {
442 const FunctionDecl *FD = CE->getDirectCallee();
443 if (!FD)
444 return;
445
446 if (II_setid[0] == NULL) {
Ted Kremenek24650472009-09-02 02:47:41 +0000447 static const char * const identifiers[num_setids] = {
Ted Kremenek65a81a92009-08-28 00:08:09 +0000448 "setuid", "setgid", "seteuid", "setegid",
449 "setreuid", "setregid"
450 };
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenek24650472009-09-02 02:47:41 +0000452 for (size_t i = 0; i < num_setids; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000453 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Ted Kremenek65a81a92009-08-28 00:08:09 +0000456 const IdentifierInfo *id = FD->getIdentifier();
457 size_t identifierid;
458
Ted Kremenek24650472009-09-02 02:47:41 +0000459 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000460 if (id == II_setid[identifierid])
461 break;
462
Ted Kremenek24650472009-09-02 02:47:41 +0000463 if (identifierid >= num_setids)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000464 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Abramo Bagnara723df242010-12-14 22:11:44 +0000466 const FunctionProtoType *FTP
467 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek65a81a92009-08-28 00:08:09 +0000468 if (!FTP)
469 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Ted Kremeneka8187832009-08-28 00:24:55 +0000471 // Verify that the function takes one or two arguments (depending on
472 // the function).
Ted Kremenek65a81a92009-08-28 00:08:09 +0000473 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
474 return;
475
476 // The arguments must be integers.
477 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
478 if (! FTP->getArgType(i)->isIntegerType())
479 return;
480
481 // Issue a warning.
Ted Kremenek2c016762010-03-24 22:39:47 +0000482 llvm::SmallString<256> buf1;
483 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000484 os1 << "Return value is not checked in call to '" << FD << '\'';
Ted Kremenek65a81a92009-08-28 00:08:09 +0000485
Ted Kremenek2c016762010-03-24 22:39:47 +0000486 llvm::SmallString<256> buf2;
487 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000488 os2 << "The return value from the call to '" << FD
489 << "' is not checked. If an error occurs in '" << FD
Ted Kremenek65a81a92009-08-28 00:08:09 +0000490 << "', the following code may execute with unexpected privileges";
491
492 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek2c016762010-03-24 22:39:47 +0000493 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000494}
495
496//===----------------------------------------------------------------------===//
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000497// Entry point for check.
498//===----------------------------------------------------------------------===//
499
Ted Kremenek9ef65372010-12-23 07:20:52 +0000500void ento::CheckSecuritySyntaxOnly(const Decl *D, BugReporter &BR) {
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000501 WalkAST walker(BR);
Mike Stump1eb44332009-09-09 15:08:12 +0000502 walker.Visit(D->getBody());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000503}