Re: [PATCH v2] ARM: kprobes: Avoid fortify_panic() when copying optprobe template

From: Joel Stanley
Date: Tue Oct 20 2020 - 01:32:51 EST


On Fri, 9 Oct 2020 at 05:20, Joel Stanley <joel@xxxxxxxxx> wrote:
>
> On Thu, 1 Oct 2020 at 04:30, Andrew Jeffery <andrew@xxxxxxxx> wrote:
> >
> > Setting both CONFIG_KPROBES=y and CONFIG_FORTIFY_SOURCE=y on ARM leads
> > to a panic in memcpy() when injecting a kprobe despite the fixes found
> > in commit e46daee53bb5 ("ARM: 8806/1: kprobes: Fix false positive with
> > FORTIFY_SOURCE") and commit 0ac569bf6a79 ("ARM: 8834/1: Fix: kprobes:
> > optimized kprobes illegal instruction").
> >
> > arch/arm/include/asm/kprobes.h effectively declares
> > the target type of the optprobe_template_entry assembly label as a u32
> > which leads memcpy()'s __builtin_object_size() call to determine that
> > the pointed-to object is of size four. However, the symbol is used as a handle
> > for the optimised probe assembly template that is at least 96 bytes in size.
> > The symbol's use despite its type blows up the memcpy() in ARM's
> > arch_prepare_optimized_kprobe() with a false-positive fortify_panic() when it
> > should instead copy the optimised probe template into place:
> >
> > ```
> > $ sudo perf probe -a aspeed_g6_pinctrl_probe
> > [ 158.457252] detected buffer overflow in memcpy
> >
> > Fixes: e46daee53bb5 ("ARM: 8806/1: kprobes: Fix false positive with FORTIFY_SOURCE")
> > Fixes: 0ac569bf6a79 ("ARM: 8834/1: Fix: kprobes: optimized kprobes illegal instruction")
> > Cc: Luka Oreskovic <luka.oreskovic@xxxxxxxxxx>
> > Cc: Juraj Vijtiuk <juraj.vijtiuk@xxxxxxxxxx>
> > Suggested-by: Kees Cook <keescook@xxxxxxxxxxxx>
> > Signed-off-by: Andrew Jeffery <andrew@xxxxxxxx>
>
> Tested-by: Joel Stanley <joel@xxxxxxxxx>
> Reviewed-by: Joel Stanley <joel@xxxxxxxxx>
>
> Thanks Andrew.
>
> > ---
> > v1 was sent some time back, in May:
> >
> > https://lore.kernel.org/linux-arm-kernel/20200517153959.293224-1-andrew@xxxxxxxx/

Russell, are you picking this fix up?

Would you prefer it to go through someone else's tree?

Cheers,

Joel

> >
> > I've taken the patch that Kees' suggested in the replies and tested it.
> > ---
> > arch/arm/include/asm/kprobes.h | 22 +++++++++++-----------
> > arch/arm/probes/kprobes/opt-arm.c | 18 +++++++++---------
> > 2 files changed, 20 insertions(+), 20 deletions(-)
> >
> > diff --git a/arch/arm/include/asm/kprobes.h b/arch/arm/include/asm/kprobes.h
> > index 213607a1f45c..e26a278d301a 100644
> > --- a/arch/arm/include/asm/kprobes.h
> > +++ b/arch/arm/include/asm/kprobes.h
> > @@ -44,20 +44,20 @@ int kprobe_exceptions_notify(struct notifier_block *self,
> > unsigned long val, void *data);
> >
> > /* optinsn template addresses */
> > -extern __visible kprobe_opcode_t optprobe_template_entry;
> > -extern __visible kprobe_opcode_t optprobe_template_val;
> > -extern __visible kprobe_opcode_t optprobe_template_call;
> > -extern __visible kprobe_opcode_t optprobe_template_end;
> > -extern __visible kprobe_opcode_t optprobe_template_sub_sp;
> > -extern __visible kprobe_opcode_t optprobe_template_add_sp;
> > -extern __visible kprobe_opcode_t optprobe_template_restore_begin;
> > -extern __visible kprobe_opcode_t optprobe_template_restore_orig_insn;
> > -extern __visible kprobe_opcode_t optprobe_template_restore_end;
> > +extern __visible kprobe_opcode_t optprobe_template_entry[];
> > +extern __visible kprobe_opcode_t optprobe_template_val[];
> > +extern __visible kprobe_opcode_t optprobe_template_call[];
> > +extern __visible kprobe_opcode_t optprobe_template_end[];
> > +extern __visible kprobe_opcode_t optprobe_template_sub_sp[];
> > +extern __visible kprobe_opcode_t optprobe_template_add_sp[];
> > +extern __visible kprobe_opcode_t optprobe_template_restore_begin[];
> > +extern __visible kprobe_opcode_t optprobe_template_restore_orig_insn[];
> > +extern __visible kprobe_opcode_t optprobe_template_restore_end[];
> >
> > #define MAX_OPTIMIZED_LENGTH 4
> > #define MAX_OPTINSN_SIZE \
> > - ((unsigned long)&optprobe_template_end - \
> > - (unsigned long)&optprobe_template_entry)
> > + ((unsigned long)optprobe_template_end - \
> > + (unsigned long)optprobe_template_entry)
> > #define RELATIVEJUMP_SIZE 4
> >
> > struct arch_optimized_insn {
> > diff --git a/arch/arm/probes/kprobes/opt-arm.c b/arch/arm/probes/kprobes/opt-arm.c
> > index 7a449df0b359..c78180172120 100644
> > --- a/arch/arm/probes/kprobes/opt-arm.c
> > +++ b/arch/arm/probes/kprobes/opt-arm.c
> > @@ -85,21 +85,21 @@ asm (
> > "optprobe_template_end:\n");
> >
> > #define TMPL_VAL_IDX \
> > - ((unsigned long *)&optprobe_template_val - (unsigned long *)&optprobe_template_entry)
> > + ((unsigned long *)optprobe_template_val - (unsigned long *)optprobe_template_entry)
> > #define TMPL_CALL_IDX \
> > - ((unsigned long *)&optprobe_template_call - (unsigned long *)&optprobe_template_entry)
> > + ((unsigned long *)optprobe_template_call - (unsigned long *)optprobe_template_entry)
> > #define TMPL_END_IDX \
> > - ((unsigned long *)&optprobe_template_end - (unsigned long *)&optprobe_template_entry)
> > + ((unsigned long *)optprobe_template_end - (unsigned long *)optprobe_template_entry)
> > #define TMPL_ADD_SP \
> > - ((unsigned long *)&optprobe_template_add_sp - (unsigned long *)&optprobe_template_entry)
> > + ((unsigned long *)optprobe_template_add_sp - (unsigned long *)optprobe_template_entry)
> > #define TMPL_SUB_SP \
> > - ((unsigned long *)&optprobe_template_sub_sp - (unsigned long *)&optprobe_template_entry)
> > + ((unsigned long *)optprobe_template_sub_sp - (unsigned long *)optprobe_template_entry)
> > #define TMPL_RESTORE_BEGIN \
> > - ((unsigned long *)&optprobe_template_restore_begin - (unsigned long *)&optprobe_template_entry)
> > + ((unsigned long *)optprobe_template_restore_begin - (unsigned long *)optprobe_template_entry)
> > #define TMPL_RESTORE_ORIGN_INSN \
> > - ((unsigned long *)&optprobe_template_restore_orig_insn - (unsigned long *)&optprobe_template_entry)
> > + ((unsigned long *)optprobe_template_restore_orig_insn - (unsigned long *)optprobe_template_entry)
> > #define TMPL_RESTORE_END \
> > - ((unsigned long *)&optprobe_template_restore_end - (unsigned long *)&optprobe_template_entry)
> > + ((unsigned long *)optprobe_template_restore_end - (unsigned long *)optprobe_template_entry)
> >
> > /*
> > * ARM can always optimize an instruction when using ARM ISA, except
> > @@ -234,7 +234,7 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *or
> > }
> >
> > /* Copy arch-dep-instance from template. */
> > - memcpy(code, (unsigned long *)&optprobe_template_entry,
> > + memcpy(code, (unsigned long *)optprobe_template_entry,
> > TMPL_END_IDX * sizeof(kprobe_opcode_t));
> >
> > /* Adjust buffer according to instruction. */
> > --
> > 2.25.1
> >