From patchwork Mon May 30 17:49:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 54535 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 33147384BC01 for ; Mon, 30 May 2022 17:51:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 33147384BC01 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1653933089; bh=Uns1GgNkhRSL/wRXSoIbNobt/wx28sNnNj53nhxmOeU=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Tp1UwYxZlRbyj656bMGjeRnfrABQNoTh4jWwaFdX7bjg8k/iXF5V+8Q4PKjl/mSVi XExgP9BEjI37wwrAlUIn2sjoMPkfwhkPeTZNKBVwME7PNyVxfhnEWYF/IDUW/PmJuf K8YSKgNAqqYoXVZs+OiX2vhnyZfLTCACKjg1Jwdo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x234.google.com (mail-oi1-x234.google.com [IPv6:2607:f8b0:4864:20::234]) by sourceware.org (Postfix) with ESMTPS id 7538C384BC2C for ; Mon, 30 May 2022 17:49:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7538C384BC2C Received: by mail-oi1-x234.google.com with SMTP id t144so14725168oie.7 for ; Mon, 30 May 2022 10:49:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Uns1GgNkhRSL/wRXSoIbNobt/wx28sNnNj53nhxmOeU=; b=YGkqGesb7PGc23AExQe4KlyFaKMd9HxoNME2tjNjz4ldgq3MY/ZJlV4mFaAXLHCntq Walyv/NfMv6DjwS9UmiQ2gKKq3y3jmKUDXgCoCdpwIxK1mu+IJbveRcB/Wn/BnRXKP2F td6oRCWiIIaOj97EKsfILOBa5xzFfb+ScQxoFpn13/kp54gAJumzraCh1/lp+jUjkjQ7 cH3Uco1B9ff0m8ZhidGXxgqyesU9rmqgHkQ25nfXekfIeeZZ8O6UWzxWGTVf+GivR58w I2hnVC3Wpj6ke3H1xf8YRN1TuLqI/bx4agK4vm2hnLk5CLh7zddOqMzR1MIagClP9NG0 vNhQ== X-Gm-Message-State: AOAM532U+LevwFwsH7GH0deGZG7LSLQTW7/S7XbK8A+kfwHd5xcB67Yn /xsUfFihAeg4tXeTSd8qwDedeJyBshbPwg== X-Google-Smtp-Source: ABdhPJzQHRvMPu2tGf/ukR3lLHWbXgWFKHKUl+CW2Vrv7mXB/5DcWPeBIc6OFxToqDO2OrJGk8RclQ== X-Received: by 2002:a05:6808:2219:b0:32b:111a:ac1d with SMTP id bd25-20020a056808221900b0032b111aac1dmr10095342oib.246.1653932965194; Mon, 30 May 2022 10:49:25 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:9f85:32e5:37a2:c438:5101]) by smtp.gmail.com with ESMTPSA id a19-20020a544e13000000b0032af3cffac7sm4903227oiy.2.2022.05.30.10.49.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 May 2022 10:49:24 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v5 2/2] linux: Add fallback for clone failure on posix_spawn (BZ #29115) Date: Mon, 30 May 2022 14:49:18 -0300 Message-Id: <20220530174918.3056804-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220530174918.3056804-1-adhemerval.zanella@linaro.org> References: <20220530174918.3056804-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Linux clone with CLONE_VM may fail for some namespace restriction (for instance if kernel does not allow processes in different time namespaces to share the sameaddress space). In this case clone fails with EINVAL and posix_spawn can not spawn a new process. However the same process can be spawned with fork and exec. The patch fixes by retrying the clone syscall with just CLONE_VFORK if clone fails with a non transient failure (ENOMEM and EAGAIN still returns failure to caller). Error on preparation phase or execve is returned by through a shared memory for the case of only CLONE_VFORK. Failure on prepare phase in helper process does not trigger the fork and exec fallback. Some spawn tests were adapted to run inside a created time namespace. Checked on x86_64-linux-gnu. --- posix/Makefile | 16 +++- posix/tst-spawn-chdir-timens.c | 2 + posix/tst-spawn-chdir.c | 7 ++ posix/tst-spawn-timens.c | 2 + posix/tst-spawn.c | 6 ++ posix/tst-spawn2-timens.c | 2 + posix/tst-spawn2.c | 8 ++ posix/tst-spawn4-timens.c | 2 + posix/tst-spawn4.c | 10 ++- posix/tst-spawn5-timens.c | 2 + posix/tst-spawn5.c | 10 ++- posix/tst-spawn6-timens.c | 2 + posix/tst-spawn6.c | 12 ++- sysdeps/unix/sysv/linux/spawni.c | 141 +++++++++++++++++++++---------- 14 files changed, 171 insertions(+), 51 deletions(-) create mode 100644 posix/tst-spawn-chdir-timens.c create mode 100644 posix/tst-spawn-timens.c create mode 100644 posix/tst-spawn2-timens.c create mode 100644 posix/tst-spawn4-timens.c create mode 100644 posix/tst-spawn5-timens.c create mode 100644 posix/tst-spawn6-timens.c diff --git a/posix/Makefile b/posix/Makefile index cfebb8ef06..e21abbe637 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -109,7 +109,9 @@ tests := test-errno tstgetopt testfnm runtests runptests \ tst-glob-tilde test-ssize-max tst-spawn4 bug-regex37 \ bug-regex38 tst-regcomp-truncated tst-spawn-chdir \ tst-wordexp-nocmd tst-execveat tst-spawn5 \ - tst-sched_getaffinity tst-spawn6 + tst-sched_getaffinity tst-spawn6 \ + tst-spawn-chdir-timens tst-spawn4-timens tst-spawn5-timens \ + tst-spawn6-timens # Test for the glob symbol version that was replaced in glibc 2.27. ifeq ($(have-GLIBC_2.26)$(build-shared),yesyes) @@ -130,7 +132,15 @@ xtests := tst-getaddrinfo4 tst-getaddrinfo5 tst-sched_rr_get_interval xtests-time64 := tst-sched_rr_get_interval-time64 ifeq (yes,$(build-shared)) test-srcs := globtest -tests += wordexp-test tst-exec tst-spawn tst-spawn2 tst-spawn3 +tests += \ + tst-exec \ + tst-spawn \ + tst-spawn-timens \ + tst-spawn2 \ + tst-spawn2-timens \ + tst-spawn3 \ + wordexp-test \ + # tests endif ifeq (yesyes,$(build-shared)$(have-thread-library)) tests += tst-getopt-cancel tst-_Fork @@ -290,7 +300,9 @@ tst-execvpe5-ARGS = -- $(host-test-program-cmd) tst-spawn-ARGS = -- $(host-test-program-cmd) tst-spawn-static-ARGS = $(tst-spawn-ARGS) tst-spawn5-ARGS = -- $(host-test-program-cmd) +tst-spawn5-timens-ARGS = -- $(host-test-program-cmd) tst-spawn6-ARGS = -- $(host-test-program-cmd) +tst-spawn6-timens-ARGS = -- $(host-test-program-cmd) tst-dir-ARGS = `pwd` `cd $(common-objdir)/$(subdir); pwd` `cd $(common-objdir); pwd` $(objpfx)tst-dir tst-chmod-ARGS = $(objdir) tst-vfork3-ARGS = --test-dir=$(objpfx) diff --git a/posix/tst-spawn-chdir-timens.c b/posix/tst-spawn-chdir-timens.c new file mode 100644 index 0000000000..a659e3201e --- /dev/null +++ b/posix/tst-spawn-chdir-timens.c @@ -0,0 +1,2 @@ +#define CHECK_TIMENAMESPACE +#include "tst-spawn-chdir.c" diff --git a/posix/tst-spawn-chdir.c b/posix/tst-spawn-chdir.c index 18e90c2957..cadcf6ddc2 100644 --- a/posix/tst-spawn-chdir.c +++ b/posix/tst-spawn-chdir.c @@ -29,6 +29,7 @@ #include #include #include +#include #include /* Reads the file at PATH, which must consist of exactly one line. @@ -87,6 +88,12 @@ add_chdir (posix_spawn_file_actions_t *actions, const char *path, static int do_test (void) { +#ifdef CHECK_TIMENAMESPACE + support_become_root(); + if (!support_enter_time_namespace ()) + return EXIT_UNSUPPORTED; +#endif + /* Directory for temporary file data. Each subtest uses a numeric subdirectory. */ char *directory = support_create_temp_directory ("tst-spawn-chdir-"); diff --git a/posix/tst-spawn-timens.c b/posix/tst-spawn-timens.c new file mode 100644 index 0000000000..a659e3201e --- /dev/null +++ b/posix/tst-spawn-timens.c @@ -0,0 +1,2 @@ +#define CHECK_TIMENAMESPACE +#include "tst-spawn-chdir.c" diff --git a/posix/tst-spawn.c b/posix/tst-spawn.c index e378c72f1b..2007384a90 100644 --- a/posix/tst-spawn.c +++ b/posix/tst-spawn.c @@ -178,6 +178,12 @@ do_test (int argc, char *argv[]) return handle_restart (argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]); +#ifdef CHECK_TIMENAMESPACE + support_become_root(); + if (!support_enter_time_namespace ()) + return EXIT_UNSUPPORTED; +#endif + /* Prepare the test. We are creating four files: two which file descriptor will be marked with FD_CLOEXEC, another which is not. */ diff --git a/posix/tst-spawn2-timens.c b/posix/tst-spawn2-timens.c new file mode 100644 index 0000000000..612531fc86 --- /dev/null +++ b/posix/tst-spawn2-timens.c @@ -0,0 +1,2 @@ +#define CHECK_TIMENAMESPACE +#include "tst-spawn2.c" diff --git a/posix/tst-spawn2.c b/posix/tst-spawn2.c index 45ff17f9a7..16fcbe4295 100644 --- a/posix/tst-spawn2.c +++ b/posix/tst-spawn2.c @@ -26,10 +26,18 @@ #include #include +#include +#include int do_test (void) { +#ifdef CHECK_TIMENAMESPACE + support_become_root(); + if (!support_enter_time_namespace ()) + return EXIT_UNSUPPORTED; +#endif + /* Check if posix_spawn correctly returns an error and an invalid pid by trying to spawn an invalid binary. */ diff --git a/posix/tst-spawn4-timens.c b/posix/tst-spawn4-timens.c new file mode 100644 index 0000000000..3f4e8ea089 --- /dev/null +++ b/posix/tst-spawn4-timens.c @@ -0,0 +1,2 @@ +#define CHECK_TIMENAMESPACE +#include "tst-spawn4.c" diff --git a/posix/tst-spawn4.c b/posix/tst-spawn4.c index 9eac2d1080..29b7a6215d 100644 --- a/posix/tst-spawn4.c +++ b/posix/tst-spawn4.c @@ -21,13 +21,21 @@ #include #include -#include #include +#include #include +#include +#include static int do_test (void) { +#ifdef CHECK_TIMENAMESPACE + support_become_root(); + if (!support_enter_time_namespace ()) + return EXIT_UNSUPPORTED; +#endif + char *scriptname; int fd = create_temp_file ("tst-spawn4.", &scriptname); TEST_VERIFY_EXIT (fd >= 0); diff --git a/posix/tst-spawn5-timens.c b/posix/tst-spawn5-timens.c new file mode 100644 index 0000000000..8d46819222 --- /dev/null +++ b/posix/tst-spawn5-timens.c @@ -0,0 +1,2 @@ +#define CHECK_TIMENAMESPACE +#include "tst-spawn5.c" diff --git a/posix/tst-spawn5.c b/posix/tst-spawn5.c index dd5b3f9357..045ab15358 100644 --- a/posix/tst-spawn5.c +++ b/posix/tst-spawn5.c @@ -28,8 +28,10 @@ #include #include -#include +#include #include +#include +#include #include #include @@ -278,6 +280,12 @@ do_test (int argc, char *argv[]) /* Ignore the application name. */ handle_restart (argc - 1, &argv[1]); +#ifdef CHECK_TIMENAMESPACE + support_become_root(); + if (!support_enter_time_namespace ()) + return EXIT_UNSUPPORTED; +#endif + TEST_VERIFY_EXIT (argc == 2 || argc == 5); int i; diff --git a/posix/tst-spawn6-timens.c b/posix/tst-spawn6-timens.c new file mode 100644 index 0000000000..a23d068ce5 --- /dev/null +++ b/posix/tst-spawn6-timens.c @@ -0,0 +1,2 @@ +#define CHECK_TIMENAMESPACE +#include "tst-spawn6.c" diff --git a/posix/tst-spawn6.c b/posix/tst-spawn6.c index 044abd8535..4c55a1c110 100644 --- a/posix/tst-spawn6.c +++ b/posix/tst-spawn6.c @@ -25,12 +25,14 @@ #include #include #include +#include #include #include +#include +#include #include -#include #include -#include +#include #include #ifndef PATH_MAX @@ -124,6 +126,12 @@ run_subprogram (int argc, char *argv[], const posix_spawnattr_t *attr, static int run_test (int argc, char *argv[]) { +#ifdef CHECK_TIMENAMESPACE + support_become_root(); + if (!support_enter_time_namespace ()) + return EXIT_UNSUPPORTED; +#endif + /* We must have either: - four parameters left if called initially: + path to ld.so optional diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c index d6f5ca89cd..b57db64698 100644 --- a/sysdeps/unix/sysv/linux/spawni.c +++ b/sysdeps/unix/sysv/linux/spawni.c @@ -44,7 +44,12 @@ third issue is done by a stack allocation in parent, and by using a field in struct spawn_args where the child can write an error code. CLONE_VFORK ensures that the parent does not run until the - child has either exec'ed successfully or exited. */ + child has either exec'ed successfully or exited. + + If the clone with CLONE_VM and CLONE_VFORK fails (due any kernel limitation + such as time namespace), only CLONE_VFORK is used instead and the + preparation and execve failures are communicated through a shared + memory. */ /* The Unix standard contains a long explanation of the way to signal @@ -67,6 +72,7 @@ struct posix_spawn_args char *const *envp; int xflags; int err; + int *errp; }; /* Older version requires that shell script without shebang definition @@ -97,8 +103,8 @@ maybe_script_execute (struct posix_spawn_args *args) /* Function used in the clone call to setup the signals mask, posix_spawn attributes, and file actions. It run on its own stack (provided by the posix_spawn call). */ -static int -__spawni_child (void *arguments) +static _Noreturn int +spawni_child (void *arguments) { struct posix_spawn_args *args = arguments; const posix_spawnattr_t *restrict attr = args->attr; @@ -300,10 +306,91 @@ fail: (EINTERNALBUG) describing that, use ECHILD. Another option would be to set args->err to some negative sentinel and have the parent abort(), but that seems needlessly harsh. */ - args->err = errno ? : ECHILD; + *args->errp = errno ? : ECHILD; + _exit (SPAWN_ERROR); } +/* Spawn a new process using clone with FLAGS, STACK, and STACK_SIZE and + return TRUE if the helper was created or if the failure was not due + resource exhaustion. */ +static bool +spawni_clone (struct posix_spawn_args *args, pid_t *new_pid, int *ec, + int flags, void *stack, size_t stack_size) +{ + /* The clone flags used will create a new child that will run in the same + memory space (CLONE_VM) and the execution of calling thread will be + suspend until the child calls execve or _exit. + + Also since the calling thread execution will be suspend, there is not + need for CLONE_SETTLS. Although parent and child share the same TLS + namespace, there will be no concurrent access for TLS variables (errno + for instance). */ + struct clone_args clone_args = + { + .flags = flags, + .exit_signal = SIGCHLD, + .stack = (uintptr_t) stack, + .stack_size = stack_size, + }; + *new_pid = __clone_internal (&clone_args, spawni_child, args); + + /* It needs to collect the case where the auxiliary process was created + but failed to execute the file (due either any preparation step or + for execve itself). */ + if (*new_pid > 0) + { + /* Also, it handles the unlikely case where the auxiliary process was + terminated before calling execve as if it was successfully. The + args.err is set to 0 as default and changed to a positive value + only in case of failure, so in case of premature termination + due a signal args.err will remain zeroed and it will be up to + caller to actually collect it. */ + *ec = *args->errp; + if (*ec > 0) + /* There still an unlikely case where the child is cancelled after + setting args.err, due to a positive error value. Also there is + possible pid reuse race (where the kernel allocated the same pid + to an unrelated process). Unfortunately due synchronization + issues where the kernel might not have the process collected + the waitpid below can not use WNOHANG. */ + __waitpid (*new_pid, NULL, 0); + } + else + *ec = errno; + + /* There is no much point in retrying with fork and exec if kernel returns a + failure due resource exhaustion. */ + return *new_pid > 0 || (errno == ENOMEM || errno == EAGAIN); +} + +static bool +spawni_clone_vfork_vm (struct posix_spawn_args *args, pid_t *new_pid, int *ec, + void *stack, size_t stack_size) +{ + args->errp = &args->err; + bool r = spawni_clone (args, new_pid, ec, CLONE_VM | CLONE_VFORK, + stack, stack_size); + return r; +} + +static void +spawni_clone_vfork (struct posix_spawn_args *args, pid_t *new_pid, int *ec, + void *stack, size_t stack_size) +{ + int *errmap = __mmap (NULL, sizeof (int), PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, -1, 0); + if (__glibc_unlikely (errmap == MAP_FAILED)) + { + *ec = errno; + return; + } + + args->errp = errmap; + spawni_clone (args, new_pid, ec, CLONE_VFORK, stack, stack_size); + __munmap (errmap, sizeof (int)); +} + /* Spawn a new process executing PATH with the attributes describes in *ATTRP. Before running the process perform the actions described in FILE-ACTIONS. */ static int @@ -370,47 +457,11 @@ __spawnix (pid_t * pid, const char *file, __libc_signal_block_all (&args.oldmask); - /* The clone flags used will create a new child that will run in the same - memory space (CLONE_VM) and the execution of calling thread will be - suspend until the child calls execve or _exit. - - Also since the calling thread execution will be suspend, there is not - need for CLONE_SETTLS. Although parent and child share the same TLS - namespace, there will be no concurrent access for TLS variables (errno - for instance). */ - struct clone_args clone_args = - { - .flags = CLONE_VM | CLONE_VFORK, - .exit_signal = SIGCHLD, - .stack = (uintptr_t) stack, - .stack_size = stack_size, - }; - new_pid = __clone_internal (&clone_args, __spawni_child, &args); - - /* It needs to collect the case where the auxiliary process was created - but failed to execute the file (due either any preparation step or - for execve itself). */ - if (new_pid > 0) - { - /* Also, it handles the unlikely case where the auxiliary process was - terminated before calling execve as if it was successfully. The - args.err is set to 0 as default and changed to a positive value - only in case of failure, so in case of premature termination - due a signal args.err will remain zeroed and it will be up to - caller to actually collect it. */ - ec = args.err; - if (ec > 0) - /* There still an unlikely case where the child is cancelled after - setting args.err, due to a positive error value. Also there is - possible pid reuse race (where the kernel allocated the same pid - to an unrelated process). Unfortunately due synchronization - issues where the kernel might not have the process collected - the waitpid below can not use WNOHANG. */ - __waitpid (new_pid, NULL, 0); - } - else - ec = errno; - + /* First try with CLONE_VM | CLONE_VFORK with errors being communicate through + ARGS. If clone fails, only CLONE_VFORK is used and error status is passed + through a MAP_SHARED memory. */ + if (!spawni_clone_vfork_vm (&args, &new_pid, &ec, stack, stack_size)) + spawni_clone_vfork (&args, &new_pid, &ec, stack, stack_size); __munmap (stack, stack_size); if ((ec == 0) && (pid != NULL))