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