public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Yann Droneaud <ydroneaud@opteya.com>
To: "Alexandra Hájková" <alexandra.khirnova@gmail.com>,
	libc-alpha@sourceware.org
Cc: "Alexandra Hájková" <ahajkova@redhat.com>
Subject: Re: [PATCH]  Linux: Add execveat system call wrapper
Date: Mon, 09 Nov 2020 22:34:18 +0100	[thread overview]
Message-ID: <1874738a1a5f9bddd81783e198def7c5ea11c085.camel@opteya.com> (raw)
In-Reply-To: <20201106210314.399276-1-ahajkova@redhat.com>

Hi,

Le vendredi 06 novembre 2020 à 22:03 +0100, Alexandra Hájková via Libc-
alpha a écrit :

> diff --git a/posix/tst-execveat.c b/posix/tst-execveat.c
> new file mode 100644
> index 0000000000..e2c4164513
> --- /dev/null
> +++ b/posix/tst-execveat.c
> @@ -0,0 +1,174 @@
> +/* Copyright (C) 2017-2020 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 <fcntl.h>
> +#include <errno.h>
> +#include <stdlib.h>
> +#include <sys/types.h>
> +#include <dirent.h>
> +#include <support/check.h>
> +#include <support/support.h>
> +#include <support/temp_file.h>
> +#include <support/xdlfcn.h>
> +#include <support/xstdio.h>
> +#include <support/xunistd.h>
> +#include <wait.h>
> +#include <support/test-driver.h>
> +
> +int
> +call_execveat (int fd, const char *pathname, int flags, int expected_fail,
> +               int num)
> +{
> +  char *argv[] = { (char *) "sh", (char *) "-c", (char *) "exit 3", NULL };

May be there's no need to run a shell for the test. What about /bin/true or /bin/false ?

> +  char *envp[] = { (char *) "FOO=BAR", NULL };
> +  pid_t pid;
> +  int status;
> +
> +  test_verbose = 1;
> +  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, errno %d", errno);
> +    }
> +  xwaitpid (pid, &status, 0);
> +
> +  if (WIFEXITED (status))
> +    {
> +      if (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
> +        FAIL_UNSUPPORTED ("execveat is unimplemented");
> +      else if (expected_fail)
> +        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 stat st;
> +
> +  dirp = opendir ("/bin");
> +  if (dirp == NULL)
> +    FAIL_EXIT1 ("failed to open /bin");
> +  fd = dirfd (dirp);
> +
> +  /* Call execveat for various fd/pathname combinations  */
> +
> +  /* fd: valid dir, pathname: relative, flags:: 0  */
> +  call_execveat (fd, "sh", 0, 0, __LINE__);
> +  /* fd: AT_FDCWD, pathname: relative, flags: 0
> +     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  */
> +  chdir ("/bin");
> +  call_execveat (AT_FDCWD, "sh", 0, 0, __LINE__);
> +  xclose (fd);
> +#ifdef O_PATH
> +  /* fd: valid dir with O_PATH, pathname: relative, flags: 0  */
> +  fd = xopen ("/bin", O_PATH | O_DIRECTORY, O_RDONLY);
> +  call_execveat (fd, "sh", 0, 0, __LINE__);
> +  xclose (fd);
> +
> +  fd = xopen ("/usr", O_PATH | O_DIRECTORY, 0);
> +  /* fd: AT_FDCWD, pathname: absolute in different dir, flags: 0  */
> +  call_execveat (AT_FDCWD, "/bin/sh", 0, 0, __LINE__);
> +  /* fd: valid dir with O_PATH, pathname: absolute, flags: 0  */
> +  call_execveat (fd, "/bin/sh", 0, 0, __LINE__);
> +  xclose (fd);
> +#endif
> +
> +  fd = xopen ("/usr", O_RDONLY, 0);
> +  /* fd: valid dir, pathname: absolute in differen dir, flags: 0  */
> +  call_execveat (fd, "/bin/sh", 0, 0, __LINE__);
> +  xclose (fd);
> +
> +  fd = xopen ("/bin/sh", O_RDONLY, 0);
> +  /* fd: regular file, pathname: relative, flags: 0  */
> +  call_execveat (fd, "sh", 0, ENOTDIR, __LINE__);
> +  /* fd: regular file, pathname: absolute, flags: 0  */
> +  call_execveat (fd, "/bin/sh", 0, 0, __LINE__);
> +  xclose (fd);
> +
> +#ifdef O_PATH
> +  fd = xopen ("/bin/sh", O_PATH, 0);
> +  /* fd: O_PATH of regular file, pathname: empty, flags: 0  */
> +  call_execveat (fd, "", 0, ENOENT, __LINE__);
> +  /* fd: O_PATH of regular file, pathname: empty, flags: AT_EMPTY_PATH  */
> +  call_execveat (fd, "", AT_EMPTY_PATH, 0, __LINE__);
> +  /* fd: O_PATH of regular file, pathname: empty,
> +     flags: AT_EMPTY_PATH  AT_SYMLINK_NOFOLLOW  */
> +  call_execveat (fd, "", AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW, 0, __LINE__);
> +  xclose (fd);
> +
> +  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);
> +  stat ("/bin/sh", &st);
> +  fd = xopen ("/bin/sh", O_RDONLY, 0);
> +  xcopy_file_range (fd, 0, fd_out, 0, st.st_size, 0);
> +  fchmod (fd_out, 0777);
> +  xclose (fd);
> +  xclose (fd_out);
> +  fd_out = xopen (symlink_name, O_PATH, 0);
> +
> +  /* fd: O_PATH of symbolic link, pathname: empty, flags: 0 */
> +  call_execveat (fd_out, "", 0, ENOENT, __LINE__);
> +  /* fd: O_PATH of symbolic link, pathname: empty, flags: AT_EMPTY_PATH */
> +  call_execveat (fd_out, "", AT_EMPTY_PATH, 0, __LINE__);
> +  /* fd: O_PATH of symbolic link, pathname: empty,
> +     flags: AT_EMPTY_PATH  AT_SYMLINK_NOFOLLOW */
> +  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 <support/test-driver.c>

