File: sys-execveat.c

package info (click to toggle)
valgrind 1%3A3.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 170,080 kB
  • sloc: ansic: 782,981; exp: 26,134; xml: 22,780; asm: 14,072; cpp: 7,903; makefile: 6,768; perl: 6,097; sh: 5,669; javascript: 981; awk: 148
file content (64 lines) | stat: -rw-r--r-- 1,489 bytes parent folder | download | duplicates (3)
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
#include <sys/syscall.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>

static int sys_execveat (int dirfd, const char *pathname,
			 char *const argv[], char *const envp[],
			 int flags)
{
#if defined(__NR_execveat)
  return syscall(__NR_execveat, dirfd, pathname, argv, envp, flags);
#else
  errno = ENOSYS;
  return -1;
#endif
}


int main()
{
  char *argv[] = { "foobar", "execveat exists", NULL };
  char *envp[] = { NULL };
  DIR  *dirp;
  int  fd;

  dirp = opendir("/bin");
  if (dirp == NULL) {
      perror("execveat");
      exit(EXIT_FAILURE);
  }
  fd = dirfd(dirp);

  /* Check valgrind will produce expected warnings for the
     various wrong arguments. */
  do {
      char *mem = malloc(16);
      void *t = (void *) &mem[0];
      void *z = (void *) -1;
      int flag = *((int *) &mem[8]);

      sys_execveat(-1, "bin/xecho", argv, envp, 0);
      sys_execveat(-1, "xecho", argv, envp, 0);
      sys_execveat(fd, "xecho", argv, envp, flag);
      sys_execveat(fd, "", argv, envp, 0);
      sys_execveat(fd, NULL, argv, envp, 0);
      sys_execveat(fd, "xecho", t, envp, 0);
      sys_execveat(fd, "xecho", z, envp, 0);
  } while (0);

  /* Check execveat called with the correct arguments works. */
  if (sys_execveat(fd, "echo", argv, envp, 0) == -1) {
      perror("execveat");
      exit(EXIT_FAILURE);
  }

  closedir(dirp);
  exit(EXIT_SUCCESS);
}