2022-07-12 19:23:20

by Greg Kroah-Hartman

[permalink] [raw]
Subject: [PATCH 5.18 34/61] objtool: Update Retpoline validation

From: Peter Zijlstra <[email protected]>

commit 9bb2ec608a209018080ca262f771e6a9ff203b6f upstream.

Update retpoline validation with the new CONFIG_RETPOLINE requirement of
not having bare naked RET instructions.

Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Signed-off-by: Borislav Petkov <[email protected]>
Reviewed-by: Josh Poimboeuf <[email protected]>
Signed-off-by: Borislav Petkov <[email protected]>
Signed-off-by: Thadeu Lima de Souza Cascardo <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
---
arch/x86/include/asm/nospec-branch.h | 6 ++++++
arch/x86/mm/mem_encrypt_boot.S | 2 ++
arch/x86/xen/xen-head.S | 1 +
tools/objtool/check.c | 19 +++++++++++++------
4 files changed, 22 insertions(+), 6 deletions(-)

--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -76,6 +76,12 @@
.endm

/*
+ * (ab)use RETPOLINE_SAFE on RET to annotate away 'bare' RET instructions
+ * vs RETBleed validation.
+ */
+#define ANNOTATE_UNRET_SAFE ANNOTATE_RETPOLINE_SAFE
+
+/*
* JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
* indirect jmp/call which may be susceptible to the Spectre variant 2
* attack.
--- a/arch/x86/mm/mem_encrypt_boot.S
+++ b/arch/x86/mm/mem_encrypt_boot.S
@@ -66,6 +66,7 @@ SYM_FUNC_START(sme_encrypt_execute)
pop %rbp

/* Offset to __x86_return_thunk would be wrong here */
+ ANNOTATE_UNRET_SAFE
ret
int3
SYM_FUNC_END(sme_encrypt_execute)
@@ -154,6 +155,7 @@ SYM_FUNC_START(__enc_copy)
pop %r15

