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