alpine 3.8
format weakness #15

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/vasprintf.c

Context:

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

    bothering to identify which style vsnprintf we've got, so just take the
   pessimistic option and assume it's glibc 2.0.x.

   Notice the use of ret+2 for the new space in the C99 case.  This ensures
   the next vsnprintf return value will be space-2, which is unambiguously
   successful.  But actually GMP_ASPRINTF_T_NEED() will realloc to even
   bigger than that ret+2.

   vsnprintf might trash it's given ap, so copy it in case we need to use it
   more than once.  See comments with gmp_snprintf_format.  */

static int
gmp_asprintf_format (struct gmp_asprintf_t *d, const char *fmt,
                     va_list orig_ap)
{
  int      ret;
  va_list  ap;
  size_t   space = 256;

  for (;;)
    {
      GMP_ASPRINTF_T_NEED (d, space);
      space = d->alloc - d->size;
      va_copy (ap, orig_ap);
      ret = vsnprintf (d->buf + d->size, space, fmt, ap);
      if (ret == -1)
        {
          ASSERT (strlen (d->buf + d->size) == space-1);
          ret = space-1;
        }

      /* done if output fits in our space */
      if (ret < space-1)
        break;

      if (ret == space-1)
        space *= 2;     /* possible glibc 2.0.x, so double */
      else
        space = ret+2;  /* C99, so now know space required */
    }

  d->size += ret;
  return ret;
}

const struct doprnt_funs_t  __gmp_asprintf_funs = {
  (doprnt_format_t) gmp_asprintf_format,
  (doprnt_memory_t) __gmp_asprintf_memory,
  (doprnt_reps_t)   __gmp_asprintf_reps,
  (doprnt_final_t)  __gmp_asprintf_final 

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.