blob: 5c98a526b7f6361f78024828ffd56e05e786342b [file] [log] [blame]
Ted Kremenek076d84e2009-07-23 01:07:19 +00001//==- 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 "clang/Analysis/PathSensitive/BugReporter.h"
15#include "clang/Analysis/LocalCheckers.h"
16#include "clang/AST/StmtVisitor.h"
17#include "llvm/Support/Compiler.h"
Ted Kremenekf08a6c52009-07-23 21:34:35 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenek076d84e2009-07-23 01:07:19 +000019
20using namespace clang;
21
22namespace {
23class VISIBILITY_HIDDEN WalkAST : public StmtVisitor<WalkAST> {
Ted Kremenekad9f89d2009-07-23 22:29:41 +000024 BugReporter &BR;
25 IdentifierInfo *II_gets;
Ted Kremenek076d84e2009-07-23 01:07:19 +000026public:
Ted Kremenekad9f89d2009-07-23 22:29:41 +000027 WalkAST(BugReporter &br) : BR(br),
28 II_gets(0) {}
Ted Kremenek076d84e2009-07-23 01:07:19 +000029
30 // Statement visitor methods.
Ted Kremenekad9f89d2009-07-23 22:29:41 +000031 void VisitCallExpr(CallExpr *CE);
Ted Kremenek076d84e2009-07-23 01:07:19 +000032 void VisitForStmt(ForStmt *S);
Ted Kremenekf08a6c52009-07-23 21:34:35 +000033 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenek076d84e2009-07-23 01:07:19 +000034
35 void VisitChildren(Stmt *S);
Ted Kremenek076d84e2009-07-23 01:07:19 +000036
Ted Kremenekad9f89d2009-07-23 22:29:41 +000037 // Helpers.
38 IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str);
39
Ted Kremenek076d84e2009-07-23 01:07:19 +000040 // Checker-specific methods.
Ted Kremenekf08a6c52009-07-23 21:34:35 +000041 void CheckLoopConditionForFloat(const ForStmt *FS);
Ted Kremenekad9f89d2009-07-23 22:29:41 +000042 void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenek076d84e2009-07-23 01:07:19 +000043};
44} // end anonymous namespace
45
46//===----------------------------------------------------------------------===//
Ted Kremenekad9f89d2009-07-23 22:29:41 +000047// Helper methods.
48//===----------------------------------------------------------------------===//
49
50IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) {
51 if (!II)
52 II = &BR.getContext().Idents.get(str);
53
54 return II;
55}
56
57//===----------------------------------------------------------------------===//
Ted Kremenek076d84e2009-07-23 01:07:19 +000058// AST walking.
59//===----------------------------------------------------------------------===//
60
61void WalkAST::VisitChildren(Stmt *S) {
62 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
63 if (Stmt *child = *I)
64 Visit(child);
65}
66
Ted Kremenekad9f89d2009-07-23 22:29:41 +000067void WalkAST::VisitCallExpr(CallExpr *CE) {
68 if (const FunctionDecl *FD = CE->getDirectCallee()) {
69 CheckCall_gets(CE, FD);
70 }
71
72 // Recurse and check children.
73 VisitChildren(CE);
74}
75
Ted Kremenekf08a6c52009-07-23 21:34:35 +000076void WalkAST::VisitForStmt(ForStmt *FS) {
77 CheckLoopConditionForFloat(FS);
Ted Kremenek076d84e2009-07-23 01:07:19 +000078
Ted Kremenekf08a6c52009-07-23 21:34:35 +000079 // Recurse and check children.
80 VisitChildren(FS);
Ted Kremenek076d84e2009-07-23 01:07:19 +000081}
82
83//===----------------------------------------------------------------------===//
Ted Kremenekf08a6c52009-07-23 21:34:35 +000084// Check: floating poing variable used as loop counter.
Ted Kremenek69576e92009-07-23 21:44:18 +000085// Originally: <rdar://problem/6336718>
86// Implements: CERT security coding advisory FLP-30.
Ted Kremenek076d84e2009-07-23 01:07:19 +000087//===----------------------------------------------------------------------===//
88
Ted Kremenekf08a6c52009-07-23 21:34:35 +000089static const DeclRefExpr*
90GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
91 expr = expr->IgnoreParenCasts();
92
93 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
94 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
95 B->getOpcode() == BinaryOperator::Comma))
96 return NULL;
97
98 if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
99 return lhs;
100
101 if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
102 return rhs;
103
104 return NULL;
Ted Kremenek076d84e2009-07-23 01:07:19 +0000105 }
Ted Kremenekf08a6c52009-07-23 21:34:35 +0000106
107 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
108 const NamedDecl *ND = DR->getDecl();
109 return ND == x || ND == y ? DR : NULL;
110 }
111
112 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
113 return U->isIncrementDecrementOp()
114 ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
115
Ted Kremenek076d84e2009-07-23 01:07:19 +0000116 return NULL;
117}
118
Ted Kremenekf08a6c52009-07-23 21:34:35 +0000119/// CheckLoopConditionForFloat - This check looks for 'for' statements that
120/// use a floating point variable as a loop counter.
121/// CERT: FLP30-C, FLP30-CPP.
122///
123void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
124 // Does the loop have a condition?
125 const Expr *condition = FS->getCond();
126
127 if (!condition)
128 return;
129
130 // Does the loop have an increment?
131 const Expr *increment = FS->getInc();
132
133 if (!increment)
134 return;
135
136 // Strip away '()' and casts.
137 condition = condition->IgnoreParenCasts();
138 increment = increment->IgnoreParenCasts();
139
140 // Is the loop condition a comparison?
141 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
142
143 if (!B)
144 return;
145
146 // The actual error condition.
147 if (!((B->isRelationalOp() || B->isEqualityOp()) &&
148 ((B->getLHS()->getType()->isFloatingType() ||
149 B->getRHS()->getType()->isFloatingType()))))
150 return;
151
152 // Are we comparing variables?
153 const DeclRefExpr *drLHS = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParens());
154 const DeclRefExpr *drRHS = dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParens());
155
156 if (!drLHS && !drRHS)
157 return;
158
159 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
160 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
161
162 if (!vdLHS && !vdRHS)
163 return;
164
165 // Does either variable appear in increment?
166 const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
167
168 if (!drInc)
169 return;
170
171 // Emit the error. First figure out which DeclRefExpr in the condition
172 // referenced the compared variable.
173 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
174
175 llvm::SmallVector<SourceRange, 2> ranges;
176 std::string sbuf;
177 llvm::raw_string_ostream os(sbuf);
178
179 os << "Variable '" << drCond->getDecl()->getNameAsCString()
180 << "' with floating point type '" << drCond->getType().getAsString()
181 << "' should not be used as a loop counter";
182
183 ranges.push_back(drCond->getSourceRange());
184 ranges.push_back(drInc->getSourceRange());
185
186 const char *bugType = "Floating point variable used as loop counter";
187 BR.EmitBasicReport(bugType, "Security", os.str().c_str(),
188 FS->getLocStart(), ranges.data(), ranges.size());
Ted Kremenek076d84e2009-07-23 01:07:19 +0000189}
190
191//===----------------------------------------------------------------------===//
Ted Kremenekad9f89d2009-07-23 22:29:41 +0000192// Check: Any use of 'gets' is insecure.
193// Originally: <rdar://problem/6335715>
194// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
195//===----------------------------------------------------------------------===//
196
197void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
198 if (FD->getIdentifier() != GetIdentifier(II_gets, "gets"))
199 return;
200
201 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType());
202 if (!FTP)
203 return;
204
205 // Verify that the function takes a single argument.
206 if (FTP->getNumArgs() != 1)
207 return;
208
209 // Is the argument a 'char*'?
210 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
211 if (!PT)
212 return;
213
214 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
215 return;
216
217 // Issue a warning.
218 SourceRange R = CE->getCallee()->getSourceRange();
219 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
220 "Security",
221 "Call to function 'gets' is extremely insecure as it can "
222 "always result in a buffer overflow",
223 CE->getLocStart(), &R, 1);
224}
225
226//===----------------------------------------------------------------------===//
Ted Kremenek076d84e2009-07-23 01:07:19 +0000227// Entry point for check.
228//===----------------------------------------------------------------------===//
229
230void clang::CheckSecuritySyntaxOnly(Decl *D, BugReporter &BR) {
231 WalkAST walker(BR);
232 walker.Visit(D->getBody());
233}