1 //==- CheckSecuritySyntaxOnly.cpp - Basic security checks --------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a set of flow-insensitive security checks.
12 //===----------------------------------------------------------------------===//
14 #include "ClangSACheckers.h"
15 #include "clang/StaticAnalyzer/Core/CheckerV2.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/AST/StmtVisitor.h"
19 #include "llvm/Support/raw_ostream.h"
21 using namespace clang;
24 static bool isArc4RandomAvailable(const ASTContext &Ctx) {
25 const llvm::Triple &T = Ctx.Target.getTriple();
26 return T.getVendor() == llvm::Triple::Apple ||
27 T.getOS() == llvm::Triple::FreeBSD ||
28 T.getOS() == llvm::Triple::NetBSD ||
29 T.getOS() == llvm::Triple::OpenBSD ||
30 T.getOS() == llvm::Triple::DragonFly;
34 class WalkAST : public StmtVisitor<WalkAST> {
36 IdentifierInfo *II_gets;
37 IdentifierInfo *II_getpw;
38 IdentifierInfo *II_mktemp;
39 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];
48 WalkAST(BugReporter &br) : BR(br),
49 II_gets(0), II_getpw(0), II_mktemp(0),
50 II_rand(), II_random(0), II_setid(),
51 CheckRand(isArc4RandomAvailable(BR.getContext())) {}
53 // Statement visitor methods.
54 void VisitCallExpr(CallExpr *CE);
55 void VisitForStmt(ForStmt *S);
56 void VisitCompoundStmt (CompoundStmt *S);
57 void VisitStmt(Stmt *S) { VisitChildren(S); }
59 void VisitChildren(Stmt *S);
62 IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str);
64 // Checker-specific methods.
65 void CheckLoopConditionForFloat(const ForStmt *FS);
66 void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD);
67 void CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
68 void CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
69 void CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD);
70 void CheckCall_random(const CallExpr *CE, const FunctionDecl *FD);
71 void CheckUncheckedReturnValue(CallExpr *CE);
73 } // end anonymous namespace
75 //===----------------------------------------------------------------------===//
77 //===----------------------------------------------------------------------===//
79 IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) {
81 II = &BR.getContext().Idents.get(str);
86 //===----------------------------------------------------------------------===//
88 //===----------------------------------------------------------------------===//
90 void WalkAST::VisitChildren(Stmt *S) {
91 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
96 void WalkAST::VisitCallExpr(CallExpr *CE) {
97 if (const FunctionDecl *FD = CE->getDirectCallee()) {
98 CheckCall_gets(CE, FD);
99 CheckCall_getpw(CE, FD);
100 CheckCall_mktemp(CE, FD);
102 CheckCall_rand(CE, FD);
103 CheckCall_random(CE, FD);
107 // Recurse and check children.
111 void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
112 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
113 if (Stmt *child = *I) {
114 if (CallExpr *CE = dyn_cast<CallExpr>(child))
115 CheckUncheckedReturnValue(CE);
120 void WalkAST::VisitForStmt(ForStmt *FS) {
121 CheckLoopConditionForFloat(FS);
123 // Recurse and check children.
127 //===----------------------------------------------------------------------===//
128 // Check: floating poing variable used as loop counter.
129 // Originally: <rdar://problem/6336718>
130 // Implements: CERT security coding advisory FLP-30.
131 //===----------------------------------------------------------------------===//
133 static const DeclRefExpr*
134 GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
135 expr = expr->IgnoreParenCasts();
137 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
138 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
139 B->getOpcode() == BO_Comma))
142 if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
145 if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
151 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
152 const NamedDecl *ND = DR->getDecl();
153 return ND == x || ND == y ? DR : NULL;
156 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
157 return U->isIncrementDecrementOp()
158 ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
163 /// CheckLoopConditionForFloat - This check looks for 'for' statements that
164 /// use a floating point variable as a loop counter.
165 /// CERT: FLP30-C, FLP30-CPP.
167 void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
168 // Does the loop have a condition?
169 const Expr *condition = FS->getCond();
174 // Does the loop have an increment?
175 const Expr *increment = FS->getInc();
180 // Strip away '()' and casts.
181 condition = condition->IgnoreParenCasts();
182 increment = increment->IgnoreParenCasts();
184 // Is the loop condition a comparison?
185 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
190 // Is this a comparison?
191 if (!(B->isRelationalOp() || B->isEqualityOp()))
194 // Are we comparing variables?
195 const DeclRefExpr *drLHS =
196 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
197 const DeclRefExpr *drRHS =
198 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
200 // Does at least one of the variables have a floating point type?
201 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
202 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
204 if (!drLHS && !drRHS)
207 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
208 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
210 if (!vdLHS && !vdRHS)
213 // Does either variable appear in increment?
214 const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
219 // 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;
223 llvm::SmallVector<SourceRange, 2> ranges;
224 llvm::SmallString<256> sbuf;
225 llvm::raw_svector_ostream os(sbuf);
227 os << "Variable '" << drCond->getDecl()->getName()
228 << "' with floating point type '" << drCond->getType().getAsString()
229 << "' should not be used as a loop counter";
231 ranges.push_back(drCond->getSourceRange());
232 ranges.push_back(drInc->getSourceRange());
234 const char *bugType = "Floating point variable used as loop counter";
235 BR.EmitBasicReport(bugType, "Security", os.str(),
236 FS->getLocStart(), ranges.data(), ranges.size());
239 //===----------------------------------------------------------------------===//
240 // Check: Any use of 'gets' is insecure.
241 // Originally: <rdar://problem/6335715>
242 // Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
243 // CWE-242: Use of Inherently Dangerous Function
244 //===----------------------------------------------------------------------===//
246 void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
247 if (FD->getIdentifier() != GetIdentifier(II_gets, "gets"))
250 const FunctionProtoType *FPT
251 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
255 // Verify that the function takes a single argument.
256 if (FPT->getNumArgs() != 1)
259 // Is the argument a 'char*'?
260 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
264 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
268 SourceRange R = CE->getCallee()->getSourceRange();
269 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
271 "Call to function 'gets' is extremely insecure as it can "
272 "always result in a buffer overflow",
273 CE->getLocStart(), &R, 1);
276 //===----------------------------------------------------------------------===//
277 // Check: Any use of 'getpwd' is insecure.
278 // CWE-477: Use of Obsolete Functions
279 //===----------------------------------------------------------------------===//
281 void WalkAST::CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
282 if (FD->getIdentifier() != GetIdentifier(II_getpw, "getpw"))
285 const FunctionProtoType *FPT
286 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
290 // Verify that the function takes two arguments.
291 if (FPT->getNumArgs() != 2)
294 // Verify the first argument type is integer.
295 if (!FPT->getArgType(0)->isIntegerType())
298 // Verify the second argument type is char*.
299 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
303 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
307 SourceRange R = CE->getCallee()->getSourceRange();
308 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
310 "The getpw() function is dangerous as it may overflow the "
311 "provided buffer. It is obsoleted by getpwuid().",
312 CE->getLocStart(), &R, 1);
315 //===----------------------------------------------------------------------===//
316 // Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
317 // CWE-377: Insecure Temporary File
318 //===----------------------------------------------------------------------===//
320 void WalkAST::CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
321 if (FD->getIdentifier() != GetIdentifier(II_mktemp, "mktemp"))
324 const FunctionProtoType *FPT
325 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
329 // Verify that the funcion takes a single argument.
330 if (FPT->getNumArgs() != 1)
333 // Verify that the argument is Pointer Type.
334 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
338 // Verify that the argument is a 'char*'.
339 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
343 SourceRange R = CE->getCallee()->getSourceRange();
344 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
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);
351 //===----------------------------------------------------------------------===//
352 // Check: Linear congruent random number generators should not be used
353 // Originally: <rdar://problem/63371000>
354 // CWE-338: Use of cryptographically weak prng
355 //===----------------------------------------------------------------------===//
357 void 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",
366 for (size_t i = 0; i < num_rands; i++)
367 II_rand[i] = &BR.getContext().Idents.get(identifiers[i]);
370 const IdentifierInfo *id = FD->getIdentifier();
373 for (identifierid = 0; identifierid < num_rands; identifierid++)
374 if (id == II_rand[identifierid])
377 if (identifierid >= num_rands)
380 const FunctionProtoType *FTP
381 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
385 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));
392 if (! PT->getPointeeType()->isIntegerType())
395 else if (FTP->getNumArgs() != 0)
399 llvm::SmallString<256> buf1;
400 llvm::raw_svector_ostream os1(buf1);
401 os1 << '\'' << FD << "' is a poor random number generator";
403 llvm::SmallString<256> buf2;
404 llvm::raw_svector_ostream os2(buf2);
405 os2 << "Function '" << FD
406 << "' is obsolete because it implements a poor random number generator."
407 << " Use 'arc4random' instead";
409 SourceRange R = CE->getCallee()->getSourceRange();
410 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
413 //===----------------------------------------------------------------------===//
414 // Check: 'random' should not be used
415 // Originally: <rdar://problem/63371000>
416 //===----------------------------------------------------------------------===//
418 void WalkAST::CheckCall_random(const CallExpr *CE, const FunctionDecl *FD) {
419 if (FD->getIdentifier() != GetIdentifier(II_random, "random"))
422 const FunctionProtoType *FTP
423 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
427 // Verify that the function takes no argument.
428 if (FTP->getNumArgs() != 0)
432 SourceRange R = CE->getCallee()->getSourceRange();
433 BR.EmitBasicReport("'random' is not a secure random number generator",
435 "The 'random' function produces a sequence of values that "
436 "an adversary may be able to predict. Use 'arc4random' "
437 "instead", CE->getLocStart(), &R, 1);
440 //===----------------------------------------------------------------------===//
441 // Check: Should check whether privileges are dropped successfully.
442 // Originally: <rdar://problem/6337132>
443 //===----------------------------------------------------------------------===//
445 void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) {
446 const FunctionDecl *FD = CE->getDirectCallee();
450 if (II_setid[0] == NULL) {
451 static const char * const identifiers[num_setids] = {
452 "setuid", "setgid", "seteuid", "setegid",
453 "setreuid", "setregid"
456 for (size_t i = 0; i < num_setids; i++)
457 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
460 const IdentifierInfo *id = FD->getIdentifier();
463 for (identifierid = 0; identifierid < num_setids; identifierid++)
464 if (id == II_setid[identifierid])
467 if (identifierid >= num_setids)
470 const FunctionProtoType *FTP
471 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
475 // Verify that the function takes one or two arguments (depending on
477 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
480 // The arguments must be integers.
481 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
482 if (! FTP->getArgType(i)->isIntegerType())
486 llvm::SmallString<256> buf1;
487 llvm::raw_svector_ostream os1(buf1);
488 os1 << "Return value is not checked in call to '" << FD << '\'';
490 llvm::SmallString<256> buf2;
491 llvm::raw_svector_ostream os2(buf2);
492 os2 << "The return value from the call to '" << FD
493 << "' is not checked. If an error occurs in '" << FD
494 << "', the following code may execute with unexpected privileges";
496 SourceRange R = CE->getCallee()->getSourceRange();
497 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
500 //===----------------------------------------------------------------------===//
501 // SecuritySyntaxChecker
502 //===----------------------------------------------------------------------===//
505 class SecuritySyntaxChecker : public CheckerV2<check::ASTCodeBody> {
507 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
508 BugReporter &BR) const {
510 walker.Visit(D->getBody());
515 void ento::registerSecuritySyntaxChecker(CheckerManager &mgr) {
516 mgr.registerChecker<SecuritySyntaxChecker>();