Skip to content

Commit ed6e4ef

Browse files
Julian Anastasovdavem330
authored andcommitted
netfilter: Fix ip_route_me_harder triggering ip_rt_bug
Avoid creating input routes with ip_route_me_harder. It does not work for locally generated packets. Instead, restrict sockets to provide valid saddr for output route (or unicast saddr for transparent proxy). For other traffic allow saddr to be unicast or local but if callers forget to check saddr type use 0 for the output route. The resulting handling should be: - REJECT TCP: - in INPUT we can provide addr_type = RTN_LOCAL but better allow rejecting traffic delivered with local route (no IP address => use RTN_UNSPEC to allow also RTN_UNICAST). - FORWARD: RTN_UNSPEC => allow RTN_LOCAL/RTN_UNICAST saddr, add fix to ignore RTN_BROADCAST and RTN_MULTICAST - OUTPUT: RTN_UNSPEC - NAT, mangle, ip_queue, nf_ip_reroute: RTN_UNSPEC in LOCAL_OUT - IPVS: - use RTN_LOCAL in LOCAL_OUT and FORWARD after SNAT to restrict saddr to be local Signed-off-by: Julian Anastasov <ja@ssi.bg> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 353e5c9 commit ed6e4ef

2 files changed

Lines changed: 26 additions & 48 deletions

File tree

‎net/ipv4/netfilter.c‎

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,35 @@ int ip_route_me_harder(struct sk_buff *skb, unsigned addr_type)
1717
const struct iphdr *iph = ip_hdr(skb);
1818
struct rtable *rt;
1919
struct flowi4 fl4 = {};
20-
unsigned long orefdst;
20+
__be32 saddr = iph->saddr;
21+
__u8 flags = 0;
2122
unsigned int hh_len;
22-
unsigned int type;
2323

24-
type = inet_addr_type(net, iph->saddr);
25-
if (skb->sk && inet_sk(skb->sk)->transparent)
26-
type = RTN_LOCAL;
27-
if (addr_type == RTN_UNSPEC)
28-
addr_type = type;
24+
if (!skb->sk && addr_type != RTN_LOCAL) {
25+
if (addr_type == RTN_UNSPEC)
26+
addr_type = inet_addr_type(net, saddr);
27+
if (addr_type == RTN_LOCAL || addr_type == RTN_UNICAST)
28+
flags |= FLOWI_FLAG_ANYSRC;
29+
else
30+
saddr = 0;
31+
}
2932

3033
/* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
3134
* packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook.
3235
*/
33-
if (addr_type == RTN_LOCAL) {
34-
fl4.daddr = iph->daddr;
35-
if (type == RTN_LOCAL)
36-
fl4.saddr = iph->saddr;
37-
fl4.flowi4_tos = RT_TOS(iph->tos);
38-
fl4.flowi4_oif = skb->sk ? skb->sk->sk_bound_dev_if : 0;
39-
fl4.flowi4_mark = skb->mark;
40-
fl4.flowi4_flags = skb->sk ? inet_sk_flowi_flags(skb->sk) : 0;
41-
rt = ip_route_output_key(net, &fl4);
42-
if (IS_ERR(rt))
43-
return -1;
44-
45-
/* Drop old route. */
46-
skb_dst_drop(skb);
47-
skb_dst_set(skb, &rt->dst);
48-
} else {
49-
/* non-local src, find valid iif to satisfy
50-
* rp-filter when calling ip_route_input. */
51-
fl4.daddr = iph->saddr;
52-
rt = ip_route_output_key(net, &fl4);
53-
if (IS_ERR(rt))
54-
return -1;
36+
fl4.daddr = iph->daddr;
37+
fl4.saddr = saddr;
38+
fl4.flowi4_tos = RT_TOS(iph->tos);
39+
fl4.flowi4_oif = skb->sk ? skb->sk->sk_bound_dev_if : 0;
40+
fl4.flowi4_mark = skb->mark;
41+
fl4.flowi4_flags = skb->sk ? inet_sk_flowi_flags(skb->sk) : flags;
42+
rt = ip_route_output_key(net, &fl4);
43+
if (IS_ERR(rt))
44+
return -1;
5545

56-
orefdst = skb->_skb_refdst;
57-
if (ip_route_input(skb, iph->daddr, iph->saddr,
58-
RT_TOS(iph->tos), rt->dst.dev) != 0) {
59-
dst_release(&rt->dst);
60-
return -1;
61-
}
62-
dst_release(&rt->dst);
63-
refdst_drop(orefdst);
64-
}
46+
/* Drop old route. */
47+
skb_dst_drop(skb);
48+
skb_dst_set(skb, &rt->dst);
6549

6650
if (skb_dst(skb)->error)
6751
return -1;

‎net/ipv4/netfilter/ipt_REJECT.c‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ static void send_reset(struct sk_buff *oldskb, int hook)
4040
struct iphdr *niph;
4141
const struct tcphdr *oth;
4242
struct tcphdr _otcph, *tcph;
43-
unsigned int addr_type;
4443

4544
/* IP header checks: fragment. */
4645
if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
@@ -55,6 +54,9 @@ static void send_reset(struct sk_buff *oldskb, int hook)
5554
if (oth->rst)
5655
return;
5756

57+
if (skb_rtable(oldskb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
58+
return;
59+
5860
/* Check checksum */
5961
if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
6062
return;
@@ -101,19 +103,11 @@ static void send_reset(struct sk_buff *oldskb, int hook)
101103
nskb->csum_start = (unsigned char *)tcph - nskb->head;
102104
nskb->csum_offset = offsetof(struct tcphdr, check);
103105

104-
addr_type = RTN_UNSPEC;
105-
if (hook != NF_INET_FORWARD
106-
#ifdef CONFIG_BRIDGE_NETFILTER
107-
|| (nskb->nf_bridge && nskb->nf_bridge->mask & BRNF_BRIDGED)
108-
#endif
109-
)
110-
addr_type = RTN_LOCAL;
111-
112106
/* ip_route_me_harder expects skb->dst to be set */
113107
skb_dst_set_noref(nskb, skb_dst(oldskb));
114108

115109
nskb->protocol = htons(ETH_P_IP);
116-
if (ip_route_me_harder(nskb, addr_type))
110+
if (ip_route_me_harder(nskb, RTN_UNSPEC))
117111
goto free_nskb;
118112

119113
niph->ttl = ip4_dst_hoplimit(skb_dst(nskb));

0 commit comments

Comments
 (0)