18 #ifndef CHECKER_PSA_VULNERABILITY_H
19 #define CHECKER_PSA_VULNERABILITY_H
21 #include <llvm/Pass.h>
23 #include "IR/SEG/SEGCallSite.h"
24 #include "IR/SEG/SEGReturnSite.h"
25 #include "IR/SEG/SymbolicExprGraph.h"
26 #include "IR/SEG/SymbolicExprGraphSolver.h"
27 #include "Report/BugReport/BugReportMgr.h"
28 #include "VulnerabilityTrace.h"
29 #include "VulnerabilityTraceBuilder.h"
31 #include "Checker/PSA/PSAReportDecoratorDefault.h"
32 #include "Checker/PSA/PSATaintReportDecorator.h"
33 #include "IR/SEG/SEGCallSiteOutputNode.h"
34 #include "IR/SEG/SEGSimpleSite.h"
89 typedef typename std::pair<const SEGOperandNode *, const SEGSiteBase *>
108 bool Parasitical =
true;
110 const char *VulnName;
111 const BugDescription::BugImportance Importance;
112 const BugDescription::BugClassification Classification;
113 const char *VulnDescription;
119 const BugDescription::BugImportance Importance,
120 const BugDescription::BugClassification Classification,
122 : CategoryType(CTy), VulnName(Name), Importance(Importance),
123 Classification(Classification), VulnDescription(Desc) {}
142 std::vector<ValueSitePairType> &Sources) = 0;
156 const VulnerabilityTraceBuilder &TraceHistory,
157 SMTExprVec &Prerequisites) = 0;
170 const VulnerabilityTraceBuilder &TraceHistory) {
177 const VulnerabilityTraceBuilder &TraceHistory) = 0;
181 virtual bool checkTrace(std::shared_ptr<VulnerabilityTrace> &Trace) {
214 void setParasitical(
bool B) { Parasitical = B; }
219 BugDescription::BugImportance getImportance()
const {
return Importance; }
221 BugDescription::BugClassification getClassification()
const {
222 return Classification;
228 virtual PSAReportDecoratorDefault *allocNewDecorator() {
229 return new PSAReportDecoratorDefault;
232 virtual void destroyDecorator(PSAReportDecoratorDefault *decorator) {
242 const char *Name,
const BugDescription::BugImportance importance,
243 const BugDescription::BugClassification classification,
const char *Desc)
244 :
Vulnerability(VCT_SinkMustReach, Name, importance, classification,
253 const char *Name,
const BugDescription::BugImportance Importance,
254 const BugDescription::BugClassification Classification,
const char *Desc)
255 :
Vulnerability(VCT_SinkMustNotReach, Name, Importance, Classification,
262 const BugDescription::BugImportance Importance,
263 const BugDescription::BugClassification Classification,
265 :
Vulnerability(CTy, Name, Importance, Classification, Desc) {}
268 std::vector<ValueSitePairType> &Sources) {
269 for (
auto It = SEG->value_node_begin(), E = SEG->value_node_end(); It != E;
273 for (
auto SiteIt = Node->use_site_begin(), SiteE = Node->use_site_end();
274 SiteIt != SiteE; SiteIt++) {
275 if (isSource(Node, *SiteIt)) {
276 Sources.emplace_back(Node, *SiteIt);
280 if (!Node->use_site_size()) {
281 if (isSource(Node,
nullptr)) {
282 Sources.emplace_back(Node,
nullptr);
290 const VulnerabilityTraceBuilder &TraceHistory)
override {
295 }
else if (isa<SEGReturnSite>(CurrSite)) {
297 }
else if (isa<SEGCallSite>(CurrSite)) {
309 const VulnerabilityTraceBuilder &TraceHistory,
310 SMTExprVec &Prerequisites)
override {
321 std::vector<const SEGNodeBase *> &TransferDsts) {}
325 return vuln->getCategoryType() > VCT_Begin &&
326 vuln->getCategoryType() < VCT_End;
334 BugDescription::BugImportance Importance = BugDescription::BI_MEDIUM,
335 BugDescription::BugClassification Classification =
336 BugDescription::BC_SECURITY,
337 const char *Desc =
"CUSTOMIZED")
339 Classification, Desc) {}
346 BugDescription::BugImportance Importance = BugDescription::BI_MEDIUM,
347 BugDescription::BugClassification Classification =
348 BugDescription::BC_SECURITY,
349 const char *Desc =
"CUSTOMIZED")
351 Classification, Desc) {}
371 std::vector<const SEGNodeBase *> &TransferDsts) {}
379 const char *Name,
const BugDescription::BugImportance importance,
380 const BugDescription::BugClassification classification,
const char *Desc)
381 :
Vulnerability(VCT_Taint, Name, importance, classification, Desc) {}
383 virtual PSAReportDecoratorDefault *allocNewDecorator()
override {
384 return new PSATaintReportDecorator;
393 std::vector<std::shared_ptr<Vulnerability>>
Vulns;
399 std::map<std::shared_ptr<Vulnerability>,
400 std::set<std::shared_ptr<Vulnerability>>>
405 std::map<std::shared_ptr<Vulnerability>,
406 std::set<std::shared_ptr<Vulnerability>>>
409 typedef std::pair<std::shared_ptr<Vulnerability>,
410 std::shared_ptr<Vulnerability>>
412 typedef std::function<
const SEGObject *(std::shared_ptr<VulnerabilityTrace>)>
414 std::map<VulnPairTy, GetTraceFunctionTy> HeadDepFctorMap;
415 std::map<VulnPairTy, GetTraceFunctionTy> TailDepFctorMap;
423 bool add(std::shared_ptr<Vulnerability> V) {
428 std::shared_ptr<Vulnerability> get(
size_t Index)
const {
432 size_t size()
const {
return Vulns.size(); }
434 std::vector<std::shared_ptr<Vulnerability>>::const_iterator begin()
const {
435 return Vulns.begin();
438 std::vector<std::shared_ptr<Vulnerability>>::const_iterator end()
const {
444 virtual const char *getFullName() = 0;
447 virtual const char *getAbbrName() = 0;
458 virtual void buildDependence() = 0;
463 std::shared_ptr<Vulnerability> V2,
464 GetTraceFunctionTy &Func) {
465 HeadDeps[V1].insert(V2);
466 HeadDepFctorMap[std::make_pair(V1, V2)] = Func;
472 std::shared_ptr<Vulnerability> V2,
473 GetTraceFunctionTy &Func) {
474 TailDeps[V1].insert(V2);
475 TailDepFctorMap[std::make_pair(V1, V2)] = Func;
480 std::set<std::shared_ptr<Vulnerability>>::iterator
482 return HeadDeps[V].begin();
485 std::set<std::shared_ptr<Vulnerability>>::iterator
486 head_dep_end(std::shared_ptr<Vulnerability> V) {
487 return HeadDeps[V].end();
490 std::set<std::shared_ptr<Vulnerability>>::iterator
491 tail_dep_begin(std::shared_ptr<Vulnerability> V) {
492 return TailDeps[V].begin();
495 std::set<std::shared_ptr<Vulnerability>>::iterator
496 tail_dep_end(std::shared_ptr<Vulnerability> V) {
497 return TailDeps[V].end();
509 std::shared_ptr<VulnerabilityTrace> T1,
510 std::shared_ptr<Vulnerability> V2) {
511 auto It = HeadDepFctorMap.find(std::make_pair(V1, V2));
512 if (It != HeadDepFctorMap.end()) {
513 return It->second(T1);
518 const SEGObject *getTraceTail(std::shared_ptr<Vulnerability> V1,
519 std::shared_ptr<VulnerabilityTrace> T1,
520 std::shared_ptr<Vulnerability> V2) {
521 auto It = TailDepFctorMap.find(std::make_pair(V1, V2));
522 if (It != TailDepFctorMap.end()) {
523 return It->second(T1);