public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jsm28 at gcc dot gnu.org" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug stdio/26211] New: printf integer overflow calculating allocation size
Date: Mon, 06 Jul 2020 23:03:36 +0000	[thread overview]
Message-ID: <bug-26211-131@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=26211

            Bug ID: 26211
           Summary: printf integer overflow calculating allocation size
           Product: glibc
           Version: 2.32
            Status: NEW
          Severity: normal
          Priority: P2
         Component: stdio
          Assignee: unassigned at sourceware dot org
          Reporter: jsm28 at gcc dot gnu.org
  Target Milestone: ---

The following test segfaults on a 64-bit system (haven't tested 32-bit).  (Any
precision less than 0x7fffffe0 is OK; 0x7fffffe0 through 0x7fffffff result in
the crash.)

#include <stdio.h>
#include <stdlib.h>

int
main (void)
{
  FILE *fp;
  if ((fp = fopen ("/dev/null", "w")) == NULL)
    exit (1);
  fprintf (fp, "%2$.*1$a", 0x7fffffff, 1e200);
}

The problem is the code in printf_positional:

      /* Maybe the buffer is too small.  */
      if (MAX (prec, width) + EXTSIZ > WORK_BUFFER_SIZE)
        {
          if (__libc_use_alloca ((MAX (prec, width) + EXTSIZ)
                                 * sizeof (CHAR_T)))
            workend = ((CHAR_T *) alloca ((MAX (prec, width) + EXTSIZ)
                                          * sizeof (CHAR_T))
                       + (MAX (prec, width) + EXTSIZ));
          else
            {
              workstart = (CHAR_T *) malloc ((MAX (prec, width) + EXTSIZ)
                                             * sizeof (CHAR_T));
              if (workstart == NULL)
                {
                  done = -1;
                  goto all_done;
                }
              workend = workstart + (MAX (prec, width) + EXTSIZ);
            }
        }

The code in the implementation that doesn't use '$' argument numbers has a
check

        if (__glibc_unlikely (width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
          {
            __set_errno (EOVERFLOW);
            done = -1;
            goto all_done;
          }

(and likewise for precision). The implementation for the case with '$' argument
numbers is missing that check. So MAX (prec, width) + EXTSIZ overflows.

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

             reply	other threads:[~2020-07-06 23:03 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-06 23:03 jsm28 at gcc dot gnu.org [this message]
2020-07-07 14:54 ` [Bug stdio/26211] " cvs-commit at gcc dot gnu.org
2020-07-07 14:57 ` jsm28 at gcc dot gnu.org
2022-08-29 15:06 ` cvs-commit at gcc dot gnu.org
2022-08-30  8:23 ` cvs-commit at gcc dot gnu.org
2022-08-30  8:45 ` cvs-commit at gcc dot gnu.org
2022-08-30  8:45 ` cvs-commit at gcc dot gnu.org
2022-08-30  8:48 ` cvs-commit at gcc dot gnu.org
2022-08-30  9:20 ` cvs-commit at gcc dot gnu.org
2022-08-30  9:20 ` cvs-commit at gcc dot gnu.org
2022-08-30 11:07 ` cvs-commit at gcc dot gnu.org
2022-08-30 11:08 ` cvs-commit at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-26211-131@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).