Skip to content

Commit 66e3531

Browse files
committed
xen/netfront: react properly to failing gnttab_end_foreign_access_ref()
When calling gnttab_end_foreign_access_ref() the returned value must be tested and the reaction to that value should be appropriate. In case of failure in xennet_get_responses() the reaction should not be to crash the system, but to disable the network device. The calls in setup_netfront() can be replaced by calls of gnttab_end_foreign_access(). While at it avoid double free of ring pages and grant references via xennet_disconnect_backend() in this case. This is CVE-2022-23042 / part of XSA-396. Reported-by: Demi Marie Obenour <[email protected]> Signed-off-by: Juergen Gross <[email protected]> Reviewed-by: Jan Beulich <[email protected]> --- V2: - avoid double free V3: - remove pointless initializer (Jan Beulich)
1 parent 42baefa commit 66e3531

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

drivers/net/xen-netfront.c

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,6 @@ static int xennet_get_responses(struct netfront_queue *queue,
988988
struct device *dev = &queue->info->netdev->dev;
989989
struct bpf_prog *xdp_prog;
990990
struct xdp_buff xdp;
991-
unsigned long ret;
992991
int slots = 1;
993992
int err = 0;
994993
u32 verdict;
@@ -1030,8 +1029,13 @@ static int xennet_get_responses(struct netfront_queue *queue,
10301029
goto next;
10311030
}
10321031

1033-
ret = gnttab_end_foreign_access_ref(ref, 0);
1034-
BUG_ON(!ret);
1032+
if (!gnttab_end_foreign_access_ref(ref, 0)) {
1033+
dev_alert(dev,
1034+
"Grant still in use by backend domain\n");
1035+
queue->info->broken = true;
1036+
dev_alert(dev, "Disabled for further use\n");
1037+
return -EINVAL;
1038+
}
10351039

10361040
gnttab_release_grant_reference(&queue->gref_rx_head, ref);
10371041

@@ -1252,6 +1256,10 @@ static int xennet_poll(struct napi_struct *napi, int budget)
12521256
&need_xdp_flush);
12531257

12541258
if (unlikely(err)) {
1259+
if (queue->info->broken) {
1260+
spin_unlock(&queue->rx_lock);
1261+
return 0;
1262+
}
12551263
err:
12561264
while ((skb = __skb_dequeue(&tmpq)))
12571265
__skb_queue_tail(&errq, skb);
@@ -1916,7 +1924,7 @@ static int setup_netfront(struct xenbus_device *dev,
19161924
struct netfront_queue *queue, unsigned int feature_split_evtchn)
19171925
{
19181926
struct xen_netif_tx_sring *txs;
1919-
struct xen_netif_rx_sring *rxs;
1927+
struct xen_netif_rx_sring *rxs = NULL;
19201928
grant_ref_t gref;
19211929
int err;
19221930

@@ -1936,21 +1944,21 @@ static int setup_netfront(struct xenbus_device *dev,
19361944

19371945
err = xenbus_grant_ring(dev, txs, 1, &gref);
19381946
if (err < 0)
1939-
goto grant_tx_ring_fail;
1947+
goto fail;
19401948
queue->tx_ring_ref = gref;
19411949

19421950
rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
19431951
if (!rxs) {
19441952
err = -ENOMEM;
19451953
xenbus_dev_fatal(dev, err, "allocating rx ring page");
1946-
goto alloc_rx_ring_fail;
1954+
goto fail;
19471955
}
19481956
SHARED_RING_INIT(rxs);
19491957
FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
19501958

19511959
err = xenbus_grant_ring(dev, rxs, 1, &gref);
19521960
if (err < 0)
1953-
goto grant_rx_ring_fail;
1961+
goto fail;
19541962
queue->rx_ring_ref = gref;
19551963

19561964
if (feature_split_evtchn)
@@ -1963,22 +1971,28 @@ static int setup_netfront(struct xenbus_device *dev,
19631971
err = setup_netfront_single(queue);
19641972

19651973
if (err)
1966-
goto alloc_evtchn_fail;
1974+
goto fail;
19671975

19681976
return 0;
19691977

19701978
/* If we fail to setup netfront, it is safe to just revoke access to
19711979
* granted pages because backend is not accessing it at this point.
19721980
*/
1973-
alloc_evtchn_fail:
1974-
gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
1975-
grant_rx_ring_fail:
1976-
free_page((unsigned long)rxs);
1977-
alloc_rx_ring_fail:
1978-
gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
1979-
grant_tx_ring_fail:
1980-
free_page((unsigned long)txs);
1981-
fail:
1981+
fail:
1982+
if (queue->rx_ring_ref != GRANT_INVALID_REF) {
1983+
gnttab_end_foreign_access(queue->rx_ring_ref, 0,
1984+
(unsigned long)rxs);
1985+
queue->rx_ring_ref = GRANT_INVALID_REF;
1986+
} else {
1987+
free_page((unsigned long)rxs);
1988+
}
1989+
if (queue->tx_ring_ref != GRANT_INVALID_REF) {
1990+
gnttab_end_foreign_access(queue->tx_ring_ref, 0,
1991+
(unsigned long)txs);
1992+
queue->tx_ring_ref = GRANT_INVALID_REF;
1993+
} else {
1994+
free_page((unsigned long)txs);
1995+
}
19821996
return err;
19831997
}
19841998

0 commit comments

Comments
 (0)