blob: 20c045fe6b2c0ff95dc3f236289e3af3ef9d86d5 [file] [log] [blame]
Ted Kremenekdbfb5f82009-07-23 01:07:19 +00001//==- CheckSecuritySyntaxOnly.cpp - Basic security checks --------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a set of flow-insensitive security checks.
11//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +000014#include "ClangSACheckers.h"
Anna Zaks590dd8e2011-09-20 21:38:35 +000015#include "clang/Analysis/AnalysisContext.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/Basic/TargetInfo.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Anna Zaks590dd8e2011-09-20 21:38:35 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Lenny Maioranic2dace12011-04-03 05:07:11 +000021#include "llvm/ADT/StringSwitch.h"
Anna Zaks590dd8e2011-09-20 21:38:35 +000022#include "llvm/Support/raw_ostream.h"
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000023
24using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000025using namespace ento;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000026
Ted Kremenek88c8bc82010-01-15 08:20:31 +000027static bool isArc4RandomAvailable(const ASTContext &Ctx) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000028 const llvm::Triple &T = Ctx.getTargetInfo().getTriple();
Ted Kremenek88c8bc82010-01-15 08:20:31 +000029 return T.getVendor() == llvm::Triple::Apple ||
Douglas Gregor0f565592011-01-17 19:16:24 +000030 T.getOS() == llvm::Triple::FreeBSD ||
31 T.getOS() == llvm::Triple::NetBSD ||
32 T.getOS() == llvm::Triple::OpenBSD ||
33 T.getOS() == llvm::Triple::DragonFly;
Ted Kremenek88c8bc82010-01-15 08:20:31 +000034}
35
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000036namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000037class WalkAST : public StmtVisitor<WalkAST> {
Mike Stump1eb44332009-09-09 15:08:12 +000038 BugReporter &BR;
Anna Zaks590dd8e2011-09-20 21:38:35 +000039 AnalysisContext* AC;
Ted Kremenek24650472009-09-02 02:47:41 +000040 enum { num_setids = 6 };
41 IdentifierInfo *II_setid[num_setids];
Ted Kremenek2c016762010-03-24 22:39:47 +000042
Ted Kremenek88c8bc82010-01-15 08:20:31 +000043 const bool CheckRand;
Mike Stump1eb44332009-09-09 15:08:12 +000044
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000045public:
Anna Zaks590dd8e2011-09-20 21:38:35 +000046 WalkAST(BugReporter &br, AnalysisContext* ac)
47 : BR(br), AC(ac), II_setid(),
48 CheckRand(isArc4RandomAvailable(BR.getContext())) {}
Mike Stump1eb44332009-09-09 15:08:12 +000049
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000050 // Statement visitor methods.
Ted Kremenekefcbb152009-07-23 22:29:41 +000051 void VisitCallExpr(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000052 void VisitForStmt(ForStmt *S);
Ted Kremenek65a81a92009-08-28 00:08:09 +000053 void VisitCompoundStmt (CompoundStmt *S);
Ted Kremenek8baf86d2009-07-23 21:34:35 +000054 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000055
56 void VisitChildren(Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +000057
Ted Kremenekefcbb152009-07-23 22:29:41 +000058 // Helpers.
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000059 bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +000060
Lenny Maioranic2dace12011-04-03 05:07:11 +000061 typedef void (WalkAST::*FnCheck)(const CallExpr *,
62 const FunctionDecl *);
63
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000064 // Checker-specific methods.
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000065 void checkLoopConditionForFloat(const ForStmt *FS);
66 void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD);
67 void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD);
68 void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD);
69 void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD);
70 void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD);
71 void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD);
72 void checkCall_random(const CallExpr *CE, const FunctionDecl *FD);
Anna Zaksa7957ff2011-10-11 04:34:54 +000073 void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +000074 void checkUncheckedReturnValue(CallExpr *CE);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +000075};
76} // end anonymous namespace
77
78//===----------------------------------------------------------------------===//
79// AST walking.
80//===----------------------------------------------------------------------===//
81
82void WalkAST::VisitChildren(Stmt *S) {
83 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
84 if (Stmt *child = *I)
85 Visit(child);
86}
87
Ted Kremenekefcbb152009-07-23 22:29:41 +000088void WalkAST::VisitCallExpr(CallExpr *CE) {
Lenny Maioranic2dace12011-04-03 05:07:11 +000089 // Get the callee.
90 const FunctionDecl *FD = CE->getDirectCallee();
91
92 if (!FD)
93 return;
94
95 // Get the name of the callee. If it's a builtin, strip off the prefix.
96 IdentifierInfo *II = FD->getIdentifier();
97 if (!II) // if no identifier, not a simple C function
98 return;
Chris Lattner5f9e2722011-07-23 10:55:15 +000099 StringRef Name = II->getName();
Lenny Maioranic2dace12011-04-03 05:07:11 +0000100 if (Name.startswith("__builtin_"))
101 Name = Name.substr(10);
102
103 // Set the evaluation function by switching on the callee name.
104 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000105 .Case("gets", &WalkAST::checkCall_gets)
106 .Case("getpw", &WalkAST::checkCall_getpw)
107 .Case("mktemp", &WalkAST::checkCall_mktemp)
108 .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy)
109 .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat)
110 .Case("drand48", &WalkAST::checkCall_rand)
111 .Case("erand48", &WalkAST::checkCall_rand)
112 .Case("jrand48", &WalkAST::checkCall_rand)
113 .Case("lrand48", &WalkAST::checkCall_rand)
114 .Case("mrand48", &WalkAST::checkCall_rand)
115 .Case("nrand48", &WalkAST::checkCall_rand)
116 .Case("lcong48", &WalkAST::checkCall_rand)
117 .Case("rand", &WalkAST::checkCall_rand)
118 .Case("rand_r", &WalkAST::checkCall_rand)
119 .Case("random", &WalkAST::checkCall_random)
Anna Zaksa7957ff2011-10-11 04:34:54 +0000120 .Case("vfork", &WalkAST::checkCall_vfork)
Lenny Maioranic2dace12011-04-03 05:07:11 +0000121 .Default(NULL);
122
123 // If the callee isn't defined, it is not of security concern.
124 // Check and evaluate the call.
125 if (evalFunction)
126 (this->*evalFunction)(CE, FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenekefcbb152009-07-23 22:29:41 +0000128 // Recurse and check children.
129 VisitChildren(CE);
130}
131
Ted Kremenek65a81a92009-08-28 00:08:09 +0000132void WalkAST::VisitCompoundStmt(CompoundStmt *S) {
133 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000134 if (Stmt *child = *I) {
135 if (CallExpr *CE = dyn_cast<CallExpr>(child))
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000136 checkUncheckedReturnValue(CE);
Mike Stump1eb44332009-09-09 15:08:12 +0000137 Visit(child);
138 }
Ted Kremenek65a81a92009-08-28 00:08:09 +0000139}
140
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000141void WalkAST::VisitForStmt(ForStmt *FS) {
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000142 checkLoopConditionForFloat(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000143
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000144 // Recurse and check children.
145 VisitChildren(FS);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000146}
147
148//===----------------------------------------------------------------------===//
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000149// Check: floating poing variable used as loop counter.
Ted Kremenek5abeb522009-07-23 21:44:18 +0000150// Originally: <rdar://problem/6336718>
151// Implements: CERT security coding advisory FLP-30.
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000152//===----------------------------------------------------------------------===//
153
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000154static const DeclRefExpr*
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000155getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000156 expr = expr->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000157
158 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000159 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
John McCall2de56d12010-08-25 11:45:40 +0000160 B->getOpcode() == BO_Comma))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000161 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000163 if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000164 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000166 if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000167 return rhs;
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000169 return NULL;
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000170 }
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000172 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
173 const NamedDecl *ND = DR->getDecl();
174 return ND == x || ND == y ? DR : NULL;
175 }
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000177 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
178 return U->isIncrementDecrementOp()
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000179 ? getIncrementedVar(U->getSubExpr(), x, y) : NULL;
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000180
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000181 return NULL;
182}
183
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000184/// CheckLoopConditionForFloat - This check looks for 'for' statements that
185/// use a floating point variable as a loop counter.
186/// CERT: FLP30-C, FLP30-CPP.
187///
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000188void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000189 // Does the loop have a condition?
190 const Expr *condition = FS->getCond();
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000192 if (!condition)
193 return;
194
195 // Does the loop have an increment?
196 const Expr *increment = FS->getInc();
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000198 if (!increment)
199 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000201 // Strip away '()' and casts.
202 condition = condition->IgnoreParenCasts();
203 increment = increment->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000205 // Is the loop condition a comparison?
206 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
207
208 if (!B)
209 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenekcad9f412009-07-24 20:26:31 +0000211 // Is this a comparison?
212 if (!(B->isRelationalOp() || B->isEqualityOp()))
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000213 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000215 // Are we comparing variables?
John McCallf6a16482010-12-04 03:47:34 +0000216 const DeclRefExpr *drLHS =
217 dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts());
218 const DeclRefExpr *drRHS =
219 dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Ted Kremenekcad9f412009-07-24 20:26:31 +0000221 // Does at least one of the variables have a floating point type?
Douglas Gregor0c293ea2010-06-22 23:07:26 +0000222 drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL;
223 drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000225 if (!drLHS && !drRHS)
226 return;
227
228 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
229 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000231 if (!vdLHS && !vdRHS)
Mike Stump1eb44332009-09-09 15:08:12 +0000232 return;
233
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000234 // Does either variable appear in increment?
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000235 const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000237 if (!drInc)
238 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000240 // Emit the error. First figure out which DeclRefExpr in the condition
241 // referenced the compared variable.
242 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
243
Chris Lattner5f9e2722011-07-23 10:55:15 +0000244 SmallVector<SourceRange, 2> ranges;
Ted Kremenek2c016762010-03-24 22:39:47 +0000245 llvm::SmallString<256> sbuf;
246 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Daniel Dunbar4087f272010-08-17 22:39:59 +0000248 os << "Variable '" << drCond->getDecl()->getName()
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000249 << "' with floating point type '" << drCond->getType().getAsString()
250 << "' should not be used as a loop counter";
251
252 ranges.push_back(drCond->getSourceRange());
253 ranges.push_back(drInc->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Ted Kremenek8baf86d2009-07-23 21:34:35 +0000255 const char *bugType = "Floating point variable used as loop counter";
Anna Zaks590dd8e2011-09-20 21:38:35 +0000256
257 PathDiagnosticLocation FSLoc =
258 PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC);
Benjamin Kramerf0171732009-11-29 18:27:55 +0000259 BR.EmitBasicReport(bugType, "Security", os.str(),
Anna Zaks590dd8e2011-09-20 21:38:35 +0000260 FSLoc, ranges.data(), ranges.size());
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000261}
262
263//===----------------------------------------------------------------------===//
Ted Kremenekefcbb152009-07-23 22:29:41 +0000264// Check: Any use of 'gets' is insecure.
265// Originally: <rdar://problem/6335715>
266// Implements (part of): 300-BSI (buildsecurityin.us-cert.gov)
Zhongxing Xuaa30b3b2009-11-09 08:13:04 +0000267// CWE-242: Use of Inherently Dangerous Function
Ted Kremenekefcbb152009-07-23 22:29:41 +0000268//===----------------------------------------------------------------------===//
269
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000270void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000271 const FunctionProtoType *FPT
272 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000273 if (!FPT)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000274 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Ted Kremenekefcbb152009-07-23 22:29:41 +0000276 // Verify that the function takes a single argument.
Zhongxing Xubd842e32009-11-09 12:19:26 +0000277 if (FPT->getNumArgs() != 1)
Ted Kremenekefcbb152009-07-23 22:29:41 +0000278 return;
279
280 // Is the argument a 'char*'?
Zhongxing Xubd842e32009-11-09 12:19:26 +0000281 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
Ted Kremenekefcbb152009-07-23 22:29:41 +0000282 if (!PT)
283 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Ted Kremenekefcbb152009-07-23 22:29:41 +0000285 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
286 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Ted Kremenekefcbb152009-07-23 22:29:41 +0000288 // Issue a warning.
289 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000290 PathDiagnosticLocation CELoc =
291 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenekefcbb152009-07-23 22:29:41 +0000292 BR.EmitBasicReport("Potential buffer overflow in call to 'gets'",
293 "Security",
294 "Call to function 'gets' is extremely insecure as it can "
295 "always result in a buffer overflow",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000296 CELoc, &R, 1);
Ted Kremenekefcbb152009-07-23 22:29:41 +0000297}
298
299//===----------------------------------------------------------------------===//
Zhongxing Xubd842e32009-11-09 12:19:26 +0000300// Check: Any use of 'getpwd' is insecure.
301// CWE-477: Use of Obsolete Functions
302//===----------------------------------------------------------------------===//
303
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000304void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000305 const FunctionProtoType *FPT
306 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xubd842e32009-11-09 12:19:26 +0000307 if (!FPT)
308 return;
309
310 // Verify that the function takes two arguments.
311 if (FPT->getNumArgs() != 2)
312 return;
313
314 // Verify the first argument type is integer.
315 if (!FPT->getArgType(0)->isIntegerType())
316 return;
317
318 // Verify the second argument type is char*.
319 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1));
320 if (!PT)
321 return;
322
323 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
324 return;
325
326 // Issue a warning.
327 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000328 PathDiagnosticLocation CELoc =
329 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Zhongxing Xubd842e32009-11-09 12:19:26 +0000330 BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'",
331 "Security",
332 "The getpw() function is dangerous as it may overflow the "
333 "provided buffer. It is obsoleted by getpwuid().",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000334 CELoc, &R, 1);
Zhongxing Xubd842e32009-11-09 12:19:26 +0000335}
336
337//===----------------------------------------------------------------------===//
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000338// Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp().
339// CWE-377: Insecure Temporary File
340//===----------------------------------------------------------------------===//
341
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000342void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000343 const FunctionProtoType *FPT
344 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000345 if(!FPT)
346 return;
Ted Kremenek2c016762010-03-24 22:39:47 +0000347
Lenny Maioraniea4411e2011-03-31 21:26:55 +0000348 // Verify that the function takes a single argument.
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000349 if (FPT->getNumArgs() != 1)
350 return;
351
352 // Verify that the argument is Pointer Type.
353 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0));
354 if (!PT)
355 return;
356
357 // Verify that the argument is a 'char*'.
358 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
359 return;
Ted Kremenek431a2cb2010-03-24 22:39:45 +0000360
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000361 // Issue a waring.
362 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000363 PathDiagnosticLocation CELoc =
364 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000365 BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'",
Eli Friedmana7e68452010-08-22 01:00:03 +0000366 "Security",
367 "Call to function 'mktemp' is insecure as it always "
368 "creates or uses insecure temporary file. Use 'mkstemp' instead",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000369 CELoc, &R, 1);
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000370}
371
Zhongxing Xu1bf40562009-12-03 09:15:23 +0000372//===----------------------------------------------------------------------===//
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000373// Check: Any use of 'strcpy' is insecure.
374//
375// CWE-119: Improper Restriction of Operations within
376// the Bounds of a Memory Buffer
377//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000378void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
379 if (!checkCall_strCommon(CE, FD))
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000380 return;
381
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000382 // Issue a warning.
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000383 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000384 PathDiagnosticLocation CELoc =
385 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000386 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000387 "call 'strcpy'",
388 "Security",
389 "Call to function 'strcpy' is insecure as it does not "
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000390 "provide bounding of the memory buffer. Replace "
391 "unbounded copy functions with analogous functions that "
392 "support length arguments such as 'strncpy'. CWE-119.",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000393 CELoc, &R, 1);
Lenny Maiorani5b67a822011-03-31 22:09:14 +0000394}
395
396//===----------------------------------------------------------------------===//
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000397// Check: Any use of 'strcat' is insecure.
398//
399// CWE-119: Improper Restriction of Operations within
400// the Bounds of a Memory Buffer
401//===----------------------------------------------------------------------===//
402void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) {
403 if (!checkCall_strCommon(CE, FD))
404 return;
405
406 // Issue a warning.
407 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000408 PathDiagnosticLocation CELoc =
409 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000410 BR.EmitBasicReport("Potential insecure memory buffer bounds restriction in "
411 "call 'strcat'",
412 "Security",
413 "Call to function 'strcat' is insecure as it does not "
414 "provide bounding of the memory buffer. Replace "
415 "unbounded copy functions with analogous functions that "
416 "support length arguments such as 'strncat'. CWE-119.",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000417 CELoc, &R, 1);
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000418}
419
420//===----------------------------------------------------------------------===//
421// Common check for str* functions with no bounds parameters.
422//===----------------------------------------------------------------------===//
423bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) {
424 const FunctionProtoType *FPT
425 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
426 if (!FPT)
427 return false;
428
429 // Verify the function takes two arguments, three in the _chk version.
430 int numArgs = FPT->getNumArgs();
431 if (numArgs != 2 && numArgs != 3)
432 return false;
433
434 // Verify the type for both arguments.
435 for (int i = 0; i < 2; i++) {
436 // Verify that the arguments are pointers.
437 const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(i));
438 if (!PT)
439 return false;
440
441 // Verify that the argument is a 'char*'.
442 if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy)
443 return false;
444 }
445
446 return true;
447}
448
449//===----------------------------------------------------------------------===//
Ted Kremenek24650472009-09-02 02:47:41 +0000450// Check: Linear congruent random number generators should not be used
451// Originally: <rdar://problem/63371000>
452// CWE-338: Use of cryptographically weak prng
453//===----------------------------------------------------------------------===//
454
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000455void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) {
Lenny Maioranic2dace12011-04-03 05:07:11 +0000456 if (!CheckRand)
Ted Kremenek24650472009-09-02 02:47:41 +0000457 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Abramo Bagnara723df242010-12-14 22:11:44 +0000459 const FunctionProtoType *FTP
460 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000461 if (!FTP)
462 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek24650472009-09-02 02:47:41 +0000464 if (FTP->getNumArgs() == 1) {
465 // Is the argument an 'unsigned short *'?
466 // (Actually any integer type is allowed.)
467 const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0));
468 if (!PT)
469 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Ted Kremenek24650472009-09-02 02:47:41 +0000471 if (! PT->getPointeeType()->isIntegerType())
472 return;
473 }
Mike Stump1eb44332009-09-09 15:08:12 +0000474 else if (FTP->getNumArgs() != 0)
Ted Kremenek24650472009-09-02 02:47:41 +0000475 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Ted Kremenek24650472009-09-02 02:47:41 +0000477 // Issue a warning.
Ted Kremenek2c016762010-03-24 22:39:47 +0000478 llvm::SmallString<256> buf1;
479 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000480 os1 << '\'' << FD << "' is a poor random number generator";
Ted Kremenek24650472009-09-02 02:47:41 +0000481
Ted Kremenek2c016762010-03-24 22:39:47 +0000482 llvm::SmallString<256> buf2;
483 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000484 os2 << "Function '" << FD
Ted Kremenek24650472009-09-02 02:47:41 +0000485 << "' is obsolete because it implements a poor random number generator."
486 << " Use 'arc4random' instead";
487
488 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000489 PathDiagnosticLocation CELoc =
490 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
491 BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000492}
493
494//===----------------------------------------------------------------------===//
495// Check: 'random' should not be used
496// Originally: <rdar://problem/63371000>
497//===----------------------------------------------------------------------===//
498
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000499void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) {
Lenny Maioranic2dace12011-04-03 05:07:11 +0000500 if (!CheckRand)
Ted Kremenek24650472009-09-02 02:47:41 +0000501 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Abramo Bagnara723df242010-12-14 22:11:44 +0000503 const FunctionProtoType *FTP
504 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek24650472009-09-02 02:47:41 +0000505 if (!FTP)
506 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Ted Kremenek24650472009-09-02 02:47:41 +0000508 // Verify that the function takes no argument.
509 if (FTP->getNumArgs() != 0)
510 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Ted Kremenek24650472009-09-02 02:47:41 +0000512 // Issue a warning.
513 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000514 PathDiagnosticLocation CELoc =
515 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
Ted Kremenek24650472009-09-02 02:47:41 +0000516 BR.EmitBasicReport("'random' is not a secure random number generator",
517 "Security",
518 "The 'random' function produces a sequence of values that "
519 "an adversary may be able to predict. Use 'arc4random' "
Anna Zaks590dd8e2011-09-20 21:38:35 +0000520 "instead", CELoc, &R, 1);
Ted Kremenek24650472009-09-02 02:47:41 +0000521}
522
523//===----------------------------------------------------------------------===//
Anna Zaksa7957ff2011-10-11 04:34:54 +0000524// Check: 'vfork' should not be used.
525// POS33-C: Do not use vfork().
526//===----------------------------------------------------------------------===//
527
528void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) {
529 // All calls to vfork() are insecure, issue a warning.
530 SourceRange R = CE->getCallee()->getSourceRange();
531 PathDiagnosticLocation CELoc =
532 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
533 BR.EmitBasicReport("Potential insecure implementation-specific behavior in "
534 "call 'vfork'",
535 "Security",
536 "Call to function 'vfork' is insecure as it can lead to "
537 "denial of service situations in the parent process. "
538 "Replace calls to vfork with calls to the safer "
539 "'posix_spawn' function",
540 CELoc, &R, 1);
541}
542
543//===----------------------------------------------------------------------===//
Ted Kremenek65a81a92009-08-28 00:08:09 +0000544// Check: Should check whether privileges are dropped successfully.
545// Originally: <rdar://problem/6337132>
546//===----------------------------------------------------------------------===//
547
Lenny Maiorani9cb677e2011-04-05 20:18:46 +0000548void WalkAST::checkUncheckedReturnValue(CallExpr *CE) {
Ted Kremenek65a81a92009-08-28 00:08:09 +0000549 const FunctionDecl *FD = CE->getDirectCallee();
550 if (!FD)
551 return;
552
553 if (II_setid[0] == NULL) {
Ted Kremenek24650472009-09-02 02:47:41 +0000554 static const char * const identifiers[num_setids] = {
Ted Kremenek65a81a92009-08-28 00:08:09 +0000555 "setuid", "setgid", "seteuid", "setegid",
556 "setreuid", "setregid"
557 };
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Ted Kremenek24650472009-09-02 02:47:41 +0000559 for (size_t i = 0; i < num_setids; i++)
Mike Stump1eb44332009-09-09 15:08:12 +0000560 II_setid[i] = &BR.getContext().Idents.get(identifiers[i]);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000561 }
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Ted Kremenek65a81a92009-08-28 00:08:09 +0000563 const IdentifierInfo *id = FD->getIdentifier();
564 size_t identifierid;
565
Ted Kremenek24650472009-09-02 02:47:41 +0000566 for (identifierid = 0; identifierid < num_setids; identifierid++)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000567 if (id == II_setid[identifierid])
568 break;
569
Ted Kremenek24650472009-09-02 02:47:41 +0000570 if (identifierid >= num_setids)
Ted Kremenek65a81a92009-08-28 00:08:09 +0000571 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Abramo Bagnara723df242010-12-14 22:11:44 +0000573 const FunctionProtoType *FTP
574 = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens());
Ted Kremenek65a81a92009-08-28 00:08:09 +0000575 if (!FTP)
576 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Ted Kremeneka8187832009-08-28 00:24:55 +0000578 // Verify that the function takes one or two arguments (depending on
579 // the function).
Ted Kremenek65a81a92009-08-28 00:08:09 +0000580 if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2))
581 return;
582
583 // The arguments must be integers.
584 for (unsigned i = 0; i < FTP->getNumArgs(); i++)
585 if (! FTP->getArgType(i)->isIntegerType())
586 return;
587
588 // Issue a warning.
Ted Kremenek2c016762010-03-24 22:39:47 +0000589 llvm::SmallString<256> buf1;
590 llvm::raw_svector_ostream os1(buf1);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000591 os1 << "Return value is not checked in call to '" << FD << '\'';
Ted Kremenek65a81a92009-08-28 00:08:09 +0000592
Ted Kremenek2c016762010-03-24 22:39:47 +0000593 llvm::SmallString<256> buf2;
594 llvm::raw_svector_ostream os2(buf2);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000595 os2 << "The return value from the call to '" << FD
596 << "' is not checked. If an error occurs in '" << FD
Ted Kremenek65a81a92009-08-28 00:08:09 +0000597 << "', the following code may execute with unexpected privileges";
598
599 SourceRange R = CE->getCallee()->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000600 PathDiagnosticLocation CELoc =
601 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
602 BR.EmitBasicReport(os1.str(), "Security", os2.str(), CELoc, &R, 1);
Ted Kremenek65a81a92009-08-28 00:08:09 +0000603}
604
605//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000606// SecuritySyntaxChecker
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000607//===----------------------------------------------------------------------===//
608
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000609namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000610class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000611public:
612 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
613 BugReporter &BR) const {
Anna Zaks590dd8e2011-09-20 21:38:35 +0000614 WalkAST walker(BR, mgr.getAnalysisContext(D));
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000615 walker.Visit(D->getBody());
616 }
617};
618}
619
620void ento::registerSecuritySyntaxChecker(CheckerManager &mgr) {
621 mgr.registerChecker<SecuritySyntaxChecker>();
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000622}