]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
slub: Handle freelist cycle in on_freelist()
authorLilith Gkini <lilithpgkini@gmail.com>
Wed, 5 Mar 2025 15:48:39 +0000 (17:48 +0200)
committerVlastimil Babka <vbabka@suse.cz>
Thu, 6 Mar 2025 08:39:53 +0000 (09:39 +0100)
The on_freelist() doesn't have a way to handle the edgecase of having a
full freelist that doesn't end in NULL and instead has another valid
pointer in the slab as a result of a Use-After-Free or anything similar.

This case won't get caught by check_valid_pointer() and it will result in
nr incrementing to `slab->objects + 1`, corrupting the slab->inuse entry
later in the code by setting it to -1.

Add an if check to detect that case, report it and handle the freelist
and slab appropriately, as is the standard process in these situations.

Furthermore change the return type of the function from int to bool as
per coding style guidelines.

Also move the `break;` line inside the `if (object) {` to make it more
obvious that the code breaks the while loop in that branch.

Signed-off-by: Lilith Persefoni Gkini <lilithgkini@proton.me>
Reviewed-by: Harry Yoo <harry.yoo@oracle.com>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
mm/slub.c

index 3e6ab4986f8f41cefc56a3420f73a0c5917c11b2..6493b26f08cf522652a2e7a7a08660309fc5d992 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -1441,7 +1441,7 @@ static int check_slab(struct kmem_cache *s, struct slab *slab)
  * Determine if a certain object in a slab is on the freelist. Must hold the
  * slab lock to guarantee that the chains are in a consistent state.
  */
-static int on_freelist(struct kmem_cache *s, struct slab *slab, void *search)
+static bool on_freelist(struct kmem_cache *s, struct slab *slab, void *search)
 {
        int nr = 0;
        void *fp;
@@ -1451,26 +1451,34 @@ static int on_freelist(struct kmem_cache *s, struct slab *slab, void *search)
        fp = slab->freelist;
        while (fp && nr <= slab->objects) {
                if (fp == search)
-                       return 1;
+                       return true;
                if (!check_valid_pointer(s, slab, fp)) {
                        if (object) {
                                object_err(s, slab, object,
                                        "Freechain corrupt");
                                set_freepointer(s, object, NULL);
+                               break;
                        } else {
                                slab_err(s, slab, "Freepointer corrupt");
                                slab->freelist = NULL;
                                slab->inuse = slab->objects;
                                slab_fix(s, "Freelist cleared");
-                               return 0;
+                               return false;
                        }
-                       break;
                }
                object = fp;
                fp = get_freepointer(s, object);
                nr++;
        }
 
+       if (nr > slab->objects) {
+               slab_err(s, slab, "Freelist cycle detected");
+               slab->freelist = NULL;
+               slab->inuse = slab->objects;
+               slab_fix(s, "Freelist cleared");
+               return false;
+       }
+
        max_objects = order_objects(slab_order(slab), s->size);
        if (max_objects > MAX_OBJS_PER_PAGE)
                max_objects = MAX_OBJS_PER_PAGE;