[RFC PATCH 09/17] bfd: check that relocation fields lie inside their section
Harshit Kumar
harshit7kr@gmail.com
Mon Nov 16 18:43:25 GMT 2026
Many ELF backends apply a relocation at contents + r_offset without
checking that the field lies inside the section, so an object with an
out-of-range r_offset makes ld read and write past the section
contents. i386 and x86-64 check this when they scan relocations, and
backends that use _bfd_final_link_relocate get bfd_reloc_offset_in_range
for the relocations they pass to it; but AArch64, ARM, RISC-V,
LoongArch, SPARC and others apply many relocations, and rewrite many
instructions, at r_offset directly.
Check it once for every ELF backend, where the linker reads the
relocations, next to the existing symbol index check. That is before
check_relocs, relaxation, garbage collection and relocate_section, so
each of them sees only relocations whose field fits. The limit is the
larger of the section's size and rawsize, the size of the buffer that
the backends apply relocations to.
The field size is the howto size, unless the backend's
elf_backend_reloc_field_size gives another size for the relocation
type. Relocations whose howto has no dst_mask are not checked: NONE
and markers such as R_SH_ALIGN or R_LARCH_RELAX, which may sit at the
end of a section or at an odd offset, but also hints such as
R_PPC64_TLS, which let the backend rewrite the instruction at
r_offset. Those are left to the backend, unless its
elf_backend_reloc_field_size gives them a size, as the following
patches do for some. When the
backend sets relocs_compose_at_offset, a relocation followed at the
same offset by another that has a field is not checked either, as only
the last of them writes the field, and that one is checked. The
previous patches set these for the backends where the howto size would
reject valid objects (nds32, MIPS, RX, MSP430 and MN10300), and give
the field size where the default cannot use the howto (MIPS ELF64 and
hppa). The test of the RX, MSP430 and MN10300 hooks is added here.
The howto is looked up with error messages suppressed, through the
thread-local mechanism bfd_check_format_matches uses, since a
relocation type that the backend rejects is reported by the backend.
The field sizes are cached per relocation type in the input bfd, and
the bound is computed once per section. A section is reported once:
some callers carry on after a failed read, so its relocations may be
read, and rejected, again.
bfd/
* bfd.c (_bfd_ignore_error_messages): New function.
* libbfd.h: Regenerate.
* elf-bfd.h (struct bfd_elf_section_data): Add
reloc_offset_reported.
(struct elf_obj_tdata): Add reloc_field_cache.
* elflink.c (elf_link_howto_field_size): New function.
(struct elf_reloc_field_cache): New.
(elf_link_reloc_field_size, elf_link_check_reloc_offsets): New
functions.
(elf_link_read_relocs_from_section): Call
elf_link_check_reloc_offsets.
ld/
* testsuite/ld-elf/reloc-offset-1.d: New test.
* testsuite/ld-elf/reloc-offset-1.s: New file.
* testsuite/ld-elf/reloc-offset-2.d: New test.
* testsuite/ld-elf/reloc-offset-2.s: New file.
* testsuite/ld-elf/reloc-offset-3.d: New test.
* testsuite/ld-elf/reloc-offset-3.s: New file.
* testsuite/ld-elf/reloc-offset-diff.d: New test.
* testsuite/ld-elf/reloc-offset-diff.s: New file.
Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
bfd/bfd.c | 21 +++
bfd/elf-bfd.h | 8 +
bfd/elflink.c | 198 +++++++++++++++++++++++-
bfd/libbfd.h | 2 +
ld/testsuite/ld-elf/reloc-offset-1.d | 8 +
ld/testsuite/ld-elf/reloc-offset-1.s | 4 +
ld/testsuite/ld-elf/reloc-offset-2.d | 8 +
ld/testsuite/ld-elf/reloc-offset-2.s | 4 +
ld/testsuite/ld-elf/reloc-offset-3.d | 8 +
ld/testsuite/ld-elf/reloc-offset-3.s | 4 +
ld/testsuite/ld-elf/reloc-offset-diff.d | 8 +
ld/testsuite/ld-elf/reloc-offset-diff.s | 6 +
12 files changed, 278 insertions(+), 1 deletion(-)
create mode 100644 ld/testsuite/ld-elf/reloc-offset-1.d
create mode 100644 ld/testsuite/ld-elf/reloc-offset-1.s
create mode 100644 ld/testsuite/ld-elf/reloc-offset-2.d
create mode 100644 ld/testsuite/ld-elf/reloc-offset-2.s
create mode 100644 ld/testsuite/ld-elf/reloc-offset-3.d
create mode 100644 ld/testsuite/ld-elf/reloc-offset-3.s
create mode 100644 ld/testsuite/ld-elf/reloc-offset-diff.d
create mode 100644 ld/testsuite/ld-elf/reloc-offset-diff.s
diff --git a/bfd/bfd.c b/bfd/bfd.c
index 59f15d2dd4..ae6f7dff18 100644
--- a/bfd/bfd.c
+++ b/bfd/bfd.c
@@ -1872,6 +1872,27 @@ _bfd_restore_error_handler_caching (struct per_xvec_messages *old)
error_handler_messages = old;
}
+/*
+INTERNAL_FUNCTION
+ _bfd_ignore_error_messages
+
+SYNOPSIS
+ struct per_xvec_messages *_bfd_ignore_error_messages (void);
+
+DESCRIPTION
+ Make _bfd_error_handler drop messages, until
+ _bfd_restore_error_handler_caching is called with the returned
+ value.
+*/
+
+struct per_xvec_messages *
+_bfd_ignore_error_messages (void)
+{
+ struct per_xvec_messages *old = error_handler_messages;
+ error_handler_messages = IGNORE_ERROR_MESSAGES;
+ return old;
+}
+
/*
FUNCTION
bfd_set_error_program_name
diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 3e96b2a3cc..a70576d9c8 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -1963,6 +1963,10 @@ struct bfd_elf_section_data
FIXME: In the future it might be better to change this into a list
of secondary reloc sections, making lookup easier and faster. */
bool has_secondary_relocs;
+
+ /* TRUE once the linker has reported a relocation of this section
+ whose field does not lie inside it. */
+ bool reloc_offset_reported;
};
#define elf_section_data(sec) ((struct bfd_elf_section_data*)(sec)->used_by_bfd)
@@ -2301,6 +2305,10 @@ struct elf_obj_tdata
/* More information held for output ELF BFDs. */
struct output_elf_obj_tdata *o;
+
+ /* Field sizes of relocation types, as used by the linker's
+ relocation offset check. Allocated on first use. */
+ struct elf_reloc_field_cache *reloc_field_cache;
};
#define elf_tdata(bfd) ((bfd) -> tdata.elf_obj_data)
diff --git a/bfd/elflink.c b/bfd/elflink.c
index 1eef868be4..e05d858915 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -2834,6 +2834,201 @@ _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
return true;
}
+/* Return the number of octets that relocation REL of ABFD may read or
+ write at its r_offset, from its howto, or 0 for a type the backend
+ does not know or whose howto has no dst_mask. REL_P is true for a
+ relocation from a SHT_REL section. Relocations with no dst_mask are
+ not checked: NONE, which relaxation may leave past the end of the
+ section; markers such as R_SH_ALIGN or R_LARCH_RELAX, which may sit
+ at the end of the section or at an odd offset; and hints such as
+ R_PPC64_TLS, which leave the instruction at r_offset to the
+ backend. */
+
+static unsigned int
+elf_link_howto_field_size (bfd *abfd, const Elf_Internal_Rela *rel,
+ bool rel_p)
+{
+ elf_backend_data *bed = get_elf_backend_data (abfd);
+ bool (*info_to_howto) (bfd *, arelent *, Elf_Internal_Rela *);
+ struct per_xvec_messages *old_messages;
+ bfd_error_type old_error;
+ Elf_Internal_Rela copy;
+ arelent cache;
+ asymbol *sym;
+ bool ok;
+
+ if (rel_p && bed->elf_info_to_howto_rel != NULL)
+ info_to_howto = bed->elf_info_to_howto_rel;
+ else if (bed->elf_info_to_howto != NULL)
+ info_to_howto = bed->elf_info_to_howto;
+ else if (bed->elf_info_to_howto_rel != NULL)
+ info_to_howto = bed->elf_info_to_howto_rel;
+ else
+ return 0;
+
+ memset (&cache, 0, sizeof (cache));
+ /* Some info_to_howto functions look at the symbol (MIPS checks for a
+ section symbol), as they normally run from canonicalize_reloc. */
+ sym = bfd_abs_section_ptr->symbol;
+ cache.sym_ptr_ptr = &sym;
+ /* info_to_howto takes a non-const relocation. */
+ copy = *rel;
+ /* A relocation type that the backend rejects is reported by the
+ backend when it processes the relocation, so do not report it
+ here too. */
+ old_error = bfd_get_error ();
+ old_messages = _bfd_ignore_error_messages ();
+ ok = info_to_howto (abfd, &cache, ©);
+ _bfd_restore_error_handler_caching (old_messages);
+ bfd_set_error (old_error);
+
+ if (!ok || cache.howto == NULL || cache.howto->dst_mask == 0)
+ return 0;
+ return bfd_get_reloc_size (cache.howto);
+}
+
+/* The field sizes of a few relocation types of an input bfd, kept by
+ elf_link_reloc_field_size. The slot of a type is a hash of it. */
+
+#define ELF_RELOC_FIELD_CACHE_SLOTS 64
+
+struct elf_reloc_field_cache
+{
+ /* The relocation type in each slot. */
+ unsigned int type[ELF_RELOC_FIELD_CACHE_SLOTS];
+ /* Its field size. */
+ unsigned char size[ELF_RELOC_FIELD_CACHE_SLOTS];
+ /* 0 for an empty slot, 1 for a RELA and 2 for a REL relocation. */
+ unsigned char kind[ELF_RELOC_FIELD_CACHE_SLOTS];
+};
+
+/* Return the field size of relocation REL of ABFD, of type R_TYPE, from
+ the backend's elf_backend_reloc_field_size or else from the howto.
+ Remember it in ABFD, as types repeat. */
+
+static unsigned int
+elf_link_reloc_field_size (bfd *abfd, const Elf_Internal_Rela *rel,
+ unsigned int r_type, bool rel_p)
+{
+ elf_backend_data *bed = get_elf_backend_data (abfd);
+ struct elf_reloc_field_cache *cache = elf_tdata (abfd)->reloc_field_cache;
+ unsigned int slot = (r_type ^ (r_type >> 6)) % ELF_RELOC_FIELD_CACHE_SLOTS;
+ unsigned char kind = rel_p ? 2 : 1;
+ unsigned int size;
+
+ if (cache == NULL)
+ {
+ cache = bfd_zalloc (abfd, sizeof (*cache));
+ elf_tdata (abfd)->reloc_field_cache = cache;
+ }
+ if (cache != NULL
+ && cache->kind[slot] == kind
+ && cache->type[slot] == r_type)
+ return cache->size[slot];
+
+ if (bed->elf_backend_reloc_field_size == NULL
+ || !bed->elf_backend_reloc_field_size (abfd, r_type, rel_p, &size))
+ size = elf_link_howto_field_size (abfd, rel, rel_p);
+
+ if (cache != NULL && size <= 0xff)
+ {
+ cache->type[slot] = r_type;
+ cache->size[slot] = size;
+ cache->kind[slot] = kind;
+ }
+ return size;
+}
+
+/* Return false if the field of a relocation in RELS to RELEND, read
+ for section SEC of ABFD, does not lie inside the section contents,
+ reporting an error the first time for SEC: some callers carry on
+ after a failed read, so the section's relocations may be read again.
+ REL_P is true for relocations from a SHT_REL section. The limit is
+ the larger of the section's size and rawsize, the size of the
+ contents buffer the backends apply relocations to. This is the check
+ the i386 and x86-64 backends already make when they scan relocations,
+ done here for every ELF backend and every pass that reads
+ relocations: check_relocs, relaxation, garbage collection and
+ relocate_section. */
+
+static bool
+elf_link_check_reloc_offsets (bfd *abfd, const asection *sec,
+ const Elf_Internal_Rela *rels,
+ const Elf_Internal_Rela *relend, bool rel_p)
+{
+ elf_backend_data *bed = get_elf_backend_data (abfd);
+ bool compose = bed->relocs_compose_at_offset;
+ bool elf64 = bed->s->arch_size == 64;
+ const Elf_Internal_Rela *rel;
+ bfd_size_type limit;
+ bfd_vma max_offset;
+ unsigned int opb;
+ unsigned int last_type = 0;
+ unsigned int last_size = 0;
+ bool have_last = false;
+
+ if ((sec->flags & SEC_HAS_CONTENTS) == 0)
+ return true;
+
+ limit = sec->rawsize > sec->size ? sec->rawsize : sec->size;
+ opb = bfd_octets_per_byte (abfd, sec);
+ max_offset = limit / opb;
+ for (rel = rels; rel < relend; rel++)
+ {
+ unsigned int r_type;
+ unsigned int size;
+
+ /* Where the backend composes consecutive relocations at one
+ offset, only the last of them writes the field. A following
+ relocation with no field, such as NONE, ends the sequence; if it
+ has none, this one is checked. Otherwise the following one is
+ checked at the same offset. */
+ if (compose
+ && rel + 1 < relend
+ && rel[1].r_offset == rel->r_offset
+ && elf_link_reloc_field_size (abfd, rel + 1,
+ (elf64 ? ELF64_R_TYPE (rel[1].r_info)
+ : ELF32_R_TYPE (rel[1].r_info)),
+ rel_p) != 0)
+ continue;
+
+ r_type = elf64 ? ELF64_R_TYPE (rel->r_info) : ELF32_R_TYPE (rel->r_info);
+ if (!have_last || r_type != last_type)
+ {
+ last_size = elf_link_reloc_field_size (abfd, rel, r_type, rel_p);
+ last_type = r_type;
+ have_last = true;
+ }
+ size = last_size;
+ if (size == 0)
+ continue;
+
+ if (rel->r_offset <= max_offset
+ && size <= limit - rel->r_offset * opb)
+ continue;
+
+ if (!elf_section_data (sec)->reloc_offset_reported)
+ {
+ elf_section_data (sec)->reloc_offset_reported = true;
+ if (rel->r_offset > max_offset)
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB: bad reloc offset (%#" PRIx64 " > %#" PRIx64 ") for"
+ " section `%pA'"),
+ abfd, (uint64_t) rel->r_offset, (uint64_t) limit, sec);
+ else
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB: bad reloc offset (%#" PRIx64 " + %#x > %#" PRIx64 ")"
+ " for section `%pA'"),
+ abfd, (uint64_t) rel->r_offset, size, (uint64_t) limit, sec);
+ }
+ bfd_set_error (bfd_error_bad_value);
+ return false;
+ }
+ return true;
+}
+
/* Read and swap the relocs from the section indicated by SHDR. This
may be either a REL or a RELA section. The relocations are
translated into RELA relocations and stored in INTERNAL_RELOCS,
@@ -2925,7 +3120,8 @@ elf_link_read_relocs_from_section (bfd *abfd,
erela += shdr->sh_entsize;
}
- return true;
+ return elf_link_check_reloc_offsets (abfd, sec, internal_relocs, irela,
+ swap_in == bed->s->swap_reloc_in);
}
/* Read and swap the relocs for a section O. They may have been
diff --git a/bfd/libbfd.h b/bfd/libbfd.h
index 80adcef01f..6b27938019 100644
--- a/bfd/libbfd.h
+++ b/bfd/libbfd.h
@@ -965,6 +965,8 @@ struct per_xvec_messages *_bfd_set_error_handler_caching (struct per_xvec_messag
void _bfd_restore_error_handler_caching (struct per_xvec_messages *) ATTRIBUTE_HIDDEN;
+struct per_xvec_messages *_bfd_ignore_error_messages (void) ATTRIBUTE_HIDDEN;
+
const char *_bfd_get_error_program_name (void) ATTRIBUTE_HIDDEN;
bool _bfd_threading_enabled (void) ATTRIBUTE_HIDDEN;
diff --git a/ld/testsuite/ld-elf/reloc-offset-1.d b/ld/testsuite/ld-elf/reloc-offset-1.d
new file mode 100644
index 0000000000..aae819a963
--- /dev/null
+++ b/ld/testsuite/ld-elf/reloc-offset-1.d
@@ -0,0 +1,8 @@
+#name: relocation field crossing the end of its section
+#ld: -r
+# gas rejects the .reloc on bfin, cr16, crx, ia64, mep and MIPS o32 (the
+# default ABI of many MIPS targets); dlx, pj, s12z and xgate use the
+# generic linker, which does not run the check.
+#notarget: bfin-*-* cr16-*-* crx-*-* dlx-*-* ia64-*-* mep-*-* mips*-*-*
+#notarget: pj-*-* s12z-*-* xgate-*-*
+#error: .*bad reloc offset \(0x4 \+ 0x4 > 0x6\) for section `\.probe'
diff --git a/ld/testsuite/ld-elf/reloc-offset-1.s b/ld/testsuite/ld-elf/reloc-offset-1.s
new file mode 100644
index 0000000000..687451ea41
--- /dev/null
+++ b/ld/testsuite/ld-elf/reloc-offset-1.s
@@ -0,0 +1,4 @@
+ .section .probe,"a"
+ .byte 0, 0, 0, 0, 0, 0
+ /* A 4-octet field at offset 4 of a 6-octet section. */
+ .reloc 4, BFD_RELOC_32, x
diff --git a/ld/testsuite/ld-elf/reloc-offset-2.d b/ld/testsuite/ld-elf/reloc-offset-2.d
new file mode 100644
index 0000000000..8bfda916f2
--- /dev/null
+++ b/ld/testsuite/ld-elf/reloc-offset-2.d
@@ -0,0 +1,8 @@
+#name: relocation field at the end of its section
+#ld: -r
+# gas rejects the .reloc on bfin, cr16, crx, ia64, mep and MIPS o32 (the
+# default ABI of many MIPS targets); dlx, pj, s12z and xgate use the
+# generic linker, which does not run the check.
+#notarget: bfin-*-* cr16-*-* crx-*-* dlx-*-* ia64-*-* mep-*-* mips*-*-*
+#notarget: pj-*-* s12z-*-* xgate-*-*
+#error: .*bad reloc offset \(0x4 \+ 0x4 > 0x4\) for section `\.probe'
diff --git a/ld/testsuite/ld-elf/reloc-offset-2.s b/ld/testsuite/ld-elf/reloc-offset-2.s
new file mode 100644
index 0000000000..9b14d5085e
--- /dev/null
+++ b/ld/testsuite/ld-elf/reloc-offset-2.s
@@ -0,0 +1,4 @@
+ .section .probe,"a"
+ .byte 0, 0, 0, 0
+ /* A 4-octet field at the end of a 4-octet section. */
+ .reloc 4, BFD_RELOC_32, x
diff --git a/ld/testsuite/ld-elf/reloc-offset-3.d b/ld/testsuite/ld-elf/reloc-offset-3.d
new file mode 100644
index 0000000000..32e37301db
--- /dev/null
+++ b/ld/testsuite/ld-elf/reloc-offset-3.d
@@ -0,0 +1,8 @@
+#name: relocation field ending at the end of its section
+#ld: -r
+# cr16, crx, ia64 and mep gas reject BFD_RELOC_32 in .reloc.
+#notarget: cr16-*-* crx-*-* ia64-*-* mep-*-*
+#objdump: -r -j .probe
+#...
+0+2 [^ ]+ +x
+#pass
diff --git a/ld/testsuite/ld-elf/reloc-offset-3.s b/ld/testsuite/ld-elf/reloc-offset-3.s
new file mode 100644
index 0000000000..2c42234c60
--- /dev/null
+++ b/ld/testsuite/ld-elf/reloc-offset-3.s
@@ -0,0 +1,4 @@
+ .section .probe,"a"
+ .byte 0, 0, 0, 0, 0, 0
+ /* A 4-octet field that ends at the end of the section. */
+ .reloc 2, BFD_RELOC_32, x
diff --git a/ld/testsuite/ld-elf/reloc-offset-diff.d b/ld/testsuite/ld-elf/reloc-offset-diff.d
new file mode 100644
index 0000000000..521ef81f21
--- /dev/null
+++ b/ld/testsuite/ld-elf/reloc-offset-diff.d
@@ -0,0 +1,8 @@
+#name: relocation offset check, symbol difference at section end
+#target: am33_2.0-*-* mn10300-*-* msp430-*-* rx-*-*
+#ld: -r
+#readelf: -r
+
+#...
+0+3 .*
+#pass
diff --git a/ld/testsuite/ld-elf/reloc-offset-diff.s b/ld/testsuite/ld-elf/reloc-offset-diff.s
new file mode 100644
index 0000000000..04d1af12ca
--- /dev/null
+++ b/ld/testsuite/ld-elf/reloc-offset-diff.s
@@ -0,0 +1,6 @@
+ # A symbol difference in the last octet of the section. Some
+ # targets emit operand relocations at the same offset, which
+ # write nothing; only the final 1-octet field must fit.
+ .data
+ .byte 0, 0, 0
+ .byte foo - bar
--
2.55.0
More information about the Binutils
mailing list