Re:7-Zip: From Uninitialized Memory to Remote Code Execution
by landave on Thursday May 03, 2018 @12:56PM
(#16987638)
You missed my point. It is trivial to find out what the magic number is. What is more important though: How exactly is the magic number matched? From what you have written, one might be tempted to simply check whether a file begins with this magic number. And this would be wrong. If you take a look at the matching in CPP/7zip/Archive/Rar/RarHandler.cpp:
Byte marker[NHeader::kMarkerSize];
RINOK(ReadStream_FALSE(stream, marker, NHeader::kMarkerSize));
if (memcmp(marker, kMarker, NHeader::kMarkerSize) == 0)
m_Position += NHeader::kMarkerSize;
else
{
if (searchHeaderSizeLimit && *searchHeaderSizeLimit == 0)
return S_FALSE;
RINOK(stream->Seek(m_StreamStartPosition, STREAM_SEEK_SET, NULL));
RINOK(FindSignatureInStream(stream, kMarker, NHeader::kMarkerSize,
searchHeaderSizeLimit, arcStartPos));
m_Position = arcStartPos + NHeader::kMarkerSize;
RINOK(stream->Seek(m_Position, STREAM_SEEK_SET, NULL));
}
7-Zip finds the magic number if it appears within some searchHeaderSizeLimit, i.e., the file does not need to start (at offset 0) with the magic number. For example, 7-Zip will extract a RAR file which begins with [00 52 61 72 21 1A 07 00] (instead of [52 61 72 21 1A 07 00]) just fine.
Re:7-Zip: From Uninitialized Memory to Remote Code Execution
by codetrotter on Thursday May 03, 2018 @01:03PM
(#16987691)
Oh, I did not expect that. Yes you are right, one must be careful about these things.
CPP/7zip/Archive/Rar/RarHandler.cpp:
CPP/7zip/UI/Common/OpenArchive.cpp: CPP/7zip/Archive/Rar/Rar5Handler.cpp: Those are the two magic numbers for RAR archive version 1.50 onwards and RAR archive version 5.0 onwards respectively, and those are the places they are referenced in the 7-zip source code. I looked at the source archive of the 18.05 version, downloaded from https://www.7-zip.org/a/7z1805-src.7z. I guess if you wanted to be really rigorous you'd look at previous versions as well. If the 7-zip project makes use of a version control system and that vcs supports the equivalent of git blame then that should not be too difficult of a task for whoever wanted to go to that extent of investigation.And here is where one copy of the 'file' command identifies the same magic numbers as RAR archives:
https://github.com/file/file/blob/f0a725a13fe0c1b046d8e07057...
https://github.com/file/file/blob/f0a725a13fe0c1b046d8e07057... :)