copy_process: fix CLONE_PARENT && ->exit_signal interaction
Subsystems (3)
PATCH 1/2 copy_process: fix CLONE_PARENT && ->exit_signal interaction
clone(CLONE_PARENT | SIGXXX) blindly sets ->exit_signal = SIGXXX. It is not clear to me what was the supposed behaviour but this does not look right. The parent of the forking task will receive this signal which bypasses all security checks.
With this patch CLONE_PARENT re-uses both ->real_parent and ->exit_signal, this looks at least logical.
kernel/fork.c renamed from kernel/fork.c~1_EXIT_SIGNAL +9 -3
@@ -1217,10 +1217,16 @@ static struct task_struct *copy_process( !cpu_online(task_cpu(p))))set_task_cpu (p,smp_processor_id ()); -/* CLONE_PARENT re-uses the old parent */ -if (clone_flags & (CLONE_PARENT |CLONE_THREAD )) +/* CLONE_PARENT re-uses the old parent and exit_signal */ +if (clone_flags & (CLONE_PARENT |CLONE_THREAD )) { +/* +* Do this under tasklist_lock to avoid the race with +* re-parenting to init. +*/ +if (!(clone_flags &CLONE_THREAD )) + p->exit_signal = current->group_leader->exit_signal; p->real_parent = current->real_parent; -else + }else p->real_parent = current;spin_lock (¤t->sighand->siglock);
As I think I said before, I don't really know what the actual use case is for CLONE_PARENT without CLONE_THREAD. So it's easy to approve changing its behavior, but I do vaguely worry about who expected what behavior before.
Just for readability, I would prefer that you just change the first line above that sets p->exit_signal initially. i.e. (but with some comments):
kernel/fork.c +4 -1
@@ -1184,7 +1184,10 @@ static struct task_struct *copy_process(unsigned long clone_flags, p->parent_exec_id = p->self_exec_id;/* ok, now we should be set up.. */ - p->exit_signal = (clone_flags &CLONE_THREAD ) ?-1 : (clone_flags &CSIGNAL ); + p->exit_signal = ((clone_flags &CLONE_THREAD ) ?-1 : + (clone_flags &CLONE_PARENT ) ? + current->group_leader->exit_signal : + (clone_flags &CSIGNAL )); p->pdeath_signal =0 ; p->exit_state =0 ;
Thanks,
Roland
Quoted 4 lines · Roland McGrath
As I think I said before, I don't really know what the actual use case is for
CLONE_PARENTwithoutCLONE_THREAD. So it's easy to approve changing its behavior, but I do vaguely worry about who expected what behavior before.
Me too ;)
Hopefully CC is wide enough...
Quoted 8 lines
Just for readability, I would prefer that you just change the first line above that sets
p->exit_signalinitially. i.e. (but with some comments):kernel/fork.c +0
-0
kernel/fork.c static struct task_struct *copy_process(unsigned long clone_flags, · line 1190
Quoted 8 lines
kernel/fork.c +4
-1@@ -1184,7 +1184,10 @@ static struct task_struct *copy_process(unsigned long clone_flags, p->parent_exec_id = p->self_exec_id;/* ok, now we should be set up.. */ - p->exit_signal = (clone_flags &CLONE_THREAD ) ?-1 : (clone_flags &CSIGNAL ); + p->exit_signal = ((clone_flags &CLONE_THREAD ) ?-1 : + (clone_flags &CLONE_PARENT ) ? + current->group_leader->exit_signal : + (clone_flags &CSIGNAL ));
Yes, I sent this patch to rhel list because it is most simple.
But afaics this is not exactly right.
The task T has ->exit_signal != SIGCHLD, say, SIGHUP. It does clone(CLONE_PARENT), copy_process() sets ->exit_signal = SIGHUP.
T->parent exits, T is re-parented to /sbin/init, and its ->exit_signal is changed to SIGCHLD.
copy_process() continues, takes tasklist, notices CLONE_PARENT and sets ->real_parent = init. But child's ->exit_signal is still SIGHUP.
Oleg.
Quoted 5 lines
The task T has ->exit_signal != SIGCHLD, say, SIGHUP. It does
clone(CLONE_PARENT),copy_process()sets ->exit_signal = SIGHUP.
T->parentexits, T is re-parented to/sbin/init, and its ->exit_signal is changed to SIGCHLD.
I see. It's tempting to call this a reason to just use SIGCHLD instead of current->group_leader->exit_signal. Of course, this all really begs the question of who uses the case and what they want.
Thanks,
Roland
Quoted 4 lines · Roland McGrath
As I think I said before, I don't really know what the actual use case is for
CLONE_PARENTwithoutCLONE_THREAD. So it's easy to approve changing its behavior, but I do vaguely worry about who expected what behavior before.
I think changing it is wrong.
I can easily see somebody using CLONE_PARENT to get the correct getppid semantics in the thread, and then setting the signal to zero to not make the parent see the thread go away. I think both yours and Oleg's patch break that.
And there _are_ programs that have used clone() directly - not using any pthreads emulation layer. Some game used it for audio threads etc.
So at the very least it should accept zero for "no signal". And quite frankly, it would be good to try to see if there are other alternatives.
Linus
Quoted 6 lines · Linus Torvalds
Quoted 4 lines
As I think I said before, I don't really know what the actual use case is for
CLONE_PARENTwithoutCLONE_THREAD. So it's easy to approve changing its behavior, but I do vaguely worry about who expected what behavior before.I think changing it is wrong.
Perhaps. As I said, I don't know what is the expected behaviour. And in fact I can't think of the "obviously good" behaviour.
Quoted 3 lines
I can easily see somebody using
CLONE_PARENTto get the correct getppid semantics in the thread, and then setting the signal to zero to not make the parent see the thread go away.
->exit_signal == 0 doesn't mean the thread silently goes away, it becomes a zombie (even if ->parent ignores SIGCHLD). We don't send the signal, but that is all.
And if ->parent execs, we reset ->exit_signal to SIGCHLD anyway.
Quoted 1 line
So at the very least it should accept zero for "no signal".
perhaps. I don't know. But I am not sure this is always right.
Quoted 2 lines
And quite
frankly, it would be good to try to see if there are other alternatives.
Agreed. I thought about checking ->xxx_exec_id's in copy_process(), but doesn't look very nice...
(can't resist... hopefully now it is clear this should have beeen discussed
outside of the closed lists from the very beginning ;)
Oleg.
Quoted 2 lines · Oleg Nesterov
(can't resist... hopefully now it is clear this should have beeen discussed
outside of the closed lists from the very beginning ;)
I don't understand why it wasn't in the first place, holding this back helped nothing except delay this by a few months :(
Quoted 16 lines · Oleg Nesterov
Quoted 6 lines · Linus Torvalds
Quoted 4 lines
As I think I said before, I don't really know what the actual use case is for
CLONE_PARENTwithoutCLONE_THREAD. So it's easy to approve changing its behavior, but I do vaguely worry about who expected what behavior before.I think changing it is wrong.
Perhaps. As I said, I don't know what is the expected behaviour. And in fact I can't think of the "obviously good" behaviour.
Quoted 3 lines
I can easily see somebody using
CLONE_PARENTto get the correct getppid semantics in the thread, and then setting the signal to zero to not make the parent see the thread go away.
Quoted 5 lines
->exit_signal == 0 doesn't mean the thread silently goes away, it becomes a zombie (even if ->parent ignores SIGCHLD). We don't send the signal, but that is all.
And if ->parent execs, we reset ->exit_signal to SIGCHLD anyway.
Really, how CLONE_PARENT + exit_signal==0 can be useful?
I still think this patch does the right change. Not because it fixes the security problem, as I said I do not think the ability to send SIGKILL to ->parent is wrong from the security pov, even if child does setuid/etc, the problem is that CLONE_PARENT can fool ->xxx_exec_id logic and we can send SIGKILL after parent/child exec.
But because the current behaviour is just silly. Imho.
But of course, if this change can break the user-space applications, then it should not be applied.
Quoted 5 lines
Quoted 2 lines
And quite
frankly, it would be good to try to see if there are other alternatives.Agreed. I thought about checking ->xxx_exec_id's in
copy_process(), but doesn't look very nice...
I meant something like the patch below. But I don't like it.
Anybody has other ideas?
Oleg.
fork.c +8 -2
@@ -1218,9 +1218,15 @@ static struct task_struct *copy_process(set_task_cpu (p,smp_processor_id ());/* CLONE_PARENT re-uses the old parent */ -if (clone_flags & (CLONE_PARENT |CLONE_THREAD )) +if (clone_flags & (CLONE_PARENT |CLONE_THREAD )) { p->real_parent = current->real_parent; -else + p->self_exec_id = p->parent_exec_id = + p->real_parent->self_exec_id; + +if (current->parent_exec_id != current->real_parent->self_exec_id || + current->self_exec_id != current->parent_exec_id) + p->exit_signal =SIGCHLD ; + }else p->real_parent = current;spin_lock (¤t->sighand->siglock);
Quoted 2 lines · Oleg Nesterov
Really, how
CLONE_PARENT+ exit_signal==0 can be useful?
Umm? For the reason I already stated?
Quoted 1 line
But because the current behaviour is just silly. Imho.
So? If there are people using it, you calling it silly is immaterial.
The fact is, it's an existing user space interface. We don't break them.
Quoted 2 lines
But of course, if this change can break the user-space applications, then it should not be applied.
That's the point. Nobody has shown that there aren't any users.
Linus
Quoted 6 lines · Linus Torvalds
Quoted 2 lines · Oleg Nesterov
Really, how
CLONE_PARENT+ exit_signal==0 can be useful?Umm? For the reason I already stated?
But I can not understand that reason... As I said, the thread does not go away if exit_signal == 0. Yes, the parent does not see it has a new zombie, but why this is useful?
Quoted 10 lines
Quoted 1 line
But because the current behaviour is just silly. Imho.
So? If there are people using it, you calling it silly is immaterial.
The fact is, it's an existing user space interface. We don't break them.
Quoted 2 lines
But of course, if this change can break the user-space applications, then it should not be applied.
That's the point. Nobody has shown that there aren't any users.
Yes, yes, sure...
I sent another patch, perhaps someone can comment. Or suggest something else.
Oleg.
Quoted 4 lines · Oleg Nesterov
But I can not understand that reason... As I said, the thread does not go away if
exit_signal== 0. Yes, the parent does not see it has a new zombie, but why this is useful?
Well, I know that people actually do this, because I found it in google code search. Maybe it was just a test program or something. So I didn't really care about "useful vs not", because I only care about "is it done vs not".
People do tons of useless things. That's the only way to explain Dr Phil.
Linus
PATCH 1/1 copy_process: fix CLONE_PARENT && parent_exec_id interaction
In reply to Oleg NesterovQuoted 3 lines · Oleg Nesterov
I sent another patch, perhaps someone can comment. Or suggest something else.
Nobody commented ;)
I am re-sending this patch simplified to one-liner. If this patch is accepted, I think it makes sense to move the first "p->parent_exec_id = p->self_exec_id" in copy_process() down, under the "else" branch. Just for readability.
Note! This patch doesn't even try to address the original CVE. Let me repeat, I am not the security expert, please correct me. But, unless parent or child change security context (via exec), it is OK to send any ->exit_signal when the child exits.
Comments?
----------------------------------------------------------------------
[PATCH] copy_process: fix CLONE_PARENT && parent_exec_id interaction
CLONE_PARENT can fool the ->self_exec_id/parent_exec_id logic. If we re-use the old parent, we must also re-use ->parent_exec_id to make sure exit_notify() sees the right ->xxx_exec_id's when the CLONE_PARENT'ed task exits.
kernel/fork.c renamed from kernel/fork.c~3_CLONE_PARENT +3 -2
@@ -1218,9 +1218,10 @@ static struct task_struct *copy_process(set_task_cpu (p,smp_processor_id ());/* CLONE_PARENT re-uses the old parent */ -if (clone_flags & (CLONE_PARENT |CLONE_THREAD )) +if (clone_flags & (CLONE_PARENT |CLONE_THREAD )) { p->real_parent = current->real_parent; -else + p->parent_exec_id = current->parent_exec_id; + }else p->real_parent = current;spin_lock (¤t->sighand->siglock);
Re: [PATCH] copy_process: fix CLONE_PARENT && parent_exec_id interaction
Quoted 6 lines · Oleg Nesterov
I am re-sending this patch simplified to one-liner. If this patch is accepted, I think it makes sense to move the first "
p->parent_exec_id=p->self_exec_id" incopy_process()down, under the "else" branch. Just for readability.
Quoted 6 lines
Note! This patch doesn't even try to address the original CVE. Let me repeat, I am not the security expert, please correct me. But, unless parent or child change security context (via exec), it is OK to send any ->exit_signal when the child exits.
Comments?
I think this looks correct and sane. And I agree with your "also move down the "p->parent_exec_id = p->self_exec_id" thing. In fact, I'd agree with it so much that I think it should be part of this patch, just because that not only clarifies the code, but it also makes it more obvious what the real change of this one single _patch_ is.
No?
Linus
Re: [PATCH] copy_process: fix CLONE_PARENT && parent_exec_id interaction
Quoted 14 lines · Linus Torvalds
Quoted 11 lines · Oleg Nesterov
I am re-sending this patch simplified to one-liner. If this patch is accepted, I think it makes sense to move the first "
p->parent_exec_id=p->self_exec_id" incopy_process()down, under the "else" branch. Just for readability.Note! This patch doesn't even try to address the original CVE. Let me repeat, I am not the security expert, please correct me. But, unless parent or child change security context (via exec), it is OK to send any ->exit_signal when the child exits.
Quoted 7 lines
Quoted 1 line
Comments?
I think this looks correct and sane. And I agree with your "also move down the "
p->parent_exec_id=p->self_exec_id" thing. In fact, I'd agree with it so much that I think it should be part of this patch, just because that not only clarifies the code, but it also makes it more obvious what the real change of this one single _patch_ is.
Agreed, please find v2 below. But I failed to make the comment...
Also, "p->parent_exec_id = current->parent_exec_id" is not really needed in v2, it was already copied by dup_task_struct(). But I think it is better to make the code a bit more explicit.
------------------
[PATCH v2] copy_process: fix CLONE_PARENT && parent_exec_id interaction
CLONE_PARENT can fool the ->self_exec_id/parent_exec_id logic. If we re-use the old parent, we must also re-use ->parent_exec_id to make sure exit_notify() sees the right ->xxx_exec_id's when the CLONE_PARENT'ed task exits.
Also, move down the "p->parent_exec_id = p->self_exec_id" thing, to place two different cases together.
kernel/fork.c renamed from kernel/fork.c~3_CLONE_PARENT +5 -6
@@ -1177,10 +1177,6 @@ static struct task_struct *copy_process(#endif clear_all_latency_tracing (p); -/* Our parent execution domain becomes current domain -These must match for thread signalling to apply */ - p->parent_exec_id = p->self_exec_id; -/* ok, now we should be set up.. */ p->exit_signal = (clone_flags &CLONE_THREAD ) ?-1 : (clone_flags &CSIGNAL ); p->pdeath_signal =0 ;
@@ -1218,10 +1214,13 @@ static struct task_struct *copy_process(set_task_cpu (p,smp_processor_id ());/* CLONE_PARENT re-uses the old parent */ -if (clone_flags & (CLONE_PARENT |CLONE_THREAD )) +if (clone_flags & (CLONE_PARENT |CLONE_THREAD )) { p->real_parent = current->real_parent; -else + p->parent_exec_id = current->parent_exec_id; + }else { p->real_parent = current; + p->parent_exec_id = current->self_exec_id; + }spin_lock (¤t->sighand->siglock);
Re: [PATCH] copy_process: fix CLONE_PARENT && parent_exec_id interaction
kernel/fork.c static struct task_struct *copy_process( · line 1181
Quoted 2 lines · Oleg Nesterov
kernel/fork.c renamed from kernel/fork.c~3_CLONE_PARENT +0
-2-/* Our parent execution domain becomes current domain -These must match for thread signalling to apply */
Should you retain this comment somewhere?
David
Re: [PATCH] copy_process: fix CLONE_PARENT && parent_exec_id interaction
Quoted 7 lines · David Howells
kernel/fork.c static struct task_struct *copy_process( · line 1181
Quoted 2 lines · Oleg Nesterov
kernel/fork.c renamed from kernel/fork.c~3_CLONE_PARENT +0
-2-/* Our parent execution domain becomes current domain -These must match for thread signalling to apply */ Should you retain this comment somewhere?
This comment should be changed, but I failed to make the new one. Actually, this was one of the reasons why the first patch I sent
http://marc.info/?l=linux-kernel&m=123602926516698
didn't touch the first ""p->parent_exec_id = ...".
There is a fat comment in exit_notify(), perhaps we can just add /* see the comment in exit_notify() */.
But the "good" commenent should also explain that, since we re-use ->real_parent, the child also "inherits" the rights _and_ restrictions to send ->exit_signal != SIGCHLD.
Oleg.