From patchwork Wed May 5 21:41:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 43264 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 376B8384F011; Wed, 5 May 2021 21:42:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 376B8384F011 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1620250921; bh=pxZxHwY/MHLZYpjGY30YX/1MRHYwYr36HFhIcujshJk=; 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=X8aL8sSAkxcabScvw8dAssiVg/Gmyl6k5+OI1qZBBZUUj0DSQlpLZ+TLsJzPucKDc jUjKANjhyBvPBsKA2PBtTBgRV5e9WQvkGiKyBXdfOt60Q+0LDqxeKnvMQ4alcNOUnE jVdfRwVwAkx8qdIelkaYlwwv9S2d0bYoYHotVl7o= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x733.google.com (mail-qk1-x733.google.com [IPv6:2607:f8b0:4864:20::733]) by sourceware.org (Postfix) with ESMTPS id 4D03238618F3 for ; Wed, 5 May 2021 21:41:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 4D03238618F3 Received: by mail-qk1-x733.google.com with SMTP id o27so3095705qkj.9 for ; Wed, 05 May 2021 14:41:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pxZxHwY/MHLZYpjGY30YX/1MRHYwYr36HFhIcujshJk=; b=O6Vc7lRyJ3mfQiRQQG8GkKNxW6OhT2D/OBy7gYNR/KfJR7Vv1hmFkuJrBL91fPRiwi xnoitf2rkZLZY/trrvV/avesA3ol+Qi83nsiuO9Ghp6KrdkZW/+pXMfv5GAG8H3EoFLY dqkZW78SCTOoX5wS+z6yhSqvZOZUenAi/gs7jL/H6vNEtWD9SmjV8FpUonLDvuyO7qf1 k5hk42Sy+pn7HmxOupoL+2fMa9fv0mMPAYdqYFHt+08IWwrVu2sZtWAv3iT8OK1NguEO IUxvfeyQJqUdNoBazdm4Z47C687K+ueAJi5/obNPcuI4ngVKdFPRxW+vY+8KnKQ3tmKV UaeA== X-Gm-Message-State: AOAM532jNTxnDEymcaEF+FN7Uso6M5UHXgl36oAisIrbDDb8KSoz7Rma W4F0+l243EgCsZB6yQ3LBMU/p+V89Q9drQ== X-Google-Smtp-Source: ABdhPJzF5/albIxepDsf21Z/UPxVn7JyWF0hk2FBoVqMqI3fUu7MisFfmVdsy17SzAELgkCq41UiFw== X-Received: by 2002:ae9:ed54:: with SMTP id c81mr862666qkg.251.1620250913383; Wed, 05 May 2021 14:41:53 -0700 (PDT) Received: from birita.. ([177.194.37.86]) by smtp.googlemail.com with ESMTPSA id t9sm466146qkg.121.2021.05.05.14.41.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 May 2021 14:41:52 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v5 5/5] posix: Add posix_spawn_file_actions_closefrom_np Date: Wed, 5 May 2021 18:41:43 -0300 Message-Id: <20210505214143.3649585-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210505214143.3649585-1-adhemerval.zanella@linaro.org> References: <20210505214143.3649585-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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@sourceware.org Sender: "Libc-alpha" Changes from v4: * Rebased against master. --- This patch adds a way to close a range of file descriptors on posix_spawn as a new file action. The API is similar to the one provided by Solaris 11 [1], where the file action causes the all open file descriptors greater than or equal to input on to be closed when the new process is spawned. The function posix_spawn_file_actions_closefrom_np is safe to be implemented by iterating over /proc/self/fd, since the Linux spawni.c helper process does not use CLONE_FILES, so its has own file descriptor table and any failure (in /proc operation) aborts the process creation and returns an error to the caller. I am aware that this file action might be redundant to the current approach of POSIX in promoting O_CLOEXEC in more interfaces. However O_CLOEXEC is still not the default and for some specific usages, the caller needs to close all possible file descriptors to avoid them leaking. Some examples are CPython (discussed in BZ#10353) and OpenJDK jspawnhelper [2] (where OpenJDK spawns a helper process to exactly closes all file descriptors). Most likely any environment which calls functions that might open file descriptor under the hood and aim to use posix_spawn might face the same requirement. Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu, and aarch64-linux-gnu. [1] https://docs.oracle.com/cd/E36784_01/html/E36874/posix-spawn-file-actions-addclosefrom-np-3c.html [2] https://github.com/openjdk/jdk/blob/master/src/java.base/unix/native/libjava/childproc.c#L82 --- NEWS | 5 + include/unistd.h | 2 +- posix/Makefile | 4 +- posix/Versions | 1 + posix/spawn.h | 8 + posix/spawn_faction_addclosefrom.c | 57 ++++ posix/spawn_faction_destroy.c | 1 + posix/spawn_int.h | 6 + posix/tst-spawn5.c | 284 ++++++++++++++++++ sysdeps/generic/spawn_int_def.h | 24 ++ sysdeps/mach/hurd/i386/libc.abilist | 1 + sysdeps/mach/hurd/spawni.c | 4 + sysdeps/posix/spawni.c | 4 + sysdeps/unix/sysv/linux/aarch64/libc.abilist | 1 + sysdeps/unix/sysv/linux/alpha/libc.abilist | 1 + sysdeps/unix/sysv/linux/arc/libc.abilist | 1 + sysdeps/unix/sysv/linux/arm/be/libc.abilist | 1 + sysdeps/unix/sysv/linux/arm/le/libc.abilist | 1 + sysdeps/unix/sysv/linux/closefrom.c | 3 +- sysdeps/unix/sysv/linux/closefrom_fallback.c | 8 +- sysdeps/unix/sysv/linux/csky/libc.abilist | 1 + sysdeps/unix/sysv/linux/hppa/libc.abilist | 1 + sysdeps/unix/sysv/linux/i386/libc.abilist | 1 + sysdeps/unix/sysv/linux/ia64/libc.abilist | 1 + .../sysv/linux/m68k/coldfire/libc.abilist | 1 + .../unix/sysv/linux/m68k/m680x0/libc.abilist | 1 + .../sysv/linux/microblaze/be/libc.abilist | 1 + .../sysv/linux/microblaze/le/libc.abilist | 1 + .../sysv/linux/mips/mips32/fpu/libc.abilist | 1 + .../sysv/linux/mips/mips32/nofpu/libc.abilist | 1 + .../sysv/linux/mips/mips64/n32/libc.abilist | 1 + .../sysv/linux/mips/mips64/n64/libc.abilist | 1 + sysdeps/unix/sysv/linux/nios2/libc.abilist | 1 + .../linux/powerpc/powerpc32/fpu/libc.abilist | 1 + .../powerpc/powerpc32/nofpu/libc.abilist | 1 + .../linux/powerpc/powerpc64/be/libc.abilist | 1 + .../linux/powerpc/powerpc64/le/libc.abilist | 1 + .../unix/sysv/linux/riscv/rv32/libc.abilist | 1 + .../unix/sysv/linux/riscv/rv64/libc.abilist | 1 + .../unix/sysv/linux/s390/s390-32/libc.abilist | 1 + .../unix/sysv/linux/s390/s390-64/libc.abilist | 1 + sysdeps/unix/sysv/linux/sh/be/libc.abilist | 1 + sysdeps/unix/sysv/linux/sh/le/libc.abilist | 1 + .../sysv/linux/sparc/sparc32/libc.abilist | 1 + .../sysv/linux/sparc/sparc64/libc.abilist | 1 + sysdeps/unix/sysv/linux/spawn_int_def.h | 25 ++ sysdeps/unix/sysv/linux/spawni.c | 34 ++- .../unix/sysv/linux/x86_64/64/libc.abilist | 1 + .../unix/sysv/linux/x86_64/x32/libc.abilist | 1 + 49 files changed, 482 insertions(+), 21 deletions(-) create mode 100644 posix/spawn_faction_addclosefrom.c create mode 100644 posix/tst-spawn5.c create mode 100644 sysdeps/generic/spawn_int_def.h create mode 100644 sysdeps/unix/sysv/linux/spawn_int_def.h diff --git a/NEWS b/NEWS index d4506f5f4b..9cabcf9e51 100644 --- a/NEWS +++ b/NEWS @@ -30,6 +30,11 @@ Major new features: greater than given integer. This function is a GNU extension, although it also present in other systems. +* The posix_spawn_file_actions_closefrom_np function has been added, enabling + posix_spawn and posix_spawnp to close all file descriptors great than or + equal to a giver integer. This function is a GNU extension, although + Solaris also provides a similar function. + Deprecated and removed features, and other changes affecting compatibility: * The function pthread_mutex_consistent_np has been deprecated; programs diff --git a/include/unistd.h b/include/unistd.h index c596c6fb24..e3dc964f2f 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -152,7 +152,7 @@ extern int __brk (void *__addr) attribute_hidden; extern int __close (int __fd); libc_hidden_proto (__close) extern int __libc_close (int __fd); -extern _Bool __closefrom_fallback (int __lowfd) attribute_hidden; +extern _Bool __closefrom_fallback (int __lowfd, _Bool) attribute_hidden; extern ssize_t __read (int __fd, void *__buf, size_t __nbytes); libc_hidden_proto (__read) extern ssize_t __write (int __fd, const void *__buf, size_t __n); diff --git a/posix/Makefile b/posix/Makefile index fa0dc0ea20..1e3d8d5f63 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -57,6 +57,7 @@ routines := \ spawn_faction_init spawn_faction_destroy spawn_faction_addclose \ spawn_faction_addopen spawn_faction_adddup2 spawn_valid_fd \ spawn_faction_addchdir spawn_faction_addfchdir \ + spawn_faction_addclosefrom \ spawnattr_init spawnattr_destroy \ spawnattr_getdefault spawnattr_setdefault \ spawnattr_getflags spawnattr_setflags \ @@ -104,7 +105,7 @@ tests := test-errno tstgetopt testfnm runtests runptests \ tst-sysconf-empty-chroot tst-glob_symlinks tst-fexecve \ tst-glob-tilde test-ssize-max tst-spawn4 bug-regex37 \ bug-regex38 tst-regcomp-truncated tst-spawn-chdir \ - tst-wordexp-nocmd tst-execveat + tst-wordexp-nocmd tst-execveat tst-spawn5 # Test for the glob symbol version that was replaced in glibc 2.27. ifeq ($(have-GLIBC_2.26)$(build-shared),yesyes) @@ -271,6 +272,7 @@ tst-exec-static-ARGS = $(tst-exec-ARGS) 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-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/Versions b/posix/Versions index b77e251e00..14f2ee0a2d 100644 --- a/posix/Versions +++ b/posix/Versions @@ -149,6 +149,7 @@ libc { } GLIBC_2.34 { execveat; + posix_spawn_file_actions_addclosefrom_np; } GLIBC_PRIVATE { __libc_fork; __libc_pread; __libc_pwrite; diff --git a/posix/spawn.h b/posix/spawn.h index a29da028cc..990d8a6ba2 100644 --- a/posix/spawn.h +++ b/posix/spawn.h @@ -213,6 +213,14 @@ extern int posix_spawn_file_actions_addchdir_np (posix_spawn_file_actions_t * extern int posix_spawn_file_actions_addfchdir_np (posix_spawn_file_actions_t *, int __fd) __THROW __nonnull ((1)); + +/* Add an action to close all file descriptor greater than or equal to FROM + during spawn. This affects the subsequent file actions. */ +extern int +posix_spawn_file_actions_addclosefrom_np (posix_spawn_file_actions_t *, + int __from) + __THROW __nonnull ((1)); + #endif __END_DECLS diff --git a/posix/spawn_faction_addclosefrom.c b/posix/spawn_faction_addclosefrom.c new file mode 100644 index 0000000000..ff35b0519b --- /dev/null +++ b/posix/spawn_faction_addclosefrom.c @@ -0,0 +1,57 @@ +/* Add a closefrom to a file action list for posix_spawn. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +int +__posix_spawn_file_actions_addclosefrom (posix_spawn_file_actions_t + *file_actions, int from) +{ +#if __SPAWN_SUPPORT_CLOSEFROM + struct __spawn_action *rec; + + if (!__spawn_valid_fd (from)) + return EBADF; + + /* Allocate more memory if needed. */ + if (file_actions->__used == file_actions->__allocated + && __posix_spawn_file_actions_realloc (file_actions) != 0) + /* This can only mean we ran out of memory. */ + return ENOMEM; + + /* Add the new value. */ + rec = &file_actions->__actions[file_actions->__used]; + rec->tag = spawn_do_closefrom; + rec->action.closefrom_action.from = from; + + /* Account for the new entry. */ + ++file_actions->__used; + + return 0; +#else + return EINVAL; +#endif +} +weak_alias (__posix_spawn_file_actions_addclosefrom, + posix_spawn_file_actions_addclosefrom_np) +#if !__SPAWN_SUPPORT_CLOSEFROM +stub_warning (posix_spawn_file_actions_addclosefrom_np) +#endif diff --git a/posix/spawn_faction_destroy.c b/posix/spawn_faction_destroy.c index 7776f1a462..1a01b8e80e 100644 --- a/posix/spawn_faction_destroy.c +++ b/posix/spawn_faction_destroy.c @@ -39,6 +39,7 @@ __posix_spawn_file_actions_destroy (posix_spawn_file_actions_t *file_actions) case spawn_do_close: case spawn_do_dup2: case spawn_do_fchdir: + case spawn_do_closefrom: /* No cleanup required. */ break; } diff --git a/posix/spawn_int.h b/posix/spawn_int.h index c579cb981d..81d43f2fa3 100644 --- a/posix/spawn_int.h +++ b/posix/spawn_int.h @@ -20,6 +20,7 @@ #define _SPAWN_INT_H #include +#include #include /* Data structure to contain the action information. */ @@ -32,6 +33,7 @@ struct __spawn_action spawn_do_open, spawn_do_chdir, spawn_do_fchdir, + spawn_do_closefrom, } tag; union @@ -60,6 +62,10 @@ struct __spawn_action { int fd; } fchdir_action; + struct + { + int from; + } closefrom_action; } action; }; diff --git a/posix/tst-spawn5.c b/posix/tst-spawn5.c new file mode 100644 index 0000000000..1e67d071b4 --- /dev/null +++ b/posix/tst-spawn5.c @@ -0,0 +1,284 @@ +/* Tests for posix_spawn signal handling. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +/* Nonzero if the program gets called via `exec'. */ +static int restart; + +/* Hold the four initial argument used to respawn the process, plus + the extra '--direct' and '--restart', and a final NULL. */ +static char *initial_argv[7]; + +#define CMDLINE_OPTIONS \ + { "restart", no_argument, &restart, 1 }, + +#define NFDS 100 + +static int +open_multiple_temp_files (void) +{ + /* Check if the temporary file descriptor has no no gaps. */ + int lowfd = xopen ("/dev/null", O_RDONLY, 0600); + for (int i = 1; i <= NFDS; i++) + TEST_COMPARE (xopen ("/dev/null", O_RDONLY, 0600), + lowfd + i); + return lowfd; +} + +/* Called on process re-execution. The arguments are the expected opened + file descriptors. */ +_Noreturn static void +handle_restart (int argc, char *argv[]) +{ + size_t nfds = argc > 1 ? argc - 1 : 0; + struct fd_t + { + int fd; + _Bool found; + } *fds = xmalloc (sizeof (struct fd_t) * nfds); + for (int i = 0; i < nfds; i++) + { + char *endptr; + long unsigned int fd = strtoul (argv[i+1], &endptr, 10); + if (*endptr != '\0' || fd > INT_MAX) + FAIL_EXIT1 ("readdir: invalid file descriptor value: %s", argv[i]); + + fds[i].fd = fd; + fds[i].found = false; + } + + DIR *dirp = opendir (FD_TO_FILENAME_PREFIX); + if (dirp == NULL) + FAIL_EXIT1 ("opendir (\"" FD_TO_FILENAME_PREFIX "\"): %m"); + + while (true) + { + errno = 0; + struct dirent64 *e = readdir64 (dirp); + if (e == NULL) + { + if (errno != 0) + FAIL_EXIT1 ("readdir: %m"); + break; + } + + if (e->d_name[0] == '.') + continue; + + char *endptr; + long int fd = strtol (e->d_name, &endptr, 10); + if (*endptr != '\0' || fd < 0 || fd > INT_MAX) + FAIL_EXIT1 ("readdir: invalid file descriptor name: /proc/self/fd/%s", + e->d_name); + + /* Skip the descriptor which is used to enumerate the descriptors. */ + if (fd == dirfd (dirp) + || fd == STDIN_FILENO + || fd == STDOUT_FILENO + || fd == STDERR_FILENO) + continue; + + bool found = false; + for (int i = 0; i < nfds; i++) + if (fds[i].fd == fd) + fds[i].found = found = true; + + if (!found) + FAIL_EXIT1 ("unexpected open file descriptor: %ld", fd); + } + closedir (dirp); + + for (int i = 0; i < nfds; i++) + if (!fds[i].found) + FAIL_EXIT1 ("file descriptor %d not opened", fds[i].fd); + + free (fds); + + exit (EXIT_SUCCESS); +} + +static void +spawn_closefrom_test (posix_spawn_file_actions_t *fa, int lowfd, int highfd, + int *extrafds, size_t nextrafds) +{ + /* 6 elements from initial_argv (path to ld.so, '--library-path', the + path', application name', '--direct', and '--restart'), up to + 2 * maximum_fd arguments (the expected open file descriptors), plus + NULL. */ + enum { argv_size = array_length (initial_argv) + 2 * NFDS + 1 }; + char *args[argv_size]; + int argc = 0; + + for (char **arg = initial_argv; *arg != NULL; arg++) + args[argc++] = *arg; + + for (int i = lowfd; i < highfd; i++) + args[argc++] = xasprintf ("%d", i); + + for (int i = 0; i < nextrafds; i++) + args[argc++] = xasprintf ("%d", extrafds[i]); + + args[argc] = NULL; + TEST_VERIFY (argc < argv_size); + + pid_t pid; + int status; + + TEST_COMPARE (posix_spawn (&pid, args[0], fa, NULL, args, environ), 0); + TEST_COMPARE (xwaitpid (pid, &status, 0), pid); + TEST_VERIFY (WIFEXITED (status)); + TEST_VERIFY (!WIFSIGNALED (status)); + TEST_COMPARE (WEXITSTATUS (status), 0); +} + +static void +do_test_closefrom (void) +{ + int lowfd = open_multiple_temp_files (); + const int half_fd = lowfd + NFDS / 2; + + /* Close half of the descriptors and check result. */ + { + posix_spawn_file_actions_t fa; + TEST_COMPARE (posix_spawn_file_actions_init (&fa), 0); + + int ret = posix_spawn_file_actions_addclosefrom_np (&fa, half_fd); + if (ret == EINVAL) + /* Hurd currently does not support closefrom fileaction. */ + FAIL_UNSUPPORTED ("posix_spawn_file_actions_addclosefrom_np unsupported"); + TEST_COMPARE (ret, 0); + + spawn_closefrom_test (&fa, lowfd, half_fd, NULL, 0); + + TEST_COMPARE (posix_spawn_file_actions_destroy (&fa), 0); + } + + /* Create some gaps, close up to a threshold, and check result. */ + xclose (lowfd + 57); + xclose (lowfd + 78); + xclose (lowfd + 81); + xclose (lowfd + 82); + xclose (lowfd + 84); + xclose (lowfd + 90); + + { + posix_spawn_file_actions_t fa; + TEST_COMPARE (posix_spawn_file_actions_init (&fa), 0); + + TEST_COMPARE (posix_spawn_file_actions_addclosefrom_np (&fa, half_fd), 0); + + spawn_closefrom_test (&fa, lowfd, half_fd, NULL, 0); + + TEST_COMPARE (posix_spawn_file_actions_destroy (&fa), 0); + } + + /* Close the remaining but the last one. */ + { + posix_spawn_file_actions_t fa; + TEST_COMPARE (posix_spawn_file_actions_init (&fa), 0); + + TEST_COMPARE (posix_spawn_file_actions_addclosefrom_np (&fa, lowfd + 1), 0); + + spawn_closefrom_test (&fa, lowfd, lowfd + 1, NULL, 0); + + TEST_COMPARE (posix_spawn_file_actions_destroy (&fa), 0); + } + + /* Close everything. */ + { + posix_spawn_file_actions_t fa; + TEST_COMPARE (posix_spawn_file_actions_init (&fa), 0); + + TEST_COMPARE (posix_spawn_file_actions_addclosefrom_np (&fa, lowfd), 0); + + spawn_closefrom_test (&fa, lowfd, lowfd, NULL, 0); + + TEST_COMPARE (posix_spawn_file_actions_destroy (&fa), 0); + } + + /* Close a range and add some file actions. */ + { + posix_spawn_file_actions_t fa; + TEST_COMPARE (posix_spawn_file_actions_init (&fa), 0); + + TEST_COMPARE (posix_spawn_file_actions_addclosefrom_np (&fa, lowfd + 1), 0); + TEST_COMPARE (posix_spawn_file_actions_addopen (&fa, lowfd, "/dev/null", + 0666, O_RDONLY), 0); + TEST_COMPARE (posix_spawn_file_actions_adddup2 (&fa, lowfd, lowfd + 1), 0); + TEST_COMPARE (posix_spawn_file_actions_addopen (&fa, lowfd, "/dev/null", + 0666, O_RDONLY), 0); + + spawn_closefrom_test (&fa, lowfd, lowfd, (int[]){lowfd, lowfd + 1}, 2); + + TEST_COMPARE (posix_spawn_file_actions_destroy (&fa), 0); + } +} + +static int +do_test (int argc, char *argv[]) +{ + /* We must have either: + + - one or four parameters if called initially: + + argv[1]: path for ld.so optional + + argv[2]: "--library-path" optional + + argv[3]: the library path optional + + argv[4]: the application name + + - six parameters left if called through re-execution: + + argv[1]: the application name + + argv[2]: first expected open file descriptor + + argv[n]: last expected open file descritptor + + * When built with --enable-hardcoded-path-in-tests or issued without + using the loader directly. */ + + if (restart) + handle_restart (argc, argv); + + initial_argv[0] = argv[1]; /* path for ld.so */ + initial_argv[1] = argv[2]; /* "--library-path" */ + initial_argv[2] = argv[3]; /* the library path */ + initial_argv[3] = argv[4]; /* the application name */ + initial_argv[4] = (char *) "--direct"; + initial_argv[5] = (char *) "--restart"; + + do_test_closefrom (); + + return 0; +} + +#define TEST_FUNCTION_ARGV do_test +#include diff --git a/sysdeps/generic/spawn_int_def.h b/sysdeps/generic/spawn_int_def.h new file mode 100644 index 0000000000..ef6e03b1ef --- /dev/null +++ b/sysdeps/generic/spawn_int_def.h @@ -0,0 +1,24 @@ +/* Internal definitions for posix_spawn functionality. Generic version. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _SPAWN_INT_DEF_H +#define _SPAWN_INT_DEF_H + +#define __SPAWN_SUPPORT_CLOSEFROM 0 + +#endif /* _SPAWN_INT_H */ diff --git a/sysdeps/mach/hurd/i386/libc.abilist b/sysdeps/mach/hurd/i386/libc.abilist index 52d9134a4e..968f87b13e 100644 --- a/sysdeps/mach/hurd/i386/libc.abilist +++ b/sysdeps/mach/hurd/i386/libc.abilist @@ -2209,6 +2209,7 @@ GLIBC_2.34 __libc_start_main F GLIBC_2.34 _hurd_libc_proc_init F GLIBC_2.34 closefrom F GLIBC_2.34 execveat F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/mach/hurd/spawni.c b/sysdeps/mach/hurd/spawni.c index b5c92365f2..e011c3b34b 100644 --- a/sysdeps/mach/hurd/spawni.c +++ b/sysdeps/mach/hurd/spawni.c @@ -613,6 +613,10 @@ retry: case spawn_do_fchdir: err = child_fchdir (action->action.fchdir_action.fd); break; + + case spawn_do_closefrom: + err = EINVAL; + break; } if (err) diff --git a/sysdeps/posix/spawni.c b/sysdeps/posix/spawni.c index a649788668..75dedaca56 100644 --- a/sysdeps/posix/spawni.c +++ b/sysdeps/posix/spawni.c @@ -231,6 +231,10 @@ __spawni_child (void *arguments) if (__fchdir (action->action.fchdir_action.fd) != 0) goto fail; break; + + case spawn_do_closefrom: + __set_errno (EINVAL); + goto fail; } } } diff --git a/sysdeps/unix/sysv/linux/aarch64/libc.abilist b/sysdeps/unix/sysv/linux/aarch64/libc.abilist index 35772d07da..dba1b22657 100644 --- a/sysdeps/unix/sysv/linux/aarch64/libc.abilist +++ b/sysdeps/unix/sysv/linux/aarch64/libc.abilist @@ -2307,6 +2307,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/alpha/libc.abilist b/sysdeps/unix/sysv/linux/alpha/libc.abilist index a51aff6089..bfb12440fa 100644 --- a/sysdeps/unix/sysv/linux/alpha/libc.abilist +++ b/sysdeps/unix/sysv/linux/alpha/libc.abilist @@ -2394,6 +2394,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/arc/libc.abilist b/sysdeps/unix/sysv/linux/arc/libc.abilist index 853caba0e3..4f4b9dab60 100644 --- a/sysdeps/unix/sysv/linux/arc/libc.abilist +++ b/sysdeps/unix/sysv/linux/arc/libc.abilist @@ -2066,6 +2066,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/arm/be/libc.abilist b/sysdeps/unix/sysv/linux/arm/be/libc.abilist index 14ddad655b..3721b15960 100644 --- a/sysdeps/unix/sysv/linux/arm/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/arm/be/libc.abilist @@ -203,6 +203,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/arm/le/libc.abilist b/sysdeps/unix/sysv/linux/arm/le/libc.abilist index 313401c458..62884d04b3 100644 --- a/sysdeps/unix/sysv/linux/arm/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/arm/le/libc.abilist @@ -200,6 +200,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/closefrom.c b/sysdeps/unix/sysv/linux/closefrom.c index f5d7342c2c..372896b775 100644 --- a/sysdeps/unix/sysv/linux/closefrom.c +++ b/sysdeps/unix/sysv/linux/closefrom.c @@ -16,6 +16,7 @@ License along with the GNU C Library; if not, see . */ +#include #include #include #include @@ -29,7 +30,7 @@ __closefrom (int lowfd) if (r == 0) return; - if (!__closefrom_fallback (l)) + if (!__closefrom_fallback (l, true)) __fortify_fail ("closefrom failed to close a file descriptor"); } weak_alias (__closefrom, closefrom) diff --git a/sysdeps/unix/sysv/linux/closefrom_fallback.c b/sysdeps/unix/sysv/linux/closefrom_fallback.c index 61e71d388d..9cca55653a 100644 --- a/sysdeps/unix/sysv/linux/closefrom_fallback.c +++ b/sysdeps/unix/sysv/linux/closefrom_fallback.c @@ -22,9 +22,11 @@ #include /* Fallback code: iterates over /proc/self/fd, closing each file descriptor - that fall on the criteria. */ + that fall on the criteria. If DIRFD_FALLBACK is set, a failure on + /proc/self/fd open will trigger a fallback that tries to close a file + descriptor before proceed. */ _Bool -__closefrom_fallback (int from) +__closefrom_fallback (int from, _Bool dirfd_fallback) { bool ret = false; @@ -33,7 +35,7 @@ __closefrom_fallback (int from) if (dirfd == -1) { /* The closefrom should work even when process can't open new files. */ - if (errno == ENOENT) + if (errno == ENOENT || !dirfd_fallback) goto err; for (int i = from; i < INT_MAX; i++) diff --git a/sysdeps/unix/sysv/linux/csky/libc.abilist b/sysdeps/unix/sysv/linux/csky/libc.abilist index 68658e3ef8..09702592bc 100644 --- a/sysdeps/unix/sysv/linux/csky/libc.abilist +++ b/sysdeps/unix/sysv/linux/csky/libc.abilist @@ -2250,6 +2250,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/hppa/libc.abilist b/sysdeps/unix/sysv/linux/hppa/libc.abilist index c16e7d8f2e..1567ba38a1 100644 --- a/sysdeps/unix/sysv/linux/hppa/libc.abilist +++ b/sysdeps/unix/sysv/linux/hppa/libc.abilist @@ -2201,6 +2201,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/i386/libc.abilist b/sysdeps/unix/sysv/linux/i386/libc.abilist index e73a11a8ea..47b9e4b7cc 100644 --- a/sysdeps/unix/sysv/linux/i386/libc.abilist +++ b/sysdeps/unix/sysv/linux/i386/libc.abilist @@ -2384,6 +2384,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/ia64/libc.abilist b/sysdeps/unix/sysv/linux/ia64/libc.abilist index 84ee3d6243..c0ee94f908 100644 --- a/sysdeps/unix/sysv/linux/ia64/libc.abilist +++ b/sysdeps/unix/sysv/linux/ia64/libc.abilist @@ -2235,6 +2235,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist index 297b8cab6c..091f0748f1 100644 --- a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist +++ b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist @@ -204,6 +204,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist index c562997d51..719880459a 100644 --- a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist +++ b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist @@ -2327,6 +2327,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist index 8fcbcfffeb..c04c9f5ea2 100644 --- a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist @@ -2301,6 +2301,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist index 5586d3610a..21f726de99 100644 --- a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist @@ -2298,6 +2298,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist index 3d5210a6e3..fd5e5972b0 100644 --- a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist @@ -2290,6 +2290,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist index 49c1a2c30b..a034f836c2 100644 --- a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist @@ -2288,6 +2288,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist index 238f2b3b21..68031ee840 100644 --- a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist @@ -2296,6 +2296,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist index 244c8dab63..5e66609ed0 100644 --- a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist @@ -2290,6 +2290,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/nios2/libc.abilist b/sysdeps/unix/sysv/linux/nios2/libc.abilist index 016317653c..7cdbb5d19c 100644 --- a/sysdeps/unix/sysv/linux/nios2/libc.abilist +++ b/sysdeps/unix/sysv/linux/nios2/libc.abilist @@ -2340,6 +2340,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist index e81d40ec43..ef3b634fb9 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist @@ -2354,6 +2354,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist index 33ba8522fd..b6ea64df68 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist @@ -2387,6 +2387,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist index 252a669528..101725aa33 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist @@ -2202,6 +2202,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist index 19f7bda2a2..cd1e8e692f 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist @@ -2503,6 +2503,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist index 7bbdc8e400..e7c1ff81fa 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist @@ -2068,6 +2068,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist index 94fd53fd54..20863b48d6 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist @@ -2268,6 +2268,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist index 43d5f17ef4..2f567802b2 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist +++ b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist @@ -2352,6 +2352,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist index 418643ba2b..b505a40bdb 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist +++ b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist @@ -2239,6 +2239,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/sh/be/libc.abilist b/sysdeps/unix/sysv/linux/sh/be/libc.abilist index b88e4c1b7b..afdd27c24b 100644 --- a/sysdeps/unix/sysv/linux/sh/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/sh/be/libc.abilist @@ -2208,6 +2208,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/sh/le/libc.abilist b/sysdeps/unix/sysv/linux/sh/le/libc.abilist index 33c1bb6555..ba56841175 100644 --- a/sysdeps/unix/sysv/linux/sh/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/sh/le/libc.abilist @@ -2205,6 +2205,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist index 3af45b8b0e..bfaf278214 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist +++ b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist @@ -2343,6 +2343,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist index 0cc316d932..91ed365b8d 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist +++ b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist @@ -2256,6 +2256,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/spawn_int_def.h b/sysdeps/unix/sysv/linux/spawn_int_def.h new file mode 100644 index 0000000000..3be65b5711 --- /dev/null +++ b/sysdeps/unix/sysv/linux/spawn_int_def.h @@ -0,0 +1,25 @@ +/* Internal definitions for posix_spawn functionality. Linux version. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _SPAWN_INT_DEF_H +#define _SPAWN_INT_DEF_H + +/* spawni.c implements closefrom by interacting over /proc/self/fd. */ +#define __SPAWN_SUPPORT_CLOSEFROM 1 + +#endif /* _SPAWN_INT_H */ diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c index 501f8fbccd..403bf609df 100644 --- a/sysdeps/unix/sysv/linux/spawni.c +++ b/sysdeps/unix/sysv/linux/spawni.c @@ -16,22 +16,16 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include #include +#include +#include #include -#include -#include -#include -#include -#include "spawn_int.h" +#include +#include +#include +#include /* The Linux implementation of posix_spawn{p} uses the clone syscall directly with CLONE_VM and CLONE_VFORK flags and an allocated stack. The new stack @@ -280,6 +274,14 @@ __spawni_child (void *arguments) if (__fchdir (action->action.fchdir_action.fd) != 0) goto fail; break; + + case spawn_do_closefrom: + { + int lowfd = action->action.closefrom_action.from; + int r = INLINE_SYSCALL_CALL (close_range, lowfd, ~0U, 0); + if (r != 0 && !__closefrom_fallback (lowfd, false)) + goto fail; + } break; } } } @@ -344,7 +346,9 @@ __spawnix (pid_t * pid, const char *file, /* We need at least a few pages in case the compiler's stack checking is enabled. In some configs, it is known to use at least 24KiB. We use 32KiB to be "safe" from anything the compiler might do. Besides, the - extra pages won't actually be allocated unless they get used. */ + extra pages won't actually be allocated unless they get used. + It also acts the slack for spawn_closefrom (including MIPS64 getdents64 + where it might use about 1k extra stack space. */ argv_size += (32 * 1024); size_t stack_size = ALIGN_UP (argv_size, GLRO(dl_pagesize)); void *stack = __mmap (NULL, stack_size, prot, diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist index 5d2719da05..dd25bce609 100644 --- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist @@ -2217,6 +2217,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist index 029abb00e4..1221084d50 100644 --- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist @@ -2322,6 +2322,7 @@ GLIBC_2.34 mtx_lock F GLIBC_2.34 mtx_timedlock F GLIBC_2.34 mtx_trylock F GLIBC_2.34 mtx_unlock F +GLIBC_2.34 posix_spawn_file_actions_addclosefrom_np F GLIBC_2.34 pthread_cond_clockwait F GLIBC_2.34 pthread_condattr_getclock F GLIBC_2.34 pthread_condattr_getpshared F