1d99bd55aSTed Kremenek //==- CheckSecuritySyntaxOnly.cpp - Basic security checks --------*- C++ -*-==//
2d99bd55aSTed Kremenek //
3d99bd55aSTed Kremenek //                     The LLVM Compiler Infrastructure
4d99bd55aSTed Kremenek //
5d99bd55aSTed Kremenek // This file is distributed under the University of Illinois Open Source
6d99bd55aSTed Kremenek // License. See LICENSE.TXT for details.
7d99bd55aSTed Kremenek //
8d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
9d99bd55aSTed Kremenek //
10d99bd55aSTed Kremenek //  This file defines a set of flow-insensitive security checks.
11d99bd55aSTed Kremenek //
12d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
13d99bd55aSTed Kremenek 
14af45aca6SArgyrios Kyrtzidis #include "ClangSACheckers.h"
156a5674ffSArgyrios Kyrtzidis #include "clang/StaticAnalyzer/Core/Checker.h"
16f8cbac4bSTed Kremenek #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
17af45aca6SArgyrios Kyrtzidis #include "clang/Basic/TargetInfo.h"
18d99bd55aSTed Kremenek #include "clang/AST/StmtVisitor.h"
19d99bd55aSTed Kremenek #include "llvm/Support/raw_ostream.h"
20fca2e961SLenny Maiorani #include "llvm/ADT/StringSwitch.h"
21d99bd55aSTed Kremenek 
22d99bd55aSTed Kremenek using namespace clang;
23d99bd55aSTed Kremenek using namespace ento;
24d99bd55aSTed Kremenek 
25d99bd55aSTed Kremenek static bool isArc4RandomAvailable(const ASTContext &Ctx) {
26*e8bbc121SDouglas Gregor   const llvm::Triple &T = Ctx.getTargetInfo().getTriple();
27d99bd55aSTed Kremenek   return T.getVendor() == llvm::Triple::Apple ||
2845e84b00SDouglas Gregor          T.getOS() == llvm::Triple::FreeBSD ||
2945e84b00SDouglas Gregor          T.getOS() == llvm::Triple::NetBSD ||
3045e84b00SDouglas Gregor          T.getOS() == llvm::Triple::OpenBSD ||
3145e84b00SDouglas Gregor          T.getOS() == llvm::Triple::DragonFly;
32d99bd55aSTed Kremenek }
33d99bd55aSTed Kremenek 
34d99bd55aSTed Kremenek namespace {
35d99bd55aSTed Kremenek class WalkAST : public StmtVisitor<WalkAST> {
36d99bd55aSTed Kremenek   BugReporter &BR;
37d99bd55aSTed Kremenek   enum { num_setids = 6 };
38d99bd55aSTed Kremenek   IdentifierInfo *II_setid[num_setids];
39d99bd55aSTed Kremenek 
40d99bd55aSTed Kremenek   const bool CheckRand;
41d99bd55aSTed Kremenek 
42d99bd55aSTed Kremenek public:
43fca2e961SLenny Maiorani   WalkAST(BugReporter &br) : BR(br), II_setid(),
44d99bd55aSTed Kremenek                  CheckRand(isArc4RandomAvailable(BR.getContext())) {}
45d99bd55aSTed Kremenek 
46d99bd55aSTed Kremenek   // Statement visitor methods.
47d99bd55aSTed Kremenek   void VisitCallExpr(CallExpr *CE);
48d99bd55aSTed Kremenek   void VisitForStmt(ForStmt *S);
49d99bd55aSTed Kremenek   void VisitCompoundStmt (CompoundStmt *S);
50d99bd55aSTed Kremenek   void VisitStmt(Stmt *S) { VisitChildren(S); }
51d99bd55aSTed Kremenek 
52d99bd55aSTed Kremenek   void VisitChildren(Stmt *S);
53d99bd55aSTed Kremenek 
54d99bd55aSTed Kremenek   // Helpers.
55de909e49SLenny Maiorani   bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
56d99bd55aSTed Kremenek 
57fca2e961SLenny Maiorani   typedef void (WalkAST::*FnCheck)(const CallExpr *,
58fca2e961SLenny Maiorani 				   const FunctionDecl *);
59fca2e961SLenny Maiorani 
60d99bd55aSTed Kremenek   // Checker-specific methods.
61de909e49SLenny Maiorani   void checkLoopConditionForFloat(const ForStmt *FS);
62de909e49SLenny Maiorani   void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
63de909e49SLenny Maiorani   void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
64de909e49SLenny Maiorani   void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
65de909e49SLenny Maiorani   void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
66de909e49SLenny Maiorani   void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
67de909e49SLenny Maiorani   void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
68de909e49SLenny Maiorani   void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
69de909e49SLenny Maiorani   void checkUncheckedReturnValue(CallExpr *CE);
70d99bd55aSTed Kremenek };
71d99bd55aSTed Kremenek } // end anonymous namespace
72d99bd55aSTed Kremenek 
73d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
74d99bd55aSTed Kremenek // AST walking.
75d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
76d99bd55aSTed Kremenek 
77d99bd55aSTed Kremenek void WalkAST::VisitChildren(Stmt *S) {
78d99bd55aSTed Kremenek   for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
79d99bd55aSTed Kremenek     if (Stmt *child = *I)
80d99bd55aSTed Kremenek       Visit(child);
81d99bd55aSTed Kremenek }
82d99bd55aSTed Kremenek 
83d99bd55aSTed Kremenek void WalkAST::VisitCallExpr(CallExpr *CE) {
84fca2e961SLenny Maiorani   // Get the callee.
85fca2e961SLenny Maiorani   const FunctionDecl *FD = CE->getDirectCallee();
86fca2e961SLenny Maiorani 
87fca2e961SLenny Maiorani   if (!FD)
88fca2e961SLenny Maiorani     return;
89fca2e961SLenny Maiorani 
90fca2e961SLenny Maiorani   // Get the name of the callee. If it's a builtin, strip off the prefix.
91fca2e961SLenny Maiorani   IdentifierInfo *II = FD->getIdentifier();
92fca2e961SLenny Maiorani   if (!II)   // if no identifier, not a simple C function
93fca2e961SLenny Maiorani     return;
940e62c1ccSChris Lattner   StringRef Name = II->getName();
95fca2e961SLenny Maiorani   if (Name.startswith("__builtin_"))
96fca2e961SLenny Maiorani     Name = Name.substr(10);
97fca2e961SLenny Maiorani 
98fca2e961SLenny Maiorani   // Set the evaluation function by switching on the callee name.
99fca2e961SLenny Maiorani   FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
100de909e49SLenny Maiorani     .Case("gets", &WalkAST::checkCall_gets)
101de909e49SLenny Maiorani     .Case("getpw", &WalkAST::checkCall_getpw)
102de909e49SLenny Maiorani     .Case("mktemp", &WalkAST::checkCall_mktemp)
103de909e49SLenny Maiorani     .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
104de909e49SLenny Maiorani     .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
105de909e49SLenny Maiorani     .Case("drand48", &WalkAST::checkCall_rand)
106de909e49SLenny Maiorani     .Case("erand48", &WalkAST::checkCall_rand)
107de909e49SLenny Maiorani     .Case("jrand48", &WalkAST::checkCall_rand)
108de909e49SLenny Maiorani     .Case("lrand48", &WalkAST::checkCall_rand)
109de909e49SLenny Maiorani     .Case("mrand48", &WalkAST::checkCall_rand)
110de909e49SLenny Maiorani     .Case("nrand48", &WalkAST::checkCall_rand)
111de909e49SLenny Maiorani     .Case("lcong48", &WalkAST::checkCall_rand)
112de909e49SLenny Maiorani     .Case("rand", &WalkAST::checkCall_rand)
113de909e49SLenny Maiorani     .Case("rand_r", &WalkAST::checkCall_rand)
114de909e49SLenny Maiorani     .Case("random", &WalkAST::checkCall_random)
115fca2e961SLenny Maiorani     .Default(NULL);
116fca2e961SLenny Maiorani 
117fca2e961SLenny Maiorani   // If the callee isn't defined, it is not of security concern.
118fca2e961SLenny Maiorani   // Check and evaluate the call.
119fca2e961SLenny Maiorani   if (evalFunction)
120fca2e961SLenny Maiorani     (this->*evalFunction)(CE, FD);
121d99bd55aSTed Kremenek 
122d99bd55aSTed Kremenek   // Recurse and check children.
123d99bd55aSTed Kremenek   VisitChildren(CE);
124d99bd55aSTed Kremenek }
125d99bd55aSTed Kremenek 
126d99bd55aSTed Kremenek void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
127d99bd55aSTed Kremenek   for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
128d99bd55aSTed Kremenek     if (Stmt *child = *I) {
129d99bd55aSTed Kremenek       if (CallExpr *CE = dyn_cast<CallExpr>(child))
130de909e49SLenny Maiorani         checkUncheckedReturnValue(CE);
131d99bd55aSTed Kremenek       Visit(child);
132d99bd55aSTed Kremenek     }
133d99bd55aSTed Kremenek }
134d99bd55aSTed Kremenek 
135d99bd55aSTed Kremenek void WalkAST::VisitForStmt(ForStmt *FS) {
136de909e49SLenny Maiorani   checkLoopConditionForFloat(FS);
137d99bd55aSTed Kremenek 
138d99bd55aSTed Kremenek   // Recurse and check children.
139d99bd55aSTed Kremenek   VisitChildren(FS);
140d99bd55aSTed Kremenek }
141d99bd55aSTed Kremenek 
142d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
143d99bd55aSTed Kremenek // Check: floating poing variable used as loop counter.
144d99bd55aSTed Kremenek // Originally: <rdar://problem/6336718>
145d99bd55aSTed Kremenek // Implements: CERT security coding advisory FLP-30.
146d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
147d99bd55aSTed Kremenek 
148d99bd55aSTed Kremenek static const DeclRefExpr*
149de909e49SLenny Maiorani getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
150d99bd55aSTed Kremenek   expr = expr->IgnoreParenCasts();
151d99bd55aSTed Kremenek 
152d99bd55aSTed Kremenek   if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
153d99bd55aSTed Kremenek     if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
154d99bd55aSTed Kremenek           B->getOpcode() == BO_Comma))
155d99bd55aSTed Kremenek       return NULL;
156d99bd55aSTed Kremenek 
157de909e49SLenny Maiorani     if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
158d99bd55aSTed Kremenek       return lhs;
159d99bd55aSTed Kremenek 
160de909e49SLenny Maiorani     if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
161d99bd55aSTed Kremenek       return rhs;
162d99bd55aSTed Kremenek 
163d99bd55aSTed Kremenek     return NULL;
164d99bd55aSTed Kremenek   }
165d99bd55aSTed Kremenek 
166d99bd55aSTed Kremenek   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
167d99bd55aSTed Kremenek     const NamedDecl *ND = DR->getDecl();
168d99bd55aSTed Kremenek     return ND == x || ND == y ? DR : NULL;
169d99bd55aSTed Kremenek   }
170d99bd55aSTed Kremenek 
171d99bd55aSTed Kremenek   if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
172d99bd55aSTed Kremenek     return U->isIncrementDecrementOp()
173de909e49SLenny Maiorani       ? getIncrementedVar(U->getSubExpr(), x, y) : NULL;
174d99bd55aSTed Kremenek 
175d99bd55aSTed Kremenek   return NULL;
176d99bd55aSTed Kremenek }
177d99bd55aSTed Kremenek 
178d99bd55aSTed Kremenek /// CheckLoopConditionForFloat - This check looks for 'for' statements that
179d99bd55aSTed Kremenek ///  use a floating point variable as a loop counter.
180d99bd55aSTed Kremenek ///  CERT: FLP30-C, FLP30-CPP.
181d99bd55aSTed Kremenek ///
182de909e49SLenny Maiorani void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
183d99bd55aSTed Kremenek   // Does the loop have a condition?
184d99bd55aSTed Kremenek   const Expr *condition = FS->getCond();
185d99bd55aSTed Kremenek 
186d99bd55aSTed Kremenek   if (!condition)
187d99bd55aSTed Kremenek     return;
188d99bd55aSTed Kremenek 
189d99bd55aSTed Kremenek   // Does the loop have an increment?
190d99bd55aSTed Kremenek   const Expr *increment = FS->getInc();
191d99bd55aSTed Kremenek 
192d99bd55aSTed Kremenek   if (!increment)
193d99bd55aSTed Kremenek     return;
194d99bd55aSTed Kremenek 
195d99bd55aSTed Kremenek   // Strip away '()' and casts.
196d99bd55aSTed Kremenek   condition = condition->IgnoreParenCasts();
197d99bd55aSTed Kremenek   increment = increment->IgnoreParenCasts();
198d99bd55aSTed Kremenek 
199d99bd55aSTed Kremenek   // Is the loop condition a comparison?
200d99bd55aSTed Kremenek   const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
201d99bd55aSTed Kremenek 
202d99bd55aSTed Kremenek   if (!B)
203d99bd55aSTed Kremenek     return;
204d99bd55aSTed Kremenek 
205d99bd55aSTed Kremenek   // Is this a comparison?
206d99bd55aSTed Kremenek   if (!(B->isRelationalOp() || B->isEqualityOp()))
207d99bd55aSTed Kremenek     return;
208d99bd55aSTed Kremenek 
209d99bd55aSTed Kremenek   // Are we comparing variables?
210d99bd55aSTed Kremenek   const DeclRefExpr *drLHS =
211d99bd55aSTed Kremenek     dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
212d99bd55aSTed Kremenek   const DeclRefExpr *drRHS =
213d99bd55aSTed Kremenek     dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
214d99bd55aSTed Kremenek 
215d99bd55aSTed Kremenek   // Does at least one of the variables have a floating point type?
216d99bd55aSTed Kremenek   drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
217d99bd55aSTed Kremenek   drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
218d99bd55aSTed Kremenek 
219d99bd55aSTed Kremenek   if (!drLHS && !drRHS)
220d99bd55aSTed Kremenek     return;
221d99bd55aSTed Kremenek 
222d99bd55aSTed Kremenek   const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
223d99bd55aSTed Kremenek   const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
224d99bd55aSTed Kremenek 
225d99bd55aSTed Kremenek   if (!vdLHS && !vdRHS)
226d99bd55aSTed Kremenek     return;
227d99bd55aSTed Kremenek 
228d99bd55aSTed Kremenek   // Does either variable appear in increment?
229de909e49SLenny Maiorani   const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
230d99bd55aSTed Kremenek 
231d99bd55aSTed Kremenek   if (!drInc)
232d99bd55aSTed Kremenek     return;
233d99bd55aSTed Kremenek 
234d99bd55aSTed Kremenek   // Emit the error.  First figure out which DeclRefExpr in the condition
235d99bd55aSTed Kremenek   // referenced the compared variable.
236d99bd55aSTed Kremenek   const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
237d99bd55aSTed Kremenek 
2380e62c1ccSChris Lattner   SmallVector<SourceRange, 2> ranges;
239d99bd55aSTed Kremenek   llvm::SmallString<256> sbuf;
240d99bd55aSTed Kremenek   llvm::raw_svector_ostream os(sbuf);
241d99bd55aSTed Kremenek 
242d99bd55aSTed Kremenek   os << "Variable '" << drCond->getDecl()->getName()
243d99bd55aSTed Kremenek      << "' with floating point type '" << drCond->getType().getAsString()
244d99bd55aSTed Kremenek      << "' should not be used as a loop counter";
245d99bd55aSTed Kremenek 
246d99bd55aSTed Kremenek   ranges.push_back(drCond->getSourceRange());
247d99bd55aSTed Kremenek   ranges.push_back(drInc->getSourceRange());
248d99bd55aSTed Kremenek 
249d99bd55aSTed Kremenek   const char *bugType = "Floating point variable used as loop counter";
250d99bd55aSTed Kremenek   BR.EmitBasicReport(bugType, "Security", os.str(),
251d99bd55aSTed Kremenek                      FS->getLocStart(), ranges.data(), ranges.size());
252d99bd55aSTed Kremenek }
253d99bd55aSTed Kremenek 
254d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
255d99bd55aSTed Kremenek // Check: Any use of 'gets' is insecure.
256d99bd55aSTed Kremenek // Originally: <rdar://problem/6335715>
257d99bd55aSTed Kremenek // Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
258d99bd55aSTed Kremenek // CWE-242: Use of Inherently Dangerous Function
259d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
260d99bd55aSTed Kremenek 
261de909e49SLenny Maiorani void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
262d99bd55aSTed Kremenek   const FunctionProtoType *FPT
263d99bd55aSTed Kremenek     = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
264d99bd55aSTed Kremenek   if (!FPT)
265d99bd55aSTed Kremenek     return;
266d99bd55aSTed Kremenek 
267d99bd55aSTed Kremenek   // Verify that the function takes a single argument.
268d99bd55aSTed Kremenek   if (FPT->getNumArgs() != 1)
269d99bd55aSTed Kremenek     return;
270d99bd55aSTed Kremenek 
271d99bd55aSTed Kremenek   // Is the argument a 'char*'?
272d99bd55aSTed Kremenek   const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
273d99bd55aSTed Kremenek   if (!PT)
274d99bd55aSTed Kremenek     return;
275d99bd55aSTed Kremenek 
276d99bd55aSTed Kremenek   if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
277d99bd55aSTed Kremenek     return;
278d99bd55aSTed Kremenek 
279d99bd55aSTed Kremenek   // Issue a warning.
280d99bd55aSTed Kremenek   SourceRange R = CE->getCallee()->getSourceRange();
281d99bd55aSTed Kremenek   BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
282d99bd55aSTed Kremenek                      "Security",
283d99bd55aSTed Kremenek                      "Call to function 'gets' is extremely insecure as it can "
284d99bd55aSTed Kremenek                      "always result in a buffer overflow",
285d99bd55aSTed Kremenek                      CE->getLocStart(), &R, 1);
286d99bd55aSTed Kremenek }
287d99bd55aSTed Kremenek 
288d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
289d99bd55aSTed Kremenek // Check: Any use of 'getpwd' is insecure.
290d99bd55aSTed Kremenek // CWE-477: Use of Obsolete Functions
291d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
292d99bd55aSTed Kremenek 
293de909e49SLenny Maiorani void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
294d99bd55aSTed Kremenek   const FunctionProtoType *FPT
295d99bd55aSTed Kremenek     = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
296d99bd55aSTed Kremenek   if (!FPT)
297d99bd55aSTed Kremenek     return;
298d99bd55aSTed Kremenek 
299d99bd55aSTed Kremenek   // Verify that the function takes two arguments.
300d99bd55aSTed Kremenek   if (FPT->getNumArgs() != 2)
301d99bd55aSTed Kremenek     return;
302d99bd55aSTed Kremenek 
303d99bd55aSTed Kremenek   // Verify the first argument type is integer.
304d99bd55aSTed Kremenek   if (!FPT->getArgType(0)->isIntegerType())
305d99bd55aSTed Kremenek     return;
306d99bd55aSTed Kremenek 
307d99bd55aSTed Kremenek   // Verify the second argument type is char*.
308d99bd55aSTed Kremenek   const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
309d99bd55aSTed Kremenek   if (!PT)
310d99bd55aSTed Kremenek     return;
311d99bd55aSTed Kremenek 
312d99bd55aSTed Kremenek   if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
313d99bd55aSTed Kremenek     return;
314d99bd55aSTed Kremenek 
315d99bd55aSTed Kremenek   // Issue a warning.
316d99bd55aSTed Kremenek   SourceRange R = CE->getCallee()->getSourceRange();
317d99bd55aSTed Kremenek   BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
318d99bd55aSTed Kremenek                      "Security",
319d99bd55aSTed Kremenek                      "The getpw() function is dangerous as it may overflow the "
320d99bd55aSTed Kremenek                      "provided buffer. It is obsoleted by getpwuid().",
321d99bd55aSTed Kremenek                      CE->getLocStart(), &R, 1);
322d99bd55aSTed Kremenek }
323d99bd55aSTed Kremenek 
324d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
325d99bd55aSTed Kremenek // Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
326d99bd55aSTed Kremenek // CWE-377: Insecure Temporary File
327d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
328d99bd55aSTed Kremenek 
329de909e49SLenny Maiorani void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
330d99bd55aSTed Kremenek   const FunctionProtoType *FPT
331d99bd55aSTed Kremenek     = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
332d99bd55aSTed Kremenek   if(!FPT)
333d99bd55aSTed Kremenek     return;
334d99bd55aSTed Kremenek 
33570568c2bSLenny Maiorani   // Verify that the function takes a single argument.
336d99bd55aSTed Kremenek   if (FPT->getNumArgs() != 1)
337d99bd55aSTed Kremenek     return;
338d99bd55aSTed Kremenek 
339d99bd55aSTed Kremenek   // Verify that the argument is Pointer Type.
340d99bd55aSTed Kremenek   const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
341d99bd55aSTed Kremenek   if (!PT)
342d99bd55aSTed Kremenek     return;
343d99bd55aSTed Kremenek 
344d99bd55aSTed Kremenek   // Verify that the argument is a 'char*'.
345d99bd55aSTed Kremenek   if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
346d99bd55aSTed Kremenek     return;
347d99bd55aSTed Kremenek 
348d99bd55aSTed Kremenek   // Issue a waring.
349d99bd55aSTed Kremenek   SourceRange R = CE->getCallee()->getSourceRange();
350d99bd55aSTed Kremenek   BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
351d99bd55aSTed Kremenek                      "Security",
352d99bd55aSTed Kremenek                      "Call to function 'mktemp' is insecure as it always "
353d99bd55aSTed Kremenek                      "creates or uses insecure temporary file.  Use 'mkstemp' instead",
354d99bd55aSTed Kremenek                      CE->getLocStart(), &R, 1);
355d99bd55aSTed Kremenek }
356d99bd55aSTed Kremenek 
357d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
3586ffe738fSLenny Maiorani // Check: Any use of 'strcpy' is insecure.
3596ffe738fSLenny Maiorani //
3606ffe738fSLenny Maiorani // CWE-119: Improper Restriction of Operations within
3616ffe738fSLenny Maiorani // the Bounds of a Memory Buffer
3626ffe738fSLenny Maiorani //===----------------------------------------------------------------------===//
363de909e49SLenny Maiorani void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
364de909e49SLenny Maiorani   if (!checkCall_strCommon(CE, FD))
3656ffe738fSLenny Maiorani     return;
3666ffe738fSLenny Maiorani 
367de909e49SLenny Maiorani   // Issue a warning.
3686ffe738fSLenny Maiorani   SourceRange R = CE->getCallee()->getSourceRange();
3696ffe738fSLenny Maiorani   BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
3706ffe738fSLenny Maiorani                      "call 'strcpy'",
3716ffe738fSLenny Maiorani                      "Security",
3726ffe738fSLenny Maiorani                      "Call to function 'strcpy' is insecure as it does not "
3736ffe738fSLenny Maiorani 		     "provide bounding of the memory buffer. Replace "
3746ffe738fSLenny Maiorani 		     "unbounded copy functions with analogous functions that "
3756ffe738fSLenny Maiorani 		     "support length arguments such as 'strncpy'. CWE-119.",
3766ffe738fSLenny Maiorani                      CE->getLocStart(), &R, 1);
3776ffe738fSLenny Maiorani }
3786ffe738fSLenny Maiorani 
3796ffe738fSLenny Maiorani //===----------------------------------------------------------------------===//
380de909e49SLenny Maiorani // Check: Any use of 'strcat' is insecure.
381de909e49SLenny Maiorani //
382de909e49SLenny Maiorani // CWE-119: Improper Restriction of Operations within
383de909e49SLenny Maiorani // the Bounds of a Memory Buffer
384de909e49SLenny Maiorani //===----------------------------------------------------------------------===//
385de909e49SLenny Maiorani void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
386de909e49SLenny Maiorani   if (!checkCall_strCommon(CE, FD))
387de909e49SLenny Maiorani     return;
388de909e49SLenny Maiorani 
389de909e49SLenny Maiorani   // Issue a warning.
390de909e49SLenny Maiorani   SourceRange R = CE->getCallee()->getSourceRange();
391de909e49SLenny Maiorani   BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
392de909e49SLenny Maiorani 		     "call 'strcat'",
393de909e49SLenny Maiorani 		     "Security",
394de909e49SLenny Maiorani 		     "Call to function 'strcat' is insecure as it does not "
395de909e49SLenny Maiorani 		     "provide bounding of the memory buffer. Replace "
396de909e49SLenny Maiorani 		     "unbounded copy functions with analogous functions that "
397de909e49SLenny Maiorani 		     "support length arguments such as 'strncat'. CWE-119.",
398de909e49SLenny Maiorani                      CE->getLocStart(), &R, 1);
399de909e49SLenny Maiorani }
400de909e49SLenny Maiorani 
401de909e49SLenny Maiorani //===----------------------------------------------------------------------===//
402de909e49SLenny Maiorani // Common check for str* functions with no bounds parameters.
403de909e49SLenny Maiorani //===----------------------------------------------------------------------===//
404de909e49SLenny Maiorani bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
405de909e49SLenny Maiorani   const FunctionProtoType *FPT
406de909e49SLenny Maiorani     = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
407de909e49SLenny Maiorani   if (!FPT)
408de909e49SLenny Maiorani     return false;
409de909e49SLenny Maiorani 
410de909e49SLenny Maiorani   // Verify the function takes two arguments, three in the _chk version.
411de909e49SLenny Maiorani   int numArgs = FPT->getNumArgs();
412de909e49SLenny Maiorani   if (numArgs != 2 && numArgs != 3)
413de909e49SLenny Maiorani     return false;
414de909e49SLenny Maiorani 
415de909e49SLenny Maiorani   // Verify the type for both arguments.
416de909e49SLenny Maiorani   for (int i = 0; i < 2; i++) {
417de909e49SLenny Maiorani     // Verify that the arguments are pointers.
418de909e49SLenny Maiorani     const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
419de909e49SLenny Maiorani     if (!PT)
420de909e49SLenny Maiorani       return false;
421de909e49SLenny Maiorani 
422de909e49SLenny Maiorani     // Verify that the argument is a 'char*'.
423de909e49SLenny Maiorani     if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
424de909e49SLenny Maiorani       return false;
425de909e49SLenny Maiorani   }
426de909e49SLenny Maiorani 
427de909e49SLenny Maiorani   return true;
428de909e49SLenny Maiorani }
429de909e49SLenny Maiorani 
430de909e49SLenny Maiorani //===----------------------------------------------------------------------===//
431d99bd55aSTed Kremenek // Check: Linear congruent random number generators should not be used
432d99bd55aSTed Kremenek // Originally: <rdar://problem/63371000>
433d99bd55aSTed Kremenek // CWE-338: Use of cryptographically weak prng
434d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
435d99bd55aSTed Kremenek 
436de909e49SLenny Maiorani void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
437fca2e961SLenny Maiorani   if (!CheckRand)
438d99bd55aSTed Kremenek     return;
439d99bd55aSTed Kremenek 
440d99bd55aSTed Kremenek   const FunctionProtoType *FTP
441d99bd55aSTed Kremenek     = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
442d99bd55aSTed Kremenek   if (!FTP)
443d99bd55aSTed Kremenek     return;
444d99bd55aSTed Kremenek 
445d99bd55aSTed Kremenek   if (FTP->getNumArgs() == 1) {
446d99bd55aSTed Kremenek     // Is the argument an 'unsigned short *'?
447d99bd55aSTed Kremenek     // (Actually any integer type is allowed.)
448d99bd55aSTed Kremenek     const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
449d99bd55aSTed Kremenek     if (!PT)
450d99bd55aSTed Kremenek       return;
451d99bd55aSTed Kremenek 
452d99bd55aSTed Kremenek     if (! PT->getPointeeType()->isIntegerType())
453d99bd55aSTed Kremenek       return;
454d99bd55aSTed Kremenek   }
455d99bd55aSTed Kremenek   else if (FTP->getNumArgs() != 0)
456d99bd55aSTed Kremenek     return;
457d99bd55aSTed Kremenek 
458d99bd55aSTed Kremenek   // Issue a warning.
459d99bd55aSTed Kremenek   llvm::SmallString<256> buf1;
460d99bd55aSTed Kremenek   llvm::raw_svector_ostream os1(buf1);
461d99bd55aSTed Kremenek   os1 << '\'' << FD << "' is a poor random number generator";
462d99bd55aSTed Kremenek 
463d99bd55aSTed Kremenek   llvm::SmallString<256> buf2;
464d99bd55aSTed Kremenek   llvm::raw_svector_ostream os2(buf2);
465d99bd55aSTed Kremenek   os2 << "Function '" << FD
466d99bd55aSTed Kremenek       << "' is obsolete because it implements a poor random number generator."
467d99bd55aSTed Kremenek       << "  Use 'arc4random' instead";
468d99bd55aSTed Kremenek 
469d99bd55aSTed Kremenek   SourceRange R = CE->getCallee()->getSourceRange();
470d99bd55aSTed Kremenek   BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
471d99bd55aSTed Kremenek }
472d99bd55aSTed Kremenek 
473d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
474d99bd55aSTed Kremenek // Check: 'random' should not be used
475d99bd55aSTed Kremenek // Originally: <rdar://problem/63371000>
476d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
477d99bd55aSTed Kremenek 
478de909e49SLenny Maiorani void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
479fca2e961SLenny Maiorani   if (!CheckRand)
480d99bd55aSTed Kremenek     return;
481d99bd55aSTed Kremenek 
482d99bd55aSTed Kremenek   const FunctionProtoType *FTP
483d99bd55aSTed Kremenek     = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
484d99bd55aSTed Kremenek   if (!FTP)
485d99bd55aSTed Kremenek     return;
486d99bd55aSTed Kremenek 
487d99bd55aSTed Kremenek   // Verify that the function takes no argument.
488d99bd55aSTed Kremenek   if (FTP->getNumArgs() != 0)
489d99bd55aSTed Kremenek     return;
490d99bd55aSTed Kremenek 
491d99bd55aSTed Kremenek   // Issue a warning.
492d99bd55aSTed Kremenek   SourceRange R = CE->getCallee()->getSourceRange();
493d99bd55aSTed Kremenek   BR.EmitBasicReport("'random' is not a secure random number generator",
494d99bd55aSTed Kremenek                      "Security",
495d99bd55aSTed Kremenek                      "The 'random' function produces a sequence of values that "
496d99bd55aSTed Kremenek                      "an adversary may be able to predict.  Use 'arc4random' "
497d99bd55aSTed Kremenek                      "instead", CE->getLocStart(), &R, 1);
498d99bd55aSTed Kremenek }
499d99bd55aSTed Kremenek 
500d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
501d99bd55aSTed Kremenek // Check: Should check whether privileges are dropped successfully.
502d99bd55aSTed Kremenek // Originally: <rdar://problem/6337132>
503d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
504d99bd55aSTed Kremenek 
505de909e49SLenny Maiorani void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
506d99bd55aSTed Kremenek   const FunctionDecl *FD = CE->getDirectCallee();
507d99bd55aSTed Kremenek   if (!FD)
508d99bd55aSTed Kremenek     return;
509d99bd55aSTed Kremenek 
510d99bd55aSTed Kremenek   if (II_setid[0] == NULL) {
511d99bd55aSTed Kremenek     static const char * const identifiers[num_setids] = {
512d99bd55aSTed Kremenek       "setuid", "setgid", "seteuid", "setegid",
513d99bd55aSTed Kremenek       "setreuid", "setregid"
514d99bd55aSTed Kremenek     };
515d99bd55aSTed Kremenek 
516d99bd55aSTed Kremenek     for (size_t i = 0; i < num_setids; i++)
517d99bd55aSTed Kremenek       II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
518d99bd55aSTed Kremenek   }
519d99bd55aSTed Kremenek 
520d99bd55aSTed Kremenek   const IdentifierInfo *id = FD->getIdentifier();
521d99bd55aSTed Kremenek   size_t identifierid;
522d99bd55aSTed Kremenek 
523d99bd55aSTed Kremenek   for (identifierid = 0; identifierid < num_setids; identifierid++)
524d99bd55aSTed Kremenek     if (id == II_setid[identifierid])
525d99bd55aSTed Kremenek       break;
526d99bd55aSTed Kremenek 
527d99bd55aSTed Kremenek   if (identifierid >= num_setids)
528d99bd55aSTed Kremenek     return;
529d99bd55aSTed Kremenek 
530d99bd55aSTed Kremenek   const FunctionProtoType *FTP
531d99bd55aSTed Kremenek     = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
532d99bd55aSTed Kremenek   if (!FTP)
533d99bd55aSTed Kremenek     return;
534d99bd55aSTed Kremenek 
535d99bd55aSTed Kremenek   // Verify that the function takes one or two arguments (depending on
536d99bd55aSTed Kremenek   //   the function).
537d99bd55aSTed Kremenek   if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
538d99bd55aSTed Kremenek     return;
539d99bd55aSTed Kremenek 
540d99bd55aSTed Kremenek   // The arguments must be integers.
541d99bd55aSTed Kremenek   for (unsigned i = 0; i < FTP->getNumArgs(); i++)
542d99bd55aSTed Kremenek     if (! FTP->getArgType(i)->isIntegerType())
543d99bd55aSTed Kremenek       return;
544d99bd55aSTed Kremenek 
545d99bd55aSTed Kremenek   // Issue a warning.
546d99bd55aSTed Kremenek   llvm::SmallString<256> buf1;
547d99bd55aSTed Kremenek   llvm::raw_svector_ostream os1(buf1);
548d99bd55aSTed Kremenek   os1 << "Return value is not checked in call to '" << FD << '\'';
549d99bd55aSTed Kremenek 
550d99bd55aSTed Kremenek   llvm::SmallString<256> buf2;
551d99bd55aSTed Kremenek   llvm::raw_svector_ostream os2(buf2);
552d99bd55aSTed Kremenek   os2 << "The return value from the call to '" << FD
553d99bd55aSTed Kremenek       << "' is not checked.  If an error occurs in '" << FD
554d99bd55aSTed Kremenek       << "', the following code may execute with unexpected privileges";
555d99bd55aSTed Kremenek 
556d99bd55aSTed Kremenek   SourceRange R = CE->getCallee()->getSourceRange();
557d99bd55aSTed Kremenek   BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1);
558d99bd55aSTed Kremenek }
559d99bd55aSTed Kremenek 
560d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
561af45aca6SArgyrios Kyrtzidis // SecuritySyntaxChecker
562d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
563d99bd55aSTed Kremenek 
564af45aca6SArgyrios Kyrtzidis namespace {
5656a5674ffSArgyrios Kyrtzidis class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
566af45aca6SArgyrios Kyrtzidis public:
567af45aca6SArgyrios Kyrtzidis   void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
568af45aca6SArgyrios Kyrtzidis                         BugReporter &BR) const {
569d99bd55aSTed Kremenek     WalkAST walker(BR);
570d99bd55aSTed Kremenek     walker.Visit(D->getBody());
571d99bd55aSTed Kremenek   }
572af45aca6SArgyrios Kyrtzidis };
573af45aca6SArgyrios Kyrtzidis }
574af45aca6SArgyrios Kyrtzidis 
575af45aca6SArgyrios Kyrtzidis void ento::registerSecuritySyntaxChecker(CheckerManager &mgr) {
576af45aca6SArgyrios Kyrtzidis   mgr.registerChecker<SecuritySyntaxChecker>();
577af45aca6SArgyrios Kyrtzidis }
578