Commit a884daa6 authored by Juergen Gross's avatar Juergen Gross Committed by David S. Miller

xen/netfront: don't trust the backend response data blindly

Today netfront will trust the backend to send only sane response data.
In order to avoid privilege escalations or crashes in case of malicious
backends verify the data to be within expected limits. Especially make
sure that the response always references an outstanding request.

Note that only the tx queue needs special id handling, as for the rx
queue the id is equal to the index in the ring page.

Introduce a new indicator for the device whether it is broken and let
the device stop working when it is set. Set this indicator in case the
backend sets any weird data.
Signed-off-by: default avatarJuergen Gross <jgross@suse.com>
Reviewed-by: default avatarJan Beulich <jbeulich@suse.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 21631d2d
...@@ -131,10 +131,12 @@ struct netfront_queue { ...@@ -131,10 +131,12 @@ struct netfront_queue {
struct sk_buff *tx_skbs[NET_TX_RING_SIZE]; struct sk_buff *tx_skbs[NET_TX_RING_SIZE];
unsigned short tx_link[NET_TX_RING_SIZE]; unsigned short tx_link[NET_TX_RING_SIZE];
#define TX_LINK_NONE 0xffff #define TX_LINK_NONE 0xffff
#define TX_PENDING 0xfffe
grant_ref_t gref_tx_head; grant_ref_t gref_tx_head;
grant_ref_t grant_tx_ref[NET_TX_RING_SIZE]; grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
struct page *grant_tx_page[NET_TX_RING_SIZE]; struct page *grant_tx_page[NET_TX_RING_SIZE];
unsigned tx_skb_freelist; unsigned tx_skb_freelist;
unsigned int tx_pend_queue;
spinlock_t rx_lock ____cacheline_aligned_in_smp; spinlock_t rx_lock ____cacheline_aligned_in_smp;
struct xen_netif_rx_front_ring rx; struct xen_netif_rx_front_ring rx;
...@@ -167,6 +169,9 @@ struct netfront_info { ...@@ -167,6 +169,9 @@ struct netfront_info {
bool netback_has_xdp_headroom; bool netback_has_xdp_headroom;
bool netfront_xdp_enabled; bool netfront_xdp_enabled;
/* Is device behaving sane? */
bool broken;
atomic_t rx_gso_checksum_fixup; atomic_t rx_gso_checksum_fixup;
}; };
...@@ -349,7 +354,7 @@ static int xennet_open(struct net_device *dev) ...@@ -349,7 +354,7 @@ static int xennet_open(struct net_device *dev)
unsigned int i = 0; unsigned int i = 0;
struct netfront_queue *queue = NULL; struct netfront_queue *queue = NULL;
if (!np->queues) if (!np->queues || np->broken)
return -ENODEV; return -ENODEV;
for (i = 0; i < num_queues; ++i) { for (i = 0; i < num_queues; ++i) {
...@@ -377,11 +382,17 @@ static void xennet_tx_buf_gc(struct netfront_queue *queue) ...@@ -377,11 +382,17 @@ static void xennet_tx_buf_gc(struct netfront_queue *queue)
unsigned short id; unsigned short id;
struct sk_buff *skb; struct sk_buff *skb;
bool more_to_do; bool more_to_do;
const struct device *dev = &queue->info->netdev->dev;
BUG_ON(!netif_carrier_ok(queue->info->netdev)); BUG_ON(!netif_carrier_ok(queue->info->netdev));
do { do {
prod = queue->tx.sring->rsp_prod; prod = queue->tx.sring->rsp_prod;
if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) {
dev_alert(dev, "Illegal number of responses %u\n",
prod - queue->tx.rsp_cons);
goto err;
}
rmb(); /* Ensure we see responses up to 'rp'. */ rmb(); /* Ensure we see responses up to 'rp'. */
for (cons = queue->tx.rsp_cons; cons != prod; cons++) { for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
...@@ -391,14 +402,27 @@ static void xennet_tx_buf_gc(struct netfront_queue *queue) ...@@ -391,14 +402,27 @@ static void xennet_tx_buf_gc(struct netfront_queue *queue)
if (txrsp.status == XEN_NETIF_RSP_NULL) if (txrsp.status == XEN_NETIF_RSP_NULL)
continue; continue;
id = txrsp.id; id = txrsp.id;
if (id >= RING_SIZE(&queue->tx)) {
dev_alert(dev,
"Response has incorrect id (%u)\n",
id);
goto err;
}
if (queue->tx_link[id] != TX_PENDING) {
dev_alert(dev,
"Response for inactive request\n");
goto err;
}
queue->tx_link[id] = TX_LINK_NONE;
skb = queue->tx_skbs[id]; skb = queue->tx_skbs[id];
queue->tx_skbs[id] = NULL; queue->tx_skbs[id] = NULL;
if (unlikely(gnttab_query_foreign_access( if (unlikely(gnttab_query_foreign_access(
queue->grant_tx_ref[id]) != 0)) { queue->grant_tx_ref[id]) != 0)) {
pr_alert("%s: warning -- grant still in use by backend domain\n", dev_alert(dev,
__func__); "Grant still in use by backend domain\n");
BUG(); goto err;
} }
gnttab_end_foreign_access_ref( gnttab_end_foreign_access_ref(
queue->grant_tx_ref[id], GNTMAP_readonly); queue->grant_tx_ref[id], GNTMAP_readonly);
...@@ -416,6 +440,12 @@ static void xennet_tx_buf_gc(struct netfront_queue *queue) ...@@ -416,6 +440,12 @@ static void xennet_tx_buf_gc(struct netfront_queue *queue)
} while (more_to_do); } while (more_to_do);
xennet_maybe_wake_tx(queue); xennet_maybe_wake_tx(queue);
return;
err:
queue->info->broken = true;
dev_alert(dev, "Disabled for further use\n");
} }
struct xennet_gnttab_make_txreq { struct xennet_gnttab_make_txreq {
...@@ -459,6 +489,12 @@ static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset, ...@@ -459,6 +489,12 @@ static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
*tx = info->tx_local; *tx = info->tx_local;
/*
* Put the request in the pending queue, it will be set to be pending
* when the producer index is about to be raised.
*/
add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
info->tx = tx; info->tx = tx;
info->size += info->tx_local.size; info->size += info->tx_local.size;
} }
...@@ -551,6 +587,15 @@ static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb, ...@@ -551,6 +587,15 @@ static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
return queue_idx; return queue_idx;
} }
static void xennet_mark_tx_pending(struct netfront_queue *queue)
{
unsigned int i;
while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
TX_LINK_NONE)
queue->tx_link[i] = TX_PENDING;
}
static int xennet_xdp_xmit_one(struct net_device *dev, static int xennet_xdp_xmit_one(struct net_device *dev,
struct netfront_queue *queue, struct netfront_queue *queue,
struct xdp_frame *xdpf) struct xdp_frame *xdpf)
...@@ -568,6 +613,8 @@ static int xennet_xdp_xmit_one(struct net_device *dev, ...@@ -568,6 +613,8 @@ static int xennet_xdp_xmit_one(struct net_device *dev,
offset_in_page(xdpf->data), offset_in_page(xdpf->data),
xdpf->len); xdpf->len);
xennet_mark_tx_pending(queue);
RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify); RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
if (notify) if (notify)
notify_remote_via_irq(queue->tx_irq); notify_remote_via_irq(queue->tx_irq);
...@@ -592,6 +639,8 @@ static int xennet_xdp_xmit(struct net_device *dev, int n, ...@@ -592,6 +639,8 @@ static int xennet_xdp_xmit(struct net_device *dev, int n,
int nxmit = 0; int nxmit = 0;
int i; int i;
if (unlikely(np->broken))
return -ENODEV;
if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
return -EINVAL; return -EINVAL;
...@@ -636,6 +685,8 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev ...@@ -636,6 +685,8 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev
/* Drop the packet if no queues are set up */ /* Drop the packet if no queues are set up */
if (num_queues < 1) if (num_queues < 1)
goto drop; goto drop;
if (unlikely(np->broken))
goto drop;
/* Determine which queue to transmit this SKB on */ /* Determine which queue to transmit this SKB on */
queue_index = skb_get_queue_mapping(skb); queue_index = skb_get_queue_mapping(skb);
queue = &np->queues[queue_index]; queue = &np->queues[queue_index];
...@@ -742,6 +793,8 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev ...@@ -742,6 +793,8 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev
/* timestamp packet in software */ /* timestamp packet in software */
skb_tx_timestamp(skb); skb_tx_timestamp(skb);
xennet_mark_tx_pending(queue);
RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify); RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
if (notify) if (notify)
notify_remote_via_irq(queue->tx_irq); notify_remote_via_irq(queue->tx_irq);
...@@ -1141,6 +1194,13 @@ static int xennet_poll(struct napi_struct *napi, int budget) ...@@ -1141,6 +1194,13 @@ static int xennet_poll(struct napi_struct *napi, int budget)
skb_queue_head_init(&tmpq); skb_queue_head_init(&tmpq);
rp = queue->rx.sring->rsp_prod; rp = queue->rx.sring->rsp_prod;
if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) {
dev_alert(&dev->dev, "Illegal number of responses %u\n",
rp - queue->rx.rsp_cons);
queue->info->broken = true;
spin_unlock(&queue->rx_lock);
return 0;
}
rmb(); /* Ensure we see queued responses up to 'rp'. */ rmb(); /* Ensure we see queued responses up to 'rp'. */
i = queue->rx.rsp_cons; i = queue->rx.rsp_cons;
...@@ -1362,6 +1422,9 @@ static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id) ...@@ -1362,6 +1422,9 @@ static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
struct netfront_queue *queue = dev_id; struct netfront_queue *queue = dev_id;
unsigned long flags; unsigned long flags;
if (queue->info->broken)
return IRQ_HANDLED;
spin_lock_irqsave(&queue->tx_lock, flags); spin_lock_irqsave(&queue->tx_lock, flags);
xennet_tx_buf_gc(queue); xennet_tx_buf_gc(queue);
spin_unlock_irqrestore(&queue->tx_lock, flags); spin_unlock_irqrestore(&queue->tx_lock, flags);
...@@ -1374,6 +1437,9 @@ static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id) ...@@ -1374,6 +1437,9 @@ static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
struct netfront_queue *queue = dev_id; struct netfront_queue *queue = dev_id;
struct net_device *dev = queue->info->netdev; struct net_device *dev = queue->info->netdev;
if (queue->info->broken)
return IRQ_HANDLED;
if (likely(netif_carrier_ok(dev) && if (likely(netif_carrier_ok(dev) &&
RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))) RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
napi_schedule(&queue->napi); napi_schedule(&queue->napi);
...@@ -1395,6 +1461,10 @@ static void xennet_poll_controller(struct net_device *dev) ...@@ -1395,6 +1461,10 @@ static void xennet_poll_controller(struct net_device *dev)
struct netfront_info *info = netdev_priv(dev); struct netfront_info *info = netdev_priv(dev);
unsigned int num_queues = dev->real_num_tx_queues; unsigned int num_queues = dev->real_num_tx_queues;
unsigned int i; unsigned int i;
if (info->broken)
return;
for (i = 0; i < num_queues; ++i) for (i = 0; i < num_queues; ++i)
xennet_interrupt(0, &info->queues[i]); xennet_interrupt(0, &info->queues[i]);
} }
...@@ -1466,6 +1536,11 @@ static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog, ...@@ -1466,6 +1536,11 @@ static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp) static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
{ {
struct netfront_info *np = netdev_priv(dev);
if (np->broken)
return -ENODEV;
switch (xdp->command) { switch (xdp->command) {
case XDP_SETUP_PROG: case XDP_SETUP_PROG:
return xennet_xdp_set(dev, xdp->prog, xdp->extack); return xennet_xdp_set(dev, xdp->prog, xdp->extack);
...@@ -1841,6 +1916,7 @@ static int xennet_init_queue(struct netfront_queue *queue) ...@@ -1841,6 +1916,7 @@ static int xennet_init_queue(struct netfront_queue *queue)
/* Initialise tx_skb_freelist as a free chain containing every entry. */ /* Initialise tx_skb_freelist as a free chain containing every entry. */
queue->tx_skb_freelist = 0; queue->tx_skb_freelist = 0;
queue->tx_pend_queue = TX_LINK_NONE;
for (i = 0; i < NET_TX_RING_SIZE; i++) { for (i = 0; i < NET_TX_RING_SIZE; i++) {
queue->tx_link[i] = i + 1; queue->tx_link[i] = i + 1;
queue->grant_tx_ref[i] = GRANT_INVALID_REF; queue->grant_tx_ref[i] = GRANT_INVALID_REF;
...@@ -2115,6 +2191,9 @@ static int talk_to_netback(struct xenbus_device *dev, ...@@ -2115,6 +2191,9 @@ static int talk_to_netback(struct xenbus_device *dev,
if (info->queues) if (info->queues)
xennet_destroy_queues(info); xennet_destroy_queues(info);
/* For the case of a reconnect reset the "broken" indicator. */
info->broken = false;
err = xennet_create_queues(info, &num_queues); err = xennet_create_queues(info, &num_queues);
if (err < 0) { if (err < 0) {
xenbus_dev_fatal(dev, err, "creating queues"); xenbus_dev_fatal(dev, err, "creating queues");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment