1 //==- 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 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/StringSwitch.h" 22 #include "llvm/Support/raw_ostream.h" 23 24 using namespace clang; 25 using namespace ento; 26 27 static bool isArc4RandomAvailable(const ASTContext &Ctx) { 28 const llvm::Triple &T = Ctx.getTargetInfo().getTriple(); 29 return T.getVendor() == llvm::Triple::Apple || 30 T.getOS() == llvm::Triple::FreeBSD || 31 T.getOS() == llvm::Triple::NetBSD || 32 T.getOS() == llvm::Triple::OpenBSD || 33 T.getOS() == llvm::Triple::DragonFly; 34 } 35 36 namespace { 37 struct DefaultBool { 38 bool val; 39 DefaultBool() : val(false) {} 40 operator bool() const { return val; } 41 DefaultBool &operator=(bool b) { val = b; return *this; } 42 }; 43 44 struct ChecksFilter { 45 DefaultBool check_gets; 46 DefaultBool check_getpw; 47 DefaultBool check_mktemp; 48 DefaultBool check_strcpy; 49 DefaultBool check_rand; 50 DefaultBool check_vfork; 51 DefaultBool check_FloatLoopCounter; 52 }; 53 54 class WalkAST : public StmtVisitor<WalkAST> { 55 BugReporter &BR; 56 AnalysisDeclContext* AC; 57 enum { num_setids = 6 }; 58 IdentifierInfo *II_setid[num_setids]; 59 60 const bool CheckRand; 61 const ChecksFilter &filter; 62 63 public: 64 WalkAST(BugReporter &br, AnalysisDeclContext* ac, 65 const ChecksFilter &f) 66 : BR(br), AC(ac), II_setid(), 67 CheckRand(isArc4RandomAvailable(BR.getContext())), 68 filter(f) {} 69 70 // Statement visitor methods. 71 void VisitCallExpr(CallExpr *CE); 72 void VisitForStmt(ForStmt *S); 73 void VisitCompoundStmt (CompoundStmt *S); 74 void VisitStmt(Stmt *S) { VisitChildren(S); } 75 76 void VisitChildren(Stmt *S); 77 78 // Helpers. 79 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD); 80 81 typedef void (WalkAST::*FnCheck)(const CallExpr *, 82 const FunctionDecl *); 83 84 // Checker-specific methods. 85 void checkLoopConditionForFloat(const ForStmt *FS); 86 void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD); 87 void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD); 88 void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD); 89 void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD); 90 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD); 91 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD); 92 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD); 93 void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD); 94 void checkUncheckedReturnValue(CallExpr *CE); 95 }; 96 } // end anonymous namespace 97 98 //===----------------------------------------------------------------------===// 99 // AST walking. 100 //===----------------------------------------------------------------------===// 101 102 void WalkAST::VisitChildren(Stmt *S) { 103 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) 104 if (Stmt *child = *I) 105 Visit(child); 106 } 107 108 void WalkAST::VisitCallExpr(CallExpr *CE) { 109 // Get the callee. 110 const FunctionDecl *FD = CE->getDirectCallee(); 111 112 if (!FD) 113 return; 114 115 // Get the name of the callee. If it's a builtin, strip off the prefix. 116 IdentifierInfo *II = FD->getIdentifier(); 117 if (!II) // if no identifier, not a simple C function 118 return; 119 StringRef Name = II->getName(); 120 if (Name.startswith("__builtin_")) 121 Name = Name.substr(10); 122 123 // Set the evaluation function by switching on the callee name. 124 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name) 125 .Case("gets", &WalkAST::checkCall_gets) 126 .Case("getpw", &WalkAST::checkCall_getpw) 127 .Case("mktemp", &WalkAST::checkCall_mktemp) 128 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy) 129 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat) 130 .Case("drand48", &WalkAST::checkCall_rand) 131 .Case("erand48", &WalkAST::checkCall_rand) 132 .Case("jrand48", &WalkAST::checkCall_rand) 133 .Case("lrand48", &WalkAST::checkCall_rand) 134 .Case("mrand48", &WalkAST::checkCall_rand) 135 .Case("nrand48", &WalkAST::checkCall_rand) 136 .Case("lcong48", &WalkAST::checkCall_rand) 137 .Case("rand", &WalkAST::checkCall_rand) 138 .Case("rand_r", &WalkAST::checkCall_rand) 139 .Case("random", &WalkAST::checkCall_random) 140 .Case("vfork", &WalkAST::checkCall_vfork) 141 .Default(NULL); 142 143 // If the callee isn't defined, it is not of security concern. 144 // Check and evaluate the call. 145 if (evalFunction) 146 (this->*evalFunction)(CE, FD); 147 148 // Recurse and check children. 149 VisitChildren(CE); 150 } 151 152 void WalkAST::VisitCompoundStmt(CompoundStmt *S) { 153 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) 154 if (Stmt *child = *I) { 155 if (CallExpr *CE = dyn_cast<CallExpr>(child)) 156 checkUncheckedReturnValue(CE); 157 Visit(child); 158 } 159 } 160 161 void WalkAST::VisitForStmt(ForStmt *FS) { 162 checkLoopConditionForFloat(FS); 163 164 // Recurse and check children. 165 VisitChildren(FS); 166 } 167 168 //===----------------------------------------------------------------------===// 169 // Check: floating poing variable used as loop counter. 170 // Originally: <rdar://problem/6336718> 171 // Implements: CERT security coding advisory FLP-30. 172 //===----------------------------------------------------------------------===// 173 174 static const DeclRefExpr* 175 getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) { 176 expr = expr->IgnoreParenCasts(); 177 178 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) { 179 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() || 180 B->getOpcode() == BO_Comma)) 181 return NULL; 182 183 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y)) 184 return lhs; 185 186 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y)) 187 return rhs; 188 189 return NULL; 190 } 191 192 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) { 193 const NamedDecl *ND = DR->getDecl(); 194 return ND == x || ND == y ? DR : NULL; 195 } 196 197 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr)) 198 return U->isIncrementDecrementOp() 199 ? getIncrementedVar(U->getSubExpr(), x, y) : NULL; 200 201 return NULL; 202 } 203 204 /// CheckLoopConditionForFloat - This check looks for 'for' statements that 205 /// use a floating point variable as a loop counter. 206 /// CERT: FLP30-C, FLP30-CPP. 207 /// 208 void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) { 209 if (!filter.check_FloatLoopCounter) 210 return; 211 212 // Does the loop have a condition? 213 const Expr *condition = FS->getCond(); 214 215 if (!condition) 216 return; 217 218 // Does the loop have an increment? 219 const Expr *increment = FS->getInc(); 220 221 if (!increment) 222 return; 223 224 // Strip away '()' and casts. 225 condition = condition->IgnoreParenCasts(); 226 increment = increment->IgnoreParenCasts(); 227 228 // Is the loop condition a comparison? 229 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition); 230 231 if (!B) 232 return; 233 234 // Is this a comparison? 235 if (!(B->isRelationalOp() || B->isEqualityOp())) 236 return; 237 238 // Are we comparing variables? 239 const DeclRefExpr *drLHS = 240 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts()); 241 const DeclRefExpr *drRHS = 242 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts()); 243 244 // Does at least one of the variables have a floating point type? 245 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL; 246 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL; 247 248 if (!drLHS && !drRHS) 249 return; 250 251 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL; 252 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL; 253 254 if (!vdLHS && !vdRHS) 255 return; 256 257 // Does either variable appear in increment? 258 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS); 259 260 if (!drInc) 261 return; 262 263 // Emit the error. First figure out which DeclRefExpr in the condition 264 // referenced the compared variable. 265 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS; 266 267 SmallVector<SourceRange, 2> ranges; 268 llvm::SmallString<256> sbuf; 269 llvm::raw_svector_ostream os(sbuf); 270 271 os << "Variable '" << drCond->getDecl()->getName() 272 << "' with floating point type '" << drCond->getType().getAsString() 273 << "' should not be used as a loop counter"; 274 275 ranges.push_back(drCond->getSourceRange()); 276 ranges.push_back(drInc->getSourceRange()); 277 278 const char *bugType = "Floating point variable used as loop counter"; 279 280 PathDiagnosticLocation FSLoc = 281 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC); 282 BR.EmitBasicReport(bugType, "Security", os.str(), 283 FSLoc, ranges.data(), ranges.size()); 284 } 285 286 //===----------------------------------------------------------------------===// 287 // Check: Any use of 'gets' is insecure. 288 // Originally: <rdar://problem/6335715> 289 // Implements (part of): 300-BSI (buildsecurityin.us-cert.gov) 290 // CWE-242: Use of Inherently Dangerous Function 291 //===----------------------------------------------------------------------===// 292 293 void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) { 294 if (!filter.check_gets) 295 return; 296 297 const FunctionProtoType *FPT 298 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 299 if (!FPT) 300 return; 301 302 // Verify that the function takes a single argument. 303 if (FPT->getNumArgs() != 1) 304 return; 305 306 // Is the argument a 'char*'? 307 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0)); 308 if (!PT) 309 return; 310 311 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) 312 return; 313 314 // Issue a warning. 315 SourceRange R = CE->getCallee()->getSourceRange(); 316 PathDiagnosticLocation CELoc = 317 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 318 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'", 319 "Security", 320 "Call to function 'gets' is extremely insecure as it can " 321 "always result in a buffer overflow", 322 CELoc, &R, 1); 323 } 324 325 //===----------------------------------------------------------------------===// 326 // Check: Any use of 'getpwd' is insecure. 327 // CWE-477: Use of Obsolete Functions 328 //===----------------------------------------------------------------------===// 329 330 void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) { 331 if (!filter.check_getpw) 332 return; 333 334 const FunctionProtoType *FPT 335 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 336 if (!FPT) 337 return; 338 339 // Verify that the function takes two arguments. 340 if (FPT->getNumArgs() != 2) 341 return; 342 343 // Verify the first argument type is integer. 344 if (!FPT->getArgType(0)->isIntegerType()) 345 return; 346 347 // Verify the second argument type is char*. 348 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1)); 349 if (!PT) 350 return; 351 352 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) 353 return; 354 355 // Issue a warning. 356 SourceRange R = CE->getCallee()->getSourceRange(); 357 PathDiagnosticLocation CELoc = 358 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 359 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'", 360 "Security", 361 "The getpw() function is dangerous as it may overflow the " 362 "provided buffer. It is obsoleted by getpwuid().", 363 CELoc, &R, 1); 364 } 365 366 //===----------------------------------------------------------------------===// 367 // Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp(). 368 // CWE-377: Insecure Temporary File 369 //===----------------------------------------------------------------------===// 370 371 void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) { 372 if (!filter.check_mktemp) 373 return; 374 375 const FunctionProtoType *FPT 376 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 377 if(!FPT) 378 return; 379 380 // Verify that the function takes a single argument. 381 if (FPT->getNumArgs() != 1) 382 return; 383 384 // Verify that the argument is Pointer Type. 385 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0)); 386 if (!PT) 387 return; 388 389 // Verify that the argument is a 'char*'. 390 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) 391 return; 392 393 // Issue a waring. 394 SourceRange R = CE->getCallee()->getSourceRange(); 395 PathDiagnosticLocation CELoc = 396 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 397 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'", 398 "Security", 399 "Call to function 'mktemp' is insecure as it always " 400 "creates or uses insecure temporary file. Use 'mkstemp' instead", 401 CELoc, &R, 1); 402 } 403 404 //===----------------------------------------------------------------------===// 405 // Check: Any use of 'strcpy' is insecure. 406 // 407 // CWE-119: Improper Restriction of Operations within 408 // the Bounds of a Memory Buffer 409 //===----------------------------------------------------------------------===// 410 void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) { 411 if (!filter.check_strcpy) 412 return; 413 414 if (!checkCall_strCommon(CE, FD)) 415 return; 416 417 // Issue a warning. 418 SourceRange R = CE->getCallee()->getSourceRange(); 419 PathDiagnosticLocation CELoc = 420 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 421 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in " 422 "call 'strcpy'", 423 "Security", 424 "Call to function 'strcpy' is insecure as it does not " 425 "provide bounding of the memory buffer. Replace " 426 "unbounded copy functions with analogous functions that " 427 "support length arguments such as 'strncpy'. CWE-119.", 428 CELoc, &R, 1); 429 } 430 431 //===----------------------------------------------------------------------===// 432 // Check: Any use of 'strcat' is insecure. 433 // 434 // CWE-119: Improper Restriction of Operations within 435 // the Bounds of a Memory Buffer 436 //===----------------------------------------------------------------------===// 437 void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) { 438 if (!filter.check_strcpy) 439 return; 440 441 if (!checkCall_strCommon(CE, FD)) 442 return; 443 444 // Issue a warning. 445 SourceRange R = CE->getCallee()->getSourceRange(); 446 PathDiagnosticLocation CELoc = 447 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 448 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in " 449 "call 'strcat'", 450 "Security", 451 "Call to function 'strcat' is insecure as it does not " 452 "provide bounding of the memory buffer. Replace " 453 "unbounded copy functions with analogous functions that " 454 "support length arguments such as 'strncat'. CWE-119.", 455 CELoc, &R, 1); 456 } 457 458 //===----------------------------------------------------------------------===// 459 // Common check for str* functions with no bounds parameters. 460 //===----------------------------------------------------------------------===// 461 bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) { 462 const FunctionProtoType *FPT 463 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 464 if (!FPT) 465 return false; 466 467 // Verify the function takes two arguments, three in the _chk version. 468 int numArgs = FPT->getNumArgs(); 469 if (numArgs != 2 && numArgs != 3) 470 return false; 471 472 // Verify the type for both arguments. 473 for (int i = 0; i < 2; i++) { 474 // Verify that the arguments are pointers. 475 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i)); 476 if (!PT) 477 return false; 478 479 // Verify that the argument is a 'char*'. 480 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) 481 return false; 482 } 483 484 return true; 485 } 486 487 //===----------------------------------------------------------------------===// 488 // Check: Linear congruent random number generators should not be used 489 // Originally: <rdar://problem/63371000> 490 // CWE-338: Use of cryptographically weak prng 491 //===----------------------------------------------------------------------===// 492 493 void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) { 494 if (!filter.check_rand || !CheckRand) 495 return; 496 497 const FunctionProtoType *FTP 498 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 499 if (!FTP) 500 return; 501 502 if (FTP->getNumArgs() == 1) { 503 // Is the argument an 'unsigned short *'? 504 // (Actually any integer type is allowed.) 505 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0)); 506 if (!PT) 507 return; 508 509 if (! PT->getPointeeType()->isIntegerType()) 510 return; 511 } 512 else if (FTP->getNumArgs() != 0) 513 return; 514 515 // Issue a warning. 516 llvm::SmallString<256> buf1; 517 llvm::raw_svector_ostream os1(buf1); 518 os1 << '\'' << *FD << "' is a poor random number generator"; 519 520 llvm::SmallString<256> buf2; 521 llvm::raw_svector_ostream os2(buf2); 522 os2 << "Function '" << *FD 523 << "' is obsolete because it implements a poor random number generator." 524 << " Use 'arc4random' instead"; 525 526 SourceRange R = CE->getCallee()->getSourceRange(); 527 PathDiagnosticLocation CELoc = 528 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 529 BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1); 530 } 531 532 //===----------------------------------------------------------------------===// 533 // Check: 'random' should not be used 534 // Originally: <rdar://problem/63371000> 535 //===----------------------------------------------------------------------===// 536 537 void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) { 538 if (!CheckRand) 539 return; 540 541 const FunctionProtoType *FTP 542 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 543 if (!FTP) 544 return; 545 546 // Verify that the function takes no argument. 547 if (FTP->getNumArgs() != 0) 548 return; 549 550 // Issue a warning. 551 SourceRange R = CE->getCallee()->getSourceRange(); 552 PathDiagnosticLocation CELoc = 553 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 554 BR.EmitBasicReport("'random' is not a secure random number generator", 555 "Security", 556 "The 'random' function produces a sequence of values that " 557 "an adversary may be able to predict. Use 'arc4random' " 558 "instead", CELoc, &R, 1); 559 } 560 561 //===----------------------------------------------------------------------===// 562 // Check: 'vfork' should not be used. 563 // POS33-C: Do not use vfork(). 564 //===----------------------------------------------------------------------===// 565 566 void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) { 567 if (!filter.check_vfork) 568 return; 569 570 // All calls to vfork() are insecure, issue a warning. 571 SourceRange R = CE->getCallee()->getSourceRange(); 572 PathDiagnosticLocation CELoc = 573 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 574 BR.EmitBasicReport("Potential insecure implementation-specific behavior in " 575 "call 'vfork'", 576 "Security", 577 "Call to function 'vfork' is insecure as it can lead to " 578 "denial of service situations in the parent process. " 579 "Replace calls to vfork with calls to the safer " 580 "'posix_spawn' function", 581 CELoc, &R, 1); 582 } 583 584 //===----------------------------------------------------------------------===// 585 // Check: Should check whether privileges are dropped successfully. 586 // Originally: <rdar://problem/6337132> 587 //===----------------------------------------------------------------------===// 588 589 void WalkAST::checkUncheckedReturnValue(CallExpr *CE) { 590 const FunctionDecl *FD = CE->getDirectCallee(); 591 if (!FD) 592 return; 593 594 if (II_setid[0] == NULL) { 595 static const char * const identifiers[num_setids] = { 596 "setuid", "setgid", "seteuid", "setegid", 597 "setreuid", "setregid" 598 }; 599 600 for (size_t i = 0; i < num_setids; i++) 601 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]); 602 } 603 604 const IdentifierInfo *id = FD->getIdentifier(); 605 size_t identifierid; 606 607 for (identifierid = 0; identifierid < num_setids; identifierid++) 608 if (id == II_setid[identifierid]) 609 break; 610 611 if (identifierid >= num_setids) 612 return; 613 614 const FunctionProtoType *FTP 615 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); 616 if (!FTP) 617 return; 618 619 // Verify that the function takes one or two arguments (depending on 620 // the function). 621 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2)) 622 return; 623 624 // The arguments must be integers. 625 for (unsigned i = 0; i < FTP->getNumArgs(); i++) 626 if (! FTP->getArgType(i)->isIntegerType()) 627 return; 628 629 // Issue a warning. 630 llvm::SmallString<256> buf1; 631 llvm::raw_svector_ostream os1(buf1); 632 os1 << "Return value is not checked in call to '" << *FD << '\''; 633 634 llvm::SmallString<256> buf2; 635 llvm::raw_svector_ostream os2(buf2); 636 os2 << "The return value from the call to '" << *FD 637 << "' is not checked. If an error occurs in '" << *FD 638 << "', the following code may execute with unexpected privileges"; 639 640 SourceRange R = CE->getCallee()->getSourceRange(); 641 PathDiagnosticLocation CELoc = 642 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); 643 BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1); 644 } 645 646 //===----------------------------------------------------------------------===// 647 // SecuritySyntaxChecker 648 //===----------------------------------------------------------------------===// 649 650 namespace { 651 class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> { 652 public: 653 ChecksFilter filter; 654 655 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, 656 BugReporter &BR) const { 657 WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter); 658 walker.Visit(D->getBody()); 659 } 660 }; 661 } 662 663 #define REGISTER_CHECKER(name) \ 664 void ento::register##name(CheckerManager &mgr) {\ 665 mgr.registerChecker<SecuritySyntaxChecker>()->filter.check_##name = true;\ 666 } 667 668 REGISTER_CHECKER(gets) 669 REGISTER_CHECKER(getpw) 670 REGISTER_CHECKER(mktemp) 671 REGISTER_CHECKER(strcpy) 672 REGISTER_CHECKER(rand) 673 REGISTER_CHECKER(vfork) 674 REGISTER_CHECKER(FloatLoopCounter) 675 676