Skip to content

Ghidra is a software reverse engineering (SRE) framework

Better protections for overlapping input analysis

caheckman5319b9703cfe
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc
index 7129f75b0d..e7df72241f 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc
@@ -482,7 +482,7 @@ int4 ParamListStandard::characterizeAsParam(const Address &loc,int4 size) const
const ParamEntry *testEntry = (*iterpair.first).getParamEntry();
if (testEntry->getMinSize() <= size && testEntry->justifiedContain(loc, size)==0)
return 1;
- if (testEntry->containedBy(loc, size))
+ if (testEntry->isExclusion() && testEntry->containedBy(loc, size))
res = 2;
++iterpair.first;
}
@@ -490,7 +490,7 @@ int4 ParamListStandard::characterizeAsParam(const Address &loc,int4 size) const
iterpair.second = resolver->find_end(loc.getOffset() + (size-1));
while(iterpair.first != iterpair.second) {
const ParamEntry *testEntry = (*iterpair.first).getParamEntry();
- if (testEntry->containedBy(loc, size)) {
+ if (testEntry->isExclusion() && testEntry->containedBy(loc, size)) {
res = 2;
break;
}
@@ -953,9 +953,12 @@ bool ParamListStandard::getBiggestContainedParam(const Address &loc,int4 size,Va
ParamEntryResolver *resolver = resolverMap[index];
if (resolver == (ParamEntryResolver *)0)
return false;
+ Address endLoc = loc + (size-1);
+ if (endLoc.getOffset() < loc.getOffset())
+ return false; // Assume there is no parameter if we see wrapping
const ParamEntry *maxEntry = (const ParamEntry *)0;
ParamEntryResolver::const_iterator iter = resolver->find_begin(loc.getOffset());
- ParamEntryResolver::const_iterator enditer = resolver->find_end(loc.getOffset() + (size-1));
+ ParamEntryResolver::const_iterator enditer = resolver->find_end(endLoc.getOffset());
while(iter != enditer) {
const ParamEntry *testEntry = (*iter).getParamEntry();
++iter;
@@ -966,9 +969,9 @@ bool ParamListStandard::getBiggestContainedParam(const Address &loc,int4 size,Va
maxEntry = testEntry;
}
}
- if (!maxEntry->isExclusion())
- return false;
if (maxEntry != (const ParamEntry *)0) {
+ if (!maxEntry->isExclusion())
+ return false;
res.space = maxEntry->getSpace();
res.offset = maxEntry->getBase();
res.size = maxEntry->getSize();
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc
index a09574c95b..622ab1e444 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc
@@ -1043,17 +1043,20 @@ void Heritage::guard(const Address &addr,int4 size,vector<Varnode *> &read,vecto
/// pulls out the potential parameter.
/// \param fc is the call site potentially taking a parameter
/// \param addr is the starting address of the range
+/// \param transAddr is the start of the same range from the callee's stack perspective
/// \param size is the size of the range in bytes
-void Heritage::guardCallOverlappingInput(FuncCallSpecs *fc,const Address &addr,int4 size)
+void Heritage::guardCallOverlappingInput(FuncCallSpecs *fc,const Address &addr,const Address &transAddr,int4 size)
{
VarnodeData vData;
- if (fc->getBiggestContainedInputParam(addr, size, vData)) {
+ if (fc->getBiggestContainedInputParam(transAddr, size, vData)) {
ParamActive *active = fc->getActiveInput();
- Address taddr(vData.space,vData.offset);
- if (active->whichTrial(taddr, size) < 0) { // If not already a trial
- int4 truncateAmount = addr.justifiedContain(size, taddr, vData.size, false);
+ Address truncAddr(vData.space,vData.offset);
+ if (active->whichTrial(truncAddr, size) < 0) { // If not already a trial
+ int4 truncateAmount = transAddr.justifiedContain(size, truncAddr, vData.size, false);
+ int4 diff = (int4)(truncAddr.getOffset() - transAddr.getOffset());
+ truncAddr = addr + diff; // Convert truncated Address to caller's perspective
PcodeOp *op = fc->getOp();
PcodeOp *subpieceOp = fd->newOp(2,op->getAddr());
fd->opSetOpcode(subpieceOp, CPUI_SUBPIECE);
@@ -1061,9 +1064,9 @@ void Heritage::guardCallOverlappingInput(FuncCallSpecs *fc,const Address &addr,i
wholeVn->setActiveHeritage();
fd->opSetInput(subpieceOp,wholeVn,0);
fd->opSetInput(subpieceOp,fd->newConstant(4,truncateAmount),1);
- Varnode *vn = fd->newVarnodeOut(vData.size, taddr, subpieceOp);
+ Varnode *vn = fd->newVarnodeOut(vData.size, truncAddr, subpieceOp);
fd->opInsertBefore(subpieceOp,op);
- active->registerTrial(taddr, vData.size);
+ active->registerTrial(truncAddr, vData.size);
fd->opInsertInput(op, vn, op->numInput());
}
}
@@ -1121,21 +1124,21 @@ void Heritage::guardCalls(uint4 flags,const Address &addr,int4 size,vector<Varno
tryregister = false;
}
}
- Address taddr(spc,off);
+ Address transAddr(spc,off); // Address relative to callee's stack
if (tryregister) {
- int4 inputCharacter = fc->characterizeAsInputParam(taddr,size);
+ int4 inputCharacter = fc->characterizeAsInputParam(transAddr,size);
if (inputCharacter == 1) { // Call could be using this range as an input parameter
ParamActive *active = fc->getActiveInput();
- if (active->whichTrial(taddr,size)<0) { // If not already a trial
+ if (active->whichTrial(transAddr,size)<0) { // If not already a trial
PcodeOp *op = fc->getOp();
- active->registerTrial(taddr,size);
+ active->registerTrial(transAddr,size);
Varnode *vn = fd->newVarnode(size,addr);
vn->setActiveHeritage();
fd->opInsertInput(op,vn,op->numInput());
}
}
else if (inputCharacter == 2) // Call may be using part of this range as an input parameter
- guardCallOverlappingInput(fc, addr, size);
+ guardCallOverlappingInput(fc, addr, transAddr, size);
}
}
// We do not guard the call if the effect is "unaffected" or "reload"
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.hh
index 7ee49ad8a9..08a5e740b7 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.hh
@@ -246,7 +246,7 @@ class Heritage {
void reprocessFreeStores(AddrSpace *spc,vector<PcodeOp *> &freeStores);
void guard(const Address &addr,int4 size,vector<Varnode *> &read,vector<Varnode *> &write,vector<Varnode *> &inputvars);
void guardInput(const Address &addr,int4 size,vector<Varnode *> &input);
- void guardCallOverlappingInput(FuncCallSpecs *fc,const Address &addr,int4 size);
+ void guardCallOverlappingInput(FuncCallSpecs *fc,const Address &addr,const Address &transAddr,int4 size);
void guardCalls(uint4 flags,const Address &addr,int4 size,vector<Varnode *> &write);
void guardStores(const Address &addr,int4 size,vector<Varnode *> &write);
void guardLoads(uint4 flags,const Address &addr,int4 size,vector<Varnode *> &write);