xref: /netbsd-src/lib/libwrap/fix_options.c (revision e1a2f47f1264dd9755e9e747d45ba016dff2334f)
1*e1a2f47fSmatt /*	$NetBSD: fix_options.c,v 1.11 2012/03/21 10:10:37 matt Exp $	*/
2d6ddaab4Schristos 
3541be36cSmrg  /*
4541be36cSmrg   * Routine to disable IP-level socket options. This code was taken from 4.4BSD
5b98c2633Sitojun   * rlogind and kernel source, but all mistakes in it are my fault.
6541be36cSmrg   *
7541be36cSmrg   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
8541be36cSmrg   */
9541be36cSmrg 
10d6ddaab4Schristos #include <sys/cdefs.h>
11541be36cSmrg #ifndef lint
12d6ddaab4Schristos #if 0
13b98c2633Sitojun static char sccsid[] = "@(#) fix_options.c 1.6 97/04/08 02:29:19";
14d6ddaab4Schristos #else
15*e1a2f47fSmatt __RCSID("$NetBSD: fix_options.c,v 1.11 2012/03/21 10:10:37 matt Exp $");
16d6ddaab4Schristos #endif
17541be36cSmrg #endif
18541be36cSmrg 
19541be36cSmrg #include <sys/types.h>
20541be36cSmrg #include <sys/param.h>
21d6ddaab4Schristos #include <sys/socket.h>
22541be36cSmrg #include <netinet/in.h>
23b98c2633Sitojun #include <netinet/in_systm.h>
24b98c2633Sitojun #include <netinet/ip.h>
25541be36cSmrg #include <netdb.h>
26541be36cSmrg #include <stdio.h>
27541be36cSmrg #include <syslog.h>
28d6ddaab4Schristos #include <stdlib.h>
29d6ddaab4Schristos #include <unistd.h>
30b98c2633Sitojun 
31b98c2633Sitojun #ifndef IPOPT_OPTVAL
32b98c2633Sitojun #define IPOPT_OPTVAL	0
33b98c2633Sitojun #define IPOPT_OLEN	1
34b98c2633Sitojun #endif
35b98c2633Sitojun 
36541be36cSmrg #include "tcpd.h"
37541be36cSmrg 
38b98c2633Sitojun #define BUFFER_SIZE	512		/* Was: BUFSIZ */
39b98c2633Sitojun 
40541be36cSmrg /* fix_options - get rid of IP-level socket options */
41541be36cSmrg 
42d6ddaab4Schristos void
fix_options(struct request_info * request)43*e1a2f47fSmatt fix_options(struct request_info *request)
44541be36cSmrg {
45541be36cSmrg #ifdef IP_OPTIONS
46b98c2633Sitojun     unsigned char optbuf[BUFFER_SIZE / 3], *cp;
47b98c2633Sitojun     char    lbuf[BUFFER_SIZE], *lp;
480c37c63eSmrg     int     ipproto;
490c37c63eSmrg     socklen_t optsize = sizeof(optbuf);
50541be36cSmrg     struct protoent *ip;
51541be36cSmrg     int     fd = request->fd;
529cd5492cSmrg     int     len = sizeof lbuf;
53b98c2633Sitojun     unsigned int opt;
54b98c2633Sitojun     int     optlen;
55b98c2633Sitojun     struct in_addr dummy;
561e8c736aSitojun     struct sockaddr_storage ss;
570c37c63eSmrg     socklen_t sslen;
581e8c736aSitojun 
591e8c736aSitojun     /*
601e8c736aSitojun      * check if this is AF_INET socket
611e8c736aSitojun      * XXX IPv6 support?
621e8c736aSitojun      */
631e8c736aSitojun     sslen = sizeof(ss);
640c37c63eSmrg     if (getsockname(fd, (struct sockaddr *)(void *)&ss, &sslen) < 0) {
65d70fe2e8Swiz 	syslog(LOG_ERR, "getsockname: %m");
661e8c736aSitojun 	clean_exit(request);
671e8c736aSitojun     }
681e8c736aSitojun     if (ss.ss_family != AF_INET)
691e8c736aSitojun 	return;
70541be36cSmrg 
71541be36cSmrg     if ((ip = getprotobyname("ip")) != 0)
72541be36cSmrg 	ipproto = ip->p_proto;
73541be36cSmrg     else
74541be36cSmrg 	ipproto = IPPROTO_IP;
75541be36cSmrg 
760c37c63eSmrg     if (getsockopt(fd, ipproto, IP_OPTIONS, optbuf, &optsize) == 0
77541be36cSmrg 	&& optsize != 0) {
78b98c2633Sitojun 
79b98c2633Sitojun 	/*
80b98c2633Sitojun 	 * Horror! 4.[34] BSD getsockopt() prepends the first-hop destination
81b98c2633Sitojun 	 * address to the result IP options list when source routing options
82b98c2633Sitojun 	 * are present (see <netinet/ip_var.h>), but produces no output for
83b98c2633Sitojun 	 * other IP options. Solaris 2.x getsockopt() does produce output for
84b98c2633Sitojun 	 * non-routing IP options, and uses the same format as BSD even when
85b98c2633Sitojun 	 * the space for the destination address is unused. The code below
86b98c2633Sitojun 	 * does the right thing with 4.[34]BSD derivatives and Solaris 2, but
87b98c2633Sitojun 	 * may occasionally miss source routing options on incompatible
88b98c2633Sitojun 	 * systems such as Linux. Their choice.
89b98c2633Sitojun 	 *
90b98c2633Sitojun 	 * Look for source routing options. Drop the connection when one is
91b98c2633Sitojun 	 * found. Just wiping the IP options is insufficient: we would still
92b98c2633Sitojun 	 * help the attacker by providing a real TCP sequence number, and the
93b98c2633Sitojun 	 * attacker would still be able to send packets (blind spoofing). I
94b98c2633Sitojun 	 * discussed this attack with Niels Provos, half a year before the
95b98c2633Sitojun 	 * attack was described in open mailing lists.
96b98c2633Sitojun 	 *
97b98c2633Sitojun 	 * It would be cleaner to just return a yes/no reply and let the caller
98b98c2633Sitojun 	 * decide how to deal with it. Resident servers should not terminate.
99b98c2633Sitojun 	 * However I am not prepared to make changes to internal interfaces
100b98c2633Sitojun 	 * on short notice.
101b98c2633Sitojun 	 */
102b98c2633Sitojun #define ADDR_LEN sizeof(dummy.s_addr)
103b98c2633Sitojun 
104b98c2633Sitojun 	for (cp = optbuf + ADDR_LEN; cp < optbuf + optsize; cp += optlen) {
105b98c2633Sitojun 	    opt = cp[IPOPT_OPTVAL];
106b98c2633Sitojun 	    if (opt == IPOPT_LSRR || opt == IPOPT_SSRR) {
107b98c2633Sitojun 		syslog(LOG_WARNING,
108b98c2633Sitojun 		   "refused connect from %s with IP source routing options",
109b98c2633Sitojun 		       eval_client(request));
110b98c2633Sitojun 		shutdown(fd, 2);
111b98c2633Sitojun 		return;
112b98c2633Sitojun 	    }
113b98c2633Sitojun 	    if (opt == IPOPT_EOL)
114b98c2633Sitojun 		break;
115b98c2633Sitojun 	    if (opt == IPOPT_NOP) {
116b98c2633Sitojun 		optlen = 1;
1175faa3858Sitojun 	    } else if (&cp[IPOPT_OLEN] < optbuf + optsize) {
118b98c2633Sitojun 		optlen = cp[IPOPT_OLEN];
1195ab78ccfSitojun 		if (optlen < 2 || cp + optlen >= optbuf + optsize) {
1205faa3858Sitojun 		    syslog(LOG_WARNING,
1215faa3858Sitojun 		       "refused connect from %s with malformed IP options",
1225faa3858Sitojun 			   eval_client(request));
1235faa3858Sitojun 		    shutdown(fd, 2);
1245faa3858Sitojun 		    return;
1255faa3858Sitojun 		}
1265faa3858Sitojun 	    } else {
1275faa3858Sitojun 		syslog(LOG_WARNING,
1285faa3858Sitojun 		   "refused connect from %s with malformed IP options",
1295faa3858Sitojun 		       eval_client(request));
1305faa3858Sitojun 		shutdown(fd, 2);
1315faa3858Sitojun 		return;
132b98c2633Sitojun 	    }
133b98c2633Sitojun 	}
134541be36cSmrg 	lp = lbuf;
135541be36cSmrg 	for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
1369cd5492cSmrg 	    len -= snprintf(lp, len, " %2.2x", *cp);
137541be36cSmrg 	syslog(LOG_NOTICE,
138541be36cSmrg 	       "connect from %s with IP options (ignored):%s",
139541be36cSmrg 	       eval_client(request), lbuf);
140541be36cSmrg 	if (setsockopt(fd, ipproto, IP_OPTIONS, (char *) 0, optsize) != 0) {
141541be36cSmrg 	    syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
142b98c2633Sitojun 	    shutdown(fd, 2);
143541be36cSmrg 	}
144541be36cSmrg     }
145541be36cSmrg #endif
146541be36cSmrg }
147