Home | History | Annotate | Line # | Download | only in libwrap
fix_options.c revision 1.8
      1  1.8    itojun /*	$NetBSD: fix_options.c,v 1.8 2002/05/24 05:30:54 itojun Exp $	*/
      2  1.3  christos 
      3  1.1       mrg  /*
      4  1.1       mrg   * Routine to disable IP-level socket options. This code was taken from 4.4BSD
      5  1.4    itojun   * rlogind and kernel source, but all mistakes in it are my fault.
      6  1.1       mrg   *
      7  1.1       mrg   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
      8  1.1       mrg   */
      9  1.1       mrg 
     10  1.3  christos #include <sys/cdefs.h>
     11  1.1       mrg #ifndef lint
     12  1.3  christos #if 0
     13  1.4    itojun static char sccsid[] = "@(#) fix_options.c 1.6 97/04/08 02:29:19";
     14  1.3  christos #else
     15  1.8    itojun __RCSID("$NetBSD: fix_options.c,v 1.8 2002/05/24 05:30:54 itojun Exp $");
     16  1.3  christos #endif
     17  1.1       mrg #endif
     18  1.1       mrg 
     19  1.1       mrg #include <sys/types.h>
     20  1.1       mrg #include <sys/param.h>
     21  1.3  christos #include <sys/socket.h>
     22  1.1       mrg #include <netinet/in.h>
     23  1.4    itojun #include <netinet/in_systm.h>
     24  1.4    itojun #include <netinet/ip.h>
     25  1.1       mrg #include <netdb.h>
     26  1.1       mrg #include <stdio.h>
     27  1.1       mrg #include <syslog.h>
     28  1.3  christos #include <stdlib.h>
     29  1.3  christos #include <unistd.h>
     30  1.4    itojun 
     31  1.4    itojun #ifndef IPOPT_OPTVAL
     32  1.4    itojun #define IPOPT_OPTVAL	0
     33  1.4    itojun #define IPOPT_OLEN	1
     34  1.4    itojun #endif
     35  1.4    itojun 
     36  1.1       mrg #include "tcpd.h"
     37  1.1       mrg 
     38  1.4    itojun #define BUFFER_SIZE	512		/* Was: BUFSIZ */
     39  1.4    itojun 
     40  1.1       mrg /* fix_options - get rid of IP-level socket options */
     41  1.1       mrg 
     42  1.3  christos void
     43  1.1       mrg fix_options(request)
     44  1.1       mrg struct request_info *request;
     45  1.1       mrg {
     46  1.1       mrg #ifdef IP_OPTIONS
     47  1.4    itojun     unsigned char optbuf[BUFFER_SIZE / 3], *cp;
     48  1.4    itojun     char    lbuf[BUFFER_SIZE], *lp;
     49  1.1       mrg     int     optsize = sizeof(optbuf), ipproto;
     50  1.1       mrg     struct protoent *ip;
     51  1.1       mrg     int     fd = request->fd;
     52  1.2       mrg     int     len = sizeof lbuf;
     53  1.4    itojun     unsigned int opt;
     54  1.4    itojun     int     optlen;
     55  1.4    itojun     struct in_addr dummy;
     56  1.5    itojun     struct sockaddr_storage ss;
     57  1.5    itojun     int sslen;
     58  1.1       mrg 
     59  1.5    itojun     /*
     60  1.5    itojun      * check if this is AF_INET socket
     61  1.5    itojun      * XXX IPv6 support?
     62  1.5    itojun      */
     63  1.5    itojun     sslen = sizeof(ss);
     64  1.6    itojun     if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0) {
     65  1.7       wiz 	syslog(LOG_ERR, "getsockname: %m");
     66  1.5    itojun 	clean_exit(request);
     67  1.5    itojun     }
     68  1.5    itojun     if (ss.ss_family != AF_INET)
     69  1.5    itojun 	return;
     70  1.5    itojun 
     71  1.1       mrg     if ((ip = getprotobyname("ip")) != 0)
     72  1.1       mrg 	ipproto = ip->p_proto;
     73  1.1       mrg     else
     74  1.1       mrg 	ipproto = IPPROTO_IP;
     75  1.1       mrg 
     76  1.1       mrg     if (getsockopt(fd, ipproto, IP_OPTIONS, (char *) optbuf, &optsize) == 0
     77  1.1       mrg 	&& optsize != 0) {
     78  1.4    itojun 
     79  1.4    itojun 	/*
     80  1.4    itojun 	 * Horror! 4.[34] BSD getsockopt() prepends the first-hop destination
     81  1.4    itojun 	 * address to the result IP options list when source routing options
     82  1.4    itojun 	 * are present (see <netinet/ip_var.h>), but produces no output for
     83  1.4    itojun 	 * other IP options. Solaris 2.x getsockopt() does produce output for
     84  1.4    itojun 	 * non-routing IP options, and uses the same format as BSD even when
     85  1.4    itojun 	 * the space for the destination address is unused. The code below
     86  1.4    itojun 	 * does the right thing with 4.[34]BSD derivatives and Solaris 2, but
     87  1.4    itojun 	 * may occasionally miss source routing options on incompatible
     88  1.4    itojun 	 * systems such as Linux. Their choice.
     89  1.4    itojun 	 *
     90  1.4    itojun 	 * Look for source routing options. Drop the connection when one is
     91  1.4    itojun 	 * found. Just wiping the IP options is insufficient: we would still
     92  1.4    itojun 	 * help the attacker by providing a real TCP sequence number, and the
     93  1.4    itojun 	 * attacker would still be able to send packets (blind spoofing). I
     94  1.4    itojun 	 * discussed this attack with Niels Provos, half a year before the
     95  1.4    itojun 	 * attack was described in open mailing lists.
     96  1.4    itojun 	 *
     97  1.4    itojun 	 * It would be cleaner to just return a yes/no reply and let the caller
     98  1.4    itojun 	 * decide how to deal with it. Resident servers should not terminate.
     99  1.4    itojun 	 * However I am not prepared to make changes to internal interfaces
    100  1.4    itojun 	 * on short notice.
    101  1.4    itojun 	 */
    102  1.4    itojun #define ADDR_LEN sizeof(dummy.s_addr)
    103  1.4    itojun 
    104  1.4    itojun 	for (cp = optbuf + ADDR_LEN; cp < optbuf + optsize; cp += optlen) {
    105  1.4    itojun 	    opt = cp[IPOPT_OPTVAL];
    106  1.4    itojun 	    if (opt == IPOPT_LSRR || opt == IPOPT_SSRR) {
    107  1.4    itojun 		syslog(LOG_WARNING,
    108  1.4    itojun 		   "refused connect from %s with IP source routing options",
    109  1.4    itojun 		       eval_client(request));
    110  1.4    itojun 		shutdown(fd, 2);
    111  1.4    itojun 		return;
    112  1.4    itojun 	    }
    113  1.4    itojun 	    if (opt == IPOPT_EOL)
    114  1.4    itojun 		break;
    115  1.4    itojun 	    if (opt == IPOPT_NOP) {
    116  1.4    itojun 		optlen = 1;
    117  1.8    itojun 	    } else if (&cp[IPOPT_OLEN] < optbuf + optsize) {
    118  1.4    itojun 		optlen = cp[IPOPT_OLEN];
    119  1.4    itojun 		if (optlen <= 0)		/* Do not loop! */
    120  1.4    itojun 		    break;
    121  1.8    itojun 		if (cp + optlen >= optbuf + optsize) {
    122  1.8    itojun 		    syslog(LOG_WARNING,
    123  1.8    itojun 		       "refused connect from %s with malformed IP options",
    124  1.8    itojun 			   eval_client(request));
    125  1.8    itojun 		    shutdown(fd, 2);
    126  1.8    itojun 		    return;
    127  1.8    itojun 		}
    128  1.8    itojun 	    } else {
    129  1.8    itojun 		syslog(LOG_WARNING,
    130  1.8    itojun 		   "refused connect from %s with malformed IP options",
    131  1.8    itojun 		       eval_client(request));
    132  1.8    itojun 		shutdown(fd, 2);
    133  1.8    itojun 		return;
    134  1.4    itojun 	    }
    135  1.4    itojun 	}
    136  1.1       mrg 	lp = lbuf;
    137  1.1       mrg 	for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
    138  1.2       mrg 	    len -= snprintf(lp, len, " %2.2x", *cp);
    139  1.1       mrg 	syslog(LOG_NOTICE,
    140  1.1       mrg 	       "connect from %s with IP options (ignored):%s",
    141  1.1       mrg 	       eval_client(request), lbuf);
    142  1.1       mrg 	if (setsockopt(fd, ipproto, IP_OPTIONS, (char *) 0, optsize) != 0) {
    143  1.1       mrg 	    syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
    144  1.4    itojun 	    shutdown(fd, 2);
    145  1.1       mrg 	}
    146  1.1       mrg     }
    147  1.1       mrg #endif
    148  1.1       mrg }
    149