Skip to content

Commit

Permalink
rtdl: Return null when non-elf file is dlopened
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwinci committed Aug 18, 2023
1 parent 67bee48 commit fd59581
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions options/rtdl/generic/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ SharedObject *ObjectRepository::requestObjectWithName(frg::string_view name,
auto object = frg::construct<SharedObject>(getAllocator(),
name.data(), std::move(chosenPath), false, localScope, rts);

Elf64_Ehdr hdr;
readExactlyOrDie(fd, &hdr, sizeof(Elf64_Ehdr));
if (hdr.e_ident[0] != 0x7F || hdr.e_ident[1] != 'E' || hdr.e_ident[2] != 'L' || hdr.e_ident[3] != 'F') {
closeOrDie(fd);
return nullptr;
}
off_t dumb;
mlibc::sys_seek(fd, 0, SEEK_SET, &dumb);

_fetchFromFile(object, fd);
closeOrDie(fd);

Expand Down Expand Up @@ -288,6 +297,16 @@ SharedObject *ObjectRepository::requestObjectAtPath(frg::string_view path,
int fd;
if(mlibc::sys_open((no_prefix + '\0').data(), O_RDONLY, 0, &fd))
return nullptr; // TODO: Free the SharedObject.

Elf64_Ehdr hdr;
readExactlyOrDie(fd, &hdr, sizeof(Elf64_Ehdr));
if (hdr.e_ident[0] != 0x7F || hdr.e_ident[1] != 'E' || hdr.e_ident[2] != 'L' || hdr.e_ident[3] != 'F') {
closeOrDie(fd);
return nullptr;
}
off_t dumb;
mlibc::sys_seek(fd, 0, SEEK_SET, &dumb);

_fetchFromFile(object, fd);
closeOrDie(fd);

Expand Down

0 comments on commit fd59581

Please sign in to comment.