Re: siginfo pid not populated from ptrace?

From: Eric W. Biederman
Date: Thu Dec 06 2018 - 10:00:19 EST


Kees Cook <keescook@xxxxxxxxxxxx> writes:

> On Sat, Dec 1, 2018 at 7:04 AM Eric W. Biederman <ebiederm@xxxxxxxxxxxx> wrote:
>>
>> Kees Cook <keescook@xxxxxxxxxxxx> writes:
>>
>> > On Tue, Nov 27, 2018 at 8:44 PM Eric W. Biederman <ebiederm@xxxxxxxxxxxx> wrote:
>> >>
>> >> Kees Cook <keescook@xxxxxxxxxxxx> writes:
>> >>
>> >> > On Tue, Nov 27, 2018 at 4:38 PM, Kees Cook <keescook@xxxxxxxxxxxx> wrote:
>> >> >> On Tue, Nov 27, 2018 at 3:21 PM, Tycho Andersen <tycho@xxxxxxxx> wrote:
>> >> >>> On Mon, Nov 12, 2018 at 12:24:43PM -0700, Tycho Andersen wrote:
>> >> >>>> On Mon, Nov 12, 2018 at 11:55:38AM -0700, Tycho Andersen wrote:
>> >> >>>> > I haven't manage to reproduce it on stock v4.20-rc2, unfortunately.
>> >> >>>>
>> >> >>>> Ok, now I have,
>> >> >>>>
>> >> >>>> seccomp_bpf.c:2736:global.syscall_restart:Expected getpid() (1493) == info._sifields._kill.si_pid (0)
>> >> >>>> global.syscall_restart: Test failed at step #22
>> >> >>>
>> >> >>> Seems like this is still happening on v4.20-rc4,
>> >> >>>
>> >> >>> [ RUN ] global.syscall_restart
>> >> >>> seccomp_bpf.c:2736:global.syscall_restart:Expected getpid() (1901) == info._sifields._kill.si_pid (0)
>> >> >>> global.syscall_restart: Test failed at step #22
>> >> >>
>> >> >> This fails every time for me -- is it still racey for you?
>> >> >>
>> >> >> I'm attempting a bisect, hoping it doesn't _become_ racey for me. ;)
>> >> >
>> >> > This bisects to here for me:
>> >> >
>> >> > commit f149b31557446aff9ca96d4be7e39cc266f6e7cc
>> >> > Author: Eric W. Biederman <ebiederm@xxxxxxxxxxxx>
>> >> > Date: Mon Sep 3 09:50:36 2018 +0200
>> >> >
>> >> > signal: Never allocate siginfo for SIGKILL or SIGSTOP
>> >> >
>> >> > The SIGKILL and SIGSTOP signals are never delivered to userspace so
>> >> > queued siginfo for these signals can never be observed. Therefore
>> >> > remove the chance of failure by never even attempting to allocate
>> >> > siginfo in those cases.
>> >> >
>> >> > Reviewed-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
>> >> > Signed-off-by: "Eric W. Biederman" <ebiederm@xxxxxxxxxxxx>
>> >> >
>> >> > They are certainly visible via seccomp ;)
>> >>
>> >> Well SIGSTOP is visible via PTRACE_GETSIGINFO.
>> >>
>> >> I see what is happening now. Since we don't have queued siginfo
>> >> we generate some as:
>> >> /*
>> >> * Ok, it wasn't in the queue. This must be
>> >> * a fast-pathed signal or we must have been
>> >> * out of queue space. So zero out the info.
>> >> */
>> >> clear_siginfo(info);
>> >> info->si_signo = sig;
>> >> info->si_errno = 0;
>> >> info->si_code = SI_USER;
>> >> info->si_pid = 0;
>> >> info->si_uid = 0;
>> >>
>> >> Which allows last_signfo to be set,
>> >> so despite not really having any siginfo PTRACE_GET_SIGINFO
>> >> has something to return so does not return -EINVAL.
>> >>
>> >> Reconstructing my context that was part of removing SEND_SIG_FORCED
>> >> so this looks like it will take a little more than a revert to fix
>> >> this.
>> >>
>> >> This is definitely a change that is visible to user space. The logic in
>> >> my patch was definitely wrong with respect to SIGSTOP and
>> >> PTRACE_GETSIGINFO. Is there something in userspace that actually cares?
>> >> AKA is the idiom that the test seccomp_bpf.c is using something that
>> >> non-test code does?
>> >
>> > I think this would be needed by any ptracer that handled multiple
>> > threads. It needs to figure out which pid stopped. I think it's worth
>> > fixing, yes.
>> >
>> >> The change below should restore the old behavior. I am just wondering
>> >> if this is something we want to do. siginfo is allocated with
>> >> GFP_ATOMIC so if your machine is under memory pressure there is a real
>> >> chance the allocation can fail. Which would cause whatever is breaking
>> >> now to break less deterministically then.
>> >
>> > I think memory pressure that would block a 128 byte GFP_ATOMIC
>> > allocation would mean the system was about to seriously fall over.
>> > Given the user-facing behavior change and that an existing test was
>> > already checking for this means we need to fix it.
>>
>> That sounds good but it is all rubbish.
>> A) It doesn't matter for tracing multiple processes because ptrace
>> only works on a single signal at a time. AKA if by the time you
>> are calling PTRACE_GETSIGINFO you already know which process you are
>> working against.
>> B) Not every signal includes si_pid so even if you didn't know who you
>> were talking to this would be an issue.
>> C) For a non-rt signal we only every try and queue up a signal signal.
>> We don't even attempt to queue siginfo otherwise.
>
> Fair enough, all true.
>
>> So what is the real world use case?
>
> A quick search didn't show anything I could find (looking for
> combinations of PTRACE_GETSIGINFO and si_pid use around a SIGSTOP).
> That doesn't mean it doesn't exist, though. The reason seccomp's
> selftests are so extensive is because we've had some very
> corner-case[1] bugs in real-world software.

