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/AST/StmtVisitor.h"
16 #include "clang/Analysis/AnalysisContext.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
19 #include "clang/StaticAnalyzer/Core/Checker.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;
40 DefaultBool check_gets;
41 DefaultBool check_getpw;
42 DefaultBool check_mktemp;
43 DefaultBool check_mkstemp;
44 DefaultBool check_strcpy;
45 DefaultBool check_rand;
46 DefaultBool check_vfork;
47 DefaultBool check_FloatLoopCounter;
48 DefaultBool check_UncheckedReturn;
51 class WalkAST : public StmtVisitor<WalkAST> {
53 AnalysisDeclContext* AC;
54 enum { num_setids = 6 };
55 IdentifierInfo *II_setid[num_setids];
58 const ChecksFilter &filter;
61 WalkAST(BugReporter &br, AnalysisDeclContext* ac,
62 const ChecksFilter &f)
63 : BR(br), AC(ac), II_setid(),
64 CheckRand(isArc4RandomAvailable(BR.getContext())),
67 // Statement visitor methods.
68 void VisitCallExpr(CallExpr *CE);
69 void VisitForStmt(ForStmt *S);
70 void VisitCompoundStmt (CompoundStmt *S);
71 void VisitStmt(Stmt *S) { VisitChildren(S); }
73 void VisitChildren(Stmt *S);
76 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
78 typedef void (WalkAST::*FnCheck)(const CallExpr *,
79 const FunctionDecl *);
81 // Checker-specific methods.
82 void checkLoopConditionForFloat(const ForStmt *FS);
83 void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
84 void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
85 void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
86 void checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD);
87 void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
88 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
89 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
90 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
91 void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD);
92 void checkUncheckedReturnValue(CallExpr *CE);
94 } // end anonymous namespace
96 //===----------------------------------------------------------------------===//
98 //===----------------------------------------------------------------------===//
100 void WalkAST::VisitChildren(Stmt *S) {
101 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
102 if (Stmt *child = *I)
106 void WalkAST::VisitCallExpr(CallExpr *CE) {
108 const FunctionDecl *FD = CE->getDirectCallee();
113 // Get the name of the callee. If it's a builtin, strip off the prefix.
114 IdentifierInfo *II = FD->getIdentifier();
115 if (!II) // if no identifier, not a simple C function
117 StringRef Name = II->getName();
118 if (Name.startswith("__builtin_"))
119 Name = Name.substr(10);
121 // Set the evaluation function by switching on the callee name.
122 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
123 .Case("gets", &WalkAST::checkCall_gets)
124 .Case("getpw", &WalkAST::checkCall_getpw)
125 .Case("mktemp", &WalkAST::checkCall_mktemp)
126 .Case("mkstemp", &WalkAST::checkCall_mkstemp)
127 .Case("mkdtemp", &WalkAST::checkCall_mkstemp)
128 .Case("mkstemps", &WalkAST::checkCall_mkstemp)
129 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
130 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
131 .Case("drand48", &WalkAST::checkCall_rand)
132 .Case("erand48", &WalkAST::checkCall_rand)
133 .Case("jrand48", &WalkAST::checkCall_rand)
134 .Case("lrand48", &WalkAST::checkCall_rand)
135 .Case("mrand48", &WalkAST::checkCall_rand)
136 .Case("nrand48", &WalkAST::checkCall_rand)
137 .Case("lcong48", &WalkAST::checkCall_rand)
138 .Case("rand", &WalkAST::checkCall_rand)
139 .Case("rand_r", &WalkAST::checkCall_rand)
140 .Case("random", &WalkAST::checkCall_random)
141 .Case("vfork", &WalkAST::checkCall_vfork)
144 // If the callee isn't defined, it is not of security concern.
145 // Check and evaluate the call.
147 (this->*evalFunction)(CE, FD);
149 // Recurse and check children.
153 void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
154 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
155 if (Stmt *child = *I) {
156 if (CallExpr *CE = dyn_cast<CallExpr>(child))
157 checkUncheckedReturnValue(CE);
162 void WalkAST::VisitForStmt(ForStmt *FS) {
163 checkLoopConditionForFloat(FS);
165 // Recurse and check children.
169 //===----------------------------------------------------------------------===//
170 // Check: floating poing variable used as loop counter.
171 // Originally: <rdar://problem/6336718>
172 // Implements: CERT security coding advisory FLP-30.
173 //===----------------------------------------------------------------------===//
175 static const DeclRefExpr*
176 getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
177 expr = expr->IgnoreParenCasts();
179 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
180 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
181 B->getOpcode() == BO_Comma))
184 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
187 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
193 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
194 const NamedDecl *ND = DR->getDecl();
195 return ND == x || ND == y ? DR : NULL;
198 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
199 return U->isIncrementDecrementOp()
200 ? getIncrementedVar(U->getSubExpr(), x, y) : NULL;
205 /// CheckLoopConditionForFloat - This check looks for 'for' statements that
206 /// use a floating point variable as a loop counter.
207 /// CERT: FLP30-C, FLP30-CPP.
209 void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
210 if (!filter.check_FloatLoopCounter)
213 // Does the loop have a condition?
214 const Expr *condition = FS->getCond();
219 // Does the loop have an increment?
220 const Expr *increment = FS->getInc();
225 // Strip away '()' and casts.
226 condition = condition->IgnoreParenCasts();
227 increment = increment->IgnoreParenCasts();
229 // Is the loop condition a comparison?
230 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
235 // Is this a comparison?
236 if (!(B->isRelationalOp() || B->isEqualityOp()))
239 // Are we comparing variables?
240 const DeclRefExpr *drLHS =
241 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
242 const DeclRefExpr *drRHS =
243 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
245 // Does at least one of the variables have a floating point type?
246 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
247 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
249 if (!drLHS && !drRHS)
252 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
253 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
255 if (!vdLHS && !vdRHS)
258 // Does either variable appear in increment?
259 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
264 // Emit the error. First figure out which DeclRefExpr in the condition
265 // referenced the compared variable.
266 assert(drInc->getDecl());
267 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
269 SmallVector<SourceRange, 2> ranges;
270 SmallString<256> sbuf;
271 llvm::raw_svector_ostream os(sbuf);
273 os << "Variable '" << drCond->getDecl()->getName()
274 << "' with floating point type '" << drCond->getType().getAsString()
275 << "' should not be used as a loop counter";
277 ranges.push_back(drCond->getSourceRange());
278 ranges.push_back(drInc->getSourceRange());
280 const char *bugType = "Floating point variable used as loop counter";
282 PathDiagnosticLocation FSLoc =
283 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
284 BR.EmitBasicReport(AC->getDecl(),
285 bugType, "Security", os.str(),
289 //===----------------------------------------------------------------------===//
290 // Check: Any use of 'gets' is insecure.
291 // Originally: <rdar://problem/6335715>
292 // Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
293 // CWE-242: Use of Inherently Dangerous Function
294 //===----------------------------------------------------------------------===//
296 void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
297 if (!filter.check_gets)
300 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
304 // Verify that the function takes a single argument.
305 if (FPT->getNumArgs() != 1)
308 // Is the argument a 'char*'?
309 const PointerType *PT = FPT->getArgType(0)->getAs<PointerType>();
313 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
317 PathDiagnosticLocation CELoc =
318 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
319 BR.EmitBasicReport(AC->getDecl(),
320 "Potential buffer overflow in call to 'gets'",
322 "Call to function 'gets' is extremely insecure as it can "
323 "always result in a buffer overflow",
324 CELoc, CE->getCallee()->getSourceRange());
327 //===----------------------------------------------------------------------===//
328 // Check: Any use of 'getpwd' is insecure.
329 // CWE-477: Use of Obsolete Functions
330 //===----------------------------------------------------------------------===//
332 void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
333 if (!filter.check_getpw)
336 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
340 // Verify that the function takes two arguments.
341 if (FPT->getNumArgs() != 2)
344 // Verify the first argument type is integer.
345 if (!FPT->getArgType(0)->isIntegralOrUnscopedEnumerationType())
348 // Verify the second argument type is char*.
349 const PointerType *PT = FPT->getArgType(1)->getAs<PointerType>();
353 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
357 PathDiagnosticLocation CELoc =
358 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
359 BR.EmitBasicReport(AC->getDecl(),
360 "Potential buffer overflow in call to 'getpw'",
362 "The getpw() function is dangerous as it may overflow the "
363 "provided buffer. It is obsoleted by getpwuid().",
364 CELoc, CE->getCallee()->getSourceRange());
367 //===----------------------------------------------------------------------===//
368 // Check: Any use of 'mktemp' is insecure. It is obsoleted by mkstemp().
369 // CWE-377: Insecure Temporary File
370 //===----------------------------------------------------------------------===//
372 void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
373 if (!filter.check_mktemp) {
374 // Fall back to the security check of looking for enough 'X's in the
375 // format string, since that is a less severe warning.
376 checkCall_mkstemp(CE, FD);
380 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
384 // Verify that the function takes a single argument.
385 if (FPT->getNumArgs() != 1)
388 // Verify that the argument is Pointer Type.
389 const PointerType *PT = FPT->getArgType(0)->getAs<PointerType>();
393 // Verify that the argument is a 'char*'.
394 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
398 PathDiagnosticLocation CELoc =
399 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
400 BR.EmitBasicReport(AC->getDecl(),
401 "Potential insecure temporary file in call 'mktemp'",
403 "Call to function 'mktemp' is insecure as it always "
404 "creates or uses insecure temporary file. Use 'mkstemp' "
406 CELoc, CE->getCallee()->getSourceRange());
410 //===----------------------------------------------------------------------===//
411 // Check: Use of 'mkstemp', 'mktemp', 'mkdtemp' should contain at least 6 X's.
412 //===----------------------------------------------------------------------===//
414 void WalkAST::checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD) {
415 if (!filter.check_mkstemp)
418 StringRef Name = FD->getIdentifier()->getName();
419 std::pair<signed, signed> ArgSuffix =
420 llvm::StringSwitch<std::pair<signed, signed> >(Name)
421 .Case("mktemp", std::make_pair(0,-1))
422 .Case("mkstemp", std::make_pair(0,-1))
423 .Case("mkdtemp", std::make_pair(0,-1))
424 .Case("mkstemps", std::make_pair(0,1))
425 .Default(std::make_pair(-1, -1));
427 assert(ArgSuffix.first >= 0 && "Unsupported function");
429 // Check if the number of arguments is consistent with out expectations.
430 unsigned numArgs = CE->getNumArgs();
431 if ((signed) numArgs <= ArgSuffix.first)
434 const StringLiteral *strArg =
435 dyn_cast<StringLiteral>(CE->getArg((unsigned)ArgSuffix.first)
436 ->IgnoreParenImpCasts());
438 // Currently we only handle string literals. It is possible to do better,
439 // either by looking at references to const variables, or by doing real
441 if (!strArg || strArg->getCharByteWidth() != 1)
444 // Count the number of X's, taking into account a possible cutoff suffix.
445 StringRef str = strArg->getString();
447 unsigned n = str.size();
449 // Take into account the suffix.
451 if (ArgSuffix.second >= 0) {
452 const Expr *suffixEx = CE->getArg((unsigned)ArgSuffix.second);
454 if (!suffixEx->EvaluateAsInt(Result, BR.getContext()))
456 // FIXME: Issue a warning.
457 if (Result.isNegative())
459 suffix = (unsigned) Result.getZExtValue();
460 n = (n > suffix) ? n - suffix : 0;
463 for (unsigned i = 0; i < n; ++i)
464 if (str[i] == 'X') ++numX;
470 PathDiagnosticLocation CELoc =
471 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
472 SmallString<512> buf;
473 llvm::raw_svector_ostream out(buf);
474 out << "Call to '" << Name << "' should have at least 6 'X's in the"
475 " format string to be secure (" << numX << " 'X'";
480 out << ", " << suffix << " character";
483 out << " used as a suffix";
486 BR.EmitBasicReport(AC->getDecl(),
487 "Insecure temporary file creation", "Security",
488 out.str(), CELoc, strArg->getSourceRange());
491 //===----------------------------------------------------------------------===//
492 // Check: Any use of 'strcpy' is insecure.
494 // CWE-119: Improper Restriction of Operations within
495 // the Bounds of a Memory Buffer
496 //===----------------------------------------------------------------------===//
497 void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
498 if (!filter.check_strcpy)
501 if (!checkCall_strCommon(CE, FD))
505 PathDiagnosticLocation CELoc =
506 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
507 BR.EmitBasicReport(AC->getDecl(),
508 "Potential insecure memory buffer bounds restriction in "
511 "Call to function 'strcpy' is insecure as it does not "
512 "provide bounding of the memory buffer. Replace "
513 "unbounded copy functions with analogous functions that "
514 "support length arguments such as 'strlcpy'. CWE-119.",
515 CELoc, CE->getCallee()->getSourceRange());
518 //===----------------------------------------------------------------------===//
519 // Check: Any use of 'strcat' is insecure.
521 // CWE-119: Improper Restriction of Operations within
522 // the Bounds of a Memory Buffer
523 //===----------------------------------------------------------------------===//
524 void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
525 if (!filter.check_strcpy)
528 if (!checkCall_strCommon(CE, FD))
532 PathDiagnosticLocation CELoc =
533 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
534 BR.EmitBasicReport(AC->getDecl(),
535 "Potential insecure memory buffer bounds restriction in "
538 "Call to function 'strcat' is insecure as it does not "
539 "provide bounding of the memory buffer. Replace "
540 "unbounded copy functions with analogous functions that "
541 "support length arguments such as 'strlcat'. CWE-119.",
542 CELoc, CE->getCallee()->getSourceRange());
545 //===----------------------------------------------------------------------===//
546 // Common check for str* functions with no bounds parameters.
547 //===----------------------------------------------------------------------===//
548 bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
549 const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
553 // Verify the function takes two arguments, three in the _chk version.
554 int numArgs = FPT->getNumArgs();
555 if (numArgs != 2 && numArgs != 3)
558 // Verify the type for both arguments.
559 for (int i = 0; i < 2; i++) {
560 // Verify that the arguments are pointers.
561 const PointerType *PT = FPT->getArgType(i)->getAs<PointerType>();
565 // Verify that the argument is a 'char*'.
566 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
573 //===----------------------------------------------------------------------===//
574 // Check: Linear congruent random number generators should not be used
575 // Originally: <rdar://problem/63371000>
576 // CWE-338: Use of cryptographically weak prng
577 //===----------------------------------------------------------------------===//
579 void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
580 if (!filter.check_rand || !CheckRand)
583 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
587 if (FTP->getNumArgs() == 1) {
588 // Is the argument an 'unsigned short *'?
589 // (Actually any integer type is allowed.)
590 const PointerType *PT = FTP->getArgType(0)->getAs<PointerType>();
594 if (! PT->getPointeeType()->isIntegralOrUnscopedEnumerationType())
597 else if (FTP->getNumArgs() != 0)
601 SmallString<256> buf1;
602 llvm::raw_svector_ostream os1(buf1);
603 os1 << '\'' << *FD << "' is a poor random number generator";
605 SmallString<256> buf2;
606 llvm::raw_svector_ostream os2(buf2);
607 os2 << "Function '" << *FD
608 << "' is obsolete because it implements a poor random number generator."
609 << " Use 'arc4random' instead";
611 PathDiagnosticLocation CELoc =
612 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
613 BR.EmitBasicReport(AC->getDecl(), os1.str(), "Security", os2.str(),
614 CELoc, CE->getCallee()->getSourceRange());
617 //===----------------------------------------------------------------------===//
618 // Check: 'random' should not be used
619 // Originally: <rdar://problem/63371000>
620 //===----------------------------------------------------------------------===//
622 void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
623 if (!CheckRand || !filter.check_rand)
626 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
630 // Verify that the function takes no argument.
631 if (FTP->getNumArgs() != 0)
635 PathDiagnosticLocation CELoc =
636 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
637 BR.EmitBasicReport(AC->getDecl(),
638 "'random' is not a secure random number generator",
640 "The 'random' function produces a sequence of values that "
641 "an adversary may be able to predict. Use 'arc4random' "
642 "instead", CELoc, CE->getCallee()->getSourceRange());
645 //===----------------------------------------------------------------------===//
646 // Check: 'vfork' should not be used.
647 // POS33-C: Do not use vfork().
648 //===----------------------------------------------------------------------===//
650 void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
651 if (!filter.check_vfork)
654 // All calls to vfork() are insecure, issue a warning.
655 PathDiagnosticLocation CELoc =
656 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
657 BR.EmitBasicReport(AC->getDecl(),
658 "Potential insecure implementation-specific behavior in "
661 "Call to function 'vfork' is insecure as it can lead to "
662 "denial of service situations in the parent process. "
663 "Replace calls to vfork with calls to the safer "
664 "'posix_spawn' function",
665 CELoc, CE->getCallee()->getSourceRange());
668 //===----------------------------------------------------------------------===//
669 // Check: Should check whether privileges are dropped successfully.
670 // Originally: <rdar://problem/6337132>
671 //===----------------------------------------------------------------------===//
673 void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
674 if (!filter.check_UncheckedReturn)
677 const FunctionDecl *FD = CE->getDirectCallee();
681 if (II_setid[0] == NULL) {
682 static const char * const identifiers[num_setids] = {
683 "setuid", "setgid", "seteuid", "setegid",
684 "setreuid", "setregid"
687 for (size_t i = 0; i < num_setids; i++)
688 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
691 const IdentifierInfo *id = FD->getIdentifier();
694 for (identifierid = 0; identifierid < num_setids; identifierid++)
695 if (id == II_setid[identifierid])
698 if (identifierid >= num_setids)
701 const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
705 // Verify that the function takes one or two arguments (depending on
707 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
710 // The arguments must be integers.
711 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
712 if (! FTP->getArgType(i)->isIntegralOrUnscopedEnumerationType())
716 SmallString<256> buf1;
717 llvm::raw_svector_ostream os1(buf1);
718 os1 << "Return value is not checked in call to '" << *FD << '\'';
720 SmallString<256> buf2;
721 llvm::raw_svector_ostream os2(buf2);
722 os2 << "The return value from the call to '" << *FD
723 << "' is not checked. If an error occurs in '" << *FD
724 << "', the following code may execute with unexpected privileges";
726 PathDiagnosticLocation CELoc =
727 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
728 BR.EmitBasicReport(AC->getDecl(), os1.str(), "Security", os2.str(),
729 CELoc, CE->getCallee()->getSourceRange());
732 //===----------------------------------------------------------------------===//
733 // SecuritySyntaxChecker
734 //===----------------------------------------------------------------------===//
737 class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
741 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
742 BugReporter &BR) const {
743 WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter);
744 walker.Visit(D->getBody());
749 #define REGISTER_CHECKER(name) \
750 void ento::register##name(CheckerManager &mgr) {\
751 mgr.registerChecker<SecuritySyntaxChecker>()->filter.check_##name = true;\
754 REGISTER_CHECKER(gets)
755 REGISTER_CHECKER(getpw)
756 REGISTER_CHECKER(mkstemp)
757 REGISTER_CHECKER(mktemp)
758 REGISTER_CHECKER(strcpy)
759 REGISTER_CHECKER(rand)
760 REGISTER_CHECKER(vfork)
761 REGISTER_CHECKER(FloatLoopCounter)
762 REGISTER_CHECKER(UncheckedReturn)