diff mbox series

dwarf-reader: handle symtab.section_header.sh_entsize == 0

Message ID 20200124225623.59279-1-maennich@google.com
State Committed
Headers show
Series dwarf-reader: handle symtab.section_header.sh_entsize == 0 | expand

Commit Message

Matthias Maennich via libabigail Jan. 1, 2020, midnight UTC
A broken elf file with a sh_entsize of 0 makes the dwarf reader crash
due to a division by zero. Fix this by validating the input and exiting
early in that case.

	* src/abg-dwarf-reader.cc (load_symbol_maps_from_symtab_section):
	Handle elf file with invalid sh_entsize.
	* tests/test-read-dwarf.cc (test_task::perform): handle empty
	in_abi_path and out_abi_path as 'read only' test.
	(InOutSpec): add test case.
	* tests/data/test-read-dwarf/test25-bogus-binary.elf: new test data.

Signed-off-by: Matthias Maennich <maennich@google.com>
---
 src/abg-dwarf-reader.cc                         |   5 +++++
 .../test-read-dwarf/test25-bogus-binary.elf     | Bin 0 -> 95769 bytes
 tests/test-read-dwarf.cc                        |  12 ++++++++++++
 3 files changed, 17 insertions(+)
 create mode 100644 tests/data/test-read-dwarf/test25-bogus-binary.elf

Comments

Dodji Seketeli Jan. 1, 2020, midnight UTC | #1
Hello Matthias,

Matthias Maennich <maennich@google.com> a ?crit:

> A broken elf file with a sh_entsize of 0 makes the dwarf reader crash
> due to a division by zero. Fix this by validating the input and exiting
> early in that case.
>
> 	* src/abg-dwarf-reader.cc (load_symbol_maps_from_symtab_section):
> 	Handle elf file with invalid sh_entsize.
> 	* tests/test-read-dwarf.cc (test_task::perform): handle empty
> 	in_abi_path and out_abi_path as 'read only' test.
> 	(InOutSpec): add test case.
> 	* tests/data/test-read-dwarf/test25-bogus-binary.elf: new test data.

This is OK to commit to master.

Thanks!
Matthias Maennich via libabigail Jan. 1, 2020, midnight UTC | #2
On Mon, Jan 27, 2020 at 04:20:21PM +0100, Dodji Seketeli wrote:
>Hello Matthias,
>
>Matthias Maennich <maennich@google.com> a ?crit:
>
>> A broken elf file with a sh_entsize of 0 makes the dwarf reader crash
>> due to a division by zero. Fix this by validating the input and exiting
>> early in that case.
>>
>> 	* src/abg-dwarf-reader.cc (load_symbol_maps_from_symtab_section):
>> 	Handle elf file with invalid sh_entsize.
>> 	* tests/test-read-dwarf.cc (test_task::perform): handle empty
>> 	in_abi_path and out_abi_path as 'read only' test.
>> 	(InOutSpec): add test case.
>> 	* tests/data/test-read-dwarf/test25-bogus-binary.elf: new test data.
>
>This is OK to commit to master.

Done. Thanks!

Cheers,
Matthias

>
>Thanks!
>
>-- 
>		Dodji
diff mbox series

Patch

diff --git a/src/abg-dwarf-reader.cc b/src/abg-dwarf-reader.cc
index d3efb02a9dbf..555170eccdf0 100644
--- a/src/abg-dwarf-reader.cc
+++ b/src/abg-dwarf-reader.cc
@@ -7388,6 +7388,11 @@  public:
     GElf_Shdr header_mem;
     GElf_Shdr* symtab_sheader = gelf_getshdr(symtab_section,
 					     &header_mem);
+
+    // check for bogus section header
+    if (symtab_sheader->sh_entsize == 0)
+      return false;
+
     size_t nb_syms = symtab_sheader->sh_size / symtab_sheader->sh_entsize;
 
     Elf_Data* symtab = elf_getdata(symtab_section, 0);
