[PATCH 2/6 v2] doc: Add elf_getaroff.3

2025-06-23 Thread Aaron Merey
Signed-off-by: Aaron Merey 

---
v2: Clarify that the offset refers to the archive member header.

> > +.SH RETURN VALUE
> > +Return the file offset, in bytes, of the archive member referred to by
> > +.IR elf .
> > +If
> > +.I elf
> > +is NULL or is not a member of an archive,
> > +return
> > +.BR ELF_C_NULL .
>
> ehe, yes, that is true, although confusing...
> It also isn't what other implementations seem to do, which return -1.
> And it looks like we actually expect -1 ourselves in ar.c and ranlib.c
> Groan :{
> Might this really be a bug that nobody noticed before?
> Should we fix it? Or is there a big risk we have users that rely on it
> returning ELF_C_NULL instead of -1?

IMO we should just leave it as is.  elf_getaroff was added nearly 20
years ago and I can't find any complaints about about this detail.
But if we change this now we might get complaints.

Aaron

 doc/Makefile.am|  1 +
 doc/elf_getaroff.3 | 59 ++
 2 files changed, 60 insertions(+)
 create mode 100644 doc/elf_getaroff.3

diff --git a/doc/Makefile.am b/doc/Makefile.am
index fbfebfe0..6451ffab 100644
--- a/doc/Makefile.am
+++ b/doc/Makefile.am
@@ -55,6 +55,7 @@ notrans_dist_man3_MANS= elf32_checksum.3 \
elf_errmsg.3 \
elf_errno.3 \
elf_fill.3 \
+   elf_getaroff.3 \
elf_getbase.3 \
elf_getdata.3 \
elf_getscn.3 \
diff --git a/doc/elf_getaroff.3 b/doc/elf_getaroff.3
new file mode 100644
index ..3a393e5d
--- /dev/null
+++ b/doc/elf_getaroff.3
@@ -0,0 +1,59 @@
+.TH ELF_GETAROFF 3 2025-06-06 "Libelf" "Libelf Programmer's Manual"
+
+.SH NAME
+elf_getaroff \- retrieve the offset of an archive member header
+
+.SH SYNOPSIS
+.nf
+.B #include 
+
+.BI "int64_t elf_getaroff(Elf *" elf ");"
+.fi
+.SH DESCRIPTION
+Return the file offset, in bytes, of the archive member header currently
+referred to by an ELF descriptor.  This is the offset of the member header
+in the parent archive file.  This offset can be used with
+.BR elf_rand .
+
+.SH PARAMETERS
+.TP
+.I elf
+Elf descriptor referring to a member of an archive file header.
+
+.SH RETURN VALUE
+Return the file offset, in bytes, of the archive member header referred
+to by
+.IR elf .
+If
+.I elf
+is NULL or is not a member of an archive,
+return
+.BR ELF_C_NULL .
+
+.SH SEE ALSO
+.BR elf_begin (3),
+.BR elf_next (3),
+.BR elf_rand (3),
+.BR libelf (3),
+.BR elf (5)
+
+.SH ATTRIBUTES
+.TS
+allbox;
+lbx lb lb
+l l l.
+Interface  Attribute   Value
+T{
+.na
+.nh
+.BR elf_getaroff ()
+T} Thread safety   MT-Safe
+.TE
+
+.SH REPORTING BUGS
+Report bugs to  or 
https://sourceware.org/bugzilla/.
+
+.SH HISTORY
+.B elf_getaroff
+first appeared in elfutils 0.114.  This elfutils libelf function may not be
+found in other libelf implementations.
-- 
2.49.0



[PATCH 3/6 v2] doc: Add elf_getarhdr.3

2025-06-23 Thread Aaron Merey
Signed-off-by: Aaron Merey 

---
v2 changes: clarify when changes to the parent archive descriptor
invalidate the return value. Change thread safety attribute to
MT-Unsafe race.

 doc/Makefile.am|  1 +
 doc/elf_getarhdr.3 | 80 ++
 2 files changed, 81 insertions(+)
 create mode 100644 doc/elf_getarhdr.3

diff --git a/doc/Makefile.am b/doc/Makefile.am
index 6451ffab..aae29ebc 100644
--- a/doc/Makefile.am
+++ b/doc/Makefile.am
@@ -55,6 +55,7 @@ notrans_dist_man3_MANS= elf32_checksum.3 \
elf_errmsg.3 \
elf_errno.3 \
elf_fill.3 \
+   elf_getarhdr.3 \
elf_getaroff.3 \
elf_getbase.3 \
elf_getdata.3 \
diff --git a/doc/elf_getarhdr.3 b/doc/elf_getarhdr.3
new file mode 100644
index ..10c2f62a
--- /dev/null
+++ b/doc/elf_getarhdr.3
@@ -0,0 +1,80 @@
+.TH ELF_GETARHDR 3 2025-06-06 "Libelf" "Libelf Programmer's Manual"
+
+.SH NAME
+elf_getarhdr \- retrieve archive member header information
+
+.SH SYNOPSIS
+.nf
+.B #include 
+
+.BI "Elf_Arhdr *elf_getarhdr(Elf *" elf ");"
+.fi
+
+.SH DESCRIPTION
+Return a
+.B Elf_Arhdr *
+describing the archive header of the archive member currently referred
+to by an ELF descriptor.
+
+The
+.B Elf_Arhdr
+structure is defined as:
+
+.P
+.nf
+typedef struct {
+  char *ar_name;/* Name of archive member.  */
+  time_t ar_date;   /* File date.  */
+  uid_t ar_uid; /* User ID.  */
+  gid_t ar_gid; /* Group ID.  */
+  mode_t ar_mode;   /* File mode.  */
+  int64_t ar_size;  /* File size.  */
+  char *ar_rawname; /* Original name of archive member.  */
+} Elf_Arhdr;
+.fi
+
+.SH PARAMETERS
+.TP
+.I elf
+An ELF descriptor referring to a member of an archive file.
+
+.SH RETURN VALUE
+If
+.I elf
+refers to an archive member,
+.B elf_getarhdr()
+returns a pointer to its archive header.  This pointer is valid only until
+.B elf_next()
+or
+.B elf_rand()
+update the parent archive ELF descriptor,
+or
+.BR elf_end()
+is called on the parent archive ELF descriptor.  If
+.I elf
+does not refer to an archive member or the header cannot be
+acquired then NULL is returned.
+
+.SH SEE ALSO
+.BR elf_begin (3),
+.BR elf_getaroff (3),
+.BR elf_next (3),
+.BR elf_rand (3),
+.BR libelf (3),
+.BR elf (5)
+
+.SH ATTRIBUTES
+.TS
+allbox;
+lbx lb lb
+l l l.
+Interface  Attribute   Value
+T{
+.na
+.nh
+.BR elf_getarhdr ()
+T} Thread safety   MT-Unsafe race
+.TE
+
+.SH REPORTING BUGS
+Report bugs to  or 
https://sourceware.org/bugzilla/.
-- 
2.49.0



Re: [PATCH] libdwl: Add validate_strdata to limit Elf_Data d_size to valid strings.

2025-06-23 Thread Aaron Merey
Hi Mark,

On Sun, Jun 22, 2025 at 10:19 AM Mark Wielaard  wrote:
>
> dwfl_module_getsym returns the name of a symbol as found in the
> corresponding (symbol) string section. Make sure all names are
> correctly zero terminated by making sure the last valid index in a
> section/segment Elf_Data contains a zero character.
>
> * libdwfl/dwfl_module_getdwarf.c (validate_strdata): New
> function taking Elf_Data and restricting d_size to last zero
> char.
> (translate_offs): Call validate_strdata.
> (find_symtab): Likewise for both symstrdata and aux_symstrdata.
>
> https://sourceware.org/bugzilla/show_bug.cgi?id=33099
>
> Signed-off-by: Mark Wielaard 
> ---
>  libdwfl/dwfl_module_getdwarf.c | 20 
>  1 file changed, 20 insertions(+)
>
> diff --git a/libdwfl/dwfl_module_getdwarf.c b/libdwfl/dwfl_module_getdwarf.c
> index 7fd0d3aa3b17..135132d69178 100644
> --- a/libdwfl/dwfl_module_getdwarf.c
> +++ b/libdwfl/dwfl_module_getdwarf.c
> @@ -1,5 +1,6 @@
>  /* Find debugging and symbol information for a module in libdwfl.
> Copyright (C) 2005-2012, 2014, 2015, 2025 Red Hat, Inc.
> +   Copyright (C) 2025 Mark J. Wielaard 
> This file is part of elfutils.
>
> This file is free software; you can redistribute it and/or modify
> @@ -692,6 +693,19 @@ find_offsets (Elf *elf, GElf_Addr main_bias, size_t 
> phnum, size_t n,
>  }
>  }
>
> +/* This is a string section/segment, so we want to make sure the last
> +   valid index contains a zero character to terminate a string.  */
> +static void
> +validate_strdata (Elf_Data *symstrdata)
> +{
> +  size_t size = symstrdata->d_size;
> +  const char *buf = symstrdata->d_buf;
> +  while (size > 0 && *(buf + size - 1) != '\0')
> +--size;
> +  symstrdata->d_size = size;
> +}
> +
> +
>  /* Various addresses we might want to pull from the dynamic segment.  */
>  enum
>  {
> @@ -816,6 +830,8 @@ translate_offs (GElf_Addr adjust,
>   ELF_T_BYTE);
>   if (mod->symstrdata == NULL)
> mod->symdata = NULL;
> + else
> +   validate_strdata (mod->symstrdata);
> }
>if (mod->symdata == NULL)
> mod->symerr = DWFL_E (LIBELF, elf_errno ());
> @@ -1181,6 +1197,8 @@ find_symtab (Dwfl_Module *mod)
>mod->symstrdata = elf_getdata (symstrscn, NULL);
>if (mod->symstrdata == NULL || mod->symstrdata->d_buf == NULL)
>  goto elferr;
> +  else
> +validate_strdata (mod->symstrdata);
>
>if (xndxscn == NULL)
>  mod->symxndxdata = NULL;
> @@ -1264,6 +1282,8 @@ find_symtab (Dwfl_Module *mod)
>mod->aux_symstrdata = elf_getdata (aux_strscn, NULL);
>if (mod->aux_symstrdata == NULL || mod->aux_symstrdata->d_buf == NULL)
> goto aux_cleanup;
> +  else
> +   validate_strdata (mod->aux_symstrdata);
>
>if (aux_xndxscn == NULL)
> mod->aux_symxndxdata = NULL;
> --
> 2.49.0
>

LGTM.

Aaron



[Bug libelf/33099] heap overflow in print_dwarf_addr

2025-06-23 Thread ZeroTrac3r at outlook dot com
https://sourceware.org/bugzilla/show_bug.cgi?id=33099

--- Comment #5 from Zero Trac3r  ---
(In reply to Mark Wielaard from comment #4)
> (In reply to Mark Wielaard from comment #3)
> > Or maybe we need to add the check where the symstrdata is allocated?
> 
> I wrote a patch to do that instead:
> https://code.wildebeest.org/git/user/mjw/elfutils/commit/
> ?h=translate_offs_symstrdata
> https://inbox.sourceware.org/elfutils-devel/20250622122147.1569555-1-
> m...@klomp.org/

LGTM

-- 
You are receiving this mail because:
You are on the CC list for the bug.