Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAJoZT2hP2iEufhuh8EU2Xdhr2NfKSXKBC3heKYHtwq9qp2Qu_A@mail.gmail.com>
Date: Thu, 3 Dec 2026 15:31:32 +0800
From: Wenkai Xing <wenkaixing12@...il.com>
To: musl@...ts.openwall.com
Subject: positional precision (%.*N$) reads uninitialized nl_arg

Hi,

    printf_core() runs twice: the first pass (f == 0) records the types of
    positional arguments and, at its end, pops their values into nl_arg[].
    That final loop is only reached when l10n is set.

    The width form %*N$ sets l10n, but the precision form %.*N$ does not:

        if (isdigit(s[2]) && s[3]=='$') {
                if (!f) nl_type[s[2]-'0'] = INT, p = 0;
                else p = nl_arg[s[2]-'0'].i;
                s+=4;
        }

    So when a positional precision is the only positional reference in the
    format, the first pass returns early at "if (!l10n) return 0;" without
    filling nl_arg[], and the second pass reads an uninitialized slot.

    Reproducer (1.2.5 and master):

    #include <errno.h>
    #include <stdio.h>

    int main(void)
    {
        errno = ENOENT;
        printf("[%.*1$m]\n", 3);
        return 0;
    }

    Expected "[No ]" (strerror(ENOENT) cut to 3 characters; glibc prints
    that). The precision actually used is whatever is in the uninitialized
    nl_arg[1], so the output is indeterminate: the message is truncated to
    an arbitrary length, possibly to nothing, or printed in full.

    %m is the natural trigger because it consumes no argument of its own,
    which lets the precision be the only positional reference.

    7d358599d4c8 ("prevent invalid reads of nl_arg in printf_core") made the
    first pass stop reading nl_arg here, on the premise that it "gets
    initialized at the end of the first run". That does not hold in this
    case.

    Suggested fix, mirroring the width branch:

        if (isdigit(s[2]) && s[3]=='$') {
                l10n=1;
                if (!f) nl_type[s[2]-'0'] = INT, p = 0;
                else p = nl_arg[s[2]-'0'].i;
                s+=4;
        }

    This also makes gaps such as %.*2$m fail with EINVAL, like other
    positional formats. wprintf_core() is missing the same l10n=1.

    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.