[CRIU] [PATCHv2 1/6] compel/tests: add sections table & string section tests
Dmitry Safonov
dsafonov at virtuozzo.com
Mon May 30 06:14:00 PDT 2016
Now it has 4 new tests:
ok 4 - section table start oob (64-bit ELF)
ok 5 - too many sections in table (64-bit ELF)
ok 6 - strings section's header oob of section table (64-bit ELF)
ok 7 - strings section oob (64-bit ELF)
I.e, if we forget to test string section's header oob with the next diff:
>--- a/compel/handle-elf.c
>+++ b/compel/handle-elf.c
>@@ -122,7 +122,7 @@ static const char *get_strings_section(Ehdr_t *hdr, uintptr_t mem,
> pr_err("String section @%#zx size %#lx is out of [%#zx, %#zx)\n",
> addr, (unsigned long)secstrings_hdr->sh_size,
> mem, mem + size);
>- return NULL;
>+ return (void*)addr;
> }
>
> return (void*)addr;
It will yell with:
ok 1 - zero ELF header (64-bit ELF)
...
not ok 6 - strings section's header oob of section table (64-bit ELF), expected -4 but ret is -1
...
not ok 12 - strings section's header oob of section table (32-bit ELF), expected -4 but ret is -1
Should be more useful when I add relocations tests after all.
(but this seems for me useful too).
Cc: Cyrill Gorcunov <gorcunov at openvz.org>
Signed-off-by: Dmitry Safonov <dsafonov at virtuozzo.com>
---
v2: fixed commit description, so git-am wouldn't fail
compel/include/uapi/elf32-types.h | 4 +++
compel/include/uapi/elf64-types.h | 4 +++
test/compel/handle_binary.c | 53 +++++++++++++++++++++++++++++++++++++++
test/compel/main.c | 6 ++---
4 files changed, 64 insertions(+), 3 deletions(-)
diff --git a/compel/include/uapi/elf32-types.h b/compel/include/uapi/elf32-types.h
index 0a3b08a328fb..51bf4a24a59b 100644
--- a/compel/include/uapi/elf32-types.h
+++ b/compel/include/uapi/elf32-types.h
@@ -7,6 +7,10 @@
#define Rel_t Elf32_Rel
#define Rela_t Elf32_Rela
+#define Off_t Elf32_Off
+#define Word_t Elf32_Word
+#define Half_t Elf32_Half
+
#define ELF_ST_TYPE ELF32_ST_TYPE
#define ELF_ST_BIND ELF32_ST_BIND
diff --git a/compel/include/uapi/elf64-types.h b/compel/include/uapi/elf64-types.h
index 31fcbdb06c71..d4d6f6f163da 100644
--- a/compel/include/uapi/elf64-types.h
+++ b/compel/include/uapi/elf64-types.h
@@ -7,6 +7,10 @@
#define Rel_t Elf64_Rel
#define Rela_t Elf64_Rela
+#define Off_t Elf64_Off
+#define Word_t Elf64_Word
+#define Half_t Elf64_Half
+
#define ELF_ST_TYPE ELF64_ST_TYPE
#define ELF_ST_BIND ELF64_ST_BIND
diff --git a/test/compel/handle_binary.c b/test/compel/handle_binary.c
index 09100427f2bb..a213ed0d3151 100644
--- a/test/compel/handle_binary.c
+++ b/test/compel/handle_binary.c
@@ -6,6 +6,9 @@
#include "arch_test_handle_binary.h"
extern int launch_test(void *mem, int expected_ret, const char *test_fmt, ...);
+extern const size_t test_elf_buf_size;
+
+static const unsigned int sections_nr = 1;
static void set_elf_hdr_relocatable(Ehdr_t *hdr)
{
@@ -13,6 +16,53 @@ static void set_elf_hdr_relocatable(Ehdr_t *hdr)
hdr->e_version = EV_CURRENT;
}
+static int test_add_strings_section(void *elf, const char *msg)
+{
+ Ehdr_t *hdr = elf;
+ Shdr_t *sec_strings_hdr;
+ uintptr_t sections_table = (uintptr_t)elf + hdr->e_shoff;
+ size_t sections_table_size = sections_nr*sizeof(hdr->e_shentsize);
+
+ hdr->e_shnum = sections_nr;
+ hdr->e_shstrndx = sections_nr; /* off-by-one */
+ if (launch_test(elf, -E_NO_STR_SEC,
+ "strings section's header oob of section table %s", msg))
+ return -1;
+
+ hdr->e_shstrndx = 0;
+ sec_strings_hdr = (void *)sections_table;
+
+ sec_strings_hdr->sh_offset = (Off_t)-1;
+ if (launch_test(elf, -E_NO_STR_SEC, "strings section oob %s", msg))
+ return -1;
+
+ /* Put strings just right after sections table. */
+ sec_strings_hdr->sh_offset = sections_table - (uintptr_t)elf +
+ sections_table_size;
+ return 0;
+}
+
+static int test_prepare_section_table(void *elf, const char *msg)
+{
+ Ehdr_t *hdr = elf;
+
+ hdr->e_shoff = (Off_t)test_elf_buf_size;
+ if (launch_test(elf, -E_NO_STR_SEC, "section table start oob %s", msg))
+ return -1;
+
+ /* Lets put sections table right after ELF header. */
+ hdr->e_shoff = (Off_t) sizeof(Ehdr_t);
+ hdr->e_shentsize = (Half_t) sizeof(Shdr_t);
+
+ hdr->e_shnum = (Half_t)-1;
+ if (launch_test(elf, -E_NO_STR_SEC, "too many sections in table %s", msg))
+ return -1;
+
+ if (test_add_strings_section(elf, msg))
+ return -1;
+ return 0;
+}
+
static int test_prepare_elf_header(void *elf, const char *msg)
{
memset(elf, 0, sizeof(Ehdr_t));
@@ -29,6 +79,9 @@ static int test_prepare_elf_header(void *elf, const char *msg)
set_elf_hdr_relocatable(elf);
+ if (test_prepare_section_table(elf, msg))
+ return -1;
+
return 0;
}
diff --git a/test/compel/main.c b/test/compel/main.c
index 869d5f7a2240..67c4c84304b3 100644
--- a/test/compel/main.c
+++ b/test/compel/main.c
@@ -18,7 +18,7 @@
#include "arch_test_handle_binary.h"
/* size of buffer with formed ELF file */
-#define ELF_BUF_SIZE 4096
+const size_t test_elf_buf_size = 4096;
extern int handle_binary(void *mem, size_t size);
extern void run_tests(void *mem);
@@ -33,7 +33,7 @@ piegen_opt_t opts = {
int launch_test(void *mem, int expected_ret, const char *test_fmt, ...)
{
static unsigned test_nr = 1;
- int ret = handle_binary(mem, ELF_BUF_SIZE);
+ int ret = handle_binary(mem, test_elf_buf_size);
va_list params;
va_start(params, test_fmt);
@@ -55,7 +55,7 @@ int launch_test(void *mem, int expected_ret, const char *test_fmt, ...)
int main(int argc, char **argv)
{
- void *elf_buf = malloc(ELF_BUF_SIZE);
+ void *elf_buf = malloc(test_elf_buf_size);
arch_run_tests(elf_buf);
free(elf_buf);
--
2.8.2
More information about the CRIU
mailing list