[PATCH] Linux: Add execveat system call wrapper

Adhemerval Zanella adhemerval.zanella@linaro.org
Mon Nov 9 20:39:57 GMT 2020



On 28/04/2020 09:20, Alexandra Hájková via Libc-alpha wrote:
> From: Alexandra Hájková <ahajkova@redhat.com>

> diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
> index c315cc5cb8..55225b5447 100644
> --- a/sysdeps/unix/sysv/linux/bits/unistd_ext.h
> +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
> @@ -17,7 +17,7 @@
>     <https://www.gnu.org/licenses/>.  */
>  
>  #ifndef _UNISTD_H
> -# error "Never include <bits/unistd_ext.h> directly; use <unistd.h> instead."
> +#error "Never include <bits/unistd_ext.h> directly; use <unistd.h> instead."
>  #endif
>  

Spurious change.

>  #ifdef __USE_GNU
> @@ -33,4 +33,9 @@
>     not detached and has not been joined.  */
>  extern __pid_t gettid (void) __THROW;
>  
> +/* 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));
> +

Indentation seems off here (extra space before extern).

>  #endif

> diff --git a/sysdeps/unix/sysv/linux/execveat.c b/sysdeps/unix/sysv/linux/execveat.c
> new file mode 100644
> index 0000000000..7ebf397091
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/execveat.c
> @@ -0,0 +1,67 @@
> +/* Copyright (C) 1994-2020 Free Software Foundation, Inc.

I think it should be solely 2020.

> +   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 <errno.h>
> +#include <stddef.h>
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <fcntl.h>
> +#include <sys/stat.h>
> +
> +#include <sysdep.h>
> +#include <sys/syscall.h>
> +#include <kernel-features.h>
> +#include <fd_to_filename.h>
> +
> +
> +/* 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;
> +
> +  int fd = openat (dirfd, path, flags | O_CLOEXEC);

This will turn execevat in a cancellation entrypoint.

> +  if (fd < 0)
> +    return -1;
> +
> +  struct fd_to_filename fdfilename;
> +  const char *gfilename
> +    = path != NULL ? path : __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 stat st;
> +  if (stat ("/proc/self/fd", &st) != 0 && errno == ENOENT)
> +    save = ENOSYS;

Not sure if it would make a difference here, but I think it would be
better to use the LFS interface here. 

> diff --git a/sysdeps/unix/sysv/linux/tst-execveat.c b/sysdeps/unix/sysv/linux/tst-execveat.c
> new file mode 100644
> index 0000000000..06c4e9fc4b
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/tst-execveat.c
> @@ -0,0 +1,68 @@
> +/* Copyright (C) 2017-2020 Free Software Foundation, Inc.

Same as before, I think the Copyright year should be only 2020.

> +   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 <errno.h>
> +#include <sys/types.h>
> +#include <dirent.h>
> +#include <support/check.h>
> +#include <support/xdlfcn.h>
> +#include <support/xstdio.h>
> +#include <support/xunistd.h>
> +#include <wait.h>
> +
> +static int
> +do_test (void)
> +{
> +  char *argv[] = { (char *) "-c", (char *) "exit 3", NULL };
> +  char *envp[] = { (char *) "FOO=BAR", NULL };

Maybe add a check if the environment variable is what you expect (to
check if envp passing is ok)?

> +  DIR *dirp;
> +  int fd;
> +  pid_t pid;
> +  int status;
> +
> +  dirp = opendir ("/bin");
> +  if (!dirp)
> +    FAIL_EXIT1 ("failed to open /bin");
> +  fd = dirfd (dirp);
> +
> +  errno = 0;
> +  pid = xfork ();
> +  if (pid == 0)
> +    {
> +      execveat (fd, "sh", argv, envp, 0);
> +
> +      if (errno)

Don't use implicit checks.

> +        {
> +          closedir (dirp);
> +          FAIL_EXIT1 ("execveat failed, errno %d", errno);
> +        }
> +    }
> +  xwaitpid (pid, &status, 0);
> +
> +  if (WIFEXITED (status))
> +    TEST_COMPARE (WEXITSTATUS (status), 3);
> +  else
> +    {
> +      closedir (dirp);
> +      FAIL_EXIT1 ("execveat failed");
> +    }
> +
> +  closedir (dirp);
> +  return 0;
> +}
> +
> +#include <support/test-driver.c>

Besides the points raised by Joseph, the rest looks ok. I didn't check if it 
updates all the abilist files.


More information about the Libc-alpha mailing list