Skip to content

Commit 0baf2a4

Browse files
Martin Schwidefskytorvalds
authored andcommitted
kernel/kmod: fix use-after-free of the sub_info structure
Found this in the message log on a s390 system: BUG kmalloc-192 (Not tainted): Poison overwritten Disabling lock debugging due to kernel taint INFO: 0x00000000684761f4-0x00000000684761f7. First byte 0xff instead of 0x6b INFO: Allocated in call_usermodehelper_setup+0x70/0x128 age=71 cpu=2 pid=648 __slab_alloc.isra.47.constprop.56+0x5f6/0x658 kmem_cache_alloc_trace+0x106/0x408 call_usermodehelper_setup+0x70/0x128 call_usermodehelper+0x62/0x90 cgroup_release_agent+0x178/0x1c0 process_one_work+0x36e/0x680 worker_thread+0x2f0/0x4f8 kthread+0x10a/0x120 kernel_thread_starter+0x6/0xc kernel_thread_starter+0x0/0xc INFO: Freed in call_usermodehelper_exec+0x110/0x1b8 age=71 cpu=2 pid=648 __slab_free+0x94/0x560 kfree+0x364/0x3e0 call_usermodehelper_exec+0x110/0x1b8 cgroup_release_agent+0x178/0x1c0 process_one_work+0x36e/0x680 worker_thread+0x2f0/0x4f8 kthread+0x10a/0x120 kernel_thread_starter+0x6/0xc kernel_thread_starter+0x0/0xc There is a use-after-free bug on the subprocess_info structure allocated by the user mode helper. In case do_execve() returns with an error ____call_usermodehelper() stores the error code to sub_info->retval, but sub_info can already have been freed. Regarding UMH_NO_WAIT, the sub_info structure can be freed by __call_usermodehelper() before the worker thread returns from do_execve(), allowing memory corruption when do_execve() failed after exec_mmap() is called. Regarding UMH_WAIT_EXEC, the call to umh_complete() allows call_usermodehelper_exec() to continue which then frees sub_info. To fix this race the code needs to make sure that the call to call_usermodehelper_freeinfo() is always done after the last store to sub_info->retval. Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com> Reviewed-by: Oleg Nesterov <oleg@redhat.com> Cc: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c8d523a commit 0baf2a4

1 file changed

Lines changed: 37 additions & 39 deletions

File tree

‎kernel/kmod.c‎

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,34 @@ int __request_module(bool wait, const char *fmt, ...)
196196
EXPORT_SYMBOL(__request_module);
197197
#endif /* CONFIG_MODULES */
198198

199+
static void call_usermodehelper_freeinfo(struct subprocess_info *info)
200+
{
201+
if (info->cleanup)
202+
(*info->cleanup)(info);
203+
kfree(info);
204+
}
205+
206+
static void umh_complete(struct subprocess_info *sub_info)
207+
{
208+
struct completion *comp = xchg(&sub_info->complete, NULL);
209+
/*
210+
* See call_usermodehelper_exec(). If xchg() returns NULL
211+
* we own sub_info, the UMH_KILLABLE caller has gone away
212+
* or the caller used UMH_NO_WAIT.
213+
*/
214+
if (comp)
215+
complete(comp);
216+
else
217+
call_usermodehelper_freeinfo(sub_info);
218+
}
219+
199220
/*
200221
* This is the task which runs the usermode application
201222
*/
202223
static int ____call_usermodehelper(void *data)
203224
{
204225
struct subprocess_info *sub_info = data;
226+
int wait = sub_info->wait & ~UMH_KILLABLE;
205227
struct cred *new;
206228
int retval;
207229

@@ -221,7 +243,7 @@ static int ____call_usermodehelper(void *data)
221243
retval = -ENOMEM;
222244
new = prepare_kernel_cred(current);
223245
if (!new)
224-
goto fail;
246+
goto out;
225247

226248
spin_lock(&umh_sysctl_lock);
227249
new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
@@ -233,7 +255,7 @@ static int ____call_usermodehelper(void *data)
233255
retval = sub_info->init(sub_info, new);
234256
if (retval) {
235257
abort_creds(new);
236-
goto fail;
258+
goto out;
237259
}
238260
}
239261

@@ -242,12 +264,13 @@ static int ____call_usermodehelper(void *data)
242264
retval = do_execve(getname_kernel(sub_info->path),
243265
(const char __user *const __user *)sub_info->argv,
244266
(const char __user *const __user *)sub_info->envp);
267+
out:
268+
sub_info->retval = retval;
269+
/* wait_for_helper() will call umh_complete if UHM_WAIT_PROC. */
270+
if (wait != UMH_WAIT_PROC)
271+
umh_complete(sub_info);
245272
if (!retval)
246273
return 0;
247-
248-
/* Exec failed? */
249-
fail:
250-
sub_info->retval = retval;
251274
do_exit(0);
252275
}
253276

@@ -258,26 +281,6 @@ static int call_helper(void *data)
258281
return ____call_usermodehelper(data);
259282
}
260283

261-
static void call_usermodehelper_freeinfo(struct subprocess_info *info)
262-
{
263-
if (info->cleanup)
264-
(*info->cleanup)(info);
265-
kfree(info);
266-
}
267-
268-
static void umh_complete(struct subprocess_info *sub_info)
269-
{
270-
struct completion *comp = xchg(&sub_info->complete, NULL);
271-
/*
272-
* See call_usermodehelper_exec(). If xchg() returns NULL
273-
* we own sub_info, the UMH_KILLABLE caller has gone away.
274-
*/
275-
if (comp)
276-
complete(comp);
277-
else
278-
call_usermodehelper_freeinfo(sub_info);
279-
}
280-
281284
/* Keventd can't block, but this (a child) can. */
282285
static int wait_for_helper(void *data)
283286
{
@@ -336,18 +339,8 @@ static void __call_usermodehelper(struct work_struct *work)
336339
kmod_thread_locker = NULL;
337340
}
338341

339-
switch (wait) {
340-
case UMH_NO_WAIT:
341-
call_usermodehelper_freeinfo(sub_info);
342-
break;
343-
344-
case UMH_WAIT_PROC:
345-
if (pid > 0)
346-
break;
347-
/* FALLTHROUGH */
348-
case UMH_WAIT_EXEC:
349-
if (pid < 0)
350-
sub_info->retval = pid;
342+
if (pid < 0) {
343+
sub_info->retval = pid;
351344
umh_complete(sub_info);
352345
}
353346
}
@@ -588,7 +581,12 @@ int call_usermodehelper_exec(struct subprocess_info *sub_info, int wait)
588581
goto out;
589582
}
590583

591-
sub_info->complete = &done;
584+
/*
585+
* Set the completion pointer only if there is a waiter.
586+
* This makes it possible to use umh_complete to free
587+
* the data structure in case of UMH_NO_WAIT.
588+
*/
589+
sub_info->complete = (wait == UMH_NO_WAIT) ? NULL : &done;
592590
sub_info->wait = wait;
593591

594592
queue_work(khelper_wq, &sub_info->work);

0 commit comments

Comments
 (0)