/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* Calls with invalid transfer buffers31*/3233#include <sys/types.h>34#include <sys/stat.h>35#include <stdio.h>36#include <stdlib.h>37#include <string.h>38#include <unistd.h>39#include <limits.h>40#include <errno.h>41#include <err.h>4243#include "config.h"44#include "test.h"4546static int buf_fd;4748struct buftest {49int (*setup)(void);50int (*op)(void *);51void (*cleanup)(void);52const char *name;53};5455////////////////////////////////////////////////////////////5657static58int59read_setup(void)60{61buf_fd = open_testfile("i do not like green eggs and ham");62if (buf_fd<0) {63return -1;64}65return 0;66}6768static69int70read_badbuf(void *buf)71{72return read(buf_fd, buf, 128);73}7475static76void77read_cleanup(void)78{79close(buf_fd);80remove(TESTFILE);81}8283//////////8485static86int87write_setup(void)88{89buf_fd = open_testfile(NULL);90if (buf_fd<0) {91return -1;92}93return 0;94}9596static97int98write_badbuf(void *ptr)99{100return write(buf_fd, ptr, 128);101}102103static104void105write_cleanup(void)106{107close(buf_fd);108remove(TESTFILE);109}110111//////////112113static114int115getdirentry_setup(void)116{117buf_fd = open(".", O_RDONLY);118if (buf_fd < 0) {119warn("UH-OH: couldn't open .");120return -1;121}122return 0;123}124125static126int127getdirentry_badbuf(void *ptr)128{129return getdirentry(buf_fd, ptr, 1024);130}131132static133void134getdirentry_cleanup(void)135{136close(buf_fd);137}138139//////////140141static142int143readlink_setup(void)144{145return create_testlink();146}147148static149int150readlink_badbuf(void *buf)151{152return readlink(TESTLINK, buf, 168);153}154155static156void157readlink_cleanup(void)158{159remove(TESTLINK);160}161162//////////163164static int getcwd_setup(void) { return 0; }165static void getcwd_cleanup(void) {}166167static168int169getcwd_badbuf(void *buf)170{171return __getcwd(buf, 408);172}173174////////////////////////////////////////////////////////////175176static177void178common_badbuf(struct buftest *info, void *buf, const char *bufdesc)179{180char mydesc[128];181int rv;182183snprintf(mydesc, sizeof(mydesc), "%s with %s buffer",184info->name, bufdesc);185info->setup();186rv = info->op(buf);187report_test(rv, errno, EFAULT, mydesc);188info->cleanup();189}190191static192void193any_badbuf(struct buftest *info)194{195common_badbuf(info, NULL, "NULL");196common_badbuf(info, INVAL_PTR, "invalid");197common_badbuf(info, KERN_PTR, "kernel-space");198}199200////////////////////////////////////////////////////////////201202#define T(call) \203void \204test_##call##_buf(void) \205{ \206static struct buftest info = { \207call##_setup, \208call##_badbuf, \209call##_cleanup, \210#call, \211}; \212any_badbuf(&info); \213}214215T(read);216T(write);217T(getdirentry);218T(readlink);219T(getcwd);220221222