Re: [for-stable-4.19 PATCH 1/2] vmlinux.lds.h: Create section for protection against instrumentation

From: Nicolas Boichat
Date: Fri Mar 19 2021 - 18:49:38 EST


On Fri, Mar 19, 2021 at 7:55 PM Greg Kroah-Hartman
<gregkh@xxxxxxxxxxxxxxxxxxx> wrote:
>
> On Fri, Mar 19, 2021 at 12:20:22PM +0100, Alexandre Chartre wrote:
> >
> > On 3/19/21 11:39 AM, Greg Kroah-Hartman wrote:
> > > On Fri, Mar 19, 2021 at 07:54:15AM +0800, Nicolas Boichat wrote:
> > > > From: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> > > >
> > > > commit 6553896666433e7efec589838b400a2a652b3ffa upstream.
> > > >
> > > > Some code pathes, especially the low level entry code, must be protected
> > > > against instrumentation for various reasons:
> > > >
> > > > - Low level entry code can be a fragile beast, especially on x86.
> > > >
> > > > - With NO_HZ_FULL RCU state needs to be established before using it.
> > > >
> > > > Having a dedicated section for such code allows to validate with tooling
> > > > that no unsafe functions are invoked.
> > > >
> > > > Add the .noinstr.text section and the noinstr attribute to mark
> > > > functions. noinstr implies notrace. Kprobes will gain a section check
> > > > later.
> > > >
> > > > Provide also a set of markers: instrumentation_begin()/end()
> > > >
> > > > These are used to mark code inside a noinstr function which calls
> > > > into regular instrumentable text section as safe.
> > > >
> > > > The instrumentation markers are only active when CONFIG_DEBUG_ENTRY is
> > > > enabled as the end marker emits a NOP to prevent the compiler from merging
> > > > the annotation points. This means the objtool verification requires a
> > > > kernel compiled with this option.
> > > >
> > > > Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> > > > Reviewed-by: Alexandre Chartre <alexandre.chartre@xxxxxxxxxx>
> > > > Acked-by: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> > > > Link: https://lkml.kernel.org/r/20200505134100.075416272@xxxxxxxxxxxxx
> > > >
> > > > [Nicolas: context conflicts in:
> > > > arch/powerpc/kernel/vmlinux.lds.S
> > > > include/asm-generic/vmlinux.lds.h
> > > > include/linux/compiler.h
> > > > include/linux/compiler_types.h]
> > > > Signed-off-by: Nicolas Boichat <drinkcat@xxxxxxxxxxxx>
> > >
> > > Did you build this on x86?
> > >
> > > I get the following build error:
> > >
> > > ld:./arch/x86/kernel/vmlinux.lds:20: syntax error
> > >
> > > And that line looks like:
> > >
> > > . = ALIGN(8); *(.text.hot .text.hot.*) *(.text .text.fixup) *(.text.unlikely .text.unlikely.*) *(.text.unknown .text.unknown.*) . = ALIGN(8); __noinstr_text_start = .; *(.__attribute__((noinline)) __attribute__((no_instrument_function)) __attribute((__section__(".noinstr.text"))).text) __noinstr_text_end = .; *(.text..refcount) *(.ref.text) *(.meminit.text*) *(.memexit.text*)
> > >
> >
> > In the NOINSTR_TEXT macro, noinstr is expanded with the value of the noinstr
> > macro from linux/compiler_types.h while it shouldn't.
> >
> > The problem is possibly that the noinstr macro is defined for assembly. Make
> > sure that the macro is not defined for assembly e.g.:
> >
> > #ifndef __ASSEMBLY__
> >
> > /* Section for code which can't be instrumented at all */
> > #define noinstr \
> > noinline notrace __attribute((__section__(".noinstr.text")))
> >
> > #endif
>
> This implies that the backport is incorrect, so I'll wait for an updated
> version...

Yep, sorry about that. I did test on ARM64 only and these patches
happily went through our Chrome OS CQ (we don't have gcc coverage
though).

Guenter has a fixup here with explanation:
https://crrev.com/c/2776332, I'll look carefully and resubmit.

Thanks,

> thanks,
>
> greg k-h