From: Nadav Har'El <n...@scylladb.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

ELF: fix assertion failure when trying to run short non-executable

When trying to run a short non-executable (e.g. "scripts/run.py -e /etc/hosts")
we used to fail with an assertion failure in the function read(fileref, ...)
from fs/fs.cc. This function assumes it *can* read the desired buffer, and
if it can't (because the file is too short), it crashes. So when we try to
read the executable's header in a file which is shorter than the expected
size of such header, we crashed.

The solution is simple - verify that the size of the file is big enough
before calling the read function. If it is not, throw an exception.

This exception (and all the other exceptions in core/elf.cc during loading
an object) should be converted to a new type, as explained in issue #904.

Signed-off-by: Nadav Har'El <n...@scylladb.com>

---
diff --git a/core/elf.cc b/core/elf.cc
--- a/core/elf.cc
+++ b/core/elf.cc
@@ -267,6 +267,11 @@ void file::load_elf_header()

 void file::read(Elf64_Off offset, void* data, size_t size)
 {
+ // read(fileref, ...) is void, and crashes with assertion failure if the
+    // file is not long enough. So we need to check first.
+    if (::size(_f) < offset + size) {
+        throw std::runtime_error("executable too short");
+    }
     ::read(_f, data, offset, size);
 }

--
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to