From patchwork Wed Mar 24 13:54:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= X-Patchwork-Id: 42767 X-Patchwork-Delegate: azanella@linux.vnet.ibm.com 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 76F7F3857829; Wed, 24 Mar 2021 13:54:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 76F7F3857829 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1616594060; bh=9Yx19iBdNGc5qCcY8Np2DL0csCwZByIL7U2/SZy1qhc=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=TqB6M3g3JRS9R0XdVgZAa6Yo/XzHmIseUPTneG+dZqrWgMjEjQ4VxlEYhAtrUKxn5 5Y3B1/p25F4Msgom/UUtAoJJgzyZBp5ZjsM954YZdT5xZyjMhEErKtbnRxU19OKUPX 9I4aMMeMbO5dFQ5LJJ3TuHhjNvzpEPaEw9L6Mzl0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by sourceware.org (Postfix) with UTF8SMTPS id E5E4B3857C5F for ; Wed, 24 Mar 2021 13:54:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E5E4B3857C5F Received: by mail-wm1-x332.google.com with SMTP id f22-20020a7bc8d60000b029010c024a1407so1253478wml.2 for ; Wed, 24 Mar 2021 06:54:14 -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:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9Yx19iBdNGc5qCcY8Np2DL0csCwZByIL7U2/SZy1qhc=; b=rqCQkxt8Mc+j7reVkHxa3eJXlGchXLV8CjalRVQmp55ux0mEdGwUFZs7dQuAZUnJnl +MSs/Vx8/UM0kn+jFcSOZhzqmM6IzjcwwYtVgYBIU04ny7mvKNfyNMMMxkq22ZP4JjSF jcs+jl17BFX23+CW37yf2dTWupQp+hvIQJ8oujmMglHSeK6Q0icShBYaiqJqWZ1QY3SQ w/sisudzrDj8dcy8fbHqB+Avg69UNcyUlngKsoPGILiqaYJnOSELScMukvIC5EmEmXgV A35iO82cn93g3MsiQqbhMx8dFVf/IgPHHkJL1Kn7fxFdUX/QfoShNRamdLbXdlc3p6yE HZ7Q== X-Gm-Message-State: AOAM531BFxz04A94F9LKsNXI8DveDoYOmnIBquyn3bD7+lG6GV+Cb7fQ sYH0wz2R7UeYAWz3YRhqgyzx03RzB+xsww== X-Google-Smtp-Source: ABdhPJz0ERIPNDa1t/th+N8POk8Ri4heJcaayL61j/T0x65pZ9F3uK5h0IXEvFgVQ0U1BfieW83jIQ== X-Received: by 2002:a7b:c357:: with SMTP id l23mr3041480wmj.152.1616594053754; Wed, 24 Mar 2021 06:54:13 -0700 (PDT) Received: from picard.redhat.com (ip-244-183.pel.cz. [213.226.244.183]) by smtp.gmail.com with ESMTPSA id h10sm3381658wrp.22.2021.03.24.06.54.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 06:54:12 -0700 (PDT) X-Google-Original-From: =?unknown-8bit?q?Alexandra_H=C3=A1jkov=C3=A1_ahajkov?= =?unknown-8bit?q?a=40redhat=2Ecom?= To: libc-alpha@sourceware.org Subject: [PATCH] Linux: Add execveat system call wrapper Date: Wed, 24 Mar 2021 14:54:02 +0100 Message-Id: <20210324135402.2685623-1-ahajkova@redhat.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20200428122019.26826-1-ahajkova@redhat.com> References: <20200428122019.26826-1-ahajkova@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, 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: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1_via_Libc-alpha?= From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= Reply-To: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= Cc: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" From: Alexandra Hájková Also add the test for the new wrapper. --- This version: * was updated to use 2.24 symbol version * "difrd < 0" check was dropped NEWS | 5 + include/unistd.h | 4 + posix/Makefile | 4 +- posix/Versions | 3 + posix/execveat.c | 41 ++++ posix/tst-execveat-compat.c | 32 ++++ posix/tst-execveat.c | 180 ++++++++++++++++++ posix/unistd.h | 5 + sysdeps/mach/hurd/i386/libc.abilist | 1 + sysdeps/unix/sysv/linux/aarch64/libc.abilist | 1 + sysdeps/unix/sysv/linux/alpha/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/csky/libc.abilist | 1 + sysdeps/unix/sysv/linux/execveat.c | 54 ++++++ sysdeps/unix/sysv/linux/execveat_fallback.c | 69 +++++++ 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/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 + .../unix/sysv/linux/x86_64/64/libc.abilist | 1 + .../unix/sysv/linux/x86_64/x32/libc.abilist | 1 + 41 files changed, 427 insertions(+), 1 deletion(-) create mode 100644 posix/execveat.c create mode 100644 posix/tst-execveat-compat.c create mode 100644 posix/tst-execveat.c create mode 100644 sysdeps/unix/sysv/linux/execveat.c create mode 100644 sysdeps/unix/sysv/linux/execveat_fallback.c diff --git a/NEWS b/NEWS index aa0f10a891..a61071b494 100644 --- a/NEWS +++ b/NEWS @@ -18,6 +18,11 @@ Major new features: a dump of information related to IFUNC resolver operation and glibc-hwcaps subdirectory selection. +* The function execveat has been added and it operates similar to execve. + The syscall is already used to implement fexecve without requiring /proc to + be mounted. Similar to fexecve, if the syscall is not supported a fallback + which access /proc is used. + Deprecated and removed features, and other changes affecting compatibility: [Add deprecations, removals and changes affecting compatibility here] diff --git a/include/unistd.h b/include/unistd.h index 54becbc9eb..2dd901ed09 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -192,6 +192,10 @@ extern int __libc_pause (void); extern int __getlogin_r_loginuid (char *name, size_t namesize) attribute_hidden; +extern int +__execveat_fallback (int dirfd, const char *path, char *const argv[], + char *const envp[], int flags); + # if IS_IN (rtld) # include # endif diff --git a/posix/Makefile b/posix/Makefile index be0c72f0bb..f0ebe34c8e 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -66,7 +66,8 @@ routines := \ posix_madvise \ get_child_max sched_cpucount sched_cpualloc sched_cpufree \ streams-compat \ - shm-directory + shm-directory \ + execveat aux := init-posix environ tests := test-errno tstgetopt testfnm runtests runptests \ @@ -118,6 +119,7 @@ endif tests-internal := bug-regex5 bug-regex20 bug-regex33 \ tst-rfc3484 tst-rfc3484-2 tst-rfc3484-3 +tests-static-internal += tst-execveat-compat tests-container := bug-ga2 xtests := tst-getaddrinfo4 tst-getaddrinfo5 tst-sched_rr_get_interval ifeq (yes,$(build-shared)) diff --git a/posix/Versions b/posix/Versions index cfd3819966..b77e251e00 100644 --- a/posix/Versions +++ b/posix/Versions @@ -147,6 +147,9 @@ libc { } GLIBC_2.30 { } + GLIBC_2.34 { + execveat; + } GLIBC_PRIVATE { __libc_fork; __libc_pread; __libc_pwrite; __nanosleep_nocancel; __pause_nocancel; diff --git a/posix/execveat.c b/posix/execveat.c new file mode 100644 index 0000000000..3f0d0820e7 --- /dev/null +++ b/posix/execveat.c @@ -0,0 +1,41 @@ +/* Execute program relative to a directory file descriptor. + 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 + +/* Replace the current process, executing PATH relative to DIFRD with + * arguments ARGV and environment ENVP. + * ARGV and ENVP are terminated by NULL pointers. */ +int +__execveat (int dirfd, const char *path, char *const argv[], char *const envp[], + int flags) +{ + if (path == NULL || argv == NULL || envp == NULL) + { + __set_errno (EINVAL); + return -1; + } + + __set_errno (ENOSYS); + return -1; +} +stub_warning (execveat) + +weak_alias (__execveat, execveat) diff --git a/posix/tst-execveat-compat.c b/posix/tst-execveat-compat.c new file mode 100644 index 0000000000..082c67690d --- /dev/null +++ b/posix/tst-execveat-compat.c @@ -0,0 +1,32 @@ +/* Test the fallback implementation of execveat. + 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 + . */ + +/* Get the declaration of the official execveat function. */ +#include + +/* Compile a local version of execveat. */ +#include + +int +__execveat_fallback (int dirfd, const char *path, char *const argv[], + char *const envp[], int flags); + +/* Re-use the test, but run it against copy_file_range_compat defined + above. */ +#define execveat execveat_fallback +#include "tst-execveat.c" diff --git a/posix/tst-execveat.c b/posix/tst-execveat.c new file mode 100644 index 0000000000..52779de8cd --- /dev/null +++ b/posix/tst-execveat.c @@ -0,0 +1,180 @@ +/* Test execveat at the various corner cases. + 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 + +int +call_execveat (int fd, const char *pathname, int flags, int expected_fail, + int num) +{ + char *envp[] = { (char *) "FOO=3", NULL }; + char *argv[] = { (char *) "sh", (char *) "-c", (char *) "exit $FOO", NULL }; + pid_t pid; + int status; + + if (test_verbose > 0) + printf ("call line number: %d\n", num); + + pid = xfork (); + if (pid == 0) + { + TEST_COMPARE (execveat (fd, pathname, argv, envp, flags), -1); + if (errno == ENOSYS) + FAIL_UNSUPPORTED ("execveat is unimplemented"); + else if (errno == expected_fail) + { + if (test_verbose > 0) + printf ("expected fail: errno %d\n", errno); + _exit (0); + } + else + FAIL_EXIT1 ("execveat failed: %m (%d)", errno); + } + xwaitpid (pid, &status, 0); + + if (WIFEXITED (status)) + { + if (WEXITSTATUS (status) == EXIT_UNSUPPORTED) + FAIL_UNSUPPORTED ("execveat is unimplemented"); + else if (expected_fail != 0) + TEST_COMPARE (WEXITSTATUS (status), 0); + else + TEST_COMPARE (WEXITSTATUS (status), 3); + } + return 0; +} + +static int +do_test (void) +{ + DIR *dirp; + int fd, fd_out; + char *tmp_dir, *symlink_name, *tmp_sh; + struct stat64 st; + + dirp = opendir ("/bin"); + if (dirp == NULL) + FAIL_EXIT1 ("failed to open /bin"); + fd = dirfd (dirp); + + /* Call execveat for various fd/pathname combinations. */ + + /* Check the pathname relative to a valid dirfd. */ + call_execveat (fd, "sh", 0, 0, __LINE__); + xchdir ("/bin"); + /* Use the special value AT_FDCWD as dirfd. Quoting open(2): + If pathname is relative and dirfd is the special value AT_FDCWD, then + pathname is interpreted relative to the current working directory of + the calling process. */ + call_execveat (AT_FDCWD, "sh", 0, 0, __LINE__); + xclose (fd); +#ifdef O_PATH + /* Check the pathname relative to a valid dirfd with O_PATH. */ + fd = xopen ("/bin", O_PATH | O_DIRECTORY, O_RDONLY); + call_execveat (fd, "sh", 0, 0, __LINE__); + xclose (fd); + + /* Check absolute pathname, dirfd should be ignored. */ + call_execveat (AT_FDCWD, "/bin/sh", 0, 0, __LINE__); + fd = xopen ("/usr", O_PATH | O_DIRECTORY, 0); + /* Same check for absolute pathname, but with input file descriptor + openend with different flags. The dirfd should be ignored. */ + call_execveat (fd, "/bin/sh", 0, 0, __LINE__); + xclose (fd); +#endif + + fd = xopen ("/usr", O_RDONLY, 0); + /* Same check for absolute pathname, but with input file descriptor + openend with different flags. The dirfd should be ignored. */ + call_execveat (fd, "/bin/sh", 0, 0, __LINE__); + xclose (fd); + + fd = xopen ("/bin/sh", O_RDONLY, 0); + /* Check relative pathname, where dirfd does not point to a directory. */ + call_execveat (fd, "sh", 0, ENOTDIR, __LINE__); + /* Check absolute pathname, but dirfd is a regular file. The dirfd + should be ignored. */ + call_execveat (fd, "/bin/sh", 0, 0, __LINE__); + xclose (fd); + +#ifdef O_PATH + /* Quoting open(2): O_PATH + Obtain a file descriptor that can be used for two purposes: to + indicate a location in the filesystem tree and to perform + operations that act purely at the file descriptor level. */ + fd = xopen ("/bin/sh", O_PATH, 0); + /* Check the empty pathname. Dirfd is a regular file with O_PATH. */ + call_execveat (fd, "", 0, ENOENT, __LINE__); + /* Same check for an empty pathname, but with AT_EMPTY_PATH flag. + Quoting open(2): + If oldpath is an empty string, create a link to the file referenced + by olddirfd (which may have been obtained using the open(2) O_PATH flag. */ + call_execveat (fd, "", AT_EMPTY_PATH, 0, __LINE__); + call_execveat (fd, "", AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW, 0, __LINE__); + xclose (fd); + + /* Create a temporary directory "tmp_dir" and create a symbolik link tmp_sh + pointing to /bin/sh inside the tmp_dir. Open dirfd as a symbolic link. */ + tmp_dir = support_create_temp_directory ("tst-execveat_dir"); + symlink_name = xasprintf ("%s/symlink", tmp_dir); + xsymlink ("tmp_sh", symlink_name); + add_temp_file (symlink_name); + tmp_sh = xasprintf ("%s/tmp_sh", tmp_dir); + add_temp_file (tmp_sh); + fd_out = xopen (symlink_name, O_CREAT | O_WRONLY, 0); + xstat ("/bin/sh", &st); + fd = xopen ("/bin/sh", O_RDONLY, 0); + xcopy_file_range (fd, 0, fd_out, 0, st.st_size, 0); + xfchmod (fd_out, 0700); + xclose (fd); + xclose (fd_out); + fd_out = xopen (symlink_name, O_PATH, 0); + + /* Check the empty pathname. Dirfd is a symbolic link. */ + call_execveat (fd_out, "", 0, ENOENT, __LINE__); + call_execveat (fd_out, "", AT_EMPTY_PATH, 0, __LINE__); + call_execveat (fd_out, "", AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW, 0, + __LINE__); + xclose (fd_out); + free (symlink_name); + free (tmp_sh); + free (tmp_dir); +#endif + + /* Call execveat with closed fd, we expect this to fail with EBADF. */ + call_execveat (fd, "sh", 0, EBADF, __LINE__); + /* Call execveat with closed fd, we expect this to pass because the pathname is + absolute. */ + call_execveat (fd, "/bin/sh", 0, 0, __LINE__); + + return 0; +} + +#include diff --git a/posix/unistd.h b/posix/unistd.h index 3f22763379..d9d8929f71 100644 --- a/posix/unistd.h +++ b/posix/unistd.h @@ -295,6 +295,11 @@ extern int euidaccess (const char *__name, int __type) /* An alias for `euidaccess', used by some other systems. */ extern int eaccess (const char *__name, int __type) __THROW __nonnull ((1)); + +/* Execute program relative to a directory file descriptor. */ +extern int execveat (int __fd, const char *__path, char *const __argv[], + char *const __envp[], int __flags) + __THROW __nonnull ((2, 3)); #endif #ifdef __USE_ATFILE diff --git a/sysdeps/mach/hurd/i386/libc.abilist b/sysdeps/mach/hurd/i386/libc.abilist index e10a286d2e..bef66d105c 100644 --- a/sysdeps/mach/hurd/i386/libc.abilist +++ b/sysdeps/mach/hurd/i386/libc.abilist @@ -2204,6 +2204,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/aarch64/libc.abilist b/sysdeps/unix/sysv/linux/aarch64/libc.abilist index bac795262d..51f34d0f6d 100644 --- a/sysdeps/unix/sysv/linux/aarch64/libc.abilist +++ b/sysdeps/unix/sysv/linux/aarch64/libc.abilist @@ -2172,3 +2172,4 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F diff --git a/sysdeps/unix/sysv/linux/alpha/libc.abilist b/sysdeps/unix/sysv/linux/alpha/libc.abilist index 897f70db22..7da53174ef 100644 --- a/sysdeps/unix/sysv/linux/alpha/libc.abilist +++ b/sysdeps/unix/sysv/linux/alpha/libc.abilist @@ -2254,6 +2254,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/arm/be/libc.abilist b/sysdeps/unix/sysv/linux/arm/be/libc.abilist index 094236f713..108c41ae24 100644 --- a/sysdeps/unix/sysv/linux/arm/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/arm/be/libc.abilist @@ -156,6 +156,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 _Exit F GLIBC_2.4 _IO_2_1_stderr_ D 0xa0 GLIBC_2.4 _IO_2_1_stdin_ D 0xa0 diff --git a/sysdeps/unix/sysv/linux/arm/le/libc.abilist b/sysdeps/unix/sysv/linux/arm/le/libc.abilist index 2bb4d31e81..a995a356a4 100644 --- a/sysdeps/unix/sysv/linux/arm/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/arm/le/libc.abilist @@ -153,6 +153,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 _Exit F GLIBC_2.4 _IO_2_1_stderr_ D 0xa0 GLIBC_2.4 _IO_2_1_stdin_ D 0xa0 diff --git a/sysdeps/unix/sysv/linux/csky/libc.abilist b/sysdeps/unix/sysv/linux/csky/libc.abilist index d4291fecfb..33d0dfb2b7 100644 --- a/sysdeps/unix/sysv/linux/csky/libc.abilist +++ b/sysdeps/unix/sysv/linux/csky/libc.abilist @@ -2116,3 +2116,4 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F diff --git a/sysdeps/unix/sysv/linux/execveat.c b/sysdeps/unix/sysv/linux/execveat.c new file mode 100644 index 0000000000..0576f36b98 --- /dev/null +++ b/sysdeps/unix/sysv/linux/execveat.c @@ -0,0 +1,54 @@ +/* Execute program relative to a directory file descriptor. + 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 +#ifndef __ASSUME_EXECVEAT +# include "execveat_fallback.c" +#endif + +/* Execute the file FD refers to, overlaying the running program image. + ARGV and ENVP are passed to the new program, as for `execve'. */ +int +execveat (int dirfd, const char *path, char *const argv[], char *const envp[], + int flags) +{ + /* Avoid implicit array coercion in syscall macros. */ + INLINE_SYSCALL_CALL (execveat, dirfd, path, &argv[0], &envp[0], flags); +#ifndef __ASSUME_EXECVEAT + if (errno != ENOSYS) + return -1; + + if ((flags & ~(AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW)) != 0) + return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL); + + return __execveat_fallback (dirfd, path, argv, envp, flags); +#endif + + return -1; +} diff --git a/sysdeps/unix/sysv/linux/execveat_fallback.c b/sysdeps/unix/sysv/linux/execveat_fallback.c new file mode 100644 index 0000000000..c4a37d3799 --- /dev/null +++ b/sysdeps/unix/sysv/linux/execveat_fallback.c @@ -0,0 +1,69 @@ +/* Execute program relative to a directory file descriptor. + 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 + +int +__execveat_fallback (int dirfd, const char *path, char *const argv[], + char *const envp[], int flags) +{ + int fd; + + if (path[0] == '\0' && (flags & AT_EMPTY_PATH) && dirfd >= 0) + fd = dirfd; + else + { + int oflags = O_CLOEXEC; + if (flags & AT_SYMLINK_NOFOLLOW) + oflags |= O_NOFOLLOW; + fd = __openat_nocancel (dirfd, path, oflags); + } + if (fd < 0) + return INLINE_SYSCALL_ERROR_RETURN_VALUE (EBADFD); + + struct fd_to_filename fdfilename; + const char *gfilename = __fd_to_filename (fd, &fdfilename); + + /* We do not need the return value. */ + __execve (gfilename, argv, envp); + + int save = errno; + + /* We come here only if the 'execve' call fails. Determine whether + /proc is mounted. If not we return ENOSYS. */ + struct stat64 st; + if (__stat64 (FD_TO_FILENAME_PREFIX, &st) != 0 && errno == ENOENT) + save = ENOSYS; + + if (fd != dirfd) + __close_nocancel_nostatus (fd); + __set_errno (save); + + return -1; +} diff --git a/sysdeps/unix/sysv/linux/hppa/libc.abilist b/sysdeps/unix/sysv/linux/hppa/libc.abilist index 1fd2a862f6..e1430bb232 100644 --- a/sysdeps/unix/sysv/linux/hppa/libc.abilist +++ b/sysdeps/unix/sysv/linux/hppa/libc.abilist @@ -2075,6 +2075,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/i386/libc.abilist b/sysdeps/unix/sysv/linux/i386/libc.abilist index 943331f01e..7cb2d0dd6d 100644 --- a/sysdeps/unix/sysv/linux/i386/libc.abilist +++ b/sysdeps/unix/sysv/linux/i386/libc.abilist @@ -2242,6 +2242,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/ia64/libc.abilist b/sysdeps/unix/sysv/linux/ia64/libc.abilist index f530151bde..2171cd3cb2 100644 --- a/sysdeps/unix/sysv/linux/ia64/libc.abilist +++ b/sysdeps/unix/sysv/linux/ia64/libc.abilist @@ -2107,6 +2107,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist index 6e76b6dcaa..388eb76abf 100644 --- a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist +++ b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist @@ -157,6 +157,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 _Exit F GLIBC_2.4 _IO_2_1_stderr_ D 0x98 GLIBC_2.4 _IO_2_1_stdin_ D 0x98 diff --git a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist index 7541b8289f..067ed53f2b 100644 --- a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist +++ b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist @@ -2187,6 +2187,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist index 6cf1936c42..a792461b45 100644 --- a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist @@ -2167,3 +2167,4 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F diff --git a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist index 98730ebcda..0e09fe6322 100644 --- a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist @@ -2164,3 +2164,4 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F diff --git a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist index 92fa6cbc73..5d1b0b3d78 100644 --- a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist @@ -2158,6 +2158,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist index 265a49e74e..a278c0a092 100644 --- a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist @@ -2156,6 +2156,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist index cfa5e1111b..054d3436eb 100644 --- a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist @@ -2164,6 +2164,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist index 8c03ac52cd..c22ef9f196 100644 --- a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist @@ -2158,6 +2158,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/nios2/libc.abilist b/sysdeps/unix/sysv/linux/nios2/libc.abilist index 17f5609e06..c356e9f863 100644 --- a/sysdeps/unix/sysv/linux/nios2/libc.abilist +++ b/sysdeps/unix/sysv/linux/nios2/libc.abilist @@ -2205,3 +2205,4 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist index 76a16e2a6d..d1de01f91d 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist @@ -2214,6 +2214,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist index 697f072fd4..a1d52887bb 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist @@ -2247,6 +2247,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist index 2647bb51f1..10cdabb11c 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist @@ -2077,6 +2077,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist index 036b1c8345..70792ed87b 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist @@ -2367,3 +2367,4 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F diff --git a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist index fb5ad9909f..9b71083136 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist @@ -2134,3 +2134,4 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist index cead75acc5..aba76fbadd 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist +++ b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist @@ -2212,6 +2212,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist index 31366dd7e6..6a5a765f4b 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist +++ b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist @@ -2113,6 +2113,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/sh/be/libc.abilist b/sysdeps/unix/sysv/linux/sh/be/libc.abilist index a3a8be8910..b6629c244f 100644 --- a/sysdeps/unix/sysv/linux/sh/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/sh/be/libc.abilist @@ -2082,6 +2082,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/sh/le/libc.abilist b/sysdeps/unix/sysv/linux/sh/le/libc.abilist index 8f505c5045..e15ab4c569 100644 --- a/sysdeps/unix/sysv/linux/sh/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/sh/le/libc.abilist @@ -2079,6 +2079,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist index 53ef6304f1..11cad082d8 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist +++ b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist @@ -2203,6 +2203,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist index eba0cb156d..0ff8cb8739 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist +++ b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist @@ -2130,6 +2130,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist index 17ce5dfd58..040d44d3a8 100644 --- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist @@ -2089,6 +2089,7 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist index 17a1c83903..4355e06891 100644 --- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist @@ -2186,3 +2186,4 @@ GLIBC_2.33 mknodat F GLIBC_2.33 stat F GLIBC_2.33 stat64 F GLIBC_2.34 __libc_start_main F +GLIBC_2.34 execveat F