I support that.

>> The most useful I can think of is the whole check if this tracer was the
>> one who sent SIGSTOP. But that is fundamentally unreliable because we
>> only queue a single signal anyway. We must to that to preserve
>> compatibility for non-rt signals as otherwise there are cases we could
>> queue the same signal twice. So with two simultaneous SIGSTOPs it is a
>> race condition which siginfo is made available.
>
> Understood. However, the point of the seccomp test is for making sure
> that the ordering of the controlled test gets us the right pattern of
> events across a syscall interruption when seccomp is filtering. This
> is about making sure nothing around the restart changes since this
> could impact seccomp filters in the wild.

That is fair. Does the test fundamentally need to check the sender of
SIGSTOP to know everything you are sending is working for seccomp?

>> Right now we are on the edge of letting test cases for debug
>> infrastructure prevent obvious optimizations/improvements to the code.
>> So if we can find something other than our seccomp_bpf.c test case that
>> fails I am happy to go with the change I posted. Otherwise I think we
>> just need to fix seccomp_bpf.c.
>
> I wouldn't call this "debug infrastructure": they are behavioral test
> cases for seccomp, syscall, and ptrace interaction, and even this one
> has found bugs (see the "FIXME" note a few lines down, that remains
> unsolved[2] in arm32).

I would call ptrace "debug infrastructure".

My concern is that there were a number of signal senders that were
setting SEND_SIG_FORCED. They were setting SEND_SIG_FORCED to cause
the signal delivery process to work differently. Ensuring the pid
namespace init was killed, to not allocate siginfo, etc.

This was very fragile code internal to the kernel and required much
to much knowledge from the signal senders of the signals. So I updated
the code to apply the transformations whenever reasonably possible so
senders of those signals would not need to think about it. Reducing
bugs overall and making the kernel more consistent.

The logic was that the siginfo from SIGSTOP and SIGKILL could not be
observed. Except for PTRACE_GETSIGINFO for SIGSTOP is actually true.

> If this actually doesn't count as breaking userspace, then okay, sure,
> I'll drop the sanity check in the seccomp selftest. But it feels wrong
> to me.

With just a test showing this, this doesn't yet count as breaking
userspace. At the same time this is on a funny edge.

If something that is not a test truly depends on this it would
definitely count as breaking userspace. Equally if there is a case
where it can be shown that userspace can reasonably depend on this
behavior to achieve something userspace needs to do. Then sure.

We have in the past had ptrace users that weren't just about debugging
so I don't know that it is fair to just dismiss it as debugging
infrastructure.

Right now I am trying to understand why seccomp bpf cares. So that I
can figure out if I need to do something.

The optimization of not allocating siginfo for SIGSTOP is very
reasonable except when the process is being ptraced. It even has the
possibility of making SIGSTOP working better for the common case. So I
am reluctant to give up a kernel improvement until I understand what
reasonable case in userspace I am breaking.

What I am seeing is a case where for a multi-threaded process the
ptracer has to win the multi-threaded signal lottery to see
the siginfo. Further no one else but the ptracer can send SIGSTOP
in the same window and generate an alternate SIGSTOP.

So my best analysis is that except under very controlled circumstances
si_pid for SIGSTOP will be unreliable. Except in a test I don't know
how you could guarantee those controlled circumstances. But please if
you can see a scenario that I don't please let me know.

Eric