Skip to content

Commit 9248694

Browse files
klassertgregkh
authored andcommitted
esp: Fix possible buffer overflow in ESP transformation
commit ebe48d3 upstream. The maximum message size that can be send is bigger than the maximum site that skb_page_frag_refill can allocate. So it is possible to write beyond the allocated buffer. Fix this by doing a fallback to COW in that case. v2: Avoid get get_order() costs as suggested by Linus Torvalds. Fixes: cac2661 ("esp4: Avoid skb_cow_data whenever possible") Fixes: 03e2a30 ("esp6: Avoid skb_cow_data whenever possible") Reported-by: valis <sec@valis.email> Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com> Signed-off-by: Tadeusz Struk <tadeusz.struk@linaro.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 96340cd commit 9248694

4 files changed

Lines changed: 13 additions & 0 deletions

File tree

include/net/esp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <linux/skbuff.h>
66

7+
#define ESP_SKB_FRAG_MAXSIZE (PAGE_SIZE << SKB_FRAG_PAGE_ORDER)
8+
79
struct ip_esp_hdr;
810

911
static inline struct ip_esp_hdr *ip_esp_hdr(const struct sk_buff *skb)

include/net/sock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,6 +2670,7 @@ extern int sysctl_optmem_max;
26702670
extern __u32 sysctl_wmem_default;
26712671
extern __u32 sysctl_rmem_default;
26722672

2673+
#define SKB_FRAG_PAGE_ORDER get_order(32768)
26732674
DECLARE_STATIC_KEY_FALSE(net_high_order_alloc_disable_key);
26742675

26752676
static inline int sk_get_wmem0(const struct sock *sk, const struct proto *proto)

net/ipv4/esp4.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *
448448
struct page *page;
449449
struct sk_buff *trailer;
450450
int tailen = esp->tailen;
451+
unsigned int allocsz;
451452

452453
/* this is non-NULL only with TCP/UDP Encapsulation */
453454
if (x->encap) {
@@ -457,6 +458,10 @@ int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *
457458
return err;
458459
}
459460

461+
allocsz = ALIGN(skb->data_len + tailen, L1_CACHE_BYTES);
462+
if (allocsz > ESP_SKB_FRAG_MAXSIZE)
463+
goto cow;
464+
460465
if (!skb_cloned(skb)) {
461466
if (tailen <= skb_tailroom(skb)) {
462467
nfrags = 1;

net/ipv6/esp6.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ int esp6_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info
483483
struct page *page;
484484
struct sk_buff *trailer;
485485
int tailen = esp->tailen;
486+
unsigned int allocsz;
486487

487488
if (x->encap) {
488489
int err = esp6_output_encap(x, skb, esp);
@@ -491,6 +492,10 @@ int esp6_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info
491492
return err;
492493
}
493494

495+
allocsz = ALIGN(skb->data_len + tailen, L1_CACHE_BYTES);
496+
if (allocsz > ESP_SKB_FRAG_MAXSIZE)
497+
goto cow;
498+
494499
if (!skb_cloned(skb)) {
495500
if (tailen <= skb_tailroom(skb)) {
496501
nfrags = 1;

0 commit comments

Comments
 (0)