Skip to content

Commit

Permalink
esp: Fix possible buffer overflow in ESP transformation
Browse files Browse the repository at this point in the history
ANBZ: torvalds#709

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>
Fixes: CVE-2022-0886
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Acked-by: Dust Li <dust.li@linux.alibaba.com>
  • Loading branch information
klassert authored and shiloong committed Mar 25, 2022
1 parent f72100e commit e397978
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/net/esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <linux/skbuff.h>

#define ESP_SKB_FRAG_MAXSIZE (PAGE_SIZE << SKB_FRAG_PAGE_ORDER)

struct ip_esp_hdr;

static inline struct ip_esp_hdr *ip_esp_hdr(const struct sk_buff *skb)
Expand Down
7 changes: 7 additions & 0 deletions net/ipv4/esp4.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ static int esp_output_udp_encap(struct xfrm_state *x, struct sk_buff *skb, struc
return 0;
}

#define SKB_FRAG_PAGE_ORDER get_order(32768)

int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
{
u8 *tail;
Expand All @@ -276,6 +278,7 @@ int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *
struct page *page;
struct sk_buff *trailer;
int tailen = esp->tailen;
unsigned int allocsz;

/* this is non-NULL only with UDP Encapsulation */
if (x->encap) {
Expand All @@ -285,6 +288,10 @@ int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *
return err;
}

allocsz = ALIGN(skb->data_len + tailen, L1_CACHE_BYTES);
if (allocsz > ESP_SKB_FRAG_MAXSIZE)
goto cow;

if (!skb_cloned(skb)) {
if (tailen <= skb_tailroom(skb)) {
nfrags = 1;
Expand Down
7 changes: 7 additions & 0 deletions net/ipv6/esp6.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ static void esp_output_fill_trailer(u8 *tail, int tfclen, int plen, __u8 proto)
tail[plen - 1] = proto;
}

#define SKB_FRAG_PAGE_ORDER get_order(32768)

int esp6_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
{
u8 *tail;
Expand All @@ -242,6 +244,11 @@ int esp6_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info
struct page *page;
struct sk_buff *trailer;
int tailen = esp->tailen;
unsigned int allocsz;

allocsz = ALIGN(skb->data_len + tailen, L1_CACHE_BYTES);
if (allocsz > ESP_SKB_FRAG_MAXSIZE)
goto cow;

if (!skb_cloned(skb)) {
if (tailen <= skb_tailroom(skb)) {
Expand Down

0 comments on commit e397978

Please sign in to comment.