diff --git a/tests/data/test-read-dwarf/test25-bogus-binary.elf b/tests/data/test-read-dwarf/test25-bogus-binary.elf
new file mode 100644
index 0000000000000000000000000000000000000000..b00a9696232bd813aa413808e56e6875a3069a03
GIT binary patch
literal 95769
zcmeHQU1(j$5#DpH9645MnnF~mgjgoA<3w>}OLlA*Rgw`>lb}Kirj*DgT+4#UBzllj
z#iDSp?G)u9bx0mm`jog(XbB`xLPK09G=A`VLfz5^0wsk8QCvd|j-9l3`_1g0-+S-T
z{nP!^4AMF0o}c}hot>SXnVro}OiiXzPU^($@wxQ;?&EXwOOx4!^wh*eHl;RYl>x_|
z)j6eb?YQn8{YrJ<?!*pyG#z}NURctAhjMh(`t7&h{HIc#i`j{s%1%uEY%=9Il*H=w
zYpY?n&;ICse)c}CQc4Y*@AWlvrd(c~rZ3elrBcJgdRQa9Nyl+cGfZinH8-@$kDpNy
zJJkWDev6OPT`F_q!r50cnascd9%ROQI6++E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of?dJx%1TX{hme$z
zl#rC|1si4fj7;M9vIZr#u+Nh>`|vQq-W!c&L@|Q$;~TMU&QVB-p9eoHO&D7aFa#+D
z83!A5l^%pCf(<|HJI9$P|BSdw<L~4(+h_Nl@s%GYB7G^kkzc?W2w6sQjfS_R;Fb)s
zt+U;6L(8_#o)w$J9k36x7X{f&kr7p_y|(8CGhv8vrdqqwRpnJ-SP+8d08n00Lzk<D
z&eu|GxUFo$L#+%7vS+)hVBvw#jnGXcbR)t9bh0&E2U?XdK|WViM)*J^s6I#{!VZKT
zRKgAn-5hzL|40DXruAi|w$UTn=2yRf?P#$r>lfa91n2ik0rP(B!^iPuD4|j=zrsro
z7Jt!hEGHA{BQz(uiKgO~18d>=;cE30>iF)L#oHqE#BOuasfLKlrM<n#LIG2j6+J1m
z6xAkER0WVRo2Qt~T@rUsDY)1-Ku{wl)FB+%OLPfpx(K`(2in<K*1gns-3tciNTbLP
z!9X$w&_fMVlh{?sh?1v*feJAov8yUXT$_Dn7;Ov1u4*&<OLZEI0bzs<Zh$7qzU3HU
ziv(3^lIE3S2nIqJNa{RwU$>mFnDb<<g7|5j>$CMY3bVLA@<e)mAO`7$mtK4Q{PN{1
z<FuyqGGcLM&5CZ50(7Dy04M@;Mi~e*3cNW=TuIlyE({a`Af%x+14{!VpUAcVx7qvY
zS=knV3DYA;oSTbKCGeIZ)EZjW!SdsdfTsCHwHlHJ<WewDI}C_Mp?0`z<&+GgU2oOu
z+Iu3E9yYI)u`Ok9J`4yaY`!*;5(x$b1A+m;Koeo$voq(Ls5A*hi4^qOAKyxNwc(E<
TWo>wA5(>~V1x=_!vpxDhWLwe2

literal 0
HcmV?d00001

diff --git a/tests/test-read-dwarf.cc b/tests/test-read-dwarf.cc
index 7c656964c93c..8f460fef3f8f 100644
--- a/tests/test-read-dwarf.cc
+++ b/tests/test-read-dwarf.cc
@@ -253,6 +253,12 @@  InOutSpec in_out_specs[] =
     "output/test-read-dwarf/PR25042-libgdbm-clang-dwarf5.so.6.0.0.abi",
   },
 #endif
+  {
+    "data/test-read-dwarf/test25-bogus-binary.elf",
+    "",
+    "",
+    "",
+  },
   // This should be the last entry.
   {NULL, NULL, NULL, NULL}
 };
@@ -324,6 +330,12 @@  struct test_task : public abigail::workers::task
       set_suppressions(*ctxt, in_suppr_spec_path);
 
     abigail::corpus_sptr corp = read_corpus_from_elf(*ctxt, status);
+    // if there is no output and no input, assume that we do not care about the
+    // actual read result, just that it succeeded.
+    if (in_abi_path.empty() && out_abi_path.empty()) {
+	// Phew! we made it here and we did not crash! yay!
+	return;
+    }
     if (!corp)
       {
 	error_message = string("failed to read ") + in_elf_path  + "\n";