Re: [PATCH] slub: Fixes freepointer encoding for single free

From: Vlastimil Babka
Date: Mon Apr 29 2024 - 16:23:03 EST


On 4/29/24 6:16 PM, Nicolas Bouchinet wrote:
> On 4/29/24 16:52, Chengming Zhou wrote:
>> On 2024/4/29 22:32, Nicolas Bouchinet wrote:
>>> On 4/29/24 15:35, Chengming Zhou wrote:
>>>> On 2024/4/29 20:59, Nicolas Bouchinet wrote:
>>>>>>
>>>>>> I help maintaining the Linux-Hardened patchset in which we have a slab object canary feature that helps detecting overflows. It is located just after the object freepointer.
>>>>> I've tried a patch where the freepointer is avoided but it results in the same bug. It seems that the commit 0f181f9fbea8bc7ea ("mm/slub.c: init_on_free=1 should wipe freelist ptr for bulk allocations") inits the freepointer on allocation if init_on_free is set in order to return a clean initialized object to the caller.
>>>>>
>>>> Good catch! You may need to change maybe_wipe_obj_freeptr() too,
>>>> I haven't tested this, not sure whether it works for you. :)
>>>>
>>>> diff --git a/mm/slub.c b/mm/slub.c
>>>> index 3e33ff900d35..3f250a167cb5 100644
>>>> --- a/mm/slub.c
>>>> +++ b/mm/slub.c
>>>> @@ -3796,7 +3796,8 @@ static void *__slab_alloc_node(struct kmem_cache *s,
>>>>   static __always_inline void maybe_wipe_obj_freeptr(struct kmem_cache *s,
>>>>                                                     void *obj)
>>>>   {
>>>> -       if (unlikely(slab_want_init_on_free(s)) && obj)
>>>> +       if (unlikely(slab_want_init_on_free(s)) && obj &&
>>>> +           !freeptr_outside_object(s))
>>>>                  memset((void *)((char *)kasan_reset_tag(obj) + s->offset),
>>>>                          0, sizeof(void *));
>>>>   }
>>>>
>>>> Thanks!
>>> Indeed since check_object() avoids objects for which freepointer is in the object and since val is equal to SLUB_RED_ACTIVE in our specific case it should work. Do you want me to add you as Co-authored ?
>>>
>> Ok, it's great. Thanks!
>
> Now I think of it, doesn't it seems a bit odd to only properly
> init_on_free object's freepointer only if it's inside the object ? IMHO
> it is equally necessary to avoid information leaking about the
> freepointer whether it is inside or outside the object.
> I think it break the semantic of the commit 0f181f9fbea8bc7ea
> ("mm/slub.c: init_on_free=1 should wipe freelist ptr for bulk
> allocations") ?

Hm, AFAIU, wiping inside object prevents misuse of some buggy kernel code
that would allocate and accidentally leak prior content (including the
in-object freepointer) somewhere the attacker can read. Now for wiping the
freepointer outside the object to be useful it would have assume said
leak-prone code to additionally be reading past the allocated object size,
i.e. a read buffer overflow. That to me seems to be a much more rare
combination, and also in that case such code could also likely read even
further past the object, i.e. leak the next object's data? IOW I don't think
it buys us much additional security protection in practice?

> Thanks.
>