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 f3d15f1
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions options/rtdl/generic/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ SharedObject *ObjectRepository::requestObjectWithName(frg::string_view name,
auto object = frg::construct<SharedObject>(getAllocator(),
name.data(), std::move(chosenPath), false, localScope, rts);

Elf64_Ehdr hdr;

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

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

Expand Down Expand Up @@ -288,6 +302,21 @@ 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;

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

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

Expand Down

0 comments on commit f3d15f1

Please sign in to comment.