Skip to contentSkip to conversation

exec: Only allow a threaded init to exec from the thread_group_leader

PATCH In review Eric W. Biederman 4 participants 9 messages Jan 29, 2006 – Jan 30, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 exec: Only allow a threaded init to exec from the thread_group_leader

The weird things we do when we exec from a thread group are just ugly. Those ugly things do not handle the case of init and I suspect extending that code to properly support a threaded init would be just hideous, and impossible to maintain.

So just in case someone ever threads init return an error for the unimplemented case.

---

 fs/exec.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

408dad0f2b7067b23929866150e73b2b2f12d662

fs/exec.c +6 -0
index 055378d..c9d8e31 100644
@@ -600,6 +600,12 @@ static int de_thread(struct task_struct 
 	if (thread_group_empty(current))
 		goto no_thread_group;
 
+	/* A threaded init must exec from it's primary thread.
+	 * As the init task (i.e. child_reaper) may not exit.
+	 */
+	if (!thread_group_leader(current) && (current->tgid == 1))
+		return -EINVAL;
+	
 	/*
 	 * Kill all other threads in the thread group.
 	 * We must hold tasklist_lock to call zap_other_threads.
Signature · 1 line
1.1.5.g3480
#1 lore raw
Quoted 25 lines · [email protected] (Eric W. Biederman) wrote:

The weird things we do when we exec from a thread group are just ugly. Those ugly things do not handle the case of init and I suspect extending that code to properly support a threaded init would be just hideous, and impossible to maintain.

So just in case someone ever threads init return an error for the unimplemented case.

---

 fs/exec.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

408dad0f2b7067b23929866150e73b2b2f12d662

fs/exec.c +0 -0
index 055378d..c9d8e31 100644
@@ -600,6 +600,12 @@ static int de_thread(struct task_struct 
 	if (thread_group_empty(current))
 		goto no_thread_group;

fs/exec.c static int de_thread(struct task_struct · line 611

Quoted 10 lines
fs/exec.c +6 -0
 
+	/* A threaded init must exec from it's primary thread.
+	 * As the init task (i.e. child_reaper) may not exit.
+	 */
+	if (!thread_group_leader(current) && (current->tgid == 1))
+		return -EINVAL;
+	
 	/*
 	 * Kill all other threads in the thread group.
 	 * We must hold tasklist_lock to call zap_other_threads.

hmm, this just looks like overhead. If sometime someone _does_ try to thread init, what will happen to them? If it's something nice and nasty, they'll just whine at us and stop doing that. Same net effect, no runtime cost.

#2 lore raw
Quoted 9 lines · Andrew Morton
Quoted 9 lines

408dad0f2b7067b23929866150e73b2b2f12d662

fs/exec.c +0 -0
index 055378d..c9d8e31 100644
@@ -600,6 +600,12 @@ static int de_thread(struct task_struct 
 	if (thread_group_empty(current))
 		goto no_thread_group;
 
Quoted 14 lines

fs/exec.c static int de_thread(struct task_struct · line 611

Quoted 9 lines
fs/exec.c +6 -0
+	/* A threaded init must exec from it's primary thread.
+	 * As the init task (i.e. child_reaper) may not exit.
+	 */
+	if (!thread_group_leader(current) && (current->tgid == 1))
+		return -EINVAL;
+	
 	/*
 	 * Kill all other threads in the thread group.
 	 * We must hold tasklist_lock to call zap_other_threads.

hmm, this just looks like overhead. If sometime someone _does_ try to thread init, what will happen to them? If it's something nice and nasty, they'll just whine at us and stop doing that. Same net effect, no runtime cost.

So threading init will work just fine. The only case that will blow up is calling exec from something that is not the thread group leader. i.e. If tgid == 1 but pid != 1 the kernel will cause pid == 1 to exit.

I think that will trigger a kernel panic. It might just ensure that no more processes are re-parented to init. And we dereference a bad pointer we look at child_reaper. I haven't been brave enough to try it.

The cost is only paid if you are a threaded task and you call exec. Normal process never take that path, so we are already off of the fast path.

The test when all expanded out is only:

if ((current->tgid == current->pid) && (current->tgid == 1))
return -EINVAL

So it should be relatively cheap.

If process id namespaces become a reality init stops being terribly special, and becomes something you may have several of running at any one time. If one of those inits is compromised by a hostile user I having the whole system go down so we can avoid executing a cheap test sounds terribly wrong. That is why I really care.

Eric

#3 lore raw
Quoted 7 lines · [email protected] (Eric W. Biederman) wrote:

If process id namespaces become a reality init stops being terribly special, and becomes something you may have several of running at any one time. If one of those inits is compromised by a hostile user I having the whole system go down so we can avoid executing a cheap test sounds terribly wrong. That is why I really care.

Wouldn't it be better to do nothing until/unless there's some code in the kernel or init which actually needs the change?

#4 lore raw
Quoted 11 lines · Andrew Morton
Quoted 7 lines · [email protected] (Eric W. Biederman) wrote:

If process id namespaces become a reality init stops being terribly special, and becomes something you may have several of running at any one time. If one of those inits is compromised by a hostile user I having the whole system go down so we can avoid executing a cheap test sounds terribly wrong. That is why I really care.

Wouldn't it be better to do nothing until/unless there's some code in the kernel or init which actually needs the change?

It is common to do init=/bin/bash, and I guess people are doing it with all kinds of wonderful apps....
Pavel

Signature · 1 line
Thanks, Sharp!
#5 lore raw
Quoted 11 lines · Pavel Machek
Quoted 9 lines · Andrew Morton
Quoted 7 lines · [email protected] (Eric W. Biederman) wrote:

If process id namespaces become a reality init stops being terribly special, and becomes something you may have several of running at any one time. If one of those inits is compromised by a hostile user I having the whole system go down so we can avoid executing a cheap test sounds terribly wrong. That is why I really care.

Quoted 5 lines
Quoted 2 lines

Wouldn't it be better to do nothing until/unless there's some code in the kernel or init which actually needs the change?

It is common to do init=/bin/bash, and I guess people are doing it with all kinds of wonderful apps....

err, good point. And no reports of peculiar things happening with threading. Eric's check has the (slight) potential to cause some things to stop working though.

#6 lore raw
In reply to Pavel Machek
Quoted 10 lines · Pavel Machek
Quoted 9 lines · Andrew Morton
Quoted 7 lines · [email protected] (Eric W. Biederman) wrote:

If process id namespaces become a reality init stops being terribly special, and becomes something you may have several of running at any one time. If one of those inits is compromised by a hostile user I having the whole system go down so we can avoid executing a cheap test sounds terribly wrong. That is why I really care.

Quoted 2 lines
Quoted 2 lines

Wouldn't it be better to do nothing until/unless there's some code in the kernel or init which actually needs the change?

Well I think deliberate kernel bugs should at least be accompanied by a big fat warning in the code, and an explanation of what is happening and why.

Quoted 2 lines

It is common to do init=/bin/bash, and I guess people are doing it with all kinds of wonderful apps....

So it is just a matter of time before someone actually hits this.

I went back through and tested and the actual symptoms are weird. The thread calling exec just hangs forever, waiting for pid == 1 to exit. Anyway I found a simple fix so it is probably better to fix it than to paper over the problem anyway.

Patch follows in a minute.

Eric

#7 lore raw
In reply to Eric W. Biederman
Quoted 4 lines

So threading init will work just fine. The only case that will blow up is calling exec from something that is not the thread group leader. i.e. If tgid == 1 but pid != 1 the kernel will cause pid == 1 to exit.

Should not it, at its best, replace the whole thread group by the new program and have things carry on?

Jan Engelhardt

#8 lore raw
Quoted 7 lines · Jan Engelhardt
Quoted 4 lines

So threading init will work just fine. The only case that will blow up is calling exec from something that is not the thread group leader. i.e. If tgid == 1 but pid != 1 the kernel will cause pid == 1 to exit.

Should not it, at its best, replace the whole thread group by the new program and have things carry on?

You see the bug.

Eric