All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jan Beulich" <JBeulich@suse.com>
To: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: Juergen Gross <jgross@suse.com>,
	xen-devel <xen-devel@lists.xenproject.org>,
	Wei Liu <wei.liu2@citrix.com>,
	nmanthey@amazon.de, Roger Pau Monne <roger.pau@citrix.com>
Subject: Re: [PATCH 1/4] x86emul: avoid speculative out of bounds accesses
Date: Thu, 31 Jan 2019 09:45:37 -0700	[thread overview]
Message-ID: <5C5326310200007800212E5A@prv1-mh.provo.novell.com> (raw)
In-Reply-To: <e5779077-a6fd-9c63-017a-9dcc436e42f1@citrix.com>

>>> On 31.01.19 at 17:14, <andrew.cooper3@citrix.com> wrote:
> On 31/01/2019 15:50, Jan Beulich wrote:
>>>>> On 31.01.19 at 15:54, <andrew.cooper3@citrix.com> wrote:
>>> On 31/01/2019 14:25, Jan Beulich wrote:
>>>> --- a/xen/arch/x86/x86_emulate/x86_emulate.c
>>>> +++ b/xen/arch/x86/x86_emulate/x86_emulate.c
>>>> @@ -2207,10 +2207,7 @@ static void *_decode_gpr(
>>>>  
>>>>      ASSERT(modrm_reg < ARRAY_SIZE(byte_reg_offsets));
>>>>  
>>>> -    /* For safety in release builds.  Debug builds will hit the ASSERT() */
>>>> -    modrm_reg &= ARRAY_SIZE(byte_reg_offsets) - 1;
>>>> -
>>>> -    return (void *)regs + byte_reg_offsets[modrm_reg];
>>>> +    return (void *)regs + array_access_nospec(byte_reg_offsets, modrm_reg);
>>>>  }
>>> Actually, the &= here wasn't by accident.  When the array size is an
>>> power of two and known to the compiler, it is a rather lower overhead
>>> alternative to array_access_nospec(), as it avoids the cmp/sbb dance in
>>> the asm volatile statement.
>>>
>>> I wonder if there is a sensible way cope with this in
>>> array_access_nospec().  Perhaps something like:
>>>
>>> #define array_access_nospec(array, index)
>>> ({
>>>     size_t _s = ARRAY_SIZE(array);
>>>
>>>     if ( !(_s & (_s - 1)) )
>>>     {
>>>         typeof(index) _i = index & (_s - 1);
>>>         OPTIMIZER_HIDE_VAR(_i);
>>>         (array)[_i];
>>>     }
>>>     else
>>>         (array)[array_index_nospec(index, ARRAY_SIZE(array))];
>>> })
>>>
>>> As _s is known at compile time, only one half of the if condition will
>>> be emitted by the compiler.
>> Except that this won't work as an lvalue anymore, yet we want
>> to use it as such in some cases. I can't seem to immediately think
>> of a way to overcome this.
> 
> Does the lvalue use result in safe asm?

Hmm, I'm a little puzzled - why would it not? Could you perhaps be
a little more specific about what worries you in that case?

>  Irrespective, this macro can
> probably be expressed as a ternary operation and retain its lvalue-ness,
> albeit at the expense of readability.

Oh, you're right. Not even overly difficult. The readability aspect
could perhaps be addressed by using another helper macro. But
for the purposes here I'll go the comment adjustment route. We
can always add this improvement to the macro later on.

Jan



_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  reply	other threads:[~2019-01-31 16:45 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-31 14:07 [PATCH 0/4] x86: further L1TF / XSA-289 guards Jan Beulich
2019-01-31 14:25 ` [PATCH 1/4] x86emul: avoid speculative out of bounds accesses Jan Beulich
2019-01-31 14:54   ` Andrew Cooper
2019-01-31 15:50     ` Jan Beulich
2019-01-31 16:14       ` Andrew Cooper
2019-01-31 16:45         ` Jan Beulich [this message]
2019-02-07 11:42   ` [PATCH v2] " Jan Beulich
2019-07-04 13:19     ` [Xen-devel] " Andrew Cooper
2019-04-03  8:46   ` Ping: " Jan Beulich
2019-01-31 14:26 ` [PATCH 2/4] x86/vMSI: " Jan Beulich
2019-07-04 13:27   ` [Xen-devel] " Andrew Cooper
2019-01-31 14:26 ` [PATCH 3/4] x86/vPIC: " Jan Beulich
2019-07-04 13:35   ` [Xen-devel] " Andrew Cooper
2019-01-31 14:27 ` [PATCH 4/4] x86/vLAPIC: " Jan Beulich
2019-07-04 13:44   ` [Xen-devel] " Andrew Cooper
2019-07-04 13:57     ` Jan Beulich
     [not found] ` <5C53012902000000001030F5@prv1-mh.provo.novell.com>
     [not found]   ` <5C53012902000078002328D1@prv1-mh.provo.novell.com>
2019-05-27  9:24     ` Ping: [PATCH 0/4] x86: further L1TF / XSA-289 guards Jan Beulich
2019-05-27  9:24       ` [Xen-devel] " Jan Beulich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5C5326310200007800212E5A@prv1-mh.provo.novell.com \
    --to=jbeulich@suse.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=jgross@suse.com \
    --cc=nmanthey@amazon.de \
    --cc=roger.pau@citrix.com \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.