aboutsummaryrefslogtreecommitdiff
path: root/nptl/allocatestack.c
diff options
context:
space:
mode:
authorH.J. Lu <hjl.tools@gmail.com>2021-02-13 11:47:46 -0800
committerH.J. Lu <hjl.tools@gmail.com>2021-07-14 06:33:58 -0700
commitd8ea0d0168b190bdf138a20358293c939509367f (patch)
tree09f489ad57652978fe9c8dbf8f649d6868bafbda /nptl/allocatestack.c
parent135425a1dd50cbe2b9db0628d6c2b36c7889f30b (diff)
downloadglibc-d8ea0d0168b190bdf138a20358293c939509367f.zip
glibc-d8ea0d0168b190bdf138a20358293c939509367f.tar.gz
glibc-d8ea0d0168b190bdf138a20358293c939509367f.tar.bz2
Add an internal wrapper for clone, clone2 and clone3
The clone3 system call (since Linux 5.3) provides a superset of the functionality of clone and clone2. It also provides a number of API improvements, including the ability to specify the size of the child's stack area which can be used by kernel to compute the shadow stack size when allocating the shadow stack. Add: extern int __clone_internal (struct clone_args *__cl_args, int (*__func) (void *__arg), void *__arg); to provide an abstract interface for clone, clone2 and clone3. 1. Simplify stack management for thread creation by passing both stack base and size to create_thread. 2. Consolidate clone vs clone2 differences into a single file. 3. Call __clone3 if HAVE_CLONE3_WAPPER is defined. If __clone3 returns -1 with ENOSYS, fall back to clone or clone2. 4. Use only __clone_internal to clone a thread. Since the stack size argument for create_thread is now unconditional, always pass stack size to create_thread. 5. Enable the public clone3 wrapper in the future after it has been added to all targets. NB: Sandbox will return ENOSYS on clone3 in both Chromium: The following revision refers to this bug: https://chromium.googlesource.com/chromium/src/+/218438259dd795456f0a48f67cbe5b4e520db88b commit 218438259dd795456f0a48f67cbe5b4e520db88b Author: Matthew Denton <mpdenton@chromium.org> Date: Thu Jun 03 20:06:13 2021 Linux sandbox: return ENOSYS for clone3 Because clone3 uses a pointer argument rather than a flags argument, we cannot examine the contents with seccomp, which is essential to preventing sandboxed processes from starting other processes. So, we won't be able to support clone3 in Chromium. This CL modifies the BPF policy to return ENOSYS for clone3 so glibc always uses the fallback to clone. Bug: 1213452 Change-Id: I7c7c585a319e0264eac5b1ebee1a45be2d782303 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2936184 Reviewed-by: Robert Sesek <rsesek@chromium.org> Commit-Queue: Matthew Denton <mpdenton@chromium.org> Cr-Commit-Position: refs/heads/master@{#888980} [modify] https://crrev.com/218438259dd795456f0a48f67cbe5b4e520db88b/sandbox/linux/seccomp-bpf-helpers/baseline_policy.cc and Firefox: https://hg.mozilla.org/integration/autoland/rev/ecb4011a0c76 Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Diffstat (limited to 'nptl/allocatestack.c')
-rw-r--r--nptl/allocatestack.c59
1 files changed, 5 insertions, 54 deletions
diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c
index 9be6c42..cfe37a3 100644
--- a/nptl/allocatestack.c
+++ b/nptl/allocatestack.c
@@ -33,47 +33,6 @@
#include <kernel-features.h>
#include <nptl-stack.h>
-#ifndef NEED_SEPARATE_REGISTER_STACK
-
-/* Most architectures have exactly one stack pointer. Some have more. */
-# define STACK_VARIABLES void *stackaddr = NULL
-
-/* How to pass the values to the 'create_thread' function. */
-# define STACK_VARIABLES_ARGS stackaddr
-
-/* How to declare function which gets there parameters. */
-# define STACK_VARIABLES_PARMS void *stackaddr
-
-/* How to declare allocate_stack. */
-# define ALLOCATE_STACK_PARMS void **stack
-
-/* This is how the function is called. We do it this way to allow
- other variants of the function to have more parameters. */
-# define ALLOCATE_STACK(attr, pd) allocate_stack (attr, pd, &stackaddr)
-
-#else
-
-/* We need two stacks. The kernel will place them but we have to tell
- the kernel about the size of the reserved address space. */
-# define STACK_VARIABLES void *stackaddr = NULL; size_t stacksize = 0
-
-/* How to pass the values to the 'create_thread' function. */
-# define STACK_VARIABLES_ARGS stackaddr, stacksize
-
-/* How to declare function which gets there parameters. */
-# define STACK_VARIABLES_PARMS void *stackaddr, size_t stacksize
-
-/* How to declare allocate_stack. */
-# define ALLOCATE_STACK_PARMS void **stack, size_t *stacksize
-
-/* This is how the function is called. We do it this way to allow
- other variants of the function to have more parameters. */
-# define ALLOCATE_STACK(attr, pd) \
- allocate_stack (attr, pd, &stackaddr, &stacksize)
-
-#endif
-
-
/* Default alignment of stack. */
#ifndef STACK_ALIGN
# define STACK_ALIGN __alignof__ (long double)
@@ -252,7 +211,7 @@ advise_stack_range (void *mem, size_t size, uintptr_t pd, size_t guardsize)
PDP must be non-NULL. */
static int
allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
- ALLOCATE_STACK_PARMS)
+ void **stack, size_t *stacksize)
{
struct pthread *pd;
size_t size;
@@ -603,25 +562,17 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
/* We place the thread descriptor at the end of the stack. */
*pdp = pd;
-#if _STACK_GROWS_DOWN
void *stacktop;
-# if TLS_TCB_AT_TP
+#if TLS_TCB_AT_TP
/* The stack begins before the TCB and the static TLS block. */
stacktop = ((char *) (pd + 1) - tls_static_size_for_stack);
-# elif TLS_DTV_AT_TP
+#elif TLS_DTV_AT_TP
stacktop = (char *) (pd - 1);
-# endif
+#endif
-# ifdef NEED_SEPARATE_REGISTER_STACK
+ *stacksize = stacktop - pd->stackblock;
*stack = pd->stackblock;
- *stacksize = stacktop - *stack;
-# else
- *stack = stacktop;
-# endif
-#else
- *stack = pd->stackblock;
-#endif
return 0;
}