Skip to contentSkip to conversation

Choose retpoline only when it is safe to use

PATCH zhenzhong.duan 2 participants 6 messages Feb 6, 2018

lore
Share & tools
lore
#0 lore raw
Quoted 16 lines · 2018年2月6日 17:20于 Andrew Cooper <[email protected]>写道:
Quoted 5 lines · Zhenzhong Duan
Quoted 4 lines · Andrew Cooper
Quoted 3 lines · Zhenzhong Duan

When ( ibrs && thunk == THUNK_DEFAULT && !retpoline_safe() ) is true, thunk is set to THUNK_JMP rather than THUNK_RETPOLINE.

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

When (!ibrs && thunk == THUNK_DEFAULT && !retpoline_safe() ) is true, we should do the same.

Why?  What improvement is this intended to give?

No improvement, I just feel if retpoline isn't safe, THUNK_JMP is better and safer.
Above first check is working that way.

Quoted 7 lines

If your only two choices are unsafe repoline or plain jumps, then unsafe repoline is far far far safer.

Its unsafe properties only kick in on an RSB underflow, and an attacker would have to do call-depths analysis of the running binary to identify which rets to attempt to poison.

Thanks for explaining.
So, for a retpoline safe processor, it just stop using RSB when it's empty to avoid underflow?

Another question:

if (opt_thunk == THUNK_DEFAULT && opt_ibrs == -1 &&
CONFIG_INDIRECT_THUNK && !cpu_has_lfence_dispatch && !retpoline_safe()) results in "thunk = THUNK_JMP" regardless of the value of "boot_cpu_has(X86_FEATURE_IBRSB)"

