xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xen.org>
Cc: Stefano Stabellini <sstabellini@kernel.org>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	Ross Lagerwall <ross.lagerwall@citrix.com>,
	Julien Grall <julien.grall@arm.com>,
	Jan Beulich <JBeulich@suse.com>
Subject: [PATCH 2/2] xen/livepatch: Don't crash on encountering STN_UNDEF relocations
Date: Tue, 13 Jun 2017 21:51:36 +0100	[thread overview]
Message-ID: <1497387096-19058-2-git-send-email-andrew.cooper3@citrix.com> (raw)
In-Reply-To: <1497387096-19058-1-git-send-email-andrew.cooper3@citrix.com>

A symndx of STN_UNDEF is special, and means a symbol value of 0.

There is no real symbol data for it, so avoid tripping over a NULL pointer
with "elf->sym[symndx].sym->st_value".

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
CC: Ross Lagerwall <ross.lagerwall@citrix.com>
CC: Jan Beulich <JBeulich@suse.com>
CC: Stefano Stabellini <sstabellini@kernel.org>
CC: Julien Grall <julien.grall@arm.com>

Functionally tested on x86, but both arm variants look to suffer from the same
issue.  Compile tested on all architectures.

TODO: Figure out how my livepatch has a STN_UNDEF relocation...
---
 xen/arch/arm/arm32/livepatch.c | 14 +++++++++++---
 xen/arch/arm/arm64/livepatch.c | 14 +++++++++++---
 xen/arch/x86/livepatch.c       | 14 +++++++++++---
 3 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/xen/arch/arm/arm32/livepatch.c b/xen/arch/arm/arm32/livepatch.c
index a328179..0f7990a 100644
--- a/xen/arch/arm/arm32/livepatch.c
+++ b/xen/arch/arm/arm32/livepatch.c
@@ -254,14 +254,22 @@ int arch_livepatch_perform(struct livepatch_elf *elf,
             addend = get_addend(type, dest);
         }
 
-        if ( symndx > elf->nsym )
+        if ( symndx == STN_UNDEF )
+            val = 0;
+        else if ( symndx > elf->nsym )
         {
             dprintk(XENLOG_ERR, LIVEPATCH "%s: Relative symbol wants symbol@%u which is past end!\n",
                     elf->name, symndx);
             return -EINVAL;
         }
-
-        val = elf->sym[symndx].sym->st_value; /* S */
+        else if ( !elf->sym[symndx].sym )
+        {
+            dprintk(XENLOG_ERR, LIVEPATCH "%s: No relative symbol@%u\n",
+                    elf->name, symndx);
+            return -EINVAL;
+        }
+        else
+            val = elf->sym[symndx].sym->st_value; /* S */
 
         rc = perform_rel(type, dest, val, addend);
         switch ( rc )
