Skip to content

Commit 3fbff98

Browse files
nathanchancemhiramat
authored andcommitted
kprobes: Use struct_size() in __get_insn_slot()
__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 <nathan@kernel.org> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
1 parent da93dd9 commit 3fbff98

1 file changed

Lines changed: 1 addition & 5 deletions

File tree

kernel/kprobes.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ struct kprobe_insn_page {
9595
char slot_used[];
9696
};
9797

98-
#define KPROBE_INSN_PAGE_SIZE(slots) \
99-
(offsetof(struct kprobe_insn_page, slot_used) + \
100-
(sizeof(char) * (slots)))
101-
10298
static int slots_per_page(struct kprobe_insn_cache *c)
10399
{
104100
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)
175171
goto retry;
176172

177173
/* All out of space. Need to allocate a new page. */
178-
kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
174+
kip = kmalloc(struct_size(kip, slot_used, slots_per_page(c)), GFP_KERNEL);
179175
if (!kip)
180176
goto out;
181177

0 commit comments

Comments
 (0)