Skip to content

Instantly share code, notes, and snippets.

@sunwayforever
Last active September 27, 2017 08:54
Show Gist options
  • Save sunwayforever/59d890e52efe0dcf47dc08ed72a5aaaa to your computer and use it in GitHub Desktop.
Save sunwayforever/59d890e52efe0dcf47dc08ed72a5aaaa to your computer and use it in GitHub Desktop.
linker.diff
diff --git a/linker/linker.cpp b/linker/linker.cpp
index c661624..f426fe8 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -1029,6 +1029,16 @@ static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
}
if (fd != -1) {
+ ElfW(Ehdr) header;
+ ssize_t rc = TEMP_FAILURE_RETRY(pread64(fd, &header, sizeof(header), 0));
+ // accessing g_elf_class here should be OK, the relocation
+ // process of linker_so should not call into this function;
+ if (rc < 0 || rc != sizeof(header) || g_elf_class != header.e_ident[EI_CLASS]) {
+ PRINT("warning: open_library skipped for %s: elf_class mismatch(required: %d, got: %d)",
+ buf, g_elf_class, header.e_ident[EI_CLASS]);
+ close(fd);
+ return -1;
+ }
return fd;
}
}
diff --git a/linker/linker_globals.cpp b/linker/linker_globals.cpp
index 155ebf4..a385ceb 100644
--- a/linker/linker_globals.cpp
+++ b/linker/linker_globals.cpp
@@ -34,6 +34,8 @@ int g_argc = 0;
char** g_argv = nullptr;
char** g_envp = nullptr;
+int g_elf_class = ELFCLASSNONE;
+
android_namespace_t g_default_namespace;
std::unordered_map<uintptr_t, soinfo*> g_soinfo_handles_map;
diff --git a/linker/linker_globals.h b/linker/linker_globals.h
index 1ed479c..1d417e6 100644
--- a/linker/linker_globals.h
+++ b/linker/linker_globals.h
@@ -75,4 +75,5 @@ extern std::unordered_map<uintptr_t, soinfo*> g_soinfo_handles_map;
char* linker_get_error_buffer();
size_t linker_get_error_buffer_size();
+extern int g_elf_class;
#endif /* __LINKER_GLOBALS_H */
diff --git a/linker/linker_main.cpp b/linker/linker_main.cpp
index 40f82a1..b376e93 100644
--- a/linker/linker_main.cpp
+++ b/linker/linker_main.cpp
@@ -433,7 +433,9 @@ static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(
#if TIMING || STATS || COUNT_PAGES
fflush(stdout);
#endif
-
+#if 0
+ }
+#endif
ElfW(Addr) entry = args.getauxval(AT_ENTRY);
TRACE("[ Ready to execute \"%s\" @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry));
return entry;
@@ -531,6 +533,8 @@ extern "C" ElfW(Addr) __linker_init(void* raw_args) {
// Initialize the linker's static libc's globals
__libc_init_globals(args);
+ g_elf_class = elf_hdr->e_ident[EI_CLASS];
+
// store argc/argv/envp to use them for calling constructors
g_argc = args.argc;
g_argv = args.argv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment