bad_execv.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * bad calls to execv()
  31. */
  32. #include <sys/types.h>
  33. #include <stdlib.h>
  34. #include <unistd.h>
  35. #include <errno.h>
  36. #include <err.h>
  37. #include "config.h"
  38. #include "test.h"
  39. static
  40. int
  41. exec_common_fork(void)
  42. {
  43. int pid, rv, status;
  44. pid = fork();
  45. if (pid<0) {
  46. warn("UH-OH: fork failed");
  47. return -1;
  48. }
  49. if (pid==0) {
  50. /* child */
  51. return 0;
  52. }
  53. rv = waitpid(pid, &status, 0);
  54. if (rv == -1) {
  55. warn("UH-OH: waitpid failed");
  56. return -1;
  57. }
  58. if (!WIFEXITED(status) || WEXITSTATUS(status) != MAGIC_STATUS) {
  59. warnx("FAILURE: wrong exit code of subprocess");
  60. }
  61. return 1;
  62. }
  63. static
  64. void
  65. exec_badprog(const void *prog, const char *desc)
  66. {
  67. int rv;
  68. char *args[2];
  69. args[0] = (char *)"foo";
  70. args[1] = NULL;
  71. if (exec_common_fork() != 0) {
  72. return;
  73. }
  74. rv = execv(prog, args);
  75. report_test(rv, errno, EFAULT, desc);
  76. exit(MAGIC_STATUS);
  77. }
  78. static
  79. void
  80. exec_emptyprog(void)
  81. {
  82. int rv;
  83. char *args[2];
  84. args[0] = (char *)"foo";
  85. args[1] = NULL;
  86. if (exec_common_fork() != 0) {
  87. return;
  88. }
  89. rv = execv("", args);
  90. report_test2(rv, errno, EINVAL, EISDIR, "exec the empty string");
  91. exit(MAGIC_STATUS);
  92. }
  93. static
  94. void
  95. exec_badargs(void *args, const char *desc)
  96. {
  97. int rv;
  98. if (exec_common_fork() != 0) {
  99. return;
  100. }
  101. rv = execv("/bin/true", args);
  102. report_test(rv, errno, EFAULT, desc);
  103. exit(MAGIC_STATUS);
  104. }
  105. static
  106. void
  107. exec_onearg(void *ptr, const char *desc)
  108. {
  109. int rv;
  110. char *args[3];
  111. args[0] = (char *)"foo";
  112. args[1] = (char *)ptr;
  113. args[2] = NULL;
  114. if (exec_common_fork() != 0) {
  115. return;
  116. }
  117. rv = execv("/bin/true", args);
  118. report_test(rv, errno, EFAULT, desc);
  119. exit(MAGIC_STATUS);
  120. }
  121. void
  122. test_execv(void)
  123. {
  124. exec_badprog(NULL, "exec NULL");
  125. exec_badprog(INVAL_PTR, "exec invalid pointer");
  126. exec_badprog(KERN_PTR, "exec kernel pointer");
  127. exec_emptyprog();
  128. exec_badargs(NULL, "exec /bin/true with NULL arglist");
  129. exec_badargs(INVAL_PTR, "exec /bin/true with invalid pointer arglist");
  130. exec_badargs(KERN_PTR, "exec /bin/true with kernel pointer arglist");
  131. exec_onearg(INVAL_PTR, "exec /bin/true with invalid pointer arg");
  132. exec_onearg(KERN_PTR, "exec /bin/true with kernel pointer arg");
  133. }