Added Packer::checkAlreadyPacked().

committer: mfx <mfx> 977427988 +0000
This commit is contained in:
Markus F.X.J. Oberhumer 2000-12-21 19:46:28 +00:00
parent facca7b891
commit 046df6da76
8 changed files with 32 additions and 16 deletions

View File

@ -72,14 +72,13 @@ bool PackCom::canPack()
{ {
unsigned char buf[128]; unsigned char buf[128];
fi->readx(buf,128); fi->readx(buf, sizeof(buf));
if (memcmp(buf,"MZ",2) == 0 || memcmp(buf,"ZM",2) == 0 // .exe if (memcmp(buf,"MZ",2) == 0 || memcmp(buf,"ZM",2) == 0 // .exe
|| memcmp (buf,"\xff\xff\xff\xff",4) == 0) // .sys || memcmp (buf,"\xff\xff\xff\xff",4) == 0) // .sys
return false; return false;
if (!fn_has_ext(fi->getName(),"com")) if (!fn_has_ext(fi->getName(),"com"))
return false; return false;
if (find_le32(buf,128,UPX_MAGIC_LE32) >= 0) checkAlreadyPacked(buf, sizeof(buf));
throwAlreadyPacked();
if (file_size < 1024) if (file_size < 1024)
throwCantPack("file is too small"); throwCantPack("file is too small");
if (file_size > 0xFF00) if (file_size > 0xFF00)

View File

@ -264,8 +264,7 @@ void PackExe::pack(OutputFile *fo)
fi->seek(ih.headsize16*16,SEEK_SET); fi->seek(ih.headsize16*16,SEEK_SET);
fi->readx(ibuf,imagesize); fi->readx(ibuf,imagesize);
if (find_le32(ibuf, UPX_MIN(imagesize, 127u), UPX_MAGIC_LE32) >= 0) checkAlreadyPacked(ibuf, UPX_MIN(imagesize, 127u));
throwAlreadyPacked();
// relocations // relocations
has_9a = false; has_9a = false;

View File

@ -45,13 +45,12 @@ bool PackSys::canPack()
{ {
unsigned char buf[128]; unsigned char buf[128];
fi->readx(buf,128); fi->readx(buf, sizeof(buf));
if (memcmp (buf,"\xff\xff\xff\xff",4) != 0) if (memcmp (buf,"\xff\xff\xff\xff",4) != 0)
return false; return false;
if (!fn_has_ext(fi->getName(),"sys")) if (!fn_has_ext(fi->getName(),"sys"))
return false; return false;
if (find_le32(buf,128,UPX_MAGIC_LE32) >= 0) checkAlreadyPacked(buf, sizeof(buf));
throwAlreadyPacked();
if (file_size < 1024) if (file_size < 1024)
throwCantPack("file is too small"); throwCantPack("file is too small");
if (file_size > 0x10000) if (file_size > 0x10000)

View File

@ -295,9 +295,8 @@ bool PackTos::canPack()
return false; return false;
unsigned char buf[512]; unsigned char buf[512];
fi->readx(buf,sizeof(buf)); fi->readx(buf, sizeof(buf));
if (find_le32(buf,sizeof(buf),UPX_MAGIC_LE32) >= 0) checkAlreadyPacked(buf, sizeof(buf));
throwAlreadyPacked();
if (!checkFileHeader()) if (!checkFileHeader())
throwCantPack("unsupported header flags"); throwCantPack("unsupported header flags");

View File

@ -68,9 +68,8 @@ bool PackUnix::canPack()
// info: currently the header is 36 (32+4) bytes before EOF // info: currently the header is 36 (32+4) bytes before EOF
unsigned char buf[256]; unsigned char buf[256];
fi->seek(-(long)sizeof(buf), SEEK_END); fi->seek(-(long)sizeof(buf), SEEK_END);
fi->readx(buf,sizeof(buf)); fi->readx(buf, sizeof(buf));
if (find_le32(buf,sizeof(buf),UPX_MAGIC_LE32) >= 0) // note: always le32 checkAlreadyPacked(buf, sizeof(buf));
throwAlreadyPacked();
return true; return true;
} }

View File

@ -123,8 +123,7 @@ int PackVmlinuzI386::uncompressKernel()
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
fi->readx(obuf, file_size); fi->readx(obuf, file_size);
if (find_le32(obuf + setup_size, UPX_MIN(file_size - setup_size, 1024), UPX_MAGIC_LE32) >= 0) checkAlreadyPacked(obuf + setup_size, UPX_MIN(file_size - setup_size, 1024));
throwAlreadyPacked();
// estimate gzip-uncompressed kernel size & alloc buffer // estimate gzip-uncompressed kernel size & alloc buffer
ibuf.alloc((file_size - setup_size) * 3); ibuf.alloc((file_size - setup_size) * 3);

View File

@ -643,6 +643,27 @@ bool Packer::readPackHeader(int len)
} }
void Packer::checkAlreadyPacked(void *b, int blen)
{
int boff = find_le32(b, blen, UPX_MAGIC_LE32);
if (boff < 0)
return;
// FIXME: could add some more checks to verify that this
// is a real PackHeader, e.g.
//
//PackHeader tmp;
//tmp.magic = UPX_MAGIC_LE32;
//if (!tmp.fillPackHeader((unsigned char *)b + boff, blen - boff))
// return;
//
// This also would require that the buffer in `b' holds
// the full PackHeader, and not only the magic.
throwAlreadyPacked();
}
/************************************************************************* /*************************************************************************
// patch util for loader // patch util for loader
**************************************************************************/ **************************************************************************/

View File

@ -182,6 +182,7 @@ protected:
virtual int patchPackHeader(void *b, int blen); virtual int patchPackHeader(void *b, int blen);
virtual bool getPackHeader(void *b, int blen); virtual bool getPackHeader(void *b, int blen);
virtual bool readPackHeader(int len); virtual bool readPackHeader(int len);
virtual void checkAlreadyPacked(void *b, int blen);
// filter handling // filter handling
virtual bool isValidFilter(int filter_id) const; virtual bool isValidFilter(int filter_id) const;