"llvm/lib/Target/git@repo.hca.bsc.es:rferrer/llvm-epi-0.8.git" did not exist on "d2bc13380ffa3b8cc46b21ea2c0b9b58a189968e"
Newer
Older
Ted Kremenek
committed
//==- CheckSecuritySyntaxOnly.cpp - Basic security checks --------*- C++ -*-==//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines a set of flow-insensitive security checks.
//
//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis
committed
#include "ClangSACheckers.h"
#include "clang/Analysis/AnalysisContext.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenek
committed
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Benjamin Kramer
committed
#include "llvm/ADT/SmallString.h"
Lenny Maiorani
committed
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/raw_ostream.h"
Ted Kremenek
committed
using namespace clang;
Ted Kremenek
committed
static bool isArc4RandomAvailable(const ASTContext &Ctx) {
const llvm::Triple &T = Ctx.getTargetInfo().getTriple();
return T.getVendor() == llvm::Triple::Apple ||
T.getOS() == llvm::Triple::FreeBSD ||
T.getOS() == llvm::Triple::NetBSD ||
T.getOS() == llvm::Triple::OpenBSD ||
T.getOS() == llvm::Triple::DragonFly;
}
Ted Kremenek
committed
namespace {
Ted Kremenek
committed
struct DefaultBool {
bool val;
DefaultBool() : val(false) {}
operator bool() const { return val; }
DefaultBool &operator=(bool b) { val = b; return *this; }
};
struct ChecksFilter {
DefaultBool check_gets;
DefaultBool check_getpw;
DefaultBool check_mktemp;
Ted Kremenek
committed
DefaultBool check_mkstemp;
Ted Kremenek
committed
DefaultBool check_strcpy;
DefaultBool check_rand;
DefaultBool check_vfork;
DefaultBool check_FloatLoopCounter;
Ted Kremenek
committed
DefaultBool check_UncheckedReturn;
Ted Kremenek
committed
};
Kovarththanan Rajaratnam
committed
class WalkAST : public StmtVisitor<WalkAST> {
AnalysisDeclContext* AC;
Ted Kremenek
committed
enum { num_setids = 6 };
IdentifierInfo *II_setid[num_setids];
const bool CheckRand;
Ted Kremenek
committed
const ChecksFilter &filter;
Ted Kremenek
committed
public:
Ted Kremenek
committed
WalkAST(BugReporter &br, AnalysisDeclContext* ac,
const ChecksFilter &f)
: BR(br), AC(ac), II_setid(),
Ted Kremenek
committed
CheckRand(isArc4RandomAvailable(BR.getContext())),
filter(f) {}
Ted Kremenek
committed
// Statement visitor methods.
void VisitCallExpr(CallExpr *CE);
Ted Kremenek
committed
void VisitForStmt(ForStmt *S);
Ted Kremenek
committed
void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek
committed
void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenek
committed
void VisitChildren(Stmt *S);
// Helpers.
Lenny Maiorani
committed
bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani
committed
typedef void (WalkAST::*FnCheck)(const CallExpr *,
const FunctionDecl *);
Ted Kremenek
committed
// Checker-specific methods.
Lenny Maiorani
committed
void checkLoopConditionForFloat(const ForStmt *FS);
void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenek
committed
void checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani
committed
void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
Anna Zaks
committed
void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani
committed
void checkUncheckedReturnValue(CallExpr *CE);
Ted Kremenek
committed
};
} // end anonymous namespace
//===----------------------------------------------------------------------===//
// AST walking.
//===----------------------------------------------------------------------===//
void WalkAST::VisitChildren(Stmt *S) {
for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
if (Stmt *child = *I)
Visit(child);
}
void WalkAST::VisitCallExpr(CallExpr *CE) {
Lenny Maiorani
committed
// Get the callee.
const FunctionDecl *FD = CE->getDirectCallee();
if (!FD)
return;
// Get the name of the callee. If it's a builtin, strip off the prefix.
IdentifierInfo *II = FD->getIdentifier();
if (!II) // if no identifier, not a simple C function
return;
Chris Lattner
committed
StringRef Name = II->getName();
Lenny Maiorani
committed
if (Name.startswith("__builtin_"))
Name = Name.substr(10);
// Set the evaluation function by switching on the callee name.
FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
Lenny Maiorani
committed
.Case("gets", &WalkAST::checkCall_gets)
.Case("getpw", &WalkAST::checkCall_getpw)
.Case("mktemp", &WalkAST::checkCall_mktemp)
Ted Kremenek
committed
.Case("mkstemp", &WalkAST::checkCall_mkstemp)
.Case("mkdtemp", &WalkAST::checkCall_mkstemp)
.Case("mkstemps", &WalkAST::checkCall_mkstemp)
Lenny Maiorani
committed
.Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
.Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
.Case("drand48", &WalkAST::checkCall_rand)
.Case("erand48", &WalkAST::checkCall_rand)
.Case("jrand48", &WalkAST::checkCall_rand)
.Case("lrand48", &WalkAST::checkCall_rand)
.Case("mrand48", &WalkAST::checkCall_rand)
.Case("nrand48", &WalkAST::checkCall_rand)
.Case("lcong48", &WalkAST::checkCall_rand)
.Case("rand", &WalkAST::checkCall_rand)
.Case("rand_r", &WalkAST::checkCall_rand)
.Case("random", &WalkAST::checkCall_random)
Anna Zaks
committed
.Case("vfork", &WalkAST::checkCall_vfork)
Lenny Maiorani
committed
.Default(NULL);
// If the callee isn't defined, it is not of security concern.
// Check and evaluate the call.
if (evalFunction)
(this->*evalFunction)(CE, FD);
// Recurse and check children.
VisitChildren(CE);
}
Ted Kremenek
committed
void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
if (Stmt *child = *I) {
if (CallExpr *CE = dyn_cast<CallExpr>(child))
Lenny Maiorani
committed
checkUncheckedReturnValue(CE);
Ted Kremenek
committed
}
Ted Kremenek
committed
void WalkAST::VisitForStmt(ForStmt *FS) {
Lenny Maiorani
committed
checkLoopConditionForFloat(FS);
Ted Kremenek
committed
Ted Kremenek
committed
// Recurse and check children.
VisitChildren(FS);
Ted Kremenek
committed
}
//===----------------------------------------------------------------------===//
Ted Kremenek
committed
// Check: floating poing variable used as loop counter.
// Originally: <rdar://problem/6336718>
// Implements: CERT security coding advisory FLP-30.
Ted Kremenek
committed
//===----------------------------------------------------------------------===//
Ted Kremenek
committed
static const DeclRefExpr*
Lenny Maiorani
committed
getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
Ted Kremenek
committed
expr = expr->IgnoreParenCasts();
if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek
committed
if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCall
committed
B->getOpcode() == BO_Comma))
Ted Kremenek
committed
return NULL;
Lenny Maiorani
committed
if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
Ted Kremenek
committed
return lhs;
Lenny Maiorani
committed
if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
Ted Kremenek
committed
return rhs;
Ted Kremenek
committed
return NULL;
Ted Kremenek
committed
}
Ted Kremenek
committed
if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
const NamedDecl *ND = DR->getDecl();
return ND == x || ND == y ? DR : NULL;
}
Ted Kremenek
committed
if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
return U->isIncrementDecrementOp()
Lenny Maiorani
committed
? getIncrementedVar(U->getSubExpr(), x, y) : NULL;
Ted Kremenek
committed
Ted Kremenek
committed
return NULL;
}
Ted Kremenek
committed
/// CheckLoopConditionForFloat - This check looks for 'for' statements that
/// use a floating point variable as a loop counter.
/// CERT: FLP30-C, FLP30-CPP.
///
Lenny Maiorani
committed
void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
Ted Kremenek
committed
if (!filter.check_FloatLoopCounter)
return;
Ted Kremenek
committed
// Does the loop have a condition?
const Expr *condition = FS->getCond();
Ted Kremenek
committed
if (!condition)
return;
// Does the loop have an increment?
const Expr *increment = FS->getInc();
Ted Kremenek
committed
if (!increment)
return;
Ted Kremenek
committed
// Strip away '()' and casts.
condition = condition->IgnoreParenCasts();
increment = increment->IgnoreParenCasts();
Ted Kremenek
committed
// Is the loop condition a comparison?
const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
if (!B)
return;
// Is this a comparison?
if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek
committed
return;
Ted Kremenek
committed
// Are we comparing variables?
const DeclRefExpr *drLHS =
dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
const DeclRefExpr *drRHS =
dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
// Does at least one of the variables have a floating point type?
drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Ted Kremenek
committed
if (!drLHS && !drRHS)
return;
const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Ted Kremenek
committed
if (!vdLHS && !vdRHS)
Ted Kremenek
committed
// Does either variable appear in increment?
Lenny Maiorani
committed
const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
Ted Kremenek
committed
if (!drInc)
return;
Ted Kremenek
committed
// Emit the error. First figure out which DeclRefExpr in the condition
// referenced the compared variable.
const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
Chris Lattner
committed
SmallVector<SourceRange, 2> ranges;
SmallString<256> sbuf;
llvm::raw_svector_ostream os(sbuf);
os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek
committed
<< "' with floating point type '" << drCond->getType().getAsString()
<< "' should not be used as a loop counter";
ranges.push_back(drCond->getSourceRange());
ranges.push_back(drInc->getSourceRange());
Ted Kremenek
committed
const char *bugType = "Floating point variable used as loop counter";
PathDiagnosticLocation FSLoc =
PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
BR.EmitBasicReport(bugType, "Security", os.str(),
FSLoc, ranges.data(), ranges.size());
Ted Kremenek
committed
}
//===----------------------------------------------------------------------===//
// Check: Any use of 'gets' is insecure.
// Originally: <rdar://problem/6335715>
// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
//===----------------------------------------------------------------------===//
Lenny Maiorani
committed
void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek
committed
if (!filter.check_gets)
return;
const FunctionProtoType *FPT
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
return;
// Verify that the function takes a single argument.
if (FPT->getNumArgs() != 1)
return;
// Is the argument a 'char*'?
const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
if (!PT)
return;
if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
return;
// Issue a warning.
SourceRange R = CE->getCallee()->getSourceRange();
PathDiagnosticLocation CELoc =
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
"Security",
"Call to function 'gets' is extremely insecure as it can "
"always result in a buffer overflow",
}
//===----------------------------------------------------------------------===//
// Check: Any use of 'getpwd' is insecure.
// CWE-477: Use of Obsolete Functions
//===----------------------------------------------------------------------===//
Lenny Maiorani
committed
void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek
committed
if (!filter.check_getpw)
return;
const FunctionProtoType *FPT
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
if (!FPT)
return;
// Verify that the function takes two arguments.
if (FPT->getNumArgs() != 2)
return;
// Verify the first argument type is integer.
if (!FPT->getArgType(0)->isIntegerType())
return;
// Verify the second argument type is char*.
const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
if (!PT)
return;
if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
return;
// Issue a warning.
SourceRange R = CE->getCallee()->getSourceRange();
PathDiagnosticLocation CELoc =
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
"Security",
"The getpw() function is dangerous as it may overflow the "
"provided buffer. It is obsoleted by getpwuid().",
//===----------------------------------------------------------------------===//
Ted Kremenek
committed
// Check: Any use of 'mktemp' is insecure. It is obsoleted by mkstemp().
// CWE-377: Insecure Temporary File
//===----------------------------------------------------------------------===//
Lenny Maiorani
committed
void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek
committed
if (!filter.check_mktemp) {
// Fall back to the security check of looking for enough 'X's in the
// format string, since that is a less severe warning.
checkCall_mkstemp(CE, FD);
Ted Kremenek
committed
return;
Ted Kremenek
committed
}
Ted Kremenek
committed
const FunctionProtoType *FPT
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
// Verify that the function takes a single argument.
if (FPT->getNumArgs() != 1)
return;
// Verify that the argument is Pointer Type.
const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
if (!PT)
return;
// Verify that the argument is a 'char*'.
if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
return;
// Issue a waring.
SourceRange R = CE->getCallee()->getSourceRange();
PathDiagnosticLocation CELoc =
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
"Security",
"Call to function 'mktemp' is insecure as it always "
"creates or uses insecure temporary file. Use 'mkstemp' instead",
Ted Kremenek
committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//===----------------------------------------------------------------------===//
// Check: Use of 'mkstemp', 'mktemp', 'mkdtemp' should contain at least 6 X's.
//===----------------------------------------------------------------------===//
void WalkAST::checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD) {
if (!filter.check_mkstemp)
return;
StringRef Name = FD->getIdentifier()->getName();
std::pair<signed, signed> ArgSuffix =
llvm::StringSwitch<std::pair<signed, signed> >(Name)
.Case("mktemp", std::make_pair(0,-1))
.Case("mkstemp", std::make_pair(0,-1))
.Case("mkdtemp", std::make_pair(0,-1))
.Case("mkstemps", std::make_pair(0,1))
.Default(std::make_pair(-1, -1));
assert(ArgSuffix.first >= 0 && "Unsupported function");
// Check if the number of arguments is consistent with out expectations.
unsigned numArgs = CE->getNumArgs();
if ((signed) numArgs <= ArgSuffix.first)
return;
const StringLiteral *strArg =
dyn_cast<StringLiteral>(CE->getArg((unsigned)ArgSuffix.first)
->IgnoreParenImpCasts());
// Currently we only handle string literals. It is possible to do better,
// either by looking at references to const variables, or by doing real
// flow analysis.
if (!strArg || strArg->getCharByteWidth() != 1)
return;
// Count the number of X's, taking into account a possible cutoff suffix.
StringRef str = strArg->getString();
unsigned numX = 0;
unsigned n = str.size();
// Take into account the suffix.
unsigned suffix = 0;
if (ArgSuffix.second >= 0) {
const Expr *suffixEx = CE->getArg((unsigned)ArgSuffix.second);
llvm::APSInt Result;
if (!suffixEx->EvaluateAsInt(Result, BR.getContext()))
return;
// FIXME: Issue a warning.
if (Result.isNegative())
return;
suffix = (unsigned) Result.getZExtValue();
n = (n > suffix) ? n - suffix : 0;
}
for (unsigned i = 0; i < n; ++i)
if (str[i] == 'X') ++numX;
if (numX >= 6)
return;
// Issue a warning.
SourceRange R = strArg->getSourceRange();
PathDiagnosticLocation CELoc =
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
SmallString<512> buf;
Ted Kremenek
committed
llvm::raw_svector_ostream out(buf);
out << "Call to '" << Name << "' should have at least 6 'X's in the"
" format string to be secure (" << numX << " 'X'";
if (numX != 1)
out << 's';
out << " seen";
if (suffix) {
out << ", " << suffix << " character";
if (suffix > 1)
out << 's';
out << " used as a suffix";
}
out << ')';
BR.EmitBasicReport("Insecure temporary file creation", "Security",
out.str(), CELoc, &R, 1);
}
Lenny Maiorani
committed
//===----------------------------------------------------------------------===//
// Check: Any use of 'strcpy' is insecure.
//
// CWE-119: Improper Restriction of Operations within
// the Bounds of a Memory Buffer
//===----------------------------------------------------------------------===//
Lenny Maiorani
committed
void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek
committed
if (!filter.check_strcpy)
return;
Lenny Maiorani
committed
if (!checkCall_strCommon(CE, FD))
return;
// Issue a warning.
SourceRange R = CE->getCallee()->getSourceRange();
PathDiagnosticLocation CELoc =
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Lenny Maiorani
committed
BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
"call 'strcpy'",
"Security",
"Call to function 'strcpy' is insecure as it does not "
"provide bounding of the memory buffer. Replace "
"unbounded copy functions with analogous functions that "
"support length arguments such as 'strlcpy'. CWE-119.",
Lenny Maiorani
committed
}
//===----------------------------------------------------------------------===//
// Check: Any use of 'strcat' is insecure.
//
// CWE-119: Improper Restriction of Operations within
// the Bounds of a Memory Buffer
//===----------------------------------------------------------------------===//
void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek
committed
if (!filter.check_strcpy)
return;
Lenny Maiorani
committed
if (!checkCall_strCommon(CE, FD))
return;
// Issue a warning.
SourceRange R = CE->getCallee()->getSourceRange();
PathDiagnosticLocation CELoc =
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Lenny Maiorani
committed
BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
"call 'strcat'",
"Security",
"Call to function 'strcat' is insecure as it does not "
"provide bounding of the memory buffer. Replace "
"unbounded copy functions with analogous functions that "
"support length arguments such as 'strlcat'. CWE-119.",
Lenny Maiorani
committed
}
//===----------------------------------------------------------------------===//
// Common check for str* functions with no bounds parameters.
//===----------------------------------------------------------------------===//
bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
Lenny Maiorani
committed
const FunctionProtoType *FPT
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
if (!FPT)
Lenny Maiorani
committed
return false;
Lenny Maiorani
committed
Lenny Maiorani
committed
// Verify the function takes two arguments, three in the _chk version.
Lenny Maiorani
committed
int numArgs = FPT->getNumArgs();
if (numArgs != 2 && numArgs != 3)
Lenny Maiorani
committed
return false;
Lenny Maiorani
committed
Lenny Maiorani
committed
// Verify the type for both arguments.
Lenny Maiorani
committed
for (int i = 0; i < 2; i++) {
Lenny Maiorani
committed
// Verify that the arguments are pointers.
Lenny Maiorani
committed
const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
if (!PT)
Lenny Maiorani
committed
return false;
Lenny Maiorani
committed
// Verify that the argument is a 'char*'.
if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
Lenny Maiorani
committed
return false;
Lenny Maiorani
committed
}
Lenny Maiorani
committed
return true;
Lenny Maiorani
committed
}
Ted Kremenek
committed
//===----------------------------------------------------------------------===//
// Check: Linear congruent random number generators should not be used
// Originally: <rdar://problem/63371000>
// CWE-338: Use of cryptographically weak prng
//===----------------------------------------------------------------------===//
Lenny Maiorani
committed
void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek
committed
if (!filter.check_rand || !CheckRand)
Ted Kremenek
committed
return;
const FunctionProtoType *FTP
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek
committed
if (!FTP)
return;
Ted Kremenek
committed
if (FTP->getNumArgs() == 1) {
// Is the argument an 'unsigned short *'?
// (Actually any integer type is allowed.)
const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
if (!PT)
return;
Ted Kremenek
committed
if (! PT->getPointeeType()->isIntegerType())
return;
}
Ted Kremenek
committed
return;
Ted Kremenek
committed
// Issue a warning.
SmallString<256> buf1;
llvm::raw_svector_ostream os1(buf1);
Benjamin Kramer
committed
os1 << '\'' << *FD << "' is a poor random number generator";
Ted Kremenek
committed
SmallString<256> buf2;
llvm::raw_svector_ostream os2(buf2);
Benjamin Kramer
committed
os2 << "Function '" << *FD
Ted Kremenek
committed
<< "' is obsolete because it implements a poor random number generator."
<< " Use 'arc4random' instead";
SourceRange R = CE->getCallee()->getSourceRange();
PathDiagnosticLocation CELoc =
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1);
Ted Kremenek
committed
}
//===----------------------------------------------------------------------===//
// Check: 'random' should not be used
// Originally: <rdar://problem/63371000>
//===----------------------------------------------------------------------===//
Lenny Maiorani
committed
void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek
committed
if (!CheckRand || !filter.check_rand)
Ted Kremenek
committed
return;
const FunctionProtoType *FTP
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek
committed
if (!FTP)
return;
Ted Kremenek
committed
// Verify that the function takes no argument.
if (FTP->getNumArgs() != 0)
return;
Ted Kremenek
committed
// Issue a warning.
SourceRange R = CE->getCallee()->getSourceRange();
PathDiagnosticLocation CELoc =
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek
committed
BR.EmitBasicReport("'random' is not a secure random number generator",
"Security",
"The 'random' function produces a sequence of values that "
"an adversary may be able to predict. Use 'arc4random' "
"instead", CELoc, &R, 1);
Ted Kremenek
committed
}
Anna Zaks
committed
//===----------------------------------------------------------------------===//
// Check: 'vfork' should not be used.
// POS33-C: Do not use vfork().
//===----------------------------------------------------------------------===//
void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
Ted Kremenek
committed
if (!filter.check_vfork)
return;
Anna Zaks
committed
// All calls to vfork() are insecure, issue a warning.
SourceRange R = CE->getCallee()->getSourceRange();
PathDiagnosticLocation CELoc =
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
BR.EmitBasicReport("Potential insecure implementation-specific behavior in "
"call 'vfork'",
"Security",
"Call to function 'vfork' is insecure as it can lead to "
"denial of service situations in the parent process. "
"Replace calls to vfork with calls to the safer "
"'posix_spawn' function",
CELoc, &R, 1);
}
Ted Kremenek
committed
//===----------------------------------------------------------------------===//
// Check: Should check whether privileges are dropped successfully.
// Originally: <rdar://problem/6337132>
//===----------------------------------------------------------------------===//
Lenny Maiorani
committed
void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
Ted Kremenek
committed
if (!filter.check_UncheckedReturn)
return;
Ted Kremenek
committed
const FunctionDecl *FD = CE->getDirectCallee();
if (!FD)
return;
if (II_setid[0] == NULL) {
Ted Kremenek
committed
static const char * const identifiers[num_setids] = {
Ted Kremenek
committed
"setuid", "setgid", "seteuid", "setegid",
"setreuid", "setregid"
};
Ted Kremenek
committed
for (size_t i = 0; i < num_setids; i++)
II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek
committed
}
Ted Kremenek
committed
const IdentifierInfo *id = FD->getIdentifier();
size_t identifierid;
Ted Kremenek
committed
for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenek
committed
if (id == II_setid[identifierid])
break;
Ted Kremenek
committed
if (identifierid >= num_setids)
Ted Kremenek
committed
return;
const FunctionProtoType *FTP
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek
committed
if (!FTP)
return;
// Verify that the function takes one or two arguments (depending on
// the function).
Ted Kremenek
committed
if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
return;
// The arguments must be integers.
for (unsigned i = 0; i < FTP->getNumArgs(); i++)
if (! FTP->getArgType(i)->isIntegerType())
return;
// Issue a warning.
SmallString<256> buf1;
llvm::raw_svector_ostream os1(buf1);
Benjamin Kramer
committed
os1 << "Return value is not checked in call to '" << *FD << '\'';
Ted Kremenek
committed
SmallString<256> buf2;
llvm::raw_svector_ostream os2(buf2);
Benjamin Kramer
committed
os2 << "The return value from the call to '" << *FD
<< "' is not checked. If an error occurs in '" << *FD
Ted Kremenek
committed
<< "', the following code may execute with unexpected privileges";
SourceRange R = CE->getCallee()->getSourceRange();
PathDiagnosticLocation CELoc =
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1);
Ted Kremenek
committed
}
Ted Kremenek
committed
//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis
committed
// SecuritySyntaxChecker
Ted Kremenek
committed
//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis
committed
namespace {
class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis
committed
public:
Ted Kremenek
committed
ChecksFilter filter;
Argyrios Kyrtzidis
committed
void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
BugReporter &BR) const {
Ted Kremenek
committed
WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter);
Argyrios Kyrtzidis
committed
walker.Visit(D->getBody());
}
};
}
Ted Kremenek
committed
#define REGISTER_CHECKER(name) \
void ento::register##name(CheckerManager &mgr) {\
mgr.registerChecker<SecuritySyntaxChecker>()->filter.check_##name = true;\
Ted Kremenek
committed
}
Ted Kremenek
committed
REGISTER_CHECKER(gets)
REGISTER_CHECKER(getpw)
Ted Kremenek
committed
REGISTER_CHECKER(mkstemp)
Ted Kremenek
committed
REGISTER_CHECKER(mktemp)
REGISTER_CHECKER(strcpy)
REGISTER_CHECKER(rand)
REGISTER_CHECKER(vfork)
REGISTER_CHECKER(FloatLoopCounter)
Ted Kremenek
committed
REGISTER_CHECKER(UncheckedReturn)