2009-07-23 09:07:19 +08:00
|
|
|
//==- 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-18 05:39:33 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
2011-02-18 05:39:33 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-07-23 09:07:19 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2009-07-24 05:34:35 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-07-23 09:07:19 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-07-23 09:07:19 +08:00
|
|
|
|
2010-01-15 16:20:31 +08:00
|
|
|
static bool isArc4RandomAvailable(const ASTContext &Ctx) {
|
|
|
|
const llvm::Triple &T = Ctx.Target.getTriple();
|
|
|
|
return T.getVendor() == llvm::Triple::Apple ||
|
2011-01-18 03:16:24 +08:00
|
|
|
T.getOS() == llvm::Triple::FreeBSD ||
|
|
|
|
T.getOS() == llvm::Triple::NetBSD ||
|
|
|
|
T.getOS() == llvm::Triple::OpenBSD ||
|
|
|
|
T.getOS() == llvm::Triple::DragonFly;
|
2010-01-15 16:20:31 +08:00
|
|
|
}
|
|
|
|
|
2009-07-23 09:07:19 +08:00
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class WalkAST : public StmtVisitor<WalkAST> {
|
2009-09-09 23:08:12 +08:00
|
|
|
BugReporter &BR;
|
2009-08-28 08:08:09 +08:00
|
|
|
IdentifierInfo *II_gets;
|
2009-11-09 20:19:26 +08:00
|
|
|
IdentifierInfo *II_getpw;
|
2009-12-03 17:15:23 +08:00
|
|
|
IdentifierInfo *II_mktemp;
|
2009-09-02 10:47:41 +08:00
|
|
|
enum { num_rands = 9 };
|
|
|
|
IdentifierInfo *II_rand[num_rands];
|
|
|
|
IdentifierInfo *II_random;
|
|
|
|
enum { num_setids = 6 };
|
|
|
|
IdentifierInfo *II_setid[num_setids];
|
2010-03-25 06:39:47 +08:00
|
|
|
|
2010-01-15 16:20:31 +08:00
|
|
|
const bool CheckRand;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 09:07:19 +08:00
|
|
|
public:
|
2009-07-24 06:29:41 +08:00
|
|
|
WalkAST(BugReporter &br) : BR(br),
|
2010-08-22 09:00:03 +08:00
|
|
|
II_gets(0), II_getpw(0), II_mktemp(0),
|
|
|
|
II_rand(), II_random(0), II_setid(),
|
2010-01-15 16:20:31 +08:00
|
|
|
CheckRand(isArc4RandomAvailable(BR.getContext())) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 09:07:19 +08:00
|
|
|
// Statement visitor methods.
|
2009-07-24 06:29:41 +08:00
|
|
|
void VisitCallExpr(CallExpr *CE);
|
2009-07-23 09:07:19 +08:00
|
|
|
void VisitForStmt(ForStmt *S);
|
2009-08-28 08:08:09 +08:00
|
|
|
void VisitCompoundStmt (CompoundStmt *S);
|
2009-07-24 05:34:35 +08:00
|
|
|
void VisitStmt(Stmt *S) { VisitChildren(S); }
|
2009-07-23 09:07:19 +08:00
|
|
|
|
|
|
|
void VisitChildren(Stmt *S);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 06:29:41 +08:00
|
|
|
// Helpers.
|
|
|
|
IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 09:07:19 +08:00
|
|
|
// Checker-specific methods.
|
2009-07-24 05:34:35 +08:00
|
|
|
void CheckLoopConditionForFloat(const ForStmt *FS);
|
2009-07-24 06:29:41 +08:00
|
|
|
void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD);
|
2009-11-09 20:19:26 +08:00
|
|
|
void CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
|
2009-12-03 17:15:23 +08:00
|
|
|
void CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
|
2011-04-01 06:09:14 +08:00
|
|
|
void CheckCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
|
2009-09-02 10:47:41 +08:00
|
|
|
void CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD);
|
|
|
|
void CheckCall_random(const CallExpr *CE, const FunctionDecl *FD);
|
2009-08-28 08:08:09 +08:00
|
|
|
void CheckUncheckedReturnValue(CallExpr *CE);
|
2009-07-23 09:07:19 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2009-07-24 06:29:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) {
|
|
|
|
if (!II)
|
|
|
|
II = &BR.getContext().Idents.get(str);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
return II;
|
2009-07-24 06:29:41 +08:00
|
|
|
}
|
|
|
|
|
2009-07-23 09:07:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2009-07-24 06:29:41 +08:00
|
|
|
void WalkAST::VisitCallExpr(CallExpr *CE) {
|
|
|
|
if (const FunctionDecl *FD = CE->getDirectCallee()) {
|
2009-09-02 10:47:41 +08:00
|
|
|
CheckCall_gets(CE, FD);
|
2009-11-09 20:19:26 +08:00
|
|
|
CheckCall_getpw(CE, FD);
|
2009-12-03 17:15:23 +08:00
|
|
|
CheckCall_mktemp(CE, FD);
|
2011-04-01 06:09:14 +08:00
|
|
|
CheckCall_strcpy(CE, FD);
|
2010-01-15 16:20:31 +08:00
|
|
|
if (CheckRand) {
|
|
|
|
CheckCall_rand(CE, FD);
|
|
|
|
CheckCall_random(CE, FD);
|
|
|
|
}
|
2009-07-24 06:29:41 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 06:29:41 +08:00
|
|
|
// Recurse and check children.
|
|
|
|
VisitChildren(CE);
|
|
|
|
}
|
|
|
|
|
2009-08-28 08:08:09 +08:00
|
|
|
void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
|
|
|
|
for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
|
2009-09-09 23:08:12 +08:00
|
|
|
if (Stmt *child = *I) {
|
|
|
|
if (CallExpr *CE = dyn_cast<CallExpr>(child))
|
|
|
|
CheckUncheckedReturnValue(CE);
|
|
|
|
Visit(child);
|
|
|
|
}
|
2009-08-28 08:08:09 +08:00
|
|
|
}
|
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
void WalkAST::VisitForStmt(ForStmt *FS) {
|
2009-09-09 23:08:12 +08:00
|
|
|
CheckLoopConditionForFloat(FS);
|
2009-07-23 09:07:19 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
// Recurse and check children.
|
|
|
|
VisitChildren(FS);
|
2009-07-23 09:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-07-24 05:34:35 +08:00
|
|
|
// Check: floating poing variable used as loop counter.
|
2009-07-24 05:44:18 +08:00
|
|
|
// Originally: <rdar://problem/6336718>
|
|
|
|
// Implements: CERT security coding advisory FLP-30.
|
2009-07-23 09:07:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
static const DeclRefExpr*
|
|
|
|
GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
|
|
|
|
expr = expr->IgnoreParenCasts();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
|
2009-07-24 05:34:35 +08:00
|
|
|
if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
|
2010-08-25 19:45:40 +08:00
|
|
|
B->getOpcode() == BO_Comma))
|
2009-07-24 05:34:35 +08:00
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
|
|
|
|
return lhs;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
|
|
|
|
return rhs;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
return NULL;
|
2009-07-23 09:07:19 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
|
|
|
|
const NamedDecl *ND = DR->getDecl();
|
|
|
|
return ND == x || ND == y ? DR : NULL;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
|
|
|
|
return U->isIncrementDecrementOp()
|
|
|
|
? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
|
|
|
|
|
2009-07-23 09:07:19 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
/// CheckLoopConditionForFloat - This check looks for 'for' statements that
|
|
|
|
/// use a floating point variable as a loop counter.
|
|
|
|
/// CERT: FLP30-C, FLP30-CPP.
|
|
|
|
///
|
|
|
|
void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
|
|
|
|
// Does the loop have a condition?
|
|
|
|
const Expr *condition = FS->getCond();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
if (!condition)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Does the loop have an increment?
|
|
|
|
const Expr *increment = FS->getInc();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
if (!increment)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
// Strip away '()' and casts.
|
|
|
|
condition = condition->IgnoreParenCasts();
|
|
|
|
increment = increment->IgnoreParenCasts();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
// Is the loop condition a comparison?
|
|
|
|
const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
|
|
|
|
|
|
|
|
if (!B)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 04:26:31 +08:00
|
|
|
// Is this a comparison?
|
|
|
|
if (!(B->isRelationalOp() || B->isEqualityOp()))
|
2009-07-24 05:34:35 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
// Are we comparing variables?
|
2010-12-04 11:47:34 +08:00
|
|
|
const DeclRefExpr *drLHS =
|
|
|
|
dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
|
|
|
|
const DeclRefExpr *drRHS =
|
|
|
|
dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 04:26:31 +08:00
|
|
|
// Does at least one of the variables have a floating point type?
|
2010-06-23 07:07:26 +08:00
|
|
|
drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
|
|
|
|
drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
if (!drLHS && !drRHS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
|
|
|
|
const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
if (!vdLHS && !vdRHS)
|
2009-09-09 23:08:12 +08:00
|
|
|
return;
|
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
// Does either variable appear in increment?
|
|
|
|
const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
if (!drInc)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
// Emit the error. First figure out which DeclRefExpr in the condition
|
|
|
|
// referenced the compared variable.
|
|
|
|
const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::SmallVector<SourceRange, 2> ranges;
|
2010-03-25 06:39:47 +08:00
|
|
|
llvm::SmallString<256> sbuf;
|
|
|
|
llvm::raw_svector_ostream os(sbuf);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-18 06:39:59 +08:00
|
|
|
os << "Variable '" << drCond->getDecl()->getName()
|
2009-07-24 05:34:35 +08:00
|
|
|
<< "' 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());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 05:34:35 +08:00
|
|
|
const char *bugType = "Floating point variable used as loop counter";
|
2009-11-30 02:27:55 +08:00
|
|
|
BR.EmitBasicReport(bugType, "Security", os.str(),
|
2009-07-24 05:34:35 +08:00
|
|
|
FS->getLocStart(), ranges.data(), ranges.size());
|
2009-07-23 09:07:19 +08:00
|
|
|
}
|
|
|
|
|
2009-07-24 06:29:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check: Any use of 'gets' is insecure.
|
|
|
|
// Originally: <rdar://problem/6335715>
|
|
|
|
// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
|
2009-11-09 16:13:04 +08:00
|
|
|
// CWE-242: Use of Inherently Dangerous Function
|
2009-07-24 06:29:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
|
|
|
|
if (FD->getIdentifier() != GetIdentifier(II_gets, "gets"))
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-15 06:11:44 +08:00
|
|
|
const FunctionProtoType *FPT
|
|
|
|
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
|
2009-11-09 20:19:26 +08:00
|
|
|
if (!FPT)
|
2009-07-24 06:29:41 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 06:29:41 +08:00
|
|
|
// Verify that the function takes a single argument.
|
2009-11-09 20:19:26 +08:00
|
|
|
if (FPT->getNumArgs() != 1)
|
2009-07-24 06:29:41 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Is the argument a 'char*'?
|
2009-11-09 20:19:26 +08:00
|
|
|
const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
|
2009-07-24 06:29:41 +08:00
|
|
|
if (!PT)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 06:29:41 +08:00
|
|
|
if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 06:29:41 +08:00
|
|
|
// Issue a warning.
|
|
|
|
SourceRange R = CE->getCallee()->getSourceRange();
|
|
|
|
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",
|
|
|
|
CE->getLocStart(), &R, 1);
|
|
|
|
}
|
|
|
|
|
2009-11-09 20:19:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check: Any use of 'getpwd' is insecure.
|
|
|
|
// CWE-477: Use of Obsolete Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void WalkAST::CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
|
|
|
|
if (FD->getIdentifier() != GetIdentifier(II_getpw, "getpw"))
|
|
|
|
return;
|
|
|
|
|
2010-12-15 06:11:44 +08:00
|
|
|
const FunctionProtoType *FPT
|
|
|
|
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
|
2009-11-09 20:19:26 +08:00
|
|
|
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();
|
|
|
|
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().",
|
|
|
|
CE->getLocStart(), &R, 1);
|
|
|
|
}
|
|
|
|
|
2009-12-03 17:15:23 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
|
|
|
|
// CWE-377: Insecure Temporary File
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void WalkAST::CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
|
|
|
|
if (FD->getIdentifier() != GetIdentifier(II_mktemp, "mktemp"))
|
|
|
|
return;
|
|
|
|
|
2010-12-15 06:11:44 +08:00
|
|
|
const FunctionProtoType *FPT
|
|
|
|
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
|
2009-12-03 17:15:23 +08:00
|
|
|
if(!FPT)
|
|
|
|
return;
|
2010-03-25 06:39:47 +08:00
|
|
|
|
2011-04-01 05:26:55 +08:00
|
|
|
// Verify that the function takes a single argument.
|
2009-12-03 17:15:23 +08:00
|
|
|
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;
|
2010-03-25 06:39:45 +08:00
|
|
|
|
2009-12-03 17:15:23 +08:00
|
|
|
// Issue a waring.
|
|
|
|
SourceRange R = CE->getCallee()->getSourceRange();
|
|
|
|
BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
|
2010-08-22 09:00:03 +08:00
|
|
|
"Security",
|
|
|
|
"Call to function 'mktemp' is insecure as it always "
|
|
|
|
"creates or uses insecure temporary file. Use 'mkstemp' instead",
|
|
|
|
CE->getLocStart(), &R, 1);
|
2009-12-03 17:15:23 +08:00
|
|
|
}
|
|
|
|
|
2011-04-01 06:09:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check: Any use of 'strcpy' is insecure.
|
|
|
|
//
|
|
|
|
// CWE-119: Improper Restriction of Operations within
|
|
|
|
// the Bounds of a Memory Buffer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
void WalkAST::CheckCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
|
|
|
|
IdentifierInfo *II = FD->getIdentifier();
|
|
|
|
if (!II) // if no identifier, not a simple C function
|
|
|
|
return;
|
|
|
|
llvm::StringRef Name = II->getName();
|
|
|
|
if (Name.startswith("__builtin_"))
|
|
|
|
Name = Name.substr(10);
|
|
|
|
|
|
|
|
if ((Name != "strcpy") &&
|
|
|
|
(Name != "__strcpy_chk"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const FunctionProtoType *FPT
|
|
|
|
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
|
|
|
|
if (!FPT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Verify the function takes two arguments
|
|
|
|
int numArgs = FPT->getNumArgs();
|
|
|
|
if (numArgs != 2 && numArgs != 3)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Verify the type for both arguments
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
// Verify that the arguments are pointers
|
|
|
|
const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
|
|
|
|
if (!PT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Verify that the argument is a 'char*'.
|
|
|
|
if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue a warning
|
|
|
|
SourceRange R = CE->getCallee()->getSourceRange();
|
|
|
|
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 'strncpy'. CWE-119.",
|
|
|
|
CE->getLocStart(), &R, 1);
|
|
|
|
}
|
|
|
|
|
2009-09-02 10:47:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check: Linear congruent random number generators should not be used
|
|
|
|
// Originally: <rdar://problem/63371000>
|
|
|
|
// CWE-338: Use of cryptographically weak prng
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void WalkAST::CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
|
|
|
|
if (II_rand[0] == NULL) {
|
|
|
|
// This check applies to these functions
|
|
|
|
static const char * const identifiers[num_rands] = {
|
|
|
|
"drand48", "erand48", "jrand48", "lrand48", "mrand48", "nrand48",
|
|
|
|
"lcong48",
|
|
|
|
"rand", "rand_r"
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-02 10:47:41 +08:00
|
|
|
for (size_t i = 0; i < num_rands; i++)
|
2009-09-09 23:08:12 +08:00
|
|
|
II_rand[i] = &BR.getContext().Idents.get(identifiers[i]);
|
2009-09-02 10:47:41 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-02 10:47:41 +08:00
|
|
|
const IdentifierInfo *id = FD->getIdentifier();
|
|
|
|
size_t identifierid;
|
|
|
|
|
|
|
|
for (identifierid = 0; identifierid < num_rands; identifierid++)
|
|
|
|
if (id == II_rand[identifierid])
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (identifierid >= num_rands)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-15 06:11:44 +08:00
|
|
|
const FunctionProtoType *FTP
|
|
|
|
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
|
2009-09-02 10:47:41 +08:00
|
|
|
if (!FTP)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-02 10:47:41 +08:00
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-02 10:47:41 +08:00
|
|
|
if (! PT->getPointeeType()->isIntegerType())
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
else if (FTP->getNumArgs() != 0)
|
2009-09-02 10:47:41 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-02 10:47:41 +08:00
|
|
|
// Issue a warning.
|
2010-03-25 06:39:47 +08:00
|
|
|
llvm::SmallString<256> buf1;
|
|
|
|
llvm::raw_svector_ostream os1(buf1);
|
2010-04-17 17:33:03 +08:00
|
|
|
os1 << '\'' << FD << "' is a poor random number generator";
|
2009-09-02 10:47:41 +08:00
|
|
|
|
2010-03-25 06:39:47 +08:00
|
|
|
llvm::SmallString<256> buf2;
|
|
|
|
llvm::raw_svector_ostream os2(buf2);
|
2010-04-17 17:33:03 +08:00
|
|
|
os2 << "Function '" << FD
|
2009-09-02 10:47:41 +08:00
|
|
|
<< "' is obsolete because it implements a poor random number generator."
|
|
|
|
<< " Use 'arc4random' instead";
|
|
|
|
|
|
|
|
SourceRange R = CE->getCallee()->getSourceRange();
|
2010-03-25 06:39:47 +08:00
|
|
|
BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
|
2009-09-02 10:47:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check: 'random' should not be used
|
|
|
|
// Originally: <rdar://problem/63371000>
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void WalkAST::CheckCall_random(const CallExpr *CE, const FunctionDecl *FD) {
|
|
|
|
if (FD->getIdentifier() != GetIdentifier(II_random, "random"))
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-15 06:11:44 +08:00
|
|
|
const FunctionProtoType *FTP
|
|
|
|
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
|
2009-09-02 10:47:41 +08:00
|
|
|
if (!FTP)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-02 10:47:41 +08:00
|
|
|
// Verify that the function takes no argument.
|
|
|
|
if (FTP->getNumArgs() != 0)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-02 10:47:41 +08:00
|
|
|
// Issue a warning.
|
|
|
|
SourceRange R = CE->getCallee()->getSourceRange();
|
|
|
|
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' "
|
2010-03-25 06:39:47 +08:00
|
|
|
"instead", CE->getLocStart(), &R, 1);
|
2009-09-02 10:47:41 +08:00
|
|
|
}
|
|
|
|
|
2009-08-28 08:08:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check: Should check whether privileges are dropped successfully.
|
|
|
|
// Originally: <rdar://problem/6337132>
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) {
|
|
|
|
const FunctionDecl *FD = CE->getDirectCallee();
|
|
|
|
if (!FD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (II_setid[0] == NULL) {
|
2009-09-02 10:47:41 +08:00
|
|
|
static const char * const identifiers[num_setids] = {
|
2009-08-28 08:08:09 +08:00
|
|
|
"setuid", "setgid", "seteuid", "setegid",
|
|
|
|
"setreuid", "setregid"
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-02 10:47:41 +08:00
|
|
|
for (size_t i = 0; i < num_setids; i++)
|
2009-09-09 23:08:12 +08:00
|
|
|
II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
|
2009-08-28 08:08:09 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-28 08:08:09 +08:00
|
|
|
const IdentifierInfo *id = FD->getIdentifier();
|
|
|
|
size_t identifierid;
|
|
|
|
|
2009-09-02 10:47:41 +08:00
|
|
|
for (identifierid = 0; identifierid < num_setids; identifierid++)
|
2009-08-28 08:08:09 +08:00
|
|
|
if (id == II_setid[identifierid])
|
|
|
|
break;
|
|
|
|
|
2009-09-02 10:47:41 +08:00
|
|
|
if (identifierid >= num_setids)
|
2009-08-28 08:08:09 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-15 06:11:44 +08:00
|
|
|
const FunctionProtoType *FTP
|
|
|
|
= dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
|
2009-08-28 08:08:09 +08:00
|
|
|
if (!FTP)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-28 08:24:55 +08:00
|
|
|
// Verify that the function takes one or two arguments (depending on
|
|
|
|
// the function).
|
2009-08-28 08:08:09 +08:00
|
|
|
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.
|
2010-03-25 06:39:47 +08:00
|
|
|
llvm::SmallString<256> buf1;
|
|
|
|
llvm::raw_svector_ostream os1(buf1);
|
2010-04-17 17:33:03 +08:00
|
|
|
os1 << "Return value is not checked in call to '" << FD << '\'';
|
2009-08-28 08:08:09 +08:00
|
|
|
|
2010-03-25 06:39:47 +08:00
|
|
|
llvm::SmallString<256> buf2;
|
|
|
|
llvm::raw_svector_ostream os2(buf2);
|
2010-04-17 17:33:03 +08:00
|
|
|
os2 << "The return value from the call to '" << FD
|
|
|
|
<< "' is not checked. If an error occurs in '" << FD
|
2009-08-28 08:08:09 +08:00
|
|
|
<< "', the following code may execute with unexpected privileges";
|
|
|
|
|
|
|
|
SourceRange R = CE->getCallee()->getSourceRange();
|
2010-03-25 06:39:47 +08:00
|
|
|
BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
|
2009-08-28 08:08:09 +08:00
|
|
|
}
|
|
|
|
|
2009-07-23 09:07:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2011-02-18 05:39:33 +08:00
|
|
|
// SecuritySyntaxChecker
|
2009-07-23 09:07:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-18 05:39:33 +08:00
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
|
2011-02-18 05:39:33 +08:00
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
WalkAST walker(BR);
|
|
|
|
walker.Visit(D->getBody());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerSecuritySyntaxChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<SecuritySyntaxChecker>();
|
2009-07-23 09:07:19 +08:00
|
|
|
}
|