| Zhongxing Xu | 6c306c8 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 1 | //=== FixedAddressChecker.cpp - Fixed address usage checker ----*- 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 files defines FixedAddressChecker, a builtin checker that checks for |
| 11 | // assignment of a fixed address to a pointer. |
| 12 | // This check corresponds to CWE-587. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| Argyrios Kyrtzidis | a921528 | 2011-02-15 22:55:20 +0000 | [diff] [blame] | 16 | #include "ClangSACheckers.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| Argyrios Kyrtzidis | 507ff53 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| Argyrios Kyrtzidis | dff865d | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| Zhongxing Xu | 6c306c8 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 23 | using namespace ento; |
| Zhongxing Xu | 6c306c8 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 24 | |
| 25 | namespace { |
| Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 26 | class FixedAddressChecker |
| Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 27 | : public Checker< check::PreStmt<BinaryOperator> > { |
| Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 28 | mutable std::unique_ptr<BuiltinBug> BT; |
| Argyrios Kyrtzidis | dff865d | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 29 | |
| Zhongxing Xu | 6c306c8 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 30 | public: |
| Argyrios Kyrtzidis | dff865d | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 31 | void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const; |
| Zhongxing Xu | 6c306c8 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 32 | }; |
| 33 | } |
| 34 | |
| Argyrios Kyrtzidis | dff865d | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 35 | void FixedAddressChecker::checkPreStmt(const BinaryOperator *B, |
| 36 | CheckerContext &C) const { |
| Zhongxing Xu | 6c306c8 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 37 | // Using a fixed address is not portable because that address will probably |
| 38 | // not be valid in all environments or platforms. |
| 39 | |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 40 | if (B->getOpcode() != BO_Assign) |
| Zhongxing Xu | 6c306c8 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 41 | return; |
| 42 | |
| 43 | QualType T = B->getType(); |
| 44 | if (!T->isPointerType()) |
| 45 | return; |
| 46 | |
| George Karpenkov | d703ec9 | 2018-01-17 20:27:29 +0000 | [diff] [blame] | 47 | SVal RV = C.getSVal(B->getRHS()); |
| Zhongxing Xu | 6c306c8 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 48 | |
| 49 | if (!RV.isConstant() || RV.isZeroConstant()) |
| 50 | return; |
| 51 | |
| Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 52 | if (ExplodedNode *N = C.generateNonFatalErrorNode()) { |
| Zhongxing Xu | 6c306c8 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 53 | if (!BT) |
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 54 | BT.reset( |
| 55 | new BuiltinBug(this, "Use fixed address", |
| 56 | "Using a fixed address is not portable because that " |
| 57 | "address will probably not be valid in all " |
| 58 | "environments or platforms.")); |
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 59 | auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N); |
| Zhongxing Xu | 6c306c8 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 60 | R->addRange(B->getRHS()->getSourceRange()); |
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 61 | C.emitReport(std::move(R)); |
| Zhongxing Xu | 6c306c8 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| Argyrios Kyrtzidis | 507ff53 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 65 | void ento::registerFixedAddressChecker(CheckerManager &mgr) { |
| Argyrios Kyrtzidis | dff865d | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 66 | mgr.registerChecker<FixedAddressChecker>(); |
| Argyrios Kyrtzidis | 507ff53 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 67 | } |