From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51688) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V2UvU-000208-Ax for qemu-devel@nongnu.org; Thu, 25 Jul 2013 19:26:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V2UvR-0004ox-Uo for qemu-devel@nongnu.org; Thu, 25 Jul 2013 19:26:28 -0400 Received: from mx1.redhat.com ([209.132.183.28]:2175) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V2UvR-0004ot-Ne for qemu-devel@nongnu.org; Thu, 25 Jul 2013 19:26:25 -0400 Date: Fri, 26 Jul 2013 02:27:31 +0300 From: "Michael S. Tsirkin" Message-ID: <20130725232731.GA29184@redhat.com> References: <1374759463-6351-1-git-send-email-peter.maydell@linaro.org> <1374759463-6351-2-git-send-email-peter.maydell@linaro.org> <20130725223317.GA28632@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Subject: Re: [Qemu-devel] [PATCH 1/2] hw/virtio/virtio: Don't allow guests to add/remove queues List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Maydell Cc: Anthony Liguori , KONRAD Frederic , kvmarm@lists.cs.columbia.edu, qemu-devel@nongnu.org, patches@linaro.org On Thu, Jul 25, 2013 at 11:37:22PM +0100, Peter Maydell wrote: > On 25 July 2013 23:33, Michael S. Tsirkin wrote: > > On Thu, Jul 25, 2013 at 02:37:42PM +0100, Peter Maydell wrote: > >> A queue size of 0 is used to indicate a nonexistent queue, so > >> don't allow the guest to flip a queue between zero-size and > >> non-zero-size. Don't permit setting of negative queue sizes > >> either. > >> > >> Signed-off-by: Peter Maydell > >> --- > >> hw/virtio/virtio.c | 10 +++++++--- > >> 1 file changed, 7 insertions(+), 3 deletions(-) > >> > >> diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c > >> index 09f62c6..d5b0502 100644 > >> --- a/hw/virtio/virtio.c > >> +++ b/hw/virtio/virtio.c > >> @@ -673,10 +673,14 @@ hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) > >> > >> void virtio_queue_set_num(VirtIODevice *vdev, int n, int num) > >> { > >> - if (num <= VIRTQUEUE_MAX_SIZE) { > >> - vdev->vq[n].vring.num = num; > >> - virtqueue_init(&vdev->vq[n]); > >> + if ((num == 0 && vdev->vq[n].vring.num != 0) || > >> + (num != 0 && vdev->vq[n].vring.num == 0) || > > > > Cleaner (imho) > > > > !num != !vdev->vq[n].vring.num > > I think that's more confusing, and you really don't want > "guards so we don't let the guest do bad things" to be > confusing to read. Confusing to whom? That's really subjective. You can use cast to bool or !! if you prefer. (bool)num != (bool)vdev->vq[n].vring.num Point is, most other code in this file uses (x) and !(x) and not != 0. That's objective, so please, find a way to not test ==0/!= 0. > >> + (num < 0)) { > > > > How does it ever get negative? > > If the guest maliciously writes a value with bit 31 set > to the register... > > -- PMM Make the argument unsigned then? -- MST