2016-01-20 03:33:32 +08:00
|
|
|
/* Further tests for spawn in case of invalid binary paths.
|
2024-01-02 02:12:26 +08:00
|
|
|
Copyright (C) 2016-2024 Free Software Foundation, Inc.
|
2016-01-20 03:33:32 +08:00
|
|
|
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
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2016-01-20 03:33:32 +08:00
|
|
|
|
|
|
|
#include <spawn.h>
|
|
|
|
#include <error.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2017-04-25 02:48:01 +08:00
|
|
|
#include <support/check.h>
|
2024-05-07 00:20:56 +08:00
|
|
|
#include <support/descriptors.h>
|
posix: Add pidfd_spawn and pidfd_spawnp (BZ 30349)
Returning a pidfd allows a process to keep a race-free handle for a
child process, otherwise, the caller will need to either use pidfd_open
(which still might be subject to TOCTOU) or keep the old racy interface
base on pid_t.
To correct use pifd_spawn, the kernel must support not only returning
the pidfd with clone/clone3 but also waitid (P_PIDFD) (added on Linux
5.4). If kernel does not support the waitid, pidfd return ENOSYS.
It avoids the need to racy workarounds, such as reading the procfs
fdinfo to get the pid to use along with other wait interfaces.
These interfaces are similar to the posix_spawn and posix_spawnp, with
the only difference being it returns a process file descriptor (int)
instead of a process ID (pid_t). Their prototypes are:
int pidfd_spawn (int *restrict pidfd,
const char *restrict file,
const posix_spawn_file_actions_t *restrict facts,
const posix_spawnattr_t *restrict attrp,
char *const argv[restrict],
char *const envp[restrict])
int pidfd_spawnp (int *restrict pidfd,
const char *restrict path,
const posix_spawn_file_actions_t *restrict facts,
const posix_spawnattr_t *restrict attrp,
char *const argv[restrict_arr],
char *const envp[restrict_arr]);
A new symbol is used instead of a posix_spawn extension to avoid
possible issues with language bindings that might track the return
argument lifetime. Although on Linux pid_t and int are interchangeable,
POSIX only states that pid_t should be a signed integer.
Both symbols reuse the posix_spawn posix_spawn_file_actions_t and
posix_spawnattr_t, to void rehash posix_spawn API or add a new one. It
also means that both interfaces support the same attribute and file
actions, and a new flag or file action on posix_spawn is also added
automatically for pidfd_spawn.
Also, using posix_spawn plumbing allows the reusing of most of the
current testing with some changes:
- waitid is used instead of waitpid since it is a more generic
interface.
- tst-posix_spawn-setsid.c is adapted to take into consideration that
the caller can check for session id directly. The test now spawns
itself and writes the session id as a file instead.
- tst-spawn3.c need to know where pidfd_spawn is used so it keeps an
extra file description unused.
Checked on x86_64-linux-gnu on Linux 4.15 (no CLONE_PIDFD or waitid
support), Linux 5.4 (full support), and Linux 6.2.
Reviewed-by: Florian Weimer <fweimer@redhat.com>
2023-08-25 00:42:18 +08:00
|
|
|
#include <tst-spawn.h>
|
2017-04-25 02:48:01 +08:00
|
|
|
|
2016-01-20 03:33:32 +08:00
|
|
|
int
|
2017-04-25 02:48:01 +08:00
|
|
|
do_test (void)
|
2016-01-20 03:33:32 +08:00
|
|
|
{
|
|
|
|
/* Check if posix_spawn correctly returns an error and an invalid pid
|
|
|
|
by trying to spawn an invalid binary. */
|
|
|
|
|
|
|
|
const char *program = "/path/to/invalid/binary";
|
|
|
|
char * const args[] = { 0 };
|
posix: Add pidfd_spawn and pidfd_spawnp (BZ 30349)
Returning a pidfd allows a process to keep a race-free handle for a
child process, otherwise, the caller will need to either use pidfd_open
(which still might be subject to TOCTOU) or keep the old racy interface
base on pid_t.
To correct use pifd_spawn, the kernel must support not only returning
the pidfd with clone/clone3 but also waitid (P_PIDFD) (added on Linux
5.4). If kernel does not support the waitid, pidfd return ENOSYS.
It avoids the need to racy workarounds, such as reading the procfs
fdinfo to get the pid to use along with other wait interfaces.
These interfaces are similar to the posix_spawn and posix_spawnp, with
the only difference being it returns a process file descriptor (int)
instead of a process ID (pid_t). Their prototypes are:
int pidfd_spawn (int *restrict pidfd,
const char *restrict file,
const posix_spawn_file_actions_t *restrict facts,
const posix_spawnattr_t *restrict attrp,
char *const argv[restrict],
char *const envp[restrict])
int pidfd_spawnp (int *restrict pidfd,
const char *restrict path,
const posix_spawn_file_actions_t *restrict facts,
const posix_spawnattr_t *restrict attrp,
char *const argv[restrict_arr],
char *const envp[restrict_arr]);
A new symbol is used instead of a posix_spawn extension to avoid
possible issues with language bindings that might track the return
argument lifetime. Although on Linux pid_t and int are interchangeable,
POSIX only states that pid_t should be a signed integer.
Both symbols reuse the posix_spawn posix_spawn_file_actions_t and
posix_spawnattr_t, to void rehash posix_spawn API or add a new one. It
also means that both interfaces support the same attribute and file
actions, and a new flag or file action on posix_spawn is also added
automatically for pidfd_spawn.
Also, using posix_spawn plumbing allows the reusing of most of the
current testing with some changes:
- waitid is used instead of waitpid since it is a more generic
interface.
- tst-posix_spawn-setsid.c is adapted to take into consideration that
the caller can check for session id directly. The test now spawns
itself and writes the session id as a file instead.
- tst-spawn3.c need to know where pidfd_spawn is used so it keeps an
extra file description unused.
Checked on x86_64-linux-gnu on Linux 4.15 (no CLONE_PIDFD or waitid
support), Linux 5.4 (full support), and Linux 6.2.
Reviewed-by: Florian Weimer <fweimer@redhat.com>
2023-08-25 00:42:18 +08:00
|
|
|
PID_T_TYPE pid = -1;
|
2016-01-20 03:33:32 +08:00
|
|
|
|
2024-05-07 00:20:56 +08:00
|
|
|
{
|
|
|
|
struct support_descriptors *descrs = support_descriptors_list ();
|
|
|
|
|
|
|
|
int ret = POSIX_SPAWN (&pid, program, 0, 0, args, environ);
|
|
|
|
if (ret != ENOENT)
|
|
|
|
{
|
|
|
|
errno = ret;
|
|
|
|
FAIL_EXIT1 ("posix_spawn: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* POSIX states the value returned on pid variable in case of an error
|
|
|
|
is not specified. GLIBC will update the value iff the child
|
|
|
|
execution is successful. */
|
|
|
|
if (pid != -1)
|
|
|
|
FAIL_EXIT1 ("posix_spawn returned pid != -1 (%i)", (int) pid);
|
|
|
|
|
|
|
|
/* Check if no child is actually created. */
|
|
|
|
TEST_COMPARE (WAITID (P_ALL, 0, NULL, WEXITED), -1);
|
|
|
|
TEST_COMPARE (errno, ECHILD);
|
|
|
|
|
|
|
|
/* Also check if there is no leak descriptors. */
|
|
|
|
support_descriptors_check (descrs);
|
|
|
|
support_descriptors_free (descrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
/* Same as before, but with posix_spawnp. */
|
|
|
|
char *args2[] = { (char*) program, 0 };
|
|
|
|
|
|
|
|
struct support_descriptors *descrs = support_descriptors_list ();
|
|
|
|
|
|
|
|
int ret = POSIX_SPAWNP (&pid, args2[0], 0, 0, args2, environ);
|
|
|
|
if (ret != ENOENT)
|
|
|
|
{
|
|
|
|
errno = ret;
|
|
|
|
FAIL_EXIT1 ("posix_spawnp: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid != -1)
|
|
|
|
FAIL_EXIT1 ("posix_spawnp returned pid != -1 (%i)", (int) pid);
|
|
|
|
|
|
|
|
TEST_COMPARE (WAITID (P_ALL, 0, NULL, WEXITED), -1);
|
|
|
|
TEST_COMPARE (errno, ECHILD);
|
|
|
|
|
|
|
|
support_descriptors_check (descrs);
|
|
|
|
support_descriptors_free (descrs);
|
|
|
|
}
|
2016-01-20 03:33:32 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-25 02:48:01 +08:00
|
|
|
#include <support/test-driver.c>
|