Skip to content

Commit 628e341

Browse files
strssndktnklassert
authored andcommitted
xfrm: make local error reporting more robust
In xfrm4 and xfrm6 we need to take care about sockets of the other address family. This could happen because a 6in4 or 4in6 tunnel could get protected by ipsec. Because we don't want to have a run-time dependency on ipv6 when only using ipv4 xfrm we have to embed a pointer to the correct local_error function in xfrm_state_afinet and look it up when returning an error depending on the socket address family. Thanks to vi0ss for the great bug report: <https://bugzilla.kernel.org/show_bug.cgi?id=58691> v2: a) fix two more unsafe interpretations of skb->sk as ipv6 socket (xfrm6_local_dontfrag and __xfrm6_output) v3: a) add an EXPORT_SYMBOL_GPL(xfrm_local_error) to fix a link error when building ipv6 as a module (thanks to Steffen Klassert) Reported-by: <vi0oss@gmail.com> Cc: Steffen Klassert <steffen.klassert@secunet.com> Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org> Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
1 parent d9bf5f1 commit 628e341

File tree

7 files changed

+39
-11
lines changed

7 files changed

+39
-11
lines changed

include/net/xfrm.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,13 @@ struct xfrm_state_afinfo {
341341
struct sk_buff *skb);
342342
int (*transport_finish)(struct sk_buff *skb,
343343
int async);
344+
void (*local_error)(struct sk_buff *skb, u32 mtu);
344345
};
345346

346347
extern int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo);
347348
extern int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo);
349+
extern struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
350+
extern void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
348351

349352
extern void xfrm_state_delete_tunnel(struct xfrm_state *x);
350353

@@ -1477,6 +1480,7 @@ extern int xfrm_input_resume(struct sk_buff *skb, int nexthdr);
14771480
extern int xfrm_output_resume(struct sk_buff *skb, int err);
14781481
extern int xfrm_output(struct sk_buff *skb);
14791482
extern int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb);
1483+
extern void xfrm_local_error(struct sk_buff *skb, int mtu);
14801484
extern int xfrm4_extract_header(struct sk_buff *skb);
14811485
extern int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb);
14821486
extern int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
@@ -1497,6 +1501,7 @@ extern int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short fam
14971501
extern int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family);
14981502
extern int xfrm4_mode_tunnel_input_register(struct xfrm_tunnel *handler);
14991503
extern int xfrm4_mode_tunnel_input_deregister(struct xfrm_tunnel *handler);
1504+
extern void xfrm4_local_error(struct sk_buff *skb, u32 mtu);
15001505
extern int xfrm6_extract_header(struct sk_buff *skb);
15011506
extern int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb);
15021507
extern int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi);
@@ -1514,6 +1519,7 @@ extern int xfrm6_output(struct sk_buff *skb);
15141519
extern int xfrm6_output_finish(struct sk_buff *skb);
15151520
extern int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
15161521
u8 **prevhdr);
1522+
extern void xfrm6_local_error(struct sk_buff *skb, u32 mtu);
15171523

15181524
#ifdef CONFIG_XFRM
15191525
extern int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb);

net/ipv4/xfrm4_output.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ static int xfrm4_tunnel_check_size(struct sk_buff *skb)
3333
mtu = dst_mtu(dst);
3434
if (skb->len > mtu) {
3535
if (skb->sk)
36-
ip_local_error(skb->sk, EMSGSIZE, ip_hdr(skb)->daddr,
37-
inet_sk(skb->sk)->inet_dport, mtu);
36+
xfrm_local_error(skb, mtu);
3837
else
3938
icmp_send(skb, ICMP_DEST_UNREACH,
4039
ICMP_FRAG_NEEDED, htonl(mtu));
@@ -99,3 +98,12 @@ int xfrm4_output(struct sk_buff *skb)
9998
x->outer_mode->afinfo->output_finish,
10099
!(IPCB(skb)->flags & IPSKB_REROUTED));
101100
}
101+
102+
void xfrm4_local_error(struct sk_buff *skb, u32 mtu)
103+
{
104+
struct iphdr *hdr;
105+
106+
hdr = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
107+
ip_local_error(skb->sk, EMSGSIZE, hdr->daddr,
108+
inet_sk(skb->sk)->inet_dport, mtu);
109+
}

