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