blob: f1b9c2194f8be88d3096aec13f4c08c6674bbe2d [file] [log] [blame]
Ted Kremenekdbfb5f82009-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 Kremenek8baf86d2009-07-23 21:34:35 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000019
20using namespace clang;
21
22namespace {
23class VISIBILITY_HIDDEN WalkAST : public StmtVisitor<WalkAST> {
Mike Stump1eb44332009-09-09 15:08:12 +000024 BugReporter &BR;
Ted Kremenek65a81a92009-08-28 00:08:09 +000025 IdentifierInfo *II_gets;
Zhongxing Xubd842e32009-11-09 12:19:26 +000026 IdentifierInfo *II_getpw;
Ted Kremenek24650472009-09-02 02:47:41 +000027 enum { num_rands = 9 };
28 IdentifierInfo *II_rand[num_rands];
29 IdentifierInfo *II_random;
30 enum { num_setids = 6 };
31 IdentifierInfo *II_setid[num_setids];
Mike Stump1eb44332009-09-09 15:08:12 +000032
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000033public:
Ted Kremenekefcbb152009-07-23 22:29:41 +000034 WalkAST(BugReporter &br) : BR(br),
Zhongxing Xubd842e32009-11-09 12:19:26 +000035 II_gets(0), II_getpw(0), II_rand(), II_random(0), II_setid() {}
Mike Stump1eb44332009-09-09 15:08:12 +000036
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000037 // Statement visitor methods.
Ted Kremenekefcbb152009-07-23 22:29:41 +000038 void VisitCallExpr(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000039 void VisitForStmt(ForStmt *S);
Ted Kremenek65a81a92009-08-28 00:08:09 +000040 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek8baf86d2009-07-23 21:34:35 +000041 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000042
43 void VisitChildren(Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +000044
Ted Kremenekefcbb152009-07-23 22:29:41 +000045 // Helpers.
46 IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str);
Mike Stump1eb44332009-09-09 15:08:12 +000047
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000048 // Checker-specific methods.
Ted Kremenek8baf86d2009-07-23 21:34:35 +000049 void CheckLoopConditionForFloat(const ForStmt *FS);
Ted Kremenekefcbb152009-07-23 22:29:41 +000050 void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD);
Zhongxing Xubd842e32009-11-09 12:19:26 +000051 void CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenek24650472009-09-02 02:47:41 +000052 void CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD);
53 void CheckCall_random(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenek65a81a92009-08-28 00:08:09 +000054 void CheckUncheckedReturnValue(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000055};
56} // end anonymous namespace
57
58//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +000059// Helper methods.
60//===----------------------------------------------------------------------===//
61
62IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) {
63 if (!II)
64 II = &BR.getContext().Idents.get(str);
Mike Stump1eb44332009-09-09 15:08:12 +000065
66 return II;
Ted Kremenekefcbb152009-07-23 22:29:41 +000067}
68
69//===----------------------------------------------------------------------===//
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000070// AST walking.
71//===----------------------------------------------------------------------===//
72
73void WalkAST::VisitChildren(Stmt *S) {
74 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
75 if (Stmt *child = *I)
76 Visit(child);
77}
78
Ted Kremenekefcbb152009-07-23 22:29:41 +000079void WalkAST::VisitCallExpr(CallExpr *CE) {
80 if (const FunctionDecl *FD = CE->getDirectCallee()) {
Ted Kremenek24650472009-09-02 02:47:41 +000081 CheckCall_gets(CE, FD);
Zhongxing Xubd842e32009-11-09 12:19:26 +000082 CheckCall_getpw(CE, FD);
Ted Kremenek24650472009-09-02 02:47:41 +000083 CheckCall_rand(CE, FD);
84 CheckCall_random(CE, FD);
Ted Kremenekefcbb152009-07-23 22:29:41 +000085 }
Mike Stump1eb44332009-09-09 15:08:12 +000086
Ted Kremenekefcbb152009-07-23 22:29:41 +000087 // Recurse and check children.
88 VisitChildren(CE);
89}
90
Ted Kremenek65a81a92009-08-28 00:08:09 +000091void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
92 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +000093 if (Stmt *child = *I) {
94 if (CallExpr *CE = dyn_cast<CallExpr>(child))
95 CheckUncheckedReturnValue(CE);
96 Visit(child);
97 }
Ted Kremenek65a81a92009-08-28 00:08:09 +000098}
99
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000100void WalkAST::VisitForStmt(ForStmt *FS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000101 CheckLoopConditionForFloat(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000102
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000103 // Recurse and check children.
104 VisitChildren(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000105}
106
107//===----------------------------------------------------------------------===//
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000108// Check: floating poing variable used as loop counter.
Ted Kremenek5abeb522009-07-23 21:44:18 +0000109// Originally: <rdar://problem/6336718>
110// Implements: CERT security coding advisory FLP-30.
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000111//===----------------------------------------------------------------------===//
112
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000113static const DeclRefExpr*
114GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
115 expr = expr->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000116
117 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000118 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
119 B->getOpcode() == BinaryOperator::Comma))
120 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000122 if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
123 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000125 if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
126 return rhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000128 return NULL;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000129 }
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000131 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
132 const NamedDecl *ND = DR->getDecl();
133 return ND == x || ND == y ? DR : NULL;
134 }
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000136 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
137 return U->isIncrementDecrementOp()
138 ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
139
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000140 return NULL;
141}
142
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000143/// CheckLoopConditionForFloat - This check looks for 'for' statements that
144/// use a floating point variable as a loop counter.
145/// CERT: FLP30-C, FLP30-CPP.
146///
147void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
148 // Does the loop have a condition?
149 const Expr *condition = FS->getCond();
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000151 if (!condition)
152 return;
153
154 // Does the loop have an increment?
155 const Expr *increment = FS->getInc();
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000157 if (!increment)
158 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000160 // Strip away '()' and casts.
161 condition = condition->IgnoreParenCasts();
162 increment = increment->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000164 // Is the loop condition a comparison?
165 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
166
167 if (!B)
168 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Ted Kremenekcad9f412009-07-24 20:26:31 +0000170 // Is this a comparison?
171 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000172 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000174 // Are we comparing variables?
175 const DeclRefExpr *drLHS = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParens());
176 const DeclRefExpr *drRHS = dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParens());
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Ted Kremenekcad9f412009-07-24 20:26:31 +0000178 // Does at least one of the variables have a floating point type?
179 drLHS = drLHS && drLHS->getType()->isFloatingType() ? drLHS : NULL;
180 drRHS = drRHS && drRHS->getType()->isFloatingType() ? drRHS : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000182 if (!drLHS && !drRHS)
183 return;
184
185 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
186 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000188 if (!vdLHS && !vdRHS)
Mike Stump1eb44332009-09-09 15:08:12 +0000189 return;
190
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000191 // Does either variable appear in increment?
192 const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000194 if (!drInc)
195 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000197 // Emit the error. First figure out which DeclRefExpr in the condition
198 // referenced the compared variable.
199 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
200
Mike Stump1eb44332009-09-09 15:08:12 +0000201 llvm::SmallVector<SourceRange, 2> ranges;
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000202 std::string sbuf;
203 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000205 os << "Variable '" << drCond->getDecl()->getNameAsCString()
206 << "' with floating point type '" << drCond->getType().getAsString()
207 << "' should not be used as a loop counter";
208
209 ranges.push_back(drCond->getSourceRange());
210 ranges.push_back(drInc->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000212 const char *bugType = "Floating point variable used as loop counter";
213 BR.EmitBasicReport(bugType, "Security", os.str().c_str(),
214 FS->getLocStart(), ranges.data(), ranges.size());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000215}
216
217//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +0000218// Check: Any use of 'gets' is insecure.
219// Originally: <rdar://problem/6335715>
220// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuaa30b3b2009-11-09 08:13:04 +0000221// CWE-242: Use of Inherently Dangerous Function
Ted Kremenekefcbb152009-07-23 22:29:41 +0000222//===----------------------------------------------------------------------===//
223
224void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
225 if (FD->getIdentifier() != GetIdentifier(II_gets, "gets"))
226 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Zhongxing Xubd842e32009-11-09 12:19:26 +0000228 const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FD->getType());
229 if (!FPT)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000230 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Ted Kremenekefcbb152009-07-23 22:29:41 +0000232 // Verify that the function takes a single argument.
Zhongxing Xubd842e32009-11-09 12:19:26 +0000233 if (FPT->getNumArgs() != 1)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000234 return;
235
236 // Is the argument a 'char*'?
Zhongxing Xubd842e32009-11-09 12:19:26 +0000237 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenekefcbb152009-07-23 22:29:41 +0000238 if (!PT)
239 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremenekefcbb152009-07-23 22:29:41 +0000241 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
242 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Ted Kremenekefcbb152009-07-23 22:29:41 +0000244 // Issue a warning.
245 SourceRange R = CE->getCallee()->getSourceRange();
246 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
247 "Security",
248 "Call to function 'gets' is extremely insecure as it can "
249 "always result in a buffer overflow",
250 CE->getLocStart(), &R, 1);
251}
252
253//===----------------------------------------------------------------------===//
Zhongxing Xubd842e32009-11-09 12:19:26 +0000254// Check: Any use of 'getpwd' is insecure.
255// CWE-477: Use of Obsolete Functions
256//===----------------------------------------------------------------------===//
257
258void WalkAST::CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
259 if (FD->getIdentifier() != GetIdentifier(II_getpw, "getpw"))
260 return;
261
262 const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FD->getType());
263 if (!FPT)
264 return;
265
266 // Verify that the function takes two arguments.
267 if (FPT->getNumArgs() != 2)
268 return;
269
270 // Verify the first argument type is integer.
271 if (!FPT->getArgType(0)->isIntegerType())
272 return;
273
274 // Verify the second argument type is char*.
275 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
276 if (!PT)
277 return;
278
279 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
280 return;
281
282 // Issue a warning.
283 SourceRange R = CE->getCallee()->getSourceRange();
284 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
285 "Security",
286 "The getpw() function is dangerous as it may overflow the "
287 "provided buffer. It is obsoleted by getpwuid().",
288 CE->getLocStart(), &R, 1);
289}
290
291//===----------------------------------------------------------------------===//
Ted Kremenek24650472009-09-02 02:47:41 +0000292// Check: Linear congruent random number generators should not be used
293// Originally: <rdar://problem/63371000>
294// CWE-338: Use of cryptographically weak prng
295//===----------------------------------------------------------------------===//
296
297void WalkAST::CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
298 if (II_rand[0] == NULL) {
299 // This check applies to these functions
300 static const char * const identifiers[num_rands] = {
301 "drand48", "erand48", "jrand48", "lrand48", "mrand48", "nrand48",
302 "lcong48",
303 "rand", "rand_r"
304 };
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Ted Kremenek24650472009-09-02 02:47:41 +0000306 for (size_t i = 0; i < num_rands; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000307 II_rand[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek24650472009-09-02 02:47:41 +0000308 }
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Ted Kremenek24650472009-09-02 02:47:41 +0000310 const IdentifierInfo *id = FD->getIdentifier();
311 size_t identifierid;
312
313 for (identifierid = 0; identifierid < num_rands; identifierid++)
314 if (id == II_rand[identifierid])
315 break;
316
317 if (identifierid >= num_rands)
318 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Ted Kremenek24650472009-09-02 02:47:41 +0000320 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType());
321 if (!FTP)
322 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Ted Kremenek24650472009-09-02 02:47:41 +0000324 if (FTP->getNumArgs() == 1) {
325 // Is the argument an 'unsigned short *'?
326 // (Actually any integer type is allowed.)
327 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
328 if (!PT)
329 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Ted Kremenek24650472009-09-02 02:47:41 +0000331 if (! PT->getPointeeType()->isIntegerType())
332 return;
333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334 else if (FTP->getNumArgs() != 0)
Ted Kremenek24650472009-09-02 02:47:41 +0000335 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Ted Kremenek24650472009-09-02 02:47:41 +0000337 // Issue a warning.
338 std::string buf1;
339 llvm::raw_string_ostream os1(buf1);
340 os1 << "'" << FD->getNameAsString() << "' is a poor random number generator";
341
342 std::string buf2;
343 llvm::raw_string_ostream os2(buf2);
344 os2 << "Function '" << FD->getNameAsString()
345 << "' is obsolete because it implements a poor random number generator."
346 << " Use 'arc4random' instead";
347
348 SourceRange R = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremenek24650472009-09-02 02:47:41 +0000350 BR.EmitBasicReport(os1.str().c_str(), "Security", os2.str().c_str(),
351 CE->getLocStart(), &R, 1);
352}
353
354//===----------------------------------------------------------------------===//
355// Check: 'random' should not be used
356// Originally: <rdar://problem/63371000>
357//===----------------------------------------------------------------------===//
358
359void WalkAST::CheckCall_random(const CallExpr *CE, const FunctionDecl *FD) {
360 if (FD->getIdentifier() != GetIdentifier(II_random, "random"))
361 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek24650472009-09-02 02:47:41 +0000363 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType());
364 if (!FTP)
365 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek24650472009-09-02 02:47:41 +0000367 // Verify that the function takes no argument.
368 if (FTP->getNumArgs() != 0)
369 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Ted Kremenek24650472009-09-02 02:47:41 +0000371 // Issue a warning.
372 SourceRange R = CE->getCallee()->getSourceRange();
373 BR.EmitBasicReport("'random' is not a secure random number generator",
374 "Security",
375 "The 'random' function produces a sequence of values that "
376 "an adversary may be able to predict. Use 'arc4random' "
377 "instead",
378 CE->getLocStart(), &R, 1);
379}
380
381//===----------------------------------------------------------------------===//
Ted Kremenek65a81a92009-08-28 00:08:09 +0000382// Check: Should check whether privileges are dropped successfully.
383// Originally: <rdar://problem/6337132>
384//===----------------------------------------------------------------------===//
385
386void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) {
387 const FunctionDecl *FD = CE->getDirectCallee();
388 if (!FD)
389 return;
390
391 if (II_setid[0] == NULL) {
Ted Kremenek24650472009-09-02 02:47:41 +0000392 static const char * const identifiers[num_setids] = {
Ted Kremenek65a81a92009-08-28 00:08:09 +0000393 "setuid", "setgid", "seteuid", "setegid",
394 "setreuid", "setregid"
395 };
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek24650472009-09-02 02:47:41 +0000397 for (size_t i = 0; i < num_setids; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000398 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000399 }
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Ted Kremenek65a81a92009-08-28 00:08:09 +0000401 const IdentifierInfo *id = FD->getIdentifier();
402 size_t identifierid;
403
Ted Kremenek24650472009-09-02 02:47:41 +0000404 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000405 if (id == II_setid[identifierid])
406 break;
407
Ted Kremenek24650472009-09-02 02:47:41 +0000408 if (identifierid >= num_setids)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000409 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Ted Kremenek65a81a92009-08-28 00:08:09 +0000411 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType());
412 if (!FTP)
413 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Ted Kremeneka8187832009-08-28 00:24:55 +0000415 // Verify that the function takes one or two arguments (depending on
416 // the function).
Ted Kremenek65a81a92009-08-28 00:08:09 +0000417 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
418 return;
419
420 // The arguments must be integers.
421 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
422 if (! FTP->getArgType(i)->isIntegerType())
423 return;
424
425 // Issue a warning.
426 std::string buf1;
427 llvm::raw_string_ostream os1(buf1);
428 os1 << "Return value is not checked in call to '" << FD->getNameAsString()
429 << "'";
430
431 std::string buf2;
432 llvm::raw_string_ostream os2(buf2);
433 os2 << "The return value from the call to '" << FD->getNameAsString()
434 << "' is not checked. If an error occurs in '"
435 << FD->getNameAsString()
436 << "', the following code may execute with unexpected privileges";
437
438 SourceRange R = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Ted Kremenek65a81a92009-08-28 00:08:09 +0000440 BR.EmitBasicReport(os1.str().c_str(), "Security", os2.str().c_str(),
441 CE->getLocStart(), &R, 1);
442}
443
444//===----------------------------------------------------------------------===//
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000445// Entry point for check.
446//===----------------------------------------------------------------------===//
447
Mike Stump1eb44332009-09-09 15:08:12 +0000448void clang::CheckSecuritySyntaxOnly(const Decl *D, BugReporter &BR) {
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000449 WalkAST walker(BR);
Mike Stump1eb44332009-09-09 15:08:12 +0000450 walker.Visit(D->getBody());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000451}