[PATCH 06/33] Check ELF class

From: Janani Venkataraman
Date: Thu Mar 20 2014 - 05:40:22 EST


The process to be dumped can be either a 32bit application or a 64 bit
application. We first need to check this and proceed with the dump accordingly.

Signed-off-by: Janani Venkataraman <jananive@xxxxxxxxxxxxxxxxxx>
---
src/coredump.c | 5 +++++
src/coredump.h | 1 +
src/proc.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 51 insertions(+)

diff --git a/src/coredump.c b/src/coredump.c
index 7a559e2..f9c7176 100644
--- a/src/coredump.c
+++ b/src/coredump.c
@@ -188,6 +188,11 @@ int do_coredump(int pid, char *core_file)
if (ret)
goto cleanup;

+ /* Compat Support */
+ cp.elf_class = ret = get_elf_class(pid, &cp);
+ if (ret == -1)
+ goto cleanup;
+
cleanup:

/* Release the threads */
diff --git a/src/coredump.h b/src/coredump.h
index 291e13b..25042f5 100644
--- a/src/coredump.h
+++ b/src/coredump.h
@@ -31,4 +31,5 @@ struct core_proc {
int *t_id; /* Threads_ids of all the threads */
struct maps *vmas; /* VMAs */
int phdrs_count; /* Number of Program headers */
+ int elf_class; /* Elf class of the process */
};
diff --git a/src/proc.c b/src/proc.c
index a401aa5..3a4a387 100644
--- a/src/proc.c
+++ b/src/proc.c
@@ -27,6 +27,7 @@
#include <stdlib.h>
#include <string.h>
#include <coredump.h>
+#include <elf.h>

/* Get Process Stats */
int get_pid_stat(int pid, struct pid_stat *ps)
@@ -231,3 +232,47 @@ int get_vmas(int pid, struct core_proc *cp)
fclose(fin);
return 0;
}
+
+/* Check if its ELF */
+int check_elf_hdr(unsigned char *elf_ident)
+{
+ if (memcmp(elf_ident, ELFMAG, SELFMAG) != 0)
+ return -1;
+
+ return 0;
+}
+
+/* Get ELF Class */
+int get_elf_class(int pid, struct core_proc *cp)
+{
+ FILE *fin;
+ unsigned char elf_magic[EI_NIDENT];
+ char filename[40];
+ int ret;
+
+ snprintf(filename, 40, "/proc/%d/exe", pid);
+ fin = fopen(filename, "r");
+ if (fin == NULL) {
+ status = errno;
+ gencore_log("Failure while fetching the ELF header of the executable from %s.\n", filename);
+ return -1;
+ }
+
+ ret = fread(elf_magic, EI_NIDENT, 1, fin);
+ if (ret != 1) {
+ status = errno;
+ gencore_log("Failure while fetching the ELF header of the executable from %s.\n", filename);
+ fclose(fin);
+ return -1;
+ }
+
+ if (check_elf_hdr(elf_magic)) {
+ status = EINVAL;
+ gencore_log("Process to be dumped is not an ELF.\n");
+ return -1;
+ }
+
+ fclose(fin);
+
+ return elf_magic[EI_CLASS];
+}

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/