Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [day] [month] [year] [list]
Message-ID: <CAJoZT2iRXtk4C1Kjxf7zmC2MkTRPL5jWKZQVKyRuFjnNCxwZPA@mail.gmail.com>
Date: Thu, 3 Dec 2026 16:02:32 +0800
From: Wenkai Xing <wenkaixing12@...il.com>
To: musl@...ts.openwall.com
Subject: printf: signed overflow and out-of-bounds read in %e with huge precision

Hi,

    In fmt_fp(), the rounding position is computed before the precision is
    checked for overflow:

        j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
        if (j < 9*(z-r-1)) {
                uint32_t x;
                d = r + 1 + ((j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP);
                j += 9*LDBL_MAX_EXP;
                j %= 9;
                for (i=10, j++; j<9; i*=10, j++);
                x = *d % i;

    j is an int. For %e it equals p - e (e being the decimal exponent), so
    it overflows whenever the precision is greater than INT_MAX + e. The
    wrapped value is negative, the rounding branch is entered, and d ends
    up about 238 million limbs before the array. *d then reads roughly 954
    MB below the stack.

    Reproducer (musl 1.2.4 and 1.2.5, x86_64):

    #include <limits.h>
    #include <stdio.h>

    int main(void)
    {
        return printf("%.*e", INT_MAX, 0.5) < 0;
    }

    This segfaults, at an address consistent with the wrapped index.
    Expected is -1 with EOVERFLOW: that is what the check after the
    rounding code ("if (p > INT_MAX-1-...)") produces once it is reached,
    and what printf("%.*e", INT_MAX-1, 0.5) and printf("%.*e", INT_MAX,
    1.5) do return. %.2147483647e crashes the same way, so a literal
    precision is enough too. %f and %g do not crash for the same input.

    Since %.* takes the precision from an argument, this is reachable with
    an untrusted precision.

    Possible fix (not tested): compute j in a wider type. The existing
    checks afterwards then reject the precision with -1/EOVERFLOW. For %g,
    p - e - 1 overflows in its intermediate for the same inputs even though
    the wrapped result happens to be correct, so that expression should be
    covered as well.

    70d2687d and 167dfe96 fixed integer overflows of the same kind nearby.

    Thanks,
    KevinX

Content of type "text/html" skipped

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.