blob: 6090fa5baf2f51b08100d8a0e77f717bdee45f1c [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 Kremenek6b676302010-01-25 17:10:22 +000015#include "clang/Checker/BugReporter/BugReporter.h"
Ted Kremenek97053092010-01-26 22:59:55 +000016#include "clang/Checker/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;
21
Ted Kremenek88c8bc82010-01-15 08:20:31 +000022static bool isArc4RandomAvailable(const ASTContext &Ctx) {
23 const llvm::Triple &T = Ctx.Target.getTriple();
24 return T.getVendor() == llvm::Triple::Apple ||
25 T.getOS() == llvm::Triple::FreeBSD;
26}
27
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000028namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000029class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump1eb44332009-09-09 15:08:12 +000030 BugReporter &BR;
Ted Kremenek65a81a92009-08-28 00:08:09 +000031 IdentifierInfo *II_gets;
Zhongxing Xubd842e32009-11-09 12:19:26 +000032 IdentifierInfo *II_getpw;
Zhongxing Xu1bf40562009-12-03 09:15:23 +000033 IdentifierInfo *II_mktemp;
Ted Kremenek24650472009-09-02 02:47:41 +000034 enum { num_rands = 9 };
35 IdentifierInfo *II_rand[num_rands];
36 IdentifierInfo *II_random;
37 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:
Ted Kremenekefcbb152009-07-23 22:29:41 +000043 WalkAST(BugReporter &br) : BR(br),
Eli Friedmana7e68452010-08-22 01:00:03 +000044 II_gets(0), II_getpw(0), II_mktemp(0),
45 II_rand(), II_random(0), II_setid(),
Ted Kremenek88c8bc82010-01-15 08:20:31 +000046 CheckRand(isArc4RandomAvailable(BR.getContext())) {}
Mike Stump1eb44332009-09-09 15:08:12 +000047
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000048 // Statement visitor methods.
Ted Kremenekefcbb152009-07-23 22:29:41 +000049 void VisitCallExpr(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000050 void VisitForStmt(ForStmt *S);
Ted Kremenek65a81a92009-08-28 00:08:09 +000051 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek8baf86d2009-07-23 21:34:35 +000052 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000053
54 void VisitChildren(Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +000055
Ted Kremenekefcbb152009-07-23 22:29:41 +000056 // Helpers.
57 IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str);
Mike Stump1eb44332009-09-09 15:08:12 +000058
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000059 // Checker-specific methods.
Ted Kremenek8baf86d2009-07-23 21:34:35 +000060 void CheckLoopConditionForFloat(const ForStmt *FS);
Ted Kremenekefcbb152009-07-23 22:29:41 +000061 void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD);
Zhongxing Xubd842e32009-11-09 12:19:26 +000062 void CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
Zhongxing Xu1bf40562009-12-03 09:15:23 +000063 void CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenek24650472009-09-02 02:47:41 +000064 void CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD);
65 void CheckCall_random(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenek65a81a92009-08-28 00:08:09 +000066 void CheckUncheckedReturnValue(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000067};
68} // end anonymous namespace
69
70//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +000071// Helper methods.
72//===----------------------------------------------------------------------===//
73
74IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) {
75 if (!II)
76 II = &BR.getContext().Idents.get(str);
Mike Stump1eb44332009-09-09 15:08:12 +000077
78 return II;
Ted Kremenekefcbb152009-07-23 22:29:41 +000079}
80
81//===----------------------------------------------------------------------===//
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000082// AST walking.
83//===----------------------------------------------------------------------===//
84
85void WalkAST::VisitChildren(Stmt *S) {
86 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
87 if (Stmt *child = *I)
88 Visit(child);
89}
90
Ted Kremenekefcbb152009-07-23 22:29:41 +000091void WalkAST::VisitCallExpr(CallExpr *CE) {
92 if (const FunctionDecl *FD = CE->getDirectCallee()) {
Ted Kremenek24650472009-09-02 02:47:41 +000093 CheckCall_gets(CE, FD);
Zhongxing Xubd842e32009-11-09 12:19:26 +000094 CheckCall_getpw(CE, FD);
Zhongxing Xu1bf40562009-12-03 09:15:23 +000095 CheckCall_mktemp(CE, FD);
Ted Kremenek88c8bc82010-01-15 08:20:31 +000096 if (CheckRand) {
97 CheckCall_rand(CE, FD);
98 CheckCall_random(CE, FD);
99 }
Ted Kremenekefcbb152009-07-23 22:29:41 +0000100 }
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenekefcbb152009-07-23 22:29:41 +0000102 // Recurse and check children.
103 VisitChildren(CE);
104}
105
Ted Kremenek65a81a92009-08-28 00:08:09 +0000106void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
107 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000108 if (Stmt *child = *I) {
109 if (CallExpr *CE = dyn_cast<CallExpr>(child))
110 CheckUncheckedReturnValue(CE);
111 Visit(child);
112 }
Ted Kremenek65a81a92009-08-28 00:08:09 +0000113}
114
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000115void WalkAST::VisitForStmt(ForStmt *FS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000116 CheckLoopConditionForFloat(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000117
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000118 // Recurse and check children.
119 VisitChildren(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000120}
121
122//===----------------------------------------------------------------------===//
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000123// Check: floating poing variable used as loop counter.
Ted Kremenek5abeb522009-07-23 21:44:18 +0000124// Originally: <rdar://problem/6336718>
125// Implements: CERT security coding advisory FLP-30.
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000126//===----------------------------------------------------------------------===//
127
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000128static const DeclRefExpr*
129GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
130 expr = expr->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000131
132 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000133 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCall2de56d12010-08-25 11:45:40 +0000134 B->getOpcode() == BO_Comma))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000135 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000137 if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
138 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000140 if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
141 return rhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000143 return NULL;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000144 }
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000146 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
147 const NamedDecl *ND = DR->getDecl();
148 return ND == x || ND == y ? DR : NULL;
149 }
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000151 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
152 return U->isIncrementDecrementOp()
153 ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
154
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000155 return NULL;
156}
157
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000158/// CheckLoopConditionForFloat - This check looks for 'for' statements that
159/// use a floating point variable as a loop counter.
160/// CERT: FLP30-C, FLP30-CPP.
161///
162void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
163 // Does the loop have a condition?
164 const Expr *condition = FS->getCond();
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000166 if (!condition)
167 return;
168
169 // Does the loop have an increment?
170 const Expr *increment = FS->getInc();
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000172 if (!increment)
173 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000175 // Strip away '()' and casts.
176 condition = condition->IgnoreParenCasts();
177 increment = increment->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000179 // Is the loop condition a comparison?
180 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
181
182 if (!B)
183 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Ted Kremenekcad9f412009-07-24 20:26:31 +0000185 // Is this a comparison?
186 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000187 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000189 // Are we comparing variables?
John McCallf6a16482010-12-04 03:47:34 +0000190 const DeclRefExpr *drLHS =
191 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
192 const DeclRefExpr *drRHS =
193 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenekcad9f412009-07-24 20:26:31 +0000195 // Does at least one of the variables have a floating point type?
Douglas Gregor0c293ea2010-06-22 23:07:26 +0000196 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
197 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000199 if (!drLHS && !drRHS)
200 return;
201
202 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
203 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000205 if (!vdLHS && !vdRHS)
Mike Stump1eb44332009-09-09 15:08:12 +0000206 return;
207
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000208 // Does either variable appear in increment?
209 const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000211 if (!drInc)
212 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000214 // Emit the error. First figure out which DeclRefExpr in the condition
215 // referenced the compared variable.
216 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
217
Mike Stump1eb44332009-09-09 15:08:12 +0000218 llvm::SmallVector<SourceRange, 2> ranges;
Ted Kremenek2c016762010-03-24 22:39:47 +0000219 llvm::SmallString<256> sbuf;
220 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Daniel Dunbar4087f272010-08-17 22:39:59 +0000222 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000223 << "' with floating point type '" << drCond->getType().getAsString()
224 << "' should not be used as a loop counter";
225
226 ranges.push_back(drCond->getSourceRange());
227 ranges.push_back(drInc->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000229 const char *bugType = "Floating point variable used as loop counter";
Benjamin Kramerf0171732009-11-29 18:27:55 +0000230 BR.EmitBasicReport(bugType, "Security", os.str(),
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000231 FS->getLocStart(), ranges.data(), ranges.size());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000232}
233
234//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +0000235// Check: Any use of 'gets' is insecure.
236// Originally: <rdar://problem/6335715>
237// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuaa30b3b2009-11-09 08:13:04 +0000238// CWE-242: Use of Inherently Dangerous Function
Ted Kremenekefcbb152009-07-23 22:29:41 +0000239//===----------------------------------------------------------------------===//
240
241void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
242 if (FD->getIdentifier() != GetIdentifier(II_gets, "gets"))
243 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Abramo Bagnara723df242010-12-14 22:11:44 +0000245 const FunctionProtoType *FPT
246 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000247 if (!FPT)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000248 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Ted Kremenekefcbb152009-07-23 22:29:41 +0000250 // Verify that the function takes a single argument.
Zhongxing Xubd842e32009-11-09 12:19:26 +0000251 if (FPT->getNumArgs() != 1)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000252 return;
253
254 // Is the argument a 'char*'?
Zhongxing Xubd842e32009-11-09 12:19:26 +0000255 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenekefcbb152009-07-23 22:29:41 +0000256 if (!PT)
257 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Ted Kremenekefcbb152009-07-23 22:29:41 +0000259 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
260 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Ted Kremenekefcbb152009-07-23 22:29:41 +0000262 // Issue a warning.
263 SourceRange R = CE->getCallee()->getSourceRange();
264 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
265 "Security",
266 "Call to function 'gets' is extremely insecure as it can "
267 "always result in a buffer overflow",
268 CE->getLocStart(), &R, 1);
269}
270
271//===----------------------------------------------------------------------===//
Zhongxing Xubd842e32009-11-09 12:19:26 +0000272// Check: Any use of 'getpwd' is insecure.
273// CWE-477: Use of Obsolete Functions
274//===----------------------------------------------------------------------===//
275
276void WalkAST::CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
277 if (FD->getIdentifier() != GetIdentifier(II_getpw, "getpw"))
278 return;
279
Abramo Bagnara723df242010-12-14 22:11:44 +0000280 const FunctionProtoType *FPT
281 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000282 if (!FPT)
283 return;
284
285 // Verify that the function takes two arguments.
286 if (FPT->getNumArgs() != 2)
287 return;
288
289 // Verify the first argument type is integer.
290 if (!FPT->getArgType(0)->isIntegerType())
291 return;
292
293 // Verify the second argument type is char*.
294 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
295 if (!PT)
296 return;
297
298 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
299 return;
300
301 // Issue a warning.
302 SourceRange R = CE->getCallee()->getSourceRange();
303 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
304 "Security",
305 "The getpw() function is dangerous as it may overflow the "
306 "provided buffer. It is obsoleted by getpwuid().",
307 CE->getLocStart(), &R, 1);
308}
309
310//===----------------------------------------------------------------------===//
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000311// Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
312// CWE-377: Insecure Temporary File
313//===----------------------------------------------------------------------===//
314
315void WalkAST::CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
316 if (FD->getIdentifier() != GetIdentifier(II_mktemp, "mktemp"))
317 return;
318
Abramo Bagnara723df242010-12-14 22:11:44 +0000319 const FunctionProtoType *FPT
320 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000321 if(!FPT)
322 return;
Ted Kremenek2c016762010-03-24 22:39:47 +0000323
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000324 // Verify that the funcion takes a single argument.
325 if (FPT->getNumArgs() != 1)
326 return;
327
328 // Verify that the argument is Pointer Type.
329 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
330 if (!PT)
331 return;
332
333 // Verify that the argument is a 'char*'.
334 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
335 return;
Ted Kremenek431a2cb2010-03-24 22:39:45 +0000336
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000337 // Issue a waring.
338 SourceRange R = CE->getCallee()->getSourceRange();
339 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
Eli Friedmana7e68452010-08-22 01:00:03 +0000340 "Security",
341 "Call to function 'mktemp' is insecure as it always "
342 "creates or uses insecure temporary file. Use 'mkstemp' instead",
343 CE->getLocStart(), &R, 1);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000344}
345
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000346//===----------------------------------------------------------------------===//
Ted Kremenek24650472009-09-02 02:47:41 +0000347// Check: Linear congruent random number generators should not be used
348// Originally: <rdar://problem/63371000>
349// CWE-338: Use of cryptographically weak prng
350//===----------------------------------------------------------------------===//
351
352void WalkAST::CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
353 if (II_rand[0] == NULL) {
354 // This check applies to these functions
355 static const char * const identifiers[num_rands] = {
356 "drand48", "erand48", "jrand48", "lrand48", "mrand48", "nrand48",
357 "lcong48",
358 "rand", "rand_r"
359 };
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremenek24650472009-09-02 02:47:41 +0000361 for (size_t i = 0; i < num_rands; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000362 II_rand[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek24650472009-09-02 02:47:41 +0000363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek24650472009-09-02 02:47:41 +0000365 const IdentifierInfo *id = FD->getIdentifier();
366 size_t identifierid;
367
368 for (identifierid = 0; identifierid < num_rands; identifierid++)
369 if (id == II_rand[identifierid])
370 break;
371
372 if (identifierid >= num_rands)
373 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Abramo Bagnara723df242010-12-14 22:11:44 +0000375 const FunctionProtoType *FTP
376 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000377 if (!FTP)
378 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Ted Kremenek24650472009-09-02 02:47:41 +0000380 if (FTP->getNumArgs() == 1) {
381 // Is the argument an 'unsigned short *'?
382 // (Actually any integer type is allowed.)
383 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
384 if (!PT)
385 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremenek24650472009-09-02 02:47:41 +0000387 if (! PT->getPointeeType()->isIntegerType())
388 return;
389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390 else if (FTP->getNumArgs() != 0)
Ted Kremenek24650472009-09-02 02:47:41 +0000391 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek24650472009-09-02 02:47:41 +0000393 // Issue a warning.
Ted Kremenek2c016762010-03-24 22:39:47 +0000394 llvm::SmallString<256> buf1;
395 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000396 os1 << '\'' << FD << "' is a poor random number generator";
Ted Kremenek24650472009-09-02 02:47:41 +0000397
Ted Kremenek2c016762010-03-24 22:39:47 +0000398 llvm::SmallString<256> buf2;
399 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000400 os2 << "Function '" << FD
Ted Kremenek24650472009-09-02 02:47:41 +0000401 << "' is obsolete because it implements a poor random number generator."
402 << " Use 'arc4random' instead";
403
404 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek2c016762010-03-24 22:39:47 +0000405 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000406}
407
408//===----------------------------------------------------------------------===//
409// Check: 'random' should not be used
410// Originally: <rdar://problem/63371000>
411//===----------------------------------------------------------------------===//
412
413void WalkAST::CheckCall_random(const CallExpr *CE, const FunctionDecl *FD) {
414 if (FD->getIdentifier() != GetIdentifier(II_random, "random"))
415 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Abramo Bagnara723df242010-12-14 22:11:44 +0000417 const FunctionProtoType *FTP
418 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000419 if (!FTP)
420 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Ted Kremenek24650472009-09-02 02:47:41 +0000422 // Verify that the function takes no argument.
423 if (FTP->getNumArgs() != 0)
424 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Ted Kremenek24650472009-09-02 02:47:41 +0000426 // Issue a warning.
427 SourceRange R = CE->getCallee()->getSourceRange();
428 BR.EmitBasicReport("'random' is not a secure random number generator",
429 "Security",
430 "The 'random' function produces a sequence of values that "
431 "an adversary may be able to predict. Use 'arc4random' "
Ted Kremenek2c016762010-03-24 22:39:47 +0000432 "instead", CE->getLocStart(), &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000433}
434
435//===----------------------------------------------------------------------===//
Ted Kremenek65a81a92009-08-28 00:08:09 +0000436// Check: Should check whether privileges are dropped successfully.
437// Originally: <rdar://problem/6337132>
438//===----------------------------------------------------------------------===//
439
440void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) {
441 const FunctionDecl *FD = CE->getDirectCallee();
442 if (!FD)
443 return;
444
445 if (II_setid[0] == NULL) {
Ted Kremenek24650472009-09-02 02:47:41 +0000446 static const char * const identifiers[num_setids] = {
Ted Kremenek65a81a92009-08-28 00:08:09 +0000447 "setuid", "setgid", "seteuid", "setegid",
448 "setreuid", "setregid"
449 };
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremenek24650472009-09-02 02:47:41 +0000451 for (size_t i = 0; i < num_setids; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000452 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000453 }
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Ted Kremenek65a81a92009-08-28 00:08:09 +0000455 const IdentifierInfo *id = FD->getIdentifier();
456 size_t identifierid;
457
Ted Kremenek24650472009-09-02 02:47:41 +0000458 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000459 if (id == II_setid[identifierid])
460 break;
461
Ted Kremenek24650472009-09-02 02:47:41 +0000462 if (identifierid >= num_setids)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000463 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Abramo Bagnara723df242010-12-14 22:11:44 +0000465 const FunctionProtoType *FTP
466 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek65a81a92009-08-28 00:08:09 +0000467 if (!FTP)
468 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Ted Kremeneka8187832009-08-28 00:24:55 +0000470 // Verify that the function takes one or two arguments (depending on
471 // the function).
Ted Kremenek65a81a92009-08-28 00:08:09 +0000472 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
473 return;
474
475 // The arguments must be integers.
476 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
477 if (! FTP->getArgType(i)->isIntegerType())
478 return;
479
480 // Issue a warning.
Ted Kremenek2c016762010-03-24 22:39:47 +0000481 llvm::SmallString<256> buf1;
482 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000483 os1 << "Return value is not checked in call to '" << FD << '\'';
Ted Kremenek65a81a92009-08-28 00:08:09 +0000484
Ted Kremenek2c016762010-03-24 22:39:47 +0000485 llvm::SmallString<256> buf2;
486 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000487 os2 << "The return value from the call to '" << FD
488 << "' is not checked. If an error occurs in '" << FD
Ted Kremenek65a81a92009-08-28 00:08:09 +0000489 << "', the following code may execute with unexpected privileges";
490
491 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek2c016762010-03-24 22:39:47 +0000492 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000493}
494
495//===----------------------------------------------------------------------===//
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000496// Entry point for check.
497//===----------------------------------------------------------------------===//
498
Mike Stump1eb44332009-09-09 15:08:12 +0000499void clang::CheckSecuritySyntaxOnly(const Decl *D, BugReporter &BR) {
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000500 WalkAST walker(BR);
Mike Stump1eb44332009-09-09 15:08:12 +0000501 walker.Visit(D->getBody());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000502}