mirror of
https://sourceware.org/git/glibc.git
synced 2024-11-09 23:00:07 +00:00
0d6f9f6265
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>
78 lines
2.3 KiB
C
78 lines
2.3 KiB
C
/* Further tests for spawn in case of invalid binary paths.
|
|
Copyright (C) 2016-2023 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
|
|
<https://www.gnu.org/licenses/>. */
|
|
|
|
#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>
|
|
|
|
#include <support/check.h>
|
|
#include <tst-spawn.h>
|
|
|
|
int
|
|
do_test (void)
|
|
{
|
|
/* 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 };
|
|
PID_T_TYPE pid = -1;
|
|
|
|
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);
|
|
|
|
/* Same as before, but with posix_spawnp. */
|
|
char *args2[] = { (char*) program, 0 };
|
|
|
|
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);
|
|
|
|
return 0;
|
|
}
|
|
|
|
#include <support/test-driver.c>
|