fix_options.c revision 1.10 1 1.10 mrg /* $NetBSD: fix_options.c,v 1.10 2006/05/09 20:18:06 mrg 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.10 mrg __RCSID("$NetBSD: fix_options.c,v 1.10 2006/05/09 20:18:06 mrg 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.10 mrg int ipproto;
50 1.10 mrg socklen_t optsize = sizeof(optbuf);
51 1.1 mrg struct protoent *ip;
52 1.1 mrg int fd = request->fd;
53 1.2 mrg int len = sizeof lbuf;
54 1.4 itojun unsigned int opt;
55 1.4 itojun int optlen;
56 1.4 itojun struct in_addr dummy;
57 1.5 itojun struct sockaddr_storage ss;
58 1.10 mrg socklen_t sslen;
59 1.1 mrg
60 1.5 itojun /*
61 1.5 itojun * check if this is AF_INET socket
62 1.5 itojun * XXX IPv6 support?
63 1.5 itojun */
64 1.5 itojun sslen = sizeof(ss);
65 1.10 mrg if (getsockname(fd, (struct sockaddr *)(void *)&ss, &sslen) < 0) {
66 1.7 wiz syslog(LOG_ERR, "getsockname: %m");
67 1.5 itojun clean_exit(request);
68 1.5 itojun }
69 1.5 itojun if (ss.ss_family != AF_INET)
70 1.5 itojun return;
71 1.5 itojun
72 1.1 mrg if ((ip = getprotobyname("ip")) != 0)
73 1.1 mrg ipproto = ip->p_proto;
74 1.1 mrg else
75 1.1 mrg ipproto = IPPROTO_IP;
76 1.1 mrg
77 1.10 mrg if (getsockopt(fd, ipproto, IP_OPTIONS, optbuf, &optsize) == 0
78 1.1 mrg && optsize != 0) {
79 1.4 itojun
80 1.4 itojun /*
81 1.4 itojun * Horror! 4.[34] BSD getsockopt() prepends the first-hop destination
82 1.4 itojun * address to the result IP options list when source routing options
83 1.4 itojun * are present (see <netinet/ip_var.h>), but produces no output for
84 1.4 itojun * other IP options. Solaris 2.x getsockopt() does produce output for
85 1.4 itojun * non-routing IP options, and uses the same format as BSD even when
86 1.4 itojun * the space for the destination address is unused. The code below
87 1.4 itojun * does the right thing with 4.[34]BSD derivatives and Solaris 2, but
88 1.4 itojun * may occasionally miss source routing options on incompatible
89 1.4 itojun * systems such as Linux. Their choice.
90 1.4 itojun *
91 1.4 itojun * Look for source routing options. Drop the connection when one is
92 1.4 itojun * found. Just wiping the IP options is insufficient: we would still
93 1.4 itojun * help the attacker by providing a real TCP sequence number, and the
94 1.4 itojun * attacker would still be able to send packets (blind spoofing). I
95 1.4 itojun * discussed this attack with Niels Provos, half a year before the
96 1.4 itojun * attack was described in open mailing lists.
97 1.4 itojun *
98 1.4 itojun * It would be cleaner to just return a yes/no reply and let the caller
99 1.4 itojun * decide how to deal with it. Resident servers should not terminate.
100 1.4 itojun * However I am not prepared to make changes to internal interfaces
101 1.4 itojun * on short notice.
102 1.4 itojun */
103 1.4 itojun #define ADDR_LEN sizeof(dummy.s_addr)
104 1.4 itojun
105 1.4 itojun for (cp = optbuf + ADDR_LEN; cp < optbuf + optsize; cp += optlen) {
106 1.4 itojun opt = cp[IPOPT_OPTVAL];
107 1.4 itojun if (opt == IPOPT_LSRR || opt == IPOPT_SSRR) {
108 1.4 itojun syslog(LOG_WARNING,
109 1.4 itojun "refused connect from %s with IP source routing options",
110 1.4 itojun eval_client(request));
111 1.4 itojun shutdown(fd, 2);
112 1.4 itojun return;
113 1.4 itojun }
114 1.4 itojun if (opt == IPOPT_EOL)
115 1.4 itojun break;
116 1.4 itojun if (opt == IPOPT_NOP) {
117 1.4 itojun optlen = 1;
118 1.8 itojun } else if (&cp[IPOPT_OLEN] < optbuf + optsize) {
119 1.4 itojun optlen = cp[IPOPT_OLEN];
120 1.9 itojun if (optlen < 2 || cp + optlen >= optbuf + optsize) {
121 1.8 itojun syslog(LOG_WARNING,
122 1.8 itojun "refused connect from %s with malformed IP options",
123 1.8 itojun eval_client(request));
124 1.8 itojun shutdown(fd, 2);
125 1.8 itojun return;
126 1.8 itojun }
127 1.8 itojun } else {
128 1.8 itojun syslog(LOG_WARNING,
129 1.8 itojun "refused connect from %s with malformed IP options",
130 1.8 itojun eval_client(request));
131 1.8 itojun shutdown(fd, 2);
132 1.8 itojun return;
133 1.4 itojun }
134 1.4 itojun }
135 1.1 mrg lp = lbuf;
136 1.1 mrg for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
137 1.2 mrg len -= snprintf(lp, len, " %2.2x", *cp);
138 1.1 mrg syslog(LOG_NOTICE,
139 1.1 mrg "connect from %s with IP options (ignored):%s",
140 1.1 mrg eval_client(request), lbuf);
141 1.1 mrg if (setsockopt(fd, ipproto, IP_OPTIONS, (char *) 0, optsize) != 0) {
142 1.1 mrg syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
143 1.4 itojun shutdown(fd, 2);
144 1.1 mrg }
145 1.1 mrg }
146 1.1 mrg #endif
147 1.1 mrg }
148