Any reason not considering retpoline here if (!boot_cpu_has(X86_FEATURE_IBRSB)?

thanks
zduan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#1 lore raw
Quoted 27 lines · zhenzhong.duan

2018年2月6日 17:20于 Andrew Cooper <[email protected]

Quoted 5 lines · <mailto:[email protected]>>写道:
Quoted 3 lines · Zhenzhong Duan
Quoted 2 lines · Andrew Cooper
Quoted 1 line · Zhenzhong Duan

When ( ibrs && thunk == THUNK_DEFAULT && !retpoline_safe() ) is

true,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

thunk is set to THUNK_JMP rather than THUNK_RETPOLINE.

When (!ibrs && thunk == THUNK_DEFAULT && !retpoline_safe() ) is

true,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

we should do the same.

<mailto:[email protected]>>

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Why?  What improvement is this intended to give?

No improvement, I just feel if retpoline isn't safe, THUNK_JMP is better and safer.
Above first check is working that way.

If your only two choices are unsafe repoline or plain jumps, then

unsafe

Quoted 2 lines

repoline is far far far safer.

Quoted 8 lines
Quoted 4 lines

Its unsafe properties only kick in on an RSB underflow, and an attacker would have to do call-depths analysis of the running binary to identify which rets to attempt to poison.

Thanks for explaining.
So, for a retpoline safe processor, it just stop using RSB when it's empty to avoid underflow?

The qualification of whether a processor is retpoline-safe or not is whether an RSB underflow causes a BTB lookup (unsafe) or not (safe).

RSB underflows will always occur; you cannot get rid of them, but in most cases (i.e. pre-Skylake) they don't fall back to using a prediction source which can be poisoned by an attacker.

Quoted 7 lines

Another question:

if (opt_thunk == THUNK_DEFAULT && opt_ibrs == -1 && CONFIG_INDIRECT_THUNK && !cpu_has_lfence_dispatch && !retpoline_safe()) results in "thunk = THUNK_JMP" regardless of the value of "boot_cpu_has(X86_FEATURE_IBRSB)"

Your formatting is hard to follow, but cpu_has_lfence_dispatch can only be false when virtualised under an SP2-unaware hypervisor on AMD hardware, at which point retpoline_safe() will return true.  Also, AMD don't have IBRS in microcode and their future plans don't appear to involve using that particular CPUID bit.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 33 lines · Andrew Cooper
Quoted 32 lines · zhenzhong.duan
Quoted 5 lines · 2018年2月6日 17:20于 Andrew Cooper <[email protected]…
Quoted 3 lines · Zhenzhong Duan
Quoted 2 lines · Andrew Cooper
Quoted 1 line · Zhenzhong Duan

When ( ibrs && thunk == THUNK_DEFAULT && !retpoline_safe() ) is

true,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

thunk is set to THUNK_JMP rather than THUNK_RETPOLINE.

When (!ibrs && thunk == THUNK_DEFAULT && !retpoline_safe() ) is

true,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

we should do the same.

<mailto:[email protected]>>

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Why?  What improvement is this intended to give?

No improvement, I just feel if retpoline isn't safe, THUNK_JMP is better and safer.
Above first check is working that way.

If your only two choices are unsafe repoline or plain jumps, then

unsafe

Quoted 3 lines

repoline is far far far safer.

Its unsafe properties only kick in on an RSB underflow, and an

attacker

Quoted 1 line

would have to do call-depths analysis of the running binary to

identify

Quoted 2 lines

which rets to attempt to poison.

Quoted 7 lines
Quoted 3 lines

Thanks for explaining.
So, for a retpoline safe processor, it just stop using RSB when it's empty to avoid underflow?

The qualification of whether a processor is retpoline-safe or not is whether an RSB underflow causes a BTB lookup (unsafe) or not (safe).

RSB underflows will always occur; you cannot get rid of them, but in most cases (i.e. pre-Skylake) they don't fall back to using a prediction source which can be poisoned by an attacker.

Understood.

Quoted 9 lines
Quoted 7 lines

Another question:

if (opt_thunk == THUNK_DEFAULT && opt_ibrs == -1 &&
CONFIG_INDIRECT_THUNK && !cpu_has_lfence_dispatch && !retpoline_safe()) results in "thunk = THUNK_JMP" regardless of the value of "boot_cpu_has(X86_FEATURE_IBRSB)"

Your formatting is hard to follow,

Sorry, sent from mobile.

Quoted 1 line

but cpu_has_lfence_dispatch can only be false when virtualised under an SP2-unaware hypervisor on AMD hardware, at which point retpoline_safe() will return true.  Also, AMD don't have IBRS in microcode and their future plans don't appear to involve using that particular CPUID bit.

That does make sense for AMD. But what about Intel hardware which has (!cpu_has_lfence_dispatch && !retpoline_safe() && !boot_cpu_has(X86_FEATURE_IBRSB)), should we  prefer THUNK_RETPOLINE?

Signature · 2 lines
thanks
zduan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 58 lines · Zhenzhong Duan
Quoted 39 lines · Andrew Cooper
Quoted 28 lines · zhenzhong.duan

2018年2月6日 17:20于 Andrew Cooper <[email protected]

Quoted 5 lines · <mailto:[email protected]>>写道:
Quoted 3 lines · Zhenzhong Duan
Quoted 2 lines · Andrew Cooper
Quoted 1 line · Zhenzhong Duan

When ( ibrs && thunk == THUNK_DEFAULT && !retpoline_safe() )

is true,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

thunk is set to THUNK_JMP rather than THUNK_RETPOLINE.

When (!ibrs && thunk == THUNK_DEFAULT && !retpoline_safe() )

is true,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

we should do the same.

<mailto:[email protected]>>

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Why?  What improvement is this intended to give?

No improvement, I just feel if retpoline isn't safe, THUNK_JMP is better and safer.
Above first check is working that way.

If your only two choices are unsafe repoline or plain jumps, then

unsafe

Quoted 3 lines

repoline is far far far safer.

Its unsafe properties only kick in on an RSB underflow, and an

Quoted 9 lines

attacker

Quoted 1 line

would have to do call-depths analysis of the running binary to

identify

Quoted 2 lines

which rets to attempt to poison.

Thanks for explaining.
So, for a retpoline safe processor, it just stop using RSB when it's empty to avoid underflow?

Quoted 6 lines

The qualification of whether a processor is retpoline-safe or not is whether an RSB underflow causes a BTB lookup (unsafe) or not (safe).

RSB underflows will always occur; you cannot get rid of them, but in most cases (i.e. pre-Skylake) they don't fall back to using a prediction source which can be poisoned by an attacker.

Understood.

Quoted 10 lines
Quoted 8 lines

Another question:

if (opt_thunk == THUNK_DEFAULT && opt_ibrs == -1 && CONFIG_INDIRECT_THUNK && !cpu_has_lfence_dispatch && !retpoline_safe()) results in "thunk = THUNK_JMP" regardless of the value of "boot_cpu_has(X86_FEATURE_IBRSB)"

Your formatting is hard to follow,

Sorry, sent from mobile.

Quoted 8 lines
Quoted 5 lines

but cpu_has_lfence_dispatch can only be false when virtualised under an SP2-unaware hypervisor on AMD hardware, at which point retpoline_safe() will return true.  Also, AMD don't have IBRS in microcode and their future plans don't appear to involve using that particular CPUID bit.

That does make sense for AMD. But what about Intel hardware which has (!cpu_has_lfence_dispatch && !retpoline_safe() && !boot_cpu_has(X86_FEATURE_IBRSB)), should we  prefer THUNK_RETPOLINE?

Yes, and we do end up choosing RETPOLINE in those circumstances, as we hit the default case you originally tried to patch.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 51 lines · Andrew Cooper
Quoted 50 lines · Zhenzhong Duan
Quoted 33 lines · Andrew Cooper
Quoted 32 lines · zhenzhong.duan
Quoted 5 lines · 2018年2月6日 17:20于 Andrew Cooper <[email protected]…
Quoted 3 lines · Zhenzhong Duan
Quoted 2 lines · Andrew Cooper
Quoted 1 line · Zhenzhong Duan

When ( ibrs && thunk == THUNK_DEFAULT && !retpoline_safe() )

is true,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

thunk is set to THUNK_JMP rather than THUNK_RETPOLINE.

When (!ibrs && thunk == THUNK_DEFAULT && !retpoline_safe() )

is true,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

we should do the same.

<mailto:[email protected]>>

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Why?  What improvement is this intended to give?

No improvement, I just feel if retpoline isn't safe, THUNK_JMP is better and safer.
Above first check is working that way.

If your only two choices are unsafe repoline or plain jumps, then

unsafe

Quoted 3 lines

repoline is far far far safer.

Its unsafe properties only kick in on an RSB underflow, and an

attacker

Quoted 1 line

would have to do call-depths analysis of the running binary to

identify

Quoted 2 lines

which rets to attempt to poison.

Quoted 7 lines
Quoted 3 lines

Thanks for explaining.
So, for a retpoline safe processor, it just stop using RSB when it's empty to avoid underflow?

The qualification of whether a processor is retpoline-safe or not is whether an RSB underflow causes a BTB lookup (unsafe) or not (safe).

RSB underflows will always occur; you cannot get rid of them, but in most cases (i.e. pre-Skylake) they don't fall back to using a prediction source which can be poisoned by an attacker.

Understood.

Quoted 8 lines
Quoted 7 lines

Another question:

if (opt_thunk == THUNK_DEFAULT && opt_ibrs == -1 &&
CONFIG_INDIRECT_THUNK && !cpu_has_lfence_dispatch && !retpoline_safe()) results in "thunk = THUNK_JMP" regardless of the value of "boot_cpu_has(X86_FEATURE_IBRSB)"

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Your formatting is hard to follow,

Sorry, sent from mobile.

Quoted 1 line

but cpu_has_lfence_dispatch can only be false when virtualised under an SP2-unaware hypervisor on AMD hardware, at which point retpoline_safe() will return true.  Also, AMD don't have IBRS in microcode and their future plans don't appear to involve using that particular CPUID bit.

That does make sense for AMD. But what about Intel hardware which has (!cpu_has_lfence_dispatch && !retpoline_safe() && !boot_cpu_has(X86_FEATURE_IBRSB)), should we  prefer THUNK_RETPOLINE?

Yes, and we do end up choosing RETPOLINE in those circumstances, as we hit the default case you originally tried to patch.

If that's the case, I think we need another patch like below.

@@ -223,7 +223,7 @@ void __init init_speculation_mitigations(void) */
             else if ( retpoline_safe() )
                 thunk = THUNK_RETPOLINE;
-            else
+            else if ( boot_cpu_has(X86_FEATURE_IBRSB) ) ibrs = true;
         }
         /* Without compiler thunk support, use IBRS if available. */

