about summary refs log tree commit diff
path: root/posix/tst-execveat.c
blob: 0e0a33deb54726fadc5b7597e74240e826686727 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* 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 <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>
#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 };
  char *envp[] = { (char *) "FOO=BAR", NULL };
  pid_t pid;
  int status;

  printf("call number: %d\n", num);

  pid = xfork ();
  if (pid == 0)
  {

      TEST_COMPARE (execveat (fd, "sh", 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 (expected_fail)
      TEST_COMPARE (WEXITSTATUS (status), 0);
    else
      TEST_COMPARE (WEXITSTATUS (status), 3);
  else if (!expected_fail)
      FAIL_EXIT1 ("execveat failed");
  return 0;
}

static int
do_test (void)
{
  DIR *dirp;
  int fd;

  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, 1);
  /* fd: valid dir, pathname: relative, flags: O_PATH  */
  call_execveat (fd, "sh", O_PATH, 0, 2);
  /* 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, 3);
  xclose (fd);
  closedir (dirp);

  dirp = opendir ("/usr");
  fd = dirfd (dirp);
  chdir ("/etc");
  /* fd: AT_FDCWD, pathname: absolute in different dir, flags: 0  */
  call_execveat (AT_FDCWD, "/bin/sh", 0, 0, 4);

  /* fd: valid dir, pathname: absolute in differen dir, flags: 0  */
  call_execveat (fd, "/bin/sh", 0, 0, 5);
  /* fd: valid dir, pathname: absolute, flags: O_PATH  */
  call_execveat (fd, "/bin/sh", O_PATH, 0, 6);
  xclose (fd);
  closedir (dirp);

  fd = xopen ("/bin/sh", 0, 0);
  /* fd: regular file, pathname: relative, flags: 0  */
  call_execveat(fd, "sh", 0, ENOTDIR, 7);
  /* fd: regular file, pathname: absolute, flags: 0  */
  call_execveat (fd, "/bin/sh", 0, 0, 8);
  xclose (fd);

  fd = xopen ("/bin/sh", O_PATH, 0);
  /* fd: O_PATH of regular file, pathname: empty, flags: 0  */
  call_execveat (fd, "", 0, ENOTDIR, 10);
  /* fd: O_PATH of regular file, pathname: empty, flags: AT_EMPTY_PATH  */
  call_execveat (fd, "", AT_EMPTY_PATH, 0, 11); // fails with ENOTDIR (20)
  /* fd: O_PATH of regular file, pathname: empty,
     flags: AT_EMPTY_PATH  AT_SYMLINK_NOFOLLOW  */
  //   call_execveat (fd, "", AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW, NULL, 0, 12); //fails with ENOTDIR
  xclose (fd);

  fd = xopen ("/bin/sh", O_NOFOLLOW | O_PATH, 0);
  /* fd: O_PATH of symbolic link, pathname: empty, flags:  */
//    call_execveat(fd, "", 0, 1, 13); //fails with  errno ENOTDIR
  /* fd: O_PATH of symbolic link, pathname: empty, flags:  */
//    call_execveat (fd, "", AT_EMPTY_PATH, 0, 14); //fails with  errno ENOTDIR
  /* fd: O_PATH of symbolic link, pathname: empty,
    flags: AT_EMPTY_PATH  AT_SYMLINK_NOFOLLOW */
  call_execveat (fd, "", AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW, ENOTDIR, 15);
  xclose (fd);

  /* Call execveat with closed fd, we expect this to fail with EBADF  */
  call_execveat (fd, "sh", 0, EBADF, 16);
  /* Call execveat with closed fd, we expect this to pass because the pathname is
    absolute  */
  call_execveat (fd, "/bin/sh", 0, 0, 17);

  return 0;
}

#include <support/test-driver.c>