[PATCH 3/5] RISC-V: check ULEB128 relocation field bounds
Harshit Kumar
harshit7kr@gmail.com
Tue Nov 10 20:15:20 GMT 2026
R_RISCV_SET_ULEB128 and R_RISCV_SUB_ULEB128 have a HOWTO size of zero
because their encoded length varies, so the range check added by the
previous patch accepts an offset equal to the section size. For
R_RISCV_SUB_ULEB128 at that offset, perform_relocation decodes zero
bytes, computes len - 1 with len == 0 and passes (size_t) -1 to memset.
Before applying either relocation, check that at least one byte exists
at r_offset and that the ULEB128 sequence terminates within
input_section->size. The limit is input_section->size, because
perform_relocation decodes only up to it.
An unterminated sequence was previously truncated silently, and ld
linked such an object with exit status 0. GAS never emits one, so
reject it.
Also make perform_relocation return bfd_reloc_outofrange when the
decoded length is zero. The new check makes that case unreachable;
it is an extra safety check.
bfd/
PR ld/34987
* elfnn-riscv.c (perform_relocation): Return bfd_reloc_outofrange
if the decoded ULEB128 length is zero.
(riscv_elf_relocate_section): Check that ULEB128 relocation
fields lie within input_section->size and terminate.
ld/
PR ld/34987
* testsuite/ld-riscv-elf/uleb128-reloc-bounds*: New tests.
* testsuite/ld-riscv-elf/uleb128-unterminated*: New tests.
* testsuite/ld-riscv-elf/uleb128-multibyte-last.*: New test.
* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Run them.
Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
bfd/elfnn-riscv.c | 33 +++++++++++++++++++
ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 6 ++++
.../ld-riscv-elf/uleb128-multibyte-last.d | 10 ++++++
.../ld-riscv-elf/uleb128-multibyte-last.s | 10 ++++++
.../uleb128-reloc-bounds-control.d | 10 ++++++
.../ld-riscv-elf/uleb128-reloc-bounds-pass.s | 10 ++++++
.../ld-riscv-elf/uleb128-reloc-bounds-rv32.d | 6 ++++
.../ld-riscv-elf/uleb128-reloc-bounds.d | 5 +++
.../ld-riscv-elf/uleb128-reloc-bounds.s | 9 +++++
.../uleb128-unterminated-control.d | 10 ++++++
.../ld-riscv-elf/uleb128-unterminated-pass.s | 10 ++++++
.../ld-riscv-elf/uleb128-unterminated.d | 5 +++
.../ld-riscv-elf/uleb128-unterminated.s | 12 +++++++
13 files changed, 136 insertions(+)
create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-multibyte-last.d
create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-multibyte-last.s
create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-control.d
create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-pass.s
create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-rv32.d
create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds.d
create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds.s
create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-unterminated-control.d
create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-unterminated-pass.s
create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-unterminated.d
create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-unterminated.s
diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index b96586f522..34721be876 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -2629,6 +2629,8 @@ perform_relocation (const reloc_howto_type *howto,
endp = p - 1;
p = contents + rel->r_offset;
size_t len = endp + 1 - p;
+ if (len == 0)
+ return bfd_reloc_outofrange;
memset (p, 0x80, len - 1);
*endp = 0;
@@ -3161,6 +3163,37 @@ riscv_elf_relocate_section (struct bfd_link_info *info,
rel->r_offset, reloc_size, sec_limit))
goto out;
+ /* ULEB128 fields have no fixed size. Check that the field starts
+ inside the section and that its byte sequence terminates there.
+ Use input_section->size, because perform_relocation decodes the value
+ only up to it. A terminator in the stale tail left by relaxation
+ must not pass this check. */
+ if (r_type == R_RISCV_SET_ULEB128 || r_type == R_RISCV_SUB_ULEB128)
+ {
+ const bfd_byte *p;
+ const bfd_byte *end;
+ bfd_vma uleb_limit = input_section->size;
+
+ if (!riscv_reloc_field_in_range (input_bfd, input_section,
+ rel->r_offset, 1, uleb_limit))
+ goto out;
+
+ p = contents + rel->r_offset;
+ end = contents + uleb_limit;
+ while (p < end && (*p & 0x80) != 0)
+ p++;
+ if (p >= end)
+ {
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB: unterminated ULEB128 relocation field at offset "
+ "%#" PRIx64 " in section `%pA'"),
+ input_bfd, (uint64_t) rel->r_offset, input_section);
+ bfd_set_error (bfd_error_bad_value);
+ goto out;
+ }
+ }
+
/* This is a final link. */
r_symndx = ELFNN_R_SYM (rel->r_info);
h = NULL;
diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
index 4e3a227800..9f7f453468 100644
--- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
+++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
@@ -367,6 +367,11 @@ if [istarget "riscv*-*-*"] {
run_dump_test "set8-reloc-bounds-control"
run_dump_test "set32-reloc-bounds"
run_dump_test "set32-reloc-bounds-control"
+ run_dump_test "uleb128-reloc-bounds"
+ run_dump_test "uleb128-reloc-bounds-control"
+ run_dump_test "uleb128-unterminated"
+ run_dump_test "uleb128-unterminated-control"
+ run_dump_test "uleb128-multibyte-last"
run_dump_test "tlsdesc-call-bounds"
run_dump_test "tlsdesc-call-bounds-control"
run_dump_test "tlsdesc-call-bounds-rv32"
@@ -378,6 +383,7 @@ if [istarget "riscv*-*-*"] {
run_dump_test "call-bounds-norelax-rv32-pass"
run_dump_test "call-plt-bounds-norelax-fail"
run_dump_test "call-plt-bounds-norelax-control"
+ run_dump_test "uleb128-reloc-bounds-rv32"
# TODO: Make the following tests work under RV32.
if [istarget "riscv32-*-*"] {
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-multibyte-last.d b/ld/testsuite/ld-riscv-elf/uleb128-multibyte-last.d
new file mode 100644
index 0000000000..ef462c299a
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-multibyte-last.d
@@ -0,0 +1,10 @@
+#name: RISC-V multibyte ULEB128 field ending at section end
+#source: uleb128-multibyte-last.s
+#ld: -e _start
+#readelf: -h
+#target: riscv*-*-*
+
+ELF Header:
+ Magic: 7f 45 4c 46 .*
+ Class: ELF(32|64)
+#pass
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-multibyte-last.s b/ld/testsuite/ld-riscv-elf/uleb128-multibyte-last.s
new file mode 100644
index 0000000000..22fc09daf4
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-multibyte-last.s
@@ -0,0 +1,10 @@
+ .text
+ .globl _start
+_start:
+ .fill 14, 1, 0
+ .reloc ., R_RISCV_SET_ULEB128, foo0
+ .reloc ., R_RISCV_SUB_ULEB128, foo0
+ .byte 0x80, 0x01
+ .data
+foo0:
+ .byte 0x93
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-control.d b/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-control.d
new file mode 100644
index 0000000000..cc4485cd3b
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-control.d
@@ -0,0 +1,10 @@
+#name: RISC-V R_RISCV_SET_ULEB128 field at the last byte
+#source: uleb128-reloc-bounds-pass.s
+#ld: -e _start
+#readelf: -h
+#target: riscv*-*-*
+
+ELF Header:
+ Magic: 7f 45 4c 46 .*
+ Class: ELF(32|64)
+#pass
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-pass.s b/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-pass.s
new file mode 100644
index 0000000000..e27fe1447b
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-pass.s
@@ -0,0 +1,10 @@
+ .text
+ .globl _start
+_start:
+ .fill 15, 1, 0
+ .reloc ., R_RISCV_SET_ULEB128, foo0
+ .reloc ., R_RISCV_SUB_ULEB128, foo0
+ .byte 0
+ .data
+foo0:
+ .byte 0x93
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-rv32.d b/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-rv32.d
new file mode 100644
index 0000000000..2aa7a4ad89
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-rv32.d
@@ -0,0 +1,6 @@
+#name: RISC-V RV32 R_RISCV_SET_ULEB128 field at the section end
+#source: uleb128-reloc-bounds.s
+#as: -march=rv32i -mabi=ilp32
+#ld: -melf32lriscv -e _start
+#error: .*: bad reloc offset \(0x10 \+ 0x1 > 0x10\) for section `\.text'
+#target: riscv*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds.d b/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds.d
new file mode 100644
index 0000000000..743ffc8c6a
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds.d
@@ -0,0 +1,5 @@
+#name: RISC-V R_RISCV_SET_ULEB128 field at the section end
+#source: uleb128-reloc-bounds.s
+#ld: -e _start
+#error: .*: bad reloc offset \(0x10 \+ 0x1 > 0x10\) for section `\.text'
+#target: riscv*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds.s b/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds.s
new file mode 100644
index 0000000000..fd46237a24
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds.s
@@ -0,0 +1,9 @@
+ .text
+ .globl _start
+_start:
+ .fill 16, 1, 0
+ .reloc ., R_RISCV_SET_ULEB128, foo0
+ .reloc ., R_RISCV_SUB_ULEB128, foo0
+ .data
+foo0:
+ .byte 0x93
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-unterminated-control.d b/ld/testsuite/ld-riscv-elf/uleb128-unterminated-control.d
new file mode 100644
index 0000000000..e638244341
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-unterminated-control.d
@@ -0,0 +1,10 @@
+#name: RISC-V terminated R_RISCV_SET/SUB_ULEB128 field control
+#source: uleb128-unterminated-pass.s
+#ld: -e _start
+#readelf: -h
+#target: riscv*-*-*
+
+ELF Header:
+ Magic: 7f 45 4c 46 .*
+ Class: ELF(32|64)
+#pass
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-unterminated-pass.s b/ld/testsuite/ld-riscv-elf/uleb128-unterminated-pass.s
new file mode 100644
index 0000000000..82ae33b2fc
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-unterminated-pass.s
@@ -0,0 +1,10 @@
+ .text
+ .globl _start
+_start:
+ .fill 15, 1, 0
+ .byte 0x00
+ .reloc 15, R_RISCV_SET_ULEB128, foo0
+ .reloc 15, R_RISCV_SUB_ULEB128, foo0
+ .data
+foo0:
+ .byte 0x93
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-unterminated.d b/ld/testsuite/ld-riscv-elf/uleb128-unterminated.d
new file mode 100644
index 0000000000..9a8c46cbc8
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-unterminated.d
@@ -0,0 +1,5 @@
+#name: RISC-V unterminated R_RISCV_SUB_ULEB128 field
+#source: uleb128-unterminated.s
+#ld: -e _start
+#error: .*: unterminated ULEB128 relocation field at offset .* in section .*
+#target: riscv*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-unterminated.s b/ld/testsuite/ld-riscv-elf/uleb128-unterminated.s
new file mode 100644
index 0000000000..cfa36ae883
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-unterminated.s
@@ -0,0 +1,12 @@
+ # Note: R_RISCV_SET_ULEB128 appears first in .rela.text; its scan
+ # rejects the unterminated field before R_RISCV_SUB_ULEB128 is processed.
+ .text
+ .globl _start
+_start:
+ .fill 15, 1, 0
+ .byte 0x80
+ .reloc 15, R_RISCV_SET_ULEB128, foo0
+ .reloc 15, R_RISCV_SUB_ULEB128, foo0
+ .data
+foo0:
+ .byte 0x93
--
2.55.0
More information about the Binutils
mailing list