Skip to content

Commit 786235e

Browse files
Tetsuo Handatorvalds
authored andcommitted
kthread: make kthread_create() killable
Any user process callers of wait_for_completion() except global init process might be chosen by the OOM killer while waiting for completion() call by some other process which does memory allocation. See CVE-2012-4398 "kernel: request_module() OOM local DoS" can happen. When such users are chosen by the OOM killer when they are waiting for completion() in TASK_UNINTERRUPTIBLE, the system will be kept stressed due to memory starvation because the OOM killer cannot kill such users. kthread_create() is one of such users and this patch fixes the problem for kthreadd by making kthread_create() killable - the same approach used for fixing CVE-2012-4398. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Cc: Oleg Nesterov <oleg@redhat.com> Acked-by: David Rientjes <rientjes@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 10d0c97 commit 786235e

1 file changed

Lines changed: 55 additions & 18 deletions

File tree

‎kernel/kthread.c‎

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct kthread_create_info
3333

3434
/* Result passed back to kthread_create() from kthreadd. */
3535
struct task_struct *result;
36-
struct completion done;
36+
struct completion *done;
3737

3838
struct list_head list;
3939
};
@@ -178,6 +178,7 @@ static int kthread(void *_create)
178178
struct kthread_create_info *create = _create;
179179
int (*threadfn)(void *data) = create->threadfn;
180180
void *data = create->data;
181+
struct completion *done;
181182
struct kthread self;
182183
int ret;
183184

@@ -187,10 +188,16 @@ static int kthread(void *_create)
187188
init_completion(&self.parked);
188189
current->vfork_done = &self.exited;
189190

191+
/* If user was SIGKILLed, I release the structure. */
192+
done = xchg(&create->done, NULL);
193+
if (!done) {
194+
kfree(create);
195+
do_exit(-EINTR);
196+
}
190197
/* OK, tell user we're spawned, wait for stop or wakeup */
191198
__set_current_state(TASK_UNINTERRUPTIBLE);
192199
create->result = current;
193-
complete(&create->done);
200+
complete(done);
194201
schedule();
195202

196203
ret = -EINTR;
@@ -223,8 +230,15 @@ static void create_kthread(struct kthread_create_info *create)
223230
/* We want our own signal handler (we take no signals by default). */
224231
pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
225232
if (pid < 0) {
233+
/* If user was SIGKILLed, I release the structure. */
234+
struct completion *done = xchg(&create->done, NULL);
235+
236+
if (!done) {
237+
kfree(create);
238+
return;
239+
}
226240
create->result = ERR_PTR(pid);
227-
complete(&create->done);
241+
complete(done);
228242
}
229243
}
230244

@@ -255,36 +269,59 @@ struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
255269
const char namefmt[],
256270
...)
257271
{
258-
struct kthread_create_info create;
259-
260-
create.threadfn = threadfn;
261-
create.data = data;
262-
create.node = node;
263-
init_completion(&create.done);
272+
DECLARE_COMPLETION_ONSTACK(done);
273+
struct task_struct *task;
274+
struct kthread_create_info *create = kmalloc(sizeof(*create),
275+
GFP_KERNEL);
276+
277+
if (!create)
278+
return ERR_PTR(-ENOMEM);
279+
create->threadfn = threadfn;
280+
create->data = data;
281+
create->node = node;
282+
create->done = &done;
264283

265284
spin_lock(&kthread_create_lock);
266-
list_add_tail(&create.list, &kthread_create_list);
285+
list_add_tail(&create->list, &kthread_create_list);
267286
spin_unlock(&kthread_create_lock);
268287

269288
wake_up_process(kthreadd_task);
270-
wait_for_completion(&create.done);
271-
272-
if (!IS_ERR(create.result)) {
289+
/*
290+
* Wait for completion in killable state, for I might be chosen by
291+
* the OOM killer while kthreadd is trying to allocate memory for
292+
* new kernel thread.
293+
*/
294+
if (unlikely(wait_for_completion_killable(&done))) {
295+
/*
296+
* If I was SIGKILLed before kthreadd (or new kernel thread)
297+
* calls complete(), leave the cleanup of this structure to
298+
* that thread.
299+
*/
300+
if (xchg(&create->done, NULL))
301+
return ERR_PTR(-ENOMEM);
302+
/*
303+
* kthreadd (or new kernel thread) will call complete()
304+
* shortly.
305+
*/
306+
wait_for_completion(&done);
307+
}
308+
task = create->result;
309+
if (!IS_ERR(task)) {
273310
static const struct sched_param param = { .sched_priority = 0 };
274311
va_list args;
275312

276313
va_start(args, namefmt);
277-
vsnprintf(create.result->comm, sizeof(create.result->comm),
278-
namefmt, args);
314+
vsnprintf(task->comm, sizeof(task->comm), namefmt, args);
279315
va_end(args);
280316
/*
281317
* root may have changed our (kthreadd's) priority or CPU mask.
282318
* The kernel thread should not inherit these properties.
283319
*/
284-
sched_setscheduler_nocheck(create.result, SCHED_NORMAL, &param);
285-
set_cpus_allowed_ptr(create.result, cpu_all_mask);
320+
sched_setscheduler_nocheck(task, SCHED_NORMAL, &param);
321+
set_cpus_allowed_ptr(task, cpu_all_mask);
286322
}
287-
return create.result;
323+
kfree(create);
324+
return task;
288325
}
289326
EXPORT_SYMBOL(kthread_create_on_node);
290327

0 commit comments

Comments
 (0)