alpine 3.8
format weakness #11

4

Weakness Breakdown


Definition:

A format string exploit occurs when the data of an input string is evaluated as a command by the program. This class of attacks is very similar to buffer overflows since an attacker could execute code, read the stack or cause new behaviors that compromise security. Learn more about format string attacks on OWASP attack index.

Warning code(s):

If format strings can be influenced by an attacker, they can be exploited, and note that sprintf variations do not always 0-terminate.

File Name:

gmp/src/gmp-6.1.2/printf/snprntffuns.c

Context:

The highlighted line of code below is the trigger point of this particular Alpine 3.8 format weakness.

    "size-1" would mean success from a C99 vsnprintf, and the re-run is
   unnecessary in this case, but we don't bother to try to detect what sort
   of vsnprintf we've got.  size-1 should occur rarely in normal
   circumstances.

   vsnprintf might trash it's given ap (it does for instance in glibc 2.1.3
   on powerpc), so copy it in case we need to use it to probe for the size
   output that would have been produced.  Note there's no need to preserve
   it for our callers, just for ourselves.  */

static int
gmp_snprintf_format (struct gmp_snprintf_t *d, const char *fmt,
                     va_list orig_ap)
{
  int      ret, step, alloc, avail;
  va_list  ap;
  char     *p;

  ASSERT (d->size >= 0);

  avail = d->size;
  if (avail > 1)
    {
      va_copy (ap, orig_ap);
      ret = vsnprintf (d->buf, avail, fmt, ap);
      if (ret == -1)
        {
          ASSERT (strlen (d->buf) == avail-1);
          ret = avail-1;
        }

      step = MIN (ret, avail-1);
      d->size -= step;
      d->buf += step;

      if (ret != avail-1)
        return ret;

      /* probably glibc 2.0.x truncated output, probe for actual size */
      alloc = MAX (128, ret);
    }
  else
    {
      /* no space to write anything, just probe for size */
      alloc = 128;
    }

  do
    {
      alloc *= 2; 

The registered trademark Linux® is used pursuant to a sublicense from the Linux Foundation, the exclusive licensee of Linus Torvalds, owner of the mark on a world­wide basis.