Skip to content

Fix reading elf section from file buffer #33434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions include/swift/Reflection/ReflectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,12 @@ class ReflectionContext
for (const typename T::Section *Hdr : SecHdrVec) {
uint32_t Offset = Hdr->sh_name;
const char *Start = (const char *)StrTab + Offset;
uint64_t Size = strnlen(Start, StrTabSize - Offset);
if (Size > StrTabSize - Offset) {
uint64_t StringSize = strnlen(Start, StrTabSize - Offset);
if (StringSize > StrTabSize - Offset) {
Error = true;
break;
}
std::string SecName(Start, Size);
std::string SecName(Start, StringSize);
if (SecName != Name)
continue;
RemoteAddress SecStart =
Expand All @@ -495,15 +495,16 @@ class ReflectionContext
// sh_offset gives us the offset to the section in the file,
// while sh_addr gives us the offset in the process.
auto Offset = Hdr->sh_offset;
if (FileBuffer->allocatedSize() > Offset + Size) {
if (FileBuffer->allocatedSize() < Offset + SecSize) {
Error = true;
break;
}
auto *Buf = malloc(SecSize);
SecBuf = MemoryReader::ReadBytesResult(
Buf, [](const void *ptr) { free(const_cast<void *>(ptr)); });
memcpy((void *)Buf,
(const void *)((uint64_t)FileBuffer->base() + Offset), Size);
(const void *)((uint64_t)FileBuffer->base() + Offset),
SecSize);
} else {
SecBuf = this->getReader().readBytes(SecStart, SecSize);
}
Expand Down