mirror of
https://github.com/upx/upx
synced 2025-10-05 19:20:23 +08:00
Add some more sanity checks in pefile.cpp.
This commit is contained in:
parent
aa2ee5dfc2
commit
0d3a2072b2
|
@ -67,6 +67,11 @@
|
||||||
#define OPTR_C(type, var, v) type* const var = (v)
|
#define OPTR_C(type, var, v) type* const var = (v)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static void xcheck(const void *p)
|
||||||
|
{
|
||||||
|
if (!p)
|
||||||
|
throwCantUnpack("unexpected NULL pointer; take care!");
|
||||||
|
}
|
||||||
static void xcheck(const void *p, size_t plen, const void *b, size_t blen)
|
static void xcheck(const void *p, size_t plen, const void *b, size_t blen)
|
||||||
{
|
{
|
||||||
const char *pp = (const char *) p;
|
const char *pp = (const char *) p;
|
||||||
|
@ -1179,6 +1184,7 @@ PeFile::Resource::upx_rnode *PeFile::Resource::convert(const void *rnode,
|
||||||
for (const res_dir_entry *rde = node->entries + ic - 1; --ic >= 0; rde--)
|
for (const res_dir_entry *rde = node->entries + ic - 1; --ic >= 0; rde--)
|
||||||
{
|
{
|
||||||
upx_rnode *child = convert(start + (rde->child & 0x7fffffff),branch,level + 1);
|
upx_rnode *child = convert(start + (rde->child & 0x7fffffff),branch,level + 1);
|
||||||
|
xcheck(child);
|
||||||
branch->children[ic] = child;
|
branch->children[ic] = child;
|
||||||
child->id = rde->tnl;
|
child->id = rde->tnl;
|
||||||
if (child->id & 0x80000000)
|
if (child->id & 0x80000000)
|
||||||
|
@ -1214,6 +1220,7 @@ void PeFile::Resource::build(const upx_rnode *node, unsigned &bpos,
|
||||||
res_dir_entry *be = b->entries;
|
res_dir_entry *be = b->entries;
|
||||||
for (unsigned ic = 0; ic < branch->nc; ic++, be++)
|
for (unsigned ic = 0; ic < branch->nc; ic++, be++)
|
||||||
{
|
{
|
||||||
|
xcheck(branch->children[ic]);
|
||||||
be->tnl = branch->children[ic]->id;
|
be->tnl = branch->children[ic]->id;
|
||||||
be->child = bpos + ((level < 2) ? 0x80000000 : 0);
|
be->child = bpos + ((level < 2) ? 0x80000000 : 0);
|
||||||
|
|
||||||
|
@ -1245,6 +1252,7 @@ upx_byte *PeFile::Resource::build()
|
||||||
|
|
||||||
void PeFile::Resource::destroy(upx_rnode *node,unsigned level)
|
void PeFile::Resource::destroy(upx_rnode *node,unsigned level)
|
||||||
{
|
{
|
||||||
|
xcheck(node);
|
||||||
delete [] node->name; node->name = NULL;
|
delete [] node->name; node->name = NULL;
|
||||||
if (level != 3)
|
if (level != 3)
|
||||||
{
|
{
|
||||||
|
@ -1673,9 +1681,10 @@ void PeFile::rebuildResources(upx_byte *& extrainfo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
upx_byte *p = res.build();
|
upx_byte *p = res.build();
|
||||||
|
OCHECK(obuf + ODADDR(PEDIR_RESOURCE) - rvamin, 16);
|
||||||
// write back when the original is zeroed
|
// write back when the original is zeroed
|
||||||
if (get_le32(obuf + ODADDR(PEDIR_RESOURCE) - rvamin + 12) == 0)
|
if (get_le32(obuf + ODADDR(PEDIR_RESOURCE) - rvamin + 12) == 0)
|
||||||
omemcpy(obuf + ODADDR(PEDIR_RESOURCE) - rvamin,p,res.dirsize());
|
omemcpy(obuf + ODADDR(PEDIR_RESOURCE) - rvamin, p, res.dirsize());
|
||||||
delete [] p;
|
delete [] p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1724,6 +1733,7 @@ void PeFile::unpack(OutputFile *fo)
|
||||||
Filter ft(ph.level);
|
Filter ft(ph.level);
|
||||||
ft.init(ph.filter,oh.codebase - rvamin);
|
ft.init(ph.filter,oh.codebase - rvamin);
|
||||||
ft.cto = (unsigned char) ph.filter_cto;
|
ft.cto = (unsigned char) ph.filter_cto;
|
||||||
|
OCHECK(obuf + oh.codebase - rvamin, oh.codesize);
|
||||||
ft.unfilter(obuf + oh.codebase - rvamin, oh.codesize);
|
ft.unfilter(obuf + oh.codebase - rvamin, oh.codesize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user