glibc/posix/tst-spawn-chdir.c
Adhemerval Zanella Netto 0d6f9f6265 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-09-05 13:08:59 -03:00

222 lines
7.7 KiB
C

/* Test the posix_spawn_file_actions_addchdir_np function.
Copyright (C) 2018-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 <array_length.h>
#include <errno.h>
#include <fcntl.h>
#include <spawn.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/test-driver.h>
#include <support/xstdio.h>
#include <support/xunistd.h>
#include <sys/wait.h>
#include <unistd.h>
#include <tst-spawn.h>
/* Reads the file at PATH, which must consist of exactly one line.
Removes the line terminator at the end of the file. */
static char *
read_one_line (const char *path)
{
FILE *fp = xfopen (path, "r");
char *buffer = NULL;
size_t length = 0;
ssize_t ret = getline (&buffer, &length, fp);
if (ferror (fp))
FAIL_EXIT1 ("getline: %m");
if (ret < 1)
FAIL_EXIT1 ("getline returned %zd", ret);
if (fgetc (fp) != EOF)
FAIL_EXIT1 ("trailing bytes in %s", path);
if (ferror (fp))
FAIL_EXIT1 ("fgetc: %m");
xfclose (fp);
if (buffer[ret - 1] != '\n')
FAIL_EXIT1 ("missing line terminator in %s", path);
buffer[ret - 1] = 0;
return buffer;
}
/* Return the path to the "pwd" program. */
const char *
get_pwd_program (void)
{
const char *const paths[] = { "/bin/pwd", "/usr/bin/pwd" };
for (size_t i = 0; i < array_length (paths); ++i)
if (access (paths[i], X_OK) == 0)
return paths[i];
FAIL_EXIT1 ("cannot find pwd program");
}
/* Adds chdir operations to ACTIONS, using PATH. If DO_FCHDIR, use
the open function and TMPFD to emulate chdir using fchdir. */
static void
add_chdir (posix_spawn_file_actions_t *actions, const char *path,
bool do_fchdir, int tmpfd)
{
if (do_fchdir)
{
TEST_COMPARE (posix_spawn_file_actions_addopen
(actions, tmpfd, path, O_DIRECTORY | O_RDONLY, 0), 0);
TEST_COMPARE (posix_spawn_file_actions_addfchdir_np
(actions, tmpfd), 0);
TEST_COMPARE (posix_spawn_file_actions_addclose (actions, tmpfd), 0);
}
else
TEST_COMPARE (posix_spawn_file_actions_addchdir_np (actions, path), 0);
}
static int
do_test (void)
{
/* Directory for temporary file data. Each subtest uses a numeric
subdirectory. */
char *directory = support_create_temp_directory ("tst-spawn-chdir-");
{
/* Avoid symbolic links, to get more consistent behavior from the
pwd command. */
char *tmp = realpath (directory, NULL);
if (tmp == NULL)
FAIL_EXIT1 ("realpath: %m");
free (directory);
directory = tmp;
}
char *original_cwd = get_current_dir_name ();
if (original_cwd == NULL)
FAIL_EXIT1 ("get_current_dir_name: %m");
int iteration = 0;
for (int do_spawnp = 0; do_spawnp < 2; ++do_spawnp)
for (int do_overwrite = 0; do_overwrite < 2; ++do_overwrite)
for (int do_fchdir = 0; do_fchdir < 2; ++do_fchdir)
{
/* This subtest does not make sense for fchdir. */
if (do_overwrite && do_fchdir)
continue;
++iteration;
if (test_verbose > 0)
printf ("info: iteration=%d do_spawnp=%d do_overwrite=%d"
" do_fchdir=%d\n",
iteration, do_spawnp, do_overwrite, do_fchdir);
/* The "pwd" program runs in this directory. */
char *iteration_directory = xasprintf ("%s/%d", directory, iteration);
add_temp_file (iteration_directory);
xmkdir (iteration_directory, 0777);
/* This file receives output from "pwd". */
char *output_file_path
= xasprintf ("%s/output-file", iteration_directory);
add_temp_file (output_file_path);
/* This subdirectory is used for chdir ordering checks. */
char *subdir_path = xasprintf ("%s/subdir", iteration_directory);
add_temp_file (subdir_path);
xmkdir (subdir_path, 0777);
/* Also used for checking the order of actions. */
char *probe_file_path
= xasprintf ("%s/subdir/probe-file", iteration_directory);
add_temp_file (probe_file_path);
TEST_COMPARE (access (probe_file_path, F_OK), -1);
TEST_COMPARE (errno, ENOENT);
/* This symbolic link is used in a relative path with
posix_spawn. */
char *pwd_symlink_path
= xasprintf ("%s/subdir/pwd-symlink", iteration_directory);
xsymlink (get_pwd_program (), pwd_symlink_path);
add_temp_file (pwd_symlink_path);
posix_spawn_file_actions_t actions;
TEST_COMPARE (posix_spawn_file_actions_init (&actions), 0);
add_chdir (&actions, subdir_path, do_fchdir, 4);
TEST_COMPARE (posix_spawn_file_actions_addopen
(&actions, 3, /* Arbitrary unused descriptor. */
"probe-file",
O_WRONLY | O_CREAT | O_EXCL, 0777), 0);
TEST_COMPARE (posix_spawn_file_actions_addclose (&actions, 3), 0);
/* Run the actual in iteration_directory. */
add_chdir (&actions, "..", do_fchdir, 5);
TEST_COMPARE (posix_spawn_file_actions_addopen
(&actions, STDOUT_FILENO, "output-file",
O_WRONLY | O_CREAT | O_EXCL, 0777), 0);
/* Check that posix_spawn_file_actions_addchdir_np made a copy
of the path. */
if (do_overwrite)
subdir_path[0] = '\0';
char *const argv[] = { (char *) "pwd", NULL };
char *const envp[] = { NULL } ;
PID_T_TYPE pid;
if (do_spawnp)
TEST_COMPARE (POSIX_SPAWNP (&pid, "pwd", &actions,
NULL, argv, envp), 0);
else
TEST_COMPARE (POSIX_SPAWN (&pid, "subdir/pwd-symlink", &actions,
NULL, argv, envp), 0);
TEST_VERIFY (pid > 0);
siginfo_t sinfo;
TEST_COMPARE (WAITID (P_ALL, 0, &sinfo, WEXITED), 0);
TEST_COMPARE (sinfo.si_code, CLD_EXITED);
TEST_COMPARE (sinfo.si_status, 0);
/* Check that the current directory did not change. */
{
char *cwd = get_current_dir_name ();
if (cwd == NULL)
FAIL_EXIT1 ("get_current_dir_name: %m");
TEST_COMPARE_BLOB (original_cwd, strlen (original_cwd),
cwd, strlen (cwd));
free (cwd);
}
/* Check the output from "pwd". */
{
char *pwd = read_one_line (output_file_path);
TEST_COMPARE_BLOB (iteration_directory, strlen (iteration_directory),
pwd, strlen (pwd));
free (pwd);
}
/* This file must now exist. */
TEST_COMPARE (access (probe_file_path, F_OK), 0);
TEST_COMPARE (posix_spawn_file_actions_destroy (&actions), 0);
free (pwd_symlink_path);
free (probe_file_path);
free (subdir_path);
free (output_file_path);
}
free (directory);
return 0;
}
#include <support/test-driver.c>