Re: [regression] x86/signal/64: Fix SS handling for signals delivered to 64-bit programs breaks dosemu

From: Stas Sergeev
Date: Wed Aug 19 2015 - 05:36:03 EST


19.08.2015 01:47, Andy Lutomirski ÐÐÑÐÑ:
> On Mon, Aug 17, 2015 at 11:19 PM, Stas Sergeev <stsp@xxxxxxx> wrote:
>> 14.08.2015 04:37, Andy Lutomirski ÐÐÑÐÑ:
>>
>>> On Thu, Aug 13, 2015 at 6:32 PM, Stas Sergeev <stsp@xxxxxxx> wrote:
>>>>
>>>> 14.08.2015 04:21, Andy Lutomirski ÐÐÑÐÑ:
>>>>
>>>>> On Thu, Aug 13, 2015 at 5:50 PM, Stas Sergeev <stsp@xxxxxxx> wrote:
>>>>>>
>>>>>> 14.08.2015 03:27, Linus Torvalds ÐÐÑÐÑ:
>>>>>>>
>>>>>>> On Thu, Aug 13, 2015 at 5:17 PM, Stas Sergeev <stsp@xxxxxxx> wrote:
>>>>>>>>
>>>>>>>> For example because you can as well do:
>>>>>>>> prctl(ARCH_SET_SIGNAL_SS, 0)
>>>>>>>> which will mean "restore ss in sighandler to its current value",
>>>>>>>
>>>>>>> I really think a prctl() is the wrong thing to do.
>>>>>>>
>>>>>>> If you want a signal handler to save/restore segments, I think it
>>>>>>> should be a SA_xyz flag to sigaction() (the way we have SA_RESTART
>>>>>>
>>>>>> Yes, I was proposing the new sigaction() flag in this thread
>>>>>> already too. But at the end, prctl() looks better to me because
>>>>>> it allows to pass the TLS value to use when restoring FS.
>>>>>> The thing is that I am trying to find the similar treatment for
>>>>>> both the SS and FS problems. If you don't think they need a
>>>>>> similar treatment, then perhaps the Andy's patch is enough.
>>>>>>
>>>>>>> etc). And off by default because of the obvious compatibility issues.
>>>>>>
>>>>>> Of course.
>>>>>>
>>>>>> So, what we have right now (in the latest Andy's patch) is:
>>>>>> 1. lar heuristics
>>>>>> 2. new uc_flags flag
>>>>>>
>>>>>> What it solves: dosemu's regression.
>>>>>>
>>>>>> What prctl() can give:
>>>>>> - fix to dosemu's regression
>>>>>> - fix to the TLS problem in the future
>>>>>> - no hack and heuristics
>>>>>>
>>>>>> With SA_xyz you can only solve the SS problem, so it is
>>>>>> probably not any better than the uc_flags things coded
>>>>>> up by Andy.
>>>>>
>>>>> I'm leaning slightly toward LAR heuristic + SA_SAVE_SS.
>>>>
>>>> Stop right here, doesn't the SA_xyz allow to avoid the
>>>> lar heuristic? Why would you still need the lar heuristic then?
>>>> Just call it SA_RESTORE_SS instead of SA_SAVE_SS, and
>>>> the lar heuristic is gone.
>>>
>>> The LAR heuristic is about five lines of code, and it makes signal
>>> delivery more reliable.
>>
>> Why more reliable? In what case?
>>
>>> Sure, we could gate the "regs->ss =
>>> __USER_DS" line on a flag, but why?
>>
>> A few things I can think of why:
>> - nested signals (usual for dosemu)
>
> What's the issue with nested signals?
If nested signal is async and SS is from LDT just freed, then
the nested signal will silently change the SS value. So if you
are saving it somewhere, you'll save it by luck.
Now this is unlikely to happen, as the async signals in dosemu
are all blocked inside any sighandler. So the siglongjmp() case
is more expressive: if dosemu jumped via siglongjmp() and as such
unblocked the async signals, it will likely want to save its
registers before doing a new switch. Now, since SS is invalid
and will be therefore changed by a sighandler, what it will save
depends on a luck.
Not that dosemu uses siglongjmp() right now, but I am just asking
to please not implement the unreliable interfaces _if possible_.

>> - using siglongjmp() to return to dosemu (rather than to DOS code)
>> Both cases look very scare when using SS from just freed LDT entry.
>> How would you even justify and changelog the patch that adds a lar
>> heuristic code that no one uses or wants? Since SA_hyz flag allows
>> you to do without, why not to just keep things safe and simple?
>
> The LAR heuristic is just for compatibility.
>
> ISTM what DOSEMU should want (on new kernels, anyway) is the ability
> to save and restore SS just like any other register, which is what my
> patch did. The issue is that it broke old DOSEMU. I want to find a
> way to keep old DOSEMU working while making things work better for new
> code that's aware of new behavior. That means we want some way
> (opt-in or magically compatible with old DOSEMU) to get SS saved and
> restored.
>
> Incidentally, I tried implementing the sigaction flag approach. I
> think it's no good. When we return from a signal, there's no concept
> of sigaction -- it's just sigreturn. Sigreturn can't look up the
> sigaction flags -- what if the signal handler calls sigaction itself.
How about the SA_hyz flag that does the following:
- Saves SS into sigcontext
- Forces SS to USER_DS on signal delivery
- Sets the uc_flags flag for sigreturn() to take care of the rest.
You'll have both the control on every bit of action, and a simple
detection logic: if SA_hyz didn't set the uc flag - it didn't work.
You can even employ your lar heuristic here for the case when the
aforementioned SA_hyz is not set. But please, please not when it is
set! In fact, I wonder if you had in mind exactly that: using the
lar heuristic only if the SA_hyz is not set. If so - I misunderstood.
Just please don't add it when it is set.

> So we either need a per-task flag, a per-sighand flag, or a sigcontext
> flag indicating what we should do.
>
> (Yes, I suspect we really might want some way to get FS, GS, and their
> bases saved and restored, but I still think we should do that
> separately.)
In fact, I have already convinced myself that SA_hyz can take
care of both cases. :) Maybe you'll just need to extend the struct sigaction
to pass the TLS address, but this doesn't look absolutely impossible...
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/