blob: e037cd315ecdbf6b8edbb4e5e87dee333e5b8805 [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"
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000020
21using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000022using namespace ento;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000023
Ted Kremenek88c8bc82010-01-15 08:20:31 +000024static bool isArc4RandomAvailable(const ASTContext &Ctx) {
25 const llvm::Triple &T = Ctx.Target.getTriple();
26 return T.getVendor() == llvm::Triple::Apple ||
Douglas Gregor0f565592011-01-17 19:16:24 +000027 T.getOS() == llvm::Triple::FreeBSD ||
28 T.getOS() == llvm::Triple::NetBSD ||
29 T.getOS() == llvm::Triple::OpenBSD ||
30 T.getOS() == llvm::Triple::DragonFly;
Ted Kremenek88c8bc82010-01-15 08:20:31 +000031}
32
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000033namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000034class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump1eb44332009-09-09 15:08:12 +000035 BugReporter &BR;
Ted Kremenek65a81a92009-08-28 00:08:09 +000036 IdentifierInfo *II_gets;
Zhongxing Xubd842e32009-11-09 12:19:26 +000037 IdentifierInfo *II_getpw;
Zhongxing Xu1bf40562009-12-03 09:15:23 +000038 IdentifierInfo *II_mktemp;
Ted Kremenek24650472009-09-02 02:47:41 +000039 enum { num_rands = 9 };
40 IdentifierInfo *II_rand[num_rands];
41 IdentifierInfo *II_random;
42 enum { num_setids = 6 };
43 IdentifierInfo *II_setid[num_setids];
Ted Kremenek2c016762010-03-24 22:39:47 +000044
Ted Kremenek88c8bc82010-01-15 08:20:31 +000045 const bool CheckRand;
Mike Stump1eb44332009-09-09 15:08:12 +000046
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000047public:
Ted Kremenekefcbb152009-07-23 22:29:41 +000048 WalkAST(BugReporter &br) : BR(br),
Eli Friedmana7e68452010-08-22 01:00:03 +000049 II_gets(0), II_getpw(0), II_mktemp(0),
50 II_rand(), II_random(0), II_setid(),
Ted Kremenek88c8bc82010-01-15 08:20:31 +000051 CheckRand(isArc4RandomAvailable(BR.getContext())) {}
Mike Stump1eb44332009-09-09 15:08:12 +000052
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000053 // Statement visitor methods.
Ted Kremenekefcbb152009-07-23 22:29:41 +000054 void VisitCallExpr(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000055 void VisitForStmt(ForStmt *S);
Ted Kremenek65a81a92009-08-28 00:08:09 +000056 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek8baf86d2009-07-23 21:34:35 +000057 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000058
59 void VisitChildren(Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +000060
Ted Kremenekefcbb152009-07-23 22:29:41 +000061 // Helpers.
62 IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str);
Mike Stump1eb44332009-09-09 15:08:12 +000063
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000064 // Checker-specific methods.
Ted Kremenek8baf86d2009-07-23 21:34:35 +000065 void CheckLoopConditionForFloat(const ForStmt *FS);
Ted Kremenekefcbb152009-07-23 22:29:41 +000066 void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD);
Zhongxing Xubd842e32009-11-09 12:19:26 +000067 void CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
Zhongxing Xu1bf40562009-12-03 09:15:23 +000068 void CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenek24650472009-09-02 02:47:41 +000069 void CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD);
70 void CheckCall_random(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenek65a81a92009-08-28 00:08:09 +000071 void CheckUncheckedReturnValue(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000072};
73} // end anonymous namespace
74
75//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +000076// Helper methods.
77//===----------------------------------------------------------------------===//
78
79IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) {
80 if (!II)
81 II = &BR.getContext().Idents.get(str);
Mike Stump1eb44332009-09-09 15:08:12 +000082
83 return II;
Ted Kremenekefcbb152009-07-23 22:29:41 +000084}
85
86//===----------------------------------------------------------------------===//
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000087// AST walking.
88//===----------------------------------------------------------------------===//
89
90void WalkAST::VisitChildren(Stmt *S) {
91 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
92 if (Stmt *child = *I)
93 Visit(child);
94}
95
Ted Kremenekefcbb152009-07-23 22:29:41 +000096void WalkAST::VisitCallExpr(CallExpr *CE) {
97 if (const FunctionDecl *FD = CE->getDirectCallee()) {
Ted Kremenek24650472009-09-02 02:47:41 +000098 CheckCall_gets(CE, FD);
Zhongxing Xubd842e32009-11-09 12:19:26 +000099 CheckCall_getpw(CE, FD);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000100 CheckCall_mktemp(CE, FD);
Ted Kremenek88c8bc82010-01-15 08:20:31 +0000101 if (CheckRand) {
102 CheckCall_rand(CE, FD);
103 CheckCall_random(CE, FD);
104 }
Ted Kremenekefcbb152009-07-23 22:29:41 +0000105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Ted Kremenekefcbb152009-07-23 22:29:41 +0000107 // Recurse and check children.
108 VisitChildren(CE);
109}
110
Ted Kremenek65a81a92009-08-28 00:08:09 +0000111void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
112 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000113 if (Stmt *child = *I) {
114 if (CallExpr *CE = dyn_cast<CallExpr>(child))
115 CheckUncheckedReturnValue(CE);
116 Visit(child);
117 }
Ted Kremenek65a81a92009-08-28 00:08:09 +0000118}
119
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000120void WalkAST::VisitForStmt(ForStmt *FS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000121 CheckLoopConditionForFloat(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000122
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000123 // Recurse and check children.
124 VisitChildren(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000125}
126
127//===----------------------------------------------------------------------===//
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000128// Check: floating poing variable used as loop counter.
Ted Kremenek5abeb522009-07-23 21:44:18 +0000129// Originally: <rdar://problem/6336718>
130// Implements: CERT security coding advisory FLP-30.
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000131//===----------------------------------------------------------------------===//
132
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000133static const DeclRefExpr*
134GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
135 expr = expr->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000136
137 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000138 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCall2de56d12010-08-25 11:45:40 +0000139 B->getOpcode() == BO_Comma))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000140 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000142 if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
143 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000145 if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
146 return rhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000148 return NULL;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000149 }
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000151 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
152 const NamedDecl *ND = DR->getDecl();
153 return ND == x || ND == y ? DR : NULL;
154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000156 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
157 return U->isIncrementDecrementOp()
158 ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
159
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000160 return NULL;
161}
162
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000163/// CheckLoopConditionForFloat - This check looks for 'for' statements that
164/// use a floating point variable as a loop counter.
165/// CERT: FLP30-C, FLP30-CPP.
166///
167void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
168 // Does the loop have a condition?
169 const Expr *condition = FS->getCond();
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000171 if (!condition)
172 return;
173
174 // Does the loop have an increment?
175 const Expr *increment = FS->getInc();
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000177 if (!increment)
178 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000180 // Strip away '()' and casts.
181 condition = condition->IgnoreParenCasts();
182 increment = increment->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000184 // Is the loop condition a comparison?
185 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
186
187 if (!B)
188 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Ted Kremenekcad9f412009-07-24 20:26:31 +0000190 // Is this a comparison?
191 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000192 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000194 // Are we comparing variables?
John McCallf6a16482010-12-04 03:47:34 +0000195 const DeclRefExpr *drLHS =
196 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
197 const DeclRefExpr *drRHS =
198 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremenekcad9f412009-07-24 20:26:31 +0000200 // Does at least one of the variables have a floating point type?
Douglas Gregor0c293ea2010-06-22 23:07:26 +0000201 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
202 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000204 if (!drLHS && !drRHS)
205 return;
206
207 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
208 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000210 if (!vdLHS && !vdRHS)
Mike Stump1eb44332009-09-09 15:08:12 +0000211 return;
212
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000213 // Does either variable appear in increment?
214 const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000216 if (!drInc)
217 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000219 // Emit the error. First figure out which DeclRefExpr in the condition
220 // referenced the compared variable.
221 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
222
Mike Stump1eb44332009-09-09 15:08:12 +0000223 llvm::SmallVector<SourceRange, 2> ranges;
Ted Kremenek2c016762010-03-24 22:39:47 +0000224 llvm::SmallString<256> sbuf;
225 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Daniel Dunbar4087f272010-08-17 22:39:59 +0000227 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000228 << "' with floating point type '" << drCond->getType().getAsString()
229 << "' should not be used as a loop counter";
230
231 ranges.push_back(drCond->getSourceRange());
232 ranges.push_back(drInc->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000234 const char *bugType = "Floating point variable used as loop counter";
Benjamin Kramerf0171732009-11-29 18:27:55 +0000235 BR.EmitBasicReport(bugType, "Security", os.str(),
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000236 FS->getLocStart(), ranges.data(), ranges.size());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000237}
238
239//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +0000240// Check: Any use of 'gets' is insecure.
241// Originally: <rdar://problem/6335715>
242// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuaa30b3b2009-11-09 08:13:04 +0000243// CWE-242: Use of Inherently Dangerous Function
Ted Kremenekefcbb152009-07-23 22:29:41 +0000244//===----------------------------------------------------------------------===//
245
246void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
247 if (FD->getIdentifier() != GetIdentifier(II_gets, "gets"))
248 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Abramo Bagnara723df242010-12-14 22:11:44 +0000250 const FunctionProtoType *FPT
251 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000252 if (!FPT)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000253 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Ted Kremenekefcbb152009-07-23 22:29:41 +0000255 // Verify that the function takes a single argument.
Zhongxing Xubd842e32009-11-09 12:19:26 +0000256 if (FPT->getNumArgs() != 1)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000257 return;
258
259 // Is the argument a 'char*'?
Zhongxing Xubd842e32009-11-09 12:19:26 +0000260 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenekefcbb152009-07-23 22:29:41 +0000261 if (!PT)
262 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Ted Kremenekefcbb152009-07-23 22:29:41 +0000264 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
265 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Ted Kremenekefcbb152009-07-23 22:29:41 +0000267 // Issue a warning.
268 SourceRange R = CE->getCallee()->getSourceRange();
269 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
270 "Security",
271 "Call to function 'gets' is extremely insecure as it can "
272 "always result in a buffer overflow",
273 CE->getLocStart(), &R, 1);
274}
275
276//===----------------------------------------------------------------------===//
Zhongxing Xubd842e32009-11-09 12:19:26 +0000277// Check: Any use of 'getpwd' is insecure.
278// CWE-477: Use of Obsolete Functions
279//===----------------------------------------------------------------------===//
280
281void WalkAST::CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
282 if (FD->getIdentifier() != GetIdentifier(II_getpw, "getpw"))
283 return;
284
Abramo Bagnara723df242010-12-14 22:11:44 +0000285 const FunctionProtoType *FPT
286 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000287 if (!FPT)
288 return;
289
290 // Verify that the function takes two arguments.
291 if (FPT->getNumArgs() != 2)
292 return;
293
294 // Verify the first argument type is integer.
295 if (!FPT->getArgType(0)->isIntegerType())
296 return;
297
298 // Verify the second argument type is char*.
299 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
300 if (!PT)
301 return;
302
303 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
304 return;
305
306 // Issue a warning.
307 SourceRange R = CE->getCallee()->getSourceRange();
308 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
309 "Security",
310 "The getpw() function is dangerous as it may overflow the "
311 "provided buffer. It is obsoleted by getpwuid().",
312 CE->getLocStart(), &R, 1);
313}
314
315//===----------------------------------------------------------------------===//
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000316// Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
317// CWE-377: Insecure Temporary File
318//===----------------------------------------------------------------------===//
319
320void WalkAST::CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
321 if (FD->getIdentifier() != GetIdentifier(II_mktemp, "mktemp"))
322 return;
323
Abramo Bagnara723df242010-12-14 22:11:44 +0000324 const FunctionProtoType *FPT
325 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000326 if(!FPT)
327 return;
Ted Kremenek2c016762010-03-24 22:39:47 +0000328
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000329 // Verify that the funcion takes a single argument.
330 if (FPT->getNumArgs() != 1)
331 return;
332
333 // Verify that the argument is Pointer Type.
334 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
335 if (!PT)
336 return;
337
338 // Verify that the argument is a 'char*'.
339 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
340 return;
Ted Kremenek431a2cb2010-03-24 22:39:45 +0000341
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000342 // Issue a waring.
343 SourceRange R = CE->getCallee()->getSourceRange();
344 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
Eli Friedmana7e68452010-08-22 01:00:03 +0000345 "Security",
346 "Call to function 'mktemp' is insecure as it always "
347 "creates or uses insecure temporary file. Use 'mkstemp' instead",
348 CE->getLocStart(), &R, 1);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000349}
350
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000351//===----------------------------------------------------------------------===//
Ted Kremenek24650472009-09-02 02:47:41 +0000352// Check: Linear congruent random number generators should not be used
353// Originally: <rdar://problem/63371000>
354// CWE-338: Use of cryptographically weak prng
355//===----------------------------------------------------------------------===//
356
357void WalkAST::CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
358 if (II_rand[0] == NULL) {
359 // This check applies to these functions
360 static const char * const identifiers[num_rands] = {
361 "drand48", "erand48", "jrand48", "lrand48", "mrand48", "nrand48",
362 "lcong48",
363 "rand", "rand_r"
364 };
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Ted Kremenek24650472009-09-02 02:47:41 +0000366 for (size_t i = 0; i < num_rands; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000367 II_rand[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek24650472009-09-02 02:47:41 +0000368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenek24650472009-09-02 02:47:41 +0000370 const IdentifierInfo *id = FD->getIdentifier();
371 size_t identifierid;
372
373 for (identifierid = 0; identifierid < num_rands; identifierid++)
374 if (id == II_rand[identifierid])
375 break;
376
377 if (identifierid >= num_rands)
378 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Abramo Bagnara723df242010-12-14 22:11:44 +0000380 const FunctionProtoType *FTP
381 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000382 if (!FTP)
383 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremenek24650472009-09-02 02:47:41 +0000385 if (FTP->getNumArgs() == 1) {
386 // Is the argument an 'unsigned short *'?
387 // (Actually any integer type is allowed.)
388 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
389 if (!PT)
390 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek24650472009-09-02 02:47:41 +0000392 if (! PT->getPointeeType()->isIntegerType())
393 return;
394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395 else if (FTP->getNumArgs() != 0)
Ted Kremenek24650472009-09-02 02:47:41 +0000396 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremenek24650472009-09-02 02:47:41 +0000398 // Issue a warning.
Ted Kremenek2c016762010-03-24 22:39:47 +0000399 llvm::SmallString<256> buf1;
400 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000401 os1 << '\'' << FD << "' is a poor random number generator";
Ted Kremenek24650472009-09-02 02:47:41 +0000402
Ted Kremenek2c016762010-03-24 22:39:47 +0000403 llvm::SmallString<256> buf2;
404 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000405 os2 << "Function '" << FD
Ted Kremenek24650472009-09-02 02:47:41 +0000406 << "' is obsolete because it implements a poor random number generator."
407 << " Use 'arc4random' instead";
408
409 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek2c016762010-03-24 22:39:47 +0000410 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000411}
412
413//===----------------------------------------------------------------------===//
414// Check: 'random' should not be used
415// Originally: <rdar://problem/63371000>
416//===----------------------------------------------------------------------===//
417
418void WalkAST::CheckCall_random(const CallExpr *CE, const FunctionDecl *FD) {
419 if (FD->getIdentifier() != GetIdentifier(II_random, "random"))
420 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Abramo Bagnara723df242010-12-14 22:11:44 +0000422 const FunctionProtoType *FTP
423 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000424 if (!FTP)
425 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek24650472009-09-02 02:47:41 +0000427 // Verify that the function takes no argument.
428 if (FTP->getNumArgs() != 0)
429 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Ted Kremenek24650472009-09-02 02:47:41 +0000431 // Issue a warning.
432 SourceRange R = CE->getCallee()->getSourceRange();
433 BR.EmitBasicReport("'random' is not a secure random number generator",
434 "Security",
435 "The 'random' function produces a sequence of values that "
436 "an adversary may be able to predict. Use 'arc4random' "
Ted Kremenek2c016762010-03-24 22:39:47 +0000437 "instead", CE->getLocStart(), &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000438}
439
440//===----------------------------------------------------------------------===//
Ted Kremenek65a81a92009-08-28 00:08:09 +0000441// Check: Should check whether privileges are dropped successfully.
442// Originally: <rdar://problem/6337132>
443//===----------------------------------------------------------------------===//
444
445void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) {
446 const FunctionDecl *FD = CE->getDirectCallee();
447 if (!FD)
448 return;
449
450 if (II_setid[0] == NULL) {
Ted Kremenek24650472009-09-02 02:47:41 +0000451 static const char * const identifiers[num_setids] = {
Ted Kremenek65a81a92009-08-28 00:08:09 +0000452 "setuid", "setgid", "seteuid", "setegid",
453 "setreuid", "setregid"
454 };
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Ted Kremenek24650472009-09-02 02:47:41 +0000456 for (size_t i = 0; i < num_setids; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000457 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Ted Kremenek65a81a92009-08-28 00:08:09 +0000460 const IdentifierInfo *id = FD->getIdentifier();
461 size_t identifierid;
462
Ted Kremenek24650472009-09-02 02:47:41 +0000463 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000464 if (id == II_setid[identifierid])
465 break;
466
Ted Kremenek24650472009-09-02 02:47:41 +0000467 if (identifierid >= num_setids)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000468 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Abramo Bagnara723df242010-12-14 22:11:44 +0000470 const FunctionProtoType *FTP
471 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek65a81a92009-08-28 00:08:09 +0000472 if (!FTP)
473 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Ted Kremeneka8187832009-08-28 00:24:55 +0000475 // Verify that the function takes one or two arguments (depending on
476 // the function).
Ted Kremenek65a81a92009-08-28 00:08:09 +0000477 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
478 return;
479
480 // The arguments must be integers.
481 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
482 if (! FTP->getArgType(i)->isIntegerType())
483 return;
484
485 // Issue a warning.
Ted Kremenek2c016762010-03-24 22:39:47 +0000486 llvm::SmallString<256> buf1;
487 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000488 os1 << "Return value is not checked in call to '" << FD << '\'';
Ted Kremenek65a81a92009-08-28 00:08:09 +0000489
Ted Kremenek2c016762010-03-24 22:39:47 +0000490 llvm::SmallString<256> buf2;
491 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000492 os2 << "The return value from the call to '" << FD
493 << "' is not checked. If an error occurs in '" << FD
Ted Kremenek65a81a92009-08-28 00:08:09 +0000494 << "', the following code may execute with unexpected privileges";
495
496 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek2c016762010-03-24 22:39:47 +0000497 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000498}
499
500//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000501// SecuritySyntaxChecker
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000502//===----------------------------------------------------------------------===//
503
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000504namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000505class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000506public:
507 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
508 BugReporter &BR) const {
509 WalkAST walker(BR);
510 walker.Visit(D->getBody());
511 }
512};
513}
514
515void ento::registerSecuritySyntaxChecker(CheckerManager &mgr) {
516 mgr.registerChecker<SecuritySyntaxChecker>();
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000517}