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