Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 1 | //==- CheckSecuritySyntaxOnly.cpp - Basic security checks --------*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a set of flow-insensitive security checks. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
| 15 | #include "clang/Analysis/LocalCheckers.h" |
| 16 | #include "clang/AST/StmtVisitor.h" |
| 17 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | namespace { |
| 23 | class VISIBILITY_HIDDEN WalkAST : public StmtVisitor<WalkAST> { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 24 | BugReporter &BR; |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 25 | IdentifierInfo *II_gets; |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 26 | enum { num_rands = 9 }; |
| 27 | IdentifierInfo *II_rand[num_rands]; |
| 28 | IdentifierInfo *II_random; |
| 29 | enum { num_setids = 6 }; |
| 30 | IdentifierInfo *II_setid[num_setids]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 31 | |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 32 | public: |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 33 | WalkAST(BugReporter &br) : BR(br), |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 34 | II_gets(0), II_rand(), II_random(0), II_setid() {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 35 | |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 36 | // Statement visitor methods. |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 37 | void VisitCallExpr(CallExpr *CE); |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 38 | void VisitForStmt(ForStmt *S); |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 39 | void VisitCompoundStmt (CompoundStmt *S); |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 40 | void VisitStmt(Stmt *S) { VisitChildren(S); } |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 41 | |
| 42 | void VisitChildren(Stmt *S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 43 | |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 44 | // Helpers. |
| 45 | IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 46 | |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 47 | // Checker-specific methods. |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 48 | void CheckLoopConditionForFloat(const ForStmt *FS); |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 49 | void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD); |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 50 | void CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD); |
| 51 | void CheckCall_random(const CallExpr *CE, const FunctionDecl *FD); |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 52 | void CheckUncheckedReturnValue(CallExpr *CE); |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 53 | }; |
| 54 | } // end anonymous namespace |
| 55 | |
| 56 | //===----------------------------------------------------------------------===// |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 57 | // Helper methods. |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | |
| 60 | IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) { |
| 61 | if (!II) |
| 62 | II = &BR.getContext().Idents.get(str); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 63 | |
| 64 | return II; |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 68 | // AST walking. |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
| 71 | void WalkAST::VisitChildren(Stmt *S) { |
| 72 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) |
| 73 | if (Stmt *child = *I) |
| 74 | Visit(child); |
| 75 | } |
| 76 | |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 77 | void WalkAST::VisitCallExpr(CallExpr *CE) { |
| 78 | if (const FunctionDecl *FD = CE->getDirectCallee()) { |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 79 | CheckCall_gets(CE, FD); |
| 80 | CheckCall_rand(CE, FD); |
| 81 | CheckCall_random(CE, FD); |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 82 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 83 | |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 84 | // Recurse and check children. |
| 85 | VisitChildren(CE); |
| 86 | } |
| 87 | |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 88 | void WalkAST::VisitCompoundStmt(CompoundStmt *S) { |
| 89 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 90 | if (Stmt *child = *I) { |
| 91 | if (CallExpr *CE = dyn_cast<CallExpr>(child)) |
| 92 | CheckUncheckedReturnValue(CE); |
| 93 | Visit(child); |
| 94 | } |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 97 | void WalkAST::VisitForStmt(ForStmt *FS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 98 | CheckLoopConditionForFloat(FS); |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 100 | // Recurse and check children. |
| 101 | VisitChildren(FS); |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 105 | // Check: floating poing variable used as loop counter. |
Ted Kremenek | 5abeb52 | 2009-07-23 21:44:18 +0000 | [diff] [blame] | 106 | // Originally: <rdar://problem/6336718> |
| 107 | // Implements: CERT security coding advisory FLP-30. |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 108 | //===----------------------------------------------------------------------===// |
| 109 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 110 | static const DeclRefExpr* |
| 111 | GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) { |
| 112 | expr = expr->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 113 | |
| 114 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) { |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 115 | if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() || |
| 116 | B->getOpcode() == BinaryOperator::Comma)) |
| 117 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 118 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 119 | if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y)) |
| 120 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 121 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 122 | if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y)) |
| 123 | return rhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 124 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 125 | return NULL; |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 126 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 127 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 128 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) { |
| 129 | const NamedDecl *ND = DR->getDecl(); |
| 130 | return ND == x || ND == y ? DR : NULL; |
| 131 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 132 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 133 | if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr)) |
| 134 | return U->isIncrementDecrementOp() |
| 135 | ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL; |
| 136 | |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 137 | return NULL; |
| 138 | } |
| 139 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 140 | /// CheckLoopConditionForFloat - This check looks for 'for' statements that |
| 141 | /// use a floating point variable as a loop counter. |
| 142 | /// CERT: FLP30-C, FLP30-CPP. |
| 143 | /// |
| 144 | void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) { |
| 145 | // Does the loop have a condition? |
| 146 | const Expr *condition = FS->getCond(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 147 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 148 | if (!condition) |
| 149 | return; |
| 150 | |
| 151 | // Does the loop have an increment? |
| 152 | const Expr *increment = FS->getInc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 153 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 154 | if (!increment) |
| 155 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 156 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 157 | // Strip away '()' and casts. |
| 158 | condition = condition->IgnoreParenCasts(); |
| 159 | increment = increment->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 160 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 161 | // Is the loop condition a comparison? |
| 162 | const BinaryOperator *B = dyn_cast<BinaryOperator>(condition); |
| 163 | |
| 164 | if (!B) |
| 165 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 166 | |
Ted Kremenek | cad9f41 | 2009-07-24 20:26:31 +0000 | [diff] [blame] | 167 | // Is this a comparison? |
| 168 | if (!(B->isRelationalOp() || B->isEqualityOp())) |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 169 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 170 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 171 | // Are we comparing variables? |
| 172 | const DeclRefExpr *drLHS = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParens()); |
| 173 | const DeclRefExpr *drRHS = dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParens()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 174 | |
Ted Kremenek | cad9f41 | 2009-07-24 20:26:31 +0000 | [diff] [blame] | 175 | // Does at least one of the variables have a floating point type? |
| 176 | drLHS = drLHS && drLHS->getType()->isFloatingType() ? drLHS : NULL; |
| 177 | drRHS = drRHS && drRHS->getType()->isFloatingType() ? drRHS : NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 178 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 179 | if (!drLHS && !drRHS) |
| 180 | return; |
| 181 | |
| 182 | const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL; |
| 183 | const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 184 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 185 | if (!vdLHS && !vdRHS) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 186 | return; |
| 187 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 188 | // Does either variable appear in increment? |
| 189 | const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 190 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 191 | if (!drInc) |
| 192 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 193 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 194 | // Emit the error. First figure out which DeclRefExpr in the condition |
| 195 | // referenced the compared variable. |
| 196 | const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS; |
| 197 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 198 | llvm::SmallVector<SourceRange, 2> ranges; |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 199 | std::string sbuf; |
| 200 | llvm::raw_string_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 201 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 202 | os << "Variable '" << drCond->getDecl()->getNameAsCString() |
| 203 | << "' with floating point type '" << drCond->getType().getAsString() |
| 204 | << "' should not be used as a loop counter"; |
| 205 | |
| 206 | ranges.push_back(drCond->getSourceRange()); |
| 207 | ranges.push_back(drInc->getSourceRange()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 208 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 209 | const char *bugType = "Floating point variable used as loop counter"; |
| 210 | BR.EmitBasicReport(bugType, "Security", os.str().c_str(), |
| 211 | FS->getLocStart(), ranges.data(), ranges.size()); |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | //===----------------------------------------------------------------------===// |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 215 | // Check: Any use of 'gets' is insecure. |
| 216 | // Originally: <rdar://problem/6335715> |
| 217 | // Implements (part of): 300-BSI (buildsecurityin.us-cert.gov) |
| 218 | //===----------------------------------------------------------------------===// |
| 219 | |
| 220 | void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) { |
| 221 | if (FD->getIdentifier() != GetIdentifier(II_gets, "gets")) |
| 222 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 223 | |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 224 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType()); |
| 225 | if (!FTP) |
| 226 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 227 | |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 228 | // Verify that the function takes a single argument. |
| 229 | if (FTP->getNumArgs() != 1) |
| 230 | return; |
| 231 | |
| 232 | // Is the argument a 'char*'? |
| 233 | const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0)); |
| 234 | if (!PT) |
| 235 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 236 | |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 237 | if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) |
| 238 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 239 | |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 240 | // Issue a warning. |
| 241 | SourceRange R = CE->getCallee()->getSourceRange(); |
| 242 | BR.EmitBasicReport("Potential buffer overflow in call to 'gets'", |
| 243 | "Security", |
| 244 | "Call to function 'gets' is extremely insecure as it can " |
| 245 | "always result in a buffer overflow", |
| 246 | CE->getLocStart(), &R, 1); |
| 247 | } |
| 248 | |
| 249 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 250 | // Check: Linear congruent random number generators should not be used |
| 251 | // Originally: <rdar://problem/63371000> |
| 252 | // CWE-338: Use of cryptographically weak prng |
| 253 | //===----------------------------------------------------------------------===// |
| 254 | |
| 255 | void WalkAST::CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD) { |
| 256 | if (II_rand[0] == NULL) { |
| 257 | // This check applies to these functions |
| 258 | static const char * const identifiers[num_rands] = { |
| 259 | "drand48", "erand48", "jrand48", "lrand48", "mrand48", "nrand48", |
| 260 | "lcong48", |
| 261 | "rand", "rand_r" |
| 262 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 263 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 264 | for (size_t i = 0; i < num_rands; i++) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 265 | II_rand[i] = &BR.getContext().Idents.get(identifiers[i]); |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 266 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 267 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 268 | const IdentifierInfo *id = FD->getIdentifier(); |
| 269 | size_t identifierid; |
| 270 | |
| 271 | for (identifierid = 0; identifierid < num_rands; identifierid++) |
| 272 | if (id == II_rand[identifierid]) |
| 273 | break; |
| 274 | |
| 275 | if (identifierid >= num_rands) |
| 276 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 277 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 278 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType()); |
| 279 | if (!FTP) |
| 280 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 281 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 282 | if (FTP->getNumArgs() == 1) { |
| 283 | // Is the argument an 'unsigned short *'? |
| 284 | // (Actually any integer type is allowed.) |
| 285 | const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0)); |
| 286 | if (!PT) |
| 287 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 288 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 289 | if (! PT->getPointeeType()->isIntegerType()) |
| 290 | return; |
| 291 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 292 | else if (FTP->getNumArgs() != 0) |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 293 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 294 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 295 | // Issue a warning. |
| 296 | std::string buf1; |
| 297 | llvm::raw_string_ostream os1(buf1); |
| 298 | os1 << "'" << FD->getNameAsString() << "' is a poor random number generator"; |
| 299 | |
| 300 | std::string buf2; |
| 301 | llvm::raw_string_ostream os2(buf2); |
| 302 | os2 << "Function '" << FD->getNameAsString() |
| 303 | << "' is obsolete because it implements a poor random number generator." |
| 304 | << " Use 'arc4random' instead"; |
| 305 | |
| 306 | SourceRange R = CE->getCallee()->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 307 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 308 | BR.EmitBasicReport(os1.str().c_str(), "Security", os2.str().c_str(), |
| 309 | CE->getLocStart(), &R, 1); |
| 310 | } |
| 311 | |
| 312 | //===----------------------------------------------------------------------===// |
| 313 | // Check: 'random' should not be used |
| 314 | // Originally: <rdar://problem/63371000> |
| 315 | //===----------------------------------------------------------------------===// |
| 316 | |
| 317 | void WalkAST::CheckCall_random(const CallExpr *CE, const FunctionDecl *FD) { |
| 318 | if (FD->getIdentifier() != GetIdentifier(II_random, "random")) |
| 319 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 320 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 321 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType()); |
| 322 | if (!FTP) |
| 323 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 324 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 325 | // Verify that the function takes no argument. |
| 326 | if (FTP->getNumArgs() != 0) |
| 327 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 328 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 329 | // Issue a warning. |
| 330 | SourceRange R = CE->getCallee()->getSourceRange(); |
| 331 | BR.EmitBasicReport("'random' is not a secure random number generator", |
| 332 | "Security", |
| 333 | "The 'random' function produces a sequence of values that " |
| 334 | "an adversary may be able to predict. Use 'arc4random' " |
| 335 | "instead", |
| 336 | CE->getLocStart(), &R, 1); |
| 337 | } |
| 338 | |
| 339 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 340 | // Check: Should check whether privileges are dropped successfully. |
| 341 | // Originally: <rdar://problem/6337132> |
| 342 | //===----------------------------------------------------------------------===// |
| 343 | |
| 344 | void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) { |
| 345 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 346 | if (!FD) |
| 347 | return; |
| 348 | |
| 349 | if (II_setid[0] == NULL) { |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 350 | static const char * const identifiers[num_setids] = { |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 351 | "setuid", "setgid", "seteuid", "setegid", |
| 352 | "setreuid", "setregid" |
| 353 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 354 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 355 | for (size_t i = 0; i < num_setids; i++) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 356 | II_setid[i] = &BR.getContext().Idents.get(identifiers[i]); |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 357 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 358 | |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 359 | const IdentifierInfo *id = FD->getIdentifier(); |
| 360 | size_t identifierid; |
| 361 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 362 | for (identifierid = 0; identifierid < num_setids; identifierid++) |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 363 | if (id == II_setid[identifierid]) |
| 364 | break; |
| 365 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 366 | if (identifierid >= num_setids) |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 367 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 368 | |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 369 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType()); |
| 370 | if (!FTP) |
| 371 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 372 | |
Ted Kremenek | a818783 | 2009-08-28 00:24:55 +0000 | [diff] [blame] | 373 | // Verify that the function takes one or two arguments (depending on |
| 374 | // the function). |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 375 | if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2)) |
| 376 | return; |
| 377 | |
| 378 | // The arguments must be integers. |
| 379 | for (unsigned i = 0; i < FTP->getNumArgs(); i++) |
| 380 | if (! FTP->getArgType(i)->isIntegerType()) |
| 381 | return; |
| 382 | |
| 383 | // Issue a warning. |
| 384 | std::string buf1; |
| 385 | llvm::raw_string_ostream os1(buf1); |
| 386 | os1 << "Return value is not checked in call to '" << FD->getNameAsString() |
| 387 | << "'"; |
| 388 | |
| 389 | std::string buf2; |
| 390 | llvm::raw_string_ostream os2(buf2); |
| 391 | os2 << "The return value from the call to '" << FD->getNameAsString() |
| 392 | << "' is not checked. If an error occurs in '" |
| 393 | << FD->getNameAsString() |
| 394 | << "', the following code may execute with unexpected privileges"; |
| 395 | |
| 396 | SourceRange R = CE->getCallee()->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 397 | |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 398 | BR.EmitBasicReport(os1.str().c_str(), "Security", os2.str().c_str(), |
| 399 | CE->getLocStart(), &R, 1); |
| 400 | } |
| 401 | |
| 402 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 403 | // Entry point for check. |
| 404 | //===----------------------------------------------------------------------===// |
| 405 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 406 | void clang::CheckSecuritySyntaxOnly(const Decl *D, BugReporter &BR) { |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 407 | WalkAST walker(BR); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 408 | walker.Visit(D->getBody()); |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 409 | } |