diff --git a/xen/arch/arm/arm64/livepatch.c b/xen/arch/arm/arm64/livepatch.c
index 63929b1..476e238 100644
--- a/xen/arch/arm/arm64/livepatch.c
+++ b/xen/arch/arm/arm64/livepatch.c
@@ -252,14 +252,22 @@ int arch_livepatch_perform_rela(struct livepatch_elf *elf,
         int ovf = 0;
         uint64_t val;
 
-        if ( symndx > elf->nsym )
+        if ( symndx == STN_UNDEF )
+            val = 0;
+        else if ( symndx > elf->nsym )
         {
             dprintk(XENLOG_ERR, LIVEPATCH "%s: Relative relocation wants symbol@%u which is past end!\n",
                     elf->name, symndx);
             return -EINVAL;
         }
-
-        val = elf->sym[symndx].sym->st_value +  r->r_addend; /* S+A */
+        else if ( !elf->sym[symndx].sym )
+        {
+            dprintk(XENLOG_ERR, LIVEPATCH "%s: No relative symbol@%u\n",
+                    elf->name, symndx);
+            return -EINVAL;
+        }
+        else
+            val = elf->sym[symndx].sym->st_value + r->r_addend; /* S+A */
 
         /* ARM64 operations at minimum are always 32-bit. */
         if ( r->r_offset >= base->sec->sh_size ||
diff --git a/xen/arch/x86/livepatch.c b/xen/arch/x86/livepatch.c
index 7917610..6f44128 100644
--- a/xen/arch/x86/livepatch.c
+++ b/xen/arch/x86/livepatch.c
@@ -170,14 +170,22 @@ int arch_livepatch_perform_rela(struct livepatch_elf *elf,
         uint8_t *dest = base->load_addr + r->r_offset;
         uint64_t val;
 
-        if ( symndx > elf->nsym )
+        if ( symndx == STN_UNDEF )
+            val = 0;
+        else if ( symndx > elf->nsym )
         {
             dprintk(XENLOG_ERR, LIVEPATCH "%s: Relative relocation wants symbol@%u which is past end!\n",
                     elf->name, symndx);
             return -EINVAL;
         }
-
-        val = r->r_addend + elf->sym[symndx].sym->st_value;
+        else if ( !elf->sym[symndx].sym )
+        {
+            dprintk(XENLOG_ERR, LIVEPATCH "%s: No symbol@%u\n",
+                    elf->name, symndx);
+            return -EINVAL;
+        }
+        else
+            val = r->r_addend + elf->sym[symndx].sym->st_value;
 
         switch ( ELF64_R_TYPE(r->r_info) )
         {
-- 
2.1.4


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

  reply	other threads:[~2017-06-13 20:51 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-13 20:51 [PATCH 1/2] xen/livepatch: Clean up arch relocation handling Andrew Cooper
2017-06-13 20:51 ` Andrew Cooper [this message]
2017-06-13 21:13   ` [PATCH 2/2] xen/livepatch: Don't crash on encountering STN_UNDEF relocations Andrew Cooper
2017-06-14 10:03     ` Jan Beulich
2017-06-14 10:11   ` Jan Beulich
2017-06-14 10:13     ` Andrew Cooper
2017-06-14 10:24       ` Jan Beulich
2017-06-14 14:18         ` Konrad Rzeszutek Wilk
2017-06-14 18:33           ` Andrew Cooper
2017-06-14 18:49             ` Jan Beulich
2017-06-19 18:30               ` Konrad Rzeszutek Wilk
2017-06-19 23:05                 ` Andrew Cooper
2017-06-20  7:15                   ` Jan Beulich
2017-06-20 13:30                     ` Konrad Rzeszutek Wilk
2017-06-14 19:08             ` Konrad Rzeszutek Wilk
2017-06-21 18:13   ` [PATCH for-4.9 v2] " Andrew Cooper
2017-06-22  1:26     ` Konrad Rzeszutek Wilk
2017-06-22 15:27       ` Konrad Rzeszutek Wilk
2017-06-22 16:10         ` Konrad Rzeszutek Wilk
2017-06-22 16:33           ` Konrad Rzeszutek Wilk
2017-06-22 17:05             ` Konrad Rzeszutek Wilk
2017-06-23  9:44               ` Jan Beulich
2017-06-22  7:40     ` Jan Beulich
2017-06-22  9:49     ` Ross Lagerwall
2017-06-14  9:25 ` [PATCH 1/2] xen/livepatch: Clean up arch relocation handling Jan Beulich
2017-06-14 13:44 ` Konrad Rzeszutek Wilk
2017-06-14 14:02   ` Jan Beulich
2017-06-14 18:28     ` Andrew Cooper
2017-06-19 18:18       ` Konrad Rzeszutek Wilk
2017-06-20  7:36         ` Jan Beulich
2017-06-20  7:39           ` Andrew Cooper
2017-06-20  7:41             ` Andrew Cooper
2017-06-20  7:56             ` Jan Beulich
2017-06-20 13:36               ` Konrad Rzeszutek Wilk
2017-06-22  1:27 ` Is [PATCH for-4.9] Was:Re: " Konrad Rzeszutek Wilk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1497387096-19058-2-git-send-email-andrew.cooper3@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=JBeulich@suse.com \
    --cc=julien.grall@arm.com \
    --cc=ross.lagerwall@citrix.com \
    --cc=sstabellini@kernel.org \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).