common_buf.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. * Calls with invalid transfer buffers
  31. */
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include <limits.h>
  39. #include <errno.h>
  40. #include <err.h>
  41. #include "config.h"
  42. #include "test.h"
  43. static int buf_fd;
  44. struct buftest {
  45. int (*setup)(void);
  46. int (*op)(void *);
  47. void (*cleanup)(void);
  48. const char *name;
  49. };
  50. ////////////////////////////////////////////////////////////
  51. static
  52. int
  53. read_setup(void)
  54. {
  55. buf_fd = open_testfile("i do not like green eggs and ham");
  56. if (buf_fd<0) {
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. static
  62. int
  63. read_badbuf(void *buf)
  64. {
  65. return read(buf_fd, buf, 128);
  66. }
  67. static
  68. void
  69. read_cleanup(void)
  70. {
  71. close(buf_fd);
  72. remove(TESTFILE);
  73. }
  74. //////////
  75. static
  76. int
  77. write_setup(void)
  78. {
  79. buf_fd = open_testfile(NULL);
  80. if (buf_fd<0) {
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. static
  86. int
  87. write_badbuf(void *ptr)
  88. {
  89. return write(buf_fd, ptr, 128);
  90. }
  91. static
  92. void
  93. write_cleanup(void)
  94. {
  95. close(buf_fd);
  96. remove(TESTFILE);
  97. }
  98. //////////
  99. static
  100. int
  101. getdirentry_setup(void)
  102. {
  103. buf_fd = open(".", O_RDONLY);
  104. if (buf_fd < 0) {
  105. warn("UH-OH: couldn't open .");
  106. return -1;
  107. }
  108. return 0;
  109. }
  110. static
  111. int
  112. getdirentry_badbuf(void *ptr)
  113. {
  114. return getdirentry(buf_fd, ptr, 1024);
  115. }
  116. static
  117. void
  118. getdirentry_cleanup(void)
  119. {
  120. close(buf_fd);
  121. }
  122. //////////
  123. static
  124. int
  125. readlink_setup(void)
  126. {
  127. return create_testlink();
  128. }
  129. static
  130. int
  131. readlink_badbuf(void *buf)
  132. {
  133. return readlink(TESTLINK, buf, 168);
  134. }
  135. static
  136. void
  137. readlink_cleanup(void)
  138. {
  139. remove(TESTLINK);
  140. }
  141. //////////
  142. static int getcwd_setup(void) { return 0; }
  143. static void getcwd_cleanup(void) {}
  144. static
  145. int
  146. getcwd_badbuf(void *buf)
  147. {
  148. return __getcwd(buf, 408);
  149. }
  150. ////////////////////////////////////////////////////////////
  151. static
  152. void
  153. common_badbuf(struct buftest *info, void *buf, const char *bufdesc)
  154. {
  155. char mydesc[128];
  156. int rv;
  157. snprintf(mydesc, sizeof(mydesc), "%s with %s buffer",
  158. info->name, bufdesc);
  159. info->setup();
  160. rv = info->op(buf);
  161. report_test(rv, errno, EFAULT, mydesc);
  162. info->cleanup();
  163. }
  164. static
  165. void
  166. any_badbuf(struct buftest *info)
  167. {
  168. common_badbuf(info, NULL, "NULL");
  169. common_badbuf(info, INVAL_PTR, "invalid");
  170. common_badbuf(info, KERN_PTR, "kernel-space");
  171. }
  172. ////////////////////////////////////////////////////////////
  173. #define T(call) \
  174. void \
  175. test_##call##_buf(void) \
  176. { \
  177. static struct buftest info = { \
  178. call##_setup, \
  179. call##_badbuf, \
  180. call##_cleanup, \
  181. #call, \
  182. }; \
  183. any_badbuf(&info); \
  184. }
  185. T(read);
  186. T(write);
  187. T(getdirentry);
  188. T(readlink);
  189. T(getcwd);