Regards

-- 
Yann Droneaud
OPTEYA



  parent reply	other threads:[~2020-11-09 21:34 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-28 12:20 Alexandra Hájková
2020-04-28 14:17 ` Florian Weimer
2020-04-28 15:03   ` Joseph Myers
2020-04-28 15:03 ` Joseph Myers
2020-04-28 15:08   ` Florian Weimer
2020-04-28 15:29     ` Joseph Myers
2020-04-28 17:15       ` Florian Weimer
2020-04-28 17:19         ` Joseph Myers
2020-04-28 17:44 ` Adhemerval Zanella
2020-04-28 17:50   ` Florian Weimer
2020-04-28 18:31     ` Adhemerval Zanella
2020-04-30 11:15 ` Florian Weimer
2020-04-30 12:28   ` Adhemerval Zanella
2020-04-30 12:55     ` Florian Weimer
2020-04-30 19:03       ` Adhemerval Zanella
2020-04-30 12:32 ` Adhemerval Zanella
2020-11-06 21:03 ` Alexandra Hájková
2020-11-06 22:15   ` Joseph Myers
2020-11-09 18:43     ` Florian Weimer
2020-11-09 21:34   ` Yann Droneaud [this message]
2020-11-26 11:31     ` Alexandra Petlanova Hajkova
2020-11-09 20:39 ` Adhemerval Zanella
2020-12-03 13:55   ` Florian Weimer
2020-11-26 21:28 ` Alexandra Hájková
2020-11-27 14:58   ` Adhemerval Zanella
2020-11-27 17:32     ` Florian Weimer
2020-11-27 17:38       ` Adhemerval Zanella
2020-12-03 14:20 ` Alexandra Hájková
2020-12-03 14:37   ` Andreas Schwab
2020-12-08 14:44   ` Adhemerval Zanella
2020-12-08 15:18     ` Florian Weimer
2020-12-08 16:41       ` Adhemerval Zanella
2021-03-15 21:42 ` Alexandra Hájková
2021-03-15 22:12   ` Joseph Myers
2021-03-15 22:17   ` Dmitry V. Levin
2021-03-24 13:54 ` Alexandra Hájková
2021-03-26 20:36   ` Adhemerval Zanella
2021-04-02 12:13     ` Alexandra Hájková
2021-04-02 13:29       ` Adhemerval Zanella
2021-04-05 16:32 ` Alexandra Hájková
2021-04-12 10:26   ` Alexandra Hájková
2021-04-12 11:14   ` Andreas Schwab
2021-04-12 19:26 ` Alexandra Hájková
2021-04-13 19:27   ` Adhemerval Zanella
2021-04-21 18:11 ` Alexandra Hájková
2021-05-03 19:20   ` Adhemerval Zanella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1874738a1a5f9bddd81783e198def7c5ea11c085.camel@opteya.com \
    --to=ydroneaud@opteya.com \
    --cc=ahajkova@redhat.com \
    --cc=alexandra.khirnova@gmail.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).