On Fri, Mar 14, 2025 at 08:15:45PM -0700, Kees Cook wrote:
> +Performing open-coded kmalloc()-family allocation assignments prevents
> +the kernel (and compiler) from being able to examine the type of the
> +variable being assigned, which limits any related introspection that
> +may help with alignment, wrap-around, or additional hardening. The
> +kmalloc_obj()-family of macros provide this introspection, which can be
> +used for the common code patterns for single, array, and flexible object
> +allocations. For example, these open coded assignments::
> +
> + ptr = kmalloc(sizeof(*ptr), gfp);
> + ptr = kmalloc(sizeof(struct the_type_of_ptr_obj), gfp);
> + ptr = kzalloc(sizeof(*ptr), gfp);
> + ptr = kmalloc_array(count, sizeof(*ptr), gfp);
> + ptr = kcalloc(count, sizeof(*ptr), gfp);
> + ptr = kmalloc(struct_size(ptr, flex_member, count), gfp);
> +
> +become, respectively::
> +
> + kmalloc_obj(ptr, gfp);
> + kzalloc_obj(ptr, gfp);
> + kmalloc_objs(ptr, count, gfp);
> + kzalloc_objs(ptr, count, gfp);
> + kmalloc_flex(ptr, flex_member, count, gfp);
I'd like to propose a different approach for consideration.
The first two are obvious. If we now believe that object type is the
important thing, well we have an API for that:
struct buffer_head { ... };
bh_cache = KMEM_CACHE(buffer_head, SLAB_RECLAIM_ACCOUNT);
...
ptr = kmem_cache_alloc(bh_cache, GFP_KERNEL);
It's a little more verbose than what you're suggesting, but it does give
us the chance to specify SLAB_ flags. It already does the alignment
optimisation you're suggesting. Maybe we can use some macro sugar to
simplify this.
The array ones are a little tougher. Let's set them aside for the
moment and tackle the really hard one; kmalloc_flex.
Slab is fundamentally built on all objects in the slab being the same
size. But maybe someone sufficiently enterprising could build a
"variable sized" slab variant? If we're saying that object type is
more important a discriminator than object size, then perhaps grouping
objects of the same size together isn't nearly as useful as grouping
objects of the same type together, even if they're different sizes.
Might be a good GSoC/outreachy project?