[CRIU] [PATCH 3/3] compel: define arch_is_machine_supported

Dmitry Safonov dsafonov at virtuozzo.com
Fri Apr 29 12:47:55 PDT 2016


Check if ELF header complements ELF magic in per-arch way.

Cc: Cyrill Gorcunov <gorcunov at openvz.org>
Signed-off-by: Dmitry Safonov <dsafonov at virtuozzo.com>
---
 compel/arch/aarch64/include/handle-elf.h |  1 +
 compel/arch/arm/include/handle-elf.h     |  1 +
 compel/arch/ppc64/include/handle-elf.h   |  1 +
 compel/arch/x86/include/handle-elf.h     |  2 ++
 compel/handle-elf.c                      | 20 ++++++++++----------
 5 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/compel/arch/aarch64/include/handle-elf.h b/compel/arch/aarch64/include/handle-elf.h
index e9c5036f2a08..7d7bfb7b3ade 100644
--- a/compel/arch/aarch64/include/handle-elf.h
+++ b/compel/arch/aarch64/include/handle-elf.h
@@ -4,6 +4,7 @@
 #include "uapi/elf64-types.h"
 
 #define __handle_elf	handle_elf_aarch64
+#define arch_is_machine_supported(e_machine)   (e_machine == EM_AARCH64)
 
 extern int handle_elf_aarch64(void *mem, size_t size);
 
diff --git a/compel/arch/arm/include/handle-elf.h b/compel/arch/arm/include/handle-elf.h
index b44ed9f6a839..a465f427397c 100644
--- a/compel/arch/arm/include/handle-elf.h
+++ b/compel/arch/arm/include/handle-elf.h
@@ -4,6 +4,7 @@
 #include "uapi/elf32-types.h"
 
 #define __handle_elf	handle_elf_arm
+#define arch_is_machine_supported(e_machine)   (e_machine == EM_ARM)
 
 extern int handle_elf_arm(void *mem, size_t size);
 
diff --git a/compel/arch/ppc64/include/handle-elf.h b/compel/arch/ppc64/include/handle-elf.h
index c6f23123d94d..ec0cad159fe4 100644
--- a/compel/arch/ppc64/include/handle-elf.h
+++ b/compel/arch/ppc64/include/handle-elf.h
@@ -5,6 +5,7 @@
 
 #define ELF_PPC64
 #define __handle_elf	handle_elf_ppc64
+#define arch_is_machine_supported(e_machine)	(e_machine == EM_PPC64)
 
 extern int handle_elf_ppc64(void *mem, size_t size);
 
diff --git a/compel/arch/x86/include/handle-elf.h b/compel/arch/x86/include/handle-elf.h
index e02098dbe385..eea94d4633ab 100644
--- a/compel/arch/x86/include/handle-elf.h
+++ b/compel/arch/x86/include/handle-elf.h
@@ -6,12 +6,14 @@
 #include "uapi/elf32-types.h"
 #define ELF_X86_32
 #define __handle_elf	handle_elf_x86_32
+#define arch_is_machine_supported(e_machine)	(e_machine == EM_386)
 
 #else /* CONFIG_X86_64 */
 
 #include "uapi/elf64-types.h"
 #define ELF_X86_64
 #define __handle_elf	handle_elf_x86_64
+#define arch_is_machine_supported(e_machine)	(e_machine == EM_X86_64)
 
 #endif
 
diff --git a/compel/handle-elf.c b/compel/handle-elf.c
index 70a6d376ef7e..9c6fb7e366de 100644
--- a/compel/handle-elf.c
+++ b/compel/handle-elf.c
@@ -61,6 +61,15 @@ static int do_relative_toc(long value, uint16_t *location,
 }
 #endif
 
+static bool is_header_supported(Ehdr_t *hdr)
+{
+	if (!arch_is_machine_supported(hdr->e_machine))
+		return false;
+	if (hdr->e_type != ET_REL || hdr->e_version != EV_CURRENT)
+		return false;
+	return true;
+}
+
 int __handle_elf(void *mem, size_t size)
 {
 	const char *symstrings = NULL;
@@ -83,19 +92,10 @@ int __handle_elf(void *mem, size_t size)
 	pr_debug("\ttype 0x%x machine 0x%x version 0x%x\n",
 		 (unsigned)hdr->e_type, (unsigned)hdr->e_machine, (unsigned)hdr->e_version);
 
-#ifdef ELF_X86_64
-	if (hdr->e_type != ET_REL || hdr->e_machine != EM_X86_64 || hdr->e_version != EV_CURRENT) {
+	if (!is_header_supported(hdr)) {
 		pr_err("Unsupported header detected\n");
 		goto err;
 	}
-#endif
-
-#ifdef ELF_X86_32
-	if (hdr->e_type != ET_REL || hdr->e_machine != EM_386 || hdr->e_version != EV_CURRENT) {
-		pr_err("Unsupported header detected\n");
-		goto err;
-	}
-#endif
 
 	sec_hdrs = malloc(sizeof(*sec_hdrs) * hdr->e_shnum);
 	if (!sec_hdrs) {
-- 
2.8.0



More information about the CRIU mailing list