]> Git Repo - linux.git/commitdiff
kprobes: Use struct_size() in __get_insn_slot()
authorNathan Chancellor <[email protected]>
Wed, 30 Oct 2024 16:14:49 +0000 (09:14 -0700)
committerMasami Hiramatsu (Google) <[email protected]>
Thu, 31 Oct 2024 02:00:58 +0000 (11:00 +0900)
__get_insn_slot() allocates 'struct kprobe_insn_page' using a custom
structure size calculation macro, KPROBE_INSN_PAGE_SIZE. Replace
KPROBE_INSN_PAGE_SIZE with the struct_size() macro, which is the
preferred way to calculate the size of flexible structures in the kernel
because it handles overflow and makes it easier to change and audit how
flexible structures are allocated across the entire tree.

Link: https://lore.kernel.org/all/20241030-kprobes-fix-counted-by-annotation-v1-2-8f266001fad0@kernel.org/
(Masami modofied this to be applicable without the 1st patch in the series.)

Signed-off-by: Nathan Chancellor <[email protected]>
Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
kernel/kprobes.c

index 5381c6571fa24831931f017fcb261721c97df886..b027a4030976a2864e9e828179013f79f143be97 100644 (file)
@@ -95,10 +95,6 @@ struct kprobe_insn_page {
        char slot_used[];
 };
 
-#define KPROBE_INSN_PAGE_SIZE(slots)                   \
-       (offsetof(struct kprobe_insn_page, slot_used) + \
-        (sizeof(char) * (slots)))
-
 static int slots_per_page(struct kprobe_insn_cache *c)
 {
        return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
@@ -175,7 +171,7 @@ kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c)
                goto retry;
 
        /* All out of space.  Need to allocate a new page. */
-       kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
+       kip = kmalloc(struct_size(kip, slot_used, slots_per_page(c)), GFP_KERNEL);
        if (!kip)
                goto out;
 
This page took 0.060576 seconds and 4 git commands to generate.