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/StringSwitch.h"
22 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
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;
37 class WalkAST : public StmtVisitor<WalkAST> {
40 enum { num_setids = 6 };
41 IdentifierInfo *II_setid[num_setids];
46 WalkAST(BugReporter &br, AnalysisContext* ac)
47 : BR(br), AC(ac), II_setid(),
48 CheckRand(isArc4RandomAvailable(BR.getContext())) {}
50 // Statement visitor methods.
51 void VisitCallExpr(CallExpr *CE);
52 void VisitForStmt(ForStmt *S);
53 void VisitCompoundStmt (CompoundStmt *S);
54 void VisitStmt(Stmt *S) { VisitChildren(S); }
56 void VisitChildren(Stmt *S);
59 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
61 typedef void (WalkAST::*FnCheck)(const CallExpr *,
62 const FunctionDecl *);
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_strcpy(const CallExpr *CE, const FunctionDecl *FD);
70 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
71 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
72 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
73 void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD);
74 void checkUncheckedReturnValue(CallExpr *CE);
76 } // end anonymous namespace
78 //===----------------------------------------------------------------------===//
80 //===----------------------------------------------------------------------===//
82 void WalkAST::VisitChildren(Stmt *S) {
83 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
88 void WalkAST::VisitCallExpr(CallExpr *CE) {
90 const FunctionDecl *FD = CE->getDirectCallee();
95 // Get the name of the callee. If it's a builtin, strip off the prefix.
96 IdentifierInfo *II = FD->getIdentifier();
97 if (!II) // if no identifier, not a simple C function
99 StringRef Name = II->getName();
100 if (Name.startswith("__builtin_"))
101 Name = Name.substr(10);
103 // Set the evaluation function by switching on the callee name.
104 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
105 .Case("gets", &WalkAST::checkCall_gets)
106 .Case("getpw", &WalkAST::checkCall_getpw)
107 .Case("mktemp", &WalkAST::checkCall_mktemp)
108 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
109 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
110 .Case("drand48", &WalkAST::checkCall_rand)
111 .Case("erand48", &WalkAST::checkCall_rand)
112 .Case("jrand48", &WalkAST::checkCall_rand)
113 .Case("lrand48", &WalkAST::checkCall_rand)
114 .Case("mrand48", &WalkAST::checkCall_rand)
115 .Case("nrand48", &WalkAST::checkCall_rand)
116 .Case("lcong48", &WalkAST::checkCall_rand)
117 .Case("rand", &WalkAST::checkCall_rand)
118 .Case("rand_r", &WalkAST::checkCall_rand)
119 .Case("random", &WalkAST::checkCall_random)
120 .Case("vfork", &WalkAST::checkCall_vfork)
123 // If the callee isn't defined, it is not of security concern.
124 // Check and evaluate the call.
126 (this->*evalFunction)(CE, FD);
128 // Recurse and check children.
132 void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
133 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
134 if (Stmt *child = *I) {
135 if (CallExpr *CE = dyn_cast<CallExpr>(child))
136 checkUncheckedReturnValue(CE);
141 void WalkAST::VisitForStmt(ForStmt *FS) {
142 checkLoopConditionForFloat(FS);
144 // Recurse and check children.
148 //===----------------------------------------------------------------------===//
149 // Check: floating poing variable used as loop counter.
150 // Originally: <rdar://problem/6336718>
151 // Implements: CERT security coding advisory FLP-30.
152 //===----------------------------------------------------------------------===//
154 static const DeclRefExpr*
155 getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
156 expr = expr->IgnoreParenCasts();
158 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
159 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
160 B->getOpcode() == BO_Comma))
163 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
166 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
172 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
173 const NamedDecl *ND = DR->getDecl();
174 return ND == x || ND == y ? DR : NULL;
177 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
178 return U->isIncrementDecrementOp()
179 ? getIncrementedVar(U->getSubExpr(), x, y) : NULL;
184 /// CheckLoopConditionForFloat - This check looks for 'for' statements that
185 /// use a floating point variable as a loop counter.
186 /// CERT: FLP30-C, FLP30-CPP.
188 void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
189 // Does the loop have a condition?
190 const Expr *condition = FS->getCond();
195 // Does the loop have an increment?
196 const Expr *increment = FS->getInc();
201 // Strip away '()' and casts.
202 condition = condition->IgnoreParenCasts();
203 increment = increment->IgnoreParenCasts();
205 // Is the loop condition a comparison?
206 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
211 // Is this a comparison?
212 if (!(B->isRelationalOp() || B->isEqualityOp()))
215 // Are we comparing variables?
216 const DeclRefExpr *drLHS =
217 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
218 const DeclRefExpr *drRHS =
219 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
221 // Does at least one of the variables have a floating point type?
222 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
223 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
225 if (!drLHS && !drRHS)
228 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
229 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
231 if (!vdLHS && !vdRHS)
234 // Does either variable appear in increment?
235 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
240 // Emit the error. First figure out which DeclRefExpr in the condition
241 // referenced the compared variable.
242 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
244 SmallVector<SourceRange, 2> ranges;
245 llvm::SmallString<256> sbuf;
246 llvm::raw_svector_ostream os(sbuf);
248 os << "Variable '" << drCond->getDecl()->getName()
249 << "' with floating point type '" << drCond->getType().getAsString()
250 << "' should not be used as a loop counter";
252 ranges.push_back(drCond->getSourceRange());
253 ranges.push_back(drInc->getSourceRange());
255 const char *bugType = "Floating point variable used as loop counter";
257 PathDiagnosticLocation FSLoc =
258 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
259 BR.EmitBasicReport(bugType, "Security", os.str(),
260 FSLoc, ranges.data(), ranges.size());
263 //===----------------------------------------------------------------------===//
264 // Check: Any use of 'gets' is insecure.
265 // Originally: <rdar://problem/6335715>
266 // Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
267 // CWE-242: Use of Inherently Dangerous Function
268 //===----------------------------------------------------------------------===//
270 void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
271 const FunctionProtoType *FPT
272 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
276 // Verify that the function takes a single argument.
277 if (FPT->getNumArgs() != 1)
280 // Is the argument a 'char*'?
281 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
285 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
289 SourceRange R = CE->getCallee()->getSourceRange();
290 PathDiagnosticLocation CELoc =
291 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
292 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
294 "Call to function 'gets' is extremely insecure as it can "
295 "always result in a buffer overflow",
299 //===----------------------------------------------------------------------===//
300 // Check: Any use of 'getpwd' is insecure.
301 // CWE-477: Use of Obsolete Functions
302 //===----------------------------------------------------------------------===//
304 void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
305 const FunctionProtoType *FPT
306 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
310 // Verify that the function takes two arguments.
311 if (FPT->getNumArgs() != 2)
314 // Verify the first argument type is integer.
315 if (!FPT->getArgType(0)->isIntegerType())
318 // Verify the second argument type is char*.
319 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
323 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
327 SourceRange R = CE->getCallee()->getSourceRange();
328 PathDiagnosticLocation CELoc =
329 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
330 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
332 "The getpw() function is dangerous as it may overflow the "
333 "provided buffer. It is obsoleted by getpwuid().",
337 //===----------------------------------------------------------------------===//
338 // Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
339 // CWE-377: Insecure Temporary File
340 //===----------------------------------------------------------------------===//
342 void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
343 const FunctionProtoType *FPT
344 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
348 // Verify that the function takes a single argument.
349 if (FPT->getNumArgs() != 1)
352 // Verify that the argument is Pointer Type.
353 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
357 // Verify that the argument is a 'char*'.
358 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
362 SourceRange R = CE->getCallee()->getSourceRange();
363 PathDiagnosticLocation CELoc =
364 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
365 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
367 "Call to function 'mktemp' is insecure as it always "
368 "creates or uses insecure temporary file. Use 'mkstemp' instead",
372 //===----------------------------------------------------------------------===//
373 // Check: Any use of 'strcpy' is insecure.
375 // CWE-119: Improper Restriction of Operations within
376 // the Bounds of a Memory Buffer
377 //===----------------------------------------------------------------------===//
378 void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
379 if (!checkCall_strCommon(CE, FD))
383 SourceRange R = CE->getCallee()->getSourceRange();
384 PathDiagnosticLocation CELoc =
385 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
386 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
389 "Call to function 'strcpy' is insecure as it does not "
390 "provide bounding of the memory buffer. Replace "
391 "unbounded copy functions with analogous functions that "
392 "support length arguments such as 'strncpy'. CWE-119.",
396 //===----------------------------------------------------------------------===//
397 // Check: Any use of 'strcat' is insecure.
399 // CWE-119: Improper Restriction of Operations within
400 // the Bounds of a Memory Buffer
401 //===----------------------------------------------------------------------===//
402 void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
403 if (!checkCall_strCommon(CE, FD))
407 SourceRange R = CE->getCallee()->getSourceRange();
408 PathDiagnosticLocation CELoc =
409 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
410 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
413 "Call to function 'strcat' is insecure as it does not "
414 "provide bounding of the memory buffer. Replace "
415 "unbounded copy functions with analogous functions that "
416 "support length arguments such as 'strncat'. CWE-119.",
420 //===----------------------------------------------------------------------===//
421 // Common check for str* functions with no bounds parameters.
422 //===----------------------------------------------------------------------===//
423 bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
424 const FunctionProtoType *FPT
425 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
429 // Verify the function takes two arguments, three in the _chk version.
430 int numArgs = FPT->getNumArgs();
431 if (numArgs != 2 && numArgs != 3)
434 // Verify the type for both arguments.
435 for (int i = 0; i < 2; i++) {
436 // Verify that the arguments are pointers.
437 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
441 // Verify that the argument is a 'char*'.
442 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
449 //===----------------------------------------------------------------------===//
450 // Check: Linear congruent random number generators should not be used
451 // Originally: <rdar://problem/63371000>
452 // CWE-338: Use of cryptographically weak prng
453 //===----------------------------------------------------------------------===//
455 void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
459 const FunctionProtoType *FTP
460 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
464 if (FTP->getNumArgs() == 1) {
465 // Is the argument an 'unsigned short *'?
466 // (Actually any integer type is allowed.)
467 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
471 if (! PT->getPointeeType()->isIntegerType())
474 else if (FTP->getNumArgs() != 0)
478 llvm::SmallString<256> buf1;
479 llvm::raw_svector_ostream os1(buf1);
480 os1 << '\'' << *FD << "' is a poor random number generator";
482 llvm::SmallString<256> buf2;
483 llvm::raw_svector_ostream os2(buf2);
484 os2 << "Function '" << *FD
485 << "' is obsolete because it implements a poor random number generator."
486 << " Use 'arc4random' instead";
488 SourceRange R = CE->getCallee()->getSourceRange();
489 PathDiagnosticLocation CELoc =
490 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
491 BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1);
494 //===----------------------------------------------------------------------===//
495 // Check: 'random' should not be used
496 // Originally: <rdar://problem/63371000>
497 //===----------------------------------------------------------------------===//
499 void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
503 const FunctionProtoType *FTP
504 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
508 // Verify that the function takes no argument.
509 if (FTP->getNumArgs() != 0)
513 SourceRange R = CE->getCallee()->getSourceRange();
514 PathDiagnosticLocation CELoc =
515 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
516 BR.EmitBasicReport("'random' is not a secure random number generator",
518 "The 'random' function produces a sequence of values that "
519 "an adversary may be able to predict. Use 'arc4random' "
520 "instead", CELoc, &R, 1);
523 //===----------------------------------------------------------------------===//
524 // Check: 'vfork' should not be used.
525 // POS33-C: Do not use vfork().
526 //===----------------------------------------------------------------------===//
528 void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
529 // All calls to vfork() are insecure, issue a warning.
530 SourceRange R = CE->getCallee()->getSourceRange();
531 PathDiagnosticLocation CELoc =
532 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
533 BR.EmitBasicReport("Potential insecure implementation-specific behavior in "
536 "Call to function 'vfork' is insecure as it can lead to "
537 "denial of service situations in the parent process. "
538 "Replace calls to vfork with calls to the safer "
539 "'posix_spawn' function",
543 //===----------------------------------------------------------------------===//
544 // Check: Should check whether privileges are dropped successfully.
545 // Originally: <rdar://problem/6337132>
546 //===----------------------------------------------------------------------===//
548 void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
549 const FunctionDecl *FD = CE->getDirectCallee();
553 if (II_setid[0] == NULL) {
554 static const char * const identifiers[num_setids] = {
555 "setuid", "setgid", "seteuid", "setegid",
556 "setreuid", "setregid"
559 for (size_t i = 0; i < num_setids; i++)
560 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
563 const IdentifierInfo *id = FD->getIdentifier();
566 for (identifierid = 0; identifierid < num_setids; identifierid++)
567 if (id == II_setid[identifierid])
570 if (identifierid >= num_setids)
573 const FunctionProtoType *FTP
574 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
578 // Verify that the function takes one or two arguments (depending on
580 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
583 // The arguments must be integers.
584 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
585 if (! FTP->getArgType(i)->isIntegerType())
589 llvm::SmallString<256> buf1;
590 llvm::raw_svector_ostream os1(buf1);
591 os1 << "Return value is not checked in call to '" << *FD << '\'';
593 llvm::SmallString<256> buf2;
594 llvm::raw_svector_ostream os2(buf2);
595 os2 << "The return value from the call to '" << *FD
596 << "' is not checked. If an error occurs in '" << *FD
597 << "', the following code may execute with unexpected privileges";
599 SourceRange R = CE->getCallee()->getSourceRange();
600 PathDiagnosticLocation CELoc =
601 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
602 BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1);
605 //===----------------------------------------------------------------------===//
606 // SecuritySyntaxChecker
607 //===----------------------------------------------------------------------===//
610 class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
612 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
613 BugReporter &BR) const {
614 WalkAST walker(BR, mgr.getAnalysisContext(D));
615 walker.Visit(D->getBody());
620 void ento::registerSecuritySyntaxChecker(CheckerManager &mgr) {
621 mgr.registerChecker<SecuritySyntaxChecker>();