On Fri, Sep 3, 2021 at 8:11 PM Philippe Mathieu-Daudé <phi...@redhat.com> wrote: > > Per > https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538 > > The old API took the size of the memory to duplicate as a guint, > whereas most memory functions take memory sizes as a gsize. This > made it easy to accidentally pass a gsize to g_memdup(). For large > values, that would lead to a silent truncation of the size from 64 > to 32 bits, and result in a heap area being returned which is > significantly smaller than what the caller expects. This can likely > be exploited in various modules to cause a heap buffer overflow. > > Replace g_memdup() by the safer g_memdup2() wrapper. > > Signed-off-by: Philippe Mathieu-Daudé <phi...@redhat.com> > --- > Should we check in_num/out_num in range?
I'd say it is not needed to check: virtqueue_pop fills them by iterating through the descriptor chain so the range is restricted to [0, 1024]. Acked-by: Eugenio Pérez <epere...@redhat.com> > --- > hw/net/virtio-net.c | 3 ++- > hw/virtio/virtio-crypto.c | 6 +++--- > 2 files changed, 5 insertions(+), 4 deletions(-) > > diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c > index 16d20cdee52..338fbeb8c57 100644 > --- a/hw/net/virtio-net.c > +++ b/hw/net/virtio-net.c > @@ -1449,7 +1449,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, > VirtQueue *vq) > } > > iov_cnt = elem->out_num; > - iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * > elem->out_num); > + iov2 = iov = g_memdup2(elem->out_sg, > + sizeof(struct iovec) * elem->out_num); > s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl)); > iov_discard_front(&iov, &iov_cnt, sizeof(ctrl)); > if (s != sizeof(ctrl)) { > diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c > index 54f9bbb789c..59886c1790d 100644 > --- a/hw/virtio/virtio-crypto.c > +++ b/hw/virtio/virtio-crypto.c > @@ -242,7 +242,7 @@ static void virtio_crypto_handle_ctrl(VirtIODevice *vdev, > VirtQueue *vq) > } > > out_num = elem->out_num; > - out_iov_copy = g_memdup(elem->out_sg, sizeof(out_iov[0]) * out_num); > + out_iov_copy = g_memdup2(elem->out_sg, sizeof(out_iov[0]) * out_num); > out_iov = out_iov_copy; > > in_num = elem->in_num; > @@ -605,11 +605,11 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) > } > > out_num = elem->out_num; > - out_iov_copy = g_memdup(elem->out_sg, sizeof(out_iov[0]) * out_num); > + out_iov_copy = g_memdup2(elem->out_sg, sizeof(out_iov[0]) * out_num); > out_iov = out_iov_copy; > > in_num = elem->in_num; > - in_iov_copy = g_memdup(elem->in_sg, sizeof(in_iov[0]) * in_num); > + in_iov_copy = g_memdup2(elem->in_sg, sizeof(in_iov[0]) * in_num); > in_iov = in_iov_copy; > > if (unlikely(iov_to_buf(out_iov, out_num, 0, &req, sizeof(req)) > -- > 2.31.1 > >