[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v1 1/3] xen/livepatch: Tighten alignment checks.



The ELF specification mentions nothing about the sh_size being
modulo the sh_addralign. Only that sh_addr MUST be aligned on
sh_addralign if sh_addralign is not zero or one.

We on loading did not take this in-to account so this patch adds
two checks: One on the ELF file itself as it is being parsed, and
then when we copy the payload contents in memory - and check the
aligmnent needs there.

Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@xxxxxxxxxx>
---
 xen/common/livepatch.c       | 9 +++++++++
 xen/common/livepatch_elf.c   | 7 +++++++
 xen/include/xen/elfstructs.h | 2 ++
 3 files changed, 18 insertions(+)

diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c
index ca36161..5d53096 100644
--- a/xen/common/livepatch.c
+++ b/xen/common/livepatch.c
@@ -406,6 +406,15 @@ static int move_payload(struct payload *payload, struct 
livepatch_elf *elf)
             ASSERT(offset[i] != UINT_MAX);
 
             elf->sec[i].load_addr = buf + offset[i];
+            if ( elf->sec[i].sec->sh_addralign > 1 &&
+                 ((Elf_Addr)elf->sec[i].load_addr % 
elf->sec[i].sec->sh_addralign) )
+             {
+                dprintk(XENLOG_ERR, LIVEPATCH "%s: %s @ %p is not aligned 
(%"PRIuElfWord")\n",
+                        elf->name, elf->sec[i].name, elf->sec[i].load_addr,
+                        elf->sec[i].sec->sh_addralign);
+                rc = -EINVAL;
+                goto out;
+            }
 
             /* Don't copy NOBITS - such as BSS. */
             if ( elf->sec[i].sec->sh_type != SHT_NOBITS )
diff --git a/xen/common/livepatch_elf.c b/xen/common/livepatch_elf.c
index b69e271..852e9c4 100644
--- a/xen/common/livepatch_elf.c
+++ b/xen/common/livepatch_elf.c
@@ -86,6 +86,13 @@ static int elf_resolve_sections(struct livepatch_elf *elf, 
const void *data)
                     delta < sizeof(Elf_Ehdr) ? "at ELF header" : "is past 
end");
             return -EINVAL;
         }
+        else if ( sec[i].sec->sh_addralign > 1 &&
+                  sec[i].sec->sh_addr % sec[i].sec->sh_addralign )
+        {
+            dprintk(XENLOG_ERR, LIVEPATCH "%s: Section [%u] size 
(%"PRIuElfWord") is not aligned properly (%"PRIuElfWord")\n",
+                    elf->name, i, sec[i].sec->sh_size, 
sec[i].sec->sh_addralign);
+            return -EINVAL;
+        }
         else if ( (sec[i].sec->sh_flags & (SHF_WRITE | SHF_ALLOC)) &&
                   sec[i].sec->sh_type == SHT_NOBITS &&
                   sec[i].sec->sh_size > LIVEPATCH_MAX_SIZE )
diff --git a/xen/include/xen/elfstructs.h b/xen/include/xen/elfstructs.h
index 950e149..edc8862 100644
--- a/xen/include/xen/elfstructs.h
+++ b/xen/include/xen/elfstructs.h
@@ -555,6 +555,7 @@ typedef struct {
 
 #if defined(ELFSIZE) && (ELFSIZE == 32)
 #define PRIxElfAddr    "08x"
+#define PRIuElfWord    "08u"
 
 #define Elf_Ehdr       Elf32_Ehdr
 #define Elf_Phdr       Elf32_Phdr
@@ -582,6 +583,7 @@ typedef struct {
 #define AuxInfo                Aux32Info
 #elif defined(ELFSIZE) && (ELFSIZE == 64)
 #define PRIxElfAddr    PRIx64
+#define PRIuElfWord    PRIu64
 
 #define Elf_Ehdr       Elf64_Ehdr
 #define Elf_Phdr       Elf64_Phdr
-- 
2.9.3


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
https://lists.xen.org/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.