Re: [PATCH] lkdtm: Avoid objtool/ibt warning

From: Kees Cook
Date: Wed Nov 16 2022 - 17:32:00 EST


On Mon, Nov 14, 2022 at 04:23:45PM +0100, Peter Zijlstra wrote:
> On Mon, Nov 14, 2022 at 08:26:17PM +0800, kernel test robot wrote:
> > tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> > head: 094226ad94f471a9f19e8f8e7140a09c2625abaa
> > commit: 656d054e0a15ec327bd82801ccd58201e59f6896 jump_label,noinstr: Avoid instrumentation for JUMP_LABEL=n builds
> > date: 6 months ago
> > config: x86_64-buildonly-randconfig-r001-20221114
> > compiler: gcc-11 (Debian 11.3.0-8) 11.3.0
> > reproduce (this is a W=1 build):
> > # https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=656d054e0a15ec327bd82801ccd58201e59f6896
> > git remote add linus https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
> > git fetch --no-tags linus master
> > git checkout 656d054e0a15ec327bd82801ccd58201e59f6896
> > # save the config file
> > mkdir build_dir && cp config build_dir/.config
> > make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash
>
>
> > >> vmlinux.o: warning: objtool: lkdtm_UNSET_SMEP+0x1c3: relocation to !ENDBR: native_write_cr4+0x41
>
> ---
> Subject: lkdtm: Avoid objtool/ibt warning
> From: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Date: Mon Nov 14 16:17:50 CET 2022
>
> For certain configs objtool will complain like:
>
> vmlinux.o: warning: objtool: lkdtm_UNSET_SMEP+0x1c3: relocation to !ENDBR: native_write_cr4+0x41
>
> What happens is that GCC optimizes the loop:
>
> insn = (unsigned char *)native_write_cr4;
> for (i = 0; i < MOV_CR4_DEPTH; i++)
>
> to read something like:
>
> for (insn = (unsigned char *)native_write_cr4;
> insn < (unsigned char *)native_write_cr4 + MOV_CR4_DEPTH;
> insn++)
>
> Which then obviously generates the text reference
> native_write_cr4+041. Since none of this is a fast path, simply
> confuse GCC enough to inhibit this optimization.
>
> Reported-by: kernel test robot <lkp@xxxxxxxxx>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>

Acked-by: Kees Cook <keescook@xxxxxxxxxxxx>

Please feel free to carry this, or forward it to Greg KH.

-Kees

> ---
> drivers/misc/lkdtm/bugs.c | 1 +
> 1 file changed, 1 insertion(+)
>
> --- a/drivers/misc/lkdtm/bugs.c
> +++ b/drivers/misc/lkdtm/bugs.c
> @@ -487,6 +487,7 @@ static void lkdtm_UNSET_SMEP(void)
> * the cr4 writing instruction.
> */
> insn = (unsigned char *)native_write_cr4;
> + OPTIMIZER_HIDE_VAR(insn);
> for (i = 0; i < MOV_CR4_DEPTH; i++) {
> /* mov %rdi, %cr4 */
> if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7)

--
Kees Cook