root/user/testbin/badcall/common_buf.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. read_setup
  2. read_badbuf
  3. read_cleanup
  4. write_setup
  5. write_badbuf
  6. write_cleanup
  7. getdirentry_setup
  8. getdirentry_badbuf
  9. getdirentry_cleanup
  10. readlink_setup
  11. readlink_badbuf
  12. readlink_cleanup
  13. getcwd_setup
  14. getcwd_cleanup
  15. getcwd_badbuf
  16. common_badbuf
  17. any_badbuf

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

/* [<][>][^][v][top][bottom][index][help] */