Static Value-Flow Analysis
Loading...
Searching...
No Matches
AEDetector.h
Go to the documentation of this file.
1//===- AEDetector.h -- Vulnerability Detectors---------------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-> <Yulei Sui>
6//
7
8// This program is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Affero General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU Affero General Public License for more details.
17
18// You should have received a copy of the GNU Affero General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20//
21//===----------------------------------------------------------------------===//
22
23
24//
25// Created on: May 1, 2025
26// Author: Xiao Cheng, Jiawei Wang, Mingxiu Wang
27//
28#pragma once
29#include <Graphs/PAG.h>
31#include "Util/SVFBugReport.h"
32
33namespace SVF
34{
35
37
43{
44public:
55
60
64 virtual ~AEDetector() = default;
65
71 static bool classof(const AEDetector* detector)
72 {
73 return detector->getKind() == AEDetector::UNKNOWN;
74 }
75
81 virtual void detect(const ICFGNode* node) = 0;
82
87 virtual void handleStubFunctions(const CallICFGNode* call) = 0;
88
92 virtual void reportBug() = 0;
93
99 {
100 return kind;
101 }
102
103protected:
105};
106
112{
114public:
120
129
134
140 static bool classof(const AEDetector* detector)
141 {
142 return detector->getKind() == AEDetector::BUF_OVERFLOW;
143 }
144
152 void updateGepObjOffsetFromBase(const ICFGNode* node,
153 AddressValue gepAddrs,
154 AddressValue objAddrs,
156
162 void detect(const ICFGNode*) override;
163
164
169 void handleStubFunctions(const CallICFGNode*) override;
170
177 {
179 }
180
186 bool hasGepObjOffsetFromBase(const GepObjVar* obj) const
187 {
188 return gepObjOffsetFromBase.find(obj) != gepObjOffsetFromBase.end();
189 }
190
197 {
199 return gepObjOffsetFromBase.at(obj);
200 else
201 {
202 assert(false && "GepObjVar not found in gepObjOffsetFromBase");
203 abort();
204 }
205 }
206
214 IntervalValue getAccessOffset(NodeID objId, const GepStmt* gep);
215
221 void addBugToReporter(const ICFGNode* node, const SVFStmt* stmt = nullptr)
222 {
224 }
225
227 void addBugToReporter(const ICFGNode* node, const BufferOverflowInfo& info,
228 const SVFStmt* stmt = nullptr)
229 {
230
231 GenericBug::EventStack eventStack;
232 SVFBugEvent sourceInstEvent(SVFBugEvent::EventType::SourceInst, node);
233 eventStack.push_back(sourceInstEvent); // Add the source instruction event to the event stack
234
235 if (eventStack.empty())
236 {
237 return; // If the event stack is empty, return early
238 }
239
240 std::string loc = eventStack.back().getEventLoc(); // Get the location of the last event in the stack
241
242 // Check if the bug at this location has already been reported
243 if (bugLoc.find(loc) != bugLoc.end())
244 {
245 return; // If the bug location is already reported, return early
246 }
247 else
248 {
249 bugLoc.insert(loc); // Otherwise, mark this location as reported
250 }
251
252 // Add the bug to the recorder with details from the event stack
253 recoder.addAbsExecBug(GenericBug::FULLBUFOVERFLOW, eventStack,
254 info.bufferSize.lb(), info.bufferSize.ub(),
255 info.accessOffset.lb(), info.accessOffset.ub());
256 // Duplicate locations returned above without formatting the IR again.
257 nodeToBugInfo[node] = stmt ? stmt->toString() : node->toString();
258 }
259
263 void reportBug() override
264 {
265 if (!nodeToBugInfo.empty())
266 {
267 std::cerr << "######################Buffer Overflow (" + std::to_string(nodeToBugInfo.size())
268 + " found)######################\n";
269 std::cerr << "---------------------------------------------\n";
270 for (const auto& it : nodeToBugInfo)
271 {
272 std::cerr << it.second << "\n---------------------------------------------\n";
273 }
274 }
275 }
276
281
287 void detectExtAPI(const CallICFGNode *call);
288
296 bool canSafelyAccessMemory(const ValVar *value, const IntervalValue &len,
297 const ICFGNode* node, BufferOverflowInfo* info = nullptr);
298
299private:
305 bool detectStrcat(const CallICFGNode *call, BufferOverflowInfo& info);
306
312 bool detectStrcpy(const CallICFGNode *call, BufferOverflowInfo& info);
313
314private:
320};
322{
324public:
329
331
332 static bool classof(const AEDetector* detector)
333 {
334 return detector->getKind() == AEDetector::NULL_DEREF;
335 }
336
342 void detect(const ICFGNode* node) override;
343
348 void handleStubFunctions(const CallICFGNode* call) override;
349
356 {
357 // uninitialized value has neither interval value nor address value
358 bool is = v.getAddrs().isBottom() && v.getInterval().isBottom();
359 return is;
360 }
361
367 void addBugToReporter(const ICFGNode* node, const SVFStmt* stmt = nullptr)
368 {
369 GenericBug::EventStack eventStack;
370 SVFBugEvent sourceInstEvent(SVFBugEvent::EventType::SourceInst, node);
371 eventStack.push_back(sourceInstEvent); // Add the source instruction event to the event stack
372
373 if (eventStack.empty())
374 {
375 return; // If the event stack is empty, return early
376 }
377 std::string loc = eventStack.back().getEventLoc(); // Get the location of the last event in the stack
378
379 // Check if the bug at this location has already been reported
380 if (bugLoc.find(loc) != bugLoc.end())
381 {
382 return; // If the bug location is already reported, return early
383 }
384 else
385 {
386 bugLoc.insert(loc); // Otherwise, mark this location as reported
387 }
388 recoder.addAbsExecBug(GenericBug::FULLNULLPTRDEREFERENCE, eventStack, 0, 0, 0, 0);
389 // Duplicate locations returned above without formatting the IR again.
390 nodeToBugInfo[node] = stmt ? stmt->toString() : node->toString();
391 }
392
396 void reportBug() override
397 {
398 if (!nodeToBugInfo.empty())
399 {
400 std::cerr << "###################### Nullptr Dereference (" + std::to_string(nodeToBugInfo.size())
401 + " found)######################\n";
402 std::cerr << "---------------------------------------------\n";
403 for (const auto& it : nodeToBugInfo)
404 {
405 std::cerr << it.second << "\n---------------------------------------------\n";
406 }
407 }
408 }
409
415 void detectExtAPI(const CallICFGNode* call);
416
417
424 {
425 return !v.isAddr() && !v.isInterval();
426 }
427
428 bool canSafelyDerefPtr(const ValVar* ptr, const ICFGNode* node);
429
430private:
434};
435}
buffer offset
Definition cJSON.cpp:1113
static bool classof(const AEDetector *detector)
Check if the detector is of the UNKNOWN kind.
Definition AEDetector.h:71
DetectorKind
Enumerates the types of detectors available.
Definition AEDetector.h:50
@ NULL_DEREF
Detector for nullptr dereference issues.
Definition AEDetector.h:52
@ UNKNOWN
Default type if the kind is not specified.
Definition AEDetector.h:53
@ BUF_OVERFLOW
Detector for buffer overflow issues.
Definition AEDetector.h:51
virtual void handleStubFunctions(const CallICFGNode *call)=0
Pure virtual function for handling stub external API calls. (e.g. UNSAFE_BUFACCESS).
DetectorKind kind
The kind of the detector.
Definition AEDetector.h:104
virtual void reportBug()=0
Pure virtual function to report detected bugs.
AEDetector()
Constructor initializes the detector kind to UNKNOWN.
Definition AEDetector.h:59
virtual ~AEDetector()=default
Virtual destructor for safe polymorphic use.
virtual void detect(const ICFGNode *node)=0
Pure virtual function for detecting issues within a node.
DetectorKind getKind() const
Get the kind of the detector.
Definition AEDetector.h:98
bool isInterval() const
IntervalValue & getInterval()
AddressValue & getAddrs()
bool isAddr() const
bool isBottom() const
bool detectStrcat(const CallICFGNode *call, BufferOverflowInfo &info)
Detects buffer overflow in 'strcat' function calls.
void addToGepObjOffsetFromBase(const GepObjVar *obj, const IntervalValue &offset)
Adds an offset to a GEP object.
Definition AEDetector.h:176
void detect(const ICFGNode *) override
Detect buffer overflow issues within a node.
void reportBug() override
Reports all detected buffer overflow bugs.
Definition AEDetector.h:263
~BufOverflowDetector()=default
Destructor.
Map< const GepObjVar *, IntervalValue > gepObjOffsetFromBase
Maps GEP objects to their offsets from the base.
Definition AEDetector.h:315
Map< std::string, std::vector< std::pair< u32_t, u32_t > > > extAPIBufOverflowCheckRules
Rules for checking buffer overflows in external APIs.
Definition AEDetector.h:316
SVFBugReport recoder
Recorder for abstract execution bugs.
Definition AEDetector.h:318
IntervalValue getAccessOffset(NodeID objId, const GepStmt *gep)
Retrieves the access offset for a given object and GEP statement.
void updateGepObjOffsetFromBase(const ICFGNode *node, AddressValue gepAddrs, AddressValue objAddrs, IntervalValue offset)
Updates the offset of a GEP object from its base.
void detectExtAPI(const CallICFGNode *call)
Handles external API calls related to buffer overflow detection.
void addBugToReporter(const ICFGNode *node, const SVFStmt *stmt=nullptr)
Reports a bug, generating its text only for a new location.
Definition AEDetector.h:221
BufOverflowDetector()
Constructor initializes the detector kind to BUF_OVERFLOW and sets up external API buffer overflow ru...
Definition AEDetector.h:124
Set< std::string > bugLoc
Set of locations where bugs have been reported.
Definition AEDetector.h:317
void addBugToReporter(const ICFGNode *node, const BufferOverflowInfo &info, const SVFStmt *stmt=nullptr)
Report an OOB alarm with the buffer size and offset from its check.
Definition AEDetector.h:227
IntervalValue getGepObjOffsetFromBase(const GepObjVar *obj) const
Retrieves the offset of a GEP object from its base.
Definition AEDetector.h:196
static bool classof(const AEDetector *detector)
Check if the detector is of the BUF_OVERFLOW kind.
Definition AEDetector.h:140
bool canSafelyAccessMemory(const ValVar *value, const IntervalValue &len, const ICFGNode *node, BufferOverflowInfo *info=nullptr)
Checks if memory can be safely accessed.
friend class AbstractInterpretation
Definition AEDetector.h:113
void handleStubFunctions(const CallICFGNode *) override
Handles external API calls related to buffer overflow detection.
Map< const ICFGNode *, std::string > nodeToBugInfo
Maps ICFG nodes to bug information.
Definition AEDetector.h:319
bool hasGepObjOffsetFromBase(const GepObjVar *obj) const
Checks if a GEP object has an associated offset.
Definition AEDetector.h:186
void initExtAPIBufOverflowCheckRules()
Initializes external API buffer overflow check rules.
bool detectStrcpy(const CallICFGNode *call, BufferOverflowInfo &info)
Detects buffer overflow in 'strcpy' function calls.
std::vector< SVFBugEvent > EventStack
virtual const std::string toString() const
Definition ICFG.cpp:50
const BoundedInt & ub() const
Return the upper bound.
bool isBottom() const
const BoundedInt & lb() const
Return the lower bound.
bool canSafelyDerefPtr(const ValVar *ptr, const ICFGNode *node)
Set< std::string > bugLoc
Set of locations where bugs have been reported.
Definition AEDetector.h:431
bool isNull(AbstractValue v)
Check if an Abstract Value is NULL (or uninitialized).
Definition AEDetector.h:423
bool isUninit(AbstractValue v)
Checks if an Abstract Value is uninitialized.
Definition AEDetector.h:355
void handleStubFunctions(const CallICFGNode *call) override
Handles external API calls related to nullptr dereferences.
void detect(const ICFGNode *node) override
Detects nullptr dereferences issues within a node.
static bool classof(const AEDetector *detector)
Definition AEDetector.h:332
friend class AbstractInterpretation
Definition AEDetector.h:323
void reportBug() override
Reports all detected nullptr dereference bugs.
Definition AEDetector.h:396
void addBugToReporter(const ICFGNode *node, const SVFStmt *stmt=nullptr)
Reports a bug, generating its text only for a new location.
Definition AEDetector.h:367
void detectExtAPI(const CallICFGNode *call)
Handle external API calls related to nullptr dereferences.
SVFBugReport recoder
Recorder for abstract execution bugs.
Definition AEDetector.h:432
Map< const ICFGNode *, std::string > nodeToBugInfo
Maps ICFG nodes to bug information.
Definition AEDetector.h:433
for isBitcode
Definition BasicTypes.h:70
u32_t NodeID
Definition GeneralType.h:76
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition GeneralType.h:51
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition GeneralType.h:56