]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
kasan: unpoison vms[area] addresses with a common tag
authorMaciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
Thu, 4 Dec 2025 19:00:11 +0000 (19:00 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 8 Jan 2026 09:14:49 +0000 (10:14 +0100)
commit 6a0e5b333842cf65d6f4e4f0a2a4386504802515 upstream.

A KASAN tag mismatch, possibly causing a kernel panic, can be observed on
systems with a tag-based KASAN enabled and with multiple NUMA nodes.  It
was reported on arm64 and reproduced on x86.  It can be explained in the
following points:

1. There can be more than one virtual memory chunk.
2. Chunk's base address has a tag.
3. The base address points at the first chunk and thus inherits
   the tag of the first chunk.
4. The subsequent chunks will be accessed with the tag from the
   first chunk.
5. Thus, the subsequent chunks need to have their tag set to
   match that of the first chunk.

Use the new vmalloc flag that disables random tag assignment in
__kasan_unpoison_vmalloc() - pass the same random tag to all the
vm_structs by tagging the pointers before they go inside
__kasan_unpoison_vmalloc().  Assigning a common tag resolves the pcpu
chunk address mismatch.

[akpm@linux-foundation.org: use WARN_ON_ONCE(), per Andrey]
Link: https://lkml.kernel.org/r/CA+fCnZeuGdKSEm11oGT6FS71_vGq1vjq-xY36kxVdFvwmag2ZQ@mail.gmail.com
[maciej.wieczor-retman@intel.com: remove unneeded pr_warn()]
Link: https://lkml.kernel.org/r/919897daaaa3c982a27762a2ee038769ad033991.1764945396.git.m.wieczorretman@pm.me
Link: https://lkml.kernel.org/r/873821114a9f722ffb5d6702b94782e902883fdf.1764874575.git.m.wieczorretman@pm.me
Fixes: 1d96320f8d53 ("kasan, vmalloc: add vmalloc tagging for SW_TAGS")
Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Danilo Krummrich <dakr@kernel.org>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Jiayuan Chen <jiayuan.chen@linux.dev>
Cc: Kees Cook <kees@kernel.org>
Cc: Marco Elver <elver@google.com>
Cc: "Uladzislau Rezki (Sony)" <urezki@gmail.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: <stable@vger.kernel.org> [6.1+]
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
mm/kasan/common.c

index 7c66d5445cf5d8447d69130170a0e85dd3bb9671..c49b8520b3647b6b4923db9ae55b8c8af35cb866 100644 (file)
@@ -568,11 +568,26 @@ void __kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
        unsigned long size;
        void *addr;
        int area;
+       u8 tag;
+
+       /*
+        * If KASAN_VMALLOC_KEEP_TAG was set at this point, all vms[] pointers
+        * would be unpoisoned with the KASAN_TAG_KERNEL which would disable
+        * KASAN checks down the line.
+        */
+       if (WARN_ON_ONCE(flags & KASAN_VMALLOC_KEEP_TAG))
+               return;
+
+       size = vms[0]->size;
+       addr = vms[0]->addr;
+       vms[0]->addr = __kasan_unpoison_vmalloc(addr, size, flags);
+       tag = get_tag(vms[0]->addr);
 
-       for (area = 0 ; area < nr_vms ; area++) {
+       for (area = 1 ; area < nr_vms ; area++) {
                size = vms[area]->size;
-               addr = vms[area]->addr;
-               vms[area]->addr = __kasan_unpoison_vmalloc(addr, size, flags);
+               addr = set_tag(vms[area]->addr, tag);
+               vms[area]->addr =
+                       __kasan_unpoison_vmalloc(addr, size, flags | KASAN_VMALLOC_KEEP_TAG);
        }
 }
 #endif