linux-audit / audit-userspace

Linux audit userspace repository
GNU General Public License v2.0
567 stars 201 forks source link

In-house implementation of `strndupa` is faulty #381

Open MIvanchev opened 2 weeks ago

MIvanchev commented 2 weeks ago
#ifndef HAVE_STRNDUPA
static inline char *strndupa(const char *old, size_t n)
{
    size_t len = strnlen(old, n);
    char *tmp = alloca(len + 1);
    tmp[len] = 0;
    return memcpy(tmp, old, len);
}
#endif

This code is undefined behavior, a pointer to stack memory is returned but the stack memory is reclaimed immediately. See our build for Void Linux https://github.com/void-linux/void-packages/pull/50666