/* Offset to __x86_return_thunk would be wrong here */
+ ANNOTATE_UNRET_SAFE
ret
int3
.L__enc_copy_end:
--- a/arch/x86/xen/xen-head.S
+++ b/arch/x86/xen/xen-head.S
@@ -26,6 +26,7 @@ SYM_CODE_START(hypercall_page)
.rept (PAGE_SIZE / 32)
UNWIND_HINT_FUNC
ANNOTATE_NOENDBR
+ ANNOTATE_UNRET_SAFE
ret
/*
* Xen will write the hypercall page, and sort out ENDBR.
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2114,8 +2114,9 @@ static int read_retpoline_hints(struct o
}

if (insn->type != INSN_JUMP_DYNAMIC &&
- insn->type != INSN_CALL_DYNAMIC) {
- WARN_FUNC("retpoline_safe hint not an indirect jump/call",
+ insn->type != INSN_CALL_DYNAMIC &&
+ insn->type != INSN_RETURN) {
+ WARN_FUNC("retpoline_safe hint not an indirect jump/call/ret",
insn->sec, insn->offset);
return -1;
}
@@ -3648,7 +3649,8 @@ static int validate_retpoline(struct obj

for_each_insn(file, insn) {
if (insn->type != INSN_JUMP_DYNAMIC &&
- insn->type != INSN_CALL_DYNAMIC)
+ insn->type != INSN_CALL_DYNAMIC &&
+ insn->type != INSN_RETURN)
continue;

if (insn->retpoline_safe)
@@ -3663,9 +3665,14 @@ static int validate_retpoline(struct obj
if (!strcmp(insn->sec->name, ".init.text") && !module)
continue;

- WARN_FUNC("indirect %s found in RETPOLINE build",
- insn->sec, insn->offset,
- insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
+ if (insn->type == INSN_RETURN) {
+ WARN_FUNC("'naked' return found in RETPOLINE build",
+ insn->sec, insn->offset);
+ } else {
+ WARN_FUNC("indirect %s found in RETPOLINE build",
+ insn->sec, insn->offset,
+ insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
+ }

warnings++;
}



2022-07-13 08:08:59

by Jiri Slaby

[permalink] [raw]
Subject: Re: [PATCH 5.18 34/61] objtool: Update Retpoline validation

On 12. 07. 22, 20:39, Greg Kroah-Hartman wrote:
> From: Peter Zijlstra <[email protected]>
>
> commit 9bb2ec608a209018080ca262f771e6a9ff203b6f upstream.
>
> Update retpoline validation with the new CONFIG_RETPOLINE requirement of
> not having bare naked RET instructions.

Hi,

this breaks compilation on i386:
> arch/x86/kernel/../../x86/xen/xen-head.S:35: Error: no such
instruction: `annotate_unret_safe'

Config:
https://raw.githubusercontent.com/openSUSE/kernel-source/stable/config/i386/pae

And yeah, upstream¹⁾ is affected too.

¹⁾I am at commit b047602d579b4fb028128a525f056bbdc890e7f0.

> Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
> Signed-off-by: Borislav Petkov <[email protected]>
> Reviewed-by: Josh Poimboeuf <[email protected]>
> Signed-off-by: Borislav Petkov <[email protected]>
> Signed-off-by: Thadeu Lima de Souza Cascardo <[email protected]>
> Signed-off-by: Greg Kroah-Hartman <[email protected]>
> ---
> arch/x86/include/asm/nospec-branch.h | 6 ++++++
> arch/x86/mm/mem_encrypt_boot.S | 2 ++
> arch/x86/xen/xen-head.S | 1 +
> tools/objtool/check.c | 19 +++++++++++++------
> 4 files changed, 22 insertions(+), 6 deletions(-)
>
> --- a/arch/x86/include/asm/nospec-branch.h
> +++ b/arch/x86/include/asm/nospec-branch.h
> @@ -76,6 +76,12 @@
> .endm
>
> /*
> + * (ab)use RETPOLINE_SAFE on RET to annotate away 'bare' RET instructions
> + * vs RETBleed validation.
> + */
> +#define ANNOTATE_UNRET_SAFE ANNOTATE_RETPOLINE_SAFE
> +
> +/*
> * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
> * indirect jmp/call which may be susceptible to the Spectre variant 2
> * attack.
> --- a/arch/x86/mm/mem_encrypt_boot.S
> +++ b/arch/x86/mm/mem_encrypt_boot.S
> @@ -66,6 +66,7 @@ SYM_FUNC_START(sme_encrypt_execute)
> pop %rbp
>
> /* Offset to __x86_return_thunk would be wrong here */
> + ANNOTATE_UNRET_SAFE
> ret
> int3
> SYM_FUNC_END(sme_encrypt_execute)
> @@ -154,6 +155,7 @@ SYM_FUNC_START(__enc_copy)
> pop %r15
>
> /* Offset to __x86_return_thunk would be wrong here */
> + ANNOTATE_UNRET_SAFE
> ret
> int3
> .L__enc_copy_end:
> --- a/arch/x86/xen/xen-head.S
> +++ b/arch/x86/xen/xen-head.S
> @@ -26,6 +26,7 @@ SYM_CODE_START(hypercall_page)
> .rept (PAGE_SIZE / 32)
> UNWIND_HINT_FUNC
> ANNOTATE_NOENDBR
> + ANNOTATE_UNRET_SAFE
> ret
> /*
> * Xen will write the hypercall page, and sort out ENDBR.
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -2114,8 +2114,9 @@ static int read_retpoline_hints(struct o
> }
>
> if (insn->type != INSN_JUMP_DYNAMIC &&
> - insn->type != INSN_CALL_DYNAMIC) {
> - WARN_FUNC("retpoline_safe hint not an indirect jump/call",
> + insn->type != INSN_CALL_DYNAMIC &&
> + insn->type != INSN_RETURN) {
> + WARN_FUNC("retpoline_safe hint not an indirect jump/call/ret",
> insn->sec, insn->offset);
> return -1;
> }
> @@ -3648,7 +3649,8 @@ static int validate_retpoline(struct obj
>
> for_each_insn(file, insn) {
> if (insn->type != INSN_JUMP_DYNAMIC &&
> - insn->type != INSN_CALL_DYNAMIC)
> + insn->type != INSN_CALL_DYNAMIC &&
> + insn->type != INSN_RETURN)
> continue;
>
> if (insn->retpoline_safe)
> @@ -3663,9 +3665,14 @@ static int validate_retpoline(struct obj
> if (!strcmp(insn->sec->name, ".init.text") && !module)
> continue;
>
> - WARN_FUNC("indirect %s found in RETPOLINE build",
> - insn->sec, insn->offset,
> - insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
> + if (insn->type == INSN_RETURN) {
> + WARN_FUNC("'naked' return found in RETPOLINE build",
> + insn->sec, insn->offset);
> + } else {
> + WARN_FUNC("indirect %s found in RETPOLINE build",
> + insn->sec, insn->offset,
> + insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
> + }
>
> warnings++;
> }
>
>


--
js
suse labs

2022-07-13 08:43:02

by Jiri Slaby

[permalink] [raw]
Subject: Re: [PATCH 5.18 34/61] objtool: Update Retpoline validation

On 13. 07. 22, 9:45, Jiri Slaby wrote:
> On 12. 07. 22, 20:39, Greg Kroah-Hartman wrote:
>> From: Peter Zijlstra <[email protected]>
>>
>> commit 9bb2ec608a209018080ca262f771e6a9ff203b6f upstream.
>>
>> Update retpoline validation with the new CONFIG_RETPOLINE requirement of
>> not having bare naked RET instructions.
>
> Hi,
>
> this breaks compilation on i386:
> > arch/x86/kernel/../../x86/xen/xen-head.S:35: Error: no such
> instruction: `annotate_unret_safe'
>
> Config:
> https://raw.githubusercontent.com/openSUSE/kernel-source/stable/config/i386/pae
>
>
> And yeah, upstream¹⁾ is affected too.
>
> ¹⁾I am at commit b047602d579b4fb028128a525f056bbdc890e7f0.

A naive fix is:
--- a/arch/x86/kernel/head_32.S
+++ b/arch/x86/kernel/head_32.S
@@ -23,6 +23,7 @@
#include <asm/cpufeatures.h>
#include <asm/percpu.h>
#include <asm/nops.h>
+#include <asm/nospec-branch.h>
#include <asm/bootparam.h>
#include <asm/export.h>
#include <asm/pgtable_32.h>

The question (I don't know answer to) is whether x86_32 should actually
do ANNOTATE_UNRET_SAFE.

thanks,
--
js

2022-07-13 09:09:38

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH 5.18 34/61] objtool: Update Retpoline validation

On Wed, Jul 13, 2022 at 09:54:01AM +0200, Jiri Slaby wrote:
> On 13. 07. 22, 9:45, Jiri Slaby wrote:
> > On 12. 07. 22, 20:39, Greg Kroah-Hartman wrote:
> > > From: Peter Zijlstra <[email protected]>
> > >
> > > commit 9bb2ec608a209018080ca262f771e6a9ff203b6f upstream.
> > >
> > > Update retpoline validation with the new CONFIG_RETPOLINE requirement of
> > > not having bare naked RET instructions.
> >
> > Hi,
> >
> > this breaks compilation on i386:
> > > arch/x86/kernel/../../x86/xen/xen-head.S:35: Error: no such
> > instruction: `annotate_unret_safe'
> >
> > Config:
> > https://raw.githubusercontent.com/openSUSE/kernel-source/stable/config/i386/pae
> >
> >
> > And yeah, upstream¹⁾ is affected too.
> >
> > ¹⁾I am at commit b047602d579b4fb028128a525f056bbdc890e7f0.
>
> A naive fix is:
> --- a/arch/x86/kernel/head_32.S
> +++ b/arch/x86/kernel/head_32.S
> @@ -23,6 +23,7 @@
> #include <asm/cpufeatures.h>
> #include <asm/percpu.h>
> #include <asm/nops.h>
> +#include <asm/nospec-branch.h>
> #include <asm/bootparam.h>
> #include <asm/export.h>
> #include <asm/pgtable_32.h>
>
> The question (I don't know answer to) is whether x86_32 should actually do
> ANNOTATE_UNRET_SAFE.

I doubt it should be doing that, but I'll let others answer more
definitively.

Your commit seems sane, for some reason I thought Boris tested i386
builds, but maybe in the end something snuck in that broke it.

thanks,

greg k-h

2022-07-13 09:31:19

by Borislav Petkov

[permalink] [raw]
Subject: Re: [PATCH 5.18 34/61] objtool: Update Retpoline validation

On Wed, Jul 13, 2022 at 10:17:54AM +0200, Greg Kroah-Hartman wrote:
> > A naive fix is:
> > --- a/arch/x86/kernel/head_32.S
> > +++ b/arch/x86/kernel/head_32.S
> > @@ -23,6 +23,7 @@
> > #include <asm/cpufeatures.h>
> > #include <asm/percpu.h>
> > #include <asm/nops.h>
> > +#include <asm/nospec-branch.h>
> > #include <asm/bootparam.h>
> > #include <asm/export.h>
> > #include <asm/pgtable_32.h>

Yap, pls send a proper patch.

> I doubt it should be doing that, but I'll let others answer more
> definitively.

I'd leave that question to the objtool folks.

Technically, the UNTRAIN_RET sequence is meant for both 32- and 64-bit
so there should be no reason not to mitigate that on 32-bit too. Not
that people should run 32-bit kernels...

> Your commit seems sane, for some reason I thought Boris tested i386
> builds, but maybe in the end something snuck in that broke it.

I caught one build breakage in relocate_kernel_32.S but this is a new
one.

Thx.

--
Regards/Gruss,
Boris.

SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
(HRB 36809, AG Nürnberg)

2022-07-13 10:25:10

by Jiri Slaby

[permalink] [raw]
Subject: [PATCH] x86/asm/32: fix ANNOTATE_UNRET_SAFE use on 32bit

The build on x86_32 currently fails after commit 9bb2ec608a20 (objtool:
Update Retpoline validation) with:
arch/x86/kernel/../../x86/xen/xen-head.S:35: Error: no such instruction: `annotate_unret_safe'

ANNOTATE_UNRET_SAFE is defined in nospec-branch.h. And head_32.S is
missing this include. Fix this.

Fixes: 9bb2ec608a20 (objtool: Update Retpoline validation)
Cc: Peter Zijlstra (Intel) <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Josh Poimboeuf <[email protected]>
Signed-off-by: Jiri Slaby <[email protected]>
---
arch/x86/kernel/head_32.S | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/x86/kernel/head_32.S b/arch/x86/kernel/head_32.S
index eb8656bac99b..9b7acc9c7874 100644
--- a/arch/x86/kernel/head_32.S
+++ b/arch/x86/kernel/head_32.S
@@ -23,6 +23,7 @@
#include <asm/cpufeatures.h>
#include <asm/percpu.h>
#include <asm/nops.h>
+#include <asm/nospec-branch.h>
#include <asm/bootparam.h>
#include <asm/export.h>
#include <asm/pgtable_32.h>
--
2.37.0

Subject: [tip: x86/urgent] x86/asm/32: Fix ANNOTATE_UNRET_SAFE use on 32-bit

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID: 3131ef39fb03bbde237d0b8260445898f3dfda5b
Gitweb: https://git.kernel.org/tip/3131ef39fb03bbde237d0b8260445898f3dfda5b
Author: Jiri Slaby <[email protected]>
AuthorDate: Wed, 13 Jul 2022 11:50:46 +02:00
Committer: Borislav Petkov <[email protected]>
CommitterDate: Wed, 13 Jul 2022 12:43:26 +02:00

x86/asm/32: Fix ANNOTATE_UNRET_SAFE use on 32-bit

The build on x86_32 currently fails after commit

9bb2ec608a20 (objtool: Update Retpoline validation)

with:

arch/x86/kernel/../../x86/xen/xen-head.S:35: Error: no such instruction: `annotate_unret_safe'

ANNOTATE_UNRET_SAFE is defined in nospec-branch.h. And head_32.S is
missing this include. Fix this.

Fixes: 9bb2ec608a20 ("objtool: Update Retpoline validation")
Signed-off-by: Jiri Slaby <[email protected]>
Signed-off-by: Borislav Petkov <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
---
arch/x86/kernel/head_32.S | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/x86/kernel/head_32.S b/arch/x86/kernel/head_32.S
index eb8656b..9b7acc9 100644
--- a/arch/x86/kernel/head_32.S
+++ b/arch/x86/kernel/head_32.S
@@ -23,6 +23,7 @@
#include <asm/cpufeatures.h>
#include <asm/percpu.h>
#include <asm/nops.h>
+#include <asm/nospec-branch.h>
#include <asm/bootparam.h>
#include <asm/export.h>
#include <asm/pgtable_32.h>

Subject: [tip: x86/urgent] x86/asm/32: Fix ANNOTATE_UNRET_SAFE use on 32-bit

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID: 56453410313828bf4eb70f3efbf7e2b29d6c647a
Gitweb: https://git.kernel.org/tip/56453410313828bf4eb70f3efbf7e2b29d6c647a
Author: Jiri Slaby <[email protected]>
AuthorDate: Wed, 13 Jul 2022 11:50:46 +02:00
Committer: Borislav Petkov <[email protected]>
CommitterDate: Wed, 13 Jul 2022 12:20:42 +02:00

x86/asm/32: Fix ANNOTATE_UNRET_SAFE use on 32-bit

The build on x86_32 currently fails after commit

9bb2ec608a20 (objtool: Update Retpoline validation)

with:

arch/x86/kernel/../../x86/xen/xen-head.S:35: Error: no such instruction: `annotate_unret_safe'

ANNOTATE_UNRET_SAFE is defined in nospec-branch.h. And head_32.S is
missing this include. Fix this.

Fixes: 9bb2ec608a20 (objtool: Update Retpoline validation)
Signed-off-by: Jiri Slaby <[email protected]>
Signed-off-by: Borislav Petkov <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
---
arch/x86/kernel/head_32.S | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/x86/kernel/head_32.S b/arch/x86/kernel/head_32.S
index eb8656b..9b7acc9 100644
--- a/arch/x86/kernel/head_32.S
+++ b/arch/x86/kernel/head_32.S
@@ -23,6 +23,7 @@
#include <asm/cpufeatures.h>
#include <asm/percpu.h>
#include <asm/nops.h>
+#include <asm/nospec-branch.h>
#include <asm/bootparam.h>
#include <asm/export.h>
#include <asm/pgtable_32.h>