Skip to content

Commit fa729c4

Browse files
author
Christian Brauner
committed
clone3: validate stack arguments
Validate the stack arguments and setup the stack depening on whether or not it is growing down or up. Legacy clone() required userspace to know in which direction the stack is growing and pass down the stack pointer appropriately. To make things more confusing microblaze uses a variant of the clone() syscall selected by CONFIG_CLONE_BACKWARDS3 that takes an additional stack_size argument. IA64 has a separate clone2() syscall which also takes an additional stack_size argument. Finally, parisc has a stack that is growing upwards. Userspace therefore has a lot nasty code like the following: #define __STACK_SIZE (8 * 1024 * 1024) pid_t sys_clone(int (*fn)(void *), void *arg, int flags, int *pidfd) { pid_t ret; void *stack; stack = malloc(__STACK_SIZE); if (!stack) return -ENOMEM; #ifdef __ia64__ ret = __clone2(fn, stack, __STACK_SIZE, flags | SIGCHLD, arg, pidfd); #elif defined(__parisc__) /* stack grows up */ ret = clone(fn, stack, flags | SIGCHLD, arg, pidfd); #else ret = clone(fn, stack + __STACK_SIZE, flags | SIGCHLD, arg, pidfd); #endif return ret; } or even crazier variants such as [3]. With clone3() we have the ability to validate the stack. We can check that when stack_size is passed, the stack pointer is valid and the other way around. We can also check that the memory area userspace gave us is fine to use via access_ok(). Furthermore, we probably should not require userspace to know in which direction the stack is growing. It is easy for us to do this in the kernel and I couldn't find the original reasoning behind exposing this detail to userspace. /* Intentional user visible API change */ clone3() was released with 5.3. Currently, it is not documented and very unclear to userspace how the stack and stack_size argument have to be passed. After talking to glibc folks we concluded that trying to change clone3() to setup the stack instead of requiring userspace to do this is the right course of action. Note, that this is an explicit change in user visible behavior we introduce with this patch. If it breaks someone's use-case we will revert! (And then e.g. place the new behavior under an appropriate flag.) Breaking someone's use-case is very unlikely though. First, neither glibc nor musl currently expose a wrapper for clone3(). Second, there is no real motivation for anyone to use clone3() directly since it does not provide features that legacy clone doesn't. New features for clone3() will first happen in v5.5 which is why v5.4 is still a good time to try and make that change now and backport it to v5.3. Searches on [4] did not reveal any packages calling clone3(). [1]: https://lore.kernel.org/r/CAG48ez3q=BeNcuVTKBN79kJui4vC6nw0Bfq6xc-i0neheT17TA@mail.gmail.com [2]: https://lore.kernel.org/r/20191028172143.4vnnjpdljfnexaq5@wittgenstein [3]: https://github.com/systemd/systemd/blob/5238e9575906297608ff802a27e2ff9effa3b338/src/basic/raw-clone.h#L31 [4]: https://codesearch.debian.net Fixes: 7f192e3 ("fork: add clone3") Cc: Kees Cook <[email protected]> Cc: Jann Horn <[email protected]> Cc: David Howells <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Oleg Nesterov <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Florian Weimer <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: [email protected] Cc: [email protected] Cc: <[email protected]> # 5.3 Cc: GNU C Library <[email protected]> Signed-off-by: Christian Brauner <[email protected]> Acked-by: Arnd Bergmann <[email protected]> Acked-by: Aleksa Sarai <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent d6d5df1 commit fa729c4

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

include/uapi/linux/sched.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
* sent when the child exits.
5252
* @stack: Specify the location of the stack for the
5353
* child process.
54+
* Note, @stack is expected to point to the
55+
* lowest address. The stack direction will be
56+
* determined by the kernel and set up
57+
* appropriately based on @stack_size.
5458
* @stack_size: The size of the stack for the child process.
5559
* @tls: If CLONE_SETTLS is set, the tls descriptor
5660
* is set to tls.

kernel/fork.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2561,7 +2561,35 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
25612561
return 0;
25622562
}
25632563

2564-
static bool clone3_args_valid(const struct kernel_clone_args *kargs)
2564+
/**
2565+
* clone3_stack_valid - check and prepare stack
2566+
* @kargs: kernel clone args
2567+
*
2568+
* Verify that the stack arguments userspace gave us are sane.
2569+
* In addition, set the stack direction for userspace since it's easy for us to
2570+
* determine.
2571+
*/
2572+
static inline bool clone3_stack_valid(struct kernel_clone_args *kargs)
2573+
{
2574+
if (kargs->stack == 0) {
2575+
if (kargs->stack_size > 0)
2576+
return false;
2577+
} else {
2578+
if (kargs->stack_size == 0)
2579+
return false;
2580+
2581+
if (!access_ok((void __user *)kargs->stack, kargs->stack_size))
2582+
return false;
2583+
2584+
#if !defined(CONFIG_STACK_GROWSUP) && !defined(CONFIG_IA64)
2585+
kargs->stack += kargs->stack_size;
2586+
#endif
2587+
}
2588+
2589+
return true;
2590+
}
2591+
2592+
static bool clone3_args_valid(struct kernel_clone_args *kargs)
25652593
{
25662594
/*
25672595
* All lower bits of the flag word are taken.
@@ -2581,6 +2609,9 @@ static bool clone3_args_valid(const struct kernel_clone_args *kargs)
25812609
kargs->exit_signal)
25822610
return false;
25832611

2612+
if (!clone3_stack_valid(kargs))
2613+
return false;
2614+
25842615
return true;
25852616
}
25862617

0 commit comments

Comments
 (0)