]> www.infradead.org Git - users/dwmw2/openconnect.git/commit
fix potential read overflow in compat.c replacement for strndup()
authorDaniel Lenski <dlenski@gmail.com>
Tue, 23 Feb 2021 05:17:13 +0000 (21:17 -0800)
committerDaniel Lenski <dlenski@gmail.com>
Tue, 23 Feb 2021 05:17:13 +0000 (21:17 -0800)
commita01e74536201ce55511f782b6b2c03d7609f3533
tree77772cf7f952023cc87f4b2bf44085a3ecb98948
parent859b76a9b77cef024faf41419ba14839d35d0a68
fix potential read overflow in compat.c replacement for strndup()

The openconnect__strndup() function is used as a replacement for
strndup() on platforms that lack it.

It is unsafe in its current form, because it calls strlen() on
a buffer that may not be zero-terminated.

Here's a short C program that demonstrates the issue:

    #include <stdio.h>
    #include <string.h>

    int main(int argc, char **argv)
    {
        char *foo = (void *)printf; /* should be legal to read at least 4 bytes */
        printf("We didn't crash in strndup (EXPECTED): %s.\n", strndup(foo, 3));
        printf("We didn't crash in strlen (NOT GUARANTEED): %d\n", strlen(foo));
        return 0;
    }

Signed-off-by: Daniel Lenski <dlenski@gmail.com>
compat.c