ld/pdb: Zero-initialize symbol arrays in handle_debugs_section

Message ID 20261102225947.1725-12-oleg.tolmatcev@gmail.com
State New
Headers
Series ld/pdb: Zero-initialize symbol arrays in handle_debugs_section |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply

Commit Message

Oleg Tolmatcev Nov. 2, 2026, 10:59 p.m. UTC
  handle_debugs_section only fills in symbols and sectlist for primary
symbols with a positive section number, so a relocation in a malformed
object referring to any other symbol read uninitialized memory.
Allocate both arrays with xcalloc; the relocate_section routines skip
relocations against a NULL section.

ld/
	* pdb.c (handle_debugs_section): Allocate symbols and sectlist
	with xcalloc.

Signed-off-by: Oleg Tolmatcev <oleg.tolmatcev@gmail.com>
---
 ld/pdb.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Jan Beulich Nov. 6, 2026, 10:35 a.m. UTC | #1
On 02.11.2026 23:59, Oleg Tolmatcev wrote:
> handle_debugs_section only fills in symbols and sectlist for primary
> symbols with a positive section number, so a relocation in a malformed
> object referring to any other symbol read uninitialized memory.
> Allocate both arrays with xcalloc; the relocate_section routines skip
> relocations against a NULL section.

This primarily explains why sectlist wants clearing up front. And indeed
for that you address a pre-existing issue. For symbols[], otoh, prior to
"ld/pdb: Skip auxiliary symbol entries in handle_debugs_section" all
elements of the array are initialized. Hence that clearing (to play safe)
imo wants moving to that patch.

> --- a/ld/pdb.c
> +++ b/ld/pdb.c
> @@ -1948,8 +1948,8 @@ handle_debugs_section (asection *s, bfd *mod, struct string_table *strings,
>  
>        relocs = bfd_coff_read_internal_relocs (mod, s, false, NULL, true, NULL);
>  
> -      symbols = xmalloc (sizeof (struct internal_syment) * syment_count);
> -      sectlist = xmalloc (sizeof (asection *) * syment_count);
> +      symbols = xcalloc (syment_count, sizeof (struct internal_syment));
> +      sectlist = xcalloc (syment_count, sizeof (asection *));

While making these changes it would be nice if the sizeof(<type>) were
also changed to the more robust sizeof(<expr>) (making sure a type change
of the variables stored into is also reflected in a size change).

I can make these changes ahead of committing, provided you agree.

Jan
  
Oleg Tolmatcev Nov. 6, 2026, 11:16 a.m. UTC | #2
пт, 6 нояб. 2026 г. в 11:35, Jan Beulich <jbeulich@suse.com>:
>
> On 02.11.2026 23:59, Oleg Tolmatcev wrote:
> > handle_debugs_section only fills in symbols and sectlist for primary
> > symbols with a positive section number, so a relocation in a malformed
> > object referring to any other symbol read uninitialized memory.
> > Allocate both arrays with xcalloc; the relocate_section routines skip
> > relocations against a NULL section.
>
> This primarily explains why sectlist wants clearing up front. And indeed
> for that you address a pre-existing issue. For symbols[], otoh, prior to
> "ld/pdb: Skip auxiliary symbol entries in handle_debugs_section" all
> elements of the array are initialized. Hence that clearing (to play safe)
> imo wants moving to that patch.
>
> > --- a/ld/pdb.c
> > +++ b/ld/pdb.c
> > @@ -1948,8 +1948,8 @@ handle_debugs_section (asection *s, bfd *mod, struct string_table *strings,
> >
> >        relocs = bfd_coff_read_internal_relocs (mod, s, false, NULL, true, NULL);
> >
> > -      symbols = xmalloc (sizeof (struct internal_syment) * syment_count);
> > -      sectlist = xmalloc (sizeof (asection *) * syment_count);
> > +      symbols = xcalloc (syment_count, sizeof (struct internal_syment));
> > +      sectlist = xcalloc (syment_count, sizeof (asection *));
>
> While making these changes it would be nice if the sizeof(<type>) were
> also changed to the more robust sizeof(<expr>) (making sure a type change
> of the variables stored into is also reflected in a size change).
>
> I can make these changes ahead of committing, provided you agree.
>
> Jan

I do agree of course, thanks.

Oleg
  

Patch

diff --git a/ld/pdb.c b/ld/pdb.c
index c36efeede19..2b9e8d313c7 100644
--- a/ld/pdb.c
+++ b/ld/pdb.c
@@ -1948,8 +1948,8 @@  handle_debugs_section (asection *s, bfd *mod, struct string_table *strings,
 
       relocs = bfd_coff_read_internal_relocs (mod, s, false, NULL, true, NULL);
 
-      symbols = xmalloc (sizeof (struct internal_syment) * syment_count);
-      sectlist = xmalloc (sizeof (asection *) * syment_count);
+      symbols = xcalloc (syment_count, sizeof (struct internal_syment));
+      sectlist = xcalloc (syment_count, sizeof (asection *));
 
       ext = (bfd_byte *) coff_data (mod)->external_syms;