virtio_ring: validate used buffer length
PATCH 1/1 virtio_ring: validate used buffer length
This patch validate the used buffer length provided by the device before trying to use it. This is done by remembering the in buffer length in a dedicated array during virtqueue_add(), then we can fail the virtqueue_get_buf() when we find the device is trying to give us a used buffer length which is greater than we stored before.
This validation is disable by default via module parameter to unbreak some existing devices since some legacy devices are known to report buggy used length.
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize
--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)
drivers/virtio/virtio_ring.c +75 -0
@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \
@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;
@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;
@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);
@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */
@@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );
@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;
@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,
@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(
@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);
@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }
@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);
@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);
@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }
@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising
@@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;
@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }
@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ; err:
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Michael S. Tsirkin
maintainerQuoted 1 line · Jason Wang
This patch validate
validates
Quoted 2 lines
the used buffer length provided by the device
before trying to use it.
before returning it to caller
Quoted 4 lines
This is done by remembering the in buffer
length in a dedicated array duringvirtqueue_add(), then we can fail thevirtqueue_get_buf()when we find the device is trying to give us a used buffer length which is greater than we stored before.
than what we stored
Quoted 2 lines
This validation is disable
disabled
Quoted 5 lines
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Signed-off-by Jason Wang <[email protected]>
First I'm not merging this without more data about
what is known to be broken and what is known to work well in the commit log. And how exactly do things work if used length is wrong?
Second what's wrong with dma_desc_extra that we already maintain? Third motivation - it's part and parcel of the hardening effort yes? I'd like to know the fate of VIRTIO_HARDEN_NOTIFICATION before we do more hardening. If it's irrevocably broken let's rip it out?
Quoted 222 lines
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)drivers/virtio/virtio_ring.c +75
-0@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */ @@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ; err:Signature · 1 line
2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 27 lines · Michael S. Tsirkin
Quoted 1 line · Jason Wang
This patch validate
validates
Quoted 2 lines
the used buffer length provided by the device
before trying to use it.before returning it to caller
Quoted 4 lines
This is done by remembering the in buffer
length in a dedicated array duringvirtqueue_add(), then we can fail thevirtqueue_get_buf()when we find the device is trying to give us a used buffer length which is greater than we stored before.than what we stored
Quoted 2 lines
This validation is disable
disabled
Quoted 4 lines
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.
Quoted 6 lines
Quoted 1 line
Signed-off-by Jason Wang <[email protected]>First I'm not merging this without more data about what is known to be broken and what is known to work well in the commit log. And how exactly do things work if used length is wrong?
Assuming the device is malicious, it would be very hard to answer. Auditing and fuzzing won't cover every case. Instead of trying to seek the answer, we can simply make sure the used in buffer length is validated then we know we're fine or not.
Quoted 2 lines
Second what's wrong with
dma_desc_extrathat we already maintain? Third motivation - it's part and parcel of the hardening effort yes?
They are different. dma_desc_extra is for a descriptor ring, but this is for a used ring. Technically we can go back to iterate on the descriptor ring for a legal used in buffer length. But it will have worse performance.
Quoted 2 lines
I'd like to know the fate of
VIRTIO_HARDEN_NOTIFICATIONbefore we do more hardening. If it's irrevocably broken let's rip it out?
So the plan is
1) finish used ring validation (this had been proposed, merged and reverted before notification hardening)
2) do notification hardening on top.
So let's leave it as is and I will do a rework after we finalize the used ring validation.
Thanks
Quoted 221 lines
Quoted 221 lines
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)drivers/virtio/virtio_ring.c +75
-0@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */ @@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ;
Quoted 4 lines
Quoted 3 lines
drivers/virtio/virtio_ring.c +0
-1err: --2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Michael S. Tsirkin
maintainerQuoted 35 lines · Jason Wang
Quoted 27 lines · Michael S. Tsirkin
Quoted 1 line · Jason Wang
This patch validate
validates
Quoted 2 lines
the used buffer length provided by the device
before trying to use it.before returning it to caller
Quoted 4 lines
This is done by remembering the in buffer
length in a dedicated array duringvirtqueue_add(), then we can fail thevirtqueue_get_buf()when we find the device is trying to give us a used buffer length which is greater than we stored before.than what we stored
Quoted 2 lines
This validation is disable
disabled
Quoted 4 lines
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Quoted 6 lines
Quoted 1 line
Signed-off-by Jason Wang <[email protected]>First I'm not merging this without more data about what is known to be broken and what is known to work well in the commit log. And how exactly do things work if used length is wrong?
Quoted 4 lines
Assuming the device is malicious, it would be very hard to answer. Auditing and fuzzing won't cover every case. Instead of trying to seek the answer, we can simply make sure the used in buffer length is validated then we know we're fine or not.
To restate the question, you said above "some legacy devices are known to report buggy used length". If they report buggy length then how can things work?
Quoted 7 lines
Quoted 2 lines
Second what's wrong with
dma_desc_extrathat we already maintain? Third motivation - it's part and parcel of the hardening effort yes?They are different.
dma_desc_extrais for a descriptor ring, but this is for a used ring. Technically we can go back to iterate on the descriptor ring for a legal used in buffer length. But it will have worse performance.
I don't really understand. We already iterate when we unmap - all that is necessary is to subtract it from used length, if at the end of the process it is >0 then we know used length is too large.
Quoted 5 lines
Quoted 2 lines
I'd like to know the fate of
VIRTIO_HARDEN_NOTIFICATIONbefore we do more hardening. If it's irrevocably broken let's rip it out?So the plan is
Quoted 234 lines
1) finish used ring validation (this had been proposed, merged and reverted before notification hardening)
2) do notification hardening on top.So let's leave it as is and I will do a rework after we finalize the used ring validation.
Thanks
Quoted 225 lines
Quoted 224 lines
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)drivers/virtio/virtio_ring.c +75
-1@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */ @@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ; err: --2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 37 lines · Michael S. Tsirkin
Quoted 35 lines · Jason Wang
Quoted 33 lines · Michael S. Tsirkin
Quoted 1 line · Jason Wang
This patch validate
validates
Quoted 2 lines
the used buffer length provided by the device
before trying to use it.before returning it to caller
Quoted 4 lines
This is done by remembering the in buffer
length in a dedicated array duringvirtqueue_add(), then we can fail thevirtqueue_get_buf()when we find the device is trying to give us a used buffer length which is greater than we stored before.than what we stored
Quoted 2 lines
This validation is disable
disabled
Quoted 5 lines
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Signed-off-by Jason Wang <[email protected]>First I'm not merging this without more data about what is known to be broken and what is known to work well in the commit log. And how exactly do things work if used length is wrong?
Quoted 8 lines
Quoted 4 lines
Assuming the device is malicious, it would be very hard to answer. Auditing and fuzzing won't cover every case. Instead of trying to seek the answer, we can simply make sure the used in buffer length is validated then we know we're fine or not.
To restate the question, you said above "some legacy devices are known to report buggy used length". If they report buggy length then how can things work?
The validation is disabled for legacy device (as stated in the changelog):
static bool vring_needs_used_validation(const struct virtio_device *vdev)
{
/*
* Several legacy devices are known to produce buggy used
* length. In order to let driver work, we won't validate used
* buffer length in this case.
*/
if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
return false;
if (force_used_validation)
return true;
return false;
}This seems to be what we've agreed in last version:
Thanks
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines
Second what's wrong with
dma_desc_extrathat we already maintain? Third motivation - it's part and parcel of the hardening effort yes?They are different.
dma_desc_extrais for a descriptor ring, but this is for a used ring. Technically we can go back to iterate on the descriptor ring for a legal used in buffer length. But it will have worse performance.
Quoted 4 lines
I don't really understand. We already iterate when we unmap - all that is necessary is to subtract it from used length, if at the end of the process it is >0 then we know used length is too large.
Yes, but it is the job that is done in the driver level not the virtio core. Validation in virtio core is still necessary since they're working at different levels and it's hard to force the validation in all drivers by codes. Last version introduces a suppress_driver_validation to allow the driver to suppress the core validation which seems not good, we need a way to force the virtio_ring code to do validation before. Or such stuff could be added on top since the validation is by default anyway.
Thanks
Quoted 237 lines
Quoted 237 lines
Quoted 4 lines
I'd like to know the fate of
VIRTIO_HARDEN_NOTIFICATIONbefore we do more hardening. If it's irrevocably broken let's rip it out?So the plan is
1) finish used ring validation (this had been proposed, merged and reverted before notification hardening)
2) do notification hardening on top.So let's leave it as is and I will do a rework after we finalize the used ring validation.
Thanks
Quoted 221 lines
Quoted 221 lines
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)drivers/virtio/virtio_ring.c +75
-0@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */ @@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ;
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
drivers/virtio/virtio_ring.c +0
-1err: --2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Michael S. Tsirkin
maintainerQuoted 63 lines · Jason Wang
Quoted 37 lines · Michael S. Tsirkin
Quoted 35 lines · Jason Wang
Quoted 33 lines · Michael S. Tsirkin
Quoted 1 line · Jason Wang
This patch validate
validates
Quoted 2 lines
the used buffer length provided by the device
before trying to use it.before returning it to caller
Quoted 4 lines
This is done by remembering the in buffer
length in a dedicated array duringvirtqueue_add(), then we can fail thevirtqueue_get_buf()when we find the device is trying to give us a used buffer length which is greater than we stored before.than what we stored
Quoted 2 lines
This validation is disable
disabled
Quoted 5 lines
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Signed-off-by Jason Wang <[email protected]>First I'm not merging this without more data about what is known to be broken and what is known to work well in the commit log. And how exactly do things work if used length is wrong?
Quoted 8 lines
Quoted 4 lines
Assuming the device is malicious, it would be very hard to answer. Auditing and fuzzing won't cover every case. Instead of trying to seek the answer, we can simply make sure the used in buffer length is validated then we know we're fine or not.
To restate the question, you said above "some legacy devices are known to report buggy used length". If they report buggy length then how can things work?
The validation is disabled for legacy device (as stated in the changelog):
static bool
vring_needs_used_validation(const struct virtio_device *vdev){ /* * Several legacy devices are known to produce buggy used * length. In order to let driver work, we won't validate used * buffer length in this case. */ if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) return false; if (force_used_validation) return true; return false; }
Quoted 6 lines
This seems to be what we've agreed in last version:
Thanks
I don't get it. You wrote:
This validation is disable
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.
which devices? why do you need a module parameter?
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines
Second what's wrong with
dma_desc_extrathat we already maintain? Third motivation - it's part and parcel of the hardening effort yes?They are different.
dma_desc_extrais for a descriptor ring, but this is for a used ring. Technically we can go back to iterate on the descriptor ring for a legal used in buffer length. But it will have worse performance.
Quoted 7 lines
Quoted 4 lines
I don't really understand. We already iterate when we unmap - all that is necessary is to subtract it from used length, if at the end of the process it is >0 then we know used length is too large.
Yes, but it is the job that is done in the driver level not the virtio core.
What job? unmap is done in detach_buf_split and detach_buf_packed respectively. vring_desc_extra isn't even visible outside drivers/virtio/virtio_ring.c
For drivers that do unmap at driver level - I guess they can do validation there too.
Quoted 6 lines
Validation in virtio core is still necessary since they're working at different levels and it's hard to force the validation in all drivers by codes. Last version introduces a
suppress_driver_validationto allow the driver to suppress the core validation which seems not good, we need a way to force thevirtio_ringcode to do validation before.
Why do we? If driver validates length virtio_ring does not need to validate. If driver does not use length virtio_ring does not need to validate. core can provide this service for the gazillion non performance critical drivers that just want to keep things simple, but the 4-5 critical ones can do their own validation if they want to.
Quoted 249 lines
Or such stuff could be added
on top since the validation is by default anyway.Thanks
Quoted 242 lines
Quoted 241 lines
Quoted 4 lines
I'd like to know the fate of
VIRTIO_HARDEN_NOTIFICATIONbefore we do more hardening. If it's irrevocably broken let's rip it out?So the plan is
1) finish used ring validation (this had been proposed, merged and reverted before notification hardening)
2) do notification hardening on top.So let's leave it as is and I will do a rework after we finalize the used ring validation.
Thanks
Quoted 225 lines
Quoted 224 lines
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)drivers/virtio/virtio_ring.c +75
-1@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */ @@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ; err: --2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 72 lines · Michael S. Tsirkin
Quoted 63 lines · Jason Wang
Quoted 37 lines · Michael S. Tsirkin
Quoted 35 lines · Jason Wang
Quoted 33 lines · Michael S. Tsirkin
Quoted 1 line · Jason Wang
This patch validate
validates
Quoted 2 lines
the used buffer length provided by the device
before trying to use it.before returning it to caller
Quoted 4 lines
This is done by remembering the in buffer
length in a dedicated array duringvirtqueue_add(), then we can fail thevirtqueue_get_buf()when we find the device is trying to give us a used buffer length which is greater than we stored before.than what we stored
Quoted 2 lines
This validation is disable
disabled
Quoted 5 lines
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Signed-off-by Jason Wang <[email protected]>First I'm not merging this without more data about what is known to be broken and what is known to work well in the commit log. And how exactly do things work if used length is wrong?
Quoted 8 lines
Quoted 4 lines
Assuming the device is malicious, it would be very hard to answer. Auditing and fuzzing won't cover every case. Instead of trying to seek the answer, we can simply make sure the used in buffer length is validated then we know we're fine or not.
To restate the question, you said above "some legacy devices are known to report buggy used length". If they report buggy length then how can things work?
The validation is disabled for legacy device (as stated in the changelog):
static bool
vring_needs_used_validation(const struct virtio_device *vdev){ /* * Several legacy devices are known to produce buggy used * length. In order to let driver work, we won't validate used * buffer length in this case. */ if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) return false; if (force_used_validation) return true; return false; }Quoted 6 lines
This seems to be what we've agreed in last version:
Thanks
Quoted 8 lines
I don't get it. You wrote:
This validation is disable
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.which devices?
legacy rpmsg and vsock device (before 49d8c5ffad07) at least.
Quoted 1 line
why do you need a module parameter?
If we enable it unconditionally for modern devices, it may break some buggy moden device (vsock without a fix as an example).
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 10 lines
Quoted 5 lines
Second what's wrong with
dma_desc_extrathat we already maintain? Third motivation - it's part and parcel of the hardening effort yes?They are different.
dma_desc_extrais for a descriptor ring, but this is for a used ring. Technically we can go back to iterate on the descriptor ring for a legal used in buffer length. But it will have worse performance.I don't really understand. We already iterate when we unmap - all that is necessary is to subtract it from used length, if at the end of the process it is >0 then we know used length is too large.
Quoted 4 lines
Quoted 2 lines
Yes, but it is the job that is done in the driver level not the virtio core.
What job?
I meant the driver can do the validation since it has the knowledge of the buffer length if it wants.
Quoted 2 lines
unmap is done in
detach_buf_splitanddetach_buf_packedrespectively.vring_desc_extraisn't even visible outsidedrivers/virtio/virtio_ring.c
desc_extra doesn't contain buffer length for the case of indirect descriptors. So we need to iterate in the descriptors when it looks expensive if we don't need unmap.
Thanks
Quoted 11 lines
For drivers that do unmap at driver level - I guess they can do validation there too.
Quoted 6 lines
Validation in virtio core is still necessary since they're working at different levels and it's hard to force the validation in all drivers by codes. Last version introduces a
suppress_driver_validationto allow the driver to suppress the core validation which seems not good, we need a way to force thevirtio_ringcode to do validation before.
Quoted 256 lines
Why do we? If driver validates length
virtio_ringdoes not need to validate. If driver does not use lengthvirtio_ringdoes not need to validate. core can provide this service for the gazillion non performance critical drivers that just want to keep things simple, but the 4-5 critical ones can do their own validation if they want to.Quoted 249 lines
Or such stuff could be added
on top since the validation is by default anyway.Thanks
Quoted 242 lines
Quoted 241 lines
Quoted 4 lines
I'd like to know the fate of
VIRTIO_HARDEN_NOTIFICATIONbefore we do more hardening. If it's irrevocably broken let's rip it out?So the plan is
1) finish used ring validation (this had been proposed, merged and reverted before notification hardening)
2) do notification hardening on top.So let's leave it as is and I will do a rework after we finalize the used ring validation.
Thanks
Quoted 225 lines
Quoted 224 lines
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)drivers/virtio/virtio_ring.c +75
-1@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */ @@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ; err: --2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 114 lines · Jason Wang
Quoted 72 lines · Michael S. Tsirkin
Quoted 63 lines · Jason Wang
Quoted 37 lines · Michael S. Tsirkin
Quoted 35 lines · Jason Wang
Quoted 33 lines · Michael S. Tsirkin
Quoted 1 line · Jason Wang
This patch validate
validates
Quoted 2 lines
the used buffer length provided by the device
before trying to use it.before returning it to caller
Quoted 4 lines
This is done by remembering the in buffer
length in a dedicated array duringvirtqueue_add(), then we can fail thevirtqueue_get_buf()when we find the device is trying to give us a used buffer length which is greater than we stored before.than what we stored
Quoted 2 lines
This validation is disable
disabled
Quoted 5 lines
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Signed-off-by Jason Wang <[email protected]>First I'm not merging this without more data about what is known to be broken and what is known to work well in the commit log. And how exactly do things work if used length is wrong?
Quoted 8 lines
Quoted 4 lines
Assuming the device is malicious, it would be very hard to answer. Auditing and fuzzing won't cover every case. Instead of trying to seek the answer, we can simply make sure the used in buffer length is validated then we know we're fine or not.
To restate the question, you said above "some legacy devices are known to report buggy used length". If they report buggy length then how can things work?
The validation is disabled for legacy device (as stated in the changelog):
static bool
vring_needs_used_validation(const struct virtio_device *vdev){ /* * Several legacy devices are known to produce buggy used * length. In order to let driver work, we won't validate used * buffer length in this case. */ if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) return false; if (force_used_validation) return true; return false; }Quoted 6 lines
This seems to be what we've agreed in last version:
Thanks
Quoted 8 lines
I don't get it. You wrote:
This validation is disable
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.which devices?
legacy rpmsg and vsock device (before
49d8c5ffad07) at least.Quoted 1 line
why do you need a module parameter?
If we enable it unconditionally for modern devices, it may break some buggy moden device (vsock without a fix as an example).
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 10 lines
Quoted 5 lines
Second what's wrong with
dma_desc_extrathat we already maintain? Third motivation - it's part and parcel of the hardening effort yes?They are different.
dma_desc_extrais for a descriptor ring, but this is for a used ring. Technically we can go back to iterate on the descriptor ring for a legal used in buffer length. But it will have worse performance.I don't really understand. We already iterate when we unmap - all that is necessary is to subtract it from used length, if at the end of the process it is >0 then we know used length is too large.
Quoted 4 lines
Quoted 2 lines
Yes, but it is the job that is done in the driver level not the virtio core.
What job?
I meant the driver can do the validation since it has the knowledge of the buffer length if it wants.
Quoted 22 lines
Quoted 2 lines
unmap is done in
detach_buf_splitanddetach_buf_packedrespectively.vring_desc_extraisn't even visible outsidedrivers/virtio/virtio_ring.c
desc_extradoesn't contain buffer length for the case of indirect descriptors. So we need to iterate in the descriptors when it looks expensive if we don't need unmap.Thanks
Quoted 13 lines
For drivers that do unmap at driver level - I guess they can do validation there too.
Quoted 6 lines
Validation in virtio core is still necessary since they're working at different levels and it's hard to force the validation in all drivers by codes. Last version introduces a
suppress_driver_validationto allow the driver to suppress the core validation which seems not good, we need a way to force thevirtio_ringcode to do validation before.Why do we? If driver validates length
virtio_ringdoes not need to validate.
To be more safe, there's no guarantee that there's no bug in the driver.
Quoted 2 lines
If driver does not use length
virtio_ringdoes not need toQuoted 1 line
validate.
This could be done on top assuming the validation is disabled by default. But if the administrator wants to have belt and braces we need to leave an option for them.
Thanks
Quoted 254 lines
core can provide this service for the gazillion non
Quoted 253 lines
performance critical drivers that just want to keep things simple, but the 4-5 critical ones can do their own validation if they want to.
Quoted 249 lines
Or such stuff could be added
on top since the validation is by default anyway.Thanks
Quoted 242 lines
Quoted 241 lines
Quoted 4 lines
I'd like to know the fate of
VIRTIO_HARDEN_NOTIFICATIONbefore we do more hardening. If it's irrevocably broken let's rip it out?So the plan is
1) finish used ring validation (this had been proposed, merged and reverted before notification hardening)
2) do notification hardening on top.So let's leave it as is and I will do a rework after we finalize the used ring validation.
Thanks
Quoted 225 lines
Quoted 224 lines
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)drivers/virtio/virtio_ring.c +75
-1@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */ @@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ; err: --2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Michael S. Tsirkin
maintainerQuoted 135 lines · Jason Wang
Quoted 134 lines · Jason Wang
Quoted 72 lines · Michael S. Tsirkin
Quoted 63 lines · Jason Wang
Quoted 37 lines · Michael S. Tsirkin
Quoted 35 lines · Jason Wang
Quoted 33 lines · Michael S. Tsirkin
Quoted 1 line · Jason Wang
This patch validate
validates
Quoted 2 lines
the used buffer length provided by the device
before trying to use it.before returning it to caller
Quoted 4 lines
This is done by remembering the in buffer
length in a dedicated array duringvirtqueue_add(), then we can fail thevirtqueue_get_buf()when we find the device is trying to give us a used buffer length which is greater than we stored before.than what we stored
Quoted 2 lines
This validation is disable
disabled
Quoted 5 lines
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Signed-off-by Jason Wang <[email protected]>First I'm not merging this without more data about what is known to be broken and what is known to work well in the commit log. And how exactly do things work if used length is wrong?
Quoted 8 lines
Quoted 4 lines
Assuming the device is malicious, it would be very hard to answer. Auditing and fuzzing won't cover every case. Instead of trying to seek the answer, we can simply make sure the used in buffer length is validated then we know we're fine or not.
To restate the question, you said above "some legacy devices are known to report buggy used length". If they report buggy length then how can things work?
The validation is disabled for legacy device (as stated in the changelog):
static bool
vring_needs_used_validation(const struct virtio_device *vdev){ /* * Several legacy devices are known to produce buggy used * length. In order to let driver work, we won't validate used * buffer length in this case. */ if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) return false; if (force_used_validation) return true; return false; }Quoted 6 lines
This seems to be what we've agreed in last version:
Thanks
Quoted 8 lines
I don't get it. You wrote:
This validation is disable
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.which devices?
legacy rpmsg and vsock device (before
49d8c5ffad07) at least.Quoted 1 line
why do you need a module parameter?
If we enable it unconditionally for modern devices, it may break some buggy moden device (vsock without a fix as an example).
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 10 lines
Quoted 5 lines
Second what's wrong with
dma_desc_extrathat we already maintain? Third motivation - it's part and parcel of the hardening effort yes?They are different.
dma_desc_extrais for a descriptor ring, but this is for a used ring. Technically we can go back to iterate on the descriptor ring for a legal used in buffer length. But it will have worse performance.I don't really understand. We already iterate when we unmap - all that is necessary is to subtract it from used length, if at the end of the process it is >0 then we know used length is too large.
Quoted 4 lines
Quoted 2 lines
Yes, but it is the job that is done in the driver level not the virtio core.
What job?
I meant the driver can do the validation since it has the knowledge of the buffer length if it wants.
Quoted 2 lines
unmap is done in
detach_buf_splitanddetach_buf_packedrespectively.vring_desc_extraisn't even visible outsidedrivers/virtio/virtio_ring.c
desc_extradoesn't contain buffer length for the case of indirect descriptors. So we need to iterate in the descriptors when it looks expensive if we don't need unmap.Thanks
Quoted 11 lines
For drivers that do unmap at driver level - I guess they can do validation there too.
Quoted 6 lines
Validation in virtio core is still necessary since they're working at different levels and it's hard to force the validation in all drivers by codes. Last version introduces a
suppress_driver_validationto allow the driver to suppress the core validation which seems not good, we need a way to force thevirtio_ringcode to do validation before.
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Why do we? If driver validates length
virtio_ringdoes not need to validate.To be more safe, there's no guarantee that there's no bug in the driver.
Extra options increase testing matrix size so - there be bugs. We need to make these decisions for (most) users.
Quoted 8 lines
Quoted 2 lines
If driver does not use length
virtio_ringdoes not need toQuoted 1 line
validate.
This could be done on top assuming the validation is disabled by default. But if the administrator wants to have belt and braces we need to leave an option for them.
Thanks
No, we don't regress then fix on top.
As for mod parameter I am not impressed -
no one's going to have the time or inclination to do the requisite testing to know whether the module parameter is safe.
Quoted 248 lines
Quoted 248 lines
core can provide this service for the gazillion non
Quoted 247 lines
performance critical drivers that just want to keep things simple, but the 4-5 critical ones can do their own validation if they want to.
Quoted 244 lines
Or such stuff could be added
on top since the validation is by default anyway.Thanks
Quoted 237 lines
Quoted 237 lines
Quoted 4 lines
I'd like to know the fate of
VIRTIO_HARDEN_NOTIFICATIONbefore we do more hardening. If it's irrevocably broken let's rip it out?So the plan is
1) finish used ring validation (this had been proposed, merged and reverted before notification hardening)
2) do notification hardening on top.So let's leave it as is and I will do a rework after we finalize the used ring validation.
Thanks
Quoted 221 lines
Quoted 221 lines
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)drivers/virtio/virtio_ring.c +75
-0@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */ @@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ;
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
drivers/virtio/virtio_ring.c +0
-1err: --2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 137 lines · Michael S. Tsirkin
Quoted 135 lines · Jason Wang
Quoted 134 lines · Jason Wang
Quoted 72 lines · Michael S. Tsirkin
Quoted 63 lines · Jason Wang
Quoted 37 lines · Michael S. Tsirkin
Quoted 35 lines · Jason Wang
Quoted 33 lines · Michael S. Tsirkin
Quoted 1 line · Jason Wang
This patch validate
validates
Quoted 2 lines
the used buffer length provided by the device
before trying to use it.before returning it to caller
Quoted 4 lines
This is done by remembering the in buffer
length in a dedicated array duringvirtqueue_add(), then we can fail thevirtqueue_get_buf()when we find the device is trying to give us a used buffer length which is greater than we stored before.than what we stored
Quoted 2 lines
This validation is disable
disabled
Quoted 5 lines
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Signed-off-by Jason Wang <[email protected]>First I'm not merging this without more data about what is known to be broken and what is known to work well in the commit log. And how exactly do things work if used length is wrong?
Quoted 8 lines
Quoted 4 lines
Assuming the device is malicious, it would be very hard to answer. Auditing and fuzzing won't cover every case. Instead of trying to seek the answer, we can simply make sure the used in buffer length is validated then we know we're fine or not.
To restate the question, you said above "some legacy devices are known to report buggy used length". If they report buggy length then how can things work?
The validation is disabled for legacy device (as stated in the changelog):
static bool
vring_needs_used_validation(const struct virtio_device *vdev){ /* * Several legacy devices are known to produce buggy used * length. In order to let driver work, we won't validate used * buffer length in this case. */ if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) return false; if (force_used_validation) return true; return false; }Quoted 6 lines
This seems to be what we've agreed in last version:
Thanks
Quoted 8 lines
I don't get it. You wrote:
This validation is disable
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.which devices?
legacy rpmsg and vsock device (before
49d8c5ffad07) at least.Quoted 1 line
why do you need a module parameter?
If we enable it unconditionally for modern devices, it may break some buggy moden device (vsock without a fix as an example).
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 10 lines
Quoted 5 lines
Second what's wrong with
dma_desc_extrathat we already maintain? Third motivation - it's part and parcel of the hardening effort yes?They are different.
dma_desc_extrais for a descriptor ring, but this is for a used ring. Technically we can go back to iterate on the descriptor ring for a legal used in buffer length. But it will have worse performance.I don't really understand. We already iterate when we unmap - all that is necessary is to subtract it from used length, if at the end of the process it is >0 then we know used length is too large.
Quoted 4 lines
Quoted 2 lines
Yes, but it is the job that is done in the driver level not the virtio core.
What job?
I meant the driver can do the validation since it has the knowledge of the buffer length if it wants.
Quoted 2 lines
unmap is done in
detach_buf_splitanddetach_buf_packedrespectively.vring_desc_extraisn't even visible outsidedrivers/virtio/virtio_ring.c
desc_extradoesn't contain buffer length for the case of indirect descriptors. So we need to iterate in the descriptors when it looks expensive if we don't need unmap.Thanks
Quoted 11 lines
For drivers that do unmap at driver level - I guess they can do validation there too.
Quoted 6 lines
Validation in virtio core is still necessary since they're working at different levels and it's hard to force the validation in all drivers by codes. Last version introduces a
suppress_driver_validationto allow the driver to suppress the core validation which seems not good, we need a way to force thevirtio_ringcode to do validation before.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Why do we? If driver validates length
virtio_ringdoes not need to validate.To be more safe, there's no guarantee that there's no bug in the driver.
Extra options increase testing matrix size so - there be bugs. We need to make these decisions for (most) users.
In some cases the requirement is conflict:
1) for "legacy users", the requirement is not to break existing setups
2) for confidential users, the requirement is to detect and fail early instead of trying to mitigate silently
If we are trying to go in the middle (for example, doing a cap) it may end up with a result that can satisfy neither:
1) it might still break in some drivers and setups like legacy, then we need an option to disable it
2) it won't satisfy the users that ask for security
If we had an option, the management layer can choose to provision VM/guests with the good kernel command line then we are fine.
Quoted 11 lines
Quoted 9 lines
Quoted 3 lines
If driver does not use length
virtio_ringdoes not need toQuoted 1 line
validate.
This could be done on top assuming the validation is disabled by default. But if the administrator wants to have belt and braces we need to leave an option for them.
Thanks
No, we don't regress then fix on top.
I'm not sure I understand here. It would be very hard to say there won't be regression for any new codes, especially virtio had already supported several different types of devices and drivers.
Quoted 3 lines
As for mod parameter I am not impressed -
no one's going to have the time or inclination to do the requisite testing to know whether the module parameter is safe.
Actually, that's one advantage, most of the users don't care so the validation is just suppressed for them. Otherwise they might get surprised after upgrading. This is what I learnt from the past hardening effort like this and the IRQ hardening.
For the rest like confidential vendors, they need to test and make sure such kernel command line were provisioned to the VM.
Thanks
Quoted 249 lines
Quoted 249 lines
Quoted 249 lines
core can provide this service for the gazillion non
Quoted 247 lines
performance critical drivers that just want to keep things simple, but the 4-5 critical ones can do their own validation if they want to.
Quoted 244 lines
Or such stuff could be added
on top since the validation is by default anyway.Thanks
Quoted 237 lines
Quoted 237 lines
Quoted 4 lines
I'd like to know the fate of
VIRTIO_HARDEN_NOTIFICATIONbefore we do more hardening. If it's irrevocably broken let's rip it out?So the plan is
1) finish used ring validation (this had been proposed, merged and reverted before notification hardening)
2) do notification hardening on top.So let's leave it as is and I will do a rework after we finalize the used ring validation.
Thanks
Quoted 221 lines
Quoted 221 lines
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)drivers/virtio/virtio_ring.c +75
-0@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */ @@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ;
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
drivers/virtio/virtio_ring.c +0
-1err: --2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Michael S. Tsirkin
maintainerQuoted 143 lines · Jason Wang
Quoted 142 lines · Michael S. Tsirkin
Quoted 139 lines · Jason Wang
Quoted 114 lines · Jason Wang
Quoted 72 lines · Michael S. Tsirkin
Quoted 63 lines · Jason Wang
Quoted 37 lines · Michael S. Tsirkin
Quoted 35 lines · Jason Wang
Quoted 33 lines · Michael S. Tsirkin
Quoted 1 line · Jason Wang
This patch validate
validates
Quoted 2 lines
the used buffer length provided by the device
before trying to use it.before returning it to caller
Quoted 4 lines
This is done by remembering the in buffer
length in a dedicated array duringvirtqueue_add(), then we can fail thevirtqueue_get_buf()when we find the device is trying to give us a used buffer length which is greater than we stored before.than what we stored
Quoted 2 lines
This validation is disable
disabled
Quoted 5 lines
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Signed-off-by Jason Wang <[email protected]>First I'm not merging this without more data about what is known to be broken and what is known to work well in the commit log. And how exactly do things work if used length is wrong?
Quoted 8 lines
Quoted 4 lines
Assuming the device is malicious, it would be very hard to answer. Auditing and fuzzing won't cover every case. Instead of trying to seek the answer, we can simply make sure the used in buffer length is validated then we know we're fine or not.
To restate the question, you said above "some legacy devices are known to report buggy used length". If they report buggy length then how can things work?
The validation is disabled for legacy device (as stated in the changelog):
static bool
vring_needs_used_validation(const struct virtio_device *vdev){ /* * Several legacy devices are known to produce buggy used * length. In order to let driver work, we won't validate used * buffer length in this case. */ if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) return false; if (force_used_validation) return true; return false; }Quoted 6 lines
This seems to be what we've agreed in last version:
Thanks
Quoted 8 lines
I don't get it. You wrote:
This validation is disable
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.which devices?
legacy rpmsg and vsock device (before
49d8c5ffad07) at least.Quoted 1 line
why do you need a module parameter?
If we enable it unconditionally for modern devices, it may break some buggy moden device (vsock without a fix as an example).
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 10 lines
Quoted 5 lines
Second what's wrong with
dma_desc_extrathat we already maintain? Third motivation - it's part and parcel of the hardening effort yes?They are different.
dma_desc_extrais for a descriptor ring, but this is for a used ring. Technically we can go back to iterate on the descriptor ring for a legal used in buffer length. But it will have worse performance.I don't really understand. We already iterate when we unmap - all that is necessary is to subtract it from used length, if at the end of the process it is >0 then we know used length is too large.
Quoted 4 lines
Quoted 2 lines
Yes, but it is the job that is done in the driver level not the virtio core.
What job?
I meant the driver can do the validation since it has the knowledge of the buffer length if it wants.
Quoted 22 lines
Quoted 2 lines
unmap is done in
detach_buf_splitanddetach_buf_packedrespectively.vring_desc_extraisn't even visible outsidedrivers/virtio/virtio_ring.c
desc_extradoesn't contain buffer length for the case of indirect descriptors. So we need to iterate in the descriptors when it looks expensive if we don't need unmap.Thanks
Quoted 13 lines
For drivers that do unmap at driver level - I guess they can do validation there too.
Quoted 6 lines
Validation in virtio core is still necessary since they're working at different levels and it's hard to force the validation in all drivers by codes. Last version introduces a
suppress_driver_validationto allow the driver to suppress the core validation which seems not good, we need a way to force thevirtio_ringcode to do validation before.Why do we? If driver validates length
virtio_ringdoes not need to validate.To be more safe, there's no guarantee that there's no bug in the driver.
Quoted 8 lines
Quoted 2 lines
Extra options increase testing matrix size so - there be bugs. We need to make these decisions for (most) users.
In some cases the requirement is conflict:
1) for "legacy users", the requirement is not to break existing setups
2) for confidential users, the requirement is to detect and fail early instead of trying to mitigate silently
I think 2 is a red herring. No one wants panics/failures, it is just tolerated in the confidential setup.
Quoted 5 lines
If we are trying to go in the middle (for example, doing a cap) it may end up with a result that can satisfy neither:
1) it might still break in some drivers and setups like legacy, then we need an option to disable it
I don't see how cap can break anything.
Quoted 13 lines
2) it won't satisfy the users that ask for security
If we had an option, the management layer can choose to provision VM/guests with the good kernel command line then we are fine.
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
If driver does not use length
virtio_ringdoes not need toQuoted 1 line
validate.
This could be done on top assuming the validation is disabled by default. But if the administrator wants to have belt and braces we need to leave an option for them.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Thanks
No, we don't regress then fix on top.
I'm not sure I understand here. It would be very hard to say there won't be regression for any new codes, especially virtio had already supported several different types of devices and drivers.
Sure. We test what we can, just don't introduce untested code or known regressions.
Quoted 9 lines
Quoted 3 lines
As for mod parameter I am not impressed -
no one's going to have the time or inclination to do the requisite testing to know whether the module parameter is safe.Actually, that's one advantage, most of the users don't care so the validation is just suppressed for them. Otherwise they might get surprised after upgrading. This is what I learnt from the past hardening effort like this and the IRQ hardening.
Quoted 4 lines
For the rest like confidential vendors, they need to test and make sure such kernel command line were provisioned to the VM.
Thanks
Each new boolean option doubles QE effort. One of the reasons confidential seems to be successful where previous DRM schemes mostly failed is because it allows running Linux without many changes.
Quoted 256 lines
Quoted 256 lines
Quoted 255 lines
Quoted 255 lines
core can provide this service for the gazillion non
Quoted 253 lines
performance critical drivers that just want to keep things simple, but the 4-5 critical ones can do their own validation if they want to.
Quoted 249 lines
Or such stuff could be added
on top since the validation is by default anyway.Thanks
Quoted 242 lines
Quoted 241 lines
Quoted 4 lines
I'd like to know the fate of
VIRTIO_HARDEN_NOTIFICATIONbefore we do more hardening. If it's irrevocably broken let's rip it out?So the plan is
1) finish used ring validation (this had been proposed, merged and reverted before notification hardening)
2) do notification hardening on top.So let's leave it as is and I will do a rework after we finalize the used ring validation.
Thanks
Quoted 225 lines
Quoted 224 lines
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)drivers/virtio/virtio_ring.c +75
-1@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */ @@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ; err: --2.25.1
Michael S. Tsirkin
maintainerQuoted 80 lines · Jason Wang
Quoted 79 lines · Michael S. Tsirkin
Quoted 63 lines · Jason Wang
Quoted 37 lines · Michael S. Tsirkin
Quoted 35 lines · Jason Wang
Quoted 33 lines · Michael S. Tsirkin
Quoted 1 line · Jason Wang
This patch validate
validates
Quoted 2 lines
the used buffer length provided by the device
before trying to use it.before returning it to caller
Quoted 4 lines
This is done by remembering the in buffer
length in a dedicated array duringvirtqueue_add(), then we can fail thevirtqueue_get_buf()when we find the device is trying to give us a used buffer length which is greater than we stored before.than what we stored
Quoted 2 lines
This validation is disable
disabled
Quoted 5 lines
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Signed-off-by Jason Wang <[email protected]>First I'm not merging this without more data about what is known to be broken and what is known to work well in the commit log. And how exactly do things work if used length is wrong?
Quoted 8 lines
Quoted 4 lines
Assuming the device is malicious, it would be very hard to answer. Auditing and fuzzing won't cover every case. Instead of trying to seek the answer, we can simply make sure the used in buffer length is validated then we know we're fine or not.
To restate the question, you said above "some legacy devices are known to report buggy used length". If they report buggy length then how can things work?
The validation is disabled for legacy device (as stated in the changelog):
static bool
vring_needs_used_validation(const struct virtio_device *vdev){ /* * Several legacy devices are known to produce buggy used * length. In order to let driver work, we won't validate used * buffer length in this case. */ if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) return false; if (force_used_validation) return true; return false; }Quoted 6 lines
This seems to be what we've agreed in last version:
Thanks
I don't get it. You wrote:
This validation is disable
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.
Quoted 8 lines
Quoted 1 line
which devices?
legacy rpmsg and vsock device (before
49d8c5ffad07) at least.Quoted 1 line
why do you need a module parameter?
If we enable it unconditionally for modern devices, it may break some buggy moden device (vsock without a fix as an example).
Presumably this happens because vsock does not have any inbuf at all so it ignores the length.
We had the same with virtio net tx a long time ago.
My suggestion is then not to fail - just cap length at the dma length set by driver. Another idea is that if dma len is 0 then don't validate at all - driver that did not add any inbufs is not going to look at length.
Allowing passing NULL as length and skipping validation if len = 0 might also be a good idea.
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 10 lines
Quoted 5 lines
Second what's wrong with
dma_desc_extrathat we already maintain? Third motivation - it's part and parcel of the hardening effort yes?They are different.
dma_desc_extrais for a descriptor ring, but this is for a used ring. Technically we can go back to iterate on the descriptor ring for a legal used in buffer length. But it will have worse performance.I don't really understand. We already iterate when we unmap - all that is necessary is to subtract it from used length, if at the end of the process it is >0 then we know used length is too large.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Yes, but it is the job that is done in the driver level not the virtio core.
What job?
I meant the driver can do the validation since it has the knowledge of the buffer length if it wants.
It does not necessarily have it - not if virtio is doing DMA mapping.
Quoted 8 lines
Quoted 2 lines
unmap is done in
detach_buf_splitanddetach_buf_packedrespectively.vring_desc_extraisn't even visible outsidedrivers/virtio/virtio_ring.c
desc_extradoesn't contain buffer length for the case of indirect descriptors. So we need to iterate in the descriptors when it looks expensive if we don't need unmap.Thanks
Well at the moment we only don't unmap if DMA API is bypassed. And then we don't need to validate length either. Fundamentally, without ACCESS_PLATFORM device is trusted.
Quoted 261 lines
Quoted 261 lines
For drivers that do unmap at driver level - I guess they can do validation there too.
Quoted 6 lines
Validation in virtio core is still necessary since they're working at different levels and it's hard to force the validation in all drivers by codes. Last version introduces a
suppress_driver_validationto allow the driver to suppress the core validation which seems not good, we need a way to force thevirtio_ringcode to do validation before.Why do we? If driver validates length
virtio_ringdoes not need to validate. If driver does not use lengthvirtio_ringdoes not need to validate. core can provide this service for the gazillion non performance critical drivers that just want to keep things simple, but the 4-5 critical ones can do their own validation if they want to.Quoted 244 lines
Or such stuff could be added
on top since the validation is by default anyway.Thanks
Quoted 237 lines
Quoted 237 lines
Quoted 4 lines
I'd like to know the fate of
VIRTIO_HARDEN_NOTIFICATIONbefore we do more hardening. If it's irrevocably broken let's rip it out?So the plan is
1) finish used ring validation (this had been proposed, merged and reverted before notification hardening)
2) do notification hardening on top.So let's leave it as is and I will do a rework after we finalize the used ring validation.
Thanks
Quoted 221 lines
Quoted 221 lines
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)drivers/virtio/virtio_ring.c +75
-0@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */ @@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ;
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
drivers/virtio/virtio_ring.c +0
-1err: --2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 95 lines · Michael S. Tsirkin
Quoted 80 lines · Jason Wang
Quoted 79 lines · Michael S. Tsirkin
Quoted 63 lines · Jason Wang
Quoted 37 lines · Michael S. Tsirkin
Quoted 35 lines · Jason Wang
Quoted 33 lines · Michael S. Tsirkin
Quoted 1 line · Jason Wang
This patch validate
validates
Quoted 2 lines
the used buffer length provided by the device
before trying to use it.before returning it to caller
Quoted 4 lines
This is done by remembering the in buffer
length in a dedicated array duringvirtqueue_add(), then we can fail thevirtqueue_get_buf()when we find the device is trying to give us a used buffer length which is greater than we stored before.than what we stored
Quoted 2 lines
This validation is disable
disabled
Quoted 5 lines
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Signed-off-by Jason Wang <[email protected]>First I'm not merging this without more data about what is known to be broken and what is known to work well in the commit log. And how exactly do things work if used length is wrong?
Quoted 8 lines
Quoted 4 lines
Assuming the device is malicious, it would be very hard to answer. Auditing and fuzzing won't cover every case. Instead of trying to seek the answer, we can simply make sure the used in buffer length is validated then we know we're fine or not.
To restate the question, you said above "some legacy devices are known to report buggy used length". If they report buggy length then how can things work?
The validation is disabled for legacy device (as stated in the changelog):
static bool
vring_needs_used_validation(const struct virtio_device *vdev){ /* * Several legacy devices are known to produce buggy used * length. In order to let driver work, we won't validate used * buffer length in this case. */ if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) return false; if (force_used_validation) return true; return false; }Quoted 6 lines
This seems to be what we've agreed in last version:
Thanks
I don't get it. You wrote:
This validation is disable
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Quoted 8 lines
Quoted 1 line
which devices?
legacy rpmsg and vsock device (before
49d8c5ffad07) at least.Quoted 1 line
why do you need a module parameter?
If we enable it unconditionally for modern devices, it may break some buggy moden device (vsock without a fix as an example).
Presumably this happens because vsock does not have any inbuf at all so it ignores the length.
We had the same with virtio net tx a long time ago.
Quoted 7 lines
My suggestion is then not to fail - just cap length at the dma length set by driver. Another idea is that if dma len is 0 then don't validate at all - driver that did not add any inbufs is not going to look at length.
Allowing passing NULL as length and skipping validation if len = 0 might also be a good idea.
The above should work for sure, but a question is, in the case of confidential computing, if a driver detects a malicious device, is it really good to try to keep working or warn and disable the device? Or maybe we can have an option for the user to decide?
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 13 lines
Quoted 5 lines
Quoted 7 lines
Quoted 2 lines
Second what's wrong with
dma_desc_extrathat we already maintain? Third motivation - it's part and parcel of the hardening effort yes?They are different.
dma_desc_extrais for a descriptor ring, but this is for a used ring. Technically we can go back to iterate on the descriptor ring for a legal used in buffer length. But it will have worse performance.Quoted 4 lines
I don't really understand. We already iterate when we unmap - all that is necessary is to subtract it from used length, if at the end of the process it is >0 then we know used length is too large.
Yes, but it is the job that is done in the driver level not the virtio core.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
What job?
I meant the driver can do the validation since it has the knowledge of the buffer length if it wants.
It does not necessarily have it - not if virtio is doing DMA mapping.
I don't see any dependencies for DMA, I mean anyhow the in buffer is allocated by the driver, so it should know how large it is.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
unmap is done in
detach_buf_splitanddetach_buf_packedrespectively.vring_desc_extraisn't even visible outsidedrivers/virtio/virtio_ring.c
Quoted 7 lines
Quoted 5 lines
desc_extradoesn't contain buffer length for the case of indirect descriptors. So we need to iterate in the descriptors when it looks expensive if we don't need unmap.Thanks
Well at the moment we only don't unmap if DMA API is bypassed.
If I understand correctly, we will probably support premapped DMA buffers soon (AF_XDP and page pool)?
Thanks
Quoted 273 lines
And then
we don't need to validate length either. Fundamentally, withoutACCESS_PLATFORMdevice is trusted.Quoted 267 lines
Quoted 267 lines
For drivers that do unmap at driver level - I guess they can do validation there too.
Quoted 6 lines
Validation in virtio core is still necessary since they're working at different levels and it's hard to force the validation in all drivers by codes. Last version introduces a
suppress_driver_validationto allow the driver to suppress the core validation which seems not good, we need a way to force thevirtio_ringcode to do validation before.Why do we? If driver validates length
virtio_ringdoes not need to validate. If driver does not use lengthvirtio_ringdoes not need to validate. core can provide this service for the gazillion non performance critical drivers that just want to keep things simple, but the 4-5 critical ones can do their own validation if they want to.Quoted 249 lines
Or such stuff could be added
on top since the validation is by default anyway.Thanks
Quoted 242 lines
Quoted 241 lines
Quoted 4 lines
I'd like to know the fate of
VIRTIO_HARDEN_NOTIFICATIONbefore we do more hardening. If it's irrevocably broken let's rip it out?So the plan is
1) finish used ring validation (this had been proposed, merged and reverted before notification hardening)
2) do notification hardening on top.So let's leave it as is and I will do a rework after we finalize the used ring validation.
Thanks
Quoted 225 lines
Quoted 224 lines
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)drivers/virtio/virtio_ring.c +75
-1@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */ @@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ; err: --2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Michael S. Tsirkin
maintainerQuoted 101 lines · Jason Wang
Quoted 100 lines · Michael S. Tsirkin
Quoted 80 lines · Jason Wang
Quoted 79 lines · Michael S. Tsirkin
Quoted 63 lines · Jason Wang
Quoted 37 lines · Michael S. Tsirkin
Quoted 35 lines · Jason Wang
Quoted 33 lines · Michael S. Tsirkin
Quoted 1 line · Jason Wang
This patch validate
validates
Quoted 2 lines
the used buffer length provided by the device
before trying to use it.before returning it to caller
Quoted 4 lines
This is done by remembering the in buffer
length in a dedicated array duringvirtqueue_add(), then we can fail thevirtqueue_get_buf()when we find the device is trying to give us a used buffer length which is greater than we stored before.than what we stored
Quoted 2 lines
This validation is disable
disabled
Quoted 5 lines
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Signed-off-by Jason Wang <[email protected]>First I'm not merging this without more data about what is known to be broken and what is known to work well in the commit log. And how exactly do things work if used length is wrong?
Quoted 8 lines
Quoted 4 lines
Assuming the device is malicious, it would be very hard to answer. Auditing and fuzzing won't cover every case. Instead of trying to seek the answer, we can simply make sure the used in buffer length is validated then we know we're fine or not.
To restate the question, you said above "some legacy devices are known to report buggy used length". If they report buggy length then how can things work?
The validation is disabled for legacy device (as stated in the changelog):
static bool
vring_needs_used_validation(const struct virtio_device *vdev){ /* * Several legacy devices are known to produce buggy used * length. In order to let driver work, we won't validate used * buffer length in this case. */ if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) return false; if (force_used_validation) return true; return false; }Quoted 6 lines
This seems to be what we've agreed in last version:
Thanks
I don't get it. You wrote:
This validation is disable
by default via module parameter to unbreak
some existing devices since some legacy devices are known to report buggy used length.Quoted 8 lines
Quoted 1 line
which devices?
legacy rpmsg and vsock device (before
49d8c5ffad07) at least.Quoted 1 line
why do you need a module parameter?
If we enable it unconditionally for modern devices, it may break some buggy moden device (vsock without a fix as an example).
Presumably this happens because vsock does not have any inbuf at all so it ignores the length.
We had the same with virtio net tx a long time ago.My suggestion is then not to fail - just cap length at the dma length set by driver. Another idea is that if dma len is 0 then don't validate at all - driver that did not add any inbufs is not going to look at length.
Quoted 7 lines
Quoted 2 lines
Allowing passing NULL as length and skipping validation if len = 0 might also be a good idea.
The above should work for sure, but a question is, in the case of confidential computing, if a driver detects a malicious device, is it really good to try to keep working or warn and disable the device? Or maybe we can have an option for the user to decide?
A minor bug in an unused field is not worth panicing about.
Quoted 41 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 13 lines
Quoted 5 lines
Quoted 7 lines
Quoted 2 lines
Second what's wrong with
dma_desc_extrathat we already maintain? Third motivation - it's part and parcel of the hardening effort yes?They are different.
dma_desc_extrais for a descriptor ring, but this is for a used ring. Technically we can go back to iterate on the descriptor ring for a legal used in buffer length. But it will have worse performance.Quoted 4 lines
I don't really understand. We already iterate when we unmap - all that is necessary is to subtract it from used length, if at the end of the process it is >0 then we know used length is too large.
Yes, but it is the job that is done in the driver level not the virtio core.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
What job?
I meant the driver can do the validation since it has the knowledge of the buffer length if it wants.
It does not necessarily have it - not if virtio is doing DMA mapping.
I don't see any dependencies for DMA, I mean anyhow the in buffer is allocated by the driver, so it should know how large it is.
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines
unmap is done in
detach_buf_splitanddetach_buf_packedrespectively.vring_desc_extraisn't even visible outsidedrivers/virtio/virtio_ring.c
desc_extradoesn't contain buffer length for the case of indirect descriptors. So we need to iterate in the descriptors when it looks expensive if we don't need unmap.
Quoted 282 lines
Quoted 3 lines
Quoted 1 line
Thanks
Well at the moment we only don't unmap if DMA API is bypassed.
If I understand correctly, we will probably support premapped DMA buffers soon (
AF_XDPand page pool)?Thanks
Quoted 273 lines
And then
we don't need to validate length either. Fundamentally, withoutACCESS_PLATFORMdevice is trusted.Quoted 267 lines
Quoted 267 lines
For drivers that do unmap at driver level - I guess they can do validation there too.
Quoted 6 lines
Validation in virtio core is still necessary since they're working at different levels and it's hard to force the validation in all drivers by codes. Last version introduces a
suppress_driver_validationto allow the driver to suppress the core validation which seems not good, we need a way to force thevirtio_ringcode to do validation before.Why do we? If driver validates length
virtio_ringdoes not need to validate. If driver does not use lengthvirtio_ringdoes not need to validate. core can provide this service for the gazillion non performance critical drivers that just want to keep things simple, but the 4-5 critical ones can do their own validation if they want to.Quoted 249 lines
Or such stuff could be added
on top since the validation is by default anyway.Thanks
Quoted 242 lines
Quoted 241 lines
Quoted 4 lines
I'd like to know the fate of
VIRTIO_HARDEN_NOTIFICATIONbefore we do more hardening. If it's irrevocably broken let's rip it out?So the plan is
1) finish used ring validation (this had been proposed, merged and reverted before notification hardening)
2) do notification hardening on top.So let's leave it as is and I will do a rework after we finalize the used ring validation.
Thanks
Quoted 225 lines
Quoted 224 lines
---
Changes since V4:
- drop the flat for driver to suppress the check
- validation is disabled by default
- don't do validation for legacy device
- rebase and support virtqueue resize--- drivers/virtio/virtio_ring.c | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)drivers/virtio/virtio_ring.c +75
-1@@ -15,6 +15,9 @@#include <linux/spinlock.h> #include <xen/xen.h> +static bool force_used_validation = false; +module_param (force_used_validation, bool,0444 ); +#ifdef DEBUG /* For development, we want to crash whenever the ring is screwed. */ #define BAD_RING(_vq, fmt, args...) \@@ -105,6 +108,9 @@ struct vring_virtqueue_split {struct vring_desc_state_split *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t queue_dma_addr;size_t queue_size_in_bytes;@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {struct vring_desc_state_packed *desc_state;struct vring_desc_extra *desc_extra; +/* Maximum in buffer length, NULL means no used validation */ +u32 *buflen; +/* DMA address and size information */ dma_addr_t ring_dma_addr;dma_addr_t driver_event_dma_addr;@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsigned int i, n, avail, descs_used, prev, err_idx;int head;bool indirect; +u32 buflen =0 ;START_USE (vq);@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,VRING_DESC_F_NEXT |VRING_DESC_F_WRITE , indirect); + buflen += sg->length; } }/* Last one doesn't continue. */ @@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,else vq->split.desc_state[head].indir_desc = ctx; +/* Store in buffer length if necessary */ +if (vq->split.buflen) + vq->split.buflen[head] = buflen; +/* Put entry in available array (but don't update avail->idx until they * do sync). */ avail = vq->split.avail_idx_shadow & (vq->split.vring.num -1 );@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , i);return NULL ; } +if (vq->split.buflen &&unlikely (*len > vq->split.buflen[i])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->split.buflen[i]); +return NULL ; + }/* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data;@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split, vring_split->queue_dma_addr, dma_dev); +kfree (vring_split->buflen);kfree (vring_split->desc_state);kfree (vring_split->desc_extra); } +static bool vring_needs_used_validation (const struct virtio_device *vdev) +{ +/* +* Several legacy devices are known to produce buggy used +* length. In order to let driver work, we won't validate used +* buffer length in this case. +*/ +if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 )) +return false; +if (force_used_validation) +return true; +return false; +} + static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, u32 num,@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, vring_split->vring_align = vring_align; vring_split->may_reduce_num = may_reduce_num; +if (vring_needs_used_validation (vdev)) { + vring_split->buflen = +kmalloc_array (num,sizeof (*vring_split->buflen), +GFP_KERNEL ); +if (!vring_split->buflen) + goto err_buflen; + } +return 0 ; + +err_buflen: +vring_free_split (vring_split, vdev, dma_dev); +return -ENOMEM ; } static struct virtqueue *vring_create_virtqueue_split(@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,unsigned int i, n, err_idx;u16 head, id;dma_addr_t addr; +u32 buflen =0 ; head = vq->packed.next_avail_idx; desc =alloc_indirect_packed (total_sg, gfp);@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr =cpu_to_le64 (addr); desc[i].len =cpu_to_le32 (sg->length); i++; +if (n >= out_sgs) + buflen += sg->length; } }@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].last = id; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; + vq->num_added +=1 ;pr_debug ("Added buffer head %i to %p\n" , head, vq);@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,__le16 head_flags, flags;u16 head, id, prev, curr, avail_used_flags;int err; +u32 buflen =0 ;START_USE (vq);@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,1 <<VRING_PACKED_DESC_F_AVAIL |1 <<VRING_PACKED_DESC_F_USED ; } +if (n >= out_sgs) + buflen += sg->length; } }@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].last = prev; vq->packed.desc_state[id].premapped = premapped; +/* Store in buffer length if necessary */ +if (vq->packed.buflen) + vq->packed.buflen[id] = buflen; +/* * A driver MUST NOT make the first descriptor in the list * available before all subsequent descriptors comprising @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,BAD_RING (vq,"id %u is not a head!\n" , id);return NULL ; } +if (vq->packed.buflen &&unlikely (*len > vq->packed.buflen[id])) { +BAD_RING (vq,"used len %d is larger than max in buffer len %u\n" , + *len, vq->packed.buflen[id]); +return NULL ; + }/* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data;@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->device_event_dma_addr, dma_dev); +kfree (vring_packed->buflen);kfree (vring_packed->desc_state);kfree (vring_packed->desc_extra); }@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, vring_packed->vring.num = num; +if (vring_needs_used_validation (vdev)) { + vring_packed->buflen = +kmalloc_array (num,sizeof (*vring_packed->buflen), +GFP_KERNEL ); +if (!vring_packed->buflen) + goto err; + } +return 0 ; err: --2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization