blob: 61d3e4c652bbeb7827e8f0d8484a346b257b8fe5 [file] [log] [blame]
Ted Kremenekc5b4c0e2009-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
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000014#include "ClangSACheckers.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000015#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000016#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000017#include "clang/Basic/TargetInfo.h"
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek9c497622009-07-23 21:34:35 +000019#include "llvm/Support/raw_ostream.h"
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000020
21using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000022using namespace ento;
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000023
Ted Kremenekabf6ba12010-01-15 08:20:31 +000024static bool isArc4RandomAvailable(const ASTContext &Ctx) {
25 const llvm::Triple &T = Ctx.Target.getTriple();
26 return T.getVendor() == llvm::Triple::Apple ||
Douglas Gregor45e84b02011-01-17 19:16:24 +000027 T.getOS() == llvm::Triple::FreeBSD ||
28 T.getOS() == llvm::Triple::NetBSD ||
29 T.getOS() == llvm::Triple::OpenBSD ||
30 T.getOS() == llvm::Triple::DragonFly;
Ted Kremenekabf6ba12010-01-15 08:20:31 +000031}
32
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000033namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000034class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump11289f42009-09-09 15:08:12 +000035 BugReporter &BR;
Ted Kremenekd032fcc2009-08-28 00:08:09 +000036 IdentifierInfo *II_gets;
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +000037 IdentifierInfo *II_getpw;
Zhongxing Xu39bba622009-12-03 09:15:23 +000038 IdentifierInfo *II_mktemp;
Ted Kremenekad5a6002009-09-02 02:47:41 +000039 enum { num_rands = 9 };
40 IdentifierInfo *II_rand[num_rands];
41 IdentifierInfo *II_random;
42 enum { num_setids = 6 };
43 IdentifierInfo *II_setid[num_setids];
Ted Kremenek8edc6df2010-03-24 22:39:47 +000044
Ted Kremenekabf6ba12010-01-15 08:20:31 +000045 const bool CheckRand;
Mike Stump11289f42009-09-09 15:08:12 +000046
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000047public:
Ted Kremenek6610c032009-07-23 22:29:41 +000048 WalkAST(BugReporter &br) : BR(br),
Eli Friedman04831922010-08-22 01:00:03 +000049 II_gets(0), II_getpw(0), II_mktemp(0),
50 II_rand(), II_random(0), II_setid(),
Ted Kremenekabf6ba12010-01-15 08:20:31 +000051 CheckRand(isArc4RandomAvailable(BR.getContext())) {}
Mike Stump11289f42009-09-09 15:08:12 +000052
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000053 // Statement visitor methods.
Ted Kremenek6610c032009-07-23 22:29:41 +000054 void VisitCallExpr(CallExpr *CE);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000055 void VisitForStmt(ForStmt *S);
Ted Kremenekd032fcc2009-08-28 00:08:09 +000056 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek9c497622009-07-23 21:34:35 +000057 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000058
59 void VisitChildren(Stmt *S);
Mike Stump11289f42009-09-09 15:08:12 +000060
Ted Kremenek6610c032009-07-23 22:29:41 +000061 // Helpers.
62 IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str);
Mike Stump11289f42009-09-09 15:08:12 +000063
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000064 // Checker-specific methods.
Ted Kremenek9c497622009-07-23 21:34:35 +000065 void CheckLoopConditionForFloat(const ForStmt *FS);
Ted Kremenek6610c032009-07-23 22:29:41 +000066 void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD);
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +000067 void CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
Zhongxing Xu39bba622009-12-03 09:15:23 +000068 void CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani6ffe7382011-03-31 22:09:14 +000069 void CheckCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenekad5a6002009-09-02 02:47:41 +000070 void CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD);
71 void CheckCall_random(const CallExpr *CE, const FunctionDecl *FD);
Ted Kremenekd032fcc2009-08-28 00:08:09 +000072 void CheckUncheckedReturnValue(CallExpr *CE);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000073};
74} // end anonymous namespace
75
76//===----------------------------------------------------------------------===//
Ted Kremenek6610c032009-07-23 22:29:41 +000077// Helper methods.
78//===----------------------------------------------------------------------===//
79
80IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) {
81 if (!II)
82 II = &BR.getContext().Idents.get(str);
Mike Stump11289f42009-09-09 15:08:12 +000083
84 return II;
Ted Kremenek6610c032009-07-23 22:29:41 +000085}
86
87//===----------------------------------------------------------------------===//
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +000088// AST walking.
89//===----------------------------------------------------------------------===//
90
91void WalkAST::VisitChildren(Stmt *S) {
92 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
93 if (Stmt *child = *I)
94 Visit(child);
95}
96
Ted Kremenek6610c032009-07-23 22:29:41 +000097void WalkAST::VisitCallExpr(CallExpr *CE) {
98 if (const FunctionDecl *FD = CE->getDirectCallee()) {
Ted Kremenekad5a6002009-09-02 02:47:41 +000099 CheckCall_gets(CE, FD);
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000100 CheckCall_getpw(CE, FD);
Zhongxing Xu39bba622009-12-03 09:15:23 +0000101 CheckCall_mktemp(CE, FD);
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000102 CheckCall_strcpy(CE, FD);
Ted Kremenekabf6ba12010-01-15 08:20:31 +0000103 if (CheckRand) {
104 CheckCall_rand(CE, FD);
105 CheckCall_random(CE, FD);
106 }
Ted Kremenek6610c032009-07-23 22:29:41 +0000107 }
Mike Stump11289f42009-09-09 15:08:12 +0000108
Ted Kremenek6610c032009-07-23 22:29:41 +0000109 // Recurse and check children.
110 VisitChildren(CE);
111}
112
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000113void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
114 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000115 if (Stmt *child = *I) {
116 if (CallExpr *CE = dyn_cast<CallExpr>(child))
117 CheckUncheckedReturnValue(CE);
118 Visit(child);
119 }
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000120}
121
Ted Kremenek9c497622009-07-23 21:34:35 +0000122void WalkAST::VisitForStmt(ForStmt *FS) {
Mike Stump11289f42009-09-09 15:08:12 +0000123 CheckLoopConditionForFloat(FS);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000124
Ted Kremenek9c497622009-07-23 21:34:35 +0000125 // Recurse and check children.
126 VisitChildren(FS);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000127}
128
129//===----------------------------------------------------------------------===//
Ted Kremenek9c497622009-07-23 21:34:35 +0000130// Check: floating poing variable used as loop counter.
Ted Kremenek70e55262009-07-23 21:44:18 +0000131// Originally: <rdar://problem/6336718>
132// Implements: CERT security coding advisory FLP-30.
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000133//===----------------------------------------------------------------------===//
134
Ted Kremenek9c497622009-07-23 21:34:35 +0000135static const DeclRefExpr*
136GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
137 expr = expr->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000138
139 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek9c497622009-07-23 21:34:35 +0000140 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCalle3027922010-08-25 11:45:40 +0000141 B->getOpcode() == BO_Comma))
Ted Kremenek9c497622009-07-23 21:34:35 +0000142 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000143
Ted Kremenek9c497622009-07-23 21:34:35 +0000144 if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
145 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +0000146
Ted Kremenek9c497622009-07-23 21:34:35 +0000147 if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
148 return rhs;
Mike Stump11289f42009-09-09 15:08:12 +0000149
Ted Kremenek9c497622009-07-23 21:34:35 +0000150 return NULL;
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000151 }
Mike Stump11289f42009-09-09 15:08:12 +0000152
Ted Kremenek9c497622009-07-23 21:34:35 +0000153 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
154 const NamedDecl *ND = DR->getDecl();
155 return ND == x || ND == y ? DR : NULL;
156 }
Mike Stump11289f42009-09-09 15:08:12 +0000157
Ted Kremenek9c497622009-07-23 21:34:35 +0000158 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
159 return U->isIncrementDecrementOp()
160 ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
161
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000162 return NULL;
163}
164
Ted Kremenek9c497622009-07-23 21:34:35 +0000165/// CheckLoopConditionForFloat - This check looks for 'for' statements that
166/// use a floating point variable as a loop counter.
167/// CERT: FLP30-C, FLP30-CPP.
168///
169void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
170 // Does the loop have a condition?
171 const Expr *condition = FS->getCond();
Mike Stump11289f42009-09-09 15:08:12 +0000172
Ted Kremenek9c497622009-07-23 21:34:35 +0000173 if (!condition)
174 return;
175
176 // Does the loop have an increment?
177 const Expr *increment = FS->getInc();
Mike Stump11289f42009-09-09 15:08:12 +0000178
Ted Kremenek9c497622009-07-23 21:34:35 +0000179 if (!increment)
180 return;
Mike Stump11289f42009-09-09 15:08:12 +0000181
Ted Kremenek9c497622009-07-23 21:34:35 +0000182 // Strip away '()' and casts.
183 condition = condition->IgnoreParenCasts();
184 increment = increment->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000185
Ted Kremenek9c497622009-07-23 21:34:35 +0000186 // Is the loop condition a comparison?
187 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
188
189 if (!B)
190 return;
Mike Stump11289f42009-09-09 15:08:12 +0000191
Ted Kremenekb9cb1132009-07-24 20:26:31 +0000192 // Is this a comparison?
193 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek9c497622009-07-23 21:34:35 +0000194 return;
Mike Stump11289f42009-09-09 15:08:12 +0000195
Ted Kremenek9c497622009-07-23 21:34:35 +0000196 // Are we comparing variables?
John McCall34376a62010-12-04 03:47:34 +0000197 const DeclRefExpr *drLHS =
198 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
199 const DeclRefExpr *drRHS =
200 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump11289f42009-09-09 15:08:12 +0000201
Ted Kremenekb9cb1132009-07-24 20:26:31 +0000202 // Does at least one of the variables have a floating point type?
Douglas Gregor49b4d732010-06-22 23:07:26 +0000203 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
204 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000205
Ted Kremenek9c497622009-07-23 21:34:35 +0000206 if (!drLHS && !drRHS)
207 return;
208
209 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
210 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000211
Ted Kremenek9c497622009-07-23 21:34:35 +0000212 if (!vdLHS && !vdRHS)
Mike Stump11289f42009-09-09 15:08:12 +0000213 return;
214
Ted Kremenek9c497622009-07-23 21:34:35 +0000215 // Does either variable appear in increment?
216 const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump11289f42009-09-09 15:08:12 +0000217
Ted Kremenek9c497622009-07-23 21:34:35 +0000218 if (!drInc)
219 return;
Mike Stump11289f42009-09-09 15:08:12 +0000220
Ted Kremenek9c497622009-07-23 21:34:35 +0000221 // Emit the error. First figure out which DeclRefExpr in the condition
222 // referenced the compared variable.
223 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
224
Mike Stump11289f42009-09-09 15:08:12 +0000225 llvm::SmallVector<SourceRange, 2> ranges;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000226 llvm::SmallString<256> sbuf;
227 llvm::raw_svector_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +0000228
Daniel Dunbar56df9772010-08-17 22:39:59 +0000229 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek9c497622009-07-23 21:34:35 +0000230 << "' with floating point type '" << drCond->getType().getAsString()
231 << "' should not be used as a loop counter";
232
233 ranges.push_back(drCond->getSourceRange());
234 ranges.push_back(drInc->getSourceRange());
Mike Stump11289f42009-09-09 15:08:12 +0000235
Ted Kremenek9c497622009-07-23 21:34:35 +0000236 const char *bugType = "Floating point variable used as loop counter";
Benjamin Kramer63415532009-11-29 18:27:55 +0000237 BR.EmitBasicReport(bugType, "Security", os.str(),
Ted Kremenek9c497622009-07-23 21:34:35 +0000238 FS->getLocStart(), ranges.data(), ranges.size());
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000239}
240
241//===----------------------------------------------------------------------===//
Ted Kremenek6610c032009-07-23 22:29:41 +0000242// Check: Any use of 'gets' is insecure.
243// Originally: <rdar://problem/6335715>
244// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuf69973c2009-11-09 08:13:04 +0000245// CWE-242: Use of Inherently Dangerous Function
Ted Kremenek6610c032009-07-23 22:29:41 +0000246//===----------------------------------------------------------------------===//
247
248void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
249 if (FD->getIdentifier() != GetIdentifier(II_gets, "gets"))
250 return;
Mike Stump11289f42009-09-09 15:08:12 +0000251
Abramo Bagnara6d810632010-12-14 22:11:44 +0000252 const FunctionProtoType *FPT
253 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000254 if (!FPT)
Ted Kremenek6610c032009-07-23 22:29:41 +0000255 return;
Mike Stump11289f42009-09-09 15:08:12 +0000256
Ted Kremenek6610c032009-07-23 22:29:41 +0000257 // Verify that the function takes a single argument.
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000258 if (FPT->getNumArgs() != 1)
Ted Kremenek6610c032009-07-23 22:29:41 +0000259 return;
260
261 // Is the argument a 'char*'?
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000262 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenek6610c032009-07-23 22:29:41 +0000263 if (!PT)
264 return;
Mike Stump11289f42009-09-09 15:08:12 +0000265
Ted Kremenek6610c032009-07-23 22:29:41 +0000266 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
267 return;
Mike Stump11289f42009-09-09 15:08:12 +0000268
Ted Kremenek6610c032009-07-23 22:29:41 +0000269 // Issue a warning.
270 SourceRange R = CE->getCallee()->getSourceRange();
271 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
272 "Security",
273 "Call to function 'gets' is extremely insecure as it can "
274 "always result in a buffer overflow",
275 CE->getLocStart(), &R, 1);
276}
277
278//===----------------------------------------------------------------------===//
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000279// Check: Any use of 'getpwd' is insecure.
280// CWE-477: Use of Obsolete Functions
281//===----------------------------------------------------------------------===//
282
283void WalkAST::CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
284 if (FD->getIdentifier() != GetIdentifier(II_getpw, "getpw"))
285 return;
286
Abramo Bagnara6d810632010-12-14 22:11:44 +0000287 const FunctionProtoType *FPT
288 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xud6e7f9d2009-11-09 12:19:26 +0000289 if (!FPT)
290 return;
291
292 // Verify that the function takes two arguments.
293 if (FPT->getNumArgs() != 2)
294 return;
295
296 // Verify the first argument type is integer.
297 if (!FPT->getArgType(0)->isIntegerType())
298 return;
299
300 // Verify the second argument type is char*.
301 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
302 if (!PT)
303 return;
304
305 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
306 return;
307
308 // Issue a warning.
309 SourceRange R = CE->getCallee()->getSourceRange();
310 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
311 "Security",
312 "The getpw() function is dangerous as it may overflow the "
313 "provided buffer. It is obsoleted by getpwuid().",
314 CE->getLocStart(), &R, 1);
315}
316
317//===----------------------------------------------------------------------===//
Zhongxing Xu39bba622009-12-03 09:15:23 +0000318// Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
319// CWE-377: Insecure Temporary File
320//===----------------------------------------------------------------------===//
321
322void WalkAST::CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
323 if (FD->getIdentifier() != GetIdentifier(II_mktemp, "mktemp"))
324 return;
325
Abramo Bagnara6d810632010-12-14 22:11:44 +0000326 const FunctionProtoType *FPT
327 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xu39bba622009-12-03 09:15:23 +0000328 if(!FPT)
329 return;
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000330
Lenny Maiorani70568c22011-03-31 21:26:55 +0000331 // Verify that the function takes a single argument.
Zhongxing Xu39bba622009-12-03 09:15:23 +0000332 if (FPT->getNumArgs() != 1)
333 return;
334
335 // Verify that the argument is Pointer Type.
336 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
337 if (!PT)
338 return;
339
340 // Verify that the argument is a 'char*'.
341 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
342 return;
Ted Kremenekaeaf3d22010-03-24 22:39:45 +0000343
Zhongxing Xu39bba622009-12-03 09:15:23 +0000344 // Issue a waring.
345 SourceRange R = CE->getCallee()->getSourceRange();
346 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
Eli Friedman04831922010-08-22 01:00:03 +0000347 "Security",
348 "Call to function 'mktemp' is insecure as it always "
349 "creates or uses insecure temporary file. Use 'mkstemp' instead",
350 CE->getLocStart(), &R, 1);
Zhongxing Xu39bba622009-12-03 09:15:23 +0000351}
352
Zhongxing Xu39bba622009-12-03 09:15:23 +0000353//===----------------------------------------------------------------------===//
Lenny Maiorani6ffe7382011-03-31 22:09:14 +0000354// Check: Any use of 'strcpy' is insecure.
355//
356// CWE-119: Improper Restriction of Operations within
357// the Bounds of a Memory Buffer
358//===----------------------------------------------------------------------===//
359void WalkAST::CheckCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
360 IdentifierInfo *II = FD->getIdentifier();
361 if (!II) // if no identifier, not a simple C function
362 return;
363 llvm::StringRef Name = II->getName();
364 if (Name.startswith("__builtin_"))
365 Name = Name.substr(10);
366
367 if ((Name != "strcpy") &&
368 (Name != "__strcpy_chk"))
369 return;
370
371 const FunctionProtoType *FPT
372 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
373 if (!FPT)
374 return;
375
376 // Verify the function takes two arguments
377 int numArgs = FPT->getNumArgs();
378 if (numArgs != 2 && numArgs != 3)
379 return;
380
381 // Verify the type for both arguments
382 for (int i = 0; i < 2; i++) {
383 // Verify that the arguments are pointers
384 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
385 if (!PT)
386 return;
387
388 // Verify that the argument is a 'char*'.
389 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
390 return;
391 }
392
393 // Issue a warning
394 SourceRange R = CE->getCallee()->getSourceRange();
395 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
396 "call 'strcpy'",
397 "Security",
398 "Call to function 'strcpy' is insecure as it does not "
399 "provide bounding of the memory buffer. Replace "
400 "unbounded copy functions with analogous functions that "
401 "support length arguments such as 'strncpy'. CWE-119.",
402 CE->getLocStart(), &R, 1);
403}
404
405//===----------------------------------------------------------------------===//
Ted Kremenekad5a6002009-09-02 02:47:41 +0000406// Check: Linear congruent random number generators should not be used
407// Originally: <rdar://problem/63371000>
408// CWE-338: Use of cryptographically weak prng
409//===----------------------------------------------------------------------===//
410
411void WalkAST::CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
412 if (II_rand[0] == NULL) {
413 // This check applies to these functions
414 static const char * const identifiers[num_rands] = {
415 "drand48", "erand48", "jrand48", "lrand48", "mrand48", "nrand48",
416 "lcong48",
417 "rand", "rand_r"
418 };
Mike Stump11289f42009-09-09 15:08:12 +0000419
Ted Kremenekad5a6002009-09-02 02:47:41 +0000420 for (size_t i = 0; i < num_rands; i++)
Mike Stump11289f42009-09-09 15:08:12 +0000421 II_rand[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenekad5a6002009-09-02 02:47:41 +0000422 }
Mike Stump11289f42009-09-09 15:08:12 +0000423
Ted Kremenekad5a6002009-09-02 02:47:41 +0000424 const IdentifierInfo *id = FD->getIdentifier();
425 size_t identifierid;
426
427 for (identifierid = 0; identifierid < num_rands; identifierid++)
428 if (id == II_rand[identifierid])
429 break;
430
431 if (identifierid >= num_rands)
432 return;
Mike Stump11289f42009-09-09 15:08:12 +0000433
Abramo Bagnara6d810632010-12-14 22:11:44 +0000434 const FunctionProtoType *FTP
435 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000436 if (!FTP)
437 return;
Mike Stump11289f42009-09-09 15:08:12 +0000438
Ted Kremenekad5a6002009-09-02 02:47:41 +0000439 if (FTP->getNumArgs() == 1) {
440 // Is the argument an 'unsigned short *'?
441 // (Actually any integer type is allowed.)
442 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
443 if (!PT)
444 return;
Mike Stump11289f42009-09-09 15:08:12 +0000445
Ted Kremenekad5a6002009-09-02 02:47:41 +0000446 if (! PT->getPointeeType()->isIntegerType())
447 return;
448 }
Mike Stump11289f42009-09-09 15:08:12 +0000449 else if (FTP->getNumArgs() != 0)
Ted Kremenekad5a6002009-09-02 02:47:41 +0000450 return;
Mike Stump11289f42009-09-09 15:08:12 +0000451
Ted Kremenekad5a6002009-09-02 02:47:41 +0000452 // Issue a warning.
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000453 llvm::SmallString<256> buf1;
454 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000455 os1 << '\'' << FD << "' is a poor random number generator";
Ted Kremenekad5a6002009-09-02 02:47:41 +0000456
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000457 llvm::SmallString<256> buf2;
458 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000459 os2 << "Function '" << FD
Ted Kremenekad5a6002009-09-02 02:47:41 +0000460 << "' is obsolete because it implements a poor random number generator."
461 << " Use 'arc4random' instead";
462
463 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000464 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenekad5a6002009-09-02 02:47:41 +0000465}
466
467//===----------------------------------------------------------------------===//
468// Check: 'random' should not be used
469// Originally: <rdar://problem/63371000>
470//===----------------------------------------------------------------------===//
471
472void WalkAST::CheckCall_random(const CallExpr *CE, const FunctionDecl *FD) {
473 if (FD->getIdentifier() != GetIdentifier(II_random, "random"))
474 return;
Mike Stump11289f42009-09-09 15:08:12 +0000475
Abramo Bagnara6d810632010-12-14 22:11:44 +0000476 const FunctionProtoType *FTP
477 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenekad5a6002009-09-02 02:47:41 +0000478 if (!FTP)
479 return;
Mike Stump11289f42009-09-09 15:08:12 +0000480
Ted Kremenekad5a6002009-09-02 02:47:41 +0000481 // Verify that the function takes no argument.
482 if (FTP->getNumArgs() != 0)
483 return;
Mike Stump11289f42009-09-09 15:08:12 +0000484
Ted Kremenekad5a6002009-09-02 02:47:41 +0000485 // Issue a warning.
486 SourceRange R = CE->getCallee()->getSourceRange();
487 BR.EmitBasicReport("'random' is not a secure random number generator",
488 "Security",
489 "The 'random' function produces a sequence of values that "
490 "an adversary may be able to predict. Use 'arc4random' "
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000491 "instead", CE->getLocStart(), &R, 1);
Ted Kremenekad5a6002009-09-02 02:47:41 +0000492}
493
494//===----------------------------------------------------------------------===//
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000495// Check: Should check whether privileges are dropped successfully.
496// Originally: <rdar://problem/6337132>
497//===----------------------------------------------------------------------===//
498
499void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) {
500 const FunctionDecl *FD = CE->getDirectCallee();
501 if (!FD)
502 return;
503
504 if (II_setid[0] == NULL) {
Ted Kremenekad5a6002009-09-02 02:47:41 +0000505 static const char * const identifiers[num_setids] = {
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000506 "setuid", "setgid", "seteuid", "setegid",
507 "setreuid", "setregid"
508 };
Mike Stump11289f42009-09-09 15:08:12 +0000509
Ted Kremenekad5a6002009-09-02 02:47:41 +0000510 for (size_t i = 0; i < num_setids; i++)
Mike Stump11289f42009-09-09 15:08:12 +0000511 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000512 }
Mike Stump11289f42009-09-09 15:08:12 +0000513
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000514 const IdentifierInfo *id = FD->getIdentifier();
515 size_t identifierid;
516
Ted Kremenekad5a6002009-09-02 02:47:41 +0000517 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000518 if (id == II_setid[identifierid])
519 break;
520
Ted Kremenekad5a6002009-09-02 02:47:41 +0000521 if (identifierid >= num_setids)
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000522 return;
Mike Stump11289f42009-09-09 15:08:12 +0000523
Abramo Bagnara6d810632010-12-14 22:11:44 +0000524 const FunctionProtoType *FTP
525 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000526 if (!FTP)
527 return;
Mike Stump11289f42009-09-09 15:08:12 +0000528
Ted Kremenekaeca0952009-08-28 00:24:55 +0000529 // Verify that the function takes one or two arguments (depending on
530 // the function).
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000531 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
532 return;
533
534 // The arguments must be integers.
535 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
536 if (! FTP->getArgType(i)->isIntegerType())
537 return;
538
539 // Issue a warning.
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000540 llvm::SmallString<256> buf1;
541 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000542 os1 << "Return value is not checked in call to '" << FD << '\'';
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000543
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000544 llvm::SmallString<256> buf2;
545 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000546 os2 << "The return value from the call to '" << FD
547 << "' is not checked. If an error occurs in '" << FD
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000548 << "', the following code may execute with unexpected privileges";
549
550 SourceRange R = CE->getCallee()->getSourceRange();
Ted Kremenek8edc6df2010-03-24 22:39:47 +0000551 BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
Ted Kremenekd032fcc2009-08-28 00:08:09 +0000552}
553
554//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000555// SecuritySyntaxChecker
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000556//===----------------------------------------------------------------------===//
557
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000558namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000559class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000560public:
561 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
562 BugReporter &BR) const {
563 WalkAST walker(BR);
564 walker.Visit(D->getBody());
565 }
566};
567}
568
569void ento::registerSecuritySyntaxChecker(CheckerManager &mgr) {
570 mgr.registerChecker<SecuritySyntaxChecker>();
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000571}