KSPP / linux

Linux kernel source tree (Kernel Self Protection Project)
https://kernsec.org/wiki/index.php/Kernel_Self_Protection_Project
Other
81 stars 5 forks source link

Deal with kmalloc vs ksize vs __alloc_size #183

Open kees opened 2 years ago

kees commented 2 years ago

There needs to be a way to deal with __alloc_size not matching ksize().

https://lore.kernel.org/lkml/202202281516.19274C0@keescook/

Plan: remove side-effect from ksize() and refactor all users to either use krealloc() afterwards, or kmalloc_roundup_size() before.

in v6.1:

in -next:

partially reviewed:

unreviewed:

finally:

kees commented 2 years ago

Idea from keithp: create a wrapper func to re-assign __alloc_size() as if we went through realloc(). For example:

static inline __alloc_size(2) void *__resize_kmalloc(void *objp, size)
{
    return objp;
}
#define ksize(objp) ({                          \
    /*                              \
     * Getting the actual allocation size means the __alloc_size    \
     * hints are no longer valid, and the compiler needs to     \
     * learn the new one.                       \
     */                             \
        size_t __new_kmalloc_size;                                  \
        __new_kmalloc_size = _ksize(objp);                              \
        objp = __resize_kmalloc(objp, __new_kmalloc_size);              \
    __new_kmalloc_size;                         \
})
kees commented 1 year ago

See https://github.com/ClangBuiltLinux/linux/issues/1599

kees commented 1 year ago

The wrapper won't work because GCC ignores __alloc_size on inlines. 😠

So, introduce kmalloc_size_roundup() and remove the side-effect from ksize().