net/ipv4/xfrm4_state.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ static struct xfrm_state_afinfo xfrm4_state_afinfo = {
8383
.extract_input = xfrm4_extract_input,
8484
.extract_output = xfrm4_extract_output,
8585
.transport_finish = xfrm4_transport_finish,
86+
.local_error = xfrm4_local_error,
8687
};
8788

8889
void __init xfrm4_state_init(void)

net/ipv6/xfrm6_output.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ static int xfrm6_local_dontfrag(struct sk_buff *skb)
3434
struct sock *sk = skb->sk;
3535

3636
if (sk) {
37-
proto = sk->sk_protocol;
37+
if (sk->sk_family != AF_INET6)
38+
return 0;
3839

40+
proto = sk->sk_protocol;
3941
if (proto == IPPROTO_UDP || proto == IPPROTO_RAW)
4042
return inet6_sk(sk)->dontfrag;
4143
}
@@ -54,7 +56,7 @@ static void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu)
5456
ipv6_local_rxpmtu(sk, &fl6, mtu);
5557
}
5658

57-
static void xfrm6_local_error(struct sk_buff *skb, u32 mtu)
59+
void xfrm6_local_error(struct sk_buff *skb, u32 mtu)
5860
{
5961
struct flowi6 fl6;
6062
struct sock *sk = skb->sk;
@@ -80,7 +82,7 @@ static int xfrm6_tunnel_check_size(struct sk_buff *skb)
8082
if (xfrm6_local_dontfrag(skb))
8183
xfrm6_local_rxpmtu(skb, mtu);
8284
else if (skb->sk)
83-
xfrm6_local_error(skb, mtu);
85+
xfrm_local_error(skb, mtu);
8486
else
8587
icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
8688
ret = -EMSGSIZE;
@@ -142,7 +144,7 @@ static int __xfrm6_output(struct sk_buff *skb)
142144
xfrm6_local_rxpmtu(skb, mtu);
143145
return -EMSGSIZE;
144146
} else if (!skb->local_df && skb->len > mtu && skb->sk) {
145-
xfrm6_local_error(skb, mtu);
147+
xfrm_local_error(skb, mtu);
146148
return -EMSGSIZE;
147149
}
148150

net/ipv6/xfrm6_state.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ static struct xfrm_state_afinfo xfrm6_state_afinfo = {
183183
.extract_input = xfrm6_extract_input,
184184
.extract_output = xfrm6_extract_output,
185185
.transport_finish = xfrm6_transport_finish,
186+
.local_error = xfrm6_local_error,
186187
};
187188

188189
int __init xfrm6_state_init(void)

net/xfrm/xfrm_output.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,18 @@ int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
214214
return inner_mode->afinfo->extract_output(x, skb);
215215
}
216216

217+
void xfrm_local_error(struct sk_buff *skb, int mtu)
218+
{
219+
struct xfrm_state_afinfo *afinfo;
220+
221+
afinfo = xfrm_state_get_afinfo(skb->sk->sk_family);
222+
if (!afinfo)
223+
return;
224+
225+
afinfo->local_error(skb, mtu);
226+
xfrm_state_put_afinfo(afinfo);
227+
}
228+
217229
EXPORT_SYMBOL_GPL(xfrm_output);
218230
EXPORT_SYMBOL_GPL(xfrm_inner_extract_output);
231+
EXPORT_SYMBOL_GPL(xfrm_local_error);

net/xfrm/xfrm_state.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ static DEFINE_SPINLOCK(xfrm_state_lock);
3939

4040
static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
4141

42-
static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
43-
static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
44-
4542
static inline unsigned int xfrm_dst_hash(struct net *net,
4643
const xfrm_address_t *daddr,
4744
const xfrm_address_t *saddr,
@@ -1860,7 +1857,7 @@ int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
18601857
}
18611858
EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
18621859

1863-
static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1860+
struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
18641861
{
18651862
struct xfrm_state_afinfo *afinfo;
18661863
if (unlikely(family >= NPROTO))
@@ -1872,7 +1869,7 @@ static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
18721869
return afinfo;
18731870
}
18741871

1875-
static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1872+
void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
18761873
{
18771874
rcu_read_unlock();
18781875
}

0 commit comments

Comments
 (0)