Re: [PATCH] selftests/x86: Add a selftest for SYSRET to noncanonical addresses

From: Andy Lutomirski
Date: Mon Dec 19 2016 - 18:38:10 EST


On Mon, Dec 19, 2016 at 3:30 PM, Kirill A. Shutemov
<kirill@xxxxxxxxxxxxx> wrote:
> On Mon, Dec 19, 2016 at 11:12:42AM -0800, Andy Lutomirski wrote:
>> SYSRET to a noncanonical address will blow up on Intel CPUs. Linux
>> needs to prevent this from happening in two major cases, and the
>> criteria will become more complicated when support for larger virtual
>> address spaces is added.
>>
>> A fast-path SYSCALL will fallthrough to the following instruction
>> using SYSRET without any particular checking. To prevent fallthrough
>> to a noncanonical address, Linux prevents the highest canonical page
>> from being mapped. This test case checks a variety of possible maximum
>> addresses to make sure that either we can't map code there or that
>> SYSCALL fallthrough works.
>>
>> A slow-path system call can return anywhere. Linux needs to make sure
>> that, if the return address is non-canonical, it won't use SYSRET.
>> This test cases causes sigreturn() to return to a variety of addresses
>> (with RCX == RIP) and makes sure that nothing explodes.
>>
>> Some of this code comes from Kirill Shutemov.
>>
>> Cc: "Kirill A. Shutemov" <kirill.shutemov@xxxxxxxxxxxxxxx>
>> Signed-off-by: Andy Lutomirski <luto@xxxxxxxxxx>
>
> Thanks a lot.
>
> That's what I've got with 5-level paging:

[...]

Looks good.

>> + err(1, "mremap to 0x%p", new_address);
>
> "0x" is redundant for %p.
>
>> + } else {
>> + printf("[OK]\tmremap to 0x%p failed\n", new_address);
>
> Ditto.

Will fix.

>> + test_sigreturn_to((1UL<<i));
>
> Redundant parenthesis?

Indeed.

>
>> +
>> + clearhandler(SIGUSR1);
>> +
>> + sethandler(SIGSEGV, sigsegv_for_fallthrough, 0);
>> +
>> + /* This should execute on all kernels. */
>> + test_syscall_fallthrough_to((1UL << 47) - PAGE_SIZE);
>> +
>> + /* Make sure that we didn't screw up the mremap logic. */
>> + test_syscall_fallthrough_to((1UL << 47) - 2*PAGE_SIZE);
>> +
>> + /* These are the interesting cases. */
>> + for (int i = 47; i < 64; i++)
>> + test_syscall_fallthrough_to((1UL<<i));
>
> Ditto.
>
> Also, "(1UL << i) - PAGE_SIZE" is interesting too. TASK_SIZE for 5-level
> paging would be (1UL << 56) - PAGE_SIZE. I would be better to catch both
> corner cases.

There's not much scope for error, though -- (1UL << 56) - PAGE_SIZE
isn't really any different from any other address. I all add it, but
I'm not sure I see any way that the kernel could plausible get it
wrong. I guess it's comforting to see the boundary, though.

--Andy