os161-1.99
 All Data Structures
bad_ioctl.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * ioctl 
00032  */
00033 
00034 #include <sys/types.h>
00035 #include <sys/ioctl.h>
00036 #include <unistd.h>
00037 #include <stdio.h>
00038 #include <errno.h>
00039 
00040 #include "config.h"
00041 #include "test.h"
00042 
00043 static
00044 void
00045 one_ioctl_badbuf(int fd, int code, const char *codename,
00046                  void *ptr, const char *ptrdesc)
00047 {
00048         char desc[128];
00049         int rv;
00050 
00051         snprintf(desc, sizeof(desc), "ioctl %s with %s", codename, ptrdesc);
00052         rv = ioctl(fd, code, ptr);
00053         report_test(rv, errno, EFAULT, desc);
00054 }
00055 
00056 static
00057 void
00058 any_ioctl_badbuf(int fd, int code, const char *codename)
00059 {
00060         one_ioctl_badbuf(fd, code, codename, NULL, "NULL pointer");
00061         one_ioctl_badbuf(fd, code, codename, INVAL_PTR, "invalid pointer");
00062         one_ioctl_badbuf(fd, code, codename, KERN_PTR, "kernel pointer");
00063 }
00064 
00065 #define IOCTL(fd, sym) any_ioctl_badbuf(fd, sym, #sym)
00066 
00067 static
00068 void
00069 ioctl_badbuf(void)
00070 {
00071         /*
00072          * Since we don't actually define any ioctls, this code won't
00073          * actually run. But if you do define ioctls, turn these tests
00074          * on for those that actually use the data buffer argument for
00075          * anything.
00076          */
00077 
00078         /* IOCTL(STDIN_FILENO, TIOCGETA); */
00079 
00080 
00081         /* suppress gcc warning */
00082         (void)any_ioctl_badbuf;
00083 }
00084 
00085 static
00086 void
00087 ioctl_badcode(void)
00088 {
00089         int rv;
00090         rv = ioctl(STDIN_FILENO, NONEXIST_IOCTL, NULL);
00091         report_test(rv, errno, EIOCTL, "invalid ioctl");
00092 }
00093 
00094 void
00095 test_ioctl(void)
00096 {
00097         test_ioctl_fd();
00098 
00099         /* Since we don't actually define any ioctls, this is not meaningful */
00100         ioctl_badcode();
00101         ioctl_badbuf();
00102 }
 All Data Structures