public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] readelf: Use unsigned loop variables in handle_verneed and handle_verdef
@ 2024-02-21 21:19 Mark Wielaard
  2024-02-27 15:17 ` Mark Wielaard
  0 siblings, 1 reply; 2+ messages in thread
From: Mark Wielaard @ 2024-02-21 21:19 UTC (permalink / raw)
  To: elfutils-devel; +Cc: Mark Wielaard

Prevent signed underflow by changing loop variables to unsigned and
doing count checks before decrementing. This isn't really a bug, but
prevents UB detected by ubsan on fuzzed input. The bad (fuzzed) input
data does get detected anyway.

	* src/readelf.c (handle_verneed): Use unsigned cnt, cnt2.
	(handle_verdef): Likewise.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/readelf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/readelf.c b/src/readelf.c
index 802f8ede..0e931184 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -3159,7 +3159,7 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
 	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
 
   unsigned int offset = 0;
-  for (int cnt = shdr->sh_info; --cnt >= 0; )
+  for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
     {
       /* Get the data at the next offset.  */
       GElf_Verneed needmem;
@@ -3173,7 +3173,7 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
 	      (unsigned short int) need->vn_cnt);
 
       unsigned int auxoffset = offset + need->vn_aux;
-      for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
+      for (unsigned int cnt2 = need->vn_cnt; cnt2 > 0; cnt2--)
 	{
 	  GElf_Vernaux auxmem;
 	  GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
@@ -3236,7 +3236,7 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
 	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
 
   unsigned int offset = 0;
-  for (int cnt = shdr->sh_info; --cnt >= 0; )
+  for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
     {
       /* Get the data at the next offset.  */
       GElf_Verdef defmem;
@@ -3259,7 +3259,7 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
 	      elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
 
       auxoffset += aux->vda_next;
-      for (int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
+      for (unsigned int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
 	{
 	  aux = gelf_getverdaux (data, auxoffset, &auxmem);
 	  if (unlikely (aux == NULL))
-- 
2.39.3


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] readelf: Use unsigned loop variables in handle_verneed and handle_verdef
  2024-02-21 21:19 [PATCH] readelf: Use unsigned loop variables in handle_verneed and handle_verdef Mark Wielaard
@ 2024-02-27 15:17 ` Mark Wielaard
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Wielaard @ 2024-02-27 15:17 UTC (permalink / raw)
  To: elfutils-devel

On Wed, 2024-02-21 at 22:19 +0100, Mark Wielaard wrote:
> Prevent signed underflow by changing loop variables to unsigned and
> doing count checks before decrementing. This isn't really a bug, but
> prevents UB detected by ubsan on fuzzed input. The bad (fuzzed) input
> data does get detected anyway.
> 
> 	* src/readelf.c (handle_verneed): Use unsigned cnt, cnt2.
> 	(handle_verdef): Likewise.

Pushed after a quick chat with Aaron on irc.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-27 15:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-21 21:19 [PATCH] readelf: Use unsigned loop variables in handle_verneed and handle_verdef Mark Wielaard
2024-02-27 15:17 ` Mark Wielaard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).