1f4a2713aSLionel Sambuc //== CStringSyntaxChecker.cpp - CoreFoundation containers API *- C++ -*-==//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // An AST checker that looks for common pitfalls when using C string APIs.
11f4a2713aSLionel Sambuc // - Identifies erroneous patterns in the last argument to strncat - the number
12f4a2713aSLionel Sambuc // of bytes to copy.
13f4a2713aSLionel Sambuc //
14f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
15f4a2713aSLionel Sambuc #include "ClangSACheckers.h"
16f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
17f4a2713aSLionel Sambuc #include "clang/AST/OperationKinds.h"
18f4a2713aSLionel Sambuc #include "clang/AST/StmtVisitor.h"
19f4a2713aSLionel Sambuc #include "clang/Analysis/AnalysisContext.h"
20f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
21f4a2713aSLionel Sambuc #include "clang/Basic/TypeTraits.h"
22f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
23f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Checker.h"
24f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
25f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
26f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
27f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
28f4a2713aSLionel Sambuc
29f4a2713aSLionel Sambuc using namespace clang;
30f4a2713aSLionel Sambuc using namespace ento;
31f4a2713aSLionel Sambuc
32f4a2713aSLionel Sambuc namespace {
33f4a2713aSLionel Sambuc class WalkAST: public StmtVisitor<WalkAST> {
34*0a6a1f1dSLionel Sambuc const CheckerBase *Checker;
35f4a2713aSLionel Sambuc BugReporter &BR;
36f4a2713aSLionel Sambuc AnalysisDeclContext* AC;
37f4a2713aSLionel Sambuc
38f4a2713aSLionel Sambuc /// Check if two expressions refer to the same declaration.
sameDecl(const Expr * A1,const Expr * A2)39f4a2713aSLionel Sambuc inline bool sameDecl(const Expr *A1, const Expr *A2) {
40f4a2713aSLionel Sambuc if (const DeclRefExpr *D1 = dyn_cast<DeclRefExpr>(A1->IgnoreParenCasts()))
41f4a2713aSLionel Sambuc if (const DeclRefExpr *D2 = dyn_cast<DeclRefExpr>(A2->IgnoreParenCasts()))
42f4a2713aSLionel Sambuc return D1->getDecl() == D2->getDecl();
43f4a2713aSLionel Sambuc return false;
44f4a2713aSLionel Sambuc }
45f4a2713aSLionel Sambuc
46f4a2713aSLionel Sambuc /// Check if the expression E is a sizeof(WithArg).
isSizeof(const Expr * E,const Expr * WithArg)47f4a2713aSLionel Sambuc inline bool isSizeof(const Expr *E, const Expr *WithArg) {
48f4a2713aSLionel Sambuc if (const UnaryExprOrTypeTraitExpr *UE =
49f4a2713aSLionel Sambuc dyn_cast<UnaryExprOrTypeTraitExpr>(E))
50f4a2713aSLionel Sambuc if (UE->getKind() == UETT_SizeOf)
51f4a2713aSLionel Sambuc return sameDecl(UE->getArgumentExpr(), WithArg);
52f4a2713aSLionel Sambuc return false;
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc
55f4a2713aSLionel Sambuc /// Check if the expression E is a strlen(WithArg).
isStrlen(const Expr * E,const Expr * WithArg)56f4a2713aSLionel Sambuc inline bool isStrlen(const Expr *E, const Expr *WithArg) {
57f4a2713aSLionel Sambuc if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
58f4a2713aSLionel Sambuc const FunctionDecl *FD = CE->getDirectCallee();
59f4a2713aSLionel Sambuc if (!FD)
60f4a2713aSLionel Sambuc return false;
61f4a2713aSLionel Sambuc return (CheckerContext::isCLibraryFunction(FD, "strlen") &&
62f4a2713aSLionel Sambuc sameDecl(CE->getArg(0), WithArg));
63f4a2713aSLionel Sambuc }
64f4a2713aSLionel Sambuc return false;
65f4a2713aSLionel Sambuc }
66f4a2713aSLionel Sambuc
67f4a2713aSLionel Sambuc /// Check if the expression is an integer literal with value 1.
isOne(const Expr * E)68f4a2713aSLionel Sambuc inline bool isOne(const Expr *E) {
69f4a2713aSLionel Sambuc if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E))
70f4a2713aSLionel Sambuc return (IL->getValue().isIntN(1));
71f4a2713aSLionel Sambuc return false;
72f4a2713aSLionel Sambuc }
73f4a2713aSLionel Sambuc
getPrintableName(const Expr * E)74f4a2713aSLionel Sambuc inline StringRef getPrintableName(const Expr *E) {
75f4a2713aSLionel Sambuc if (const DeclRefExpr *D = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
76f4a2713aSLionel Sambuc return D->getDecl()->getName();
77f4a2713aSLionel Sambuc return StringRef();
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc
80f4a2713aSLionel Sambuc /// Identify erroneous patterns in the last argument to strncat - the number
81f4a2713aSLionel Sambuc /// of bytes to copy.
82f4a2713aSLionel Sambuc bool containsBadStrncatPattern(const CallExpr *CE);
83f4a2713aSLionel Sambuc
84f4a2713aSLionel Sambuc public:
WalkAST(const CheckerBase * checker,BugReporter & br,AnalysisDeclContext * ac)85*0a6a1f1dSLionel Sambuc WalkAST(const CheckerBase *checker, BugReporter &br, AnalysisDeclContext *ac)
86*0a6a1f1dSLionel Sambuc : Checker(checker), BR(br), AC(ac) {}
87f4a2713aSLionel Sambuc
88f4a2713aSLionel Sambuc // Statement visitor methods.
89f4a2713aSLionel Sambuc void VisitChildren(Stmt *S);
VisitStmt(Stmt * S)90f4a2713aSLionel Sambuc void VisitStmt(Stmt *S) {
91f4a2713aSLionel Sambuc VisitChildren(S);
92f4a2713aSLionel Sambuc }
93f4a2713aSLionel Sambuc void VisitCallExpr(CallExpr *CE);
94f4a2713aSLionel Sambuc };
95f4a2713aSLionel Sambuc } // end anonymous namespace
96f4a2713aSLionel Sambuc
97f4a2713aSLionel Sambuc // The correct size argument should look like following:
98f4a2713aSLionel Sambuc // strncat(dst, src, sizeof(dst) - strlen(dest) - 1);
99f4a2713aSLionel Sambuc // We look for the following anti-patterns:
100f4a2713aSLionel Sambuc // - strncat(dst, src, sizeof(dst) - strlen(dst));
101f4a2713aSLionel Sambuc // - strncat(dst, src, sizeof(dst) - 1);
102f4a2713aSLionel Sambuc // - strncat(dst, src, sizeof(dst));
containsBadStrncatPattern(const CallExpr * CE)103f4a2713aSLionel Sambuc bool WalkAST::containsBadStrncatPattern(const CallExpr *CE) {
104f4a2713aSLionel Sambuc if (CE->getNumArgs() != 3)
105f4a2713aSLionel Sambuc return false;
106f4a2713aSLionel Sambuc const Expr *DstArg = CE->getArg(0);
107f4a2713aSLionel Sambuc const Expr *SrcArg = CE->getArg(1);
108f4a2713aSLionel Sambuc const Expr *LenArg = CE->getArg(2);
109f4a2713aSLionel Sambuc
110f4a2713aSLionel Sambuc // Identify wrong size expressions, which are commonly used instead.
111f4a2713aSLionel Sambuc if (const BinaryOperator *BE =
112f4a2713aSLionel Sambuc dyn_cast<BinaryOperator>(LenArg->IgnoreParenCasts())) {
113f4a2713aSLionel Sambuc // - sizeof(dst) - strlen(dst)
114f4a2713aSLionel Sambuc if (BE->getOpcode() == BO_Sub) {
115f4a2713aSLionel Sambuc const Expr *L = BE->getLHS();
116f4a2713aSLionel Sambuc const Expr *R = BE->getRHS();
117f4a2713aSLionel Sambuc if (isSizeof(L, DstArg) && isStrlen(R, DstArg))
118f4a2713aSLionel Sambuc return true;
119f4a2713aSLionel Sambuc
120f4a2713aSLionel Sambuc // - sizeof(dst) - 1
121f4a2713aSLionel Sambuc if (isSizeof(L, DstArg) && isOne(R->IgnoreParenCasts()))
122f4a2713aSLionel Sambuc return true;
123f4a2713aSLionel Sambuc }
124f4a2713aSLionel Sambuc }
125f4a2713aSLionel Sambuc // - sizeof(dst)
126f4a2713aSLionel Sambuc if (isSizeof(LenArg, DstArg))
127f4a2713aSLionel Sambuc return true;
128f4a2713aSLionel Sambuc
129f4a2713aSLionel Sambuc // - sizeof(src)
130f4a2713aSLionel Sambuc if (isSizeof(LenArg, SrcArg))
131f4a2713aSLionel Sambuc return true;
132f4a2713aSLionel Sambuc return false;
133f4a2713aSLionel Sambuc }
134f4a2713aSLionel Sambuc
VisitCallExpr(CallExpr * CE)135f4a2713aSLionel Sambuc void WalkAST::VisitCallExpr(CallExpr *CE) {
136f4a2713aSLionel Sambuc const FunctionDecl *FD = CE->getDirectCallee();
137f4a2713aSLionel Sambuc if (!FD)
138f4a2713aSLionel Sambuc return;
139f4a2713aSLionel Sambuc
140f4a2713aSLionel Sambuc if (CheckerContext::isCLibraryFunction(FD, "strncat")) {
141f4a2713aSLionel Sambuc if (containsBadStrncatPattern(CE)) {
142f4a2713aSLionel Sambuc const Expr *DstArg = CE->getArg(0);
143f4a2713aSLionel Sambuc const Expr *LenArg = CE->getArg(2);
144f4a2713aSLionel Sambuc PathDiagnosticLocation Loc =
145f4a2713aSLionel Sambuc PathDiagnosticLocation::createBegin(LenArg, BR.getSourceManager(), AC);
146f4a2713aSLionel Sambuc
147f4a2713aSLionel Sambuc StringRef DstName = getPrintableName(DstArg);
148f4a2713aSLionel Sambuc
149f4a2713aSLionel Sambuc SmallString<256> S;
150f4a2713aSLionel Sambuc llvm::raw_svector_ostream os(S);
151f4a2713aSLionel Sambuc os << "Potential buffer overflow. ";
152f4a2713aSLionel Sambuc if (!DstName.empty()) {
153f4a2713aSLionel Sambuc os << "Replace with 'sizeof(" << DstName << ") "
154f4a2713aSLionel Sambuc "- strlen(" << DstName <<") - 1'";
155f4a2713aSLionel Sambuc os << " or u";
156f4a2713aSLionel Sambuc } else
157f4a2713aSLionel Sambuc os << "U";
158f4a2713aSLionel Sambuc os << "se a safer 'strlcat' API";
159f4a2713aSLionel Sambuc
160*0a6a1f1dSLionel Sambuc BR.EmitBasicReport(FD, Checker, "Anti-pattern in the argument",
161*0a6a1f1dSLionel Sambuc "C String API", os.str(), Loc,
162*0a6a1f1dSLionel Sambuc LenArg->getSourceRange());
163f4a2713aSLionel Sambuc }
164f4a2713aSLionel Sambuc }
165f4a2713aSLionel Sambuc
166f4a2713aSLionel Sambuc // Recurse and check children.
167f4a2713aSLionel Sambuc VisitChildren(CE);
168f4a2713aSLionel Sambuc }
169f4a2713aSLionel Sambuc
VisitChildren(Stmt * S)170f4a2713aSLionel Sambuc void WalkAST::VisitChildren(Stmt *S) {
171f4a2713aSLionel Sambuc for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E;
172f4a2713aSLionel Sambuc ++I)
173f4a2713aSLionel Sambuc if (Stmt *child = *I)
174f4a2713aSLionel Sambuc Visit(child);
175f4a2713aSLionel Sambuc }
176f4a2713aSLionel Sambuc
177f4a2713aSLionel Sambuc namespace {
178f4a2713aSLionel Sambuc class CStringSyntaxChecker: public Checker<check::ASTCodeBody> {
179f4a2713aSLionel Sambuc public:
180f4a2713aSLionel Sambuc
checkASTCodeBody(const Decl * D,AnalysisManager & Mgr,BugReporter & BR) const181f4a2713aSLionel Sambuc void checkASTCodeBody(const Decl *D, AnalysisManager& Mgr,
182f4a2713aSLionel Sambuc BugReporter &BR) const {
183*0a6a1f1dSLionel Sambuc WalkAST walker(this, BR, Mgr.getAnalysisDeclContext(D));
184f4a2713aSLionel Sambuc walker.Visit(D->getBody());
185f4a2713aSLionel Sambuc }
186f4a2713aSLionel Sambuc };
187f4a2713aSLionel Sambuc }
188f4a2713aSLionel Sambuc
registerCStringSyntaxChecker(CheckerManager & mgr)189f4a2713aSLionel Sambuc void ento::registerCStringSyntaxChecker(CheckerManager &mgr) {
190f4a2713aSLionel Sambuc mgr.registerChecker<CStringSyntaxChecker>();
191f4a2713aSLionel Sambuc }
192f4a2713aSLionel Sambuc
193