]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
elf: Fix _dl_debug_vdprintf stack buffer underflow
authorSzabolcs Nagy <szabolcs.nagy@arm.com>
Tue, 11 Oct 2022 13:22:35 +0000 (14:22 +0100)
committerSzabolcs Nagy <szabolcs.nagy@arm.com>
Wed, 12 Oct 2022 13:22:03 +0000 (14:22 +0100)
When printing numbers the alloca buffer size did not consider the
optional width parameter for padding. The width is used e.g. by
_dl_map_object_from_fd which passes '(int) sizeof (void *) * 2'
which can be larger than the buffer size on systems where
sizeof (void *) >= 2 * sizeof (unsigned long).  But even if large
width is not used currently it is better to handle it to avoid
surprises.

elf/dl-printf.c

index d3264ba96cd959bf3571791d83e3a8ebb73c3f58..10efa925bdfb48bc417ebefb02f199d5113e54c4 100644 (file)
@@ -163,8 +163,11 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
                /* We use alloca() to allocate the buffer with the most
                   pessimistic guess for the size.  Using alloca() allows
                   having more than one integer formatting in a call.  */
-               char *buf = (char *) alloca (1 + 3 * sizeof (unsigned long int));
-               char *endp = &buf[1 + 3 * sizeof (unsigned long int)];
+               int size = 1 + 3 * sizeof (unsigned long int);
+               if (width + 1 > size)
+                 size = width + 1;
+               char *buf = (char *) alloca (size);
+               char *endp = &buf[size];
                char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0);
 
                /* Pad to the width the user specified.  */