seccomp: release task filters when the task exits
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): CAEWA0a5dBvRwGAnztL56i=JV-WGGiaTd-GdJYdOxZmq1c+bdpg@mail.gmail.com
(add lkml)
Quoted 7 lines · Andrei Vagin
Quoted 3 lines · Oleg Nesterov
Let me repeat I forgot everything about seccomp, but let me ask a couple of questions...
It seems you still remember something:). Thank you for the feedback.
Just I am still remember how to use grep ;)
Quoted 11 lines
Quoted 11 lines
Review static struct seccomp_filter *get_nth_filter(struct task_struct *task, · line 2143
Quoted 8 lines
(quoted diff) +4
-0@@ -2126,6 +2137,11 @@ static struct seccomp_filter *get_nth_filter(struct task_struct *task, */ spin_lock_irq(&task->sighand->siglock); + if (task->flags & PF_EXITING) { + spin_unlock_irq(&task->sighand->siglock); + return ERR_PTR(-EINVAL); + }Why do we need the
PF_EXITINGcheck here?
Quoted 8 lines
Quoted 6 lines
This looks unnecessary even if
get_nth_filter()could race with the exiting task, but this doesn't matter.This race is not possible,
get_nth_filter()is only called fromptrace()paths, but the tracee can't stop inTASK_TRACEDafterexit_signals()which setsPF_EXITING.If we rely on using
seccomp_get_filteronly from ptrace, you are right.
Plus it too does __get_seccomp_filter/__get_seccomp_filter, so I guess it should be safe without this check even if it could be used outside of ptrace. Just like proc_pid_seccomp_cache(), see below.
Quoted 14 lines
Quoted 10 lines
Review int proc_pid_seccomp_cache(struct seq_file *m, struct pid_namespace *ns, · line 2515
Quoted 7 lines
(quoted diff) +3
-0@@ -2494,6 +2510,11 @@ int proc_pid_seccomp_cache(struct seq_file *m, struct pid_namespace *ns, if (!lock_task_sighand(task, &flags)) return -ESRCH; + if (thread->flags & PF_EXITING) { + unlock_task_sighand(task, &flags); + return 0;Again, do we really need this check?
Quoted 3 lines
It can race with the exiting task and (without this check) do
__get_seccomp_filter(f)right beforeseccomp_filter_release()takessighand->siglock. But why is it bad?
Quoted 7 lines
I think you are right, this check isn't required.
Quoted 3 lines
OTOH. I guess
proc_pid_seccomp_cache()is the only reason whyseccomp_filter_release()takes ->siglock with your patch?
seccomp_sync_threadsandseccomp_can_sync_threadsshould be considered too.
Yes. But we only need to consider them in the multi-thread case, right? In this case exit_signals() sets PF_EXITING under ->siglock, so they can't miss this flag, seccomp_filter_release() doesn't need to take siglock.
Quoted 2 lines
If we check
PF_EXITINGin all of them, we don't need to take ->siglock inseccomp_filter_release. Does it sound right?
The problem is a single-threaded exiting task. In this case exit_signals() sets PF_EXITING lockless. This means that in this case
- proc_pid_seccomp_cache() can't rely on the PF_EXITING check but it can be safely removed.
- seccomp_filter_release() needs to take ->siglock to avoid the race with proc_pid_seccomp_cache().
And this chunk from your patch
static void __seccomp_filter_orphan(struct seccomp_filter *orig)
{
+ lockdep_assert_held(¤t->sighand->siglock);
+looks unnecessary too, seccomp_filter_release() can just do
spin_lock_irq(siglock); orig = tsk->seccomp.filter; tsk->seccomp.filter = NULL; spin_unlock_irq(siglock); __seccomp_filter_release(orig);
Right?
Oleg.
Quoted 8 lines · Oleg Nesterov
Quoted 2 lines · Andrei Vagin
seccomp_sync_threadsandseccomp_can_sync_threadsshould be considered too.Yes. But we only need to consider them in the multi-thread case, right? In this case
exit_signals()setsPF_EXITINGunder ->siglock, so they can't miss this flag,seccomp_filter_release()doesn't need to take siglock.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Ah, no. seccomp_filter_release() does need to take ->siglock even if we forget about proc_pid_seccomp_cache().
Without siglock
orig = tsk->seccomp.filter;
can leak into the critical section in exit_signals() (spin_unlock is the one-way barrier) and this LOAD can be reordered with "flags |= PF_EXITING".
Hmm. I thought we have something smp_mb__after_unlock(), but it seems we don't. So we can't add a fast-path
if (!tsk->seccomp.filter) return;
check at the start of seccomp_filter_release().
Cough... Now that I look at seccomp_can_sync_threads() I think it too doesn't need the PF_EXITING check.
If it is called before seccomp_filter_release(), this doesn't really differ from the case when it is called before do_exit/exit_signals.
If it is called after seccomp_filter_release(), then is_ancestor() must be true.
But perhaps I missed something, I won't insist, up to you.
Quoted 31 lines
Quoted 2 lines
If we check
PF_EXITINGin all of them, we don't need to take ->siglock inseccomp_filter_release. Does it sound right?The problem is a single-threaded exiting task. In this case
exit_signals()setsPF_EXITINGlockless. This means that in this case-
proc_pid_seccomp_cache()can't rely on thePF_EXITINGcheck but it can be safely removed.-
seccomp_filter_release()needs to take ->siglock to avoid the race withproc_pid_seccomp_cache().And this chunk from your patch
static void __seccomp_filter_orphan(struct seccomp_filter *orig) { + lockdep_assert_held(¤t->sighand->siglock); +looks unnecessary too,
seccomp_filter_release()can just dospin_lock_irq(siglock); orig = tsk->seccomp.filter; tsk->seccomp.filter = NULL; spin_unlock_irq(siglock); __seccomp_filter_release(orig);Right?
Oleg.
Quoted 22 lines · Oleg Nesterov
Quoted 11 lines · Oleg Nesterov
Quoted 2 lines · Andrei Vagin
seccomp_sync_threadsandseccomp_can_sync_threadsshould be considered too.Yes. But we only need to consider them in the multi-thread case, right?
In this case
exit_signals()setsPF_EXITINGunder ->siglock, so they can't miss this flag,seccomp_filter_release()doesn't need to take siglock.^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Ah, no.
seccomp_filter_release()does need to take ->siglock even if we forget aboutproc_pid_seccomp_cache().Without siglock
Quoted 7 lines
orig = tsk->seccomp.filter;can leak into the critical section in
exit_signals()(spin_unlockis the one-way barrier) and this LOAD can be reordered with "flags |=PF_EXITING".Hmm. I thought we have something
smp_mb__after_unlock(), but it seems we don't. So we can't add a fast-path
We have smp_mb__after_unlock_lock in include/linux/rcupdate.h.
Quoted 50 lines
if (!tsk->seccomp.filter) return;check at the start of
seccomp_filter_release().Cough... Now that I look at
seccomp_can_sync_threads()I think it too doesn't need thePF_EXITINGcheck.If it is called before
seccomp_filter_release(), this doesn't really differ from the case when it is called before do_exit/exit_signals.If it is called after
seccomp_filter_release(), thenis_ancestor()must be true.But perhaps I missed something, I won't insist, up to you.
Quoted 31 lines
Quoted 2 lines
If we check
PF_EXITINGin all of them, we don't need to take ->siglock inseccomp_filter_release. Does it sound right?The problem is a single-threaded exiting task. In this case
exit_signals()setsPF_EXITINGlockless. This means that in this case-
proc_pid_seccomp_cache()can't rely on thePF_EXITINGcheck but it can be safely removed.-
seccomp_filter_release()needs to take ->siglock to avoid the race withproc_pid_seccomp_cache().And this chunk from your patch
static void
__seccomp_filter_orphan(struct seccomp_filter *orig){
+lockdep_assert_held(¤t->sighand->siglock);
+looks unnecessary too,
seccomp_filter_release()can just dospin_lock_irq(siglock); orig = tsk->seccomp.filter; tsk->seccomp.filter = NULL; spin_unlock_irq(siglock); __seccomp_filter_release(orig);Right?
Oleg.
Quoted 12 lines · Oleg Nesterov
Quoted 11 lines · Oleg Nesterov
Quoted 8 lines · Andrei Vagin
Quoted 2 lines
seccomp_sync_threadsandseccomp_can_sync_threadsshould be considered too.Yes. But we only need to consider them in the multi-thread case, right? In this case
exit_signals()setsPF_EXITINGunder ->siglock, so they can't miss this flag,seccomp_filter_release()doesn't need to take siglock.^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PF_EXITING is set without holding ->siglock if tsk->signal has the SIGNAL_GROUP_EXIT flag. I think it can be a case when one thread is in seccomp_sync_threads and others are exiting. The first thread can check that PF_EXITING isn't set for another thread. Then, the second thread calls exit_signals and seccomp_filter_release(), and finally, the first thread sets its seccomp.filter to the second thread. If seccomp_filter_release takes siglock, it will be handled properly.
Quoted 6 lines
Quoted 6 lines
Ah, no.
seccomp_filter_release()does need to take ->siglock even if we forget aboutproc_pid_seccomp_cache().Without siglock
Quoted 7 lines
Quoted 7 lines
orig = tsk->seccomp.filter;can leak into the critical section in
exit_signals()(spin_unlockis the one-way barrier) and this LOAD can be reordered with "flags |=PF_EXITING".Hmm. I thought we have something
smp_mb__after_unlock(), but it seems we don't. So we can't add a fast-path
We have smp_mb__after_unlock_lock in include/linux/rcupdate.h.
Quoted 10 lines
Quoted 10 lines
if (!tsk->seccomp.filter) return;check at the start of
seccomp_filter_release().Cough... Now that I look at
seccomp_can_sync_threads()I think it too doesn't need thePF_EXITINGcheck.
Quoted 8 lines
Quoted 8 lines
If it is called before
seccomp_filter_release(), this doesn't really differ from the case when it is called before do_exit/exit_signals.If it is called after
seccomp_filter_release(), thenis_ancestor()must be true.But perhaps I missed something, I won't insist, up to you.
You are right, this check isn't required in seccomp_can_sync_threads, but I decided that it is better to be consistent with seccomp_sync_threads.
Thanks,
Andrei
Quoted 9 lines · Andrei Vagin
Quoted 9 lines · Oleg Nesterov
Quoted 7 lines · Oleg Nesterov
Quoted 5 lines · Andrei Vagin
Quoted 2 lines
seccomp_sync_threadsandseccomp_can_sync_threadsshould be considered too.
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Yes. But we only need to consider them in the multi-thread case, right? In this case
exit_signals()setsPF_EXITINGunder ->siglock, so they can't miss this flag,seccomp_filter_release()doesn't need to take siglock.^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PF_EXITINGis set without holding ->siglock iftsk->signalhas theSIGNAL_GROUP_EXITflag. I think it can be a case when one thread is inseccomp_sync_threadsand others are exiting.
Yes, I forgot this.
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Hmm. I thought we have something
smp_mb__after_unlock(), but it seems we don't. So we can't add a fast-pathWe have
smp_mb__after_unlock_lockininclude/linux/rcupdate.h.
This is another thing.
But sorry for confusion, this doesn't really matter, we could you a plain mb(). I mean, I was thinking about something like
seccomp_filter_release: smp_mb(); if (!READ_ONCE(tsk->seccomp.filter)) return; spin_lock_irq(siglock); orig = tsk->seccomp.filter; ...
but then seccomp_sync_threads() should do something like
orig = READ_ONCE(thread->seccomp.filter);
smp_store_release(&thread->seccomp.filter,
caller->seccomp.filter);
smp_mb(); // pairs with mb() in seccomp_filter_release()
if (READ_ONCE(thread->flags) & PF_EXITING) {
WRITE_ONCE(thread->seccomp.filter, orig);
continue;
}
__seccomp_filter_release(orig);
...too subtle even _if_ correct, and I am not sure at all this would be correct.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Cough... Now that I look at
seccomp_can_sync_threads()I think it too doesn't need thePF_EXITINGcheck.If it is called before
seccomp_filter_release(), this doesn't really differ from the case when it is called before do_exit/exit_signals.
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
If it is called after
seccomp_filter_release(), thenis_ancestor()must be true.But perhaps I missed something, I won't insist, up to you.
You are right, this check isn't required in
seccomp_can_sync_threads, but I decided that it is better to be consistent withseccomp_sync_threads.
OK, agreed.
Oleg.