Re: [PATCH v10 01/18] x86/ptrace: Prevent ptrace from clearing the FS/GS selector

From: Andy Lutomirski
Date: Sat Apr 25 2020 - 18:58:18 EST


On Thu, Apr 23, 2020 at 4:22 PM Sasha Levin <sashal@xxxxxxxxxx> wrote:
>
> From: "Chang S. Bae" <chang.seok.bae@xxxxxxxxx>
>
> When a ptracer writes a ptracee's FS/GS base with a different value, the
> selector is also cleared. This behavior is not correct as the selector
> should be preserved.
>
> Update only the base value and leave the selector intact. To simplify the
> code further remove the conditional checking for the same value as this
> code is not performance-critical.
>
> The only recognizable downside of this change is when the selector is
> already nonzero on write. The base will be reloaded according to the
> selector. But the case is highly unexpected in real usages.

After spending a while reading this patch, I think it's probably okay,
but this ptrace stuff is utter garbage. The changelog should explain
why common cases work with the current code, what you think the point
(if any) of the condition you're removing is, and why it's okay to
make this change.

Certainly the current changelog is wrong. You say "The base will be
reloaded according to the selector". The code you're changing calls
x86_fs/gsbase_write_task(), which is, effectively:

task->thread.fsbase = fsbase;

This doesn't reload anything.

Maybe what you're trying to say is "with this patch applied, as is or
with FSGSBASE disabled, if the tracee has FS != 0 and a tracer
modifies only fs_base, then the change won't stick."

--Andy