Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp38060rwd; Wed, 31 May 2023 18:40:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5yKk1PYPGN0fgBtfIArMd7zVvVuryJlYKLpSDB6qV9LTHEKSbTuSzZyNESsxhEXTpUZktf X-Received: by 2002:a92:cc43:0:b0:328:8770:b9c2 with SMTP id t3-20020a92cc43000000b003288770b9c2mr4495253ilq.14.1685583611527; Wed, 31 May 2023 18:40:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685583611; cv=none; d=google.com; s=arc-20160816; b=tH5CWmvDsSzuzsQyGOIYPkfPO/bWTvELKOflJRS6yJi2pgragJgcjWjgVzOviAPGkn DtiiJ1OGVwQcKefN94aBU0NUK6McwEzrg1n6hNj8TxufnZQjhbwt61U1uykSOtFdNTi0 lW8FvU3/U3kyIzva3/xOZtyIW6jAuJxj8Dz4A1T5i6kBi4Nue8wmZf75MPFHktya19E8 EybTQncE8q2dRAwJd2AfmGEbYfweIr0AXTrF5d3FaP22+Es38s1cK+81jVadkNTPo87d dfRS3GP67PAxoiImvCia36vGQdFOV021Qdf6EhOgbHdJpfrpyzq50qBdAoePU+6tfUrU lz7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=/cQS+TBR31Yd90PC3CRoxT4rNUaS7lgiB5lVqL20xqk=; b=CPHmV90eDExcb03mRA+ccvF8LOAmzAr2UzIA0s0pzevulx8rL+DtdxXTaCQAiPfFpF ydSAKODBKgGqxpReNZw0K8tgYEj9EtJAOtJgXZhuNXwQ9lyV0kgU5iA+DpmE1VAraFuT U2UmAU7MQWfLg+7pJRrSKI7Sguw+rOvhK7qSY0ERr5+dj02PTCwBfPonsRc7ayTF4KYP srEmdP5cAMyv0uxVoWmJo8DBt88zo03vXJHmkQSk5i9MG14V0cYZgUVsN5yXkuR5SIaH rWKme8JsD/l73KkfzxCmqKILNzHWjV44Q6cN7wW5hDusn2TbrwKprHDYUYSwaoUgweGz Tb9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AYxWPzvw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k3-20020a628403000000b0064378fd1d54si1577939pfd.258.2023.05.31.18.39.56; Wed, 31 May 2023 18:40:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AYxWPzvw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230332AbjFABN7 (ORCPT + 99 others); Wed, 31 May 2023 21:13:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229785AbjFABN6 (ORCPT ); Wed, 31 May 2023 21:13:58 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00390125 for ; Wed, 31 May 2023 18:13:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685581988; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/cQS+TBR31Yd90PC3CRoxT4rNUaS7lgiB5lVqL20xqk=; b=AYxWPzvwmAnnjmmA8rl4SKbLhH6NC910279jHRfytmyzfOZkeCapnj0JkXSVzDRbeQtqSk RdAyI7GeuSp3D9xqjfRpzb+r4CpuVK5bOfFD+3HbG+s83xlOwK1bpMnfyiBtS/VJ/0S3Ti afET7+zDEJm3hiFmTg4YJ4go3nc/Pxk= Received: from mail-lf1-f70.google.com (mail-lf1-f70.google.com [209.85.167.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-111-WVWAjXQBOTO1fiMkwwCNyQ-1; Wed, 31 May 2023 21:13:07 -0400 X-MC-Unique: WVWAjXQBOTO1fiMkwwCNyQ-1 Received: by mail-lf1-f70.google.com with SMTP id 2adb3069b0e04-4f59c4df0f9so73551e87.1 for ; Wed, 31 May 2023 18:13:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685581985; x=1688173985; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/cQS+TBR31Yd90PC3CRoxT4rNUaS7lgiB5lVqL20xqk=; b=Wg99dZF1TzNPIH9jikRaHCEZokBnDTAGJZE5Q/0NunuGLVvoTKxU07S7PLbGyVLLnS XKWz8RyPE+RgTwtyDlraU2DH7UjG5FsJ9KqeG697Bf/DV2hO+ASoPHElCfxz2kgPqIil AtWtAy6A+hsPBajGuR6oqJY8mKmbdizKflwGspYgdqnp6XZqukcByvzrq4g6rQdvjX0X Cnt99Yt+YLGW9pKClnebgIv4FH4ez7QHNJCwNAsozPDo7ur5sT0tDEAehZRtp/waaP9s YkXlbVWKwD+E2jNWRxrEwrKNSDpUk78oXCruhuwN4To2I3Qsox2awHjZfRNZ0t6lKhDn YNDQ== X-Gm-Message-State: AC+VfDxbbT54w3acKjoWi2TcPvY7ASa4y8FWaT+dZrBkXZ0Xk2zmHgaH 54DXZcptF18sa6j66dhu0pRRUVEOZYQT/vnrYfIO9VMX92GHovahZflEkW4gs+A6+PDA0upwhIc mBetjPYA3mWBvVXwj8cKM9U7Tfkr79UfewMsHsHHpz7JCtVzlwpc= X-Received: by 2002:a2e:b16c:0:b0:2a7:96a2:7da2 with SMTP id a12-20020a2eb16c000000b002a796a27da2mr3767376ljm.49.1685581985366; Wed, 31 May 2023 18:13:05 -0700 (PDT) X-Received: by 2002:a2e:b16c:0:b0:2a7:96a2:7da2 with SMTP id a12-20020a2eb16c000000b002a796a27da2mr3767369ljm.49.1685581984887; Wed, 31 May 2023 18:13:04 -0700 (PDT) MIME-Version: 1.0 References: <20230526063041.18359-1-jasowang@redhat.com> <20230528033037-mutt-send-email-mst@kernel.org> <20230529055729-mutt-send-email-mst@kernel.org> <20230531014326-mutt-send-email-mst@kernel.org> <20230531053001-mutt-send-email-mst@kernel.org> In-Reply-To: <20230531053001-mutt-send-email-mst@kernel.org> From: Jason Wang Date: Thu, 1 Jun 2023 09:12:53 +0800 Message-ID: Subject: Re: [PATCH] virtio_ring: validate used buffer length To: "Michael S. Tsirkin" Cc: xuanzhuo@linux.alibaba.com, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 31, 2023 at 5:55=E2=80=AFPM Michael S. Tsirkin = wrote: > > On Wed, May 31, 2023 at 03:36:51PM +0800, Jason Wang wrote: > > On Wed, May 31, 2023 at 1:50=E2=80=AFPM Michael S. Tsirkin wrote: > > > > > > On Wed, May 31, 2023 at 09:05:00AM +0800, Jason Wang wrote: > > > > On Mon, May 29, 2023 at 6:03=E2=80=AFPM Michael S. Tsirkin wrote: > > > > > > > > > > On Mon, May 29, 2023 at 09:18:10AM +0800, Jason Wang wrote: > > > > > > On Sun, May 28, 2023 at 3:57=E2=80=AFPM Michael S. Tsirkin wrote: > > > > > > > > > > > > > > On Fri, May 26, 2023 at 02:30:41PM +0800, Jason Wang wrote: > > > > > > > > This patch validate > > > > > > > > > > > > > > validates > > > > > > > > > > > > > > > the used buffer length provided by the device > > > > > > > > before trying to use it. > > > > > > > > > > > > > > before returning it to caller > > > > > > > > > > > > > > > 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 t= o give us a > > > > > > > > used buffer length which is greater than we stored before. > > > > > > > > > > > > > > than what we stored > > > > > > > > > > > > > > > > > > > > > > > This validation is disable > > > > > > > > > > > > > > disabled > > > > > > > > > > > > > > > by default via module parameter to unbreak > > > > > > > > some existing devices since some legacy devices are known t= o report > > > > > > > > buggy used length. > > > > > > > > > > > > > > > > Signed-off-by: Jason Wang > > > > > > > > > > > > > > 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 len= gth > > > > > > > is wrong? > > > > > > > > > > > > Assuming the device is malicious, it would be very hard to answ= er. > > > > > > 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 i= s > > > > > > 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 ho= w > > > > > can things work? > > > > > > > > The validation is disabled for legacy device (as stated in the chan= gelog): > > > > > > > > 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 u= sed > > > > * 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: > > > > > > > > https://lore.kernel.org/all/CANLsYkxfhamUU0bb4j7y6N4_G9odKxLCjXxgXE= x4SJ6_Kf+M2Q@mail.gmail.com/T/#m31f3b06f9032beec175c312dfa2532cb08b15c56 > > > > > > > > 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? > > > > legacy rpmsg and vsock device (before 49d8c5ffad07) at least. > > > > > 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 =3D 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? > > > > > > > > > > > > > > > > > > > > Second what's wrong with dma_desc_extra that we already maint= ain? > > > > > > > Third motivation - it's part and parcel of the hardening effo= rt yes? > > > > > > > > > > > > They are different. dma_desc_extra is for a descriptor ring, bu= t this > > > > > > is for a used ring. Technically we can go back to iterate on th= e > > > > > > 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. > > > > > > > > Yes, but it is the job that is done in the driver level not the vir= tio > > > > 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. 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. > > > > > unmap is done in detach_buf_split and detach_buf_packed respectively. > > > vring_desc_extra isn't even visible outside drivers/virtio/virtio_rin= g.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 > > 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 > And then > we don't need to validate length either. Fundamentally, without > ACCESS_PLATFORM device is trusted. > > > > > > > > For drivers that do unmap at driver level - I guess they can do > > > validation there too. > > > > > > > Validation in virtio core is still necessary since they're > > > > working at different levels and it's hard to force the validation i= n > > > > 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. > > > > > > 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= . > > > > > > > Or such stuff could be added > > > > on top since the validation is by default anyway. > > > > > > > > Thanks > > > > > > > > > > > > > > > > > > > > > > > > > > I'd like to know the fate of VIRTIO_HARDEN_NOTIFICATION befor= e > > > > > > > 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 finaliz= e the > > > > > > used ring validation. > > > > > > > > > > > > Thanks > > > > > > > > > > > > > > > > > > > > > > > > > > > > --- > > > > > > > > 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(+) > > > > > > > > > > > > > > > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/= virtio_ring.c > > > > > > > > index 143f380baa1c..5b151605aaf8 100644 > > > > > > > > --- a/drivers/virtio/virtio_ring.c > > > > > > > > +++ b/drivers/virtio/virtio_ring.c > > > > > > > > @@ -15,6 +15,9 @@ > > > > > > > > #include > > > > > > > > #include > > > > > > > > > > > > > > > > +static bool force_used_validation =3D 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 valid= ation */ > > > > > > > > + 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 valid= ation */ > > > > > > > > + 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(s= truct virtqueue *_vq, > > > > > > > > unsigned int i, n, avail, descs_used, prev, err_idx; > > > > > > > > int head; > > > > > > > > bool indirect; > > > > > > > > + u32 buflen =3D 0; > > > > > > > > > > > > > > > > START_USE(vq); > > > > > > > > > > > > > > > > @@ -635,6 +645,7 @@ static inline int virtqueue_add_split(s= truct virtqueue *_vq, > > > > > > > > VRING_DE= SC_F_NEXT | > > > > > > > > VRING_DE= SC_F_WRITE, > > > > > > > > indirect= ); > > > > > > > > + buflen +=3D 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 =3D ctx= ; > > > > > > > > > > > > > > > > + /* Store in buffer length if necessary */ > > > > > > > > + if (vq->split.buflen) > > > > > > > > + vq->split.buflen[head] =3D buflen; > > > > > > > > + > > > > > > > > /* Put entry in available array (but don't update ava= il->idx until they > > > > > > > > * do sync). */ > > > > > > > > avail =3D vq->split.avail_idx_shadow & (vq->split.vri= ng.num - 1); > > > > > > > > @@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_spl= it(struct virtqueue *_vq, > > > > > > > > BAD_RING(vq, "id %u is not a head!\n", i); > > > > > > > > return NULL; > > > > > > > > } > > > > > > > > + if (vq->split.buflen && unlikely(*len > vq->split.buf= len[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 =3D 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 virti= o_device *vdev) > > > > > > > > +{ > > > > > > > > + /* > > > > > > > > + * Several legacy devices are known to produce buggy = used > > > > > > > > + * length. In order to let driver work, we won't vali= date 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(s= truct vring_virtqueue_split *vring_split, > > > > > > > > vring_split->vring_align =3D vring_align; > > > > > > > > vring_split->may_reduce_num =3D may_reduce_num; > > > > > > > > > > > > > > > > + if (vring_needs_used_validation(vdev)) { > > > > > > > > + vring_split->buflen =3D > > > > > > > > + kmalloc_array(num, sizeof(*vring_spli= t->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_pac= ked(struct vring_virtqueue *vq, > > > > > > > > unsigned int i, n, err_idx; > > > > > > > > u16 head, id; > > > > > > > > dma_addr_t addr; > > > > > > > > + u32 buflen =3D 0; > > > > > > > > > > > > > > > > head =3D vq->packed.next_avail_idx; > > > > > > > > desc =3D alloc_indirect_packed(total_sg, gfp); > > > > > > > > @@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_pac= ked(struct vring_virtqueue *vq, > > > > > > > > desc[i].addr =3D cpu_to_le64(addr); > > > > > > > > desc[i].len =3D cpu_to_le32(sg->lengt= h); > > > > > > > > i++; > > > > > > > > + if (n >=3D out_sgs) > > > > > > > > + buflen +=3D sg->length; > > > > > > > > } > > > > > > > > } > > > > > > > > > > > > > > > > @@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_pa= cked(struct vring_virtqueue *vq, > > > > > > > > vq->packed.desc_state[id].last =3D id; > > > > > > > > vq->packed.desc_state[id].premapped =3D premapped; > > > > > > > > > > > > > > > > + /* Store in buffer length if necessary */ > > > > > > > > + if (vq->packed.buflen) > > > > > > > > + vq->packed.buflen[id] =3D buflen; > > > > > > > > + > > > > > > > > vq->num_added +=3D 1; > > > > > > > > > > > > > > > > pr_debug("Added buffer head %i to %p\n", head, vq); > > > > > > > > @@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packe= d(struct virtqueue *_vq, > > > > > > > > __le16 head_flags, flags; > > > > > > > > u16 head, id, prev, curr, avail_used_flags; > > > > > > > > int err; > > > > > > > > + u32 buflen =3D 0; > > > > > > > > > > > > > > > > START_USE(vq); > > > > > > > > > > > > > > > > @@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packe= d(struct virtqueue *_vq, > > > > > > > > 1 << VRING_PACKED_DES= C_F_AVAIL | > > > > > > > > 1 << VRING_PACKED_DES= C_F_USED; > > > > > > > > } > > > > > > > > + if (n >=3D out_sgs) > > > > > > > > + buflen +=3D sg->length; > > > > > > > > } > > > > > > > > } > > > > > > > > > > > > > > > > @@ -1518,6 +1575,10 @@ static inline int virtqueue_add_pack= ed(struct virtqueue *_vq, > > > > > > > > vq->packed.desc_state[id].last =3D prev; > > > > > > > > vq->packed.desc_state[id].premapped =3D premapped; > > > > > > > > > > > > > > > > + /* Store in buffer length if necessary */ > > > > > > > > + if (vq->packed.buflen) > > > > > > > > + vq->packed.buflen[id] =3D buflen; > > > > > > > > + > > > > > > > > /* > > > > > > > > * A driver MUST NOT make the first descriptor in the= list > > > > > > > > * available before all subsequent descriptors compri= sing > > > > > > > > @@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_p= acked(struct virtqueue *_vq, > > > > > > > > BAD_RING(vq, "id %u is not a head!\n", id); > > > > > > > > return NULL; > > > > > > > > } > > > > > > > > + if (vq->packed.buflen && unlikely(*len > vq->packed.b= uflen[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 =3D 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_d= ma_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 =3D num; > > > > > > > > > > > > > > > > + if (vring_needs_used_validation(vdev)) { > > > > > > > > + vring_packed->buflen =3D > > > > > > > > + kmalloc_array(num, sizeof(*vring_pack= ed->buflen), > > > > > > > > + GFP_KERNEL); > > > > > > > > + if (!vring_packed->buflen) > > > > > > > > + goto err; > > > > > > > > + } > > > > > > > > + > > > > > > > > return 0; > > > > > > > > > > > > > > > > err: > > > > > > > > -- > > > > > > > > 2.25.1 > > > > > > > > > > > > > > > >