The ar_size field of an archive member header is encoded as a 16
character decimal string. This can exceed the value atol can parse on
some targets. Use strtoul so we can check for overflow. Overflow with
atol is undefined.

        * libelf/elf_begin.c (read_long_names): Parse ar_size using
        strtoul and check for overflow.

Signed-off-by: Mark Wielaard <[email protected]>
---
 libelf/elf_begin.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index 0c9b95ddc2b1..4fddd1ca1383 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -1,6 +1,6 @@
 /* Create descriptor for processing file.
    Copyright (C) 1998-2010, 2012, 2014, 2015, 2016 Red Hat, Inc.
-   Copyright (C) 2021, 2022 Mark J. Wielaard <[email protected]>
+   Copyright (C) 2021, 2022, 2026 Mark J. Wielaard <[email protected]>
    This file is part of elfutils.
    Written by Ulrich Drepper <[email protected]>, 1998.
 
@@ -736,11 +736,13 @@ read_long_names (Elf *elf)
          string = buf;
        }
 
-      /* atol expects to see at least one digit.
-        It also cannot be negative (-).  */
+      /* ar_size must start with at least one digit ('0'-'9').  */
       if (!isdigit(string[0]))
        return NULL;
-      len = atol (string);
+      errno = 0;
+      len = strtoul(string, NULL, 10);
+      if (errno == ERANGE)
+       return NULL;
 
       if (memcmp (hdr->ar_name, "//              ", 16) == 0)
        break;
-- 
2.56.0

Reply via email to