As ibrs was set to true when !boot_cpu_has(X86_FEATURE_IBRSB) with current code, then thunk was set to THUNK_JMP rather than THUNK_RETPOLINE.

Signature · 2 lines
thanks
zduan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 85 lines · Zhenzhong Duan
Quoted 65 lines · Andrew Cooper
Quoted 64 lines · Zhenzhong Duan
Quoted 39 lines · Andrew Cooper
Quoted 28 lines · zhenzhong.duan

2018年2月6日 17:20于 Andrew Cooper <[email protected]

Quoted 5 lines · <mailto:[email protected]>>写道:
Quoted 3 lines · Zhenzhong Duan
Quoted 2 lines · Andrew Cooper
Quoted 1 line · Zhenzhong Duan

When ( ibrs && thunk == THUNK_DEFAULT && !retpoline_safe() )

is true,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

thunk is set to THUNK_JMP rather than THUNK_RETPOLINE.

When (!ibrs && thunk == THUNK_DEFAULT && !retpoline_safe() )

is true,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

we should do the same.

<mailto:[email protected]>>

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Why?  What improvement is this intended to give?

No improvement, I just feel if retpoline isn't safe, THUNK_JMP is better and safer.
Above first check is working that way.

If your only two choices are unsafe repoline or plain jumps,

