xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libelf: Fix div0 issues in elf_{shdr, phdr}_count()
@ 2016-12-08 14:18 Andrew Cooper
  2016-12-08 14:41 ` Jan Beulich
  0 siblings, 1 reply; 50+ messages in thread
From: Andrew Cooper @ 2016-12-08 14:18 UTC (permalink / raw)
  To: Xen-devel
  Cc: Stefano Stabellini, Wei Liu, George Dunlap, Andrew Cooper,
	Ian Jackson, Tim Deegan, Jan Beulich

elf_uval() can return zero either because the field itself is zero, or because
the access is out of bounds.

c/s a01b6d4 "libelf: treat phdr and shdr similarly" introduced two div0 issues
as e_{ph,sh}entsize are not checked for sanity before being used to divide
elf->size.

Spotted by Coverity.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: George Dunlap <George.Dunlap@eu.citrix.com>
CC: Ian Jackson <ian.jackson@eu.citrix.com>
CC: Jan Beulich <JBeulich@suse.com>
CC: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
CC: Stefano Stabellini <sstabellini@kernel.org>
CC: Tim Deegan <tim@xen.org>
CC: Wei Liu <wei.liu2@citrix.com>

I experimented with making elf_access_unsigned() __must_check, but this didn't
cause a compiler error.  I am not quite sure why.
---
 xen/common/libelf/libelf-tools.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/xen/common/libelf/libelf-tools.c b/xen/common/libelf/libelf-tools.c
index bf661e7..f62d9c3 100644
--- a/xen/common/libelf/libelf-tools.c
+++ b/xen/common/libelf/libelf-tools.c
@@ -130,11 +130,17 @@ uint64_t elf_round_up(struct elf_binary *elf, uint64_t addr)
 unsigned elf_shdr_count(struct elf_binary *elf)
 {
     unsigned count = elf_uval(elf, elf->ehdr, e_shnum);
+    unsigned entsize = elf_uval(elf, elf->ehdr, e_shentsize);
     uint64_t max;
 
     if ( !count )
         return 0;
-    max = elf->size / elf_uval(elf, elf->ehdr, e_shentsize);
+    if ( !entsize )
+    {
+        elf_mark_broken(elf, "e_shentsize is zero");
+        return 0;
+    }
+    max = elf->size / entsize;
     if ( max > UINT_MAX )
         max = UINT_MAX;
     if ( count > max )
@@ -148,11 +154,17 @@ unsigned elf_shdr_count(struct elf_binary *elf)
 unsigned elf_phdr_count(struct elf_binary *elf)
 {
     unsigned count = elf_uval(elf, elf->ehdr, e_phnum);
+    unsigned entsize = elf_uval(elf, elf->ehdr, e_phentsize);
     uint64_t max;
 
     if ( !count )
         return 0;
-    max = elf->size / elf_uval(elf, elf->ehdr, e_phentsize);
+    if ( !entsize )
+    {
+        elf_mark_broken(elf, "e_phentsize is zero");
+        return 0;
+    }
+    max = elf->size / entsize;
     if ( max > UINT_MAX )
         max = UINT_MAX;
     if ( count > max )
-- 
2.1.4


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

^ permalink raw reply related	[flat|nested] 50+ messages in thread

end of thread, other threads:[~2016-12-16 12:31 UTC | newest]

Thread overview: 50+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-12-08 14:18 [PATCH] libelf: Fix div0 issues in elf_{shdr, phdr}_count() Andrew Cooper
2016-12-08 14:41 ` Jan Beulich
2016-12-08 14:46   ` Andrew Cooper
2016-12-08 15:17     ` Jan Beulich
2016-12-08 15:23       ` Andrew Cooper
2016-12-08 15:47         ` Ian Jackson
2016-12-08 16:09           ` Jan Beulich
2016-12-08 17:28           ` Ian Jackson
2016-12-09  8:38             ` Jan Beulich
2016-12-09 11:54               ` Ian Jackson
2016-12-09 13:03                 ` Jan Beulich
2016-12-09 15:44                 ` [PATCH 0/8] libelf: safety enhancements Ian Jackson
2016-12-09 15:44                   ` [PATCH 1/8] libelf: loop safety: Introduce elf_iter_ok and elf_strcmp_safe Ian Jackson
2016-12-12 15:02                     ` Jan Beulich
2016-12-12 15:23                       ` Ian Jackson
2016-12-12 15:15                     ` Jan Beulich
2016-12-12 15:51                     ` Jan Beulich
2016-12-12 16:00                       ` Ian Jackson
2016-12-12 16:16                         ` Jan Beulich
2016-12-12 16:56                           ` Ian Jackson
2016-12-13  7:24                             ` Jan Beulich
2016-12-13 16:04                               ` Ian Jackson
2016-12-13 16:37                                 ` Jan Beulich
2016-12-09 15:44                   ` [PATCH 2/8] libelf: loop safety: Pass `elf' to elf_xen_parse_features Ian Jackson
2016-12-12 15:03                     ` Jan Beulich
2016-12-09 15:44                   ` [PATCH 3/8] libelf: loop safety: Call elf_iter_ok[_counted] in every loop Ian Jackson
2016-12-12 15:12                     ` Jan Beulich
2016-12-12 15:38                       ` Ian Jackson
2016-12-12 15:56                         ` Jan Beulich
2016-12-12 16:02                           ` Ian Jackson
2016-12-09 15:44                   ` [PATCH 4/8] libelf: loop safety: Call elf_iter_ok_counted at every *mem*_unsafe Ian Jackson
2016-12-12 15:19                     ` Jan Beulich
2016-12-12 15:54                       ` Ian Jackson
2016-12-12 15:58                         ` Jan Beulich
2016-12-12 16:03                           ` Ian Jackson
2016-12-09 15:44                   ` [PATCH 5/8] libelf: loop safety: Replace all calls to strcmp Ian Jackson
2016-12-12 15:22                     ` Jan Beulich
2016-12-12 15:44                       ` Ian Jackson
2016-12-09 15:44                   ` [PATCH 6/8] libelf: loop safety cleanup: Remove obsolete check in elf_shdr_count Ian Jackson
2016-12-12 15:41                     ` Jan Beulich
2016-12-09 15:44                   ` [PATCH 7/8] libelf: loop safety cleanup: Remove superseded image size copy check Ian Jackson
2016-12-12 16:26                     ` Jan Beulich
2016-12-09 15:44                   ` [PATCH 8/8] libelf: safety: Document safety principles in header file Ian Jackson
2016-12-15 16:43                     ` Jan Beulich
2016-12-16  4:28                       ` George Dunlap
2016-12-16 11:33                         ` Ian Jackson
2016-12-16 11:58                           ` Jan Beulich
2016-12-16 11:43                       ` Ian Jackson
2016-12-16 12:31                         ` Jan Beulich
2016-12-08 14:48   ` [PATCH] libelf: Fix div0 issues in elf_{shdr, phdr}_count() Ian Jackson

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).