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/Analysis/AnalysisContext.h"
16 #include "clang/AST/StmtVisitor.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
28 static bool isArc4RandomAvailable(const ASTContext &Ctx) {
29 const llvm::Triple &T = Ctx.getTargetInfo().getTriple();
30 return T.getVendor() == llvm::Triple::Apple ||
31 T.getOS() == llvm::Triple::FreeBSD ||
32 T.getOS() == llvm::Triple::NetBSD ||
33 T.getOS() == llvm::Triple::OpenBSD ||
34 T.getOS() == llvm::Triple::Bitrig ||
35 T.getOS() == llvm::Triple::DragonFly;
41 DefaultBool() : val(false) {}
42 operator bool() const { return val; }
43 DefaultBool &operator=(bool b) { val = b; return *this; }
47 DefaultBool check_gets;
48 DefaultBool check_getpw;
49 DefaultBool check_mktemp;
50 DefaultBool check_mkstemp;
51 DefaultBool check_strcpy;
52 DefaultBool check_rand;
53 DefaultBool check_vfork;
54 DefaultBool check_FloatLoopCounter;
55 DefaultBool check_UncheckedReturn;
58 class WalkAST : public StmtVisitor<WalkAST> {
60 AnalysisDeclContext* AC;
61 enum { num_setids = 6 };
62 IdentifierInfo *II_setid[num_setids];
65 const ChecksFilter &filter;
68 WalkAST(BugReporter &br, AnalysisDeclContext* ac,
69 const ChecksFilter &f)
70 : BR(br), AC(ac), II_setid(),
71 CheckRand(isArc4RandomAvailable(BR.getContext())),
74 // Statement visitor methods.
75 void VisitCallExpr(CallExpr *CE);
76 void VisitForStmt(ForStmt *S);
77 void VisitCompoundStmt (CompoundStmt *S);
78 void VisitStmt(Stmt *S) { VisitChildren(S); }
80 void VisitChildren(Stmt *S);
83 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
85 typedef void (WalkAST::*FnCheck)(const CallExpr *,
86 const FunctionDecl *);
88 // Checker-specific methods.
89 void checkLoopConditionForFloat(const ForStmt *FS);
90 void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
91 void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
92 void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
93 void checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD);
94 void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
95 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
96 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
97 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
98 void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD);
99 void checkUncheckedReturnValue(CallExpr *CE);
101 } // end anonymous namespace
103 //===----------------------------------------------------------------------===//
105 //===----------------------------------------------------------------------===//
107 void WalkAST::VisitChildren(Stmt *S) {
108 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
109 if (Stmt *child = *I)
113 void WalkAST::VisitCallExpr(CallExpr *CE) {
115 const FunctionDecl *FD = CE->getDirectCallee();
120 // Get the name of the callee. If it's a builtin, strip off the prefix.
121 IdentifierInfo *II = FD->getIdentifier();
122 if (!II) // if no identifier, not a simple C function
124 StringRef Name = II->getName();
125 if (Name.startswith("__builtin_"))
126 Name = Name.substr(10);
128 // Set the evaluation function by switching on the callee name.
129 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
130 .Case("gets", &WalkAST::checkCall_gets)
131 .Case("getpw", &WalkAST::checkCall_getpw)
132 .Case("mktemp", &WalkAST::checkCall_mktemp)
133 .Case("mkstemp", &WalkAST::checkCall_mkstemp)
134 .Case("mkdtemp", &WalkAST::checkCall_mkstemp)
135 .Case("mkstemps", &WalkAST::checkCall_mkstemp)
136 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
137 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
138 .Case("drand48", &WalkAST::checkCall_rand)
139 .Case("erand48", &WalkAST::checkCall_rand)
140 .Case("jrand48", &WalkAST::checkCall_rand)
141 .Case("lrand48", &WalkAST::checkCall_rand)
142 .Case("mrand48", &WalkAST::checkCall_rand)
143 .Case("nrand48", &WalkAST::checkCall_rand)
144 .Case("lcong48", &WalkAST::checkCall_rand)
145 .Case("rand", &WalkAST::checkCall_rand)
146 .Case("rand_r", &WalkAST::checkCall_rand)
147 .Case("random", &WalkAST::checkCall_random)
148 .Case("vfork", &WalkAST::checkCall_vfork)
151 // If the callee isn't defined, it is not of security concern.
152 // Check and evaluate the call.
154 (this->*evalFunction)(CE, FD);
156 // Recurse and check children.
160 void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
161 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
162 if (Stmt *child = *I) {
163 if (CallExpr *CE = dyn_cast<CallExpr>(child))
164 checkUncheckedReturnValue(CE);
169 void WalkAST::VisitForStmt(ForStmt *FS) {
170 checkLoopConditionForFloat(FS);
172 // Recurse and check children.
176 //===----------------------------------------------------------------------===//
177 // Check: floating poing variable used as loop counter.
178 // Originally: <rdar://problem/6336718>
179 // Implements: CERT security coding advisory FLP-30.
180 //===----------------------------------------------------------------------===//
182 static const DeclRefExpr*
183 getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
184 expr = expr->IgnoreParenCasts();
186 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
187 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
188 B->getOpcode() == BO_Comma))
191 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
194 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
200 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
201 const NamedDecl *ND = DR->getDecl();
202 return ND == x || ND == y ? DR : NULL;
205 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
206 return U->isIncrementDecrementOp()
207 ? getIncrementedVar(U->getSubExpr(), x, y) : NULL;
212 /// CheckLoopConditionForFloat - This check looks for 'for' statements that
213 /// use a floating point variable as a loop counter.
214 /// CERT: FLP30-C, FLP30-CPP.
216 void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
217 if (!filter.check_FloatLoopCounter)
220 // Does the loop have a condition?
221 const Expr *condition = FS->getCond();
226 // Does the loop have an increment?
227 const Expr *increment = FS->getInc();
232 // Strip away '()' and casts.
233 condition = condition->IgnoreParenCasts();
234 increment = increment->IgnoreParenCasts();
236 // Is the loop condition a comparison?
237 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
242 // Is this a comparison?
243 if (!(B->isRelationalOp() || B->isEqualityOp()))
246 // Are we comparing variables?
247 const DeclRefExpr *drLHS =
248 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
249 const DeclRefExpr *drRHS =
250 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
252 // Does at least one of the variables have a floating point type?
253 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
254 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
256 if (!drLHS && !drRHS)
259 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
260 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
262 if (!vdLHS && !vdRHS)
265 // Does either variable appear in increment?
266 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
271 // Emit the error. First figure out which DeclRefExpr in the condition
272 // referenced the compared variable.
273 assert(drInc->getDecl());
274 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
276 SmallVector<SourceRange, 2> ranges;
277 SmallString<256> sbuf;
278 llvm::raw_svector_ostream os(sbuf);
280 os << "Variable '" << drCond->getDecl()->getName()
281 << "' with floating point type '" << drCond->getType().getAsString()
282 << "' should not be used as a loop counter";
284 ranges.push_back(drCond->getSourceRange());
285 ranges.push_back(drInc->getSourceRange());
287 const char *bugType = "Floating point variable used as loop counter";
289 PathDiagnosticLocation FSLoc =
290 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
291 BR.EmitBasicReport(AC->getDecl(),
292 bugType, "Security", os.str(),
293 FSLoc, ranges.data(), ranges.size());
296 //===----------------------------------------------------------------------===//
297 // Check: Any use of 'gets' is insecure.
298 // Originally: <rdar://problem/6335715>
299 // Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
300 // CWE-242: Use of Inherently Dangerous Function
301 //===----------------------------------------------------------------------===//
303 void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
304 if (!filter.check_gets)
307 const FunctionProtoType *FPT
308 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
312 // Verify that the function takes a single argument.
313 if (FPT->getNumArgs() != 1)
316 // Is the argument a 'char*'?
317 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
321 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
325 SourceRange R = CE->getCallee()->getSourceRange();
326 PathDiagnosticLocation CELoc =
327 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
328 BR.EmitBasicReport(AC->getDecl(),
329 "Potential buffer overflow in call to 'gets'",
331 "Call to function 'gets' is extremely insecure as it can "
332 "always result in a buffer overflow",
336 //===----------------------------------------------------------------------===//
337 // Check: Any use of 'getpwd' is insecure.
338 // CWE-477: Use of Obsolete Functions
339 //===----------------------------------------------------------------------===//
341 void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
342 if (!filter.check_getpw)
345 const FunctionProtoType *FPT
346 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
350 // Verify that the function takes two arguments.
351 if (FPT->getNumArgs() != 2)
354 // Verify the first argument type is integer.
355 if (!FPT->getArgType(0)->isIntegerType())
358 // Verify the second argument type is char*.
359 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
363 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
367 SourceRange R = CE->getCallee()->getSourceRange();
368 PathDiagnosticLocation CELoc =
369 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
370 BR.EmitBasicReport(AC->getDecl(),
371 "Potential buffer overflow in call to 'getpw'",
373 "The getpw() function is dangerous as it may overflow the "
374 "provided buffer. It is obsoleted by getpwuid().",
378 //===----------------------------------------------------------------------===//
379 // Check: Any use of 'mktemp' is insecure. It is obsoleted by mkstemp().
380 // CWE-377: Insecure Temporary File
381 //===----------------------------------------------------------------------===//
383 void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
384 if (!filter.check_mktemp) {
385 // Fall back to the security check of looking for enough 'X's in the
386 // format string, since that is a less severe warning.
387 checkCall_mkstemp(CE, FD);
391 const FunctionProtoType *FPT
392 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
396 // Verify that the function takes a single argument.
397 if (FPT->getNumArgs() != 1)
400 // Verify that the argument is Pointer Type.
401 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
405 // Verify that the argument is a 'char*'.
406 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
410 SourceRange R = CE->getCallee()->getSourceRange();
411 PathDiagnosticLocation CELoc =
412 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
413 BR.EmitBasicReport(AC->getDecl(),
414 "Potential insecure temporary file in call 'mktemp'",
416 "Call to function 'mktemp' is insecure as it always "
417 "creates or uses insecure temporary file. Use 'mkstemp' "
423 //===----------------------------------------------------------------------===//
424 // Check: Use of 'mkstemp', 'mktemp', 'mkdtemp' should contain at least 6 X's.
425 //===----------------------------------------------------------------------===//
427 void WalkAST::checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD) {
428 if (!filter.check_mkstemp)
431 StringRef Name = FD->getIdentifier()->getName();
432 std::pair<signed, signed> ArgSuffix =
433 llvm::StringSwitch<std::pair<signed, signed> >(Name)
434 .Case("mktemp", std::make_pair(0,-1))
435 .Case("mkstemp", std::make_pair(0,-1))
436 .Case("mkdtemp", std::make_pair(0,-1))
437 .Case("mkstemps", std::make_pair(0,1))
438 .Default(std::make_pair(-1, -1));
440 assert(ArgSuffix.first >= 0 && "Unsupported function");
442 // Check if the number of arguments is consistent with out expectations.
443 unsigned numArgs = CE->getNumArgs();
444 if ((signed) numArgs <= ArgSuffix.first)
447 const StringLiteral *strArg =
448 dyn_cast<StringLiteral>(CE->getArg((unsigned)ArgSuffix.first)
449 ->IgnoreParenImpCasts());
451 // Currently we only handle string literals. It is possible to do better,
452 // either by looking at references to const variables, or by doing real
454 if (!strArg || strArg->getCharByteWidth() != 1)
457 // Count the number of X's, taking into account a possible cutoff suffix.
458 StringRef str = strArg->getString();
460 unsigned n = str.size();
462 // Take into account the suffix.
464 if (ArgSuffix.second >= 0) {
465 const Expr *suffixEx = CE->getArg((unsigned)ArgSuffix.second);
467 if (!suffixEx->EvaluateAsInt(Result, BR.getContext()))
469 // FIXME: Issue a warning.
470 if (Result.isNegative())
472 suffix = (unsigned) Result.getZExtValue();
473 n = (n > suffix) ? n - suffix : 0;
476 for (unsigned i = 0; i < n; ++i)
477 if (str[i] == 'X') ++numX;
483 SourceRange R = strArg->getSourceRange();
484 PathDiagnosticLocation CELoc =
485 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
486 SmallString<512> buf;
487 llvm::raw_svector_ostream out(buf);
488 out << "Call to '" << Name << "' should have at least 6 'X's in the"
489 " format string to be secure (" << numX << " 'X'";
494 out << ", " << suffix << " character";
497 out << " used as a suffix";
500 BR.EmitBasicReport(AC->getDecl(),
501 "Insecure temporary file creation", "Security",
502 out.str(), CELoc, &R, 1);
505 //===----------------------------------------------------------------------===//
506 // Check: Any use of 'strcpy' is insecure.
508 // CWE-119: Improper Restriction of Operations within
509 // the Bounds of a Memory Buffer
510 //===----------------------------------------------------------------------===//
511 void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
512 if (!filter.check_strcpy)
515 if (!checkCall_strCommon(CE, FD))
519 SourceRange R = CE->getCallee()->getSourceRange();
520 PathDiagnosticLocation CELoc =
521 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
522 BR.EmitBasicReport(AC->getDecl(),
523 "Potential insecure memory buffer bounds restriction in "
526 "Call to function 'strcpy' is insecure as it does not "
527 "provide bounding of the memory buffer. Replace "
528 "unbounded copy functions with analogous functions that "
529 "support length arguments such as 'strlcpy'. CWE-119.",
533 //===----------------------------------------------------------------------===//
534 // Check: Any use of 'strcat' is insecure.
536 // CWE-119: Improper Restriction of Operations within
537 // the Bounds of a Memory Buffer
538 //===----------------------------------------------------------------------===//
539 void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
540 if (!filter.check_strcpy)
543 if (!checkCall_strCommon(CE, FD))
547 SourceRange R = CE->getCallee()->getSourceRange();
548 PathDiagnosticLocation CELoc =
549 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
550 BR.EmitBasicReport(AC->getDecl(),
551 "Potential insecure memory buffer bounds restriction in "
554 "Call to function 'strcat' is insecure as it does not "
555 "provide bounding of the memory buffer. Replace "
556 "unbounded copy functions with analogous functions that "
557 "support length arguments such as 'strlcat'. CWE-119.",
561 //===----------------------------------------------------------------------===//
562 // Common check for str* functions with no bounds parameters.
563 //===----------------------------------------------------------------------===//
564 bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
565 const FunctionProtoType *FPT
566 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
570 // Verify the function takes two arguments, three in the _chk version.
571 int numArgs = FPT->getNumArgs();
572 if (numArgs != 2 && numArgs != 3)
575 // Verify the type for both arguments.
576 for (int i = 0; i < 2; i++) {
577 // Verify that the arguments are pointers.
578 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
582 // Verify that the argument is a 'char*'.
583 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
590 //===----------------------------------------------------------------------===//
591 // Check: Linear congruent random number generators should not be used
592 // Originally: <rdar://problem/63371000>
593 // CWE-338: Use of cryptographically weak prng
594 //===----------------------------------------------------------------------===//
596 void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
597 if (!filter.check_rand || !CheckRand)
600 const FunctionProtoType *FTP
601 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
605 if (FTP->getNumArgs() == 1) {
606 // Is the argument an 'unsigned short *'?
607 // (Actually any integer type is allowed.)
608 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
612 if (! PT->getPointeeType()->isIntegerType())
615 else if (FTP->getNumArgs() != 0)
619 SmallString<256> buf1;
620 llvm::raw_svector_ostream os1(buf1);
621 os1 << '\'' << *FD << "' is a poor random number generator";
623 SmallString<256> buf2;
624 llvm::raw_svector_ostream os2(buf2);
625 os2 << "Function '" << *FD
626 << "' is obsolete because it implements a poor random number generator."
627 << " Use 'arc4random' instead";
629 SourceRange R = CE->getCallee()->getSourceRange();
630 PathDiagnosticLocation CELoc =
631 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
632 BR.EmitBasicReport(AC->getDecl(), os1.str(), "Security", os2.str(),
636 //===----------------------------------------------------------------------===//
637 // Check: 'random' should not be used
638 // Originally: <rdar://problem/63371000>
639 //===----------------------------------------------------------------------===//
641 void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
642 if (!CheckRand || !filter.check_rand)
645 const FunctionProtoType *FTP
646 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
650 // Verify that the function takes no argument.
651 if (FTP->getNumArgs() != 0)
655 SourceRange R = CE->getCallee()->getSourceRange();
656 PathDiagnosticLocation CELoc =
657 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
658 BR.EmitBasicReport(AC->getDecl(),
659 "'random' is not a secure random number generator",
661 "The 'random' function produces a sequence of values that "
662 "an adversary may be able to predict. Use 'arc4random' "
663 "instead", CELoc, &R, 1);
666 //===----------------------------------------------------------------------===//
667 // Check: 'vfork' should not be used.
668 // POS33-C: Do not use vfork().
669 //===----------------------------------------------------------------------===//
671 void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
672 if (!filter.check_vfork)
675 // All calls to vfork() are insecure, issue a warning.
676 SourceRange R = CE->getCallee()->getSourceRange();
677 PathDiagnosticLocation CELoc =
678 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
679 BR.EmitBasicReport(AC->getDecl(),
680 "Potential insecure implementation-specific behavior in "
683 "Call to function 'vfork' is insecure as it can lead to "
684 "denial of service situations in the parent process. "
685 "Replace calls to vfork with calls to the safer "
686 "'posix_spawn' function",
690 //===----------------------------------------------------------------------===//
691 // Check: Should check whether privileges are dropped successfully.
692 // Originally: <rdar://problem/6337132>
693 //===----------------------------------------------------------------------===//
695 void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
696 if (!filter.check_UncheckedReturn)
699 const FunctionDecl *FD = CE->getDirectCallee();
703 if (II_setid[0] == NULL) {
704 static const char * const identifiers[num_setids] = {
705 "setuid", "setgid", "seteuid", "setegid",
706 "setreuid", "setregid"
709 for (size_t i = 0; i < num_setids; i++)
710 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
713 const IdentifierInfo *id = FD->getIdentifier();
716 for (identifierid = 0; identifierid < num_setids; identifierid++)
717 if (id == II_setid[identifierid])
720 if (identifierid >= num_setids)
723 const FunctionProtoType *FTP
724 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
728 // Verify that the function takes one or two arguments (depending on
730 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
733 // The arguments must be integers.
734 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
735 if (! FTP->getArgType(i)->isIntegerType())
739 SmallString<256> buf1;
740 llvm::raw_svector_ostream os1(buf1);
741 os1 << "Return value is not checked in call to '" << *FD << '\'';
743 SmallString<256> buf2;
744 llvm::raw_svector_ostream os2(buf2);
745 os2 << "The return value from the call to '" << *FD
746 << "' is not checked. If an error occurs in '" << *FD
747 << "', the following code may execute with unexpected privileges";
749 SourceRange R = CE->getCallee()->getSourceRange();
750 PathDiagnosticLocation CELoc =
751 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
752 BR.EmitBasicReport(AC->getDecl(), os1.str(), "Security", os2.str(),
756 //===----------------------------------------------------------------------===//
757 // SecuritySyntaxChecker
758 //===----------------------------------------------------------------------===//
761 class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
765 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
766 BugReporter &BR) const {
767 WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter);
768 walker.Visit(D->getBody());
773 #define REGISTER_CHECKER(name) \
774 void ento::register##name(CheckerManager &mgr) {\
775 mgr.registerChecker<SecuritySyntaxChecker>()->filter.check_##name = true;\
778 REGISTER_CHECKER(gets)
779 REGISTER_CHECKER(getpw)
780 REGISTER_CHECKER(mkstemp)
781 REGISTER_CHECKER(mktemp)
782 REGISTER_CHECKER(strcpy)
783 REGISTER_CHECKER(rand)
784 REGISTER_CHECKER(vfork)
785 REGISTER_CHECKER(FloatLoopCounter)
786 REGISTER_CHECKER(UncheckedReturn)