then unsafe

Quoted 3 lines

repoline is far far far safer.

Its unsafe properties only kick in on an RSB underflow, and an

Quoted 9 lines

attacker

Quoted 1 line

would have to do call-depths analysis of the running binary to

identify

Quoted 2 lines

which rets to attempt to poison.

Thanks for explaining.
So, for a retpoline safe processor, it just stop using RSB when it's empty to avoid underflow?

Quoted 6 lines

The qualification of whether a processor is retpoline-safe or not is whether an RSB underflow causes a BTB lookup (unsafe) or not (safe).

RSB underflows will always occur; you cannot get rid of them, but in most cases (i.e. pre-Skylake) they don't fall back to using a prediction source which can be poisoned by an attacker.

Understood.

Quoted 11 lines
Quoted 9 lines

Another question:

if (opt_thunk == THUNK_DEFAULT && opt_ibrs == -1 && CONFIG_INDIRECT_THUNK && !cpu_has_lfence_dispatch && !retpoline_safe())
results in "thunk = THUNK_JMP" regardless of the value of "boot_cpu_has(X86_FEATURE_IBRSB)"

Your formatting is hard to follow,

Sorry, sent from mobile.

Quoted 5 lines

but cpu_has_lfence_dispatch can only be false when virtualised under an SP2-unaware hypervisor on AMD hardware, at which point retpoline_safe() will return true.  Also, AMD don't have IBRS in microcode and their future plans don't appear to involve using that particular CPUID bit.

Quoted 6 lines
Quoted 3 lines

That does make sense for AMD. But what about Intel hardware which has (!cpu_has_lfence_dispatch && !retpoline_safe() && !boot_cpu_has(X86_FEATURE_IBRSB)), should we  prefer THUNK_RETPOLINE?

Yes, and we do end up choosing RETPOLINE in those circumstances, as we hit the default case you originally tried to patch.

If that's the case, I think we need another patch like below.

(quoted diff) +1 -1
@@ -223,7 +223,7 @@ void __init init_speculation_mitigations(void)
               */
              else if ( retpoline_safe() )
                  thunk = THUNK_RETPOLINE;
-            else
+            else if ( boot_cpu_has(X86_FEATURE_IBRSB) )
                  ibrs = true;
          }
          /* Without compiler thunk support, use IBRS if available. */
Quoted 2 lines

As ibrs was set to true when !boot_cpu_has(X86_FEATURE_IBRSB) with current code, then thunk was set to THUNK_JMP rather than THUNK_RETPOLINE.

You are correct.  This is a side effect of an optimisation which was requested during review.  I'll draft a patch.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel