On 07/10/2018 04:53 AM, Richard Earnshaw (lists) wrote:
> On 10/07/18 11:10, Richard Biener wrote:
>> On Tue, Jul 10, 2018 at 10:39 AM Richard Earnshaw (lists)
>> <richard.earns...@arm.com> wrote:
>>>
>>> On 10/07/18 08:19, Richard Biener wrote:
>>>> On Mon, Jul 9, 2018 at 6:39 PM Richard Earnshaw
>>>> <richard.earns...@arm.com> wrote:
>>>>>
>>>>>
>>>>> The patches I posted earlier this year for mitigating against
>>>>> CVE-2017-5753 (Spectre variant 1) attracted some useful feedback, from
>>>>> which it became obvious that a rethink was needed.  This mail, and the
>>>>> following patches attempt to address that feedback and present a new
>>>>> approach to mitigating against this form of attack surface.
>>>>>
>>>>> There were two major issues with the original approach:
>>>>>
>>>>> - The speculation bounds were too tightly constrained - essentially
>>>>>   they had to represent and upper and lower bound on a pointer, or a
>>>>>   pointer offset.
>>>>> - The speculation constraints could only cover the immediately preceding
>>>>>   branch, which often did not fit well with the structure of the existing
>>>>>   code.
>>>>>
>>>>> An additional criticism was that the shape of the intrinsic did not
>>>>> fit particularly well with systems that used a single speculation
>>>>> barrier that essentially had to wait until all preceding speculation
>>>>> had to be resolved.
>>>>>
>>>>> To address all of the above, these patches adopt a new approach, based
>>>>> in part on a posting by Chandler Carruth to the LLVM developers list
>>>>> (https://lists.llvm.org/pipermail/llvm-dev/2018-March/122085.html),
>>>>> but which we have extended to deal with inter-function speculation.
>>>>> The patches divide the problem into two halves.
>>>>>
>>>>> The first half is some target-specific code to track the speculation
>>>>> condition through the generated code to provide an internal variable
>>>>> which can tell us whether or not the CPU's control flow speculation
>>>>> matches the data flow calculations.  The idea is that the internal
>>>>> variable starts with the value TRUE and if the CPU's control flow
>>>>> speculation ever causes a jump to the wrong block of code the variable
>>>>> becomes false until such time as the incorrect control flow
>>>>> speculation gets unwound.
>>>>>
>>>>> The second half is that a new intrinsic function is introduced that is
>>>>> much simpler than we had before.  The basic version of the intrinsic
>>>>> is now simply:
>>>>>
>>>>>       T var = __builtin_speculation_safe_value (T unsafe_var);
>>>>>
>>>>> Full details of the syntax can be found in the documentation patch, in
>>>>> patch 1.  In summary, when not speculating the intrinsic returns
>>>>> unsafe_var; when speculating then if it can be shown that the
>>>>> speculative flow has diverged from the intended control flow then zero
>>>>> is returned.  An optional second argument can be used to return an
>>>>> alternative value to zero.  The builtin may cause execution to pause
>>>>> until the speculation state is resolved.
>>>>
>>>> So a trivial target implementation would be to emit a barrier and then
>>>> it would always return unsafe_var and never zero.  What I don't understand
>>>> fully is what users should do here, thus what the value of ever returning
>>>> "unsafe" is.  Also I wonder why the API is forcing you to single-out a
>>>> special value instead of doing
>>>>
>>>>  bool safe = __builtin_speculation_safe_value_p (T unsafe_value);
>>>>  if (!safe)
>>>>    /* what now? */
>>>>
>>>> I'm only guessing that the correct way to handle "unsafe" is basically
>>>>
>>>>  while (__builtin_speculation_safe_value (val) == 0)
>>>>     ;
>>>>
>>>> use val, it's now safe
>>>
>>> No, a safe version of val is returned, not a bool telling you it is now
>>> safe to use the original.
>>
>> OK, so making the old value dead is required to preserve the desired
>> dataflow.
>>
>> But how should I use the special value that signaled "failure"?
>>
>> Obviously the user isn't supposed to simply replace 'val' with
>>
>>  val = __builtin_speculation_safe_value (val);
>>
>> to make it speculation-proof.  So - how should the user _use_ this
>> builtin?  The docs do not say anything about this but says the
>> very confusing
>>
>> +The function may use target-dependent speculation tracking state to cause
>> +@var{failval} to be returned when it is known that speculative
>> +execution has incorrectly predicted a conditional branch operation.
>>
>> because speculation is about executing instructions as if they were
>> supposed to be executed.  Once it is known the prediciton was wrong
>> no more "wrong" instructions will be executed but a previously
>> speculated instruction cannot know it was "falsely" speculated.
>>
>> Does the above try to say that the function may return failval if the
>> instruction is currently executed speculatively instead?  That would
>> make sense to me.  And return failval independent of if the speculation
>> later turns out to be correct or not.
>>
>>>  You must use the sanitized version in future,
>>> not the unprotected version.
>>>
>>>
>>> So the usage is going to be more like:
>>>
>>> val = __builtin_speculation_safe_value (val);  // Overwrite val with a
>>> sanitized version.
>>>
>>> You have to use the cleaned up version, the unclean version is still
>>> vulnerable to incorrect speculation.
>>
>> But then where does failval come into play?  The above cannot be correct
>> if failval matters.
> 
> failval only comes into play if the CPU is speculating incorrectly.
> It's just a safe value that some CPUs might use until such time as the
> speculation gets unwound.  In most cases it can be zero.  Perhaps a more
> concrete example would be something like.
The value really only matters in the sense that if the processor is
speculating and using that value that the uses don't lead information leaks.

That's why 0 is so useful here as safeval.  Reading/writing *0
speculatively isn't going to populate the cache in a way that is useful
to the attacker.

Jeff

Reply via email to