Ted Kremenek | 076d84e | 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 | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 076d84e | 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> { |
Ted Kremenek | ad9f89d | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 24 | BugReporter &BR; |
Ted Kremenek | c7289a1 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 25 | IdentifierInfo *II_gets; |
| 26 | enum { num_ids = 6 }; |
| 27 | IdentifierInfo *II_setid[num_ids]; |
| 28 | |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 29 | public: |
Ted Kremenek | ad9f89d | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 30 | WalkAST(BugReporter &br) : BR(br), |
Ted Kremenek | c7289a1 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 31 | II_gets(0), II_setid() {} |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 32 | |
| 33 | // Statement visitor methods. |
Ted Kremenek | ad9f89d | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 34 | void VisitCallExpr(CallExpr *CE); |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 35 | void VisitForStmt(ForStmt *S); |
Ted Kremenek | c7289a1 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 36 | void VisitCompoundStmt (CompoundStmt *S); |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 37 | void VisitStmt(Stmt *S) { VisitChildren(S); } |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 38 | |
| 39 | void VisitChildren(Stmt *S); |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 40 | |
Ted Kremenek | ad9f89d | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 41 | // Helpers. |
| 42 | IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str); |
| 43 | |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 44 | // Checker-specific methods. |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 45 | void CheckLoopConditionForFloat(const ForStmt *FS); |
Ted Kremenek | ad9f89d | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 46 | void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD); |
Ted Kremenek | c7289a1 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 47 | void CheckUncheckedReturnValue(CallExpr *CE); |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 48 | }; |
| 49 | } // end anonymous namespace |
| 50 | |
| 51 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ad9f89d | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 52 | // Helper methods. |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
| 55 | IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) { |
| 56 | if (!II) |
| 57 | II = &BR.getContext().Idents.get(str); |
| 58 | |
| 59 | return II; |
| 60 | } |
| 61 | |
| 62 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 63 | // AST walking. |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | |
| 66 | void WalkAST::VisitChildren(Stmt *S) { |
| 67 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) |
| 68 | if (Stmt *child = *I) |
| 69 | Visit(child); |
| 70 | } |
| 71 | |
Ted Kremenek | ad9f89d | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 72 | void WalkAST::VisitCallExpr(CallExpr *CE) { |
| 73 | if (const FunctionDecl *FD = CE->getDirectCallee()) { |
| 74 | CheckCall_gets(CE, FD); |
| 75 | } |
| 76 | |
| 77 | // Recurse and check children. |
| 78 | VisitChildren(CE); |
| 79 | } |
| 80 | |
Ted Kremenek | c7289a1 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 81 | void WalkAST::VisitCompoundStmt(CompoundStmt *S) { |
| 82 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) |
| 83 | if (Stmt *child = *I) |
| 84 | { |
| 85 | if (CallExpr *CE = dyn_cast<CallExpr>(child)) |
| 86 | CheckUncheckedReturnValue(CE); |
| 87 | Visit(child); |
| 88 | } |
| 89 | } |
| 90 | |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 91 | void WalkAST::VisitForStmt(ForStmt *FS) { |
| 92 | CheckLoopConditionForFloat(FS); |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 94 | // Recurse and check children. |
| 95 | VisitChildren(FS); |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 99 | // Check: floating poing variable used as loop counter. |
Ted Kremenek | 69576e9 | 2009-07-23 21:44:18 +0000 | [diff] [blame] | 100 | // Originally: <rdar://problem/6336718> |
| 101 | // Implements: CERT security coding advisory FLP-30. |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 102 | //===----------------------------------------------------------------------===// |
| 103 | |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 104 | static const DeclRefExpr* |
| 105 | GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) { |
| 106 | expr = expr->IgnoreParenCasts(); |
| 107 | |
| 108 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) { |
| 109 | if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() || |
| 110 | B->getOpcode() == BinaryOperator::Comma)) |
| 111 | return NULL; |
| 112 | |
| 113 | if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y)) |
| 114 | return lhs; |
| 115 | |
| 116 | if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y)) |
| 117 | return rhs; |
| 118 | |
| 119 | return NULL; |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 120 | } |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 121 | |
| 122 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) { |
| 123 | const NamedDecl *ND = DR->getDecl(); |
| 124 | return ND == x || ND == y ? DR : NULL; |
| 125 | } |
| 126 | |
| 127 | if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr)) |
| 128 | return U->isIncrementDecrementOp() |
| 129 | ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL; |
| 130 | |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 131 | return NULL; |
| 132 | } |
| 133 | |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 134 | /// CheckLoopConditionForFloat - This check looks for 'for' statements that |
| 135 | /// use a floating point variable as a loop counter. |
| 136 | /// CERT: FLP30-C, FLP30-CPP. |
| 137 | /// |
| 138 | void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) { |
| 139 | // Does the loop have a condition? |
| 140 | const Expr *condition = FS->getCond(); |
| 141 | |
| 142 | if (!condition) |
| 143 | return; |
| 144 | |
| 145 | // Does the loop have an increment? |
| 146 | const Expr *increment = FS->getInc(); |
| 147 | |
| 148 | if (!increment) |
| 149 | return; |
| 150 | |
| 151 | // Strip away '()' and casts. |
| 152 | condition = condition->IgnoreParenCasts(); |
| 153 | increment = increment->IgnoreParenCasts(); |
| 154 | |
| 155 | // Is the loop condition a comparison? |
| 156 | const BinaryOperator *B = dyn_cast<BinaryOperator>(condition); |
| 157 | |
| 158 | if (!B) |
| 159 | return; |
| 160 | |
Ted Kremenek | 5adc4e3 | 2009-07-24 20:26:31 +0000 | [diff] [blame] | 161 | // Is this a comparison? |
| 162 | if (!(B->isRelationalOp() || B->isEqualityOp())) |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 163 | return; |
Ted Kremenek | 5adc4e3 | 2009-07-24 20:26:31 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 165 | // Are we comparing variables? |
| 166 | const DeclRefExpr *drLHS = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParens()); |
| 167 | const DeclRefExpr *drRHS = dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParens()); |
| 168 | |
Ted Kremenek | 5adc4e3 | 2009-07-24 20:26:31 +0000 | [diff] [blame] | 169 | // Does at least one of the variables have a floating point type? |
| 170 | drLHS = drLHS && drLHS->getType()->isFloatingType() ? drLHS : NULL; |
| 171 | drRHS = drRHS && drRHS->getType()->isFloatingType() ? drRHS : NULL; |
| 172 | |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 173 | if (!drLHS && !drRHS) |
| 174 | return; |
| 175 | |
| 176 | const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL; |
| 177 | const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL; |
| 178 | |
| 179 | if (!vdLHS && !vdRHS) |
| 180 | return; |
| 181 | |
| 182 | // Does either variable appear in increment? |
| 183 | const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS); |
| 184 | |
| 185 | if (!drInc) |
| 186 | return; |
| 187 | |
| 188 | // Emit the error. First figure out which DeclRefExpr in the condition |
| 189 | // referenced the compared variable. |
| 190 | const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS; |
| 191 | |
| 192 | llvm::SmallVector<SourceRange, 2> ranges; |
| 193 | std::string sbuf; |
| 194 | llvm::raw_string_ostream os(sbuf); |
| 195 | |
| 196 | os << "Variable '" << drCond->getDecl()->getNameAsCString() |
| 197 | << "' with floating point type '" << drCond->getType().getAsString() |
| 198 | << "' should not be used as a loop counter"; |
| 199 | |
| 200 | ranges.push_back(drCond->getSourceRange()); |
| 201 | ranges.push_back(drInc->getSourceRange()); |
| 202 | |
| 203 | const char *bugType = "Floating point variable used as loop counter"; |
| 204 | BR.EmitBasicReport(bugType, "Security", os.str().c_str(), |
| 205 | FS->getLocStart(), ranges.data(), ranges.size()); |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ad9f89d | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 209 | // Check: Any use of 'gets' is insecure. |
| 210 | // Originally: <rdar://problem/6335715> |
| 211 | // Implements (part of): 300-BSI (buildsecurityin.us-cert.gov) |
| 212 | //===----------------------------------------------------------------------===// |
| 213 | |
| 214 | void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) { |
| 215 | if (FD->getIdentifier() != GetIdentifier(II_gets, "gets")) |
| 216 | return; |
| 217 | |
| 218 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType()); |
| 219 | if (!FTP) |
| 220 | return; |
| 221 | |
| 222 | // Verify that the function takes a single argument. |
| 223 | if (FTP->getNumArgs() != 1) |
| 224 | return; |
| 225 | |
| 226 | // Is the argument a 'char*'? |
| 227 | const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0)); |
| 228 | if (!PT) |
| 229 | return; |
| 230 | |
| 231 | if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) |
| 232 | return; |
| 233 | |
| 234 | // Issue a warning. |
| 235 | SourceRange R = CE->getCallee()->getSourceRange(); |
| 236 | BR.EmitBasicReport("Potential buffer overflow in call to 'gets'", |
| 237 | "Security", |
| 238 | "Call to function 'gets' is extremely insecure as it can " |
| 239 | "always result in a buffer overflow", |
| 240 | CE->getLocStart(), &R, 1); |
| 241 | } |
| 242 | |
| 243 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c7289a1 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 244 | // Check: Should check whether privileges are dropped successfully. |
| 245 | // Originally: <rdar://problem/6337132> |
| 246 | //===----------------------------------------------------------------------===// |
| 247 | |
| 248 | void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) { |
| 249 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 250 | if (!FD) |
| 251 | return; |
| 252 | |
| 253 | if (II_setid[0] == NULL) { |
| 254 | static const char * const identifiers[num_ids] = { |
| 255 | "setuid", "setgid", "seteuid", "setegid", |
| 256 | "setreuid", "setregid" |
| 257 | }; |
| 258 | |
| 259 | for (size_t i = 0; i < num_ids; i++) |
| 260 | II_setid[i] = &BR.getContext().Idents.get(identifiers[i]); |
| 261 | } |
| 262 | |
| 263 | const IdentifierInfo *id = FD->getIdentifier(); |
| 264 | size_t identifierid; |
| 265 | |
| 266 | for (identifierid = 0; identifierid < num_ids; identifierid++) |
| 267 | if (id == II_setid[identifierid]) |
| 268 | break; |
| 269 | |
| 270 | if (identifierid >= num_ids) |
| 271 | return; |
| 272 | |
| 273 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FD->getType()); |
| 274 | if (!FTP) |
| 275 | return; |
| 276 | |
Ted Kremenek | 4fa5d6c | 2009-08-28 00:24:55 +0000 | [diff] [blame] | 277 | // Verify that the function takes one or two arguments (depending on |
| 278 | // the function). |
Ted Kremenek | c7289a1 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 279 | if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2)) |
| 280 | return; |
| 281 | |
| 282 | // The arguments must be integers. |
| 283 | for (unsigned i = 0; i < FTP->getNumArgs(); i++) |
| 284 | if (! FTP->getArgType(i)->isIntegerType()) |
| 285 | return; |
| 286 | |
| 287 | // Issue a warning. |
| 288 | std::string buf1; |
| 289 | llvm::raw_string_ostream os1(buf1); |
| 290 | os1 << "Return value is not checked in call to '" << FD->getNameAsString() |
| 291 | << "'"; |
| 292 | |
| 293 | std::string buf2; |
| 294 | llvm::raw_string_ostream os2(buf2); |
| 295 | os2 << "The return value from the call to '" << FD->getNameAsString() |
| 296 | << "' is not checked. If an error occurs in '" |
| 297 | << FD->getNameAsString() |
| 298 | << "', the following code may execute with unexpected privileges"; |
| 299 | |
| 300 | SourceRange R = CE->getCallee()->getSourceRange(); |
| 301 | |
| 302 | BR.EmitBasicReport(os1.str().c_str(), "Security", os2.str().c_str(), |
| 303 | CE->getLocStart(), &R, 1); |
| 304 | } |
| 305 | |
| 306 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 307 | // Entry point for check. |
| 308 | //===----------------------------------------------------------------------===// |
| 309 | |
Ted Kremenek | 8724eba | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 310 | void clang::CheckSecuritySyntaxOnly(const Decl *D, BugReporter &BR) { |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 311 | WalkAST walker(BR); |
| 312 | walker.Visit(D->getBody()); |
| 313 | } |