Skip to content

Commit 315dbdd

Browse files
minhbq-99Paolo Abeni
authored andcommitted
virtio-net: ensure the received length does not exceed allocated size
In xdp_linearize_page, when reading the following buffers from the ring, we forget to check the received length with the true allocate size. This can lead to an out-of-bound read. This commit adds that missing check. Cc: <[email protected]> Fixes: 4941d47 ("virtio-net: do not reset during XDP set") Signed-off-by: Bui Quang Minh <[email protected]> Acked-by: Jason Wang <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent bd475ee commit 315dbdd

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

drivers/net/virtio_net.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,26 @@ static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
778778
return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
779779
}
780780

781+
static int check_mergeable_len(struct net_device *dev, void *mrg_ctx,
782+
unsigned int len)
783+
{
784+
unsigned int headroom, tailroom, room, truesize;
785+
786+
truesize = mergeable_ctx_to_truesize(mrg_ctx);
787+
headroom = mergeable_ctx_to_headroom(mrg_ctx);
788+
tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
789+
room = SKB_DATA_ALIGN(headroom + tailroom);
790+
791+
if (len > truesize - room) {
792+
pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
793+
dev->name, len, (unsigned long)(truesize - room));
794+
DEV_STATS_INC(dev, rx_length_errors);
795+
return -1;
796+
}
797+
798+
return 0;
799+
}
800+
781801
static struct sk_buff *virtnet_build_skb(void *buf, unsigned int buflen,
782802
unsigned int headroom,
783803
unsigned int len)
@@ -1797,7 +1817,8 @@ static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
17971817
* across multiple buffers (num_buf > 1), and we make sure buffers
17981818
* have enough headroom.
17991819
*/
1800-
static struct page *xdp_linearize_page(struct receive_queue *rq,
1820+
static struct page *xdp_linearize_page(struct net_device *dev,
1821+
struct receive_queue *rq,
18011822
int *num_buf,
18021823
struct page *p,
18031824
int offset,
@@ -1817,18 +1838,27 @@ static struct page *xdp_linearize_page(struct receive_queue *rq,
18171838
memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
18181839
page_off += *len;
18191840

1841+
/* Only mergeable mode can go inside this while loop. In small mode,
1842+
* *num_buf == 1, so it cannot go inside.
1843+
*/
18201844
while (--*num_buf) {
18211845
unsigned int buflen;
18221846
void *buf;
1847+
void *ctx;
18231848
int off;
18241849

1825-
buf = virtnet_rq_get_buf(rq, &buflen, NULL);
1850+
buf = virtnet_rq_get_buf(rq, &buflen, &ctx);
18261851
if (unlikely(!buf))
18271852
goto err_buf;
18281853

18291854
p = virt_to_head_page(buf);
18301855
off = buf - page_address(p);
18311856

1857+
if (check_mergeable_len(dev, ctx, buflen)) {
1858+
put_page(p);
1859+
goto err_buf;
1860+
}
1861+
18321862
/* guard against a misconfigured or uncooperative backend that
18331863
* is sending packet larger than the MTU.
18341864
*/
@@ -1917,7 +1947,7 @@ static struct sk_buff *receive_small_xdp(struct net_device *dev,
19171947
headroom = vi->hdr_len + header_offset;
19181948
buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
19191949
SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1920-
xdp_page = xdp_linearize_page(rq, &num_buf, page,
1950+
xdp_page = xdp_linearize_page(dev, rq, &num_buf, page,
19211951
offset, header_offset,
19221952
&tlen);
19231953
if (!xdp_page)
@@ -2252,7 +2282,7 @@ static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
22522282
*/
22532283
if (!xdp_prog->aux->xdp_has_frags) {
22542284
/* linearize data for XDP */
2255-
xdp_page = xdp_linearize_page(rq, num_buf,
2285+
xdp_page = xdp_linearize_page(vi->dev, rq, num_buf,
22562286
*page, offset,
22572287
XDP_PACKET_HEADROOM,
22582288
len);

0 commit comments

Comments
 (0)