[5/6] binutils: od-xcoff: bound XCOFF traceback tag parsing
Checks
| Context |
Check |
Description |
| linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 |
success
|
Test passed
|
| linaro-tcwg-bot/tcwg_binutils_build--master-arm |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_binutils_check--master-arm |
success
|
Test passed
|
Commit Message
dump_xcoff32_tbtags parses the traceback table that follows an XCOFF
function body. The parsing loop and several optional fields advanced
an offset through the section buffer without checking against the
section size. A malformed traceback tag could read past the allocated
buffer (AddressSanitizer: heap-buffer-overflow).
Stop the scan when fewer than 4 bytes remain for the next zero word,
and guard text_size - i with an explicit i <= text_size test. Reject a
symbol value below the .text section start before subtracting it, so
the unsigned difference cannot wrap. Require the 12-byte mandatory
header, then check each optional field (parminfo, tb_offset,
hand_mask, CTL anchors, name, alloca) against the remaining section
size before reading it. Accept a routine name that ends exactly at
the end of the section, which the old check rejected. On a short
read, print a message and stop parsing tags for that function.
Add tests for the control, a symbol value below the .text section
start, a .text section too small for the tag marker, a truncated
mandatory header, truncated parminfo and name_len fields, an oversized
name_len, truncated CTL anchors, an exact-fit routine name, and an
exact-fit name whose alloca byte is missing.
Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
binutils/od-xcoff.c | 34 ++++--
.../binutils-all/xcoff-dump-bounds.exp | 100 +++++++++++++++++-
2 files changed, 123 insertions(+), 11 deletions(-)
@@ -1462,12 +1462,21 @@ dump_xcoff32_tbtags (bfd *abfd,
{
unsigned int i;
+ /* Reject a symbol below the section start before subtracting, because
+ the difference is unsigned. */
+ if (func_start < text_start)
+ {
+ printf (_(" address before section start\n"));
+ return;
+ }
if (func_start - text_start > text_size)
{
printf (_(" address beyond section size\n"));
return;
}
- for (i = func_start - text_start; i < text_size; i+= 4)
+ for (i = func_start - text_start;
+ i <= text_size && text_size - i >= 4;
+ i += 4)
if (bfd_get_32 (abfd, text + i) == 0)
{
unsigned int tb1;
@@ -1475,12 +1484,17 @@ dump_xcoff32_tbtags (bfd *abfd,
unsigned int off;
printf (_(" tags at %08x\n"), i + 4);
- if (i + 8 >= text_size)
+ /* Mandatory traceback header requires 12 bytes:
+ 4-byte zero tag marker + 4-byte tb1 + 4-byte tb2. */
+ if (text_size - i < 12)
goto truncated;
tb1 = bfd_get_32 (abfd, text + i + 4);
tb2 = bfd_get_32 (abfd, text + i + 8);
off = i + 12;
+ /* off is <= text_size here, and every increment below is
+ preceded by a bounds check, so text_size - off cannot
+ underflow. */
printf (" version: %u, lang: %u, global_link: %u, is_eprol: %u, has_tboff: %u, int_proc: %u\n",
(tb1 >> 24) & 0xff,
(tb1 >> 16) & 0xff,
@@ -1515,7 +1529,7 @@ dump_xcoff32_tbtags (bfd *abfd,
{
unsigned int parminfo;
- if (off >= text_size)
+ if (text_size - off < 4)
goto truncated;
parminfo = bfd_get_32 (abfd, text + off);
off += 4;
@@ -1526,7 +1540,7 @@ dump_xcoff32_tbtags (bfd *abfd,
{
unsigned int tboff;
- if (off >= text_size)
+ if (text_size - off < 4)
goto truncated;
tboff = bfd_get_32 (abfd, text + off);
off += 4;
@@ -1537,7 +1551,7 @@ dump_xcoff32_tbtags (bfd *abfd,
{
unsigned int hand_mask;
- if (off >= text_size)
+ if (text_size - off < 4)
goto truncated;
hand_mask = bfd_get_32 (abfd, text + off);
off += 4;
@@ -1548,14 +1562,14 @@ dump_xcoff32_tbtags (bfd *abfd,
unsigned int ctl_info;
unsigned int j;
- if (off >= text_size)
+ if (text_size - off < 4)
goto truncated;
ctl_info = bfd_get_32 (abfd, text + off);
off += 4;
printf (_(" number of CTL anchors: %u\n"), ctl_info);
for (j = 0; j < ctl_info; j++)
{
- if (off >= text_size)
+ if (text_size - off < 4)
goto truncated;
printf (" CTL[%u]: %08x\n",
j, (unsigned)bfd_get_32 (abfd, text + off));
@@ -1567,12 +1581,12 @@ dump_xcoff32_tbtags (bfd *abfd,
unsigned int name_len;
unsigned int j;
- if (off >= text_size)
+ if (text_size - off < 2)
goto truncated;
name_len = bfd_get_16 (abfd, text + off);
off += 2;
printf (_(" Name (len: %u): "), name_len);
- if (off + name_len >= text_size)
+ if (name_len > text_size - off)
{
printf (_("[truncated]\n"));
goto truncated;
@@ -1587,7 +1601,7 @@ dump_xcoff32_tbtags (bfd *abfd,
}
if ((tb1 >> 5) & 1)
{
- if (off >= text_size)
+ if (text_size - off < 1)
goto truncated;
printf (" alloca reg: %u\n",
(unsigned) bfd_get_8 (abfd, text + off));
@@ -544,6 +544,102 @@ xcoff_test "XCOFF dump traceback STYP_TEXT early return memory leak" \
{} \
[list {LeakSanitizer}]
+# Positive control test: unmutated exact-fit traceback object with valid tags.
+# s_flags at 56..59 = 0x20 (STYP_TEXT), x_smtyp at 104 = 2 (XTY_LD),
+# x_smclas at 105 = 0 (XMC_PR).
+set tb_ctrl_obj [xcoff_make $ctrl_obj \
+ "xcoff-tb-ctrl.o" {{int32 56 32} {byte 104 2} {byte 105 0}}]
+
+xcoff_test "XCOFF dump traceback control exact-fit (-P traceback)" \
+ $tb_ctrl_obj "-P traceback" \
+ [list \
+ {tags at 00000004} \
+ {end of tags at 0000000c}]
+
+# .text vaddr 0x1000, symbol value 0x800: reject before subtracting the
+# unsigned difference.
+set tb_before_obj [xcoff_make $tb_ctrl_obj \
+ "xcoff-tb-before.o" {{int32 32 4096} {int32 84 2048}}]
+
+xcoff_test "XCOFF dump traceback address before section start (-P traceback)" \
+ $tb_before_obj "-P traceback" \
+ [list {address before section start}]
+
+# .text size 2 is too small for the 4-byte zero tag marker.
+set tb_small_obj [xcoff_make $tb_ctrl_obj "xcoff-tb-small.o" {{int32 36 2}}]
+
+xcoff_test "XCOFF dump traceback small .text section s_size=2 (-P traceback)" \
+ $tb_small_obj "-P traceback" \
+ [list {no tags found}]
+
+# .text size 9 is too small for the 12-byte mandatory tag header.
+set tb_hdr_obj [xcoff_make $tb_ctrl_obj "xcoff-tb-hdr.o" {{int32 36 9}}]
+
+xcoff_test "XCOFF dump traceback truncated tag header (-P traceback)" \
+ $tb_hdr_obj "-P traceback" \
+ [list {Truncated \.text section}]
+
+# .text size 14 truncates the optional parminfo field.
+set tb_parm_obj [xcoff_make $tb_ctrl_obj \
+ "xcoff-tb-parm.o" {{int32 36 14} {int32 68 2}}]
+
+xcoff_test "XCOFF dump traceback truncated parminfo field (-P traceback)" \
+ $tb_parm_obj "-P traceback" \
+ [list {Truncated \.text section}]
+
+# .text size 13 truncates the optional name_len field.
+set tb_name_obj [xcoff_make $tb_ctrl_obj \
+ "xcoff-tb-name.o" {{int32 36 13} {int32 64 64}}]
+
+xcoff_test "XCOFF dump traceback truncated name_len field (-P traceback)" \
+ $tb_name_obj "-P traceback" \
+ [list {Truncated \.text section}]
+
+# name_len = 100 exceeds the remaining .text bytes.
+set tb_name_large_obj [xcoff_make $tb_ctrl_obj \
+ "xcoff-tb-name-large.o" {{int32 64 64} {byte 72 0} {byte 73 100}}]
+
+xcoff_test "XCOFF dump traceback large name_len truncated (-P traceback)" \
+ $tb_name_large_obj "-P traceback" \
+ [list \
+ {Name \(len: 100\): \[truncated\]} \
+ {Truncated \.text section}]
+
+# ctl_info = 100 anchors exceed the 16-byte .text section.
+set tb_ctl_obj [xcoff_make $tb_ctrl_obj \
+ "xcoff-tb-ctl.o" {{int32 64 2048} {int32 72 100}}]
+
+xcoff_test "XCOFF dump traceback truncated CTL anchors (-P traceback)" \
+ $tb_ctl_obj "-P traceback" \
+ [list \
+ {number of CTL anchors: 100} \
+ {Truncated \.text section}]
+
+# name_len = 2 with off = 14 and text_size = 16 is an exact fit.
+set tb_name_exact_obj [xcoff_make $tb_ctrl_obj \
+ "xcoff-tb-name-exact.o" {
+ {int32 64 64} {byte 72 0} {byte 73 2} {byte 74 102} {byte 75 110}
+ }]
+
+xcoff_test "XCOFF dump traceback exact-fit routine name (-P traceback)" \
+ $tb_name_exact_obj "-P traceback" \
+ [list \
+ {Name \(len: 2\): fn} \
+ {end of tags at 00000010}]
+
+# Exact-fit name with uses_alloca set: the missing alloca byte must
+# report truncation, not read past the section.
+set tb_name_exact_alloca_obj [xcoff_make $tb_ctrl_obj \
+ "xcoff-tb-name-exact-alloca.o" {
+ {int32 64 96} {byte 72 0} {byte 73 2} {byte 74 102} {byte 75 110}
+ }]
+
+xcoff_test "XCOFF dump traceback exact-fit alloca truncation (-P traceback)" \
+ $tb_name_exact_alloca_obj "-P traceback" \
+ [list \
+ {Name \(len: 2\): fn} \
+ {Truncated \.text section}]
+
# s_flags = STYP_TEXT | STYP_DATA; the dumper must test the bit, not
# compare the whole word.
set tb_mask_obj [xcoff_make $ctrl_obj \
@@ -596,4 +692,6 @@ file delete -force $ctrl_obj $multi_obj $over2_obj $over40_obj $str_ovsz_obj \
$loader_empty_st_obj $loader_zero_imp_obj $loader_symoff_obj \
$loader_unterm_obj $loader_trunc_imp_obj $loader_ovistlen_obj \
$loader_unterm_istlen_obj $loader_overrun_strtab_obj \
- $tb_mask_obj $tb_sel_obj $tb_zero_obj
+ $tb_mask_obj $tb_sel_obj $tb_zero_obj $tb_ctrl_obj $tb_before_obj \
+ $tb_small_obj $tb_hdr_obj $tb_parm_obj $tb_name_obj $tb_name_large_obj \
+ $tb_ctl_obj $tb_name_exact_obj $tb_name_exact_alloca_obj