blob: 3684360835cb8ab79f055541177ec4713f100e41 [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;
Ted Kremenekc7289a12009-08-28 00:08:09 +000025 IdentifierInfo *II_gets;
26 enum { num_ids = 6 };
27 IdentifierInfo *II_setid[num_ids];
28
Ted Kremenek076d84e2009-07-23 01:07:19 +000029public:
Ted Kremenekad9f89d2009-07-23 22:29:41 +000030 WalkAST(BugReporter &br) : BR(br),
Ted Kremenekc7289a12009-08-28 00:08:09 +000031 II_gets(0), II_setid() {}
Ted Kremenek076d84e2009-07-23 01:07:19 +000032
33 // Statement visitor methods.
Ted Kremenekad9f89d2009-07-23 22:29:41 +000034 void VisitCallExpr(CallExpr *CE);
Ted Kremenek076d84e2009-07-23 01:07:19 +000035 void VisitForStmt(ForStmt *S);
Ted Kremenekc7289a12009-08-28 00:08:09 +000036 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenekf08a6c52009-07-23 21:34:35 +000037 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenek076d84e2009-07-23 01:07:19 +000038
39 void VisitChildren(Stmt *S);
Ted Kremenek076d84e2009-07-23 01:07:19 +000040
Ted Kremenekad9f89d2009-07-23 22:29:41 +000041 // Helpers.
42 IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str);
43
Ted Kremenek076d84e2009-07-23 01:07:19 +000044 // Checker-specific methods.
Ted Kremenekf08a6c52009-07-23 21:34:35 +000045 void CheckLoopConditionForFloat(const ForStmt *FS);
Ted Kremenekad9f89d2009-07-23 22:29:41 +000046 void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenekc7289a12009-08-28 00:08:09 +000047 void CheckUncheckedReturnValue(CallExpr *CE);
Ted Kremenek076d84e2009-07-23 01:07:19 +000048};
49} // end anonymous namespace
50
51//===----------------------------------------------------------------------===//
Ted Kremenekad9f89d2009-07-23 22:29:41 +000052// Helper methods.
53//===----------------------------------------------------------------------===//
54
55IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) {
56 if (!II)
57 II = &BR.getContext().Idents.get(str);
58
59 return II;
60}
61
62//===----------------------------------------------------------------------===//
Ted Kremenek076d84e2009-07-23 01:07:19 +000063// AST walking.
64//===----------------------------------------------------------------------===//
65
66void WalkAST::VisitChildren(Stmt *S) {
67 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
68 if (Stmt *child = *I)
69 Visit(child);
70}
71
Ted Kremenekad9f89d2009-07-23 22:29:41 +000072void WalkAST::VisitCallExpr(CallExpr *CE) {
73 if (const FunctionDecl *FD = CE->getDirectCallee()) {
74 CheckCall_gets(CE, FD);
75 }
76
77 // Recurse and check children.
78 VisitChildren(CE);
79}
80
Ted Kremenekc7289a12009-08-28 00:08:09 +000081void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
82 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
83 if (Stmt *child = *I)
84 {
85 if (CallExpr *CE = dyn_cast<CallExpr>(child))
86 CheckUncheckedReturnValue(CE);
87 Visit(child);
88 }
89}
90
Ted Kremenekf08a6c52009-07-23 21:34:35 +000091void WalkAST::VisitForStmt(ForStmt *FS) {
92 CheckLoopConditionForFloat(FS);
Ted Kremenek076d84e2009-07-23 01:07:19 +000093
Ted Kremenekf08a6c52009-07-23 21:34:35 +000094 // Recurse and check children.
95 VisitChildren(FS);
Ted Kremenek076d84e2009-07-23 01:07:19 +000096}
97
98//===----------------------------------------------------------------------===//
Ted Kremenekf08a6c52009-07-23 21:34:35 +000099// Check: floating poing variable used as loop counter.
Ted Kremenek69576e92009-07-23 21:44:18 +0000100// Originally: <rdar://problem/6336718>
101// Implements: CERT security coding advisory FLP-30.
Ted Kremenek076d84e2009-07-23 01:07:19 +0000102//===----------------------------------------------------------------------===//
103
Ted Kremenekf08a6c52009-07-23 21:34:35 +0000104static const DeclRefExpr*
105GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
106 expr = expr->IgnoreParenCasts();
107
108 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
109 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
110 B->getOpcode() == BinaryOperator::Comma))
111 return NULL;
112
113 if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
114 return lhs;
115
116 if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
117 return rhs;
118
119 return NULL;
Ted Kremenek076d84e2009-07-23 01:07:19 +0000120 }
Ted Kremenekf08a6c52009-07-23 21:34:35 +0000121
122 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
123 const NamedDecl *ND = DR->getDecl();
124 return ND == x || ND == y ? DR : NULL;
125 }
126
127 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
128 return U->isIncrementDecrementOp()
129 ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
130
Ted Kremenek076d84e2009-07-23 01:07:19 +0000131 return NULL;
132}
133
Ted Kremenekf08a6c52009-07-23 21:34:35 +0000134/// CheckLoopConditionForFloat - This check looks for 'for' statements that
135/// use a floating point variable as a loop counter.
136/// CERT: FLP30-C, FLP30-CPP.
137///
138void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
139 // Does the loop have a condition?
140 const Expr *condition = FS->getCond();
141
142 if (!condition)
143 return;
144
145 // Does the loop have an increment?
146 const Expr *increment = FS->getInc();
147
148 if (!increment)
149 return;
150
151 // Strip away '()' and casts.
152 condition = condition->IgnoreParenCasts();
153 increment = increment->IgnoreParenCasts();
154
155 // Is the loop condition a comparison?
156 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
157
158 if (!B)
159 return;
160
Ted Kremenek5adc4e32009-07-24 20:26:31 +0000161 // Is this a comparison?
162 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenekf08a6c52009-07-23 21:34:35 +0000163 return;
Ted Kremenek5adc4e32009-07-24 20:26:31 +0000164
Ted Kremenekf08a6c52009-07-23 21:34:35 +0000165 // Are we comparing variables?
166 const DeclRefExpr *drLHS = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParens());
167 const DeclRefExpr *drRHS = dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParens());
168
Ted Kremenek5adc4e32009-07-24 20:26:31 +0000169 // Does at least one of the variables have a floating point type?
170 drLHS = drLHS && drLHS->getType()->isFloatingType() ? drLHS : NULL;
171 drRHS = drRHS && drRHS->getType()->isFloatingType() ? drRHS : NULL;
172
Ted Kremenekf08a6c52009-07-23 21:34:35 +0000173 if (!drLHS && !drRHS)
174 return;
175
176 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
177 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
178
179 if (!vdLHS && !vdRHS)
180 return;
181
182 // Does either variable appear in increment?
183 const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
184
185 if (!drInc)
186 return;
187
188 // Emit the error. First figure out which DeclRefExpr in the condition
189 // referenced the compared variable.
190 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
191
192 llvm::SmallVector<SourceRange, 2> ranges;
193 std::string sbuf;
194 llvm::raw_string_ostream os(sbuf);
195
196 os << "Variable '" << drCond->getDecl()->getNameAsCString()
197 << "' with floating point type '" << drCond->getType().getAsString()
198 << "' should not be used as a loop counter";
199
200 ranges.push_back(drCond->getSourceRange());
201 ranges.push_back(drInc->getSourceRange());
202
203 const char *bugType = "Floating point variable used as loop counter";
204 BR.EmitBasicReport(bugType, "Security", os.str().c_str(),
205 FS->getLocStart(), ranges.data(), ranges.size());
Ted Kremenek076d84e2009-07-23 01:07:19 +0000206}
207
208//===----------------------------------------------------------------------===//
Ted Kremenekad9f89d2009-07-23 22:29:41 +0000209// Check: Any use of 'gets' is insecure.
210// Originally: <rdar://problem/6335715>
211// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
212//===----------------------------------------------------------------------===//
213
214void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
215 if (FD->getIdentifier() != GetIdentifier(II_gets, "gets"))
216 return;
217
218 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType());
219 if (!FTP)
220 return;
221
222 // Verify that the function takes a single argument.
223 if (FTP->getNumArgs() != 1)
224 return;
225
226 // Is the argument a 'char*'?
227 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
228 if (!PT)
229 return;
230
231 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
232 return;
233
234 // Issue a warning.
235 SourceRange R = CE->getCallee()->getSourceRange();
236 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
237 "Security",
238 "Call to function 'gets' is extremely insecure as it can "
239 "always result in a buffer overflow",
240 CE->getLocStart(), &R, 1);
241}
242
243//===----------------------------------------------------------------------===//
Ted Kremenekc7289a12009-08-28 00:08:09 +0000244// Check: Should check whether privileges are dropped successfully.
245// Originally: <rdar://problem/6337132>
246//===----------------------------------------------------------------------===//
247
248void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) {
249 const FunctionDecl *FD = CE->getDirectCallee();
250 if (!FD)
251 return;
252
253 if (II_setid[0] == NULL) {
254 static const char * const identifiers[num_ids] = {
255 "setuid", "setgid", "seteuid", "setegid",
256 "setreuid", "setregid"
257 };
258
259 for (size_t i = 0; i < num_ids; i++)
260 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
261 }
262
263 const IdentifierInfo *id = FD->getIdentifier();
264 size_t identifierid;
265
266 for (identifierid = 0; identifierid < num_ids; identifierid++)
267 if (id == II_setid[identifierid])
268 break;
269
270 if (identifierid >= num_ids)
271 return;
272
273 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType());
274 if (!FTP)
275 return;
276
Ted Kremenek4fa5d6c2009-08-28 00:24:55 +0000277 // Verify that the function takes one or two arguments (depending on
278 // the function).
Ted Kremenekc7289a12009-08-28 00:08:09 +0000279 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
280 return;
281
282 // The arguments must be integers.
283 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
284 if (! FTP->getArgType(i)->isIntegerType())
285 return;
286
287 // Issue a warning.
288 std::string buf1;
289 llvm::raw_string_ostream os1(buf1);
290 os1 << "Return value is not checked in call to '" << FD->getNameAsString()
291 << "'";
292
293 std::string buf2;
294 llvm::raw_string_ostream os2(buf2);
295 os2 << "The return value from the call to '" << FD->getNameAsString()
296 << "' is not checked. If an error occurs in '"
297 << FD->getNameAsString()
298 << "', the following code may execute with unexpected privileges";
299
300 SourceRange R = CE->getCallee()->getSourceRange();
301
302 BR.EmitBasicReport(os1.str().c_str(), "Security", os2.str().c_str(),
303 CE->getLocStart(), &R, 1);
304}
305
306//===----------------------------------------------------------------------===//
Ted Kremenek076d84e2009-07-23 01:07:19 +0000307// Entry point for check.
308//===----------------------------------------------------------------------===//
309
Ted Kremenek8724eba2009-08-21 23:58:43 +0000310void clang::CheckSecuritySyntaxOnly(const Decl *D, BugReporter &BR) {
Ted Kremenek076d84e2009-07-23 01:07:19 +0000311 WalkAST walker(BR);
312 walker.Visit(D->getBody());
313}