xref: /minix3/external/bsd/llvm/dist/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //==- CheckSecuritySyntaxOnly.cpp - Basic security checks --------*- C++ -*-==//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc //  This file defines a set of flow-insensitive security checks.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "ClangSACheckers.h"
15f4a2713aSLionel Sambuc #include "clang/AST/StmtVisitor.h"
16f4a2713aSLionel Sambuc #include "clang/Analysis/AnalysisContext.h"
17f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
18f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
19f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Checker.h"
20f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
21f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
22f4a2713aSLionel Sambuc #include "llvm/ADT/StringSwitch.h"
23f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
24f4a2713aSLionel Sambuc 
25f4a2713aSLionel Sambuc using namespace clang;
26f4a2713aSLionel Sambuc using namespace ento;
27f4a2713aSLionel Sambuc 
isArc4RandomAvailable(const ASTContext & Ctx)28f4a2713aSLionel Sambuc static bool isArc4RandomAvailable(const ASTContext &Ctx) {
29f4a2713aSLionel Sambuc   const llvm::Triple &T = Ctx.getTargetInfo().getTriple();
30f4a2713aSLionel Sambuc   return T.getVendor() == llvm::Triple::Apple ||
31f4a2713aSLionel Sambuc          T.getOS() == llvm::Triple::FreeBSD ||
32f4a2713aSLionel Sambuc          T.getOS() == llvm::Triple::NetBSD ||
33f4a2713aSLionel Sambuc          T.getOS() == llvm::Triple::OpenBSD ||
34f4a2713aSLionel Sambuc          T.getOS() == llvm::Triple::Bitrig ||
35f4a2713aSLionel Sambuc          T.getOS() == llvm::Triple::DragonFly;
36f4a2713aSLionel Sambuc }
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc namespace {
39f4a2713aSLionel Sambuc struct ChecksFilter {
40f4a2713aSLionel Sambuc   DefaultBool check_gets;
41f4a2713aSLionel Sambuc   DefaultBool check_getpw;
42f4a2713aSLionel Sambuc   DefaultBool check_mktemp;
43f4a2713aSLionel Sambuc   DefaultBool check_mkstemp;
44f4a2713aSLionel Sambuc   DefaultBool check_strcpy;
45f4a2713aSLionel Sambuc   DefaultBool check_rand;
46f4a2713aSLionel Sambuc   DefaultBool check_vfork;
47f4a2713aSLionel Sambuc   DefaultBool check_FloatLoopCounter;
48f4a2713aSLionel Sambuc   DefaultBool check_UncheckedReturn;
49*0a6a1f1dSLionel Sambuc 
50*0a6a1f1dSLionel Sambuc   CheckName checkName_gets;
51*0a6a1f1dSLionel Sambuc   CheckName checkName_getpw;
52*0a6a1f1dSLionel Sambuc   CheckName checkName_mktemp;
53*0a6a1f1dSLionel Sambuc   CheckName checkName_mkstemp;
54*0a6a1f1dSLionel Sambuc   CheckName checkName_strcpy;
55*0a6a1f1dSLionel Sambuc   CheckName checkName_rand;
56*0a6a1f1dSLionel Sambuc   CheckName checkName_vfork;
57*0a6a1f1dSLionel Sambuc   CheckName checkName_FloatLoopCounter;
58*0a6a1f1dSLionel Sambuc   CheckName checkName_UncheckedReturn;
59f4a2713aSLionel Sambuc };
60f4a2713aSLionel Sambuc 
61f4a2713aSLionel Sambuc class WalkAST : public StmtVisitor<WalkAST> {
62f4a2713aSLionel Sambuc   BugReporter &BR;
63f4a2713aSLionel Sambuc   AnalysisDeclContext* AC;
64f4a2713aSLionel Sambuc   enum { num_setids = 6 };
65f4a2713aSLionel Sambuc   IdentifierInfo *II_setid[num_setids];
66f4a2713aSLionel Sambuc 
67f4a2713aSLionel Sambuc   const bool CheckRand;
68f4a2713aSLionel Sambuc   const ChecksFilter &filter;
69f4a2713aSLionel Sambuc 
70f4a2713aSLionel Sambuc public:
WalkAST(BugReporter & br,AnalysisDeclContext * ac,const ChecksFilter & f)71f4a2713aSLionel Sambuc   WalkAST(BugReporter &br, AnalysisDeclContext* ac,
72f4a2713aSLionel Sambuc           const ChecksFilter &f)
73f4a2713aSLionel Sambuc   : BR(br), AC(ac), II_setid(),
74f4a2713aSLionel Sambuc     CheckRand(isArc4RandomAvailable(BR.getContext())),
75f4a2713aSLionel Sambuc     filter(f) {}
76f4a2713aSLionel Sambuc 
77f4a2713aSLionel Sambuc   // Statement visitor methods.
78f4a2713aSLionel Sambuc   void VisitCallExpr(CallExpr *CE);
79f4a2713aSLionel Sambuc   void VisitForStmt(ForStmt *S);
80f4a2713aSLionel Sambuc   void VisitCompoundStmt (CompoundStmt *S);
VisitStmt(Stmt * S)81f4a2713aSLionel Sambuc   void VisitStmt(Stmt *S) { VisitChildren(S); }
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc   void VisitChildren(Stmt *S);
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc   // Helpers.
86f4a2713aSLionel Sambuc   bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
87f4a2713aSLionel Sambuc 
88f4a2713aSLionel Sambuc   typedef void (WalkAST::*FnCheck)(const CallExpr *,
89f4a2713aSLionel Sambuc 				   const FunctionDecl *);
90f4a2713aSLionel Sambuc 
91f4a2713aSLionel Sambuc   // Checker-specific methods.
92f4a2713aSLionel Sambuc   void checkLoopConditionForFloat(const ForStmt *FS);
93f4a2713aSLionel Sambuc   void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
94f4a2713aSLionel Sambuc   void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
95f4a2713aSLionel Sambuc   void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
96f4a2713aSLionel Sambuc   void checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD);
97f4a2713aSLionel Sambuc   void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
98f4a2713aSLionel Sambuc   void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
99f4a2713aSLionel Sambuc   void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
100f4a2713aSLionel Sambuc   void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
101f4a2713aSLionel Sambuc   void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD);
102f4a2713aSLionel Sambuc   void checkUncheckedReturnValue(CallExpr *CE);
103f4a2713aSLionel Sambuc };
104f4a2713aSLionel Sambuc } // end anonymous namespace
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
107f4a2713aSLionel Sambuc // AST walking.
108f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
109f4a2713aSLionel Sambuc 
VisitChildren(Stmt * S)110f4a2713aSLionel Sambuc void WalkAST::VisitChildren(Stmt *S) {
111f4a2713aSLionel Sambuc   for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
112f4a2713aSLionel Sambuc     if (Stmt *child = *I)
113f4a2713aSLionel Sambuc       Visit(child);
114f4a2713aSLionel Sambuc }
115f4a2713aSLionel Sambuc 
VisitCallExpr(CallExpr * CE)116f4a2713aSLionel Sambuc void WalkAST::VisitCallExpr(CallExpr *CE) {
117f4a2713aSLionel Sambuc   // Get the callee.
118f4a2713aSLionel Sambuc   const FunctionDecl *FD = CE->getDirectCallee();
119f4a2713aSLionel Sambuc 
120f4a2713aSLionel Sambuc   if (!FD)
121f4a2713aSLionel Sambuc     return;
122f4a2713aSLionel Sambuc 
123f4a2713aSLionel Sambuc   // Get the name of the callee. If it's a builtin, strip off the prefix.
124f4a2713aSLionel Sambuc   IdentifierInfo *II = FD->getIdentifier();
125f4a2713aSLionel Sambuc   if (!II)   // if no identifier, not a simple C function
126f4a2713aSLionel Sambuc     return;
127f4a2713aSLionel Sambuc   StringRef Name = II->getName();
128f4a2713aSLionel Sambuc   if (Name.startswith("__builtin_"))
129f4a2713aSLionel Sambuc     Name = Name.substr(10);
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc   // Set the evaluation function by switching on the callee name.
132f4a2713aSLionel Sambuc   FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
133f4a2713aSLionel Sambuc     .Case("gets", &WalkAST::checkCall_gets)
134f4a2713aSLionel Sambuc     .Case("getpw", &WalkAST::checkCall_getpw)
135f4a2713aSLionel Sambuc     .Case("mktemp", &WalkAST::checkCall_mktemp)
136f4a2713aSLionel Sambuc     .Case("mkstemp", &WalkAST::checkCall_mkstemp)
137f4a2713aSLionel Sambuc     .Case("mkdtemp", &WalkAST::checkCall_mkstemp)
138f4a2713aSLionel Sambuc     .Case("mkstemps", &WalkAST::checkCall_mkstemp)
139f4a2713aSLionel Sambuc     .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
140f4a2713aSLionel Sambuc     .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
141f4a2713aSLionel Sambuc     .Case("drand48", &WalkAST::checkCall_rand)
142f4a2713aSLionel Sambuc     .Case("erand48", &WalkAST::checkCall_rand)
143f4a2713aSLionel Sambuc     .Case("jrand48", &WalkAST::checkCall_rand)
144f4a2713aSLionel Sambuc     .Case("lrand48", &WalkAST::checkCall_rand)
145f4a2713aSLionel Sambuc     .Case("mrand48", &WalkAST::checkCall_rand)
146f4a2713aSLionel Sambuc     .Case("nrand48", &WalkAST::checkCall_rand)
147f4a2713aSLionel Sambuc     .Case("lcong48", &WalkAST::checkCall_rand)
148f4a2713aSLionel Sambuc     .Case("rand", &WalkAST::checkCall_rand)
149f4a2713aSLionel Sambuc     .Case("rand_r", &WalkAST::checkCall_rand)
150f4a2713aSLionel Sambuc     .Case("random", &WalkAST::checkCall_random)
151f4a2713aSLionel Sambuc     .Case("vfork", &WalkAST::checkCall_vfork)
152*0a6a1f1dSLionel Sambuc     .Default(nullptr);
153f4a2713aSLionel Sambuc 
154f4a2713aSLionel Sambuc   // If the callee isn't defined, it is not of security concern.
155f4a2713aSLionel Sambuc   // Check and evaluate the call.
156f4a2713aSLionel Sambuc   if (evalFunction)
157f4a2713aSLionel Sambuc     (this->*evalFunction)(CE, FD);
158f4a2713aSLionel Sambuc 
159f4a2713aSLionel Sambuc   // Recurse and check children.
160f4a2713aSLionel Sambuc   VisitChildren(CE);
161f4a2713aSLionel Sambuc }
162f4a2713aSLionel Sambuc 
VisitCompoundStmt(CompoundStmt * S)163f4a2713aSLionel Sambuc void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
164f4a2713aSLionel Sambuc   for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
165f4a2713aSLionel Sambuc     if (Stmt *child = *I) {
166f4a2713aSLionel Sambuc       if (CallExpr *CE = dyn_cast<CallExpr>(child))
167f4a2713aSLionel Sambuc         checkUncheckedReturnValue(CE);
168f4a2713aSLionel Sambuc       Visit(child);
169f4a2713aSLionel Sambuc     }
170f4a2713aSLionel Sambuc }
171f4a2713aSLionel Sambuc 
VisitForStmt(ForStmt * FS)172f4a2713aSLionel Sambuc void WalkAST::VisitForStmt(ForStmt *FS) {
173f4a2713aSLionel Sambuc   checkLoopConditionForFloat(FS);
174f4a2713aSLionel Sambuc 
175f4a2713aSLionel Sambuc   // Recurse and check children.
176f4a2713aSLionel Sambuc   VisitChildren(FS);
177f4a2713aSLionel Sambuc }
178f4a2713aSLionel Sambuc 
179f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
180f4a2713aSLionel Sambuc // Check: floating poing variable used as loop counter.
181f4a2713aSLionel Sambuc // Originally: <rdar://problem/6336718>
182f4a2713aSLionel Sambuc // Implements: CERT security coding advisory FLP-30.
183f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
184f4a2713aSLionel Sambuc 
185f4a2713aSLionel Sambuc static const DeclRefExpr*
getIncrementedVar(const Expr * expr,const VarDecl * x,const VarDecl * y)186f4a2713aSLionel Sambuc getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
187f4a2713aSLionel Sambuc   expr = expr->IgnoreParenCasts();
188f4a2713aSLionel Sambuc 
189f4a2713aSLionel Sambuc   if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
190f4a2713aSLionel Sambuc     if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
191f4a2713aSLionel Sambuc           B->getOpcode() == BO_Comma))
192*0a6a1f1dSLionel Sambuc       return nullptr;
193f4a2713aSLionel Sambuc 
194f4a2713aSLionel Sambuc     if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
195f4a2713aSLionel Sambuc       return lhs;
196f4a2713aSLionel Sambuc 
197f4a2713aSLionel Sambuc     if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
198f4a2713aSLionel Sambuc       return rhs;
199f4a2713aSLionel Sambuc 
200*0a6a1f1dSLionel Sambuc     return nullptr;
201f4a2713aSLionel Sambuc   }
202f4a2713aSLionel Sambuc 
203f4a2713aSLionel Sambuc   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
204f4a2713aSLionel Sambuc     const NamedDecl *ND = DR->getDecl();
205*0a6a1f1dSLionel Sambuc     return ND == x || ND == y ? DR : nullptr;
206f4a2713aSLionel Sambuc   }
207f4a2713aSLionel Sambuc 
208f4a2713aSLionel Sambuc   if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
209f4a2713aSLionel Sambuc     return U->isIncrementDecrementOp()
210*0a6a1f1dSLionel Sambuc       ? getIncrementedVar(U->getSubExpr(), x, y) : nullptr;
211f4a2713aSLionel Sambuc 
212*0a6a1f1dSLionel Sambuc   return nullptr;
213f4a2713aSLionel Sambuc }
214f4a2713aSLionel Sambuc 
215f4a2713aSLionel Sambuc /// CheckLoopConditionForFloat - This check looks for 'for' statements that
216f4a2713aSLionel Sambuc ///  use a floating point variable as a loop counter.
217f4a2713aSLionel Sambuc ///  CERT: FLP30-C, FLP30-CPP.
218f4a2713aSLionel Sambuc ///
checkLoopConditionForFloat(const ForStmt * FS)219f4a2713aSLionel Sambuc void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
220f4a2713aSLionel Sambuc   if (!filter.check_FloatLoopCounter)
221f4a2713aSLionel Sambuc     return;
222f4a2713aSLionel Sambuc 
223f4a2713aSLionel Sambuc   // Does the loop have a condition?
224f4a2713aSLionel Sambuc   const Expr *condition = FS->getCond();
225f4a2713aSLionel Sambuc 
226f4a2713aSLionel Sambuc   if (!condition)
227f4a2713aSLionel Sambuc     return;
228f4a2713aSLionel Sambuc 
229f4a2713aSLionel Sambuc   // Does the loop have an increment?
230f4a2713aSLionel Sambuc   const Expr *increment = FS->getInc();
231f4a2713aSLionel Sambuc 
232f4a2713aSLionel Sambuc   if (!increment)
233f4a2713aSLionel Sambuc     return;
234f4a2713aSLionel Sambuc 
235f4a2713aSLionel Sambuc   // Strip away '()' and casts.
236f4a2713aSLionel Sambuc   condition = condition->IgnoreParenCasts();
237f4a2713aSLionel Sambuc   increment = increment->IgnoreParenCasts();
238f4a2713aSLionel Sambuc 
239f4a2713aSLionel Sambuc   // Is the loop condition a comparison?
240f4a2713aSLionel Sambuc   const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
241f4a2713aSLionel Sambuc 
242f4a2713aSLionel Sambuc   if (!B)
243f4a2713aSLionel Sambuc     return;
244f4a2713aSLionel Sambuc 
245f4a2713aSLionel Sambuc   // Is this a comparison?
246f4a2713aSLionel Sambuc   if (!(B->isRelationalOp() || B->isEqualityOp()))
247f4a2713aSLionel Sambuc     return;
248f4a2713aSLionel Sambuc 
249f4a2713aSLionel Sambuc   // Are we comparing variables?
250f4a2713aSLionel Sambuc   const DeclRefExpr *drLHS =
251f4a2713aSLionel Sambuc     dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
252f4a2713aSLionel Sambuc   const DeclRefExpr *drRHS =
253f4a2713aSLionel Sambuc     dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
254f4a2713aSLionel Sambuc 
255f4a2713aSLionel Sambuc   // Does at least one of the variables have a floating point type?
256*0a6a1f1dSLionel Sambuc   drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : nullptr;
257*0a6a1f1dSLionel Sambuc   drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : nullptr;
258f4a2713aSLionel Sambuc 
259f4a2713aSLionel Sambuc   if (!drLHS && !drRHS)
260f4a2713aSLionel Sambuc     return;
261f4a2713aSLionel Sambuc 
262*0a6a1f1dSLionel Sambuc   const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : nullptr;
263*0a6a1f1dSLionel Sambuc   const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : nullptr;
264f4a2713aSLionel Sambuc 
265f4a2713aSLionel Sambuc   if (!vdLHS && !vdRHS)
266f4a2713aSLionel Sambuc     return;
267f4a2713aSLionel Sambuc 
268f4a2713aSLionel Sambuc   // Does either variable appear in increment?
269f4a2713aSLionel Sambuc   const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
270f4a2713aSLionel Sambuc 
271f4a2713aSLionel Sambuc   if (!drInc)
272f4a2713aSLionel Sambuc     return;
273f4a2713aSLionel Sambuc 
274f4a2713aSLionel Sambuc   // Emit the error.  First figure out which DeclRefExpr in the condition
275f4a2713aSLionel Sambuc   // referenced the compared variable.
276f4a2713aSLionel Sambuc   assert(drInc->getDecl());
277f4a2713aSLionel Sambuc   const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
278f4a2713aSLionel Sambuc 
279f4a2713aSLionel Sambuc   SmallVector<SourceRange, 2> ranges;
280f4a2713aSLionel Sambuc   SmallString<256> sbuf;
281f4a2713aSLionel Sambuc   llvm::raw_svector_ostream os(sbuf);
282f4a2713aSLionel Sambuc 
283f4a2713aSLionel Sambuc   os << "Variable '" << drCond->getDecl()->getName()
284f4a2713aSLionel Sambuc      << "' with floating point type '" << drCond->getType().getAsString()
285f4a2713aSLionel Sambuc      << "' should not be used as a loop counter";
286f4a2713aSLionel Sambuc 
287f4a2713aSLionel Sambuc   ranges.push_back(drCond->getSourceRange());
288f4a2713aSLionel Sambuc   ranges.push_back(drInc->getSourceRange());
289f4a2713aSLionel Sambuc 
290f4a2713aSLionel Sambuc   const char *bugType = "Floating point variable used as loop counter";
291f4a2713aSLionel Sambuc 
292f4a2713aSLionel Sambuc   PathDiagnosticLocation FSLoc =
293f4a2713aSLionel Sambuc     PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
294*0a6a1f1dSLionel Sambuc   BR.EmitBasicReport(AC->getDecl(), filter.checkName_FloatLoopCounter,
295f4a2713aSLionel Sambuc                      bugType, "Security", os.str(),
296f4a2713aSLionel Sambuc                      FSLoc, ranges);
297f4a2713aSLionel Sambuc }
298f4a2713aSLionel Sambuc 
299f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
300f4a2713aSLionel Sambuc // Check: Any use of 'gets' is insecure.
301f4a2713aSLionel Sambuc // Originally: <rdar://problem/6335715>
302f4a2713aSLionel Sambuc // Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
303f4a2713aSLionel Sambuc // CWE-242: Use of Inherently Dangerous Function
304f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
305f4a2713aSLionel Sambuc 
checkCall_gets(const CallExpr * CE,const FunctionDecl * FD)306f4a2713aSLionel Sambuc void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
307f4a2713aSLionel Sambuc   if (!filter.check_gets)
308f4a2713aSLionel Sambuc     return;
309f4a2713aSLionel Sambuc 
310f4a2713aSLionel Sambuc   const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
311f4a2713aSLionel Sambuc   if (!FPT)
312f4a2713aSLionel Sambuc     return;
313f4a2713aSLionel Sambuc 
314f4a2713aSLionel Sambuc   // Verify that the function takes a single argument.
315*0a6a1f1dSLionel Sambuc   if (FPT->getNumParams() != 1)
316f4a2713aSLionel Sambuc     return;
317f4a2713aSLionel Sambuc 
318f4a2713aSLionel Sambuc   // Is the argument a 'char*'?
319*0a6a1f1dSLionel Sambuc   const PointerType *PT = FPT->getParamType(0)->getAs<PointerType>();
320f4a2713aSLionel Sambuc   if (!PT)
321f4a2713aSLionel Sambuc     return;
322f4a2713aSLionel Sambuc 
323f4a2713aSLionel Sambuc   if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
324f4a2713aSLionel Sambuc     return;
325f4a2713aSLionel Sambuc 
326f4a2713aSLionel Sambuc   // Issue a warning.
327f4a2713aSLionel Sambuc   PathDiagnosticLocation CELoc =
328f4a2713aSLionel Sambuc     PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
329*0a6a1f1dSLionel Sambuc   BR.EmitBasicReport(AC->getDecl(), filter.checkName_gets,
330f4a2713aSLionel Sambuc                      "Potential buffer overflow in call to 'gets'",
331f4a2713aSLionel Sambuc                      "Security",
332f4a2713aSLionel Sambuc                      "Call to function 'gets' is extremely insecure as it can "
333f4a2713aSLionel Sambuc                      "always result in a buffer overflow",
334f4a2713aSLionel Sambuc                      CELoc, CE->getCallee()->getSourceRange());
335f4a2713aSLionel Sambuc }
336f4a2713aSLionel Sambuc 
337f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
338f4a2713aSLionel Sambuc // Check: Any use of 'getpwd' is insecure.
339f4a2713aSLionel Sambuc // CWE-477: Use of Obsolete Functions
340f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
341f4a2713aSLionel Sambuc 
checkCall_getpw(const CallExpr * CE,const FunctionDecl * FD)342f4a2713aSLionel Sambuc void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
343f4a2713aSLionel Sambuc   if (!filter.check_getpw)
344f4a2713aSLionel Sambuc     return;
345f4a2713aSLionel Sambuc 
346f4a2713aSLionel Sambuc   const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
347f4a2713aSLionel Sambuc   if (!FPT)
348f4a2713aSLionel Sambuc     return;
349f4a2713aSLionel Sambuc 
350f4a2713aSLionel Sambuc   // Verify that the function takes two arguments.
351*0a6a1f1dSLionel Sambuc   if (FPT->getNumParams() != 2)
352f4a2713aSLionel Sambuc     return;
353f4a2713aSLionel Sambuc 
354f4a2713aSLionel Sambuc   // Verify the first argument type is integer.
355*0a6a1f1dSLionel Sambuc   if (!FPT->getParamType(0)->isIntegralOrUnscopedEnumerationType())
356f4a2713aSLionel Sambuc     return;
357f4a2713aSLionel Sambuc 
358f4a2713aSLionel Sambuc   // Verify the second argument type is char*.
359*0a6a1f1dSLionel Sambuc   const PointerType *PT = FPT->getParamType(1)->getAs<PointerType>();
360f4a2713aSLionel Sambuc   if (!PT)
361f4a2713aSLionel Sambuc     return;
362f4a2713aSLionel Sambuc 
363f4a2713aSLionel Sambuc   if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
364f4a2713aSLionel Sambuc     return;
365f4a2713aSLionel Sambuc 
366f4a2713aSLionel Sambuc   // Issue a warning.
367f4a2713aSLionel Sambuc   PathDiagnosticLocation CELoc =
368f4a2713aSLionel Sambuc     PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
369*0a6a1f1dSLionel Sambuc   BR.EmitBasicReport(AC->getDecl(), filter.checkName_getpw,
370f4a2713aSLionel Sambuc                      "Potential buffer overflow in call to 'getpw'",
371f4a2713aSLionel Sambuc                      "Security",
372f4a2713aSLionel Sambuc                      "The getpw() function is dangerous as it may overflow the "
373f4a2713aSLionel Sambuc                      "provided buffer. It is obsoleted by getpwuid().",
374f4a2713aSLionel Sambuc                      CELoc, CE->getCallee()->getSourceRange());
375f4a2713aSLionel Sambuc }
376f4a2713aSLionel Sambuc 
377f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
378f4a2713aSLionel Sambuc // Check: Any use of 'mktemp' is insecure.  It is obsoleted by mkstemp().
379f4a2713aSLionel Sambuc // CWE-377: Insecure Temporary File
380f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
381f4a2713aSLionel Sambuc 
checkCall_mktemp(const CallExpr * CE,const FunctionDecl * FD)382f4a2713aSLionel Sambuc void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
383f4a2713aSLionel Sambuc   if (!filter.check_mktemp) {
384f4a2713aSLionel Sambuc     // Fall back to the security check of looking for enough 'X's in the
385f4a2713aSLionel Sambuc     // format string, since that is a less severe warning.
386f4a2713aSLionel Sambuc     checkCall_mkstemp(CE, FD);
387f4a2713aSLionel Sambuc     return;
388f4a2713aSLionel Sambuc   }
389f4a2713aSLionel Sambuc 
390f4a2713aSLionel Sambuc   const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
391f4a2713aSLionel Sambuc   if(!FPT)
392f4a2713aSLionel Sambuc     return;
393f4a2713aSLionel Sambuc 
394f4a2713aSLionel Sambuc   // Verify that the function takes a single argument.
395*0a6a1f1dSLionel Sambuc   if (FPT->getNumParams() != 1)
396f4a2713aSLionel Sambuc     return;
397f4a2713aSLionel Sambuc 
398f4a2713aSLionel Sambuc   // Verify that the argument is Pointer Type.
399*0a6a1f1dSLionel Sambuc   const PointerType *PT = FPT->getParamType(0)->getAs<PointerType>();
400f4a2713aSLionel Sambuc   if (!PT)
401f4a2713aSLionel Sambuc     return;
402f4a2713aSLionel Sambuc 
403f4a2713aSLionel Sambuc   // Verify that the argument is a 'char*'.
404f4a2713aSLionel Sambuc   if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
405f4a2713aSLionel Sambuc     return;
406f4a2713aSLionel Sambuc 
407*0a6a1f1dSLionel Sambuc   // Issue a warning.
408f4a2713aSLionel Sambuc   PathDiagnosticLocation CELoc =
409f4a2713aSLionel Sambuc     PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
410*0a6a1f1dSLionel Sambuc   BR.EmitBasicReport(AC->getDecl(), filter.checkName_mktemp,
411f4a2713aSLionel Sambuc                      "Potential insecure temporary file in call 'mktemp'",
412f4a2713aSLionel Sambuc                      "Security",
413f4a2713aSLionel Sambuc                      "Call to function 'mktemp' is insecure as it always "
414f4a2713aSLionel Sambuc                      "creates or uses insecure temporary file.  Use 'mkstemp' "
415f4a2713aSLionel Sambuc                      "instead",
416f4a2713aSLionel Sambuc                      CELoc, CE->getCallee()->getSourceRange());
417f4a2713aSLionel Sambuc }
418f4a2713aSLionel Sambuc 
419f4a2713aSLionel Sambuc 
420f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
421f4a2713aSLionel Sambuc // Check: Use of 'mkstemp', 'mktemp', 'mkdtemp' should contain at least 6 X's.
422f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
423f4a2713aSLionel Sambuc 
checkCall_mkstemp(const CallExpr * CE,const FunctionDecl * FD)424f4a2713aSLionel Sambuc void WalkAST::checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD) {
425f4a2713aSLionel Sambuc   if (!filter.check_mkstemp)
426f4a2713aSLionel Sambuc     return;
427f4a2713aSLionel Sambuc 
428f4a2713aSLionel Sambuc   StringRef Name = FD->getIdentifier()->getName();
429f4a2713aSLionel Sambuc   std::pair<signed, signed> ArgSuffix =
430f4a2713aSLionel Sambuc     llvm::StringSwitch<std::pair<signed, signed> >(Name)
431f4a2713aSLionel Sambuc       .Case("mktemp", std::make_pair(0,-1))
432f4a2713aSLionel Sambuc       .Case("mkstemp", std::make_pair(0,-1))
433f4a2713aSLionel Sambuc       .Case("mkdtemp", std::make_pair(0,-1))
434f4a2713aSLionel Sambuc       .Case("mkstemps", std::make_pair(0,1))
435f4a2713aSLionel Sambuc       .Default(std::make_pair(-1, -1));
436f4a2713aSLionel Sambuc 
437f4a2713aSLionel Sambuc   assert(ArgSuffix.first >= 0 && "Unsupported function");
438f4a2713aSLionel Sambuc 
439f4a2713aSLionel Sambuc   // Check if the number of arguments is consistent with out expectations.
440f4a2713aSLionel Sambuc   unsigned numArgs = CE->getNumArgs();
441f4a2713aSLionel Sambuc   if ((signed) numArgs <= ArgSuffix.first)
442f4a2713aSLionel Sambuc     return;
443f4a2713aSLionel Sambuc 
444f4a2713aSLionel Sambuc   const StringLiteral *strArg =
445f4a2713aSLionel Sambuc     dyn_cast<StringLiteral>(CE->getArg((unsigned)ArgSuffix.first)
446f4a2713aSLionel Sambuc                               ->IgnoreParenImpCasts());
447f4a2713aSLionel Sambuc 
448f4a2713aSLionel Sambuc   // Currently we only handle string literals.  It is possible to do better,
449f4a2713aSLionel Sambuc   // either by looking at references to const variables, or by doing real
450f4a2713aSLionel Sambuc   // flow analysis.
451f4a2713aSLionel Sambuc   if (!strArg || strArg->getCharByteWidth() != 1)
452f4a2713aSLionel Sambuc     return;
453f4a2713aSLionel Sambuc 
454f4a2713aSLionel Sambuc   // Count the number of X's, taking into account a possible cutoff suffix.
455f4a2713aSLionel Sambuc   StringRef str = strArg->getString();
456f4a2713aSLionel Sambuc   unsigned numX = 0;
457f4a2713aSLionel Sambuc   unsigned n = str.size();
458f4a2713aSLionel Sambuc 
459f4a2713aSLionel Sambuc   // Take into account the suffix.
460f4a2713aSLionel Sambuc   unsigned suffix = 0;
461f4a2713aSLionel Sambuc   if (ArgSuffix.second >= 0) {
462f4a2713aSLionel Sambuc     const Expr *suffixEx = CE->getArg((unsigned)ArgSuffix.second);
463f4a2713aSLionel Sambuc     llvm::APSInt Result;
464f4a2713aSLionel Sambuc     if (!suffixEx->EvaluateAsInt(Result, BR.getContext()))
465f4a2713aSLionel Sambuc       return;
466f4a2713aSLionel Sambuc     // FIXME: Issue a warning.
467f4a2713aSLionel Sambuc     if (Result.isNegative())
468f4a2713aSLionel Sambuc       return;
469f4a2713aSLionel Sambuc     suffix = (unsigned) Result.getZExtValue();
470f4a2713aSLionel Sambuc     n = (n > suffix) ? n - suffix : 0;
471f4a2713aSLionel Sambuc   }
472f4a2713aSLionel Sambuc 
473f4a2713aSLionel Sambuc   for (unsigned i = 0; i < n; ++i)
474f4a2713aSLionel Sambuc     if (str[i] == 'X') ++numX;
475f4a2713aSLionel Sambuc 
476f4a2713aSLionel Sambuc   if (numX >= 6)
477f4a2713aSLionel Sambuc     return;
478f4a2713aSLionel Sambuc 
479f4a2713aSLionel Sambuc   // Issue a warning.
480f4a2713aSLionel Sambuc   PathDiagnosticLocation CELoc =
481f4a2713aSLionel Sambuc     PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
482f4a2713aSLionel Sambuc   SmallString<512> buf;
483f4a2713aSLionel Sambuc   llvm::raw_svector_ostream out(buf);
484f4a2713aSLionel Sambuc   out << "Call to '" << Name << "' should have at least 6 'X's in the"
485f4a2713aSLionel Sambuc     " format string to be secure (" << numX << " 'X'";
486f4a2713aSLionel Sambuc   if (numX != 1)
487f4a2713aSLionel Sambuc     out << 's';
488f4a2713aSLionel Sambuc   out << " seen";
489f4a2713aSLionel Sambuc   if (suffix) {
490f4a2713aSLionel Sambuc     out << ", " << suffix << " character";
491f4a2713aSLionel Sambuc     if (suffix > 1)
492f4a2713aSLionel Sambuc       out << 's';
493f4a2713aSLionel Sambuc     out << " used as a suffix";
494f4a2713aSLionel Sambuc   }
495f4a2713aSLionel Sambuc   out << ')';
496*0a6a1f1dSLionel Sambuc   BR.EmitBasicReport(AC->getDecl(), filter.checkName_mkstemp,
497f4a2713aSLionel Sambuc                      "Insecure temporary file creation", "Security",
498f4a2713aSLionel Sambuc                      out.str(), CELoc, strArg->getSourceRange());
499f4a2713aSLionel Sambuc }
500f4a2713aSLionel Sambuc 
501f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
502f4a2713aSLionel Sambuc // Check: Any use of 'strcpy' is insecure.
503f4a2713aSLionel Sambuc //
504f4a2713aSLionel Sambuc // CWE-119: Improper Restriction of Operations within
505f4a2713aSLionel Sambuc // the Bounds of a Memory Buffer
506f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
checkCall_strcpy(const CallExpr * CE,const FunctionDecl * FD)507f4a2713aSLionel Sambuc void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
508f4a2713aSLionel Sambuc   if (!filter.check_strcpy)
509f4a2713aSLionel Sambuc     return;
510f4a2713aSLionel Sambuc 
511f4a2713aSLionel Sambuc   if (!checkCall_strCommon(CE, FD))
512f4a2713aSLionel Sambuc     return;
513f4a2713aSLionel Sambuc 
514f4a2713aSLionel Sambuc   // Issue a warning.
515f4a2713aSLionel Sambuc   PathDiagnosticLocation CELoc =
516f4a2713aSLionel Sambuc     PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
517*0a6a1f1dSLionel Sambuc   BR.EmitBasicReport(AC->getDecl(), filter.checkName_strcpy,
518f4a2713aSLionel Sambuc                      "Potential insecure memory buffer bounds restriction in "
519f4a2713aSLionel Sambuc                      "call 'strcpy'",
520f4a2713aSLionel Sambuc                      "Security",
521f4a2713aSLionel Sambuc                      "Call to function 'strcpy' is insecure as it does not "
522f4a2713aSLionel Sambuc                      "provide bounding of the memory buffer. Replace "
523f4a2713aSLionel Sambuc                      "unbounded copy functions with analogous functions that "
524f4a2713aSLionel Sambuc                      "support length arguments such as 'strlcpy'. CWE-119.",
525f4a2713aSLionel Sambuc                      CELoc, CE->getCallee()->getSourceRange());
526f4a2713aSLionel Sambuc }
527f4a2713aSLionel Sambuc 
528f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
529f4a2713aSLionel Sambuc // Check: Any use of 'strcat' is insecure.
530f4a2713aSLionel Sambuc //
531f4a2713aSLionel Sambuc // CWE-119: Improper Restriction of Operations within
532f4a2713aSLionel Sambuc // the Bounds of a Memory Buffer
533f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
checkCall_strcat(const CallExpr * CE,const FunctionDecl * FD)534f4a2713aSLionel Sambuc void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
535f4a2713aSLionel Sambuc   if (!filter.check_strcpy)
536f4a2713aSLionel Sambuc     return;
537f4a2713aSLionel Sambuc 
538f4a2713aSLionel Sambuc   if (!checkCall_strCommon(CE, FD))
539f4a2713aSLionel Sambuc     return;
540f4a2713aSLionel Sambuc 
541f4a2713aSLionel Sambuc   // Issue a warning.
542f4a2713aSLionel Sambuc   PathDiagnosticLocation CELoc =
543f4a2713aSLionel Sambuc     PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
544*0a6a1f1dSLionel Sambuc   BR.EmitBasicReport(AC->getDecl(), filter.checkName_strcpy,
545f4a2713aSLionel Sambuc                      "Potential insecure memory buffer bounds restriction in "
546f4a2713aSLionel Sambuc                      "call 'strcat'",
547f4a2713aSLionel Sambuc                      "Security",
548f4a2713aSLionel Sambuc                      "Call to function 'strcat' is insecure as it does not "
549f4a2713aSLionel Sambuc                      "provide bounding of the memory buffer. Replace "
550f4a2713aSLionel Sambuc                      "unbounded copy functions with analogous functions that "
551f4a2713aSLionel Sambuc                      "support length arguments such as 'strlcat'. CWE-119.",
552f4a2713aSLionel Sambuc                      CELoc, CE->getCallee()->getSourceRange());
553f4a2713aSLionel Sambuc }
554f4a2713aSLionel Sambuc 
555f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
556f4a2713aSLionel Sambuc // Common check for str* functions with no bounds parameters.
557f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
checkCall_strCommon(const CallExpr * CE,const FunctionDecl * FD)558f4a2713aSLionel Sambuc bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
559f4a2713aSLionel Sambuc   const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>();
560f4a2713aSLionel Sambuc   if (!FPT)
561f4a2713aSLionel Sambuc     return false;
562f4a2713aSLionel Sambuc 
563f4a2713aSLionel Sambuc   // Verify the function takes two arguments, three in the _chk version.
564*0a6a1f1dSLionel Sambuc   int numArgs = FPT->getNumParams();
565f4a2713aSLionel Sambuc   if (numArgs != 2 && numArgs != 3)
566f4a2713aSLionel Sambuc     return false;
567f4a2713aSLionel Sambuc 
568f4a2713aSLionel Sambuc   // Verify the type for both arguments.
569f4a2713aSLionel Sambuc   for (int i = 0; i < 2; i++) {
570f4a2713aSLionel Sambuc     // Verify that the arguments are pointers.
571*0a6a1f1dSLionel Sambuc     const PointerType *PT = FPT->getParamType(i)->getAs<PointerType>();
572f4a2713aSLionel Sambuc     if (!PT)
573f4a2713aSLionel Sambuc       return false;
574f4a2713aSLionel Sambuc 
575f4a2713aSLionel Sambuc     // Verify that the argument is a 'char*'.
576f4a2713aSLionel Sambuc     if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
577f4a2713aSLionel Sambuc       return false;
578f4a2713aSLionel Sambuc   }
579f4a2713aSLionel Sambuc 
580f4a2713aSLionel Sambuc   return true;
581f4a2713aSLionel Sambuc }
582f4a2713aSLionel Sambuc 
583f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
584f4a2713aSLionel Sambuc // Check: Linear congruent random number generators should not be used
585f4a2713aSLionel Sambuc // Originally: <rdar://problem/63371000>
586f4a2713aSLionel Sambuc // CWE-338: Use of cryptographically weak prng
587f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
588f4a2713aSLionel Sambuc 
checkCall_rand(const CallExpr * CE,const FunctionDecl * FD)589f4a2713aSLionel Sambuc void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
590f4a2713aSLionel Sambuc   if (!filter.check_rand || !CheckRand)
591f4a2713aSLionel Sambuc     return;
592f4a2713aSLionel Sambuc 
593f4a2713aSLionel Sambuc   const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
594f4a2713aSLionel Sambuc   if (!FTP)
595f4a2713aSLionel Sambuc     return;
596f4a2713aSLionel Sambuc 
597*0a6a1f1dSLionel Sambuc   if (FTP->getNumParams() == 1) {
598f4a2713aSLionel Sambuc     // Is the argument an 'unsigned short *'?
599f4a2713aSLionel Sambuc     // (Actually any integer type is allowed.)
600*0a6a1f1dSLionel Sambuc     const PointerType *PT = FTP->getParamType(0)->getAs<PointerType>();
601f4a2713aSLionel Sambuc     if (!PT)
602f4a2713aSLionel Sambuc       return;
603f4a2713aSLionel Sambuc 
604f4a2713aSLionel Sambuc     if (! PT->getPointeeType()->isIntegralOrUnscopedEnumerationType())
605f4a2713aSLionel Sambuc       return;
606*0a6a1f1dSLionel Sambuc   } else if (FTP->getNumParams() != 0)
607f4a2713aSLionel Sambuc     return;
608f4a2713aSLionel Sambuc 
609f4a2713aSLionel Sambuc   // Issue a warning.
610f4a2713aSLionel Sambuc   SmallString<256> buf1;
611f4a2713aSLionel Sambuc   llvm::raw_svector_ostream os1(buf1);
612f4a2713aSLionel Sambuc   os1 << '\'' << *FD << "' is a poor random number generator";
613f4a2713aSLionel Sambuc 
614f4a2713aSLionel Sambuc   SmallString<256> buf2;
615f4a2713aSLionel Sambuc   llvm::raw_svector_ostream os2(buf2);
616f4a2713aSLionel Sambuc   os2 << "Function '" << *FD
617f4a2713aSLionel Sambuc       << "' is obsolete because it implements a poor random number generator."
618f4a2713aSLionel Sambuc       << "  Use 'arc4random' instead";
619f4a2713aSLionel Sambuc 
620f4a2713aSLionel Sambuc   PathDiagnosticLocation CELoc =
621f4a2713aSLionel Sambuc     PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
622*0a6a1f1dSLionel Sambuc   BR.EmitBasicReport(AC->getDecl(), filter.checkName_rand, os1.str(),
623*0a6a1f1dSLionel Sambuc                      "Security", os2.str(), CELoc,
624*0a6a1f1dSLionel Sambuc                      CE->getCallee()->getSourceRange());
625f4a2713aSLionel Sambuc }
626f4a2713aSLionel Sambuc 
627f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
628f4a2713aSLionel Sambuc // Check: 'random' should not be used
629f4a2713aSLionel Sambuc // Originally: <rdar://problem/63371000>
630f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
631f4a2713aSLionel Sambuc 
checkCall_random(const CallExpr * CE,const FunctionDecl * FD)632f4a2713aSLionel Sambuc void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
633f4a2713aSLionel Sambuc   if (!CheckRand || !filter.check_rand)
634f4a2713aSLionel Sambuc     return;
635f4a2713aSLionel Sambuc 
636f4a2713aSLionel Sambuc   const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
637f4a2713aSLionel Sambuc   if (!FTP)
638f4a2713aSLionel Sambuc     return;
639f4a2713aSLionel Sambuc 
640f4a2713aSLionel Sambuc   // Verify that the function takes no argument.
641*0a6a1f1dSLionel Sambuc   if (FTP->getNumParams() != 0)
642f4a2713aSLionel Sambuc     return;
643f4a2713aSLionel Sambuc 
644f4a2713aSLionel Sambuc   // Issue a warning.
645f4a2713aSLionel Sambuc   PathDiagnosticLocation CELoc =
646f4a2713aSLionel Sambuc     PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
647*0a6a1f1dSLionel Sambuc   BR.EmitBasicReport(AC->getDecl(), filter.checkName_rand,
648f4a2713aSLionel Sambuc                      "'random' is not a secure random number generator",
649f4a2713aSLionel Sambuc                      "Security",
650f4a2713aSLionel Sambuc                      "The 'random' function produces a sequence of values that "
651f4a2713aSLionel Sambuc                      "an adversary may be able to predict.  Use 'arc4random' "
652f4a2713aSLionel Sambuc                      "instead", CELoc, CE->getCallee()->getSourceRange());
653f4a2713aSLionel Sambuc }
654f4a2713aSLionel Sambuc 
655f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
656f4a2713aSLionel Sambuc // Check: 'vfork' should not be used.
657f4a2713aSLionel Sambuc // POS33-C: Do not use vfork().
658f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
659f4a2713aSLionel Sambuc 
checkCall_vfork(const CallExpr * CE,const FunctionDecl * FD)660f4a2713aSLionel Sambuc void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
661f4a2713aSLionel Sambuc   if (!filter.check_vfork)
662f4a2713aSLionel Sambuc     return;
663f4a2713aSLionel Sambuc 
664f4a2713aSLionel Sambuc   // All calls to vfork() are insecure, issue a warning.
665f4a2713aSLionel Sambuc   PathDiagnosticLocation CELoc =
666f4a2713aSLionel Sambuc     PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
667*0a6a1f1dSLionel Sambuc   BR.EmitBasicReport(AC->getDecl(), filter.checkName_vfork,
668f4a2713aSLionel Sambuc                      "Potential insecure implementation-specific behavior in "
669f4a2713aSLionel Sambuc                      "call 'vfork'",
670f4a2713aSLionel Sambuc                      "Security",
671f4a2713aSLionel Sambuc                      "Call to function 'vfork' is insecure as it can lead to "
672f4a2713aSLionel Sambuc                      "denial of service situations in the parent process. "
673f4a2713aSLionel Sambuc                      "Replace calls to vfork with calls to the safer "
674f4a2713aSLionel Sambuc                      "'posix_spawn' function",
675f4a2713aSLionel Sambuc                      CELoc, CE->getCallee()->getSourceRange());
676f4a2713aSLionel Sambuc }
677f4a2713aSLionel Sambuc 
678f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
679f4a2713aSLionel Sambuc // Check: Should check whether privileges are dropped successfully.
680f4a2713aSLionel Sambuc // Originally: <rdar://problem/6337132>
681f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
682f4a2713aSLionel Sambuc 
checkUncheckedReturnValue(CallExpr * CE)683f4a2713aSLionel Sambuc void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
684f4a2713aSLionel Sambuc   if (!filter.check_UncheckedReturn)
685f4a2713aSLionel Sambuc     return;
686f4a2713aSLionel Sambuc 
687f4a2713aSLionel Sambuc   const FunctionDecl *FD = CE->getDirectCallee();
688f4a2713aSLionel Sambuc   if (!FD)
689f4a2713aSLionel Sambuc     return;
690f4a2713aSLionel Sambuc 
691*0a6a1f1dSLionel Sambuc   if (II_setid[0] == nullptr) {
692f4a2713aSLionel Sambuc     static const char * const identifiers[num_setids] = {
693f4a2713aSLionel Sambuc       "setuid", "setgid", "seteuid", "setegid",
694f4a2713aSLionel Sambuc       "setreuid", "setregid"
695f4a2713aSLionel Sambuc     };
696f4a2713aSLionel Sambuc 
697f4a2713aSLionel Sambuc     for (size_t i = 0; i < num_setids; i++)
698f4a2713aSLionel Sambuc       II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
699f4a2713aSLionel Sambuc   }
700f4a2713aSLionel Sambuc 
701f4a2713aSLionel Sambuc   const IdentifierInfo *id = FD->getIdentifier();
702f4a2713aSLionel Sambuc   size_t identifierid;
703f4a2713aSLionel Sambuc 
704f4a2713aSLionel Sambuc   for (identifierid = 0; identifierid < num_setids; identifierid++)
705f4a2713aSLionel Sambuc     if (id == II_setid[identifierid])
706f4a2713aSLionel Sambuc       break;
707f4a2713aSLionel Sambuc 
708f4a2713aSLionel Sambuc   if (identifierid >= num_setids)
709f4a2713aSLionel Sambuc     return;
710f4a2713aSLionel Sambuc 
711f4a2713aSLionel Sambuc   const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>();
712f4a2713aSLionel Sambuc   if (!FTP)
713f4a2713aSLionel Sambuc     return;
714f4a2713aSLionel Sambuc 
715f4a2713aSLionel Sambuc   // Verify that the function takes one or two arguments (depending on
716f4a2713aSLionel Sambuc   //   the function).
717*0a6a1f1dSLionel Sambuc   if (FTP->getNumParams() != (identifierid < 4 ? 1 : 2))
718f4a2713aSLionel Sambuc     return;
719f4a2713aSLionel Sambuc 
720f4a2713aSLionel Sambuc   // The arguments must be integers.
721*0a6a1f1dSLionel Sambuc   for (unsigned i = 0; i < FTP->getNumParams(); i++)
722*0a6a1f1dSLionel Sambuc     if (!FTP->getParamType(i)->isIntegralOrUnscopedEnumerationType())
723f4a2713aSLionel Sambuc       return;
724f4a2713aSLionel Sambuc 
725f4a2713aSLionel Sambuc   // Issue a warning.
726f4a2713aSLionel Sambuc   SmallString<256> buf1;
727f4a2713aSLionel Sambuc   llvm::raw_svector_ostream os1(buf1);
728f4a2713aSLionel Sambuc   os1 << "Return value is not checked in call to '" << *FD << '\'';
729f4a2713aSLionel Sambuc 
730f4a2713aSLionel Sambuc   SmallString<256> buf2;
731f4a2713aSLionel Sambuc   llvm::raw_svector_ostream os2(buf2);
732f4a2713aSLionel Sambuc   os2 << "The return value from the call to '" << *FD
733f4a2713aSLionel Sambuc       << "' is not checked.  If an error occurs in '" << *FD
734f4a2713aSLionel Sambuc       << "', the following code may execute with unexpected privileges";
735f4a2713aSLionel Sambuc 
736f4a2713aSLionel Sambuc   PathDiagnosticLocation CELoc =
737f4a2713aSLionel Sambuc     PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
738*0a6a1f1dSLionel Sambuc   BR.EmitBasicReport(AC->getDecl(), filter.checkName_UncheckedReturn, os1.str(),
739*0a6a1f1dSLionel Sambuc                      "Security", os2.str(), CELoc,
740*0a6a1f1dSLionel Sambuc                      CE->getCallee()->getSourceRange());
741f4a2713aSLionel Sambuc }
742f4a2713aSLionel Sambuc 
743f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
744f4a2713aSLionel Sambuc // SecuritySyntaxChecker
745f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
746f4a2713aSLionel Sambuc 
747f4a2713aSLionel Sambuc namespace {
748f4a2713aSLionel Sambuc class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
749f4a2713aSLionel Sambuc public:
750f4a2713aSLionel Sambuc   ChecksFilter filter;
751f4a2713aSLionel Sambuc 
checkASTCodeBody(const Decl * D,AnalysisManager & mgr,BugReporter & BR) const752f4a2713aSLionel Sambuc   void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
753f4a2713aSLionel Sambuc                         BugReporter &BR) const {
754f4a2713aSLionel Sambuc     WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter);
755f4a2713aSLionel Sambuc     walker.Visit(D->getBody());
756f4a2713aSLionel Sambuc   }
757f4a2713aSLionel Sambuc };
758f4a2713aSLionel Sambuc }
759f4a2713aSLionel Sambuc 
760f4a2713aSLionel Sambuc #define REGISTER_CHECKER(name)                                                 \
761f4a2713aSLionel Sambuc   void ento::register##name(CheckerManager &mgr) {                             \
762*0a6a1f1dSLionel Sambuc     SecuritySyntaxChecker *checker =                                           \
763*0a6a1f1dSLionel Sambuc         mgr.registerChecker<SecuritySyntaxChecker>();                          \
764*0a6a1f1dSLionel Sambuc     checker->filter.check_##name = true;                                       \
765*0a6a1f1dSLionel Sambuc     checker->filter.checkName_##name = mgr.getCurrentCheckName();              \
766f4a2713aSLionel Sambuc   }
767f4a2713aSLionel Sambuc 
768f4a2713aSLionel Sambuc REGISTER_CHECKER(gets)
769f4a2713aSLionel Sambuc REGISTER_CHECKER(getpw)
770f4a2713aSLionel Sambuc REGISTER_CHECKER(mkstemp)
771f4a2713aSLionel Sambuc REGISTER_CHECKER(mktemp)
772f4a2713aSLionel Sambuc REGISTER_CHECKER(strcpy)
773f4a2713aSLionel Sambuc REGISTER_CHECKER(rand)
774f4a2713aSLionel Sambuc REGISTER_CHECKER(vfork)
775f4a2713aSLionel Sambuc REGISTER_CHECKER(FloatLoopCounter)
776f4a2713aSLionel Sambuc REGISTER_CHECKER(UncheckedReturn)
777f4a2713aSLionel Sambuc 
778f4a2713aSLionel Sambuc 
779