Skip to content

Commit 206c8e2

Browse files
jgross1gregkh
authored andcommitted
xen/netfront: react properly to failing gnttab_end_foreign_access_ref()
Commit 66e3531 upstream. 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 <demi@invisiblethingslab.com> Signed-off-by: Juergen Gross <jgross@suse.com> Reviewed-by: Jan Beulich <jbeulich@suse.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 39c00d0 commit 206c8e2

1 file changed

Lines changed: 31 additions & 17 deletions

File tree

drivers/net/xen-netfront.c

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,6 @@ static int xennet_get_responses(struct netfront_queue *queue,
990990
struct device *dev = &queue->info->netdev->dev;
991991
struct bpf_prog *xdp_prog;
992992
struct xdp_buff xdp;
993-
unsigned long ret;
994993
int slots = 1;
995994
int err = 0;
996995
u32 verdict;
@@ -1032,8 +1031,13 @@ static int xennet_get_responses(struct netfront_queue *queue,
10321031
goto next;
10331032
}
10341033

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

10381042
gnttab_release_grant_reference(&queue->gref_rx_head, ref);
10391043

@@ -1254,6 +1258,10 @@ static int xennet_poll(struct napi_struct *napi, int budget)
12541258
&need_xdp_flush);
12551259

12561260
if (unlikely(err)) {
1261+
if (queue->info->broken) {
1262+
spin_unlock(&queue->rx_lock);
1263+
return 0;
1264+
}
12571265
err:
12581266
while ((skb = __skb_dequeue(&tmpq)))
12591267
__skb_queue_tail(&errq, skb);
@@ -1918,7 +1926,7 @@ static int setup_netfront(struct xenbus_device *dev,
19181926
struct netfront_queue *queue, unsigned int feature_split_evtchn)
19191927
{
19201928
struct xen_netif_tx_sring *txs;
1921-
struct xen_netif_rx_sring *rxs;
1929+
struct xen_netif_rx_sring *rxs = NULL;
19221930
grant_ref_t gref;
19231931
int err;
19241932

@@ -1938,21 +1946,21 @@ static int setup_netfront(struct xenbus_device *dev,
19381946

19391947
err = xenbus_grant_ring(dev, txs, 1, &gref);
19401948
if (err < 0)
1941-
goto grant_tx_ring_fail;
1949+
goto fail;
19421950
queue->tx_ring_ref = gref;
19431951

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

19531961
err = xenbus_grant_ring(dev, rxs, 1, &gref);
19541962
if (err < 0)
1955-
goto grant_rx_ring_fail;
1963+
goto fail;
19561964
queue->rx_ring_ref = gref;
19571965

19581966
if (feature_split_evtchn)
@@ -1965,22 +1973,28 @@ static int setup_netfront(struct xenbus_device *dev,
19651973
err = setup_netfront_single(queue);
19661974

19671975
if (err)
1968-
goto alloc_evtchn_fail;
1976+
goto fail;
19691977

19701978
return 0;
19711979

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

0 commit comments

Comments
 (0)